Exemplo n.º 1
0
    def test_recursive_fromkeys(self):
        class declared_fields_class(object):
            def __init__(self, this_dict):
                self._declared_fields = this_dict

        class dict_assigned_to_nested(object):
            def __init__(self, this_dict):
                self.nested = declared_fields_class(this_dict)

        f = dict_assigned_to_nested({"g": None})
        c = dict_assigned_to_nested({"d": ("x", "z"), "e": 7, "f": f, "h": {}})
        this_dict = {"a": 1, "b": "a", "c": c, "i": print}
        return_dict = recursive_fromkeys(this_dict)

        self.assertTrue(return_dict["a"] is None)
        self.assertTrue(return_dict["b"] is None)
        self.assertIsInstance(return_dict["c"], dict)
        self.assertTrue(len(return_dict["c"]) == 4)
        self.assertTrue(return_dict["c"]["d"] is None)
        self.assertTrue(return_dict["c"]["e"] is None)
        self.assertIsInstance(return_dict["c"]["f"], dict)
        self.assertTrue(return_dict["c"]["f"]["g"] is None)
        self.assertIsInstance(return_dict["c"]["h"], dict)
        self.assertTrue(len(return_dict["c"]["h"]) == 0)
        self.assertTrue(return_dict["i"] is None)
Exemplo n.º 2
0
    def test_recursive_fromkeys(self):
        class declared_fields_class(object):
            def __init__(self, this_dict):
                self._declared_fields = this_dict

        class dict_assigned_to_nested(object):
            def __init__(self, this_dict):
                self.nested = declared_fields_class(this_dict)

        f = dict_assigned_to_nested({'g': None})
        c = dict_assigned_to_nested({'d': ('x','z'), 'e': 7, 'f': f, 'h': {}})
        this_dict = {'a': 1, 'b': 'a', 'c': c, 'i': print}
        return_dict = recursive_fromkeys(this_dict)

        self.assertTrue(return_dict['a'] is None)
        self.assertTrue(return_dict['b'] is None)
        self.assertIsInstance(return_dict['c'], dict)
        self.assertTrue(len(return_dict['c']) == 4)
        self.assertTrue(return_dict['c']['d'] is None)
        self.assertTrue(return_dict['c']['e'] is None)
        self.assertIsInstance(return_dict['c']['f'], dict)
        self.assertTrue(return_dict['c']['f']['g'] is None)
        self.assertIsInstance(return_dict['c']['h'], dict)
        self.assertTrue(len(return_dict['c']['h']) == 0)
        self.assertTrue(return_dict['i'] is None)
Exemplo n.º 3
0
    def create_stub_object(self):
        """ Creates a stub dictionary based on the schema with all values set to None.
        We do this because our goal is to prevent (eventually) creation of new attributes
        directly by users - only allowing them to use what we already provide to them
        to keep the schema in sync with the object."""

        MLSchema.populate_registry()
        version_number = self.get_schema_version()
        self.__schema_name = MLSchema.return_schema_name(version_number,
                                                         self.get_schema_type().name)
        object_schema = marshmallow.class_registry.get_class(self.get_schema_name())
        self.__schema = object_schema()
        self.__schema_object = None
        these_fields = object_schema().fields
        this_key_dict = recursive_fromkeys(these_fields)
        self.merge_update(this_key_dict)