Re: pickling problem

2007-06-07 Thread Pomato

Brian Blais wrote:
> Hello,
>
> I have a somewhat nested dict that I want to pickle, but it (sometimes) 
> contains some
> no-no's  (specifically, in this case, functions).  I know I can't pickle 
> them, but I
> would like to be able to pickle the rest of the dict.  Is there a good way to 
> be able
> to walk through a dict, and take out all of the non-pickleable objects?  I 
> could
> replace them with something else (a tag of some sort, for me to reconstruct 
> things
> later).
>
>
>   thanks,
>
>   Brian Blais
>
> --
> -
>
>   [EMAIL PROTECTED]
>   http://web.bryant.edu/~bblais

One way to do that could be:

>> d = {1:functionA, 2:functionB, 3:"String"}
>> for key in d:
>> if callable(key): d[key] = "tag"

That would work for the functions in your dictionary.

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


Re: compressing consecutive spaces

2007-07-09 Thread Pomato
On Jul 9, 7:38 am, Beliavsky <[EMAIL PROTECTED]> wrote:
> How can I replace multiple consecutive spaces in a file with a single
> character (usually a space, but maybe a comma if converting to a CSV
> file)? Ideally, the Python program would not compress consecutive
> spaces inside single or double quotes. An inelegant method is to
> repeatedly replace two consecutive spaces with one.

You can use re.sub():
re.sub(pattern, substitution, string)

import re
re.sub(r'\ +', ' ', 'foo bar')


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