openc2lib.types.base.array_of
1import logging 2 3from openc2lib.types.base.openc2_type import Openc2Type 4from openc2lib.types.base.array import Array 5 6logger = logging.getLogger(__name__) 7 8class ArrayOf: 9 """ OpenC2 ArrayOf 10 11 Implements OpenC2 ArrayOf(*vtype*): 12 >An ordered list of fields with the same semantics. 13 Each field has a position and type *vtype*. 14 15 It extends the `Array` type. However, to make its usage simpler and compliant 16 to the description given in the 17 Language Specification, the implementation is quite different. 18 Note that in many cases `ArrayOf` is only used to create arrays without the need 19 to derive an additional data type. 20 """ 21 22 def __new__(self, fldtype): 23 """ `ArrayOf` builder 24 25 Creates a unnamed derived class from `Array`, which `fieldtypes` is set to `fldtype`. 26 :param fldtype: The type of the fields stored in the array (indicated as *vtype* in 27 the Language Specification. 28 :return: A new unnamed class definition. 29 """ 30 class ArrayOf(Array): 31 """ OpenC2 unnamed `ArrayOf` 32 33 This class inherits from `Array` and sets its `fieldtypes` to a given type. 34 35 One might like to check the type of the elements before inserting them. 36 However, this is not the Python-way. Python use the duck typing approach: 37 https://en.wikipedia.org/wiki/Duck_typing 38 We ask for the type of objects just to keep this information according to 39 the OpenC2 data model. 40 41 Note: no `todict()` method is provided, since `Array.todict()` is fine here. 42 """ 43 fieldtype = fldtype 44 """ The type of values stored in this container """ 45 46 @classmethod 47 def fromdict(cls, lis, e): 48 """ Builds instance from dictionary 49 50 It is used during deserialization to create an openc2lib instance from the text message. 51 It takes an `Encoder` instance that is used to recursively build instances of the inner 52 objects (the `Encoder` provides standard methods to create instances of base objects like 53 strings, integers, boolean). 54 55 :param lis: The intermediary dictionary representation from which the object is built. 56 :param e: The `Encoder that is being used. 57 :return: An instance of this class initialized from the dictionary values. 58 """ 59 objlis = cls() 60 logger.debug('Building %s from %s in ArrayOf', cls, lis) 61 logger.debug('-> instantiating: %s', cls.fieldtype) 62 for k in lis: 63 objlis.append(e.fromdict(cls.fieldtype, k)) 64 65 return objlis 66 67 # This is the code if I would like to do type checking 68 # when inserting data 69# def append(self, item): 70# if isinstance(item, self.fieldtype): 71# super().append(item) 72# else: 73# raise ValueError(self.fieldtype,' allowed only') 74# 75# def insert(self, index, item): 76# if isinstance(item, self.fieldtype): 77# super().insert(index, item) 78# else: 79# raise ValueError(self.fieldtype,' allowed only') 80# 81# def __add__(self, item): 82# if isinstance(item, self.fieldtype): 83# super().__add__(item) 84# else: 85# raise ValueError(self.fieldtype,' allowed only') 86# 87# def __iadd__(self, item): 88# if isinstance(item, self.fieldtype): 89# super().__iadd__(item) 90# else: 91# raise ValueError(self.fieldtype,' allowed only') 92 93 return ArrayOf
logger =
<Logger openc2lib.types.base.array_of (WARNING)>
class
ArrayOf:
9class ArrayOf: 10 """ OpenC2 ArrayOf 11 12 Implements OpenC2 ArrayOf(*vtype*): 13 >An ordered list of fields with the same semantics. 14 Each field has a position and type *vtype*. 15 16 It extends the `Array` type. However, to make its usage simpler and compliant 17 to the description given in the 18 Language Specification, the implementation is quite different. 19 Note that in many cases `ArrayOf` is only used to create arrays without the need 20 to derive an additional data type. 21 """ 22 23 def __new__(self, fldtype): 24 """ `ArrayOf` builder 25 26 Creates a unnamed derived class from `Array`, which `fieldtypes` is set to `fldtype`. 27 :param fldtype: The type of the fields stored in the array (indicated as *vtype* in 28 the Language Specification. 29 :return: A new unnamed class definition. 30 """ 31 class ArrayOf(Array): 32 """ OpenC2 unnamed `ArrayOf` 33 34 This class inherits from `Array` and sets its `fieldtypes` to a given type. 35 36 One might like to check the type of the elements before inserting them. 37 However, this is not the Python-way. Python use the duck typing approach: 38 https://en.wikipedia.org/wiki/Duck_typing 39 We ask for the type of objects just to keep this information according to 40 the OpenC2 data model. 41 42 Note: no `todict()` method is provided, since `Array.todict()` is fine here. 43 """ 44 fieldtype = fldtype 45 """ The type of values stored in this container """ 46 47 @classmethod 48 def fromdict(cls, lis, e): 49 """ Builds instance from dictionary 50 51 It is used during deserialization to create an openc2lib instance from the text message. 52 It takes an `Encoder` instance that is used to recursively build instances of the inner 53 objects (the `Encoder` provides standard methods to create instances of base objects like 54 strings, integers, boolean). 55 56 :param lis: The intermediary dictionary representation from which the object is built. 57 :param e: The `Encoder that is being used. 58 :return: An instance of this class initialized from the dictionary values. 59 """ 60 objlis = cls() 61 logger.debug('Building %s from %s in ArrayOf', cls, lis) 62 logger.debug('-> instantiating: %s', cls.fieldtype) 63 for k in lis: 64 objlis.append(e.fromdict(cls.fieldtype, k)) 65 66 return objlis 67 68 # This is the code if I would like to do type checking 69 # when inserting data 70# def append(self, item): 71# if isinstance(item, self.fieldtype): 72# super().append(item) 73# else: 74# raise ValueError(self.fieldtype,' allowed only') 75# 76# def insert(self, index, item): 77# if isinstance(item, self.fieldtype): 78# super().insert(index, item) 79# else: 80# raise ValueError(self.fieldtype,' allowed only') 81# 82# def __add__(self, item): 83# if isinstance(item, self.fieldtype): 84# super().__add__(item) 85# else: 86# raise ValueError(self.fieldtype,' allowed only') 87# 88# def __iadd__(self, item): 89# if isinstance(item, self.fieldtype): 90# super().__iadd__(item) 91# else: 92# raise ValueError(self.fieldtype,' allowed only') 93 94 return ArrayOf
OpenC2 ArrayOf
Implements OpenC2 ArrayOf(vtype):
An ordered list of fields with the same semantics. Each field has a position and type vtype.
It extends the Array type. However, to make its usage simpler and compliant
to the description given in the
Language Specification, the implementation is quite different.
Note that in many cases ArrayOf is only used to create arrays without the need
to derive an additional data type.
ArrayOf(fldtype)
23 def __new__(self, fldtype): 24 """ `ArrayOf` builder 25 26 Creates a unnamed derived class from `Array`, which `fieldtypes` is set to `fldtype`. 27 :param fldtype: The type of the fields stored in the array (indicated as *vtype* in 28 the Language Specification. 29 :return: A new unnamed class definition. 30 """ 31 class ArrayOf(Array): 32 """ OpenC2 unnamed `ArrayOf` 33 34 This class inherits from `Array` and sets its `fieldtypes` to a given type. 35 36 One might like to check the type of the elements before inserting them. 37 However, this is not the Python-way. Python use the duck typing approach: 38 https://en.wikipedia.org/wiki/Duck_typing 39 We ask for the type of objects just to keep this information according to 40 the OpenC2 data model. 41 42 Note: no `todict()` method is provided, since `Array.todict()` is fine here. 43 """ 44 fieldtype = fldtype 45 """ The type of values stored in this container """ 46 47 @classmethod 48 def fromdict(cls, lis, e): 49 """ Builds instance from dictionary 50 51 It is used during deserialization to create an openc2lib instance from the text message. 52 It takes an `Encoder` instance that is used to recursively build instances of the inner 53 objects (the `Encoder` provides standard methods to create instances of base objects like 54 strings, integers, boolean). 55 56 :param lis: The intermediary dictionary representation from which the object is built. 57 :param e: The `Encoder that is being used. 58 :return: An instance of this class initialized from the dictionary values. 59 """ 60 objlis = cls() 61 logger.debug('Building %s from %s in ArrayOf', cls, lis) 62 logger.debug('-> instantiating: %s', cls.fieldtype) 63 for k in lis: 64 objlis.append(e.fromdict(cls.fieldtype, k)) 65 66 return objlis 67 68 # This is the code if I would like to do type checking 69 # when inserting data 70# def append(self, item): 71# if isinstance(item, self.fieldtype): 72# super().append(item) 73# else: 74# raise ValueError(self.fieldtype,' allowed only') 75# 76# def insert(self, index, item): 77# if isinstance(item, self.fieldtype): 78# super().insert(index, item) 79# else: 80# raise ValueError(self.fieldtype,' allowed only') 81# 82# def __add__(self, item): 83# if isinstance(item, self.fieldtype): 84# super().__add__(item) 85# else: 86# raise ValueError(self.fieldtype,' allowed only') 87# 88# def __iadd__(self, item): 89# if isinstance(item, self.fieldtype): 90# super().__iadd__(item) 91# else: 92# raise ValueError(self.fieldtype,' allowed only') 93 94 return ArrayOf
ArrayOf builder
Creates a unnamed derived class from Array, which fieldtypes is set to fldtype.
Parameters
- fldtype: The type of the fields stored in the array (indicated as vtype in the Language Specification.
Returns
A new unnamed class definition.