Python String Substitution

2006-01-26 Thread Murali
In Python, dictionaries can have any hashable value as a string. In particular I can say d = {} d[(1,2)] = "Right" d["(1,2)"] = "Wrong" d["key"] = "test" In order to print "test" using % substitution I can say print "%(key)s&q

Dynamic character substitution.

2005-09-26 Thread John Bausano
Hello all, I've been using Ansys which is a commercial FEA package which can be controlled through its own scripting language they call APDL. Now I'm trying to write some stand alone code in Python to supplement my current efforts. In Ansys I can do something like this. *do,n,1,3

Re: Python String Substitution

2006-01-26 Thread Steven D'Aprano
uot;Right" > d["(1,2)"] = "Wrong" > d["key"] = "test" > > In order to print "test" using % substitution I can say > > print "%(key)s" % d Yes, because the dictionary has a key which is the string "key". >

Re: Python String Substitution

2006-01-26 Thread John Bauman
"Murali" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > In Python, dictionaries can have any hashable value as a string. In > particular I can say > > d = {} > d[(1,2)] = "Right" > d["(1,2)"] = "Wrong" > d["ke

Re: Python String Substitution

2006-01-27 Thread Bengt Richter
On 26 Jan 2006 15:40:47 -0800, "Murali" <[EMAIL PROTECTED]> wrote: >In Python, dictionaries can have any hashable value as a string. In >particular I can say > >d = {} >d[(1,2)] = "Right" >d["(1,2)"] = "Wrong" >d["key"]

Re: Python String Substitution

2006-01-27 Thread Murali
No. I dont have a real life example. I was explaining % substitution to somebody and realized that I have only used it in the form where the keys are strings. Was wondering if there is some special syntax already part of python with which I can lookup the dictionary using a tuple as a key

Re: Python String Substitution

2006-01-27 Thread Steven D'Aprano
On Fri, 27 Jan 2006 15:54:30 -0800, Murali wrote: > No. I dont have a real life example. I was explaining % substitution to > somebody and realized that I have only used it in the form where the > keys are strings. Was wondering if there is some special syntax already > part of pytho

Re: Dynamic character substitution.

2005-09-26 Thread Tim Roberts
"John Bausano" <[EMAIL PROTECTED]> wrote: > >Hello all, > >I've been using Ansys which is a commercial FEA package which can be >controlled through its own scripting language they call APDL. Now I'm >trying to write some stand alone code in Python to supplement my current >efforts. > >In Ansys

Re: Dynamic character substitution.

2005-09-26 Thread Steve Jorgensen
On Mon, 26 Sep 2005 21:09:51 -0400, "John Bausano" <[EMAIL PROTECTED]> wrote: >Hello all, ... Funny enough, some people have wanted to substitute a more dynamic character for me on occasion . -- http://mail.python.org/mailman/listinfo/python-list

Re: Dynamic character substitution.

2005-09-29 Thread William Park
John Bausano <[EMAIL PROTECTED]> wrote: > Hello all, > > > > I've been using Ansys which is a commercial FEA package which can be > controlled through its own scripting language they call APDL. Now I'm > trying to write some stand alone code in Python to supplement my current > efforts. > >

Regular Expression for pattern substitution

2005-07-01 Thread Vibha Tripathi
It'd be silly to write the code for it if it already exists somewhere in the Python re or sre library module: I need to find and replace all strings in a text file from a certain pattern to another pattern. so for example if I see 'this(\D*)that' anywhere in the file then I'd like to make is 'tha

Re: Regular Expression for pattern substitution

2005-07-01 Thread Devan L
re.replace. I don't think there's any way to avoid it. Except maybe having an alias email address or a fake one. -- http://mail.python.org/mailman/listinfo/python-list

Re: Regular Expression for pattern substitution

2005-07-01 Thread George Sakkis
"Deval L" wrote: > re.replace. There isn't a re.replace; be careful when you reply to newbies. > "Vibha Tripathi" wrote: > > > It'd be silly to write the code for it if it already > > exists somewhere in the Python re or sre library > > module: > > > > I need to find and replace all strings in a

Re: Regular Expression for pattern substitution

2005-07-01 Thread Devan L
Hrm, thought it had one. Guess it would help if I actually used regular expression for replacement. -- http://mail.python.org/mailman/listinfo/python-list

Re: Regular Expression for pattern substitution

2005-07-02 Thread James Stroud
You might want to be a little more explicit. Do you know that this = "this" that = "that" or do you mean this = `the part before the \D*` that = `the part after the \D*` If you mean the former, then the previously proposed py> import re py> line = 'see this man with that woman holding this do

problem with newlines in regexp substitution

2006-02-23 Thread Florian Schulze
7;1','\\n',s) '\n' >>> '\\n' '\\n' >>> re.sub('1',r'\\n',s) '\\n' >>> s.replace('1','\\n') '\\n' >>> repl = '\\n' >>> re.sub('1',repl,s) '\n' >>> s.replace('1',repl) '\\n' Why is the behaviour of the regexp substitution so weird and can I prevent that? It breaks my asumptions and thus my code. Regards, Florian Schulze -- http://mail.python.org/mailman/listinfo/python-list

On eval and its substitution of globals

2005-02-19 Thread Paddy
=1,B=2)) True >>> eval(z.func_code,dict(A=1,B=2, f1=f1)) Traceback (most recent call last): File "", line 1, in ? File "", line 1, in z File "", line 1, in f1 NameError: global name 'A' is not defined >>> ENDTEST === Is there a wa

Re: problem with newlines in regexp substitution

2006-02-23 Thread James Stroud
import re >>>> s = "1" >>>> re.sub('1','\\n',s) > > '\n' > >>>> '\\n' > > '\\n' > >>>> re.sub('1',r'\\n',s) > > '\\n' > >>>>

which is better, string concatentation or substitution?

2006-05-07 Thread John Salerno
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '' + paragraph + '\n\n' vs. def p(self, paragraph): self.source += '%s\n\n' % paragraph Is there

Re: On eval and its substitution of globals

2005-02-22 Thread Paddy
in ? File "", line 1, in z File "", line 1, in f1 NameError: global name 'A' is not defined ENDTEST === Is there a way to do a deep substitution of the globals? I should add that f1 is given as-is. I can modify z, and f1 is just one of many functions given and function z is some boolean function of the f's Thanks, Pad. -- http://mail.python.org/mailman/listinfo/python-list

Re: On eval and its substitution of globals

2005-02-23 Thread Kent Johnson
dict(A=1,B=2, f1=f1)) Traceback (most recent call last): File "", line 1, in ? File "", line 1, in z File "", line 1, in f1 NameError: global name 'A' is not defined ENDTEST === Is there a way to do a deep substitution of the globals? A workaro

Re: On eval and its substitution of globals

2005-02-23 Thread Paddy
Thanks Kent for your reply. I had to do as you suggest but I was thinking either it was a kludge, and there should be a 'deep' substitution of globals, or that there was a good reason for it to work as it does and some magician would tell me. Oh, the third reason could be that it

Re: On eval and its substitution of globals

2005-02-23 Thread Leif K-Brooks
Paddy wrote: I had to do as you suggest but I was thinking either it was a kludge, and there should be a 'deep' substitution of globals, or that there was a good reason for it to work as it does and some magician would tell me. If there was deep substitution of globals, how would

Re: On eval and its substitution of globals

2005-02-23 Thread Paddy
Leif wrote: " If globals were deeply substituted when using eval, the program would presumably print "42\n24", which would be far from intuitive. If you limit the deep substitution to functions in the same module, you're creating a confusing special case. " I guess I

Re: On eval and its substitution of globals

2005-02-23 Thread Kent Johnson
Paddy wrote: I do in fact have the case you mention. I am writing a module that will manipulate functions of global variables where the functions are defined in another module. Would it be possible to have your functions take arguments instead of globals? That would seem to be a better design. Ke

Re: which is better, string concatentation or substitution?

2006-05-07 Thread Leif K-Brooks
John Salerno wrote: > My initial feeling is that concatenation might take longer than > substitution Doesn't look that way: [EMAIL PROTECTED]:~$ python -m timeit "'%s\n\n' % 'foobar'" 100 loops, best of 3: 0.6 usec per loop [EMAIL PROTECTED]:~

Re: which is better, string concatentation or substitution?

2006-05-07 Thread fuzzylollipop
niether .join() is the fastest -- http://mail.python.org/mailman/listinfo/python-list

Re: which is better, string concatentation or substitution?

2006-05-07 Thread Leif K-Brooks
fuzzylollipop wrote: > niether .join() is the fastest Please quote what you're replying to. No, it's the slowest: [EMAIL PROTECTED]:~$ python -m timeit "'%s\n\n' % 'foobar'" 100 loops, best of 3: 0.607 usec per loop [EMAIL PROTECTED]:~$ python -m timeit "'' + 'foobar' + '\n\n'" 100 loops

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Duncan Booth
Leif K-Brooks wrote: > fuzzylollipop wrote: >> niether .join() is the fastest > > Please quote what you're replying to. > > No, it's the slowest: > > [EMAIL PROTECTED]:~$ python -m timeit "'%s\n\n' % 'foobar'" > 100 loops, best of 3: 0.607 usec per loop > [EMAIL PROTECTED]:~$ python -m time

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Roy Smith
In article <[EMAIL PROTECTED]>, John Salerno <[EMAIL PROTECTED]> wrote: > My initial feeling is that concatenation might take longer than > substitution, but that it is also easier to read: > > > def p(self, paragraph): > self.source += '' + par

Re: which is better, string concatentation or substitution?

2006-05-08 Thread John Salerno
Roy Smith wrote: > One may be marginally faster, but they both require copying the source > string, and are thus both O(n). Sorry, I'm not familiar with the O(n) notation. > If you're just doing one or a small fixed > number of these, it really doesn't matter. Pick whichever one you think is

Re: which is better, string concatentation or substitution?

2006-05-08 Thread John Salerno
Duncan Booth wrote: > If you build a > list of lines to join then you don't have to repeat '\n' on the end of each > component line. How would that work? Wouldn't the last line in the list still need the newlines? -- http://mail.python.org/mailman/listinfo/python-list

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Peter Otten
John Salerno wrote: > Duncan Booth wrote: > >> If you build a >> list of lines to join then you don't have to repeat '\n' on the end of >> each component line. > > How would that work? Wouldn't the last line in the list still need the > newlines? >>> chunks = ["alpha", "beta", "gamma"] >>> "\n"

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Roy Smith
John Salerno <[EMAIL PROTECTED]> wrote: >Roy Smith wrote: > >> One may be marginally faster, but they both require copying the source >> string, and are thus both O(n). > >Sorry, I'm not familiar with the O(n) notation. OK, here's a quick tutorial to "big-oh" notation. It's a way of measuring a

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Ted
Thank you Roy. It seems if you lurk here long enough you eventually get all you questions answered without even asking! ;-) Roy Smith wrote: > John Salerno <[EMAIL PROTECTED]> wrote: > >Roy Smith wrote: > > > >> One may be marginally faster, but they both require copying the source > >> string,

Re: which is better, string concatentation or substitution?

2006-05-08 Thread John Salerno
Roy Smith wrote: > OK, here's a quick tutorial to "big-oh" notation. Wow, that was fantastic (and interesting)! Did you just write that? That should be saved somewhere! Mind if I post it on my website? (don't worry, no one goes there anyway) :) -- http://mail.python.org/mailman/listinfo/pytho

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Roy Smith
In article <[EMAIL PROTECTED]>, John Salerno <[EMAIL PROTECTED]> wrote: >Roy Smith wrote: > >> OK, here's a quick tutorial to "big-oh" notation. > >Wow, that was fantastic (and interesting)! Did you just write that? That >should be saved somewhere! Mind if I post it on my website? (don't >worry

Re: which is better, string concatentation or substitution?

2006-05-08 Thread John Salerno
Roy Smith wrote: > In article <[EMAIL PROTECTED]>, > John Salerno <[EMAIL PROTECTED]> wrote: >> Roy Smith wrote: >> >>> OK, here's a quick tutorial to "big-oh" notation. >> Wow, that was fantastic (and interesting)! Did you just write that? That >> should be saved somewhere! Mind if I post it on

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Casey Hawthorne
[EMAIL PROTECTED] (Roy Smith) wrote: >O(n^0), which is almost always written as O(1). This is a "constant >time" algorithm, one which takes the same amount of steps to execute >no matter how big the input is. For example, in python, you can >write, "x = 'foo'". That assignment statement takes t

Re: which is better, string concatentation or substitution?

2006-05-08 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Casey Hawthorne <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] (Roy Smith) wrote: > > >O(n^0), which is almost always written as O(1). This is a "constant > >time" algorithm, one which takes the same amount of steps to execute > >no matter how big the input is.

Re: which is better, string concatentation or substitution?

2006-05-12 Thread bruno at modulix
Ted wrote: > Thank you Roy. > > It seems if you lurk here long enough you eventually get all you > questions answered without even asking! > ;-) > +1 QOTW please avoid top-posting, and please avoid posting back a long message just to add three lines. -- bruno desthuilliers python -c "print '