Friday, March 28, 2008

I miss working closely with other developers; that's part of why I'm blogging right now.
Python exacerbates that, because there's so many ways to do things, particularly the kinds of things I'm doing right now, like parsing text files with tables in human-readable but not-particularly-computer-readable formats.

Here's a tiny example: I needed to remove commas from strings like "1,207" before converting them to integers to graph them. I thought of using slicing, which is a powerful Python feature on lists, strings and more:
>>> string="1,207"
>>> string[:string.find(',')] + string [string.find(',')+1:]
'1207'
It got ugly fast so I looked for something else:
>>> string="1,207"
>>> string.replace(',','')
'1207'
Splits are also powerful:
>>> string = "1,207"
>>> ''.join(string.split(','))
'1207'
Of course I could also define a simple "removefrom(char, string)" function:
>>> string="1,207"
>>> def removefrom(char, string):
... return string.replace(',','')
...
>>> removefrom(',', string)
'1207'



To find good ways of doing things, I end up browsing a lot of Python code online. That's frustrating because some sites have started hosting "sample code" mostly as a way to put advertisements on-screen and in pop-ups. And I'm sure the most trivial code review would identify plenty of areas my code could be much more Python-clever.

1 comment:

ptmcg said...

If you are doing a lot of text file crunching, I'd invite you to look into giving pyparsing a look. It is a 100% Python parser development module, with a number of built-in short cuts. Pyparsing takes a more verbose and object-oriented approach to parsing than tools such as RE's or lex/yacc/PLY/simpleparse/etc., but this tends to simplify the grammar development process.

The pyparsing wiki is at http://pyparsing.wikispaces.com.

Cheers!
-- Paul

Blog Archive

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.