Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from __future__ import absolute_import, unicode_literals
- import logging
- logger = logging.getLogger(__name__)
- logger.setLevel(logging.DEBUG)
- class Group(object):
- """
- A collection of places with test cases and other groups.
- """
- def __init__(self, id, name, tests=[], *args, **kwargs):
- self.id = id # Needs to be hashable for the holding object.
- self.name = name # todo: validation
- self.sub_group_keys = set()
- # todo: validation of possible tests
- self.tests = set(tests)
- def add_sub_group(self, sub_group):
- self.sub_group_keys.add(sub_group.id)
- def __str__(self):
- return self.name
- class GroupStorage(object):
- """
- Here we hold groups so that they can refer to each other with propagated changes.
- """
- groups = {} # Group ID ↦ Group
- result_cache = {} # Group ID ↦ test cases
- def add_group(self, group):
- self.groups[group.id] = group
- # Cache is still fine here
- return
- def remove_group(self, group):
- del self.groups[group.id]
- self.result_cache = {}
- # todo: propagating through cache
- def get_test_cases(self, group_id, visited=None):
- """
- Extract all the test cases for this group and its subgroups, while avoiding cycles.
- todo: test for cycles
- :param group_id: An identifier for the group of interest
- :param visited: A set of group IDs that have been visited.
- :return: A set of identifiers for test cases.
- :rtype: set
- """
- visited = visited or set()
- if group_id in self.result_cache:
- logger.warning("A cached result for %d was encountered.", group_id)
- return self.result_cache[group_id]
- test_cases = set()
- if group_id in self.groups:
- test_cases = self.groups[group_id].tests
- for sub_group_key in self.groups[group_id].sub_group_keys:
- if sub_group_key not in visited:
- visited.add(sub_group_key)
- test_cases = test_cases.union(self.get_test_cases(sub_group_key))
- if len(test_cases) == 0:
- logger.warning("Group %d doesn't seem to be present.", group_id)
- self.result_cache[group_id] = test_cases
- return test_cases
- def add_subgroup(self, group_id, sub_group):
- """
- Add a group to one of our existing groups, in a way that preserves the validity of the
- cache.
- :param group_id:
- :param sub_group:
- :return:
- """
- self.result_cache.clear()
- # todo: preserve more of the cache when adding children
- if group_id in self.groups:
- self.groups[group_id].add_group(sub_group)
- return
- if __name__ == "__main__":
- storage = GroupStorage()
- locations = Group(1, "Locations", ["t1", "t2", "t3", "t4"])
- airports = Group(2, "Airports", ["t2", "t5", "t6"])
- ny_locations = Group(3, "New York Locations", ["t4", "t7", "t8", "t9"])
- ny_airports = Group(4, "New York Airports", ["t10"])
- locations.add_sub_group(airports)
- locations.add_sub_group(ny_locations)
- airports.add_sub_group(ny_airports)
- ny_locations.add_sub_group(ny_airports)
- logger.info("Now that the groups are defined, we'll store them…")
- storage.add_group(locations)
- storage.add_group(airports)
- storage.add_group(ny_locations)
- storage.add_group(ny_airports)
- tests_2 = list(storage.get_test_cases(airports.id))
- tests_2.sort()
- print("The tests for %s are %s" % (airports, tests_2))
- tests_1 = list(storage.get_test_cases(locations.id))
- tests_1.sort()
- print("The tests for %s are %s" % (locations, tests_1))
- tests_3 = list(storage.get_test_cases(ny_locations.id))
- tests_3.sort()
- print("The tests for %s are %s" % (ny_locations, tests_3))
- tests_4 = list(storage.get_test_cases(ny_airports.id))
- tests_4.sort()
- print("The tests for %s are %s" % (ny_airports, tests_4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement