Exemple #1
0
    def test_refinement_dict_setdefault(self):
        dct = RefinementDict({w: "w"}, is_element=le)

        dct.setdefault(x, "x")

        with self.subTest("Existing values ignore default"):
            self.assertEqual("w", dct[w])
            self.assertEqual("w", dct[y])
            self.assertEqual("w", dct[z])

        with self.subTest("Fall back to default value"):
            self.assertEqual("x", dct[x])
Exemple #2
0
    def test_refinement_dict_basic(self):
        dct = RefinementDict({a: "a", b: "b"})

        self.assertEqual("a", dct[1])
        self.assertEqual("b", dct[2])

        with self.assertRaises(KeyError):
            dct[3]
    def __init__(self, serialization_functions: dict, deserialization_functions: dict):
        self.serialization_functions = RefinementDict(
            serialization_functions, is_subset=issubclass, is_element=isinstance
        )
        self.deserialization_functions = RefinementDict(
            deserialization_functions, is_subset=issubclass, is_element=issubclass
        )

        self.serialization_functions.setdefault(
            dataclass, lambda obj: self.serialize(dict_serialization(obj.__dict__))
        )

        self.deserialization_functions.setdefault(
            dataclass, dict_to_dataclass(deserialization_func=self.deserialize)
        )
        self.deserialization_functions.setdefault(
            Union, union_deserialization(deserialization_func=self.deserialize)
        )
Exemple #4
0
    def test_refinement_dict_lattice_ambiguous(self):
        dct = RefinementDict({w: "w", x: "x"}, is_element=le)

        with self.subTest("With ambiguity"):
            with self.assertRaises(AmbiguousKeyError):
                dct[z]

        with self.subTest("Resolve ambiguity"):
            dct[y] = "y"

            self.assertEqual("y", dct[z])
Exemple #5
0
    def test_refinement_dict_ambiguous(self):
        dct = RefinementDict({c: "c", d: "d"})

        with self.subTest("With ambiguity"):
            with self.assertRaises(AmbiguousKeyError):
                dct[2]

        with self.subTest("Resolve ambiguity"):
            dct[b] = "b"

            self.assertEqual("b", dct[2])
class Serializer:
    serialization_functions: RefinementDict
    deserialization_functions: RefinementDict

    def __init__(self, serialization_functions: dict, deserialization_functions: dict):
        self.serialization_functions = RefinementDict(
            serialization_functions, is_subset=issubclass, is_element=isinstance
        )
        self.deserialization_functions = RefinementDict(
            deserialization_functions, is_subset=issubclass, is_element=issubclass
        )

        self.serialization_functions.setdefault(
            dataclass, lambda obj: self.serialize(dict_serialization(obj.__dict__))
        )

        self.deserialization_functions.setdefault(
            dataclass, dict_to_dataclass(deserialization_func=self.deserialize)
        )
        self.deserialization_functions.setdefault(
            Union, union_deserialization(deserialization_func=self.deserialize)
        )

    def serialize(self, obj):
        """
        Serialize given Python object
        """

        try:
            serialization_func = self.serialization_functions[obj]
        except KeyError:
            raise SerializationError("Cannot serialize type {}".format(type(obj)))

        return serialization_func(obj)

    @curry
    def deserialize(self, cls, serialized_obj):
        """
        Attempt to deserialize serialized object as given type
        """

        try:
            deserialization_func = self.deserialization_functions[cls]
        except KeyError:
            raise DeserializationError("Cannot deserialize type {}".format(cls))

        return deserialization_func(cls, serialized_obj)

    @curry
    def register_serializer(self, cls, func):
        self.serialization_functions[cls] = func

    @curry
    def register_deserializer(self, cls, func):
        self.deserialization_functions[cls] = func

    def register(self, cls, serialization_func, deserialization_func):
        self.register_serializer(cls, serialization_func)
        self.register_deserializer(cls, deserialization_func)
Exemple #7
0
    def test_refinement_dict_lattice_refinement(self):
        dct = RefinementDict({w: "w"}, is_element=le)

        self.assertEqual("w", dct[y])
Exemple #8
0
    def test_refinement_dict_lattice(self):
        dct = RefinementDict({w: "w", x: "x"}, is_element=le)

        self.assertEqual("w", dct[w])
        self.assertEqual("x", dct[x])
Exemple #9
0
    def test_refinement_dict_refinement(self):
        dct = RefinementDict({a: "a", c: "c"})

        self.assertEqual("a", dct[1])
        self.assertEqual("c", dct[2])