Advertisement
Uno-Dan

Added new feature, __eq__ dunder method.

Sep 7th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.65 KB | None | 0 0
  1. from os import path, makedirs
  2. from json import load, dump
  3. from copy import deepcopy
  4. from importlib.util import module_from_spec, spec_from_file_location
  5.  
  6.  
  7. class Cache:
  8.     def __init__(self, *args, **kwargs):
  9.         self.nodes = args[0] if args else kwargs
  10.         self.indent = '.'
  11.  
  12.     def __eq__(self, other):
  13.         if self.nodes == other.nodes:
  14.             return True
  15.         return False
  16.  
  17.     def __str__(self):
  18.         return str(self.nodes.items())
  19.  
  20.     def __repr__(self):
  21.         data = "**{'key': 'value'}"
  22.         return f"{self.__class__.__name__}({data})"
  23.  
  24.     def get(self, uri=None):
  25.         if not uri:
  26.             return self.nodes
  27.  
  28.         def walk(_uri, nodes):
  29.             parts = _uri.split('/', 1)
  30.             key = parts.pop(0)
  31.  
  32.             if key in nodes:
  33.                 node = nodes[key]
  34.  
  35.                 if not parts:
  36.                     return node
  37.                 else:
  38.                     return walk(parts[0], node)
  39.  
  40.         return walk(uri, self.nodes)
  41.  
  42.     def set(self, uri, *args, **kwargs):
  43.  
  44.         def walk(_uri, nodes):
  45.             parts = _uri.split('/', 1)
  46.             key = parts.pop(0)
  47.  
  48.             if key in nodes and parts:
  49.                 return walk(parts[0], nodes[key])
  50.             elif len(_uri.split('/')) == 1:
  51.                 nodes[key] = args[0] if args else kwargs
  52.  
  53.         return walk(uri, self.nodes)
  54.  
  55.     def dump(self, indent=None):
  56.         """ Dumps the contents of the cache to the screen.
  57.        The output from dump goes stdout and is used to view the cache contents.
  58.        Default indentation is a dot for each level.
  59.        :param indent:
  60.            indent (str): String to be use for indenting levels.
  61.        :return:
  62.            Nothing.
  63.        """
  64.         indent = indent if indent else '.'
  65.  
  66.         print('-------------------------------------------------------------------------------------------------------')
  67.         print('id =', id(self), '\nnodes =', self)
  68.         if self.nodes:
  69.             def walk(_cfg, count):
  70.                 count += 1
  71.                 for key, value in _cfg.items():
  72.                     if isinstance(value, dict):
  73.                         print(indent * count, key)
  74.                         walk(value, count)
  75.                     else:
  76.                         if isinstance(value, str):
  77.                             value = f'"{value}"'
  78.                         print(indent * count, key, f'value={value}')
  79.             walk(self.nodes, 0)
  80.         else:
  81.             print(' (No Data)')
  82.  
  83.         print('-------------------------------------------------------------------------------------------------------')
  84.  
  85.     def save(self, file=None):
  86.         if file:
  87.             dirname = path.dirname(file)
  88.  
  89.             if dirname and not path.exists(dirname):
  90.                 makedirs(dirname)
  91.  
  92.             with open(file, 'w') as f:
  93.                 dump(self.nodes, f, indent=3)
  94.  
  95.     def load(self, file=None):
  96.         file_type = path.splitext(file)[1].lstrip('.').lower()
  97.  
  98.         if file_type == 'py' and path.exists(file):
  99.             spec = spec_from_file_location("module.name", file)
  100.             module = module_from_spec(spec)
  101.             spec.loader.exec_module(module)
  102.             self.nodes = module.config
  103.  
  104.         if file_type == 'json' and path.exists(file):
  105.             with open(file) as f:
  106.                 self.nodes = load(f)
  107.  
  108.     def copy(self):
  109.         return Cache(**deepcopy(self.nodes))
  110.  
  111.     def remove(self, uri):
  112.         """ Remove entree from cache.
  113.        Removes an entree from the cache if it exists.
  114.        :param uri:
  115.            uri (str): URI that points to the entree to remove.
  116.        :return:
  117.            Nothing.
  118.        """
  119.  
  120.         uri = uri.lstrip('/')
  121.         if self.exists(uri):
  122.             node = self.get('/'.join(uri.split('/')[:-1]))
  123.             del node[uri.split('/')[-1]]
  124.  
  125.     def exists(self, uri):
  126.         """ Test if URI exists in the cache.
  127.  
  128.        :param uri:
  129.        :return:
  130.        """
  131.         return True if self.get(uri) else False
  132.  
  133.     def destroy(self):
  134.         """ Destroy cache.
  135.        Deletes all entries in the cache.
  136.        :return:
  137.            Nothing.
  138.        """
  139.         del self.nodes
  140.         self.nodes = {}
  141.  
  142.  
  143. cache = Cache()
  144. cache.load('data.py')
  145. cache.dump()
  146.  
  147. print('\nlevel_1/level_2/item2 = ', cache.get('level_1/level_2/item2'))
  148. cache.set('level_1/level_2/item2', 200)
  149. print('level_1/level_2/item2 = ', cache.get('level_1/level_2/item2'))
  150.  
  151. level_4 = {
  152.     'level_4': {
  153.         'item3': 10,
  154.         'item4': 20,
  155.     }
  156. }
  157. cache.set('level_1/level_2/level_3', level_4)
  158. cache.get('level_1/level_2/level_3').update({'level_5': {'name': 'Dan'}})
  159.  
  160. if cache.exists('level_1/level_2/level_3'):
  161.     print('level_1/level_2/level_3 =', cache.get('level_1/level_2/level_3'), '\n')
  162.  
  163. cache.save('data.json')
  164. cache.dump()
  165.  
  166. my_cache = cache.copy()
  167. my_cache.dump()
  168.  
  169. cache.destroy()
  170. cache.set('p1', {'first_name': 'Dan', 'last_name': 'Huckson'})
  171. cache.set('p2', {'first_name': 'Geoff', 'last_name': 'Skerlan'})
  172. cache.dump()
  173.  
  174. my_cache.remove('level_1/level_2/item1')
  175. my_cache.remove('level_1/level_2/level_3')
  176. my_cache.dump()
  177.  
  178. your_cache = Cache()
  179. your_cache.load('data.json')
  180. your_cache.dump()
  181.  
  182. print()
  183. print('your_cache =', str(your_cache))
  184. print('your_cache =', repr(your_cache))
  185.  
  186. # Test equality code below.
  187. my_cache2 = my_cache.copy()
  188.  
  189. # uncomment the line below to test inequality.
  190. # my_cache.set('level_1/level_2/item2', 201)
  191.  
  192. if my_cache == my_cache2:
  193.     print('\nThe contents of both caches are the same!')
  194. else:
  195.     print('\nThe contents of both caches are not exactly same!')
  196.  
  197. my_cache.dump()
  198. my_cache2.dump()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement