def write_field_one( self, c, d, schema_name, definition, name, field, opts, wrap=None ): field_class_name = None if self.resolver.has_ref(field): field_class_name, field = self.resolver.resolve_ref_definition( d, field, level=1 ) if field_class_name == schema_name and deepequal(field, definition): field_class_name = "self" if self.resolver.has_many(field): return self.write_field_many( c, d, field_class_name, definition, name, field, opts ) # finding original definition if self.resolver.has_ref(field): ref_name, field = self.resolver.resolve_ref_definition(d, field) if self.resolver.has_many(field): return self.write_field_many( c, d, field_class_name, definition, name, field, opts ) if ref_name is None: raise CodegenError("ref: %r is not found", field["$ref"]) logger.debug(" field: %s", lazy_json_dump(field)) self.accessor.update_option_on_property(c, field, opts) caller_name = self.accessor.resolver.resolve_caller_name(c, name, field) if caller_name is None: raise CodegenError( "matched field class is not found. name=%r, schema=%r", name, schema_name, ) normalized_name = self.resolver.resolve_normalized_name(name) if normalized_name != name: opts["data_key"] = name if keyword.iskeyword(normalized_name) or normalized_name == "fields": opts["data_key"] = normalized_name normalized_name = normalized_name + "_" kwargs = LazyKeywordsRepr(opts) if self.resolver.has_nested(d, field) and field_class_name: logger.debug(" nested: %s, %s", caller_name, field_class_name) if opts: kwargs = LazyFormat(", {}", kwargs) value = LazyFormat("{}({!r}{})", caller_name, field_class_name, kwargs) else: if caller_name == "fields.Nested": caller_name = "fields.Field" # field value = LazyFormat("{}({})", caller_name, kwargs) logger.info(" write field: write %s, field=%s", name, caller_name) if wrap is not None: value = wrap(value) c.m.stmt(LazyFormat("{} = {}", normalized_name, value))
def test_it(self): candidates = [ ("./src/01commit.yaml", "./src/00commit.yaml"), ("./src/01empty.yaml", "./src/02empty.yaml"), ] for src_file, dst_file in candidates: with self.subTest(src_file=src_file, dst_file=dst_file): noflatten = self.load_srcfile(src_file) actual = self._callFUT(noflatten) flatten = self.load_dstfile(dst_file) from dictknife import deepequal self.assertTrue(deepequal(flatten, actual))
def _callFUT(self, left, right, normalize): from dictknife import deepequal return deepequal(left, right, normalize=normalize)
from dictknife import deepequal a = { "errors": [{ "error": "invalid", "field": "email" }, { "error": "required", "field": "name" }], "success": False } b = { "success": False, "errors": [{ "error": "required", "field": "name" }, { "error": "invalid", "field": "email" }] } print("=") print("\t", a == b) print("deepequal") print("\t", deepequal(a, b, normalize=True))
from dictknife import deepequal from collections import OrderedDict, Counter d0 = {"a": 1, "b": {"x": OrderedDict({"i": 2}), "y": Counter({"j": 3})}} d1 = {"a": 1, "b": {"x": Counter({"i": 2}), "y": {"j": 3}}} # in almost case, this is ok. print("==") print("\t", d0 == d1) print("deepequal") print("\t", deepequal(d0, d1))
from dictknife import deepequal d0 = [[[1, 2, 3], [1]], [[1, 2], [2, 3], [3, 4]]] d1 = [[[1], [1, 2, 3]], [[1, 2], [3, 4], [2, 3]]] print("=") print(d0 == d1) print("deepequal") print(deepequal(d0, d1, normalize=True))