How to use object_hook method in assertpy

Best Python code snippet using assertpy_python

json_serialization.py

Source:json_serialization.py Github

copy

Full Screen

...64 return d65 return DesignBlock(66 label=d['label'],67 definition=BlockDefinitionDecoder(68 self.__symbol_table).object_hook(d['definition'])69 )70class GenerateBlockEncoder(json.JSONEncoder):71 def default(self, obj):72 # pylint: disable=E020273 if isinstance(obj, GenerateBlock):74 rep = dict()75 rep['block_type'] = 'generate_block'76 rep['treatment_name'] = obj.treatment.name77 rep['attribute_name'] = obj.attribute_name78 rep['values'] = [79 ValueEncoder().default(value) for value in obj.values80 ]81 return rep82 return super().default(obj)83class GenerateBlockDecoder(json.JSONDecoder):84 def __init__(self, symbol_table):85 self.__symbol_table = symbol_table86 super().__init__(object_hook=self.convert)87 def convert(self, d):88 if 'block_type' not in d:89 return d90 if d['block_type'] != 'generate_block':91 return d92 if 'attribute_name' not in d:93 return d94 if 'treatment_name' not in d:95 return d96 if d['treatment_name'] not in self.__symbol_table:97 return d98 treatment = self.__symbol_table[d['treatment_name']]99 return GenerateBlock(100 treatment=treatment,101 attribute_name=d['attribute_name'],102 values=[103 ValueDecoder().object_hook(value) for value in d['values']104 ])105class ProductBlockEncoder(json.JSONEncoder):106 def default(self, obj):107 # pylint: disable=E0202108 if isinstance(obj, ProductBlock):109 rep = dict()110 rep['block_type'] = 'product_block'111 rep['block_list'] = [112 BlockDefinitionEncoder().default(block)113 for block in obj.block_list114 ]115 return rep116 return super().default(obj)117class ProductBlockDecoder(json.JSONDecoder):118 def __init__(self, symbol_table):119 self.__symbol_table = symbol_table120 super().__init__(object_hook=self.convert)121 def convert(self, d):122 if 'block_type' not in d:123 return d124 if d['block_type'] != 'product_block':125 return d126 if 'block_list' not in d:127 return d128 return ProductBlock(block_list=[129 BlockDefinitionDecoder(self.__symbol_table).object_hook(block)130 for block in d['block_list']131 ])132class ReplicateBlockEncoder(json.JSONEncoder):133 def default(self, obj):134 # pylint: disable=E0202135 if isinstance(obj, ReplicateBlock):136 rep = dict()137 rep['block_type'] = 'replicate_block'138 rep['count'] = obj.count139 rep['block'] = BlockDefinitionEncoder().default(obj.block)140 return rep141 return super().default(obj)142class ReplicateBlockDecoder(json.JSONDecoder):143 def __init__(self, symbol_table):144 self.__symbol_table = symbol_table145 super().__init__(object_hook=self.convert)146 def convert(self, d):147 if 'block_type' not in d:148 return d149 if d['block_type'] != 'replicate_block':150 return d151 if 'count' not in d:152 return d153 if 'block' not in d:154 return d155 return ReplicateBlock(156 count=d['count'],157 block=BlockDefinitionDecoder(158 self.__symbol_table).object_hook(d['block'])159 )160class SubjectReferenceEncoder(json.JSONEncoder):161 def default(self, obj):162 # pylint: disable=E0202163 if isinstance(obj, SubjectReference):164 rep = dict()165 rep['block_type'] = 'subject_reference'166 rep['reference'] = obj.entity.name167 return rep168 return super().default(obj)169class SubjectReferenceDecoder(json.JSONDecoder):170 def __init__(self, symbol_table):171 self.__symbol_table = symbol_table172 super().__init__(object_hook=self.convert)173 def convert(self, d):174 if 'block_type' not in d:175 return d176 if d['block_type'] != 'subject_reference':177 return d178 if 'reference' not in d:179 return d180 if d['reference'] not in self.__symbol_table:181 return d182 return SubjectReference(entity=self.__symbol_table[d['reference']])183class SumBlockEncoder(json.JSONEncoder):184 def default(self, obj):185 # pylint: disable=E0202186 if isinstance(obj, SumBlock):187 rep = dict()188 rep['block_type'] = 'sum_block'189 rep['block_list'] = [190 BlockDefinitionEncoder().default(block)191 for block in obj.block_list192 ]193 return rep194 return super().default(obj)195class SumBlockDecoder(json.JSONDecoder):196 def __init__(self, symbol_table):197 self.__symbol_table = symbol_table198 super().__init__(object_hook=self.convert)199 def convert(self, d):200 if 'block_type' not in d:201 return d202 if d['block_type'] != 'sum_block':203 return d204 if 'block_list' not in d:205 return d206 return SumBlock(207 block_list=[208 BlockDefinitionDecoder(self.__symbol_table).object_hook(block)209 for block in d['block_list']210 ])211class TreatmentReferenceEncoder(json.JSONEncoder):212 def default(self, obj):213 # pylint: disable=E0202214 if isinstance(obj, TreatmentReference):215 rep = dict()216 rep['block_type'] = 'treatment_reference'217 rep['reference'] = obj.treatment_name218 if isinstance(obj, TreatmentValueReference):219 rep['block_type'] = 'value_treatment_reference'220 rep['value'] = ValueEncoder().default(obj.value)221 return rep222 return super().default(obj)223class TreatmentReferenceDecoder(json.JSONDecoder):224 def __init__(self, symbol_table: Dict[str, Treatment]):225 self.__symbol_table = symbol_table226 super().__init__(object_hook=self.convert)227 def convert(self, d):228 if isinstance(d, TreatmentReference):229 return d230 if 'block_type' not in d:231 return d232 if 'reference' not in d:233 return d234 if d['reference'] not in self.__symbol_table:235 return d236 treatment = self.__symbol_table[d['reference']]237 if d['block_type'] == 'value_treatment_reference':238 if 'value' not in d:239 return d240 return TreatmentValueReference(241 treatment=treatment,242 value=ValueDecoder().object_hook(d['value'])243 )244 return TreatmentReference(treatment=treatment)245class BlockDefinitionEncoder(json.JSONEncoder):246 def default(self, obj):247 # pylint: disable=E0202248 if isinstance(obj, BlockReference):249 return BlockReferenceEncoder().default(obj)250 if isinstance(obj, GenerateBlock):251 return GenerateBlockEncoder().default(obj)252 if isinstance(obj, ProductBlock):253 return ProductBlockEncoder().default(obj)254 if isinstance(obj, ReplicateBlock):255 return ReplicateBlockEncoder().default(obj)256 if isinstance(obj, SubjectReference):257 return SubjectReferenceEncoder().default(obj)258 if isinstance(obj, SumBlock):259 return SumBlockEncoder().default(obj)260 if isinstance(obj, TreatmentReference):261 return TreatmentReferenceEncoder().default(obj)262 return super().default(obj)263class BlockDefinitionDecoder(json.JSONDecoder):264 def __init__(self, symbol_table):265 self.__symbol_table = symbol_table266 super().__init__(object_hook=self.convert)267 def convert(self, d):268 if isinstance(d, BlockDefinition):269 return d270 if 'block_type' not in d:271 return d272 if d['block_type'] == 'block_reference':273 return BlockReferenceDecoder(self.__symbol_table).object_hook(d)274 if d['block_type'] == 'generate_block':275 return GenerateBlockDecoder(self.__symbol_table).object_hook(d)276 if d['block_type'] == 'product_block':277 return ProductBlockDecoder(self.__symbol_table).object_hook(d)278 if d['block_type'] == 'replicate_block':279 return ReplicateBlockDecoder(self.__symbol_table).object_hook(d)280 if d['block_type'] == 'subject_reference':281 return SubjectReferenceDecoder(self.__symbol_table).object_hook(d)282 if d['block_type'] == 'sum_block':283 return SumBlockDecoder(self.__symbol_table).object_hook(d)284 if (d['block_type'] == 'treatment_reference'285 or d['block_type'] == 'value_treatment_reference'):286 return TreatmentReferenceDecoder(287 self.__symbol_table).object_hook(d)...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

1# coding: utf-82import functools3import catalogue4from ._version import version5from .exceptions import *6from ._packer import Packer as _Packer7from ._unpacker import unpackb as _unpackb8from ._unpacker import unpack as _unpack9from ._unpacker import Unpacker as _Unpacker10from ._ext_type import ExtType11from ._msgpack_numpy import encode_numpy as _encode_numpy12from ._msgpack_numpy import decode_numpy as _decode_numpy13msgpack_encoders = catalogue.create("srsly", "msgpack_encoders", entry_points=True)14msgpack_decoders = catalogue.create("srsly", "msgpack_decoders", entry_points=True)15msgpack_encoders.register("numpy", func=_encode_numpy)16msgpack_decoders.register("numpy", func=_decode_numpy)17# msgpack_numpy extensions18class Packer(_Packer):19 def __init__(self, *args, **kwargs):20 default = kwargs.get("default")21 for encoder in msgpack_encoders.get_all().values():22 default = functools.partial(encoder, chain=default)23 kwargs["default"] = default24 super(Packer, self).__init__(*args, **kwargs)25class Unpacker(_Unpacker):26 def __init__(self, *args, **kwargs):27 object_hook = kwargs.get("object_hook")28 for decoder in msgpack_decoders.get_all().values():29 object_hook = functools.partial(decoder, chain=object_hook)30 kwargs["object_hook"] = object_hook31 super(Unpacker, self).__init__(*args, **kwargs)32def pack(o, stream, **kwargs):33 """34 Pack an object and write it to a stream.35 """36 packer = Packer(**kwargs)37 stream.write(packer.pack(o))38def packb(o, **kwargs):39 """40 Pack an object and return the packed bytes.41 """42 return Packer(**kwargs).pack(o)43def unpack(stream, **kwargs):44 """45 Unpack a packed object from a stream.46 """47 if "object_pairs_hook" not in kwargs:48 object_hook = kwargs.get("object_hook")49 for decoder in msgpack_decoders.get_all().values():50 object_hook = functools.partial(decoder, chain=object_hook)51 kwargs["object_hook"] = object_hook52 return _unpack(stream, **kwargs)53def unpackb(packed, **kwargs):54 """55 Unpack a packed object.56 """57 if "object_pairs_hook" not in kwargs:58 object_hook = kwargs.get("object_hook")59 for decoder in msgpack_decoders.get_all().values():60 object_hook = functools.partial(decoder, chain=object_hook)61 kwargs["object_hook"] = object_hook62 return _unpackb(packed, **kwargs)63# alias for compatibility to simplejson/marshal/pickle.64load = unpack65loads = unpackb66dump = pack...

Full Screen

Full Screen

test_decoders.py

Source:test_decoders.py Github

copy

Full Screen

1import pytest2import rapidjson3from toll_booth.obj.graph.serializers import GqlDecoder4from toll_booth.obj.graph.trident.connections import TridentDecoder5from algernon import ajson6@pytest.mark.trident_decoder7class TestTridentDecoder:8 def test_decode_vertex(self, db_get_vertex_response):9 results = rapidjson.loads(rapidjson.dumps(db_get_vertex_response), object_hook=TridentDecoder.object_hook)10 assert results11 def test_decode_vertex_properties(self, db_vertex_vertex_properties_response):12 results = rapidjson.loads(rapidjson.dumps(db_vertex_vertex_properties_response), object_hook=TridentDecoder.object_hook)13 assert results14@pytest.mark.gql_decoder15class TestGqlDecoder:16 def test_gql_decoder(self, db_get_vertex_response):17 vertex = rapidjson.loads(rapidjson.dumps(db_get_vertex_response), object_hook=TridentDecoder.object_hook)18 vertex_string = ajson.dumps(vertex)19 gql = rapidjson.loads(vertex_string, object_hook=GqlDecoder.object_hook)20 assert gql21 def test_vertex_vertex_properties(self, db_vertex_vertex_properties_response):22 vertex_properties = rapidjson.loads(23 rapidjson.dumps(db_vertex_vertex_properties_response), object_hook=TridentDecoder.object_hook)24 vertex_properties_string = ajson.dumps(vertex_properties)25 gql = rapidjson.loads(vertex_properties_string, object_hook=GqlDecoder.object_hook)26 assert gql27 def test_add_vertex_response(self, db_add_vertex_response):28 vertex = rapidjson.loads(29 rapidjson.dumps(db_add_vertex_response), object_hook=TridentDecoder.object_hook)30 vertex_string = ajson.dumps(vertex)31 gql = rapidjson.loads(vertex_string, object_hook=GqlDecoder.object_hook)32 assert gql33 def test_get_connected_edge_response(self, test_event):34 event = test_event('connected_edges')35 strung_response = ajson.dumps(event)36 rebuilt_response = rapidjson.loads(strung_response, object_hook=GqlDecoder.object_hook)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run assertpy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful