In [1]:
import copy
import json
import sys
from konfoo import *
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)
In [4]:
array = Array(template=Byte, capacity=4)
array = Array(Byte, 4)
In [5]:
array.to_list()
Out[5]:
In [6]:
array.to_csv()
Out[6]:
In [7]:
array.to_json()
Out[7]:
In [8]:
to_yaml(array, flow_style=False)
Define a concrete data object pointer
class and use the class as the array element
template.
In [9]:
class BytePointer(Pointer):
def __init__(self):
super().__init__(Byte())
In [10]:
array = Array(BytePointer, 2)
In [11]:
array.to_list(nested=True)
Out[11]:
In [12]:
array.to_csv(nested=True)
Out[12]:
In [13]:
array.to_json(nested=True)
Out[13]:
In [14]:
to_yaml(array, nested=True, flow_style=False)
In [15]:
array[0].value = 1
In [16]:
array[0].data.value = 2
In [17]:
array.to_list(nested=True)
Out[17]:
In [18]:
array.to_csv(nested=True)
Out[18]:
In [19]:
array.to_json(nested=True)
Out[19]:
In [20]:
to_yaml(array, nested=True, flow_style=False)
In [21]:
array = Array(Pointer(Byte()), 2)
In [22]:
array.to_list(nested=True)
Out[22]:
In [23]:
array.to_csv(nested=True)
Out[23]:
In [24]:
array.to_json(nested=True)
Out[24]:
In [25]:
to_yaml(array, nested=True, flow_style=False)
In [26]:
array[0].value = 1
In [27]:
array[0].data.value = 2
In [28]:
array.to_list(nested=True)
Out[28]:
In [29]:
array.to_csv(nested=True)
Out[29]:
In [30]:
array.to_json(nested=True)
Out[30]:
In [31]:
to_yaml(array, nested=True, flow_style=False)
Define a factory
for the array element
template of the array
.
In [32]:
class FieldPointerFactory:
""" A factory class to produce a pointer array element to any field. """
def __init__(self, template):
# Data object: field template (instance or class).
self.template = template
def _create_data_object(self):
""" Produces the data object attached to the pointer. """
if is_field(self.template):
# Copy data object instance from instance template
return copy.copy(self.template)
elif callable(self.template):
# Create data object instance from class template
data_object = self.template()
if is_field(data_object):
return data_object
else:
raise FactoryTypeError(self, self.template, data_object)
else:
raise MemberTypeError(self, self.template)
def __call__(self):
""" Produces the array element. """
return Pointer(self._create_data_object())
In [33]:
array = Array(FieldPointerFactory(Byte()), 2)
In [34]:
array.to_list(nested=True)
Out[34]:
In [35]:
array.to_csv(nested=True)
Out[35]:
In [36]:
array.to_json(nested=True)
Out[36]:
In [37]:
to_yaml(array, nested=True, flow_style=False)
In [38]:
array[0].value = 1
In [39]:
array[0].data.value = 2
In [40]:
array.to_list(nested=True)
Out[40]:
In [41]:
array.to_json(nested=True)
Out[41]:
In [42]:
to_yaml(array, nested=True, flow_style=False)
In [43]:
array = Array(Byte())
In [44]:
array.to_list()
Out[44]:
In [45]:
array.to_csv()
Out[45]:
In [46]:
array.to_json()
Out[46]:
In [47]:
to_yaml(array, flow_style=False)
In [48]:
array.resize(4)
In [49]:
array.to_list()
Out[49]:
In [50]:
array.to_csv()
Out[50]:
In [51]:
array.to_json()
Out[51]:
In [52]:
to_yaml(array, flow_style=False)
In [53]:
array.initialize_fields([1, 2])
In [54]:
array.to_list()
Out[54]:
In [55]:
array.to_csv()
Out[55]:
In [56]:
array.to_json()
Out[56]:
In [57]:
to_yaml(array, flow_style=False)
In [58]:
array
Out[58]:
In [59]:
array.describe()
Out[59]:
In [60]:
json.dump(array.describe(), sys.stdout, indent=2)
In [61]:
d3flare_json(array.describe(), sys.stdout, indent=2)
In [62]:
array.container_size()
Out[62]:
In [63]:
num_of_bytes, num_of_remaining_bits = array.container_size()
In [64]:
num_of_bytes
Out[64]:
In [65]:
num_of_remaining_bits
Out[65]:
In [66]:
array.to_list('index')
Out[66]:
In [67]:
array.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))
Out[67]:
In [68]:
array.to_json('index')
Out[68]:
In [69]:
array.index_fields(index=Index())
Out[69]:
In [70]:
array.index_fields()
Out[70]:
In [71]:
array.to_list('index')
Out[71]:
In [72]:
array.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))
Out[72]:
In [73]:
array.to_json('index')
Out[73]:
In [74]:
array.deserialize(bytes.fromhex('01020304f00f00'))
Out[74]:
In [75]:
array.to_list()
Out[75]:
In [76]:
array.to_csv()
Out[76]:
In [77]:
array.to_json()
Out[77]:
In [78]:
to_yaml(array, flow_style=False)
In [79]:
bytestream = bytearray()
bytestream.hex()
Out[79]:
In [80]:
array.serialize(bytestream)
Out[80]:
In [81]:
bytes(array).hex()
Out[81]:
In [82]:
bytestream.hex()
Out[82]:
In [83]:
len(array)
Out[83]:
In [84]:
array[0]
Out[84]:
In [85]:
array[0].name
Out[85]:
In [86]:
array[0].value
Out[86]:
In [87]:
array[0].bit_size
Out[87]:
In [88]:
array[0].alignment
Out[88]:
In [89]:
array[0].alignment.byte_size
Out[89]:
In [90]:
array[0].alignment.bit_offset
Out[90]:
In [91]:
array[0].byte_order
Out[91]:
In [92]:
array[0].byte_order.value
Out[92]:
In [93]:
array[0].index
Out[93]:
In [94]:
array[0].index.byte
Out[94]:
In [95]:
array[0].index.bit
Out[95]:
In [96]:
array[0].index.address
Out[96]:
In [97]:
array[0].index.base_address
Out[97]:
In [98]:
array[0].index.update
Out[98]:
In [99]:
[member.item_type for member in array]
Out[99]:
In [100]:
array.view_fields()
Out[100]:
In [101]:
array.view_fields('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order.name')
Out[101]:
In [102]:
array.to_json()
Out[102]:
In [103]:
print(array.to_json(indent=2))
In [104]:
array.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')
Out[104]:
In [105]:
print(array.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order', indent=2))
In [106]:
array.field_items()
Out[106]:
In [107]:
array.to_list()
Out[107]:
In [108]:
array.to_list('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')
Out[108]:
In [109]:
array.to_dict()
Out[109]:
In [110]:
array.to_dict('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')
Out[110]:
In [111]:
array.to_csv()
Out[111]:
In [112]:
array.to_csv('name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))
Out[112]:
In [113]:
array.write_csv('Array.csv', 'name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))
In [114]:
array.save('Array.ini')
In [115]:
array.load('Array.ini')
In [ ]: