Re: PyYAML and creating python objects from YAML code with YAMLObject ...

Kirill Simonov <[email protected]>
Newsgroups gmane.text.yaml.general
Message-ID <[email protected]>
Hi Markus,


On 02/03/2011 12:04 PM, Markus Hubig wrote:
> Hi @all,
>
> ich have a yaml file containing several documents, like this:
>
> --- !Table
> Table:         {Name: SYSTEM_PARAMETER_TABLE, Get: 10, Set: 11}
> ConfigID:      {'No': 0xfb, Type: 0x02, Status: OR, Length: 2}
> SerialNum:     {'No': 0x01, Type: 0x04, Status: WR, Length: 4}
> HWVersion:     {'No': 0x02, Type: 0x06, Status: OR, Length: 4}
> FWVersion:     {'No': 0x03, Type: 0x06, Status: OR, Length: 4}
> Baudrate:      {'No': 0x04, Type: 0x02, Status: WR, Length: 2}
> ModuleName:    {'No': 0x05, Type: 0x80, Status: OR, Length: 16}
> ModuleCode:    {'No': 0x06, Type: 0x02, Status: OR, Length: 2}
> ModuleInfo1:   {'No': 0x07, Type: 0x00, Status: WR, Length: 1}
> ModuleInfo2:   {'No': 0x08, Type: 0x00, Status: WR, Length: 1}

[...]

> and I wanna use this 'tables' to construct nested python classes by
> subclassing 'YAMLObject'.
> My first attempt was like this:
>
> class Row(object):
>      def __init__(self, *args, **kwargs):
>          self.__dict__.update(kwargs)
>      def __repr__(self):
>          return "%s" % self.__class__.__name__
> class Table(YAMLObject):
> """Spezialized Class for wraping YAML Data structures to python objects"""
>      yaml_tag = u'!Table'
>      def __init__(self, *args, **kwargs):
[...]

PyYAML follows pickle protocol when serializing and deserializing 
YAMLObject instances, which means it never calls the object constructor. 
  However you can define __getstate__ and/or __setstate__ methods. 
`__getstate__()` takes no arguments and returns a dictionary of object 
attributes, `__setstate__()` takes one argument, the said dictionary, 
and initializes the object.

See,
http://docs.python.org/library/pickle.html#object.__getstate__

Hope it helps, let me know if it's still not clear to you.

Kirill

------------------------------------------------------------------------------
The modern datacenter depends on network connectivity to access resources
and provide services. The best practices for maximizing a physical server's
connectivity to a physical network are well understood - see how these
rules translate into the virtual world? 
http://p.sf.net/sfu/oracle-sfdevnlfb
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.