Re: making a valid file name...

2006-10-18 Thread Neil Cerutti
On 2006-10-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Tim Chase: >> In practice, however, for such small strings as the given >> whitelist, the underlying find() operation likely doesn't put a >> blip on the radar. If your whitelist were some huge document >> that you were searching repeat

Re: making a valid file name...

2006-10-18 Thread bearophileHUGS
Tim Chase: > In practice, however, for such small strings as the given > whitelist, the underlying find() operation likely doesn't put a > blip on the radar. If your whitelist were some huge document > that you were searching repeatedly, it could have worse > performance. Additionally, the find()

Re: making a valid file name...

2006-10-18 Thread Fabio Chelly
You should use the s.translate() It's 100x faster: #Creates the translation table ValidChars = ":./,^0123456789abcdefghijklmnopqrstuvwxyz" InvalidChars = "".join([chr(i) for i in range(256) if not chr(i).lower() in ValidChars]) TranslationTable = "".join([chr(i) for i in range(256)]) def valid_f

Re: making a valid file name...

2006-10-18 Thread Fredrik Lundh
Matthew Warren wrote: >>> import re >>> badfilename='£"%^"£^"£$^ihgeroighroeig3645^£$^"knovin98u4#346#1461461' >>> valid=':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' >>> goodfilename=re.sub('[^'+valid+']',' ',badfilename) to create arbitrary character sets, it's usually

RE: making a valid file name...

2006-10-18 Thread Matthew Warren
> > Hi I'm writing a python script that creates directories from user > input. > Sometimes the user inputs characters that aren't valid > characters for a > file or directory name. > Here are the characters that I consider to be valid characters... > > valid = > ':./,^0123456789abcdefghijklmno

Re: making a valid file name...

2006-10-17 Thread Neil Cerutti
On 2006-10-17, Edgar Matzinger <[EMAIL PROTECTED]> wrote: > Hi, > > On 10/17/2006 06:22:45 PM, SpreadTooThin wrote: >> valid = >> ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' >> > > not specifying the OS platform, these are not all the > characters that may occur in a fil

Re: making a valid file name...

2006-10-17 Thread Tim Chase
>> If you're doing it on a time-critical basis, it might help to >> make "valid" a set, which should have O(1) membership testing, >> rather than using the "in" test with a string. I don't know >> how well the find() method of a string performs in relationship >> to "in" testing of a set. Test an

Re: making a valid file name...

2006-10-17 Thread Neil Cerutti
On 2006-10-17, Tim Chase <[EMAIL PROTECTED]> wrote: > If you're doing it on a time-critical basis, it might help to > make "valid" a set, which should have O(1) membership testing, > rather than using the "in" test with a string. I don't know > how well the find() method of a string performs in re

Re: making a valid file name...

2006-10-17 Thread Edgar Matzinger
Hi, On 10/17/2006 06:22:45 PM, SpreadTooThin wrote: > valid = > ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' > not specifying the OS platform, these are not all the characters that may occur in a filename: '[]{}-=", etc. And '/' is NOT valid. On a unix platform. And

Re: making a valid file name...

2006-10-17 Thread Tim Chase
> Sometimes the user inputs characters that aren't valid > characters for a file or directory name. Here are the > characters that I consider to be valid characters... > > valid = > ':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ' Just a caveat, as colons and slashes can gi

Re: making a valid file name...

2006-10-17 Thread Jon Clements
SpreadTooThin wrote: > Hi I'm writing a python script that creates directories from user > input. > Sometimes the user inputs characters that aren't valid characters for a > file or directory name. > Here are the characters that I consider to be valid characters... > > valid = > ':./,^0123456789a

Re: making a valid file name...

2006-10-17 Thread Jerry
I would suggest something like string.maketrans http://docs.python.org/lib/node41.html. I don't remember exactly how it works, but I think it's something like >>> invalid_chars = "abc" >>> replace_chars = "123" >>> char_map = string.maketrans(invalid_chars, replace_chars) >>> filename = "abc123.t