Category Archives: Python
How to split a string csv-style in Python
So, this is easy:
>>> ‘foo,bar’.split(’,')
[’foo’, ‘bar’]
But here’s the problem:
>>> "foo,bar,’spam,eggs’".split(’,')
[’foo’, ‘bar’, "’spam", "eggs’"]
Sometimes you want a csv-style split without the hassle of actually using Python’s otherwise excellent full-blown csv parser module.
So I wrote something that lets you do this:
>>> from csvsplit import csvsplit
>>> csvsplit(’foo,bar,"spam,eggs"’)
[’foo’, ‘bar’, ’spam,eggs’]
>>> csvsplit(’foo,bar,"spam,eggs"’, delimiter=’\t’)
[’foo,bar,"spam,eggs"’]
>>> csvsplit(’foo\tbar\t"spam\t\teggs"’, delimiter=’\t’)
[’foo’, ‘bar’, ’spam\t\teggs’]
Download it here. Enjoy!
WingIDE Colorpicker
Dun dun dun! This is a plugin for WingIDE. When editing code, I got tired of constantly googling “color picker” when I needed to make a hex color for my CSS.
CherryPy v. Pylons
Over various pints of beer, emails, and late-night twitter tweets, I’ve alone and with others wondered about whether a smart, well-adjusted programmer would use Pylons or CherryPy for all his web programming needs (and whether such a programmer would take the time to convert from CherryPy to Pylons). Pylons is newish to me, but I’ve [...]
Patch for meld to show multiple comparisons
I really like meld as a visual diff tool, but I wanted to have have it show multiple comparisons at once from the commandline. In the GUI, you can create multiple tabs for multiple comparisons, but there is (was) no way to do it directly from the command line. So, I wrote a patch that [...]
MySQL (MySQLdb) sessions on CherryPy
No such module existed, so I wrote one. Using it is pretty simple:
Making httplib log debug information
As opposed to having it print to stdout. Here’s how…
Worst lock acquisition code ever.
Courtesy of CherryPy:
while True:
try:
lockfd = os.open(path, os.O_CREAT|os.O_WRONLY|os.O_EXCL)
except OSError:
time.sleep(0.1)
else:
os.close(lockfd)
break
self.locked = True
Really people, [...]
Decorating your methods
You can read about Python Decorators in the PEP like I had to, or you can just skip to the good stuff.
Python decorators are a way of modifying a function, of quickly wrapping it. They’re sort of like aspect-oriented programming, in that they let you define logic that cross-cuts methods all over your program. Here’s [...]