def test_toml_class(self): toml_class = serializer.dumps(test_objects.MyClass, 'toml') deserialized_class = serializer.loads(toml_class, 'toml') self.assertEqual(deserialized_class.a, test_objects.MyClass.a) self.assertEqual(deserialized_class.my_func(1), test_objects.MyClass.my_func(1))
def test_pickle_class(self): pickle_class = serializer.dumps(test1.MyClass, 'pickle') deserialized_class = serializer.loads(pickle_class, 'pickle') self.assertEqual(deserialized_class.a, test1.MyClass.a) self.assertEqual(deserialized_class.my_func(1), test1.MyClass.my_func(1))
def __client_recv(self): header = self.recvall(self.socket, 24) if not header: return None _, ID, src, dst, msg, dataLength = self.msg.unpack(header) data = self.recvall(self.socket, dataLength) return msg, serializer.loads(data)
def assert_vars(obj): for language in serializer.get_formats(): serialized = serializer.dumps(obj, language) restored = serializer.loads(serialized, language) for var in vars(obj): if var not in ("__dict__", "__weakref__", "__module__"): assert getattr(obj, var) == getattr(restored, var)
def test_yaml_class(self): yaml_class = serializer.dumps(test1.MyClass, 'yaml') deserialized_class = serializer.loads(yaml_class, 'yaml') self.assertEqual(deserialized_class.a, test1.MyClass.a) self.assertEqual(deserialized_class.my_func(1), test1.MyClass.my_func(1))
def test_json_class(self): json_class = serializer.dumps(test1.MyClass, 'json') deserialized_class = serializer.loads(json_class, 'json') self.assertEqual(deserialized_class.a, test1.MyClass.a) self.assertEqual(deserialized_class.my_func(1), test1.MyClass.my_func(1))
def save_act_table(act_table, act_id): ''' Save table of act from python collections. :param act_table: Data of act table. ''' if act_id: session = create_session(DATABASES['main'].metadata.bind) session.query(ActTable).filter(ActTable.act == act_id).delete() return loads(act_table, ActTable, session)
def assert_slots(obj): for language in serializer.get_formats(): serialized = serializer.dumps(obj, language) restored = serializer.loads(serialized, language) for var in type(obj).__slots__: if hasattr(obj, var) and hasattr(restored, var): assert getattr(obj, var) == getattr(restored, var) else: assert hasattr(obj, var) == hasattr(restored, var)
def test_closure_func(): subject = indent(5) result = subject(10) serialized = serializer.dumps(subject, "json") restored = serializer.loads(serialized, "json") restored_result = restored(10) assert_restored_object(subject) assert result == restored_result
def test_inheritance(): subject = ChildCls assert_vars(subject) serialized = serializer.dumps(subject, "json") restored = serializer.loads(serialized, "json") assert restored.__bases__[0].__name__ == subject.__bases__[0].__name__ assert restored.__bases__[1].__name__ == subject.__bases__[1].__name__ assert type(restored).__name__ == type(subject).__name__
def save_act(act): ''' Save information of act from python collections. :param act: Data of act. ''' session = create_session(DATABASES['main'].metadata.bind) if act.get('date'): date = datetime.strptime(act.get('date'), '%d.%m.%y').date() act['act_date'] = date return loads(act, Act, session)
def assert_restored_object(obj): for language in serializer.get_formats(): serialized = serializer.dumps(obj, language) restored = serializer.loads(serialized, language) attrs = type_to_attrs[type(obj)] for attr in attrs: if attr in ("__closure__", "__module__"): continue assert getattr(obj, attr) == getattr(restored, attr)
def test_indent_func(): subject = indent x_result = subject(5) y_result = x_result(10) serialized = serializer.dumps(subject, "json") restored = serializer.loads(serialized, "json") restored_x_result = restored(5) restored_y_result = restored_x_result(10) assert_restored_object(subject) assert_restored_object(x_result) assert y_result == restored_y_result
def __slave_data_generator(Data): start_index = 0 end_index = 4 INT_SIZE = 4 while 1: try: object_bytes = struct.unpack("<I", Data[start_index:end_index])[0] start_index += INT_SIZE end_index = start_index + object_bytes yield serializer.loads(Data[start_index:end_index]) start_index = end_index end_index += INT_SIZE except Exception: break
def __master_data_generator(data): INT_SIZE = 4 end_index = len(data) start_index = end_index - INT_SIZE while 1: try: object_bytes = struct.unpack("<I", data[start_index:end_index])[0] end_index = start_index start_index -= object_bytes yield serializer.loads(data[start_index:end_index]) end_index = start_index start_index -= INT_SIZE except Exception: break
def test_globals_func(): subject = f_globals import math globals()["math"] = math globals()["x"] = 5 result = subject() del math serialized = serializer.dumps(subject, "json") del globals()["math"] del globals()["x"] restored = serializer.loads(serialized, "json") restored_result = restored() assert_restored_object(subject) assert result == restored_result
def pcall(self, funcs, background=False): """Execute multiple calls in parallel. given 'funcs' as a dictionary of format {'key': ('function', kwargs)} return a a dictionary of format {'key': function(**kwargs)}""" ts = Taskset() for key, func in funcs.items(): arg = {'method': func[0], 'params': func[1]} arg.update(self.add_meta()) task = Task(func=self.service, arg=serializer.dumps(arg)) task.key = key task.background = background ts.add(task) self.gearman.do_taskset(ts) if not background: out = {} for task in ts.values(): res = serializer.loads(task.result) if res['error']: raise getattr(self.exception, res['error']['type'])(res['error']['message'], res['error'].get('traceback')) out[task.key] = res['result'] return out
def assert_restored_object(subject): for language in serializer.get_formats(): serialized = serializer.dumps(subject, language) restored = serializer.loads(serialized, language) assert restored == subject
def loads(self, json_string): return serializer.loads(json_string)
def test_yaml_string(self): yaml_str = serializer.dumps(test1.string, 'yaml') des_str = serializer.loads(yaml_str, 'yaml') self.assertEqual(test1.string, des_str)
def test_yaml_func_with_defaults(self): yaml_func = serializer.dumps(test1.func_with_defaults, 'yaml') des_func = serializer.loads(yaml_func, 'yaml') self.assertEqual(test1.func_with_defaults(), des_func())
def test_yaml_lambda(self): yaml_lmbd = serializer.dumps(test1.lmbd, 'yaml') des_lmbd = serializer.loads(yaml_lmbd, 'yaml') self.assertEqual(test1.lmbd(1), des_lmbd(1))
def test_yaml_func(self): yaml_func = serializer.dumps(test1.func, 'yaml') des_func = serializer.loads(yaml_func, 'yaml') self.assertEqual(test1.func(2), des_func(2))
def test_json_string(self): json_str = serializer.dumps(test1.string, 'json') des_str = serializer.loads(json_str, 'json') self.assertEqual(test1.string, des_str)
def test_yaml_dict(self): yaml_dict = serializer.dumps(test1.dict, 'yaml') des_dict = serializer.loads(yaml_dict, 'yaml') self.assertEqual(test1.dict, des_dict)
def test_yaml_list(self): yaml_list = serializer.dumps(test1.list, 'yaml') des_list = serializer.loads(yaml_list, 'yaml') self.assertEqual(test1.list, des_list)
def loads(self, formcode_string): return serializer.loads(formcode_string)
def test_toml_string(self): toml_str = serializer.dumps(test_objects.string, 'toml') des_str = serializer.loads(toml_str, 'toml') self.assertEqual(test_objects.string, des_str)
def test_toml_func_with_defaults(self): toml_func = serializer.dumps(test_objects.func_with_defaults, 'toml') des_func = serializer.loads(toml_func, 'toml') self.assertEqual(test_objects.func_with_defaults(), des_func())
def test_toml_list(self): toml_list = serializer.dumps(test_objects.list, 'toml') des_list = serializer.loads(toml_list, 'toml') self.assertEqual(test_objects.list, des_list)
def test_toml_dict(self): toml_dict = serializer.dumps(test_objects.dict, 'toml') des_dict = serializer.loads(toml_dict, 'toml') self.assertEqual(test_objects.dict, des_dict)
def loads(self, csv_string, header = True): return serializer.loads(csv_string, header)
def test_toml_func(self): toml_func = serializer.dumps(test_objects.func, 'toml') des_func = serializer.loads(toml_func, 'toml') self.assertEqual(test_objects.func(2), des_func(2))
def loads(self, bencode_string): return serializer.loads(bencode_string)
def test_toml_lambda(self): toml_lmbd = serializer.dumps(test_objects.lmbd, 'toml') des_lmbd = serializer.loads(toml_lmbd, 'toml') self.assertEqual(test_objects.lmbd(1), des_lmbd(1))