Advertisement
kokociel

Location groups

Jun 26th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3.  
  4. import logging
  5.  
  6. logger = logging.getLogger(__name__)
  7. logger.setLevel(logging.DEBUG)
  8.  
  9. class Group(object):
  10.     """
  11.    A collection of places with test cases and other groups.
  12.    """
  13.    
  14.     def __init__(self, id, name, tests=[], *args, **kwargs):
  15.         self.id = id  # Needs to be hashable for the holding object.
  16.         self.name = name  # todo: validation
  17.         self.sub_group_keys = set()
  18.         # todo: validation of possible tests
  19.         self.tests = set(tests)
  20.        
  21.     def add_sub_group(self, sub_group):
  22.         self.sub_group_keys.add(sub_group.id)
  23.    
  24.     def __str__(self):
  25.         return self.name
  26.  
  27. class GroupStorage(object):
  28.     """
  29.    Here we hold groups so that they can refer to each other with propagated changes.
  30.    """
  31.     groups = {}  # Group ID ↦ Group
  32.     result_cache = {}  # Group ID ↦ test cases
  33.    
  34.     def add_group(self, group):
  35.         self.groups[group.id] = group
  36.         # Cache is still fine here
  37.         return
  38.    
  39.     def remove_group(self, group):
  40.         del self.groups[group.id]
  41.         self.result_cache = {}
  42.         # todo: propagating through cache
  43.    
  44.     def get_test_cases(self, group_id, visited=None):
  45.         """
  46.        Extract all the test cases for this group and its subgroups, while avoiding cycles.
  47.        
  48.        todo: test for cycles
  49.        :param group_id: An identifier for the group of interest
  50.        :param visited: A set of group IDs that have been visited.
  51.        :return: A set of identifiers for test cases.
  52.        :rtype: set
  53.        """
  54.         visited = visited or set()
  55.         if group_id in self.result_cache:
  56.             logger.warning("A cached result for %d was encountered.", group_id)
  57.             return self.result_cache[group_id]
  58.         test_cases = set()
  59.         if group_id in self.groups:
  60.             test_cases = self.groups[group_id].tests
  61.             for sub_group_key in self.groups[group_id].sub_group_keys:
  62.                 if sub_group_key not in visited:
  63.                     visited.add(sub_group_key)
  64.                     test_cases = test_cases.union(self.get_test_cases(sub_group_key))
  65.         if len(test_cases) == 0:
  66.             logger.warning("Group %d doesn't seem to be present.", group_id)
  67.         self.result_cache[group_id] = test_cases
  68.         return test_cases
  69.    
  70.     def add_subgroup(self, group_id, sub_group):
  71.         """
  72.        Add a group to one of our existing groups, in a way that preserves the validity of the
  73.        cache.
  74.        :param group_id:
  75.        :param sub_group:
  76.        :return:
  77.        """
  78.         self.result_cache.clear()
  79.         # todo: preserve more of the cache when adding children
  80.         if group_id in self.groups:
  81.             self.groups[group_id].add_group(sub_group)
  82.         return
  83.    
  84. if __name__ == "__main__":
  85.     storage = GroupStorage()
  86.     locations = Group(1, "Locations", ["t1", "t2", "t3", "t4"])
  87.     airports = Group(2, "Airports", ["t2", "t5", "t6"])
  88.     ny_locations = Group(3, "New York Locations", ["t4", "t7", "t8", "t9"])
  89.     ny_airports = Group(4, "New York Airports", ["t10"])
  90.     locations.add_sub_group(airports)
  91.     locations.add_sub_group(ny_locations)
  92.     airports.add_sub_group(ny_airports)
  93.     ny_locations.add_sub_group(ny_airports)
  94.     logger.info("Now that the groups are defined, we'll store them…")
  95.     storage.add_group(locations)
  96.     storage.add_group(airports)
  97.     storage.add_group(ny_locations)
  98.     storage.add_group(ny_airports)
  99.    
  100.     tests_2 = list(storage.get_test_cases(airports.id))
  101.     tests_2.sort()
  102.     print("The tests for %s are %s" % (airports, tests_2))
  103.     tests_1 = list(storage.get_test_cases(locations.id))
  104.     tests_1.sort()
  105.     print("The tests for %s are %s" % (locations, tests_1))
  106.     tests_3 = list(storage.get_test_cases(ny_locations.id))
  107.     tests_3.sort()
  108.     print("The tests for %s are %s" % (ny_locations, tests_3))
  109.     tests_4 = list(storage.get_test_cases(ny_airports.id))
  110.     tests_4.sort()
  111.     print("The tests for %s are %s" % (ny_airports, tests_4))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement