News for hackers. Stuff that matters.
Re:Python to get Google-backed speedup by blhack on Saturday June 13, 2009 @05:42PM (#656529)
It probably has a lot to do with my lack of programming knowledge (I've never taken any formal programming classes [err...I took java in college but never actually went to the scheduled class because it was a lot of covering what these cRaZy things called variables were])...

A replacement on python looks like this...

foo = re.compile("<") bar = foo.replace("&gt;", bar)

this works, but it just feels less awkward to me than

$bar = s/</&gt;/

Re:Python to get Google-backed speedup by llimllib on Saturday June 13, 2009 @06:00PM (#656546)
hmmm, I would write the equivalent to your perl as:

    >>> re.sub("<", "&lt;", "something < other")
    'something &lt; other'
which seems much more readable and consistent to me than s/a/b, though the s/ syntax is great for vim users like me.

I don't think this has as much to do with your programming knowledge as your python knowledge?

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("<", "&lt;") 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.

Re:Python to get Google-backed speedup by CUViper on Saturday June 13, 2009 @06:47PM (#656589)
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.
Re:Python to get Google-backed speedup by amix on Saturday June 13, 2009 @07:27PM (#656616)
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.
Re:Python to get Google-backed speedup by CUViper on Saturday June 13, 2009 @09:30PM (#656677)
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. :)

Re:Python to get Google-backed speedup by granular on Saturday June 13, 2009 @06:38PM (#656581)
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".