From: Clark C. E. <cc...@cl...> - 2006-10-18 18:54:19
|
The following code makes PyYAML treat unicode and strings as equivalent (but, it means you no longer will be using string): # this is the magic that treats all strings as unicode def represent_unicode(dumper, data): return dumper.represent_scalar("tag:yaml.org,2002:str", data) def construct_unicode(loader, node): return unicode(loader.construct_scalar(node)) yaml.add_representer(unicode, represent_unicode) yaml.add_constructor("tag:yaml.org,2002:str", construct_unicode) # simple test assert u'hi' == yaml.load(yaml.dump(u'hi')) assert u'\0x2C' == yaml.load(yaml.dump(u'\0x2C')) assert u'\0xDA' == yaml.load(yaml.dump(u'\0xDA')) Similar code could be used to convert tuples into 'tag:yaml.org,2002:seq' (aka lists) on the way out, but then you'd be loading all tuples back in as lists. Hope this helps! Clark On Wed, Oct 18, 2006 at 11:34:20AM -0400, Dimi Paun wrote: | Folks, | | I am trying to make use of PyYAML in my project, however I'm having a | problem with these !!python/* types: | * when serializing u'abc' I get !!python/unicode 'abc' | * when serializing (1, 2) I get !!python/tuple | There may be others, but I haven't enountered them yet. | | My main concern is that these things appear to have perfectly | 'standard' YAML representations, which is what I want. Having | this Python specific types in there has a number of disadvantages: | * I can not deserialize the data from non-python programs | * It is ugly to look at and may confuse users | * It exposes way too much of the implemenation behind it | | Bottom line: how can I convince PyYAML to try extra hard to use | standard YAML representations when possible? | | -- | Dimi Paun <di...@la...> | Lattica, Inc. | | | | ------------------------------------------------------------------------- | Using Tomcat but need to do more? Need to support web services, security? | Get stuff done quickly with pre-integrated technology to make your job easier | Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo | http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642 | _______________________________________________ | Yaml-core mailing list | Yam...@li... | https://lists.sourceforge.net/lists/listinfo/yaml-core |