Advertisement
Guest User

Untitled

a guest
May 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1.  
  2.  
  3. class Array(dict):
  4.     reflect = []
  5.     operations = []
  6.  
  7.     def __init__(self, mapping, **kwargs):
  8.         super().update(mapping)
  9.         Array.reflect = dir(Array)
  10.  
  11.     def __getattribute__(self, item):
  12.         """ Returns instance attribute or instance value """
  13.  
  14.         return super().__getattribute__(item) if item in Array.reflect \
  15.             else self.__getitem__(item)
  16.  
  17.     def __setattr__(self, item, value):
  18.         """ Sets attribute or value """
  19.  
  20.         if item not in Array.reflect:
  21.             super().update({item: value})
  22.         else:
  23.             super().__setattr__(item, value)
  24.  
  25.     def map(self, func):
  26.         def wrapper():
  27.             for item in self.items():
  28.                 yield func(*item)
  29.  
  30.         self.operations += [wrapper()]
  31.         return self
  32.  
  33.     def reduce(self, fn = None):
  34.         def wrapper():
  35.             carry = next(iter(self.values())).__class__()
  36.             for item in self.items():
  37.                 if callable(fn):
  38.                     carry += fn(*item)
  39.                 else:
  40.                     _, value = item
  41.                     carry += value
  42.             yield carry
  43.  
  44.         self.operations += [wrapper()]
  45.         return self
  46.  
  47.     def __iter__(self):
  48.         def wrapper():
  49.             for ops in self.operations:
  50.                 for item in ops:
  51.                     yield item
  52.         return wrapper()
  53.  
  54. def add333(key, value):
  55.     return value + "333"
  56.  
  57. def show():
  58.     a = Array({'value': 'asdasd', 'key': 'asdasd'})
  59.  
  60.     a.hej = '123'
  61.     it = a.map(add333).reduce()
  62.     for item in it:
  63.         print(item)
  64.  
  65.  
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement