Advertisement
Uno-Dan

Two new methods

Sep 7th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. cfg = {
  2.     'level_1': {
  3.         'level_2': {
  4.             'item1': 10,
  5.             'item2': 20,
  6.         }
  7.     }
  8. }
  9. more_cfg = {
  10.     'level_3': {
  11.         'level_4': {
  12.             'item3': 10,
  13.             'item4': 20,
  14.         }
  15.     }
  16. }
  17.  
  18.  
  19. class Cache:
  20.     def __init__(self, **data):
  21.         self.nodes = data
  22.  
  23.     def get(self, uri=None):
  24.         if not uri:
  25.             return self.nodes
  26.  
  27.         def walk(_uri, nodes):
  28.             parts = _uri.split('/', 1)
  29.             key = parts.pop(0)
  30.  
  31.             if key in nodes:
  32.                 node = nodes[key]
  33.  
  34.                 if not parts:
  35.                     return node
  36.                 else:
  37.                     return walk(parts[0], node)
  38.  
  39.         return walk(uri, self.nodes)
  40.  
  41.     def set(self, uri, **value):
  42.  
  43.         def walk(_uri, nodes):
  44.             parts = _uri.split('/', 1)
  45.             key = parts.pop(0)
  46.  
  47.             if key in nodes and parts:
  48.                 return walk(parts[0], nodes[key])
  49.             elif len(_uri.split('/')) == 1:
  50.                 nodes[key] = value
  51.  
  52.         return walk(uri, self.nodes)
  53.  
  54.     def dump(self, indent=None):
  55.         """ Dumps the contents of the cache to the screen.
  56.        The output from dump goes stdout and is used to view the cache contents.
  57.        Default indentation is a dot for each level.
  58.        :param indent:
  59.            indent (str): String to be use for indenting levels.
  60.        :return:
  61.            Nothing.
  62.        """
  63.         indent = indent if indent else '.'
  64.  
  65.         print('-------------------------------------------------------------------------------------------------------')
  66.  
  67.         if self.nodes:
  68.             def walk(_cfg, count):
  69.                 count += 1
  70.                 for key, value in _cfg.items():
  71.                     if isinstance(value, dict):
  72.                         print(indent * count, key)
  73.                         walk(value, count)
  74.                     else:
  75.                         if isinstance(value, str):
  76.                             value = f'"{value}"'
  77.                         print(indent * count, key, value)
  78.             walk(self.nodes, 0)
  79.         else:
  80.             print(' (No Data)')
  81.  
  82.         print('-------------------------------------------------------------------------------------------------------')
  83.  
  84.     def remove(self, uri):
  85.         """ Remove entree from cache.
  86.        Removes an entree from the cache if it exists.
  87.        :param uri:
  88.            uri (str): URI that points to the entree to remove.
  89.        :return:
  90.            Nothing.
  91.        """
  92.         uri = uri.lstrip('/')
  93.         if self.exists(uri):
  94.             node = self.get('/'.join(uri.split('/')[:-1]))
  95.             del node[uri.split('/')[-1]]
  96.  
  97.     def exists(self, uri):
  98.         """ Test if URI exists in the cache.
  99.  
  100.        :param uri:
  101.        :return:
  102.        """
  103.         return True if self.get(uri) else False
  104.  
  105.  
  106. c = cache = Cache(**cfg)
  107.  
  108. print(c.get('level_1/level_2/item2'))
  109.  
  110. c.set('level_1/level_2/more_cfg', **more_cfg)
  111.  
  112. if c.exists('level_1/level_2/more_cfg'):
  113.     print(c.get('level_1/level_2/more_cfg'))
  114.  
  115. c.dump()
  116.  
  117. if c.exists('level_1/level_2/item1'):
  118.     c.remove('level_1/level_2/item1')
  119.     c.dump()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement