|
|
Subscribe / Log in / New account

Python for system administrators (developerWorks)

Python for system administrators (developerWorks)

Posted Sep 7, 2007 13:01 UTC (Fri) by azazel (guest, #4363)
In reply to: Python for system administrators (developerWorks) by IkeTo
Parent article: Python for system administrators (developerWorks)

In python, with a slight markup change:

dic = {owner: 'IkeTo', date: '2007-09-07', time: '10:00 GMT', other: '3'}

message = "The document is owned by %(owner)s written at %(date)s, he/she own %(other)s other document(s) as well"

print message % dic


to post comments

Python for system administrators (developerWorks)

Posted Sep 7, 2007 13:18 UTC (Fri) by azazel (guest, #4363) [Link] (1 responses)

Ooops, i forgot to quote dic's keys, the right is:

dic = {'owner': 'IkeTo', 'date': '2007-09-07', 'time': '10:00 GMT', 'other': '3'}

another example, maintaining the same markup of the Perl example:

dic = {'owner': 'IkeTo', 'date': '2007-09-07', 'time': '10:00 GMT', 'other': '3'}

message = "The document is owned by (owner) written at (date), he/she own (other) other document(s) as well"

from pyparsing import Word, Suppress, alphas

token = Suppress('(') + Word(alphas)('label') + Suppress(')')

token.setParseAction(lambda tok: dic.get(tok.label, tok.label))

print token.transformString(message)

Python for system administrators (developerWorks)

Posted Sep 8, 2007 7:10 UTC (Sat) by IkeTo (subscriber, #2122) [Link]

Your first solution is uninteresting. My input is not tailored to Perl, your input is specifically tailored to Python. There are a lot of further customization that can be done in the original Perl code, there is essentially none that can still be done in the Python code.

Your second solution is much more interesting (even though it is not perfectly correct: you forgot to add the parentheses in case of hash miss). And thanks for letting me know about PyParsing.

Yes, regex is a simple parser. Indeed it doesn't quite have the power of PyParsing, and in complex cases regex looks real ugly. Of course, Perl people will use a library to counter it (like Parse::RecDescent), but that's not the best solution. If the language support some feature it should be support the feature well. Hope that Perl6 get traction soon, which replace regex by grammar and provide full recursive decent parser power in the language.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds