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

Markus Hubig <[email protected]>
Newsgroups gmane.text.yaml.general
Message-ID <[email protected]>
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}

--- !Table
Table:             {Name: DEVICE_CONFIGURATION_PARAMETER_TABLE, Get: 12,
Set: 13}
ConfigID:          {'No': 0xfb, Type: 0x02, Status: OR, Length: 2}
MeasMode:          {'No': 0x01, Type: 0x00, Status: WR, Length: 1}
MeasTimes:         {'No': 0x02, Type: 0x00, Status: WR, Length: 1}
SleepTimeInModeC:  {'No': 0x03, Type: 0x04, Status: WR, Length: 4}
WaitTimeInModeB:   {'No': 0x04, Type: 0x02, Status: WR, Length: 2}
MoistMaxValue:     {'No': 0x05, Type: 0x00, Status: WR, Length: 1}
MoistMinValue:     {'No': 0x06, Type: 0x00, Status: WR, Length: 1}
TempMaxValue:      {'No': 0x07, Type: 0x01, Status: WR, Length: 1}
TempMinValue:      {'No': 0x08, Type: 0x01, Status: WR, Length: 1}
MoistAnalogAdjust: {'No': 0x09, Type: 0x00, Status: WR, Length: 1}
TempAnalogAdjust:  {'No': 0x0a, Type: 0x00, Status: WR, Length: 1}
TMsMode:           {'No': 0x0b, Type: 0x02, Status: WR, Length: 2}
EPRByteLen:        {'No': 0x0c, Type: 0x02, Status: OR, Length: 2}

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):
        for row in kwargs:
            setattr(self, row, Row(kwargs[row]))

    def __repr__(self):
        return "%s(Name=%s)" % (self.__class__.__name__, self.Table.Name)

and then using load_all to build the classes.

tables = self._yamlProjector(load_all(stream, Loader=Loader))

But that doesn't work the way I expected, course It seems that the
__init__() methode is not
executed while factorizing the Classes ... so I tryed it that way:

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'

    @classmethod
    def from_yaml(cls, loader, node):
        return Node

    @classmethod
    def to_yaml(cls, dumper, data):
        # ...
        return node

But now I'm a little lost what to do with the complex Node-Structure I get
.... how to I unpack the node
and create Row() Classes so I get a Table()-Class with nested Row()-Classes
like this:

Table.Baudrate.Length
Table.ModuleName.Status

Or the even better question would be: How to I use the from_yaml() and
to_yaml() methodes?

Thanks in advance for your help, Markus

------------------------------------------------------------------------------
Special Offer-- Download ArcSight Logger for FREE (a $49 USD value)!
Finally, a world-class log management solution at an even better price-free!
Download using promo code Free_Logger_4_Dev2Dev. Offer expires 
February 28th, so secure your free ArcSight Logger TODAY! 
http://p.sf.net/sfu/arcsight-sfd2d

_______________________________________________
Yaml-core mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/yaml-core
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.