Filtered by JavaScript, Python

Page 44

Reset

Smurl from Python

September 22, 2005
1 comment Python

If you thought the Web Service example on the about page of Smurl.name was complicated, here's a much simpler version. I use this code on my very own site for the email notifications which will contain long URLs.

Feel free to steal this code into your own projects:


from urllib import urlopen, quote

# variable 'url' is defined elsewhere
if len(url) > 80:
    url = urlopen('http://smurl.name/createSmurl?url=%s' % quote(url)).read()

Was that hard?

Python regular expression tester

September 19, 2005
1 comment Python

retest I've just discovered retest by Christof Hoeke which is a developers tool for testing and experimenting with regular expressions. It doesn't have a GUI so it uses SimpleHTTPServer to serve a web interface on http://localhost:8087 that uses AJAX to make the interface snappier. You use this if you feel uncertain how to write your regular expression syntax and need a helpful sandbox for playing in.

This is cool because as an application it's very modern. The source code is only 100 lines python code, some javascript code for the AJAX and a relatively simple HTML page. A genuine GUI app would be considerably much more code but would admittedly run faster. However, considering how "basic" this application is, speed is not an issue.

Truncated! Read the rest by clicking the link below.

Random ID generator for Zope

September 2, 2005
0 comments Python

I was working on a little application that is similar to tinyurl.com (where any URL gets a unique random id that can be used to redirect with long clumsy URL strings) and came up with this little algorithm that I wanted to share with the world for some feedback.

There are two loops, one nested inside one big loop that goes on for infinity. The inner loop creates a list of possible combinations from a sample of letters and numbers eg (abc, acb, bac, bca, cab, cba). For each such found combinations it does a check to see if this has been saved before and if not it exists the loop like this:


if not hasattr(self, combination):
    return combination

Truncated! Read the rest by clicking the link below.

\B in Python regular expressions

July 23, 2005
0 comments Python

Today I learnt about how to use the \B gadget in Python regular expressions. I've previously talked about the usefulness of \b but there's a big benefit to using \B sometimes too.

What \b does is that it is a word-boundary for alphanumerics. It allows you to find "peter" in "peter bengtsson" but not "peter" in "nickname: peterbe". In other words, all the letters have to be grouped prefixed or suffixed by a wordboundry such as newline, start-of-line, end-of-line or a non alpha character like (.

Truncated! Read the rest by clicking the link below.

SmartDict - a smart 'dict' wrapper

July 14, 2005
9 comments Python

For a work project we needed a convenient way to wrap our SQL recordset, instance objects and dictionary variables to share the same interface. The result is SmartDict which makes it possible to assert that access can be made in any which way you want; something that is very useful when you write templates and don't want to have to know if what you're working with is a dict or a recordset.

This doesn't work:


>>> d= {'name':"Peter"}
>>> print d.get('name') # fine
>>> print d.name # Error!!!

Likewise with some instance objects or record sets, this doesn't work:


>>> d = getRecordsetObject()
>>> print d.name # fine
>>> print d.get('name') # Error!!!

Truncated! Read the rest by clicking the link below.

Lisp compared to Python

July 7, 2005
1 comment Python

I was reading a thread about "Lisp development with macros faster than Python development?" on comp.lang.python when I stumbled across a little statement by Raymond Hettinger, a core Python developer:

"With Lisp or Forth, a master programmer has unlimited power and expressiveness. With Python, even a regular guy can reach for the stars."

ztar - my wrapper on tar -z

June 29, 2005
8 comments Python, Linux

Something I find myself doing very often is to download a .tar.gz or .tgz file that I want to unpack, but only in a subfolder. Some rather annoying gzips aren't collected in one folder so that when you unpack it lots of files are created in the current directory. Do you find yourself often doing this:


$ tar -ztvf Some-0.x.tar.gz
Some/file1.txt
Some/file2.txt
...
Some/file100.txt
$ tar -zxvf Some-0.x.tar.gz
Some/file1.txt
Some/file2.txt
...
Some/file100.txt

Or, in case they the gzip is badly organised:


$ tar -ztvf Foo-0.y.tar.gz
file1.txt
file2.txt
...
file100.txt
$ mkdir Foo; mv Foo-0.y.tar.gz Foo/; cd Foo/
$ tar -zxvf Foo-0.y.tar.gz
file1.txt
file2.txt
...
file100.txt
$ cd ..

Truncated! Read the rest by clicking the link below.

\b in Python regular expressions

June 14, 2005
3 comments Python

Boy did that shut me up! The \b special character i python regular expressions is so useful. I've used it before but have forgotten about it. The following code:


def createStandaloneWordRegex(word):
   """ return a regular expression that can find 'peter'
   only if it's written alone (next to space, start of 
   string, end of string, comma, etc) but not if inside 
   another word like peterbe """
   return re.compile(r"""
     (
     ^ %s
     (?=\W | $)
     |
     (?<=\W)
     %s
     (?=\W | $)
     )
     """% (re.escape(word), re.escape(word)),
           re.I|re.L|re.M|re.X)

can with the \b gadget be simplified to this:


def createStandaloneWordRegex(word):
   """ return a regular expression that can find 'peter'
   only if it's written alone (next to space, start of 
   string, end of string, comma, etc) but not if inside 
   another word like peterbe """
   return re.compile(r'\b%s\b' % word, re.I)

Quite a lot simpler isn't it? The simplified passes all the few unit tests I had.

Jacobian highlighter

May 2, 2005
2 comments Python

My friend Jacob from Galdrion taught me about "positive lookbehind assertion" and "lookahead assertion" when writing regular expressions in Python. It was new to me and I can't believe why I didn't read up on this more earlier because they're really useful. I've now got a usage for these which I use to find words that written on their own. For example, in the string "peterbe" the word "peter" doesn't exist really. You only want to find your words when they're written alone. You can't rely on it being spaces always on both sides either. You might find brackets, fullstops, end-of-string, you name it.

Enough chatting about it, I have now put together a little something that does this properly which I for no real reason call:

the Jacobian highlighter

Please give it a spin to see if it behaves like you expect it too. If you find any problems, let me know and I'll fix it and add them to the unit tests.

Next on the todo list for this is Unicode support of course.

Grep results expanded

April 23, 2005
5 comments Python, Linux

I find myself often running a grep command with the -n which means that when it shows the filename it also shows the line number it matched it on. Then what I often do is that I open the found file with my editor and move to that line with the gotoline feature. Here's a cute little bin Python script that expands the result a little bit. Try:


$ grep -rin 'def set' *      
frintranet/sequence/tests/testSequence.py:26:    def setUp( self ):
slimmer/tests/testSlimmer.py:37:    def setUp(self):
slimmer/tests/testSlimmer.py~:37:    def setUp(self):
slimmer/tests/testSlimmer.py.bak:42:    def setUp(self):

Truncated! Read the rest by clicking the link below.