Beispiel #1
0
def assert_vars(obj):
    for language in serializer.get_formats():
        parser = serializer.get_parser(language)
        serialized = parser.dumps(obj)
        restored = parser.loads(serialized)

        for var in vars(obj):
            if var not in ("__dict__", "__weakref__", "__module__"):
                assert getattr(obj, var) == getattr(restored, var)
Beispiel #2
0
def assert_slots(obj):
    for language in serializer.get_formats():
        parser = serializer.get_parser(language)
        serialized = parser.dumps(obj)
        restored = parser.loads(serialized)

        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)
Beispiel #3
0
def main():
    l = [['lol', 'math', 54], ['pop', 8.46]]
    d = {
        56: "lol",
        "math": {
            "pi": 3.14,
            56.34: True,
            None: ["pop", "pip"]
        },
        21: [52, "string", False],
        True: False,
        None: 5.633
    }
    yaml_parser = serializer.get_parser("YAML")
    s = yaml_parser.dumps(d)
    m = yaml_parser.loads(s)
    print(s)
    print(m)
Beispiel #4
0
def assert_vars(obj):
    for language in serializer.get_formats():

        loader = serializer.get_parser(language)
        read_mode = loader.read_mod
        write_mode = loader.write_mod

        name = f"test.{language}"

        with open(name, write_mode) as fp:
            serializer.dump(obj, fp, language)

        with open(name, read_mode) as fp:
            try:
                restored = serializer.load(fp)
            finally:
                os.remove(name)

        for var in vars(obj):
            if var not in ("__dict__", "__weakref__", "__module__"):
                assert getattr(obj, var) == getattr(restored, var)
Beispiel #5
0
#!/usr/bin/env python

from lib.factory import serializer

types = {
    "json": serializer.get_parser("json"),
    "yaml": serializer.get_parser("yaml")
}


def dump(in_file, out_file):
    ifile = open(in_file, "r")
    ofile = open(out_file, "w")
    obj = types[in_file.split(".")[-1].lower()].unpack(ifile)
    types[out_file.split(".")[-1].lower()].pack(obj, ofile)
    ifile.close()
    ofile.close()