Thank you for the tip. I was actually in the middle of debugging a problem which I think may have been solved by this post. I used urlsafe_b64encode() and urlsafe_b64decode because a URL cannot contain any non-ASCII characters (can it?). I could also have used the normal b64 functions, but I wanted to be sure.
On Jan 21, 10:07 am, Torsten Bronger <bron...@physik.rwth-aachen.de> wrote: > Hallöchen! > > teth writes: > > Using str(pickle.dumps()) and thepickle.loads(str()) works > > perfectly. > > Unfortunately, I had to realise that it is not so simple due to > <http://bugs.python.org/issue2980>. Now it use the following > functions: > > def ascii_pickle(python_object): > u"""Converts an arbitrary Python object to an ASCII-only string to be > written to thedatabase. Unfortunately, even protocol 0 of Python's > ``pickle`` module may emit non-ASCII characters, see > <http://bugs.python.org/issue2980>. However, this is dangerous since the > databaseexpects Unicode strings and can't decode such octet strings. > Therefore, this routine does another encoding step using base64. This > makes debugging slightly more difficult, but there we go. > > :Parameters: > - `python_object`: the object instance to be pickled > > :type python_object: object > > :Return: > the ASCII-only string representing the object instance > > :rtype: str > """ > # FixMe: Maybe instead of base64 another encoder should be used that keeps > # the string largely readable. > return base64.b64encode(pickle.dumps(python_object, protocol=2)) > > def ascii_unpickle(string): > u"""This is the inverse function of `ascii_pickle`. > > :Parameters: > - `string`: the ASCII-onlydatabasestring to be unpickled > > :type string: str > > :Return: > the original Python object instance that was represented by the string > > :rtype: object > """ > returnpickle.loads(base64.b64decode(string)) > > Tschö, > Torsten. > > -- > Torsten Bronger, aquisgrana, europa vetus > Jabber ID: torsten.bron...@jabber.rwth-aachen.de --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---