Sequence


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)

Create a new Sequence


In [4]:
sequence = Sequence([Byte(), Unsigned8(), Decimal8(), Char()])

In [5]:
sequence.to_list()


Out[5]:
[('Sequence[0]', '0x0'),
 ('Sequence[1]', '0x0'),
 ('Sequence[2]', 0),
 ('Sequence[3]', '\x00')]

In [6]:
sequence.to_csv()


Out[6]:
[{'id': 'Sequence[0]', 'value': '0x0'},
 {'id': 'Sequence[1]', 'value': '0x0'},
 {'id': 'Sequence[2]', 'value': 0},
 {'id': 'Sequence[3]', 'value': '\x00'}]

In [7]:
sequence.to_json()


Out[7]:
'["0x0", "0x0", 0, "\\u0000"]'

In [8]:
to_yaml(sequence, flow_style=False)


- '0x0'
- '0x0'
- 0
- "\0"

Append a Member


In [9]:
sequence = Sequence()

In [10]:
sequence.append(Unsigned8())

In [11]:
sequence.to_list()


Out[11]:
[('Sequence[0]', '0x0')]

In [12]:
sequence.to_csv()


Out[12]:
[{'id': 'Sequence[0]', 'value': '0x0'}]

In [13]:
sequence.to_json()


Out[13]:
'["0x0"]'

In [14]:
to_yaml(sequence, flow_style=False)


- '0x0'

Insert a Member


In [15]:
sequence.insert(0, Byte())

In [16]:
sequence.to_list()


Out[16]:
[('Sequence[0]', '0x0'), ('Sequence[1]', '0x0')]

In [17]:
sequence.to_csv()


Out[17]:
[{'id': 'Sequence[0]', 'value': '0x0'}, {'id': 'Sequence[1]', 'value': '0x0'}]

In [18]:
sequence.to_json()


Out[18]:
'["0x0", "0x0"]'

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


- '0x0'
- '0x0'

Extend a Sequence


In [20]:
sequence.extend([Decimal8(), Char()])

In [21]:
sequence.to_list()


Out[21]:
[('Sequence[0]', '0x0'),
 ('Sequence[1]', '0x0'),
 ('Sequence[2]', 0),
 ('Sequence[3]', '\x00')]

In [22]:
sequence.to_csv()


Out[22]:
[{'id': 'Sequence[0]', 'value': '0x0'},
 {'id': 'Sequence[1]', 'value': '0x0'},
 {'id': 'Sequence[2]', 'value': 0},
 {'id': 'Sequence[3]', 'value': '\x00'}]

In [23]:
sequence.to_json()


Out[23]:
'["0x0", "0x0", 0, "\\u0000"]'

In [24]:
to_yaml(sequence, flow_style=False)


- '0x0'
- '0x0'
- 0
- "\0"

Initialize a Sequence


In [25]:
sequence.initialize_fields([1, 2, 9, 'F'])

In [26]:
sequence.to_list()


Out[26]:
[('Sequence[0]', '0x1'),
 ('Sequence[1]', '0x2'),
 ('Sequence[2]', 9),
 ('Sequence[3]', 'F')]

In [27]:
sequence.to_csv()


Out[27]:
[{'id': 'Sequence[0]', 'value': '0x1'},
 {'id': 'Sequence[1]', 'value': '0x2'},
 {'id': 'Sequence[2]', 'value': 9},
 {'id': 'Sequence[3]', 'value': 'F'}]

In [28]:
sequence.to_json()


Out[28]:
'["0x1", "0x2", 9, "F"]'

In [29]:
to_yaml(sequence, flow_style=False)


- '0x1'
- '0x2'
- 9
- F

Display a Sequence


In [30]:
sequence


Out[30]:
[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='0x1'), Unsigned8(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='0x2'), Decimal8(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=9), Char(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='F')]

Metadata of a Sequence


In [31]:
sequence.describe()


Out[31]:
OrderedDict([('class', 'Sequence'),
             ('name', 'Sequence'),
             ('size', 4),
             ('type', 'Sequence'),
             ('member',
              [OrderedDict([('address', 0),
                            ('alignment', [1, 0]),
                            ('class', 'Byte'),
                            ('index', [0, 0]),
                            ('max', 255),
                            ('min', 0),
                            ('name', 'Sequence[0]'),
                            ('order', 'auto'),
                            ('signed', False),
                            ('size', 8),
                            ('type', 'Field'),
                            ('value', '0x1')]),
               OrderedDict([('address', 0),
                            ('alignment', [1, 0]),
                            ('class', 'Unsigned8'),
                            ('index', [0, 0]),
                            ('max', 255),
                            ('min', 0),
                            ('name', 'Sequence[1]'),
                            ('order', 'auto'),
                            ('signed', False),
                            ('size', 8),
                            ('type', 'Field'),
                            ('value', '0x2')]),
               OrderedDict([('address', 0),
                            ('alignment', [1, 0]),
                            ('class', 'Decimal8'),
                            ('index', [0, 0]),
                            ('max', 255),
                            ('min', 0),
                            ('name', 'Sequence[2]'),
                            ('order', 'auto'),
                            ('signed', False),
                            ('size', 8),
                            ('type', 'Field'),
                            ('value', 9)]),
               OrderedDict([('address', 0),
                            ('alignment', [1, 0]),
                            ('class', 'Char'),
                            ('index', [0, 0]),
                            ('max', 255),
                            ('min', 0),
                            ('name', 'Sequence[3]'),
                            ('order', 'auto'),
                            ('signed', False),
                            ('size', 8),
                            ('type', 'Field'),
                            ('value', 'F')])])])

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


{
  "class": "Sequence",
  "name": "Sequence",
  "size": 4,
  "type": "Sequence",
  "member": [
    {
      "address": 0,
      "alignment": [
        1,
        0
      ],
      "class": "Byte",
      "index": [
        0,
        0
      ],
      "max": 255,
      "min": 0,
      "name": "Sequence[0]",
      "order": "auto",
      "signed": false,
      "size": 8,
      "type": "Field",
      "value": "0x1"
    },
    {
      "address": 0,
      "alignment": [
        1,
        0
      ],
      "class": "Unsigned8",
      "index": [
        0,
        0
      ],
      "max": 255,
      "min": 0,
      "name": "Sequence[1]",
      "order": "auto",
      "signed": false,
      "size": 8,
      "type": "Field",
      "value": "0x2"
    },
    {
      "address": 0,
      "alignment": [
        1,
        0
      ],
      "class": "Decimal8",
      "index": [
        0,
        0
      ],
      "max": 255,
      "min": 0,
      "name": "Sequence[2]",
      "order": "auto",
      "signed": false,
      "size": 8,
      "type": "Field",
      "value": 9
    },
    {
      "address": 0,
      "alignment": [
        1,
        0
      ],
      "class": "Char",
      "index": [
        0,
        0
      ],
      "max": 255,
      "min": 0,
      "name": "Sequence[3]",
      "order": "auto",
      "signed": false,
      "size": 8,
      "type": "Field",
      "value": "F"
    }
  ]
}

In [33]:
d3flare_json(sequence.describe(), sys.stdout, indent=2)


{
  "class": "Sequence",
  "name": "Sequence",
  "children": [
    {
      "class": "Byte",
      "name": "Sequence[0]",
      "size": 8,
      "value": "0x1"
    },
    {
      "class": "Unsigned8",
      "name": "Sequence[1]",
      "size": 8,
      "value": "0x2"
    },
    {
      "class": "Decimal8",
      "name": "Sequence[2]",
      "size": 8,
      "value": 9
    },
    {
      "class": "Char",
      "name": "Sequence[3]",
      "size": 8,
      "value": "F"
    }
  ]
}

Size of a Sequence


In [34]:
sequence.container_size()


Out[34]:
(4, 0)

In [35]:
num_of_bytes, num_of_remaining_bits = sequence.container_size()

In [36]:
num_of_bytes


Out[36]:
4

In [37]:
num_of_remaining_bits


Out[37]:
0

Indexing


In [38]:
sequence.to_list('index')


Out[38]:
[('Sequence[0]',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Sequence[1]',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Sequence[2]',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Sequence[3]',
  Index(byte=0, bit=0, address=0, base_address=0, update=False))]

In [39]:
sequence.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))


Out[39]:
[{'id': 'Sequence[0]', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Sequence[1]', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Sequence[2]', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Sequence[3]', 'index': 0, 'offset': 0, 'address': 0}]

In [40]:
sequence.to_json('index')


Out[40]:
'[[0, 0, 0, 0, false], [0, 0, 0, 0, false], [0, 0, 0, 0, false], [0, 0, 0, 0, false]]'

In [41]:
sequence.index_fields(index=Index())


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

In [42]:
sequence.index_fields()


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

In [43]:
sequence.to_list('index')


Out[43]:
[('Sequence[0]',
  Index(byte=0, bit=0, address=0, base_address=0, update=False)),
 ('Sequence[1]',
  Index(byte=1, bit=0, address=1, base_address=0, update=False)),
 ('Sequence[2]',
  Index(byte=2, bit=0, address=2, base_address=0, update=False)),
 ('Sequence[3]',
  Index(byte=3, bit=0, address=3, base_address=0, update=False))]

In [44]:
sequence.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))


Out[44]:
[{'id': 'Sequence[0]', 'index': 0, 'offset': 0, 'address': 0},
 {'id': 'Sequence[1]', 'index': 1, 'offset': 0, 'address': 1},
 {'id': 'Sequence[2]', 'index': 2, 'offset': 0, 'address': 2},
 {'id': 'Sequence[3]', 'index': 3, 'offset': 0, 'address': 3}]

In [45]:
sequence.to_json('index')


Out[45]:
'[[0, 0, 0, 0, false], [1, 0, 1, 0, false], [2, 0, 2, 0, false], [3, 0, 3, 0, false]]'

De-Serializing


In [46]:
sequence.deserialize(bytes.fromhex('01020946f00f00'))


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

In [47]:
sequence.to_list()


Out[47]:
[('Sequence[0]', '0x1'),
 ('Sequence[1]', '0x2'),
 ('Sequence[2]', 9),
 ('Sequence[3]', 'F')]

In [48]:
sequence.to_csv()


Out[48]:
[{'id': 'Sequence[0]', 'value': '0x1'},
 {'id': 'Sequence[1]', 'value': '0x2'},
 {'id': 'Sequence[2]', 'value': 9},
 {'id': 'Sequence[3]', 'value': 'F'}]

In [49]:
sequence.to_json()


Out[49]:
'["0x1", "0x2", 9, "F"]'

In [50]:
to_yaml(sequence, flow_style=False)


- '0x1'
- '0x2'
- 9
- F

Serializing


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


Out[51]:
''

In [52]:
sequence.serialize(bytestream)


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

In [53]:
bytes(sequence).hex()


Out[53]:
'01020946'

In [54]:
bytestream.hex()


Out[54]:
'01020946'

Sequence Members

Number of Members


In [55]:
len(sequence)


Out[55]:
4

Access a Member


In [56]:
sequence[0]


Out[56]:
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='0x1')

Access the Attributes of a Member Field


In [57]:
sequence[0].name


Out[57]:
'Byte'

In [58]:
sequence[0].value


Out[58]:
'0x1'

In [59]:
sequence[0].bit_size


Out[59]:
8

In [60]:
sequence[0].alignment


Out[60]:
Alignment(byte_size=1, bit_offset=0)

In [61]:
sequence[0].alignment.byte_size


Out[61]:
1

In [62]:
sequence[0].alignment.bit_offset


Out[62]:
0

In [63]:
sequence[0].byte_order


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

In [64]:
sequence[0].byte_order.value


Out[64]:
'auto'

In [65]:
sequence[0].index


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

In [66]:
sequence[0].index.byte


Out[66]:
0

In [67]:
sequence[0].index.bit


Out[67]:
0

In [68]:
sequence[0].index.address


Out[68]:
0

In [69]:
sequence[0].index.base_address


Out[69]:
0

In [70]:
sequence[0].index.update


Out[70]:
False

List the Members


In [71]:
[member.item_type for member in sequence]


Out[71]:
[ItemClass.Byte = 42,
 ItemClass.Unsigned = 45,
 ItemClass.Decimal = 40,
 ItemClass.Char = 43]

Sequence Fields

View Field Attributes


In [72]:
sequence.view_fields()


Out[72]:
['0x1', '0x2', 9, 'F']

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


Out[73]:
[{'name': 'Byte',
  'bit_size': 8,
  'value': '0x1',
  'index': Index(byte=0, bit=0, address=0, base_address=0, update=False),
  'alignment': Alignment(byte_size=1, bit_offset=0),
  'byte_order.name': 'auto'},
 {'name': 'Unsigned8',
  'bit_size': 8,
  'value': '0x2',
  'index': Index(byte=1, bit=0, address=1, base_address=0, update=False),
  'alignment': Alignment(byte_size=1, bit_offset=0),
  'byte_order.name': 'auto'},
 {'name': 'Decimal8',
  'bit_size': 8,
  'value': 9,
  'index': Index(byte=2, bit=0, address=2, base_address=0, update=False),
  'alignment': Alignment(byte_size=1, bit_offset=0),
  'byte_order.name': 'auto'},
 {'name': 'Char',
  'bit_size': 8,
  'value': 'F',
  'index': Index(byte=3, bit=0, address=3, base_address=0, update=False),
  'alignment': Alignment(byte_size=1, bit_offset=0),
  'byte_order.name': 'auto'}]

View as a JSON string


In [74]:
sequence.to_json()


Out[74]:
'["0x1", "0x2", 9, "F"]'

In [75]:
print(sequence.to_json(indent=2))


[
  "0x1",
  "0x2",
  9,
  "F"
]

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


Out[76]:
'[{"name": "Byte", "bit_size": 8, "value": "0x1", "index": [0, 0, 0, 0, false], "alignment": [1, 0], "byte_order": "auto"}, {"name": "Unsigned8", "bit_size": 8, "value": "0x2", "index": [1, 0, 1, 0, false], "alignment": [1, 0], "byte_order": "auto"}, {"name": "Decimal8", "bit_size": 8, "value": 9, "index": [2, 0, 2, 0, false], "alignment": [1, 0], "byte_order": "auto"}, {"name": "Char", "bit_size": 8, "value": "F", "index": [3, 0, 3, 0, false], "alignment": [1, 0], "byte_order": "auto"}]'

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


[
  {
    "name": "Byte",
    "bit_size": 8,
    "value": "0x1",
    "index": [
      0,
      0,
      0,
      0,
      false
    ],
    "alignment": [
      1,
      0
    ],
    "byte_order": "auto"
  },
  {
    "name": "Unsigned8",
    "bit_size": 8,
    "value": "0x2",
    "index": [
      1,
      0,
      1,
      0,
      false
    ],
    "alignment": [
      1,
      0
    ],
    "byte_order": "auto"
  },
  {
    "name": "Decimal8",
    "bit_size": 8,
    "value": 9,
    "index": [
      2,
      0,
      2,
      0,
      false
    ],
    "alignment": [
      1,
      0
    ],
    "byte_order": "auto"
  },
  {
    "name": "Char",
    "bit_size": 8,
    "value": "F",
    "index": [
      3,
      0,
      3,
      0,
      false
    ],
    "alignment": [
      1,
      0
    ],
    "byte_order": "auto"
  }
]

List the Field Items


In [78]:
sequence.field_items()


Out[78]:
[('[0]',
  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='0x1')),
 ('[1]',
  Unsigned8(index=Index(byte=1, bit=0, address=1, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=8, value='0x2')),
 ('[2]',
  Decimal8(index=Index(byte=2, bit=0, address=2, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=8, value=9)),
 ('[3]',
  Char(index=Index(byte=3, bit=0, address=3, base_address=0, update=False), alignment=Alignment(byte_size=1, bit_offset=0), bit_size=8, value='F'))]

List the Field Attributes


In [79]:
sequence.to_list()


Out[79]:
[('Sequence[0]', '0x1'),
 ('Sequence[1]', '0x2'),
 ('Sequence[2]', 9),
 ('Sequence[3]', 'F')]

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


Out[80]:
[('Sequence[0]',
  ('Byte',
   8,
   '0x1',
   Index(byte=0, bit=0, address=0, base_address=0, update=False),
   Alignment(byte_size=1, bit_offset=0),
   Byteorder.auto = 'auto')),
 ('Sequence[1]',
  ('Unsigned8',
   8,
   '0x2',
   Index(byte=1, bit=0, address=1, base_address=0, update=False),
   Alignment(byte_size=1, bit_offset=0),
   Byteorder.auto = 'auto')),
 ('Sequence[2]',
  ('Decimal8',
   8,
   9,
   Index(byte=2, bit=0, address=2, base_address=0, update=False),
   Alignment(byte_size=1, bit_offset=0),
   Byteorder.auto = 'auto')),
 ('Sequence[3]',
  ('Char',
   8,
   'F',
   Index(byte=3, bit=0, address=3, base_address=0, update=False),
   Alignment(byte_size=1, bit_offset=0),
   Byteorder.auto = 'auto'))]

In [81]:
sequence.to_dict()


Out[81]:
OrderedDict([('Sequence',
              OrderedDict([('[0]', '0x1'),
                           ('[1]', '0x2'),
                           ('[2]', 9),
                           ('[3]', 'F')]))])

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


Out[82]:
OrderedDict([('Sequence',
              OrderedDict([('[0]',
                            ('Byte',
                             8,
                             '0x1',
                             Index(byte=0, bit=0, address=0, base_address=0, update=False),
                             Alignment(byte_size=1, bit_offset=0),
                             Byteorder.auto = 'auto')),
                           ('[1]',
                            ('Unsigned8',
                             8,
                             '0x2',
                             Index(byte=1, bit=0, address=1, base_address=0, update=False),
                             Alignment(byte_size=1, bit_offset=0),
                             Byteorder.auto = 'auto')),
                           ('[2]',
                            ('Decimal8',
                             8,
                             9,
                             Index(byte=2, bit=0, address=2, base_address=0, update=False),
                             Alignment(byte_size=1, bit_offset=0),
                             Byteorder.auto = 'auto')),
                           ('[3]',
                            ('Char',
                             8,
                             'F',
                             Index(byte=3, bit=0, address=3, base_address=0, update=False),
                             Alignment(byte_size=1, bit_offset=0),
                             Byteorder.auto = 'auto'))]))])

In [83]:
sequence.to_csv()


Out[83]:
[{'id': 'Sequence[0]', 'value': '0x1'},
 {'id': 'Sequence[1]', 'value': '0x2'},
 {'id': 'Sequence[2]', 'value': 9},
 {'id': 'Sequence[3]', 'value': 'F'}]

In [84]:
sequence.to_csv('name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))


Out[84]:
[{'id': 'Sequence[0]', 'type': 'Byte', 'size': 8, 'value': '0x1'},
 {'id': 'Sequence[1]', 'type': 'Unsigned8', 'size': 8, 'value': '0x2'},
 {'id': 'Sequence[2]', 'type': 'Decimal8', 'size': 8, 'value': 9},
 {'id': 'Sequence[3]', 'type': 'Char', 'size': 8, 'value': 'F'}]

Write the Field Attributes to a .csv File


In [85]:
sequence.write_csv('Sequence.csv', 'name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))

Save the Field Attributes to an .ini File


In [86]:
sequence.save('Sequence.ini')

Load the Field Value from an .ini File


In [87]:
sequence.load('Sequence.ini')


[Sequence]
Sequence[0] = 0x1
Sequence[1] = 0x2
Sequence[2] = 9
Sequence[3] = F

In [ ]: