コード例 #1
0
ファイル: test_various.py プロジェクト: ramonhagenaars/jsons
 def test_deserialize_none(self):
     fork_inst = jsons.fork()
     jsons.set_deserializer(lambda *_, **__: 42,
                            cls=None,
                            fork_inst=fork_inst)
     self.assertEqual(
         42, jsons.load('Anything', cls=NoneType, fork_inst=fork_inst))
コード例 #2
0
    def get_serializer(cls):
        if not LMTrainingConfig._serializer:
            cls._serializer = jsons.fork()
            jsons.set_deserializer(jsons.default_object_deserializer,
                                   cls=cls,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=Arch,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=TrainingSchedule,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=Optimizer,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(PrepFunction.deserializer,
                                   cls=PrepFunction,
                                   fork_inst=cls._serializer)

            jsons.set_serializer(jsons.default_object_serializer,
                                 cls=cls,
                                 fork_inst=cls._serializer)
            jsons.set_serializer(PrepFunction.serializer,
                                 cls=PrepFunction,
                                 fork_inst=cls._serializer)

        return cls._serializer
コード例 #3
0
    def test_suppress_warning(self):
        fork_inst = jsons.fork()
        jsons.suppress_warning('some-warning', fork_inst)

        with warnings.catch_warnings(record=True) as w:
            fork_inst._warn('Some warning', 'some-warning')
            fork_inst._warn('Another warning', 'another-warning')

        self.assertEqual(1, len(w))
        self.assertIn('Another warning', str(w[0]))
コード例 #4
0
    def test_fork(self):
        f1 = jsons.fork()
        f2 = jsons.fork()
        f3 = jsons.fork(fork_inst=f1)

        jsons.set_serializer(lambda *_, **__: 'f1', str, fork_inst=f1)
        jsons.set_serializer(lambda *_, **__: 'f2', str, fork_inst=f2)
        jsons.set_serializer(lambda *_, **__: 3, int, fork_inst=f3)

        f4 = jsons.fork(fork_inst=f1)

        self.assertEqual(
            'f1', jsons.dump('I wanted a fork on the table.', fork_inst=f1))
        self.assertEqual(
            'f2', jsons.dump('I wanted a fork on the table.', fork_inst=f2))
        self.assertEqual('f3', jsons.dump('f3', fork_inst=f3))
        self.assertEqual(3, jsons.dump(42, fork_inst=f3))
        self.assertEqual(
            'f1', jsons.dump('I wanted a fork on the table.', fork_inst=f4))
コード例 #5
0
    def get_serializer(cls):
        if not cls._serializer:
            cls._serializer = jsons.fork()
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=Optimizer,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=TrainingSchedule,
                                   fork_inst=cls._serializer)

        return cls._serializer
コード例 #6
0
    def test_suppress_warnings(self):
        fork_inst = jsons.fork()
        jsons.suppress_warnings(True, fork_inst)

        with warnings.catch_warnings(record=True) as w:
            fork_inst._warn('Some warning', 'some-warning')
        self.assertEqual(0, len(w))

        jsons.suppress_warnings(False, fork_inst)

        with warnings.catch_warnings(record=True) as w:
            fork_inst._warn('Some warning', 'some-warning')
        self.assertEqual(1, len(w))
コード例 #7
0
    def get_serializer(cls):
        if not cls._serializer:
            cls._serializer = jsons.fork()
            jsons.set_deserializer(jsons.default_object_deserializer,
                                   cls=cls,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(field_based_deserializer_func,
                                   cls=Arch,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(PrepFunction.deserializer,
                                   cls=PrepFunction,
                                   fork_inst=cls._serializer)
            jsons.set_deserializer(Training.deserializer,
                                   cls=Training,
                                   fork_inst=cls._serializer)

            jsons.set_serializer(jsons.default_object_serializer,
                                 cls=cls,
                                 fork_inst=cls._serializer)
            jsons.set_serializer(PrepFunction.serializer,
                                 cls=PrepFunction,
                                 fork_inst=cls._serializer)

        return cls._serializer
コード例 #8
0
    def get_serializer(cls):
        if not cls._serializer:
            cls._serializer = jsons.fork()

        return cls._serializer