openc2lib.types.base.map_of

 1import logging
 2
 3from openc2lib.types.base.openc2_type import Openc2Type
 4from openc2lib.types.base.map import Map
 5
 6logger = logging.getLogger(__name__)
 7
 8class MapOf:
 9	""" OpenC2 MapOf
10
11		Implements OpenC2 MapOf(*ktype, vtype*):
12		>An unordered set of keys to values with the same semantics. 
13			Each key has key type *ktype* and is mapped to value type *vtype*.
14
15		It extends `Map` with the same approach already used for `ArrayOf`.
16		`MapOf` for specific types are created as anonymous classes by passing
17		`ktype` and `vtype` as arguments.
18
19		Note: `MapOf` implementation currently does not support extensins!.
20	"""
21
22	def __new__(self,ktype, vtype):
23		""" `MapOf` builder
24
25			Creates a unnamed derived class from `Map`, which `fieldtypes` is set to a single value
26		 	`ktype: vtype`.
27			:param ktype: The key type of the items stored in the map.
28			:param vtype: The value type of the items stored in the map.
29			:return: A new unnamed class definition.
30		"""
31		class MapOf(Map):
32			""" OpenC2 unnamed `MapOf`
33
34				This class inherits from `Map` and sets its `fieldtypes` to a given type.
35		
36				Note: no `todict()` method is provided, since `Map.todict()` is fine here.
37			"""
38			fieldtypes = {ktype: vtype}
39			""" The type of values stored in this container """
40
41			@classmethod
42			def fromdict(cls, dic, e):
43				""" Builds instance from dictionary 
44		
45					It is used during deserialization to create an openc2lib instance from the text message.
46					It takes an `Encoder` instance that is used to recursively build instances of the inner
47					objects (the `Encoder` provides standard methods to create instances of base objects like
48					strings, integers, boolean).
49		
50					:param dic: The intermediary dictionary representation from which the object is built.
51					:param e: The `Encoder that is being used.
52					:return: An instance of this class initialized from the dictionary values.
53				"""
54				objdic = {}
55				logger.debug('Building %s from %s in MapOf', cls, dic)
56				for k,v in dic.items():
57					kclass = list(cls.fieldtypes)[0]
58					objk = e.fromdict(kclass, k)
59					objdic[objk] = e.fromdict(cls.fieldtypes[kclass], v)
60				return objdic
61
62		return MapOf
logger = <Logger openc2lib.types.base.map_of (WARNING)>
class MapOf:
 9class MapOf:
10	""" OpenC2 MapOf
11
12		Implements OpenC2 MapOf(*ktype, vtype*):
13		>An unordered set of keys to values with the same semantics. 
14			Each key has key type *ktype* and is mapped to value type *vtype*.
15
16		It extends `Map` with the same approach already used for `ArrayOf`.
17		`MapOf` for specific types are created as anonymous classes by passing
18		`ktype` and `vtype` as arguments.
19
20		Note: `MapOf` implementation currently does not support extensins!.
21	"""
22
23	def __new__(self,ktype, vtype):
24		""" `MapOf` builder
25
26			Creates a unnamed derived class from `Map`, which `fieldtypes` is set to a single value
27		 	`ktype: vtype`.
28			:param ktype: The key type of the items stored in the map.
29			:param vtype: The value type of the items stored in the map.
30			:return: A new unnamed class definition.
31		"""
32		class MapOf(Map):
33			""" OpenC2 unnamed `MapOf`
34
35				This class inherits from `Map` and sets its `fieldtypes` to a given type.
36		
37				Note: no `todict()` method is provided, since `Map.todict()` is fine here.
38			"""
39			fieldtypes = {ktype: vtype}
40			""" The type of values stored in this container """
41
42			@classmethod
43			def fromdict(cls, dic, e):
44				""" Builds instance from dictionary 
45		
46					It is used during deserialization to create an openc2lib instance from the text message.
47					It takes an `Encoder` instance that is used to recursively build instances of the inner
48					objects (the `Encoder` provides standard methods to create instances of base objects like
49					strings, integers, boolean).
50		
51					:param dic: The intermediary dictionary representation from which the object is built.
52					:param e: The `Encoder that is being used.
53					:return: An instance of this class initialized from the dictionary values.
54				"""
55				objdic = {}
56				logger.debug('Building %s from %s in MapOf', cls, dic)
57				for k,v in dic.items():
58					kclass = list(cls.fieldtypes)[0]
59					objk = e.fromdict(kclass, k)
60					objdic[objk] = e.fromdict(cls.fieldtypes[kclass], v)
61				return objdic
62
63		return MapOf

OpenC2 MapOf

Implements OpenC2 MapOf(ktype, vtype):

An unordered set of keys to values with the same semantics. Each key has key type ktype and is mapped to value type vtype.

It extends Map with the same approach already used for ArrayOf. MapOf for specific types are created as anonymous classes by passing ktype and vtype as arguments.

Note: MapOf implementation currently does not support extensins!.

MapOf(ktype, vtype)
23	def __new__(self,ktype, vtype):
24		""" `MapOf` builder
25
26			Creates a unnamed derived class from `Map`, which `fieldtypes` is set to a single value
27		 	`ktype: vtype`.
28			:param ktype: The key type of the items stored in the map.
29			:param vtype: The value type of the items stored in the map.
30			:return: A new unnamed class definition.
31		"""
32		class MapOf(Map):
33			""" OpenC2 unnamed `MapOf`
34
35				This class inherits from `Map` and sets its `fieldtypes` to a given type.
36		
37				Note: no `todict()` method is provided, since `Map.todict()` is fine here.
38			"""
39			fieldtypes = {ktype: vtype}
40			""" The type of values stored in this container """
41
42			@classmethod
43			def fromdict(cls, dic, e):
44				""" Builds instance from dictionary 
45		
46					It is used during deserialization to create an openc2lib instance from the text message.
47					It takes an `Encoder` instance that is used to recursively build instances of the inner
48					objects (the `Encoder` provides standard methods to create instances of base objects like
49					strings, integers, boolean).
50		
51					:param dic: The intermediary dictionary representation from which the object is built.
52					:param e: The `Encoder that is being used.
53					:return: An instance of this class initialized from the dictionary values.
54				"""
55				objdic = {}
56				logger.debug('Building %s from %s in MapOf', cls, dic)
57				for k,v in dic.items():
58					kclass = list(cls.fieldtypes)[0]
59					objk = e.fromdict(kclass, k)
60					objdic[objk] = e.fromdict(cls.fieldtypes[kclass], v)
61				return objdic
62
63		return MapOf

MapOf builder

Creates a unnamed derived class from Map, which fieldtypes is set to a single value ktype: vtype.

Parameters
  • ktype: The key type of the items stored in the map.
  • vtype: The value type of the items stored in the map.
Returns

A new unnamed class definition.