Pointer


In [1]:
import json
import sys

from konfoo import *

YAML Support


In [2]:
import oyaml as yaml

In [3]:
def to_yaml(container, *attributes, **options):
    flow_style = options.pop('flow_style', False)
    return yaml.safe_dump(container.view_fields(*attributes, **options), 
                          stream=sys.stdout, 
                          default_flow_style=flow_style)

Define a Data Object


In [4]:
class DataObject(Structure):
    def __init__(self):
        super().__init__()
        self.size = Decimal(16)
        self.item = Pointer()
        self.index_fields()

In [5]:
data_object = DataObject()

In [6]:
data_object.to_list()


Out[6]:
[('DataObject.size', 0), ('DataObject.item', '0x0')]

In [7]:
data_object.to_csv()


Out[7]:
[{'id': 'DataObject.size', 'value': 0},
 {'id': 'DataObject.item', 'value': '0x0'}]

In [8]:
data_object.to_json()


Out[8]:
'{"size": 0, "item": "0x0"}'

In [9]:
to_yaml(data_object, flow_style=False)


size: 0
item: '0x0'

In [10]:
data_object.to_list(nested=True)


Out[10]:
[('DataObject.size', 0), ('DataObject.item', '0x0')]

In [11]:
data_object.to_csv(nested=True)


Out[11]:
[{'id': 'DataObject.size', 'value': 0},
 {'id': 'DataObject.item', 'value': '0x0'}]

In [12]:
data_object.to_json(nested=True)


Out[12]:
'{"size": 0, "item": {"value": "0x0", "data": null}}'

In [13]:
to_yaml(data_object, nested=True, flow_style=False)


size: 0
item:
  value: '0x0'
  data: null

Define a Data Object Pointer


In [14]:
class DataObjectPointer(Pointer):

    def __init__(self, address=None, byte_order=BYTEORDER):
        super().__init__(template=DataObject(),
                         address=address,
                         data_order=byte_order)

In [15]:
pointer = DataObjectPointer()

In [16]:
pointer.to_list()


Out[16]:
[('DataObjectPointer.field', '0x0'),
 ('DataObjectPointer.data.size', 0),
 ('DataObjectPointer.data.item', '0x0')]

In [17]:
pointer.to_csv()


Out[17]:
[{'id': 'DataObjectPointer.field', 'value': '0x0'},
 {'id': 'DataObjectPointer.data.size', 'value': 0},
 {'id': 'DataObjectPointer.data.item', 'value': '0x0'}]

In [18]:
pointer.to_json()


Out[18]:
'{"value": "0x0", "data": {"size": 0, "item": "0x0"}}'

In [19]:
to_yaml(pointer, flow_style=False)


value: '0x0'
data:
  size: 0
  item: '0x0'

In [20]:
pointer.to_list(nested=True)


Out[20]:
[('DataObjectPointer.field', '0x0'),
 ('DataObjectPointer.data.size', 0),
 ('DataObjectPointer.data.item', '0x0')]

In [21]:
pointer.to_csv(nested=True)


Out[21]:
[{'id': 'DataObjectPointer.field', 'value': '0x0'},
 {'id': 'DataObjectPointer.data.size', 'value': 0},
 {'id': 'DataObjectPointer.data.item', 'value': '0x0'}]

In [22]:
pointer.to_json(nested=True)


Out[22]:
'{"value": "0x0", "data": {"size": 0, "item": {"value": "0x0", "data": null}}}'

In [23]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x0'
data:
  size: 0
  item:
    value: '0x0'
    data: null

Nest Pointers

Nested pointer with no data object.


In [24]:
pointer = Pointer(Pointer())

In [25]:
pointer.to_list()


Out[25]:
[('Pointer.field', '0x0'), ('Pointer.data', '0x0')]

In [26]:
pointer.to_csv()


Out[26]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data', 'value': '0x0'}]

In [27]:
pointer.to_json()


Out[27]:
'{"value": "0x0", "data": "0x0"}'

In [28]:
to_yaml(pointer, flow_style=False)


value: '0x0'
data: '0x0'

In [29]:
pointer.to_list(nested=True)


Out[29]:
[('Pointer.field', '0x0'), ('Pointer.data', '0x0')]

In [30]:
pointer.to_csv(nested=True)


Out[30]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data', 'value': '0x0'}]

In [31]:
pointer.to_json(nested=True)


Out[31]:
'{"value": "0x0", "data": {"value": "0x0", "data": null}}'

In [32]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x0'
data:
  value: '0x0'
  data: null

Nested pointer with an data object


In [33]:
pointer = Pointer(Pointer(Byte()))

In [34]:
pointer.to_list()


Out[34]:
[('Pointer.field', '0x0'), ('Pointer.data', '0x0')]

In [35]:
pointer.to_csv()


Out[35]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data', 'value': '0x0'}]

In [36]:
pointer.to_json()


Out[36]:
'{"value": "0x0", "data": "0x0"}'

In [37]:
to_yaml(pointer, flow_style=False)


value: '0x0'
data: '0x0'

In [38]:
pointer.to_list(nested=True)


Out[38]:
[('Pointer.field', '0x0'),
 ('Pointer.data', '0x0'),
 ('Pointer.data.data', '0x0')]

In [39]:
pointer.to_csv(nested=True)


Out[39]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data', 'value': '0x0'},
 {'id': 'Pointer.data.data', 'value': '0x0'}]

In [40]:
pointer.to_json(nested=True)


Out[40]:
'{"value": "0x0", "data": {"value": "0x0", "data": "0x0"}}'

In [41]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x0'
data:
  value: '0x0'
  data: '0x0'

Access the Data Object


In [42]:
pointer = Pointer(Pointer(Byte()))

In [43]:
pointer.data


Out[43]:
Pointer(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x0')

In [44]:
pointer.data.data


Out[44]:
Byte(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=8, value='0x0')

Address of the Data Object


In [45]:
pointer.address


Out[45]:
0

Byte Order of the Data Object


In [46]:
pointer.data_byte_order


Out[46]:
Byteorder.little = 'little'

In [47]:
pointer.data_byte_order = 'big'

In [48]:
pointer.data_byte_order


Out[48]:
Byteorder.big = 'big'

Byte Stream for the Data Object


In [49]:
pointer.bytestream


Out[49]:
''

In [50]:
pointer.bytestream = '01020304'

In [51]:
pointer.bytestream


Out[51]:
'01020304'

Initialize a Pointer


In [52]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String(14))))

In [53]:
pointer.to_list(nested=True)


Out[53]:
[('Pointer.field', '0x0'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [54]:
pointer.to_csv(nested=True)


Out[54]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [55]:
pointer.to_json(nested=True)


Out[55]:
'{"value": "0x0", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [56]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x0'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [57]:
pointer.initialize_fields({
    'value': 0x1,
    'data': {
        'size': 14,
        'item': {'value': 0x10,
                 'data': 'Konfoo is Fun'}}})

In [58]:
pointer.to_list(nested=True)


Out[58]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [59]:
pointer.to_csv(nested=True)


Out[59]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [60]:
pointer.to_json(nested=True)


Out[60]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [61]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: Konfoo is Fun

Display a Pointer


In [62]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String())))

In [63]:
pointer.index_fields()


Out[63]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [64]:
pointer


Out[64]:
Pointer(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x0')

Display the Data Object


In [65]:
pointer.data


Out[65]:
Structure([('size',
            Decimal(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=2, bit_offset=0), bit_size=16, value=0)),
           ('item',
            Pointer(index=Index(byte=2, bit=0, address=2, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x0'))])

Metadata of a Pointer


In [66]:
pointer.describe()


Out[66]:
OrderedDict([('address', 0),
             ('alignment', [4, 0]),
             ('class', 'Pointer'),
             ('index', [0, 0]),
             ('max', 4294967295),
             ('min', 0),
             ('name', 'Pointer'),
             ('order', 'auto'),
             ('signed', False),
             ('size', 32),
             ('type', 'Pointer'),
             ('value', '0x0'),
             ('member',
              [OrderedDict([('class', 'Structure'),
                            ('name', 'data'),
                            ('size', 2),
                            ('type', 'Structure'),
                            ('member',
                             [OrderedDict([('address', 0),
                                           ('alignment', [2, 0]),
                                           ('class', 'Decimal16'),
                                           ('index', [0, 0]),
                                           ('max', 65535),
                                           ('min', 0),
                                           ('name', 'size'),
                                           ('order', 'auto'),
                                           ('signed', False),
                                           ('size', 16),
                                           ('type', 'Field'),
                                           ('value', 0)]),
                              OrderedDict([('address', 2),
                                           ('alignment', [4, 0]),
                                           ('class', 'Pointer'),
                                           ('index', [2, 0]),
                                           ('max', 4294967295),
                                           ('min', 0),
                                           ('name', 'item'),
                                           ('order', 'auto'),
                                           ('signed', False),
                                           ('size', 32),
                                           ('type', 'Pointer'),
                                           ('value', '0x0'),
                                           ('member',
                                            [OrderedDict([('address', 0),
                                                          ('alignment',
                                                           [0, 0]),
                                                          ('class', 'String'),
                                                          ('index', [0, 0]),
                                                          ('name', 'data'),
                                                          ('order', 'auto'),
                                                          ('size', 0),
                                                          ('type', 'Field'),
                                                          ('value',
                                                           '')])])])])])])])

In [67]:
json.dump(pointer.describe(), sys.stdout, indent=2)


{
  "address": 0,
  "alignment": [
    4,
    0
  ],
  "class": "Pointer",
  "index": [
    0,
    0
  ],
  "max": 4294967295,
  "min": 0,
  "name": "Pointer",
  "order": "auto",
  "signed": false,
  "size": 32,
  "type": "Pointer",
  "value": "0x0",
  "member": [
    {
      "class": "Structure",
      "name": "data",
      "size": 2,
      "type": "Structure",
      "member": [
        {
          "address": 0,
          "alignment": [
            2,
            0
          ],
          "class": "Decimal16",
          "index": [
            0,
            0
          ],
          "max": 65535,
          "min": 0,
          "name": "size",
          "order": "auto",
          "signed": false,
          "size": 16,
          "type": "Field",
          "value": 0
        },
        {
          "address": 2,
          "alignment": [
            4,
            0
          ],
          "class": "Pointer",
          "index": [
            2,
            0
          ],
          "max": 4294967295,
          "min": 0,
          "name": "item",
          "order": "auto",
          "signed": false,
          "size": 32,
          "type": "Pointer",
          "value": "0x0",
          "member": [
            {
              "address": 0,
              "alignment": [
                0,
                0
              ],
              "class": "String",
              "index": [
                0,
                0
              ],
              "name": "data",
              "order": "auto",
              "size": 0,
              "type": "Field",
              "value": ""
            }
          ]
        }
      ]
    }
  ]
}

In [68]:
d3flare_json(pointer.describe(), sys.stdout, indent=2)


{
  "class": "Pointer",
  "name": "Pointer",
  "children": [
    {
      "class": "Pointer",
      "name": "*Pointer",
      "size": 32,
      "value": "0x0"
    },
    {
      "class": "Structure",
      "name": "data",
      "children": [
        {
          "class": "Decimal16",
          "name": "size",
          "size": 16,
          "value": 0
        },
        {
          "class": "Pointer",
          "name": "item",
          "children": [
            {
              "class": "Pointer",
              "name": "*item",
              "size": 32,
              "value": "0x0"
            },
            {
              "class": "String",
              "name": "data",
              "size": 0,
              "value": ""
            }
          ]
        }
      ]
    }
  ]
}

Size of the Data Object


In [69]:
pointer.data_size


Out[69]:
6

Indexing


In [70]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String(14))))

In [71]:
pointer.initialize_fields({
    'value': 0x1,
    'data': {
        'size': 14,
        'item': {'value': 0x10,
                 'data': 'Konfoo is Fun'}}})

In [72]:
pointer.to_list('index', nested=True)


Out[72]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [73]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[73]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 0}]

In [74]:
pointer.to_json('index', nested=True)


Out[74]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 0, 0, false], "item": {"value": [0, 0, 0, 0, false], "data": [0, 0, 0, 0, false]}}}'

In [75]:
pointer.index_fields(index=Index())


Out[75]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [76]:
pointer.index_fields()


Out[76]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [77]:
pointer.to_list('index', nested=True)


Out[77]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=1, base_address=1, update=False)),
 ('Pointer.data.item',
  Index(byte=2, bit=0, address=3, base_address=1, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [78]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[78]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 1},
 {'id': 'Pointer.data.item', 'index': 2, 'offset': 0, 'address': 3},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 0}]

In [79]:
pointer.to_json('index', nested=True)


Out[79]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 1, 1, false], "item": {"value": [2, 0, 3, 1, false], "data": [0, 0, 0, 0, false]}}}'

In [80]:
pointer.index_fields(index=Index(), nested=True)


Out[80]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [81]:
pointer.index_fields(nested=True)


Out[81]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [82]:
pointer.to_list('index', nested=True)


Out[82]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=1, base_address=1, update=False)),
 ('Pointer.data.item',
  Index(byte=2, bit=0, address=3, base_address=1, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=16, base_address=16, update=False))]

In [83]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[83]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 1},
 {'id': 'Pointer.data.item', 'index': 2, 'offset': 0, 'address': 3},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 16}]

In [84]:
pointer.to_json('index', nested=True)


Out[84]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 1, 1, false], "item": {"value": [2, 0, 3, 1, false], "data": [0, 0, 16, 16, false]}}}'

Index the Pointer Field


In [85]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String(14))))

In [86]:
pointer.initialize_fields({
    'value': 0x1,
    'data': {
        'size': 14,
        'item': {'value': 0x10,
                 'data': 'Konfoo is Fun'}}})

In [87]:
pointer.to_list('index', nested=True)


Out[87]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [88]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[88]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 0}]

In [89]:
pointer.to_json('index', nested=True)


Out[89]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 0, 0, false], "item": {"value": [0, 0, 0, 0, false], "data": [0, 0, 0, 0, false]}}}'

In [90]:
pointer.index_field(index=Index())


Out[90]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [91]:
pointer.index_field()


Out[91]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [92]:
pointer.to_list('index', nested=True)


Out[92]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [93]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[93]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 0}]

In [94]:
pointer.to_json('index', nested=True)


Out[94]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 0, 0, false], "item": {"value": [0, 0, 0, 0, false], "data": [0, 0, 0, 0, false]}}}'

Index the Data Object


In [95]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String())))

In [96]:
pointer.initialize_fields({
    'value': 0x1,
    'data': {
        'size': 14,
        'item': {'value': 0x10,
                 'data': 'Konfoo is Fun'}}})

In [97]:
pointer.to_list('index', nested=True)


Out[97]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [98]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[98]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 0}]

In [99]:
pointer.to_json('index', nested=True)


Out[99]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 0, 0, false], "item": {"value": [0, 0, 0, 0, false], "data": [0, 0, 0, 0, false]}}}'

In [100]:
pointer.index_data()

In [101]:
pointer.to_list('index', nested=True)


Out[101]:
[('Pointer.field',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Pointer.data.size',
  Index(byte=0, bit=0, address=1, base_address=1, update=False)),
 ('Pointer.data.item',
  Index(byte=2, bit=0, address=3, base_address=1, update=False)),
 ('Pointer.data.item.data',
  Index(byte=0, bit=0, address=16, base_address=16, update=False))]

In [102]:
pointer.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'), nested=True)


Out[102]:
[{'id': 'Pointer.field', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Pointer.data.size', 'index': 0, 'offset': 0, 'address': 1},
 {'id': 'Pointer.data.item', 'index': 2, 'offset': 0, 'address': 3},
 {'id': 'Pointer.data.item.data', 'index': 0, 'offset': 0, 'address': 16}]

In [103]:
pointer.to_json('index', nested=True)


Out[103]:
'{"value": [0, 0, 0, 0, false], "data": {"size": [0, 0, 1, 1, false], "item": {"value": [2, 0, 3, 1, false], "data": [0, 0, 16, 16, false]}}}'

De-Serializing


In [104]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String(14))))

In [105]:
pointer.to_list(nested=True)


Out[105]:
[('Pointer.field', '0x0'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [106]:
pointer.to_csv(nested=True)


Out[106]:
[{'id': 'Pointer.field', 'value': '0x0'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [107]:
pointer.to_json(nested=True)


Out[107]:
'{"value": "0x0", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [108]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x0'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [109]:
pointer.bytestream


Out[109]:
''

In [110]:
pointer.data.item.bytestream


Out[110]:
''

In [111]:
pointer.deserialize(bytes.fromhex('010000000e00100000004b6f6e666f6f2069732046756e00f00f00'))


Out[111]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [112]:
pointer.to_list(nested=True)


Out[112]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [113]:
pointer.to_csv(nested=True)


Out[113]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [114]:
pointer.to_json(nested=True)


Out[114]:
'{"value": "0x1", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [115]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [116]:
pointer.deserialize(bytes.fromhex('010000000e00100000004b6f6e666f6f2069732046756e00f00f00'), nested=True)


Out[116]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [117]:
pointer.to_list(nested=True)


Out[117]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [118]:
pointer.to_csv(nested=True)


Out[118]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [119]:
pointer.to_json(nested=True)


Out[119]:
'{"value": "0x1", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [120]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [121]:
pointer.bytestream = '0e0010000000'

In [122]:
pointer.data.item.bytestream = '4b6f6e666f6f2069732046756e00f00f00'

In [123]:
pointer.deserialize(bytes.fromhex('01000000'))


Out[123]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [124]:
pointer.to_list(nested=True)


Out[124]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [125]:
pointer.to_csv(nested=True)


Out[125]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [126]:
pointer.to_json(nested=True)


Out[126]:
'{"value": "0x1", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [127]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [128]:
pointer.deserialize(bytes.fromhex('01000000'), nested=True)


Out[128]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [129]:
pointer.to_list(nested=True)


Out[129]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [130]:
pointer.to_csv(nested=True)


Out[130]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [131]:
pointer.to_json(nested=True)


Out[131]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [132]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: Konfoo is Fun

Deserialize the Data Object


In [133]:
pointer = Pointer(
    Structure(
        size=Decimal(16),
        item=Pointer(String(14))), 1)

In [134]:
pointer.to_list(nested=True)


Out[134]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 0),
 ('Pointer.data.item', '0x0'),
 ('Pointer.data.item.data', '')]

In [135]:
pointer.to_csv(nested=True)


Out[135]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 0},
 {'id': 'Pointer.data.item', 'value': '0x0'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [136]:
pointer.to_json(nested=True)


Out[136]:
'{"value": "0x1", "data": {"size": 0, "item": {"value": "0x0", "data": ""}}}'

In [137]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 0
  item:
    value: '0x0'
    data: ''

In [138]:
pointer.bytestream


Out[138]:
''

In [139]:
pointer.data.item.bytestream = '4b6f6e666f6f2069732046756e00'

In [140]:
pointer.deserialize_data(bytes.fromhex('0e0010000000'))


Out[140]:
Index(byte=6, bit=0, address=7, base_address=1, update=False)

In [141]:
pointer.bytestream


Out[141]:
''

In [142]:
pointer.to_list(nested=True)


Out[142]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', '')]

In [143]:
pointer.to_csv(nested=True)


Out[143]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': ''}]

In [144]:
pointer.to_json(nested=True)


Out[144]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": ""}}}'

In [145]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: ''

In [146]:
pointer.data.item.deserialize_data()


Out[146]:
Index(byte=14, bit=0, address=30, base_address=16, update=False)

In [147]:
pointer.to_list(nested=True)


Out[147]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [148]:
pointer.to_csv(nested=True)


Out[148]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [149]:
pointer.to_json(nested=True)


Out[149]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [150]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: Konfoo is Fun

Serializing


In [151]:
pointer.to_list(nested=True)


Out[151]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [152]:
pointer.to_csv(nested=True)


Out[152]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [153]:
pointer.to_json(nested=True)


Out[153]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [154]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: Konfoo is Fun

In [155]:
pointer.bytestream = ''
pointer.bytestream


Out[155]:
''

In [156]:
pointer.data.item.bytestream = ''
pointer.data.item.bytestream


Out[156]:
''

In [157]:
bytestream = bytearray()
bytestream.hex()


Out[157]:
''

In [158]:
pointer.serialize(bytestream)


Out[158]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [159]:
pointer.bytestream


Out[159]:
''

In [160]:
pointer.data.item.bytestream


Out[160]:
''

In [161]:
bytes(pointer).hex()


Out[161]:
'01000000'

In [162]:
bytestream.hex()


Out[162]:
'01000000'

In [163]:
bytestream = bytearray()
bytestream.hex()


Out[163]:
''

In [164]:
pointer.serialize(bytestream, nested=True)


Out[164]:
Index(byte=4, bit=0, address=4, base_address=0, update=False)

In [165]:
pointer.bytestream


Out[165]:
'0e0010000000'

In [166]:
pointer.data.item.bytestream


Out[166]:
'4b6f6e666f6f2069732046756e00'

In [167]:
bytes(pointer).hex()


Out[167]:
'01000000'

In [168]:
bytestream.hex()


Out[168]:
'01000000'

Serialize the Data Object


In [169]:
pointer.to_list(nested=True)


Out[169]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [170]:
pointer.to_csv(nested=True)


Out[170]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [171]:
pointer.to_json(nested=True)


Out[171]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [172]:
to_yaml(pointer, nested=True, flow_style=False)


value: '0x1'
data:
  size: 14
  item:
    value: '0x10'
    data: Konfoo is Fun

In [173]:
pointer.bytestream = ''
pointer.bytestream


Out[173]:
''

In [174]:
pointer.data.item.bytestream = ''
pointer.data.item.bytestream


Out[174]:
''

In [175]:
pointer.serialize_data().hex()


Out[175]:
'0e0010000000'

In [176]:
pointer.bytestream


Out[176]:
''

In [177]:
pointer.data.item.serialize_data().hex()


Out[177]:
'4b6f6e666f6f2069732046756e00'

In [178]:
pointer.data.item.bytestream


Out[178]:
''

Pointer Members

Access the Data Object of a Pointer Field


In [179]:
pointer.data


Out[179]:
Structure([('size',
            Decimal(index=Index(byte=0, bit=0, address=1, base_address=1, update=False), alignment=Alignment(byte_size=2, bit_offset=0), bit_size=16, value=14)),
           ('item',
            Pointer(index=Index(byte=2, bit=0, address=3, base_address=1, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x10'))])

Access the Attributes of a Pointer Field


In [180]:
pointer.name


Out[180]:
'Pointer32'

In [181]:
pointer.value


Out[181]:
'0x1'

In [182]:
pointer.bit_size


Out[182]:
32

In [183]:
pointer.alignment


Out[183]:
Alignment(byte_size=4, bit_offset=0)

In [184]:
pointer.alignment.byte_size


Out[184]:
4

In [185]:
pointer.alignment.bit_offset


Out[185]:
0

In [186]:
pointer.byte_order


Out[186]:
Byteorder.auto = 'auto'

In [187]:
pointer.byte_order.value


Out[187]:
'auto'

In [188]:
pointer.index


Out[188]:
Index(byte=0, bit=0, address=0, base_address=0, update=False)

In [189]:
pointer.index.byte


Out[189]:
0

In [190]:
pointer.index.bit


Out[190]:
0

In [191]:
pointer.index.address


Out[191]:
0

In [192]:
pointer.index.base_address


Out[192]:
0

In [193]:
pointer.index.update


Out[193]:
False

Pointer Fields

View Field Attributes


In [194]:
pointer.view_fields()


Out[194]:
OrderedDict([('value', '0x1'),
             ('data', OrderedDict([('size', 14), ('item', '0x10')]))])

In [195]:
pointer.view_fields(nested=True)


Out[195]:
OrderedDict([('value', '0x1'),
             ('data',
              OrderedDict([('size', 14),
                           ('item',
                            OrderedDict([('value', '0x10'),
                                         ('data', 'Konfoo is Fun')]))]))])

In [196]:
pointer.view_fields('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order.name')


Out[196]:
OrderedDict([('name', 'Pointer32'),
             ('bit_size', 32),
             ('value', '0x1'),
             ('index',
              Index(byte=0, bit=0, address=0, base_address=0, update=False)),
             ('alignment', Alignment(byte_size=4, bit_offset=0)),
             ('byte_order.name', 'auto'),
             ('data',
              OrderedDict([('size',
                            {'name': 'Decimal16',
                             'bit_size': 16,
                             'value': 14,
                             'index': Index(byte=0, bit=0, address=1, base_address=1, update=False),
                             'alignment': Alignment(byte_size=2, bit_offset=0),
                             'byte_order.name': 'auto'}),
                           ('item',
                            {'name': 'Pointer32',
                             'bit_size': 32,
                             'value': '0x10',
                             'index': Index(byte=2, bit=0, address=3, base_address=1, update=False),
                             'alignment': Alignment(byte_size=4, bit_offset=0),
                             'byte_order.name': 'auto'})]))])

View as a JSON string


In [197]:
pointer.to_json()


Out[197]:
'{"value": "0x1", "data": {"size": 14, "item": "0x10"}}'

In [198]:
print(pointer.to_json(indent=2))


{
  "value": "0x1",
  "data": {
    "size": 14,
    "item": "0x10"
  }
}

In [199]:
pointer.to_json(nested=True)


Out[199]:
'{"value": "0x1", "data": {"size": 14, "item": {"value": "0x10", "data": "Konfoo is Fun"}}}'

In [200]:
print(pointer.to_json(nested=True, indent=2))


{
  "value": "0x1",
  "data": {
    "size": 14,
    "item": {
      "value": "0x10",
      "data": "Konfoo is Fun"
    }
  }
}

In [201]:
pointer.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')


Out[201]:
'{"name": "Pointer32", "bit_size": 32, "value": "0x1", "index": [0, 0, 0, 0, false], "alignment": [4, 0], "byte_order": "auto", "data": {"size": {"name": "Decimal16", "bit_size": 16, "value": 14, "index": [0, 0, 1, 1, false], "alignment": [2, 0], "byte_order": "auto"}, "item": {"name": "Pointer32", "bit_size": 32, "value": "0x10", "index": [2, 0, 3, 1, false], "alignment": [4, 0], "byte_order": "auto"}}}'

In [202]:
print(pointer.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order', indent=2))


{
  "name": "Pointer32",
  "bit_size": 32,
  "value": "0x1",
  "index": [
    0,
    0,
    0,
    0,
    false
  ],
  "alignment": [
    4,
    0
  ],
  "byte_order": "auto",
  "data": {
    "size": {
      "name": "Decimal16",
      "bit_size": 16,
      "value": 14,
      "index": [
        0,
        0,
        1,
        1,
        false
      ],
      "alignment": [
        2,
        0
      ],
      "byte_order": "auto"
    },
    "item": {
      "name": "Pointer32",
      "bit_size": 32,
      "value": "0x10",
      "index": [
        2,
        0,
        3,
        1,
        false
      ],
      "alignment": [
        4,
        0
      ],
      "byte_order": "auto"
    }
  }
}

List the Field Items


In [203]:
pointer.field_items()


Out[203]:
[('field',
  Pointer(index=Index(byte=0, bit=0, address=0, base_address=0, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x1')),
 ('data.size',
  Decimal(index=Index(byte=0, bit=0, address=1, base_address=1, update=False), alignment=Alignment(byte_size=2, bit_offset=0), bit_size=16, value=14)),
 ('data.item',
  Pointer(index=Index(byte=2, bit=0, address=3, base_address=1, update=False), alignment=Alignment(byte_size=4, bit_offset=0), bit_size=32, value='0x10'))]

List the Field Attributes


In [204]:
pointer.to_list()


Out[204]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10')]

In [205]:
pointer.to_list(nested=True)


Out[205]:
[('Pointer.field', '0x1'),
 ('Pointer.data.size', 14),
 ('Pointer.data.item', '0x10'),
 ('Pointer.data.item.data', 'Konfoo is Fun')]

In [206]:
pointer.to_list('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')


Out[206]:
[('Pointer.field',
  ('Pointer32',
   32,
   '0x1',
   Index(byte=0, bit=0, address=0, base_address=0, update=False),
   Alignment(byte_size=4, bit_offset=0),
   Byteorder.auto = 'auto')),
 ('Pointer.data.size',
  ('Decimal16',
   16,
   14,
   Index(byte=0, bit=0, address=1, base_address=1, update=False),
   Alignment(byte_size=2, bit_offset=0),
   Byteorder.auto = 'auto')),
 ('Pointer.data.item',
  ('Pointer32',
   32,
   '0x10',
   Index(byte=2, bit=0, address=3, base_address=1, update=False),
   Alignment(byte_size=4, bit_offset=0),
   Byteorder.auto = 'auto'))]

In [207]:
pointer.to_dict()


Out[207]:
OrderedDict([('Pointer',
              OrderedDict([('field', '0x1'),
                           ('data.size', 14),
                           ('data.item', '0x10')]))])

In [208]:
pointer.to_dict(nested=True)


Out[208]:
OrderedDict([('Pointer',
              OrderedDict([('field', '0x1'),
                           ('data.size', 14),
                           ('data.item', '0x10'),
                           ('data.item.data', 'Konfoo is Fun')]))])

In [209]:
pointer.to_dict('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')


Out[209]:
OrderedDict([('Pointer',
              OrderedDict([('field',
                            ('Pointer32',
                             32,
                             '0x1',
                             Index(byte=0, bit=0, address=0, base_address=0, update=False),
                             Alignment(byte_size=4, bit_offset=0),
                             Byteorder.auto = 'auto')),
                           ('data.size',
                            ('Decimal16',
                             16,
                             14,
                             Index(byte=0, bit=0, address=1, base_address=1, update=False),
                             Alignment(byte_size=2, bit_offset=0),
                             Byteorder.auto = 'auto')),
                           ('data.item',
                            ('Pointer32',
                             32,
                             '0x10',
                             Index(byte=2, bit=0, address=3, base_address=1, update=False),
                             Alignment(byte_size=4, bit_offset=0),
                             Byteorder.auto = 'auto'))]))])

In [210]:
pointer.to_csv()


Out[210]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'}]

In [211]:
pointer.to_csv(nested=True)


Out[211]:
[{'id': 'Pointer.field', 'value': '0x1'},
 {'id': 'Pointer.data.size', 'value': 14},
 {'id': 'Pointer.data.item', 'value': '0x10'},
 {'id': 'Pointer.data.item.data', 'value': 'Konfoo is Fun'}]

In [212]:
pointer.to_csv('name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'), nested=True)


Out[212]:
[{'id': 'Pointer.field', 'type': 'Pointer32', 'size': 32, 'value': '0x1'},
 {'id': 'Pointer.data.size', 'type': 'Decimal16', 'size': 16, 'value': 14},
 {'id': 'Pointer.data.item', 'type': 'Pointer32', 'size': 32, 'value': '0x10'},
 {'id': 'Pointer.data.item.data',
  'type': 'String14',
  'size': 112,
  'value': 'Konfoo is Fun'}]

Write the Field Attributes to a .csv File


In [213]:
pointer.write_csv('Pointer.csv', 'name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'), nested=True)

Save the Field Attributes to an .ini File


In [214]:
pointer.save('Pointer.ini', nested=True)

Load the Field Value from an .ini File


In [215]:
pointer.load('Pointer.ini', nested=True)


[Pointer]
Pointer.field = 0x1
Pointer.data.size = 14
Pointer.data.item = 0x10
Pointer.data.item.data = Konfoo is Fun

In [ ]: