Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming

Journal goon's Journal: Using the source in more ways than 1 - Python 2.3

Use the source:

Python has *the best* documentation out of the box. It really is up to date, useful and packed with usage examples. For the absolute latest in using Python it cannot be beat. This includes all the books, Learning Python, Programming Python [1.], etc... If you want to find out more on how to use Python download the documentation then the source code.

nb: I haven't read the Nutshell series so I can't comment. If you look at Mark's site you will see he is updating the *Learning Python* book so I also can't comment wrt to this title.

last updated: $Id: journal.html,v 1.2 2003/11/08 09:21:01 sah Exp $

optionparse:

Having had to parse options using your own code is a royal PIA. Then I found optparse. Hidden in the Python [documentation] I found how to use optparse [2.]. Sometimes I wish that the Python docs had a key word cross referenced index because you really have to look to find language keywords. The best place to look is the module index. Straight out of the usage examples ...

type into your editor....

1 # test.py 2 from optparse import OptionParser 3 parser = OptionParser(usage = "usage: %prog 0.1 -f [-h], ") 4 parser.add_option("-f","--Foo",dest="foo_opt", help="this is foo 5 option") 6 (options,args) = parser.parse_args()

now you can try something like....

$python test.py -f bar

or

$python test.py -Foo bar

Best of all the help options are automagically supplied ...

$python test.py -h

usage: test.py 0.1 -s [-f -h], options: -h, --help show this help message and exit -fFOO, --foo=FOO this is foo option $

I used this technique for walk.py [3.], so to get the options ...

$python walk.py --help

usage: test.py 0.1 -s [-f -h], options: -h, --help show this help message and exit -fFOO, --foo=FOO this is foo option $

I used this technique for walk.py [3.], so to get the options ...

$python walk.py --help

usage: walk.py 1.5 -s [-a -o -D -r -h], <source filepath> options: --version show program's version number and exit -h, --help show this help message and exit -sSRC, --source=SRC read data from filepath -rRDF, --rdf=RDF read root, directory and file options -o, --out print data in rdf format to stdout -D, --Dictionary print data in dictionary format to stdout -aATRB, --Atrb=ATRB read file attributes, time, size, extension -tTPL, --tmp=TPL read template filepath for rdf markup

it makes the app easier to use without the burden of having to write your own code. This is common in python and I see it a lot at ActiveState. One person writes a tricky bit of *foo code* solving *bar problem* and someone else points out it's already in the language. this technique allows me to for instance pass in optional arguments to return different data depending on the data. Nothing out of the normal you may say, OptionParser just allows you develop faster.

e:\shipshape>d:\python23\python.exe python walk.py-ee:\shipshape
[e:\shipshape] {shipshape} ('decode.py') ('encode.py') ('export.py') ('metadata.py') ('metadata.py.bu') ('metadata.pyc') ('test.txt') ('walk.py') ('walk.py.bu') ('walkOLD.py') [e:\shipshape\CVS] {CVS} ('Entries') ('Entries.Extra') ('Repository') ('Root') [e:\shipshape\doc] {doc} ('.journal.txt.swp') ('index.html') ('index.html.bu') ('journal.txt') [e:\shipshape\doc\CVS] {CVS} ('Entries') ('Entries.Extra') ('Repository') ('Root') [e:\shipshape\meta] {meta} ('encode.mdf') ('walk.mdf') [e:\shipshape\meta\CVS] {CVS} ('Entries') ('Entries.Extra') ('Repository') ('Root') [e:\shipshape\sql] {sql} ('allHtml.cvs.sql') ('createTblDir.sql') ('createTblFile.sql') ('createTblRoot.sql') ('Fb.sql') ('reading.sql') ('selectAll.sql') ('selectAllRatpile.qry.sql') ('selectAllRatpile.sql') ('selectRatpile.sql') ('shipshape.sql') ('shipshape3.sql') ('shipshape4.sql') ('tools.sql') [e:\shipshape\sql\CVS] {CVS} ('Entries') ('Entries.Extra') ('Repository') ('Root') [e:\shipshape\sql\test] {test} ('selectAll.csv.txt') ('selectAll.sql') ('shipshape.sql') ('shipshape.wlk') [e:\shipshape\template] {template} ('encode.tpl') ('walk.tpl') ('walk.tpl.bu') [e:\shipshape\template\CVS] {CVS} ('Entries') ('Entries.Extra') ('Repository') ('Root')

then add more file detail using the -a option over the code source tree ...

e:\shipshape> e:\shipshape>d:\python23\python.exe walk.py-ee:\shipshape -a[1,1,1]
[e:\shipshape] {shipshape} ('.test.txt.swp',12288,'2003-11-08*05:19:16','swp') ('decode.py',3321,'2003-11-01*12:04:42','py') ('encode.py',5468,'2003-11-06*23:53:05','py') ('export.py',1682,'2003-11-01*12:04:42','py') ('metadata.py',9306,'2003-11-01*12:04:42','py') ('metadata.py.bu',9306,'2003-11-01*12:04:42','bu') ('metadata.pyc',13480,'2003-11-06*09:39:41','pyc') ('test.txt',0,'2003-11-08*04:51:46','txt') ('walk.py',9266,'2003-11-08*02:10:27','py') ('walk.py.bu',9266,'2003-11-08*02:10:27','bu') ('walkOLD.py',6922,'2003-11-07*08:44:13','py') [e:\shipshape\CVS] {CVS} ('Entries',266,'2003-11-08*02:10:27',NULL) ('Entries.Extra',121,'2003-11-08*02:10:27','Extra') ('Repository',16,'2003-11-01*12:04:42',NULL) ('Root',41,'2003-11-01*12:04:42',NULL) [e:\shipshape\doc] {doc} ('.journal.txt.swp',12288,'2003-11-08*04:58:56','swp') ('index.html',20701,'2003-11-02*23:15:15','html') ('index.html.bu',20701,'2003-11-02*07:37:18','bu') ('journal.txt',1708,'2003-11-08*04:58:56','txt') [e:\shipshape\doc\CVS] {CVS} ('Entries',47,'2003-11-02*23:15:15',NULL) ('Entries.Extra',16,'2003-11-02*23:15:15','Extra') ('Repository',20,'2003-11-02*13:08:54',NULL) ('Root',41,'2003-11-02*13:08:54',NULL) [e:\shipshape\meta] {meta} ('encode.mdf',706,'2003-11-01*12:04:42','mdf') ('walk.mdf',1078,'2003-11-07*21:52:17','mdf') [e:\shipshape\meta\CVS] {CVS} ('Entries',89,'2003-11-07*21:52:17',NULL) ('Entries.Extra',30,'2003-11-07*21:52:17','Extra') ('Repository',21,'2003-11-01*12:04:42',NULL) ('Root',41,'2003-11-01*12:04:42',NULL) [e:\shipshape\sql] {sql} ('allHtml.cvs.sql',658,'2003-11-02*06:38:56','sql') ('createTblDir.sql',610,'2003-11-01*12:18:18','sql') ('createTblFile.sql',614,'2003-11-01*12:18:18','sql') ('createTblRoot.sql',560,'2003-11-01*12:18:18','sql') ('Fb.sql',8084,'2003-11-02*06:19:26','sql') ('reading.sql',711649,'2003-11-02*06:23:03','sql') ('selectAll.sql',775,'2003-11-07*00:14:35','sql') ('selectAllRatpile.qry.sql',1131,'2003-11-06*22:29:13','sql') ('selectAllRatpile.sql',873,'2003-11-06*22:12:48','sql') ('selectRatpile.sql',800,'2003-11-03*08:07:33','sql') ('shipshape.sql',10757,'2003-11-02*04:31:51','sql') ('shipshape3.sql',6038,'2003-11-07*09:15:12','sql') ('shipshape4.sql',6764,'2003-11-07*09:38:53','sql') ('tools.sql',0,'2003-11-02*06:33:18','sql') [e:\shipshape\sql\CVS] {CVS} ('Entries',202,'2003-11-07*00:14:35',NULL) ('Entries.Extra',87,'2003-11-07*00:14:35','Extra') ('Repository',20,'2003-11-01*12:04:42',NULL) ('Root',41,'2003-11-01*12:04:42',NULL) [e:\shipshape\sql\test] {test} ('selectAll.csv.txt',1946,'2003-11-02*07:25:42','txt') ('selectAll.sql',339,'2003-11-02*07:25:21','sql') ('shipshape.sql',3536,'2003-11-02*07:26:24','sql') ('shipshape.wlk',771,'2003-11-02*07:27:36','wlk') [e:\shipshape\template] {template} ('encode.tpl',744,'2003-11-01*12:04:42','tpl') ('walk.tpl',388,'2003-11-07*21:51:43','tpl') ('walk.tpl.bu',388,'2003-11-07*21:49:51','bu') [e:\shipshape\template\CVS] {CVS} ('Entries',89,'2003-11-07*21:51:43',NULL) ('Entries.Extra',30,'2003-11-07*21:51:43','Extra') ('Repository',25,'2003-11-01*12:04:42',NULL) ('Root',41,'2003-11-01*12:04:42',NULL)

the -a option extracts file attributes, file size (bytes), file creation date (with some formatting) and file extension for every file. Having the option to turn some options on/off allows for more flexible code.

eval: parse plain-text data structures

if you look at the last optional arguement I passed .... you will may recognise this is the syntax for a Python List. Instead of having to make your own template language you can use a combination of plain text *Python data structures* and eval .

e:\shipshape> e:\shipshape>d:\python23\python.exe walk.py-ee:\shipshape -a[1,1,1]

Eval allows for Python to *parse and evalute* the text expression. Mark Lutz explains in 'Learning Python' that ...

"... evaluates the specified expression (string or compiled code) in the optionally specified global and local namespaces... [4.Mark Lutz, Programming Python]"

There are problems, (performance, error handling, security) but these areas can be handled or reduced.

Plain text data structures + eval means you can parse in data structures (pending complexity) and evaluate them ... and do something using the data in an easily digestable manner.

In designing an app trade-offs have to be made. This method could be seen as a *security* problem or a *user* problem but for me it hits the right level of power and leveraging existing language.

How much more work is involved creating my own templating language, then creating a parser, validating the data, writing the documentation ..... ?

References:
=============================================

1. Mark Lutz http://starship.python.net/crew/lutz/

2. Optparse - new in version 2.3 http://www.python.org/doc/2.3.2/lib/module-getopt.html

3. Read previous journal - generic cross platform tools to aid information management http://slashdot.org/comments.pl?sid=84421

4. Mark Lutz, David Ascher Programming Python, PP 222-224 Oreilly & Associates, 1999 First Edition.

http://www.oreilly.com/ ISBN: 1-56592-464-9

This discussion has been archived. No new comments can be posted.

Using the source in more ways than 1 - Python 2.3

Comments Filter:

An authority is a person who can tell you more about something than you really care to know.

Working...