Advertisement
Jump_off_a_cliff

(One of the) Best Python Dictionary

May 28th, 2022
1,638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import math
  2.  
  3.  
  4. class Container:
  5.     def __init__(self):
  6.         self.items = {}
  7.  
  8.     def add(self, item):
  9.         self.items[item] = self.items.get(item, 0) + 1
  10.  
  11.     def remove(self, item):
  12.         self.items[item] = self.items.get(item, 0) - 1
  13.  
  14.     def __contains__(self, item):
  15.         return item in self.items
  16.  
  17.     def __len__(self):
  18.         return len(self.items)
  19.  
  20.     def __iter__(self):
  21.         return iter(self.items)
  22.  
  23.     def __str__(self):
  24.         return str(self.items)
  25.  
  26.     def __repr__(self):
  27.         return repr(self.items)
  28.  
  29.     def __getitem__(self, item):
  30.         return self.items[item]
  31.  
  32.     def __setitem__(self, item, value):
  33.         self.items[item] = value
  34.  
  35.     def __delitem__(self, item):
  36.         del self.items[item]
  37.  
  38.     def __eq__(self, other):
  39.         return self.items == other.items
  40.  
  41.     def __ne__(self, other):
  42.         return self.items != other.items
  43.  
  44.     def __lt__(self, other):
  45.         return self.items < other.items
  46.  
  47.     def __gt__(self, other):
  48.         return self.items > other.items
  49.  
  50.     def __le__(self, other):
  51.         return self.items <= other.items
  52.  
  53.     def __ge__(self, other):
  54.         return self.items >= other.items
  55.  
  56.     def __add__(self, other):
  57.         return Container(self.items + other.items)
  58.  
  59.     def __sub__(self, other):
  60.         return Container(self.items - other.items)
  61.  
  62.     def __mul__(self, other):
  63.         return Container(self.items * other)
  64.  
  65.     def __truediv__(self, other):
  66.         return Container(self.items / other)
  67.  
  68.     def __floordiv__(self, other):
  69.         return Container(self.items // other)
  70.  
  71.     def __mod__(self, other):
  72.         return Container(self.items % other)
  73.  
  74.     def __divmod__(self, other):
  75.         return Container(self.items.divmod(other))
  76.  
  77.     def __pow__(self, other):
  78.         return Container(self.items ** other)
  79.  
  80.     def __lshift__(self, other):
  81.         return Container(self.items << other)
  82.  
  83.     def __rshift__(self, other):
  84.         return Container(self.items >> other)
  85.  
  86.     def __and__(self, other):
  87.         return Container(self.items & other)
  88.  
  89.     def __xor__(self, other):
  90.         return Container(self.items ^ other)
  91.  
  92.     def __or__(self, other):
  93.         return Container(self.items | other)
  94.  
  95.     def __radd__(self, other):
  96.         return Container(other + self.items)
  97.  
  98.     def __rsub__(self, other):
  99.         return Container(other - self.items)
  100.  
  101.     def __rmul__(self, other):
  102.         return Container(other * self.items)
  103.  
  104.     def __rtruediv__(self, other):
  105.         return Container(other / self.items)
  106.  
  107.     def __rfloordiv__(self, other):
  108.         return Container(other // self.items)
  109.  
  110.     def __rmod__(self, other):
  111.         return Container(other % self.items)
  112.  
  113.     def __rdivmod__(self, other):
  114.         return Container(other.divmod(self.items))
  115.  
  116.     def __rpow__(self, other):
  117.         return Container(other ** self.items)
  118.  
  119.     def __rlshift__(self, other):
  120.         return Container(other << self.items)
  121.  
  122.     def __rrshift__(self, other):
  123.         return Container(other >> self.items)
  124.  
  125.     def __rand__(self, other):
  126.         return Container(other & self.items)
  127.  
  128.     def __rxor__(self, other):
  129.         return Container(other ^ self.items)
  130.  
  131.     def __ror__(self, other):
  132.         return Container(other | self.items)
  133.  
  134.     def __iadd__(self, other):
  135.         self.items += other.items
  136.         return self
  137.  
  138.     def __isub__(self, other):
  139.         self.items -= other.items
  140.         return self
  141.  
  142.     def __imul__(self, other):
  143.         self.items *= other
  144.         return self
  145.  
  146.     def __itruediv__(self, other):
  147.         self.items /= other
  148.         return self
  149.  
  150.     def __ifloordiv__(self, other):
  151.         self.items //= other
  152.         return self
  153.  
  154.     def __imod__(self, other):
  155.         self.items %= other
  156.         return self
  157.  
  158.     def __ipow__(self, other):
  159.         self.items **= other
  160.         return self
  161.  
  162.     def __ilshift__(self, other):
  163.         self.items <<= other
  164.         return self
  165.  
  166.     def __irshift__(self, other):
  167.         self.items >>= other
  168.         return self
  169.  
  170.     def __iand__(self, other):
  171.         self.items &= other
  172.         return self
  173.  
  174.     def __ixor__(self, other):
  175.         self.items ^= other
  176.         return self
  177.  
  178.     def __ior__(self, other):
  179.         self.items |= other
  180.         return self
  181.  
  182.     def __neg__(self):
  183.         return Container(-self.items)
  184.  
  185.     def __pos__(self):
  186.         return Container(+self.items)
  187.  
  188.     def __abs__(self):
  189.         return Container(abs(self.items))
  190.  
  191.     def __invert__(self):
  192.         return Container(~self.items)
  193.  
  194.     def __round__(self, n=0):
  195.         return Container(round(self.items, n))
  196.  
  197.     def __floor__(self):
  198.         return Container(math.floor(self.items))
  199.  
  200.     def __ceil__(self):
  201.         return Container(math.ceil(self.items))
  202.  
  203.     def __trunc__(self):
  204.         return Container(math.trunc(self.items))
  205.  
  206.     def __int__(self):
  207.         return int(self.items)
  208.  
  209.     def __float__(self):
  210.         return float(self.items)
  211.  
  212.     def __complex__(self):
  213.         return complex(self.items)
  214.  
  215.     def __oct__(self):
  216.         return oct(self.items)
  217.  
  218.     def __hex__(self):
  219.         return hex(self.items)
  220.  
  221.     def __index__(self):
  222.         return self.items.__index__()
  223.  
  224.     def __coerce__(self, other):
  225.         return self.items.__coerce__(other)
  226.  
  227.     def __enter__(self):
  228.         return self.items.__enter__()
  229.  
  230.     def __exit__(self, exc_type, exc_value, traceback):
  231.         return self.items.__exit__(exc_type, exc_value, traceback)
  232.  
  233.     def __getattr__(self, name):
  234.         return getattr(self.items, name)
  235.  
  236.     def __setattr__(self, name, value):
  237.         setattr(self.items, name, value)
  238.    
  239.     def __delattr__(self, name):
  240.         delattr(self.items, name)
  241.  
  242.     def __call__(self, *args, **kwargs):
  243.         return self.items(*args, **kwargs)
  244.    
  245.     def __new__(cls, *args, **kwargs):
  246.         return object.__new__(cls)
  247.    
  248.     def __hash__(self):
  249.         return hash(self.items)
  250.  
  251.     def __format__(self, format_spec):
  252.         return self.items.__format__(format_spec)
  253.    
  254.     def __sizeof__(self):
  255.         return self.items.__sizeof__()
  256.    
  257.     def __reduce__(self):
  258.         return self.items.__reduce__()
  259.    
  260.     def __reduce_ex__(self, protocol):
  261.         return self.items.__reduce_ex__(protocol)
  262.    
  263.     def __getnewargs__(self):
  264.         return self.items.__getnewargs__()
  265.  
  266.     def __getstate__(self):
  267.         return self.items.__getstate__()
  268.    
  269.     def __setstate__(self, state):
  270.         self.items.__setstate__(state)
  271.    
  272.     def __del__(self):
  273.         del self.items
  274.  
Advertisement
RAW Paste Data Copied
Advertisement