Python for system administrators (developerWorks)
Python for system administrators (developerWorks)
Posted Sep 7, 2007 13:18 UTC (Fri) by azazel (guest, #4363)In reply to: Python for system administrators (developerWorks) by azazel
Parent article: Python for system administrators (developerWorks)
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)
Posted Sep 8, 2007 7:10 UTC (Sat)
by IkeTo (subscriber, #2122)
[Link]
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.
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.Python for system administrators (developerWorks)