Re:Python to get Google-backed speedup
by dfsdfsdsfdsf on Sunday June 14, 2009 @04:45PM
(#657529)
More likely:
s = "something < other"
s = s.replace("<", "<")
Even more likely:
import cgi
s = cgi.escape(s)
In Python, much string manipulation is done with builtins (especially string methods) and library functions. The latter work especially well when you target a specific domain (such as HTML) and will often be implemented in regexps. Writing one's own regexps are for when you actually need a small custom state machine. Many problems are too small or too large to justify the effort.
One perl nicety is that the VM will optimize to only compile the expression once. Python's re.sub() has to compile the expression every time, since it's just a string argument rather than a language construct. You can manually cache the result of re.compile() -- it's just not as sugary that way.
That's actually untrue. If you look in the implementation of the `re` module you would see that it caches the compilation of any regular expression.
Aha -- I almost suggested that they could do some memoization, and indeed they are doing just that. The last 100 unique patterns are compiled and stored in a dictionary for future use. You could still thrash badly if your regex "working set" is more than 100 patterns, but for most people it's probably Good Enough. (Plus you could override re._MAXCACHE if you really want.)
Thanks for pointing out my error, and getting me to read the actual code instead of speculating. :)
I disagree. Python regexen are a royal pain compared to how they work in Perl. You know what would be pretty funny is if a rogue group of guerilla programmers hacked Perl style regex match and replace syntax into Python.
Heheheh, boy would that get some bees in some bonnets on the Python dev ML. :)
Re:Python to get Google-backed speedup
by ubernostrum on Saturday June 13, 2009 @07:41PM
(#656627)
It's been done, by Andrew Dalke, using a Python extension toolkit he developed:
http://dalkescientific.com/Python/python4ply-tutorial.html#p...
He presented it at PyCon a couple years ago, response seemed to be "that's cool".
I don't think this has as much to do with your programming knowledge as your python knowledge?