Пример #1
0
    def test_yield_obj_parent_attr(self):
        """
        Setup a complex data structure and ensure recursive search function
        finds all target objects.
        """
        class Slots(object):
            """
            A simple class with slots
            """
            __slots__ = ('right', )

            def __init__(self, init):
                self.right = init

        slotted = Slots((ResId('1'), AttributeError, [ResId('2')]))
        nested = {
            'not_right': 'nope',
            'good': {
                'right': ResId('3'),
                'wrong': [1, [(())]]
            },
            'right': [[[[[[[[ResId('4')]]], ResId('5')]]]]],
        }

        base = dict(right=ResId('6'), slotted=slotted, nested=nested)

        out = list(_yield_obj_parent_attr(base, ResId))

        assert len(out) == 6

        for obj, parent, attr in out:
            assert attr == 'right'
            assert isinstance(obj, ResId)
Пример #2
0
    def test_yield_obj_parent_attr(self):
        """
        Setup a complex data structure and ensure recursive search function
        finds all target objects.
        """
        class Slots(object):
            """
            A simple class with slots
            """
            __slots__ = ('right', )

            def __init__(self, init):
                self.right = init

        slotted = Slots((ResId('1'), AttributeError, [ResId('2')]))
        nested = {
            'not_right': 'nope',
            'good': {'right': ResId('3'), 'wrong': [1, [(())]]},
            'right': [[[[[[[[ResId('4')]]], ResId('5')]]]]],
        }

        base = dict(right=ResId('6'), slotted=slotted, nested=nested)

        out = list(_yield_obj_parent_attr(base, ResId))

        self.assertEqual(len(out), 6)

        for obj, parent, attr in out:
            self.assertEqual(attr, 'right')
            self.assertIsInstance(obj, ResId)
Пример #3
0
 def test_all_referred_objects_in_events(self, catalogs=None):
     """
     All the referred object should be members of the current event.
     """
     for num, cat in enumerate(catalogs or make_diverse_catalog_list()):
         for ev in cat:
             ev_ids = get_object_id_dict(ev)
             gen = _yield_obj_parent_attr(ev, ResourceIdentifier)
             for rid, parent, attr in gen:
                 referred_object = rid.get_referred_object()
                 if referred_object is None:
                     continue
                 self.assertIn(id(referred_object), ev_ids)
Пример #4
0
 def test_all_referred_objects_in_events(self, catalogs=None):
     """
     All the referred object should be members of the current event.
     """
     for num, cat in enumerate(catalogs or make_diverse_catalog_list()):
         for ev in cat:
             ev_ids = get_object_id_dict(ev)
             gen = _yield_obj_parent_attr(ev, ResourceIdentifier)
             for rid, parent, attr in gen:
                 referred_object = rid.get_referred_object()
                 if referred_object is None:
                     continue
                 self.assertIn(id(referred_object), ev_ids)
Пример #5
0
def get_instances(obj, cls=None, is_attr=None, has_attr=None):
    """
    Recurse object, return a list of instances meeting search criteria.

    :param obj:
        The object to recurse through attributes of lists, tuples, and other
        instances.
    :param cls:
        Only return instances of cls if not None, else return all instances.
    :param is_attr:
        Only return objects stored as attr_name, if None return all.
    :param has_attr:
        Only return objects that have attribute has_attr, if None return all.
    """
    gen = _yield_obj_parent_attr(obj, cls, is_attr=is_attr, has_attr=has_attr)
    return [x[0] for x in gen]
Пример #6
0
def get_instances(obj, cls=None, is_attr=None, has_attr=None):
    """
    Recurse object, return a list of instances meeting search criteria.

    :param obj:
        The object to recurse through attributes of lists, tuples, and other
        instances.
    :param cls:
        Only return instances of cls if not None, else return all instances.
    :param is_attr:
        Only return objects stored as attr_name, if None return all.
    :param has_attr:
        Only return objects that have attribute has_attr, if None return all.
    """
    gen = _yield_obj_parent_attr(obj, cls, is_attr=is_attr, has_attr=has_attr)
    return [x[0] for x in gen]
Пример #7
0
def get_object_id_dict(obj, cls=None):
    """
    Recurse an object and return a dict in the form of {id: object}.
    """
    return {id(x[0]): x[0] for x in _yield_obj_parent_attr(obj, cls)}
Пример #8
0
def get_object_id_dict(obj, cls=None):
    """
    Recurse an object and return a dict in the form of {id: object}.
    """
    return {id(x[0]): x[0] for x in _yield_obj_parent_attr(obj, cls)}