Re: String to valid Python identifier

2009-08-08 Thread Дамјан Георгиевски


 Is there any easy function in the stdlib to convert any random string
 in a valid Python identifier .. possibly by replacing non-valid
 characters with _ ?
 
 I think this is fairly underspecified as a problem statement. A
 solution that would meet your specification would be

True, I was thinking that if there was an obvious solution I'd get the 
answer right away.. so it seems there's not.

I did try the string.maketrans way... it was a bit ugly so I decided to 
ask here if I'm missing something... maybe I'll try maketrans again.


ps.
by convert I didn't mean a full transformation... I was more hoping 
for the least intrusive transformation that would still leave 
discernible resemblance of the original string. 



-- 
дамјан ( http://softver.org.mk/damjan/ )

When you do things right, people won't be sure if you did anything at 
all.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String to valid Python identifier

2009-08-08 Thread Vlastimil Brom
2009/8/8 Дамјан Георгиевски gdam...@gmail.com:


 Is there any easy function in the stdlib to convert any random string
 in a valid Python identifier .. possibly by replacing non-valid
 characters with _ ?
...

 ps.
 by convert I didn't mean a full transformation... I was more hoping
 for the least intrusive transformation that would still leave
 discernible resemblance of the original string.



 --
 дамјан ( http://softver.org.mk/damjan/ )

 When you do things right, people won't be sure if you did anything at
 all.
 --
 http://mail.python.org/mailman/listinfo/python-list


Depending on the needs (speed, readability ...), the regular
expression replacement might be viable too:
 import re
 print re.sub(r[^a-zA-Z0-9_], r_, uaábc d:eé_123's - foo?!var)
a_bc_d_e__123_s___foo__var
 print re.sub(r\W, r_, uaábc d:eé_123's - foo?!var) # equivalent
a_bc_d_e__123_s___foo__var


Additionally, as with the other approaches, you also have to check for
the (illegal) starting digit and replace it or prepend some valid
prefix.
hth
 vbr
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: String to valid Python identifier

2009-08-06 Thread Martin v. Löwis
 Is there any easy function in the stdlib to convert any random string in 
 a valid Python identifier .. possibly by replacing non-valid characters 
 with _ ?

I think this is fairly underspecified as a problem statement. A solution
that would meet your specification would be

def mkident(s):
return foo

It returns a valid Python identifier for any random string.

If you now complain that this gives too many collisions, I propose

def mkident(s):
return foo%d % (hash(s)  0x7fff)

Regards,
Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


String to valid Python identifier

2009-08-05 Thread Дамјан Георгиевски
Is there any easy function in the stdlib to convert any random string in 
a valid Python identifier .. possibly by replacing non-valid characters 
with _ ?

Python 2.x only, so no need to do Unicode.


-- 
дамјан ( http://softver.org.mk/damjan/ )

Religion ends and philosophy begins, 
just as alchemy ends and chemistry begins 
and astrology ends, and astronomy begins.
-- 
http://mail.python.org/mailman/listinfo/python-list