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
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
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
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
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
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
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
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.
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
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
> 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
> 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>
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
> 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
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',
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.
"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
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
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
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
for i in xrange(10):
print i,
this could be fine.
--
http://mail.python.org/mailman/listinfo/python-list
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,
[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
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
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).
>
[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
[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
[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
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
> "Fredrik" == Fredrik Lundh <[EMAIL PROTECTED]> writes:
> Ganesan Rajagopal wrote:
>>> unicodeStrFromNetwork = '\u5927'
>>> unicodeStrNative = _unicodeRe(unisub, unicodeStrFromNetwork)
>>
>> How about unicodeStrNative = eval("u'%s'" % (unicodeStrFromNetwork,))
> unicodeStrFromNetwork = "' +
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:
> "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
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
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
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
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
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,
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
>>
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
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]._
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
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
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
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"')""",
>
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
"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
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
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
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
"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
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
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
52 matches
Mail list logo