Filtered by JavaScript, Python

Page 47

Reset

parametrize_url() adding parameters to URLs

January 14, 2005
4 comments Python

I needed to write this little function because I need to add some parameters to a URL that I was going to open with urllib2. The benefit with this script is that it can combine a any URL with some structured parameters. The URL could potentially already contain a query string (aka CGI parameters). Here's how to use it if it was placed in a file called 'urlfixer.py':


>>> from urlfixer import parametrize_url
>>> parametrize_url('https://www.peterbe.com?some=thing',
                    any='one', tv="b b c")
'https://www.peterbe.com?some=thing&tv=b+b+c&any=one'
>>> 

Truncated! Read the rest by clicking the link below.

Running simple SQL commands on the command line

January 8, 2005
2 comments Python

In my Zope usage I use a lot of SQL stored on the filesystem. These files contain DTML syntax and parameters like ZSQL Method objects inside the ZODB. Sometimes, to test the SQL code I have in these files I want to run it on the command line. To be able to do that I've written this little Python script which is placed in my ~/bin directory in Linux. It only works with PostgreSQL at the moment but people who like the idea and prefer Oracle or MySQL could probably find where to make their changes. (The psql -f command is the ticket)

Example foo.sql:


<params>id=1
name</params>
SELECT *
FROM sometable
WHERE id = <dtml-sqlvar id type="int">
AND   name = <dtml-sqlvar name type="string">

Which is run like this:


peterbe@trillian:~ $ rundotsql.py -U peterbe testdb foo.sql

Truncated! Read the rest by clicking the link below.

Python package path when executed elsewhere

December 14, 2004
3 comments Python

I stumbled across a problem today with running a python package from a different directory than where the python files are. The package looked like this (in principle):


/home/someone/mypackage/
   __init__.py
   template.html
   foobar.py

The foobar.py script contains this code:


def foo():
   tmpl = open('template.html').read()
   return tmpl.replace('X','Y')

That's fine and works when you run the file directly. The problem comes when you run the foobar.py script from somewhere completely different (e.g. /home/mrbloggs/otherpackage/). The solution to this comes from Zope's package_home.

Truncated! Read the rest by clicking the link below.

HTML entity fixer

November 25, 2004
9 comments Python

Here's a little program I wrote recently to fix incorrectly defined characters into HTML entities. For example, this is incorrect:


<b>Bärs &amp; Öl</b>

But this is correct:


<b>B&amp;auml;rs &amp;amp; &amp;Ouml;l</b>

To demonstrate I have set up a little test page here so that you can test to convert your impure HTML content.
Run test program

Truncated! Read the rest by clicking the link below.

Add links to a text (take II)

November 10, 2004
2 comments Python

If you missed the entry about Add links to text with URLs I suggest you read that first. This script has now been improved with bug fixes thanks to David Otton and Flump Cakes.

Download: addhrefs-0.4.tgz
It does not come with a nice installer because I don't think it belongs to the generic Python library anyway. If you want to use it, copy the file addhrefs.py to somewhere useful.

Truncated! Read the rest by clicking the link below.

Urwid - curses-based UI/widget library for Python

October 30, 2004
0 comments Python, Linux

"Urwid is a curses-based UI/widget library for Python. It features fluid interface resizing, multiple text layout options, simple markup for attributes, powerful scrolling list boxes and flexible edit boxes."

I've been looking for something like this for a long time. See the screenshots to get an idea of what a curses-based UI is.

Truncated! Read the rest by clicking the link below.

Massrenaming with shell and python

October 28, 2004
5 comments Python, Linux

I had the problem today that several files in a directory started with an _ underscore character. (e.g. _red.gif). Instead of manually renaming each file used the power of shell and python to solve it. (and some help from my collegue of course). Fortunately none of the files had an underscore in the middle of the name so I could keep the command quiet simple:


$ find -iname '_*' \
| xargs python -c \
'import sys;print "\n".join(["mv %s %s"%(x, x.replace("_","")) for x in sys.argv[1:]])'\
| sh -s

Truncated! Read the rest by clicking the link below.

Python inspect module

August 16, 2004
2 comments Python

My friend Jacob Lundqvist of Galdrion today showed me a nifty little method I did not know about in Python, namely the inspect module. It allows you to find out what the name of the method was that called the method "you're in". Allow me to show an example:


import inspect

def foo():
    caller_module = inspect.stack()[1][1]
    caller_method = inspect.stack()[1][3]
    print caller_module, caller_method
    return "Something"

def bar():
    foo()

if __name__=='__main__':
   bar()

Truncated! Read the rest by clicking the link below.

Pretty print SQL script

August 6, 2004
6 comments Python

In my latest work stuff I have a custom debugger module that prints the SQL statements used to stdout. To make the debug output more readable I whipped together this quick script that pretty prints SQL statements with hopefully correct case and indentation. It converts something ugly like this:


select * from foo order by bar;

into this:


SELECT
  *
FROM
  foo
ORDER BY bar;

Try with your own SQL statements

Truncated! Read the rest by clicking the link below.