FYI - this is program uses Django but I am NOT tagging it as such because it is not a django problem. The django code is here for context
~~The Background~~
I uncovered a bug that I had in a program. In short, I am using urlparse.urlparse
to get information from a given URI and save it to a database.
The goal is to do something like this:
url = urlparse.urlparse('http://somedomain.com/yada/yada')
some_instance = Domain(address=url.netloc)
~~The Problem~~
The problem is that because of a mistake in coding, the database is full of the urlparse
object. Therefore, when recalling the instance from the database the result is a unicode string:
some_instance = Domain.objects.get(pk=XX)
some_instance.address
>>> u"ParseResult(scheme=u'http', netloc=u'www.somedomain.com', path=u'/', params='', query=u'_vsrefdom=googleppc', fragment='')"
Oops.
~~The Question~~
Clearly I need to go back and fix a number of records. What I am curious to know is if there is a good pythonic way to restore a unicode representation of an object (not the actual .__unicode__()
return) back into the object itself.
Thoughts?
I have looked around a bit on Google and StackOverflow, problem is any search I have come across deals with the output of the .__unicode__()
and not the whole representation itself.
For that you can use eval
; even though generally frowned upon, it is acceptable in this case.
>>> from urlparse import ParseResult
>>> s = u"ParseResult(scheme=u'http', netloc=u'www.somedomain.com', path=u'/', params='', query=u'_vsrefdom=googleppc', fragment='')"
>>> pr = eval(s)
>>> pr.scheme, pr.netloc
(u'http', u'www.somedomain.com')