예제 #1
0
    def test_create_component_dict(self):
        cdict1 = self._container_type(
            (i, self._ctype_factory()) for i in range(5))
        self.assertEqual(len(cdict1), 5)
        for obj in cdict1.values():
            self.assertIs(obj.parent, cdict1)
        objects = iter(cdict1.values())

        def type_(x, y=None):
            self.assertEqual(x, 1)
            self.assertEqual(y, 'a')

        type_ = lambda x, y=None: six.next(objects)
        type_.ctype = cdict1.ctype
        # this will result in cdict1 and cdict2
        # being "equal" in that they both store the
        # same objects, except that cdict2 has stolen
        # ownership of the objects from cdict1 (all of the
        # .parent weakrefs have been changed)
        cdict2 = create_component_dict(self._container_type,
                                       type_,
                                       range(5),
                                       1,
                                       y='a')
        self.assertEqual(len(cdict2), 5)
        self.assertEqual(cdict1, cdict2)
        self.assertIsNot(cdict1, cdict2)
        for obj in cdict1.values():
            self.assertIs(obj.parent, cdict2)
        for obj in cdict2.values():
            self.assertIs(obj.parent, cdict2)
예제 #2
0
def create_variable_dict(keys, *args, **kwds):
    """
    Generates a full :class:`variable_dict`.

    Args:
        keys: The set of keys to used to populate the
            variable_dict.
        type_: The object type to populate the container
            with. Must have the same ctype as
            variable_dict. Default: :class:`variable`
        *args: arguments used to construct the objects
            placed in the container.
        **kwds: keywords used to construct the objects
            placed in the container.

    Returns:
        a fully populated :class:`variable_dict`
    """
    type_ = kwds.pop('type_', variable)
    return create_component_dict(variable_dict, type_, keys, *args, **kwds)