Re: how to convert string function to string method?

2009-12-08 Thread Bruno Desthuilliers
Dr. Phillip M. Feldman a écrit : Bruno- You've made some excellent suggestions, and I'm always grateful for the opportunity to learn. Glad to know I've been of any help !-) My revised code appears below. Philllip def strip_pairs(s, open='([{\'"', close=')]}\'"'): """ OVERVIEW Thi

Re: how to convert string function to string method?

2009-12-07 Thread Dr. Phillip M. Feldman
nd end with matching characters, there are no # more pairs to be stripped: if s[-1] != close[i]: break # Strip the first and last character from `s`: s= s[1:-1] return s -- View this message in context: http://old.nabble.com/how-to-convert-string-function-to-strin

Re: how to convert string function to string method?

2009-12-07 Thread Terry Reedy
Steven D'Aprano wrote: On Sun, 06 Dec 2009 22:47:48 -0800, Dr. Phillip M. Feldman wrote: I wrote a handy-dandy function (see below) called "strip_pairs" for stripping matching pairs of characters from the beginning and end of a string. This function works, but I would like to be able to invoke

Re: how to convert string function to string method?

2009-12-07 Thread Bruno Desthuilliers
r0g a écrit : (snip) > I've never tried it but I think it is possible to inject new methods into existing classes, see... Doesn't work for most builtin types - for both performances and sanity reasons. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to convert string function to string method?

2009-12-07 Thread Steven D'Aprano
On Sun, 06 Dec 2009 22:47:48 -0800, Dr. Phillip M. Feldman wrote: > I wrote a handy-dandy function (see below) called "strip_pairs" for > stripping matching pairs of characters from the beginning and end of a > string. This function works, but I would like to be able to invoke it > as a string me

Re: how to convert string function to string method?

2009-12-07 Thread Bruno Desthuilliers
Dr. Phillip M. Feldman a écrit : I wrote a handy-dandy function (see below) called "strip_pairs" for stripping matching pairs of characters from the beginning and end of a string. This function works, but I would like to be able to invoke it as a string method rather than as a function. Is this

Re: how to convert string function to string method?

2009-12-07 Thread r0g
Dr. Phillip M. Feldman wrote: > I wrote a handy-dandy function (see below) called "strip_pairs" for stripping > matching pairs of characters from the beginning and end of a string. This > function works, but I would like to be able to invoke it as a string method > rather than as a function. Is t

Re: how to convert string function to string method?

2009-12-07 Thread Peter Otten
Dr. Phillip M. Feldman wrote: > I wrote a handy-dandy function (see below) called "strip_pairs" for > stripping > matching pairs of characters from the beginning and end of a string. This > function works, but I would like to be able to invoke it as a string > method > rather than as a function.

Re: how to convert string function to string method?

2009-12-06 Thread Stephen Hansen
On Sun, Dec 6, 2009 at 10:47 PM, Dr. Phillip M. Feldman < pfeld...@verizon.net> wrote: > > I wrote a handy-dandy function (see below) called "strip_pairs" for > stripping > matching pairs of characters from the beginning and end of a string. This > function works, but I would like to be able to i

how to convert string function to string method?

2009-12-06 Thread Dr. Phillip M. Feldman
if s[-1] != close[i]: break # Strip the first and last character from `s`: s= s[1:-1] return s -- View this message in context: http://old.nabble.com/how-to-convert-string-function-to-string-method--tp26673209p26673209.html Sent from the Python - python-list mailing list archive at Nabble.com. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to convert string to number?

2007-10-16 Thread Tim Chase
> I am new to Python and find it very interesting welcome to the wonderful world of Python :) > so I decided to try to port a big project from matlab to > python. To prove the value of the python, I need to find an > python way to do it. A good exercise for learning Python. > The input file c

RE: how to convert string to number?

2007-10-16 Thread wang frank
> CC: python-list@python.org> Subject: Re: how to convert string to > number?> > > I have struggling to efficiently convert a string list to> > > number. Here is my problem. I have a file that contains lines> > such as:> > > > > data_1 1 1 2 3.5>

RE: how to convert string to number?

2007-10-16 Thread wang frank
It seems that I have problem with the python-list, so I resend this. Thanks Frank From: [EMAIL PROTECTED]: [EMAIL PROTECTED]: how to convert string to number?Date: Tue, 16 Oct 2007 18:15:06 + Hi, I have struggling to efficiently convert a string list to number. Here is my problem. I

Re: how to convert string to number?

2007-10-16 Thread Tim Chase
> I have struggling to efficiently convert a string list to > number. Here is my problem. I have a file that contains lines > such as: > > data_1 1 1 2 3.5 > > After I read the data from the file by using readlines(), each > line contains a string. I use the re moduel to split the line > into ['d

how to convert string to number?

2007-10-16 Thread wang frank
Hi, I have struggling to efficiently convert a string list to number. Here is my problem. I have a file that contains lines such as: data_1 1 1 2 3.5 After I read the data from the file by using readlines(), each line contains a string. I use the re moduel to split the line into ['data_1',

Re: how to convert string

2006-04-06 Thread Fulvio
Alle 18:21, giovedì 06 aprile 2006, Fredrik Lundh ha scritto: > will work, as long as the message isn't too long I was trying some print"\b\b\b\b", i, For a number of 4 digit, but I think I miscalculated some lenght variation. The reason of this is because it won't leave previous printing.

Re: how to convert string

2006-04-06 Thread Fredrik Lundh
"Fulvio" <[EMAIL PROTECTED]> wrote: > BTW, how to write a number repeatly in the same line and position, without let > the printout to scroll down. for i in range(100): print "\r", i, # do something print will work, as long as the message isn't too long, and you're printi

Re: how to convert string

2006-04-06 Thread Fulvio
Alle 08:51, giovedì 06 aprile 2006, [EMAIL PROTECTED] ha scritto: > for x in range(10): > sys.stdout.write(x) > sys.stdout.write(" ") BTW, how to write a number repeatly in the same line and position, without let the printout to scroll down. F -- http://mail.python.org/mailman/l

Re: how to convert string

2006-04-05 Thread Azolex
a couple more exotic variations print (10 * "%s ") % tuple(range(10)) print filter(lambda x : x not in "[,]",str(range(10))) -- http://mail.python.org/mailman/listinfo/python-list

Re: how to convert string

2006-04-05 Thread swami426
Try this for x in range(10): sys.stdout.write(x) sys.stdout.write(" ") 0 1 2 3 4 5 6 7 8 9 [EMAIL PROTECTED] wrote: > I want to print number 0 to 9 in one line like this > 0 1 2 3 4 5 6 7 8 9 > > if I do like this, it prints in different lines > > for i in xrange(10): > pr

Re: how to convert string

2006-04-05 Thread Recoruds
for i in xrange(10): print i, this could be fine. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to convert string

2006-04-05 Thread Ben C
On 2006-04-05, Scott David Daniels <[EMAIL PROTECTED]> wrote: > Ben C wrote: >> ... But this puts an extra space on the end (so did the print i, >> version above). > Actually, no (the trailing-comma prints do a funny dance). > Check it out: [...] You're right, I tried it! Thanks for that. Useful,

Re: how to convert string

2006-04-05 Thread Peter Hansen
[EMAIL PROTECTED] wrote: > I want to print number 0 to 9 in one line like this > 0 1 2 3 4 5 6 7 8 9 > > if I do like this, it prints in different lines > > for i in xrange(10): > print i > > so i tried like this > > str = "" > for i in xrange(10): > str = i + " " > print str > > but i

Re: how to convert string

2006-04-05 Thread Scott David Daniels
Ben C wrote: > ... But this puts an extra space on the end (so did the print i, > version above). Actually, no (the trailing-comma prints do a funny dance). Check it out: from StringIO import StringIO dest = StringIO() for i in range(10): print >>dest, i, print >>dest

Re: how to convert string

2006-04-05 Thread Ben C
On 2006-04-05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > I want to print number 0 to 9 in one line like this > 0 1 2 3 4 5 6 7 8 9 > > if I do like this, it prints in different lines > > for i in xrange(10): > print i for i in xrange(10): print i, should work (comma after the i). >

Re: how to convert string

2006-04-05 Thread Gary Herron
[EMAIL PROTECTED] wrote: >I want to print number 0 to 9 in one line like this >0 1 2 3 4 5 6 7 8 9 > >if I do like this, it prints in different lines > >for i in xrange(10): >print i > > A comma at the end of the print will do what you want: for i in xrange(10): print i, >so i

Re: how to convert string

2006-04-05 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I want to print number 0 to 9 in one line like this > 0 1 2 3 4 5 6 7 8 9 > > if I do like this, it prints in different lines > > for i in xrange(10): > print i > > so i tried like this > > str = "" > for i in xrange(10): > str = i + " " > print str > > but i wan

Re: how to convert string

2006-04-05 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: > I want to print number 0 to 9 in one line like this > 0 1 2 3 4 5 6 7 8 9 > > if I do like this, it prints in different lines > > for i in xrange(10): > print i for i in xrange(10): print i, > so i tried like this > > str = "" > for i in xrange(10): > st

how to convert string

2006-04-05 Thread diffuser78
I want to print number 0 to 9 in one line like this 0 1 2 3 4 5 6 7 8 9 if I do like this, it prints in different lines for i in xrange(10): print i so i tried like this str = "" for i in xrange(10): str = i + " " print str but i want to know how convert int i to string. Every help is

Re: how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Ganesan Rajagopal
> "Fredrik" == Fredrik Lundh <[EMAIL PROTECTED]> writes: > Ganesan Rajagopal wrote: >>> unicodeStrFromNetwork = '\u5927' >>> unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) >> >> How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,)) > unicodeStrFromNetwork = "' +

Re: how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Fredrik Lundh
Ganesan Rajagopal wrote: >> unicodeStrFromNetwork = '\u5927' >> unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) > > How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,)) unicodeStrFromNetwork = "' + str(__import__('os').system('really bad idea')) + '" -- http:

Re: how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Ganesan Rajagopal
> "Chris" == Chris Song <[EMAIL PROTECTED]> writes: > Here's my solution > _unicodeRe = re.compile("(\\\u[\da-f]{4})") > def unisub(mo): > return unichr(int(mo.group(1)[2:],16)) > unicodeStrFromNetwork = '\u5927' > unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) How about

Re: how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Leif K-Brooks
Chris Song wrote: > Here's my solution > > _unicodeRe = re.compile("(\\\u[\da-f]{4})") > def unisub(mo): > return unichr(int(mo.group(1)[2:],16)) > > unicodeStrFromNetwork = '\u5927' > unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) > > But I think there must be a more straigh

how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Chris Song
Here's my solution _unicodeRe = re.compile("(\\\u[\da-f]{4})") def unisub(mo): return unichr(int(mo.group(1)[2:],16)) unicodeStrFromNetwork = '\u5927' unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) But I think there must be a more straightforward way to do it. -- http://m

how to convert string like '\u5927' to unicode string u'\u5927'

2005-12-27 Thread Chris Song
Here's my solution _unicodeRe = re.compile("(\\\u[\da-f]{4})") def unisub(mo): return unichr(int(mo.group(1)[2:],16)) unicodeStrFromNetwork = '\u5927' unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork) But I think there must be a more straightforward way to do it. -- http://m

Re: how to convert string to list or tuple

2005-06-02 Thread Duncan Booth
Ruud de Jong wrote: > Steven Bethard schreef: >> But unless the person eval-ing your code *only* writes immaculate >> code I can see that you can probably screw them. ;) I wonder why >> __subclasses__ isn't a restricted attribute... Is it ever used for >> something that isn't evil? ;) >> >> S

Re: how to convert string to list or tuple

2005-06-01 Thread Ruud de Jong
Steven Bethard schreef: > But unless the person eval-ing your code *only* writes immaculate code I > can see that you can probably screw them. ;) I wonder why > __subclasses__ isn't a restricted attribute... Is it ever used for > something that isn't evil? ;) > > STeVe Completely off topic,

Re: how to convert string to list or tuple

2005-06-01 Thread Steven Bethard
Duncan Booth wrote: > Steven Bethard wrote: > > >>Interestingly, I don't seem to be able to create a file object as a >>class attribute in restricted mode: >> >>py> class C(object): >>... def __init__(self): >>... self.f = file('temp.txt', 'w') >>... >>py> eval('''[ cls for cls in >>

Re: how to convert string to list or tuple

2005-06-01 Thread Fuzzyman
flyaflya wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Probably a bit late... but there's always listquote - It's part of the pythonutils module. http://www.voidspace.org.uk/python/pythonutils.html It will turn st

Re: how to convert string to list or tuple

2005-06-01 Thread Duncan Booth
Steven Bethard wrote: > Interestingly, I don't seem to be able to create a file object as a > class attribute in restricted mode: > > py> class C(object): > ... def __init__(self): > ... self.f = file('temp.txt', 'w') > ... > py> eval('''[ cls for cls in > {}.__class__.__bases__[0]._

Re: how to convert string to list or tuple

2005-05-31 Thread Steven Bethard
Duncan Booth wrote: > e.g. Assuming that the MyDatabase class does something nasty to a file: > class MyDatabase(object): > > def __init__(self, filename): > self.filename = filename > def initialise(self): > print "Splat %s" % self.filename > eval('''[ cls for cl

Re: how to convert string to list or tuple

2005-05-31 Thread Duncan Booth
Steven Bethard wrote: > Duncan Booth wrote: >> any new style class you have defined and call any of its methods with >> whatever arguments I wish. > > Any new style class that I've defined? Or just any one I pass in as > part of dict(__builtins__=None, ...)? If the former, could you > elabora

Re: how to convert string to list or tuple

2005-05-30 Thread Steven Bethard
Duncan Booth wrote: > Steven Bethard wrote: > >>But you can try it at home if you set __builtins__ to something other >>than the default: >> >>py> eval("""__import__("os").system('echo "hello"')""", >>dict(__builtins__=None)) >>Traceback (most recent call last): >> File "", line 1, in ? >> F

Re: how to convert string to list or tuple

2005-05-30 Thread Duncan Booth
Steven Bethard wrote: >> Have you tried giving it the string '__import__("os").system("rm -rf >> *")'? [Don't try that at home children!] > > But you can try it at home if you set __builtins__ to something other > than the default: > > py> eval("""__import__("os").system('echo "hello"')""", >

Re: how to convert string to list or tuple

2005-05-29 Thread Steven Bethard
Duncan Booth wrote: > Dan Bishop wrote: >> Or if you do use eval, don't give it access to any names. [snip] >> os.system("rm -rf *") >> Traceback (most recent call last): >> File "", line 1, in ? >> File "", line 0, in ? >> NameError: name 'os' is not defined > > Have you tried giving it the s

Re: how to convert string to list or tuple

2005-05-29 Thread John Roth
"Duncan Booth" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Dan Bishop wrote: > >> Simon Brunning wrote: >>> [...] >> >> Or if you do use eval, don't give it access to any names. >> >>> [...] >> os.system("rm -rf *") >> Traceback (most recent call last): >> File "", line 1, in

Re: how to convert string to list or tuple

2005-05-29 Thread Duncan Booth
Dan Bishop wrote: > Simon Brunning wrote: >> [...] > > Or if you do use eval, don't give it access to any names. > >> [...] > os.system("rm -rf *") > Traceback (most recent call last): > File "", line 1, in ? > File "", line 0, in ? > NameError: name 'os' is not defined > Have you tried giv

Re: how to convert string to list or tuple

2005-05-29 Thread Dan Bishop
Simon Brunning wrote: > On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote: > > a = "(1,2,3)" > > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > > '2', ',', '3', ')') not (1,2,3) > > Short answer - use eval(). > > Long answer - *don't* use eval unless you are in control of the

Re: how to convert string to list or tuple

2005-05-29 Thread Steven D'Aprano
On Thu, 26 May 2005 19:53:38 +0800, flyaflya wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Others have already given some suggestions. Here are some others. You didn't say where the input string a came from. Do y

Re: how to convert string to list or tuple

2005-05-26 Thread Fredrik Lundh
"flyaflya" <[EMAIL PROTECTED]> wrote: >a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) if you trust the source, use eval(a) if you don't trust it, you can use, say tuple(int(x) for x in re.findall("\d+", a)) or, pe

Re: how to convert string to list or tuple

2005-05-26 Thread Simon Brunning
On 5/26/05, flyaflya <[EMAIL PROTECTED]> wrote: > a = "(1,2,3)" > I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', > '2', ',', '3', ')') not (1,2,3) Short answer - use eval(). Long answer - *don't* use eval unless you are in control of the source of the string that you are ev

how to convert string to list or tuple

2005-05-26 Thread flyaflya
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3) -- http://mail.python.org/mailman/listinfo/python-list