m_strat.api.ecs.component_dispatcher
1import io 2from collections import Counter 3from typing import Any 4 5import numpy as np 6import torch 7 8from api.logger import m_log as log 9 10 11# DEFAULT METHODS for packing and unpacking (because they aren't primitives allowing for native overwriting) 12def pack_torch_tensor(tensor_obj): 13 buffer = io.BytesIO() 14 torch.save(tensor_obj, buffer) 15 return buffer.getvalue() 16 17 18def unpack_torch_tensor(blob): 19 buffer = io.BytesIO(blob) 20 return torch.load(buffer, weights_only=True) 21 22 23def pack_np_array(np_arr): 24 buffer = io.BytesIO() 25 np.save(buffer, np_arr) 26 return buffer.getvalue() 27 28 29def unpack_np_array(blob): 30 buffer = io.BytesIO(blob) 31 return np.load(buffer) 32 33 34DEFAULT_DISPATCHERS = [ 35 { 36 "type_string": "torch.Tensor", 37 "pack": pack_torch_tensor, 38 "unpack": unpack_torch_tensor, 39 }, 40 { 41 "type_string": "numpy.ndarray", 42 "pack": pack_np_array, 43 "unpack": unpack_np_array, 44 }, 45] 46 47 48_CUSTOM_DISPATCHERS = {} 49 50 51class ComponentDispatcher: 52 """Automated Serialization/Conversion for the Entity Component System, can be used alone to serialize or deserialize data""" 53 54 def __init__(self, dispatcher_dicts: list[dict] = None, disparity: str = "raise"): 55 """Constructs a Component Dispatcher object instance 56 Args: 57 dispatcher_dicts (list[dict], optional): . Defaults to None. 58 disparity (str, optional): enumeration string ("raise","ignore", "warn") How the system treats pack/unpack mismatches. Defaults to "raise". 59 """ 60 self.conversions = { 61 "int": (int, int), 62 "str": (str, str), 63 "float": (float, float), 64 } 65 self._refresh_types() 66 self.__on_custom_mismatch = disparity 67 if dispatcher_dicts: 68 self.register_multiple_types(dispatcher_dicts) 69 70 def register_multiple_types(self, dispatcher_dicts: list[dict] = None): 71 """Register multiple converssion types through a list of dispatcher_dicts 72 (dictionaries composed of {name:identifier, type_string, pack, unpack}) 73 Args: 74 dispatcher_dicts (list, optional): _description_. Defaults to []. 75 """ 76 if dispatcher_dicts is None: 77 return 78 79 for dispatch in dispatcher_dicts: 80 self.register_type( 81 dispatch["type_string"], 82 pack_fn=dispatch["pack"], 83 unpack_fn=dispatch["unpack"], 84 ) 85 86 def _refresh_types(self): 87 """refreshes all the types using the available information from custom and default dispatchers""" 88 self.register_multiple_types(DEFAULT_DISPATCHERS) 89 self.overwrites = Counter() 90 if _CUSTOM_DISPATCHERS: 91 for t, fn_lst in _CUSTOM_DISPATCHERS.items(): 92 pack_fn, unpack_fn = fn_lst[0], fn_lst[1] 93 if self.__on_custom_mismatch == "raise": 94 assert pack_fn is not None, ( 95 f"Type {t} has an unpack function but no pack function registered!" 96 ) 97 assert unpack_fn is not None, ( 98 f"Type {t} has a pack function but no unpack function registered!" 99 ) 100 elif self.__on_custom_mismatch == "ignore": 101 if pack_fn is None or unpack_fn is None: 102 continue 103 elif self.__on_custom_mismatch == "warn": 104 log.warn( 105 f"For unpack, pack for type<< {t} >>, {'un' if unpack_fn is None else ''}pack function definition is missing " 106 ) 107 self.register_type(t, pack_fn, unpack_fn) 108 109 def register_type(self, t: str | type, pack_fn: callable, unpack_fn: callable): 110 """Registers a type (via str or type) to be serialized (pack_fn) and deserialized (unpack_fn) via the given callables 111 Args: 112 t (str | type): the type that these methods are for as a type primitive or type string (from ComponentDispatcher.get_type_string() ) 113 pack_fn (callable): the function used to serialize the specific type for storage 114 unpack_fn (callable): the function used to deserialize reverse serialization for runtime. 115 """ 116 if isinstance(t, type): 117 t = self.get_type_string(t) 118 119 if self.conversions.get(t) is not None: 120 self.overwrites[t] += 1 121 self.conversions[t] = (pack_fn, unpack_fn) 122 123 def get_overwrites(self): 124 """Returns a dictionary showing the counts of how often different types were overwritten, used for debugging""" 125 return dict(self.overwrites) 126 127 @staticmethod 128 def get_type_string(t: type): 129 """Generates a standardised type string from a given type primitive 130 Args: 131 t (type): primtive type to be named via string 132 Returns: 133 str: The name that will be used for the Component Dispatcher when given this type 134 """ 135 return f"{t.__module__}.{t.__name__}" 136 137 def pack(self, type_string: str, data): 138 """serializes a type, returning the data from the pack function""" 139 pack_fn = self.conversions.get(type_string, (lambda x: x, None))[0] 140 return pack_fn(data) 141 142 def pack_multiple(self, typ_data_tpls: list[tuple[str, Any]]): 143 """Serializes multiple types via pack function""" 144 out_data = [] 145 for typ, data in typ_data_tpls: 146 out_data.append(self.pack(typ, data)) 147 return out_data 148 149 def unpack(self, type_string: str, data): 150 """Deserializes data into runtime data via unpack function""" 151 unpack_fn = self.conversions.get(type_string, (None, lambda x: x))[1] 152 return unpack_fn(data) 153 154 def unpack_multiple(self, typ_data_tpls): 155 """Deserializes multiple types into runtime data via unpack function""" 156 out_data = [] 157 for typ, data in typ_data_tpls: 158 out_data.append(self.unpack(typ, data)) 159 return out_data 160 161 # def register_from_file(self, path): 162 # with open(path, "r", encoding="utf-8") as f: 163 # types_array = json.load(f) 164 # self.unpack_multiple(types_array) 165 166 167# def register_pack(func: callable, t: type): 168# t_str = ComponentDispatcher.get_type_string(t) 169# func_lst = __CUSTOM_DISPATCHERS.get(t_str, [None, None]) 170# assert func_lst[0] is None, f"Pack function decorator for {t_str} already exists" 171# func_lst[0] = func 172# __CUSTOM_DISPATCHERS[t_str] = func_lst 173# return func 174 175 176def packer(t: type): 177 """Decorator to add this function to the Component dispatcher as a pack function for the given type 178 Args: 179 t (type): type that this system will be used for 180 """ 181 182 def wrapper(func: callable): 183 t_str = ComponentDispatcher.get_type_string(t) 184 func_lst = _CUSTOM_DISPATCHERS.get(t_str, [None, None]) 185 func_lst[0] = func 186 _CUSTOM_DISPATCHERS[t_str] = func_lst 187 return func 188 189 return wrapper 190 191 192def unpacker(t: type): 193 """Decorator to add this function to the Component dispatcher as an unpack function for the given type 194 Args: 195 t (type): type that this system will be used for 196 """ 197 198 def wrapper(func: callable): 199 t_str = ComponentDispatcher.get_type_string(t) 200 func_lst = _CUSTOM_DISPATCHERS.get(t_str, [None, None]) 201 assert func_lst[1] is None, ( 202 f"Unpack function decorator for {t_str} already exists" 203 ) 204 func_lst[1] = func 205 _CUSTOM_DISPATCHERS[t_str] = func_lst 206 return func 207 208 return wrapper
52class ComponentDispatcher: 53 """Automated Serialization/Conversion for the Entity Component System, can be used alone to serialize or deserialize data""" 54 55 def __init__(self, dispatcher_dicts: list[dict] = None, disparity: str = "raise"): 56 """Constructs a Component Dispatcher object instance 57 Args: 58 dispatcher_dicts (list[dict], optional): . Defaults to None. 59 disparity (str, optional): enumeration string ("raise","ignore", "warn") How the system treats pack/unpack mismatches. Defaults to "raise". 60 """ 61 self.conversions = { 62 "int": (int, int), 63 "str": (str, str), 64 "float": (float, float), 65 } 66 self._refresh_types() 67 self.__on_custom_mismatch = disparity 68 if dispatcher_dicts: 69 self.register_multiple_types(dispatcher_dicts) 70 71 def register_multiple_types(self, dispatcher_dicts: list[dict] = None): 72 """Register multiple converssion types through a list of dispatcher_dicts 73 (dictionaries composed of {name:identifier, type_string, pack, unpack}) 74 Args: 75 dispatcher_dicts (list, optional): _description_. Defaults to []. 76 """ 77 if dispatcher_dicts is None: 78 return 79 80 for dispatch in dispatcher_dicts: 81 self.register_type( 82 dispatch["type_string"], 83 pack_fn=dispatch["pack"], 84 unpack_fn=dispatch["unpack"], 85 ) 86 87 def _refresh_types(self): 88 """refreshes all the types using the available information from custom and default dispatchers""" 89 self.register_multiple_types(DEFAULT_DISPATCHERS) 90 self.overwrites = Counter() 91 if _CUSTOM_DISPATCHERS: 92 for t, fn_lst in _CUSTOM_DISPATCHERS.items(): 93 pack_fn, unpack_fn = fn_lst[0], fn_lst[1] 94 if self.__on_custom_mismatch == "raise": 95 assert pack_fn is not None, ( 96 f"Type {t} has an unpack function but no pack function registered!" 97 ) 98 assert unpack_fn is not None, ( 99 f"Type {t} has a pack function but no unpack function registered!" 100 ) 101 elif self.__on_custom_mismatch == "ignore": 102 if pack_fn is None or unpack_fn is None: 103 continue 104 elif self.__on_custom_mismatch == "warn": 105 log.warn( 106 f"For unpack, pack for type<< {t} >>, {'un' if unpack_fn is None else ''}pack function definition is missing " 107 ) 108 self.register_type(t, pack_fn, unpack_fn) 109 110 def register_type(self, t: str | type, pack_fn: callable, unpack_fn: callable): 111 """Registers a type (via str or type) to be serialized (pack_fn) and deserialized (unpack_fn) via the given callables 112 Args: 113 t (str | type): the type that these methods are for as a type primitive or type string (from ComponentDispatcher.get_type_string() ) 114 pack_fn (callable): the function used to serialize the specific type for storage 115 unpack_fn (callable): the function used to deserialize reverse serialization for runtime. 116 """ 117 if isinstance(t, type): 118 t = self.get_type_string(t) 119 120 if self.conversions.get(t) is not None: 121 self.overwrites[t] += 1 122 self.conversions[t] = (pack_fn, unpack_fn) 123 124 def get_overwrites(self): 125 """Returns a dictionary showing the counts of how often different types were overwritten, used for debugging""" 126 return dict(self.overwrites) 127 128 @staticmethod 129 def get_type_string(t: type): 130 """Generates a standardised type string from a given type primitive 131 Args: 132 t (type): primtive type to be named via string 133 Returns: 134 str: The name that will be used for the Component Dispatcher when given this type 135 """ 136 return f"{t.__module__}.{t.__name__}" 137 138 def pack(self, type_string: str, data): 139 """serializes a type, returning the data from the pack function""" 140 pack_fn = self.conversions.get(type_string, (lambda x: x, None))[0] 141 return pack_fn(data) 142 143 def pack_multiple(self, typ_data_tpls: list[tuple[str, Any]]): 144 """Serializes multiple types via pack function""" 145 out_data = [] 146 for typ, data in typ_data_tpls: 147 out_data.append(self.pack(typ, data)) 148 return out_data 149 150 def unpack(self, type_string: str, data): 151 """Deserializes data into runtime data via unpack function""" 152 unpack_fn = self.conversions.get(type_string, (None, lambda x: x))[1] 153 return unpack_fn(data) 154 155 def unpack_multiple(self, typ_data_tpls): 156 """Deserializes multiple types into runtime data via unpack function""" 157 out_data = [] 158 for typ, data in typ_data_tpls: 159 out_data.append(self.unpack(typ, data)) 160 return out_data 161 162 # def register_from_file(self, path): 163 # with open(path, "r", encoding="utf-8") as f: 164 # types_array = json.load(f) 165 # self.unpack_multiple(types_array)
Automated Serialization/Conversion for the Entity Component System, can be used alone to serialize or deserialize data
55 def __init__(self, dispatcher_dicts: list[dict] = None, disparity: str = "raise"): 56 """Constructs a Component Dispatcher object instance 57 Args: 58 dispatcher_dicts (list[dict], optional): . Defaults to None. 59 disparity (str, optional): enumeration string ("raise","ignore", "warn") How the system treats pack/unpack mismatches. Defaults to "raise". 60 """ 61 self.conversions = { 62 "int": (int, int), 63 "str": (str, str), 64 "float": (float, float), 65 } 66 self._refresh_types() 67 self.__on_custom_mismatch = disparity 68 if dispatcher_dicts: 69 self.register_multiple_types(dispatcher_dicts)
Constructs a Component Dispatcher object instance Args: dispatcher_dicts (list[dict], optional): . Defaults to None. disparity (str, optional): enumeration string ("raise","ignore", "warn") How the system treats pack/unpack mismatches. Defaults to "raise".
71 def register_multiple_types(self, dispatcher_dicts: list[dict] = None): 72 """Register multiple converssion types through a list of dispatcher_dicts 73 (dictionaries composed of {name:identifier, type_string, pack, unpack}) 74 Args: 75 dispatcher_dicts (list, optional): _description_. Defaults to []. 76 """ 77 if dispatcher_dicts is None: 78 return 79 80 for dispatch in dispatcher_dicts: 81 self.register_type( 82 dispatch["type_string"], 83 pack_fn=dispatch["pack"], 84 unpack_fn=dispatch["unpack"], 85 )
Register multiple converssion types through a list of dispatcher_dicts (dictionaries composed of {name:identifier, type_string, pack, unpack}) Args: dispatcher_dicts (list, optional): _description_. Defaults to [].
110 def register_type(self, t: str | type, pack_fn: callable, unpack_fn: callable): 111 """Registers a type (via str or type) to be serialized (pack_fn) and deserialized (unpack_fn) via the given callables 112 Args: 113 t (str | type): the type that these methods are for as a type primitive or type string (from ComponentDispatcher.get_type_string() ) 114 pack_fn (callable): the function used to serialize the specific type for storage 115 unpack_fn (callable): the function used to deserialize reverse serialization for runtime. 116 """ 117 if isinstance(t, type): 118 t = self.get_type_string(t) 119 120 if self.conversions.get(t) is not None: 121 self.overwrites[t] += 1 122 self.conversions[t] = (pack_fn, unpack_fn)
Registers a type (via str or type) to be serialized (pack_fn) and deserialized (unpack_fn) via the given callables Args: t (str | type): the type that these methods are for as a type primitive or type string (from ComponentDispatcher.get_type_string() ) pack_fn (callable): the function used to serialize the specific type for storage unpack_fn (callable): the function used to deserialize reverse serialization for runtime.
124 def get_overwrites(self): 125 """Returns a dictionary showing the counts of how often different types were overwritten, used for debugging""" 126 return dict(self.overwrites)
Returns a dictionary showing the counts of how often different types were overwritten, used for debugging
128 @staticmethod 129 def get_type_string(t: type): 130 """Generates a standardised type string from a given type primitive 131 Args: 132 t (type): primtive type to be named via string 133 Returns: 134 str: The name that will be used for the Component Dispatcher when given this type 135 """ 136 return f"{t.__module__}.{t.__name__}"
Generates a standardised type string from a given type primitive Args: t (type): primtive type to be named via string Returns: str: The name that will be used for the Component Dispatcher when given this type
138 def pack(self, type_string: str, data): 139 """serializes a type, returning the data from the pack function""" 140 pack_fn = self.conversions.get(type_string, (lambda x: x, None))[0] 141 return pack_fn(data)
serializes a type, returning the data from the pack function
143 def pack_multiple(self, typ_data_tpls: list[tuple[str, Any]]): 144 """Serializes multiple types via pack function""" 145 out_data = [] 146 for typ, data in typ_data_tpls: 147 out_data.append(self.pack(typ, data)) 148 return out_data
Serializes multiple types via pack function
150 def unpack(self, type_string: str, data): 151 """Deserializes data into runtime data via unpack function""" 152 unpack_fn = self.conversions.get(type_string, (None, lambda x: x))[1] 153 return unpack_fn(data)
Deserializes data into runtime data via unpack function
155 def unpack_multiple(self, typ_data_tpls): 156 """Deserializes multiple types into runtime data via unpack function""" 157 out_data = [] 158 for typ, data in typ_data_tpls: 159 out_data.append(self.unpack(typ, data)) 160 return out_data
Deserializes multiple types into runtime data via unpack function
177def packer(t: type): 178 """Decorator to add this function to the Component dispatcher as a pack function for the given type 179 Args: 180 t (type): type that this system will be used for 181 """ 182 183 def wrapper(func: callable): 184 t_str = ComponentDispatcher.get_type_string(t) 185 func_lst = _CUSTOM_DISPATCHERS.get(t_str, [None, None]) 186 func_lst[0] = func 187 _CUSTOM_DISPATCHERS[t_str] = func_lst 188 return func 189 190 return wrapper
Decorator to add this function to the Component dispatcher as a pack function for the given type Args: t (type): type that this system will be used for
193def unpacker(t: type): 194 """Decorator to add this function to the Component dispatcher as an unpack function for the given type 195 Args: 196 t (type): type that this system will be used for 197 """ 198 199 def wrapper(func: callable): 200 t_str = ComponentDispatcher.get_type_string(t) 201 func_lst = _CUSTOM_DISPATCHERS.get(t_str, [None, None]) 202 assert func_lst[1] is None, ( 203 f"Unpack function decorator for {t_str} already exists" 204 ) 205 func_lst[1] = func 206 _CUSTOM_DISPATCHERS[t_str] = func_lst 207 return func 208 209 return wrapper
Decorator to add this function to the Component dispatcher as an unpack function for the given type Args: t (type): type that this system will be used for