My string module doesn't have maketrans or translate functions

2013-04-11 Thread Lamb
Hi all,
 I'm really new to python and trying to figure out the basic rule and settings 
of it. I'm using python 3.3 and I was trying this code in python:

import string
s = "string. With. Punctuation?" 
out = s.translate(string.maketrans("",""), string.punctuation)

And I got the following error:
Traceback (most recent call last):
  File "C:/Python33/trials/ls.py", line 5, in 
out = s.translate(string.maketrans("",""), string.punctuation)
AttributeError: 'module' object has no attribute 'maketrans'

So I used the following to see functions inside string
e=dir(string)
>>> print(e)
['ChainMap', 'Formatter', 'Template', '_TemplateMetaclass', '__builtins__', 
'__cached__', '__doc__', '__file__', '__initializing__', '__loader__', 
'__name__', '__package__', '_re', '_string', 'ascii_letters', 
'ascii_lowercase', 'ascii_uppercase', 'capwords', 'digits', 'hexdigits', 
'octdigits', 'printable', 'punctuation', 'whitespace']

It doesn't have translate(), maketrans(), rstrip() and a whole lot of other 
things. What's the problem?

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: My string module doesn't have maketrans or translate functions

2013-04-11 Thread Lamb
Thanks! It worked! But why didn't I see functions : translate(), maketrans(), 
rstrip(), etc. listed when I called print(dir(string))?

On Thursday, April 11, 2013 11:45:05 AM UTC-4, Chris Angelico wrote:
> 
> > import string
> 
> > s = "string. With. Punctuation?"
> 
> > out = s.translate(string.maketrans("",""), string.punctuation)
> 
> 
> 
> Try this instead:
> 
> 
> 
> import string
> 
> s = "string. With. Punctuation?"
> 
> out = s.translate(str.maketrans("", "", string.punctuation))
> 
> 
> 
> Due to the changes in string handling (s is a Unicode string in Python
> 
> 3, not a string of bytes), str.translate() got changed. Check out
> 
> help(str.maketrans) and help(str.translate) in interactive Python for
> 
> more details.
> 
> 
> 
> ChrisA

-- 
http://mail.python.org/mailman/listinfo/python-list


Curried class methods?

2006-08-16 Thread Scott Lamb
I'm trying to dynamically generate class methods which have access to
some state passed in at creation time. (Basically as a workaround to
twisted's trial not having a way to dynamically add stuff. Plain
unittest seems to have TestSuite, but the trial runner doesn't know
about it.)

My first attempt might better illustrate this -

class Foo:
def generalized(self, ctx):
print 'my ctx is %r' % ctx

for i in ['a','b','c']:
setattr(Foo, i, lambda self: self.generalized(i))

foo = Foo()
foo.a()
foo.b()
foo.c()

but this prints "my ctx is c" three times; I'd hoped for a, b, c, of
course. After reading
, I
think I understand why this is - "i" doesn't actually get added to each
new function's context; they just reference the global one. Even if I
do this:

def builder():
for i in ['a','b','c']:
setattr(Foo, i, lambda self: self.generalized(i))
builder()

they'll just keep a reference to the context that was made on entry to
builder() and share it, so the modifications builder() makes to i are
reflected in all three functions.

Okay, take two. I tried this:

try:
from functional import partial
except ImportError:
...partial pasted from PEP 309...

for i in ['a','b','c']:
setattr(Foo, i, partial(Foo.generalized, ctx=i))

but when I try to call foo.a(), I get this error:

Traceback (most recent call last):
  File "./foo.py", line 35, in ?
foo.a()
  File "./foo.py", line 25, in __call__
return self.fn(*(self.args + args), **d)
TypeError: unbound method generalized() must be called with Foo
instance as first argument (got nothing instead)

If I add a debug print to partial.__call__,

print 'partial.__call__(args=%r, kw=%r, self.kw=%r)' \
  % (args, kw, self.kw)

I see:

partial.__call__(args=(), kw={}, self.kw={'ctx': 'a'})

I'd first expected foo.a() to be equivalent to Foo.a(self), but instead
it's like Foo.a(). There must be magic that does the equivalent of

class Foo:
def __init__(self):
a = partial(a, self)

for real Python functions and not for my partial object.

With this __init__ magic, I guess I have something that works. I have
to apply the partial twice, though - if I do everything in the
__init__, my new functions won't exist by the time trial's
introspection kicks in, so they'll never get called. My ugly hack has
gotten even uglier.

Does anyone know of a better way to do this?

Thanks,
Scott

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Curried class methods?

2006-08-17 Thread Scott Lamb
Thanks, Antoon and Carl. Just tried your solutions - both work and are
much cleaner than mine.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: why doesn't have this list a "reply-to" ?

2007-10-16 Thread Steve Lamb
On 2007-10-17, Byung-Hee HWANG <[EMAIL PROTECTED]> wrote:
> Just click "Ctrl-L", then you can reply to lists directly if you use
> good mailer like mutt or thunderbird or evolution ;; 

Thunderbird only if it has the list-reply patch, has either enigmail or
mhengy installed and has the reply-to-list addon installed.  Otherwise, no,
Thunderbird still, years later, lacks that feature.  Part of the reason why I
am test driving gmane in slrn right now.  :)

-- 
 Steve C. Lamb | But who decides what they dream?
   PGP Key: 1FC01004   |   And dream I do...  
---+-

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is the difference between the two kinds of brackets?

2007-10-20 Thread Steve Lamb
On 2007-10-20, James Stroud <[EMAIL PROTECTED]> wrote:
>The long of it is that there are deep computer-science 
> issues that distinguish the two and the differences become more 
> important the more you know (presumably). However, I have been 
> programming this language for 5 years, and I still can't figure out the 
> necessity for having both. I have heard all of the arguments, however 
> but none are terribly pursuasive.

The quick answer is that tuples can be indexes into directories while
lists cannot.  This doesn't seem like that big of a deal until you realize it
allows you to group related but distict bits of data together to form an index
while retaining their individual identity.  A simplistic example would be if
you had a map with x,y coordinates and you wanted to place items on that map
at their location.

[EMAIL PROTECTED]:~} python
Python 2.4.4 (#2, Aug 16 2007, 02:03:40) 
[GCC 4.1.3 20070812 (prerelease) (Debian 4.1.2-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 1
>>> y = 1
>>> coord = (x, y)
>>> map = {}
>>> map[coord] = "Something at x%s, y%s" % coord
>>> map
{(1, 1): 'Something at x1, y1'}
>>> map[(1,1)]
'Something at x1, y1'
>>> if map.has_key(coord):
... print "Found it!"
... 
>>> if map.has_key((1,1)):
... print "Found it!"
... 
Found it!
>>> for loc in map:
... print "X: %s, Y: %s - %s" % (loc[0], loc[1], map[loc])
... 
X: 1, Y: 1 - Something at x1, y1

The lists cannot be indexes to directories because they are mutable.  
[1. 1] might not be [1, 1] the next time you look for it.  (1, 1) always will.  

-- 
 Steve C. Lamb | But who decides what they dream?
   PGP Key: 1FC01004   |   And dream I do...  
---+-

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what is the difference between the two kinds of brackets?

2007-10-20 Thread Steve Lamb
On 2007-10-21, Steven D'Aprano <[EMAIL PROTECTED]> wrote:
> A note on terminology: the things inside curly brackets {} are called 
> dictionaries, or dicts, not directories. And the things you use to store 
> data in dictionaries are called keys, not indexes:

Thanks for catching that.  Kids, don't late night post while waiting for
the other computer to do its thing.

-- 
 Steve C. Lamb | But who decides what they dream?
   PGP Key: 1FC01004   |   And dream I do...  
---+-

-- 
http://mail.python.org/mailman/listinfo/python-list