Re: What is built-in method sub

2010-01-11 Thread Phlip
trailingPattern = '(\S*)\ +?\n' line = re.sub(trailingPattern, '\\1\n', line) What happens with this? trailingPattern = '\s+$' line = re.sub(trailingPattern, '', line) I'm guessing that $ terminates \s+'s greediness without snarfing the underlying \n. Then I'm guessing that

Re: What is built-in method sub

2010-01-11 Thread John Machin
On Jan 12, 7:30 am, Jeremy wrote: > On Jan 11, 1:15 pm, "Diez B. Roggisch" wrote: > > > > > Jeremy schrieb: > > > > On Jan 11, 12:54 pm, Carl Banks wrote: > > >> On Jan 11, 11:20 am, Jeremy wrote: > > > >>> I just profiled one of my Python scripts and discovered that >99% of > > >>> the time wa

Re: What is built-in method sub

2010-01-11 Thread Steven D'Aprano
On Mon, 11 Jan 2010 13:51:48 -0800, Chris Rebert wrote: > On Mon, Jan 11, 2010 at 12:34 PM, Steven D'Aprano > wrote: >> If you can avoid regexes in favour of ordinary string methods, do so. >> In general, something like: >> >> source.replace(target, new) >> >> will potentially be much faster tha

Re: What is built-in method sub

2010-01-11 Thread Chris Rebert
On Mon, Jan 11, 2010 at 12:34 PM, Steven D'Aprano wrote: > If you can avoid regexes in favour of ordinary string methods, do so. In > general, something like: > > source.replace(target, new) > > will potentially be much faster than: > > regex = re.compile(target) > regex.sub(new, source) > # equi

Re: What is built-in method sub

2010-01-11 Thread Arnaud Delobelle
Philip Semanchuk writes: > > I second the suggestion to use rstrip(), but for future reference you > should also check out the compile() function in the re module. You > might want to time the code above against a version using a compiled > regex to see how much difference it makes. The re module

Re: What is built-in method sub

2010-01-11 Thread Diez B. Roggisch
Philip Semanchuk schrieb: On Jan 11, 2010, at 3:30 PM, Jeremy wrote: On Jan 11, 1:15 pm, "Diez B. Roggisch" wrote: Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks wrote: On Jan 11, 11:20 am, Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time w

Re: What is built-in method sub

2010-01-11 Thread Grant Edwards
On 2010-01-11, Steven D'Aprano wrote: [regarding profiling results] > I think you'll find that Python's regex engine is pretty much > optimised as well as it can be, short of a major re-write. But > to quote Jamie Zawinski: > > Some people, when confronted with a problem, think "I know, >

Re: What is built-in method sub

2010-01-11 Thread Philip Semanchuk
On Jan 11, 2010, at 3:30 PM, Jeremy wrote: On Jan 11, 1:15 pm, "Diez B. Roggisch" wrote: Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks wrote: On Jan 11, 11:20 am, Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in met

Re: What is built-in method sub

2010-01-11 Thread Terry Reedy
On 1/11/2010 3:02 PM, Jeremy wrote: I am using the re.sub command to remove trailing whitespace from lines in a text file. >>> help(str.rstrip) Help on method_descriptor: rstrip(...) S.rstrip([chars]) -> str Return a copy of the string S with trailing whitespace removed. If chars

Re: What is built-in method sub

2010-01-11 Thread J
On Mon, Jan 11, 2010 at 15:02, Jeremy wrote: > I am using the re.sub command to remove trailing whitespace from lines > in a text file.  The commands I use are copied below.  If you have any > suggestions on how they could be improved, I would love to know. Just curious, but if each line is simpl

Re: What is built-in method sub

2010-01-11 Thread Steven D'Aprano
On Mon, 11 Jan 2010 11:20:34 -0800, Jeremy wrote: > I just profiled one of my Python scripts Well done! I'm not being sarcastic, or condescending, but you'd be AMAZED (or possibly not...) at how many people try to optimize their scripts *without* profiling, and end up trying to speed up parts

Re: What is built-in method sub

2010-01-11 Thread Jeremy
On Jan 11, 1:15 pm, "Diez B. Roggisch" wrote: > Jeremy schrieb: > > > > > > > On Jan 11, 12:54 pm, Carl Banks wrote: > >> On Jan 11, 11:20 am, Jeremy wrote: > > >>> I just profiled one of my Python scripts and discovered that >99% of > >>> the time was spent in > >>> {built-in method sub} > >>>

Re: What is built-in method sub

2010-01-11 Thread Stephen Hansen
On Mon, Jan 11, 2010 at 12:02 PM, Jeremy wrote: > Your guess is correct. I had forgotten that I was using that > function. > > I am using the re.sub command to remove trailing whitespace from lines > in a text file. The commands I use are copied below. If you have any > suggestions on how they

Re: What is built-in method sub

2010-01-11 Thread Diez B. Roggisch
Jeremy schrieb: On Jan 11, 12:54 pm, Carl Banks wrote: On Jan 11, 11:20 am, Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I'm guessing this is re.sub (or

Re: What is built-in method sub

2010-01-11 Thread Jeremy
On Jan 11, 12:54 pm, Carl Banks wrote: > On Jan 11, 11:20 am, Jeremy wrote: > > > I just profiled one of my Python scripts and discovered that >99% of > > the time was spent in > > > {built-in method sub} > > > What is this function and is there a way to optimize it? > > I'm guessing this is re.s

Re: What is built-in method sub

2010-01-11 Thread MRAB
Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? I think it's the subtraction operator. The only way to optimise it is to reduce the number of subtractions th

Re: What is built-in method sub

2010-01-11 Thread Matthew Barnett
Jeremy wrote: I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list

Re: What is built-in method sub

2010-01-11 Thread Carl Banks
On Jan 11, 11:20 am, Jeremy wrote: > I just profiled one of my Python scripts and discovered that >99% of > the time was spent in > > {built-in method sub} > > What is this function and is there a way to optimize it? I'm guessing this is re.sub (or, more likely, a method sub of an internal object

What is built-in method sub

2010-01-11 Thread Jeremy
I just profiled one of my Python scripts and discovered that >99% of the time was spent in {built-in method sub} What is this function and is there a way to optimize it? Thanks, Jeremy -- http://mail.python.org/mailman/listinfo/python-list