Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.64 KB | None | 0 0
  1. import uuid
  2. import numpy as np
  3. import gzip
  4. import pickle
  5.  
  6. import sys
  7. from numbers import Number
  8. from collections import Set, Mapping, deque
  9.  
  10. try: # Python 2
  11.     zero_depth_bases = (basestring, Number, xrange, bytearray)
  12.     iteritems = 'iteritems'
  13. except NameError: # Python 3
  14.     zero_depth_bases = (str, bytes, Number, range, bytearray)
  15.     iteritems = 'items'
  16.  
  17. def getsize(obj_0):
  18.     """Recursively iterate to sum size of object & members."""
  19.     def inner(obj, _seen_ids = set()):
  20.         obj_id = id(obj)
  21.         if obj_id in _seen_ids:
  22.             return 0
  23.         _seen_ids.add(obj_id)
  24.         size = sys.getsizeof(obj)
  25.         if isinstance(obj, zero_depth_bases):
  26.             pass # bypass remaining control flow and return
  27.         elif isinstance(obj, (tuple, list, Set, deque)):
  28.             size += sum(inner(i) for i in obj)
  29.         elif isinstance(obj, Mapping) or hasattr(obj, iteritems):
  30.             size += sum(inner(k) + inner(v) for k, v in getattr(obj, iteritems)())
  31.         # Check for custom object instances - may subclass above too
  32.         if hasattr(obj, '__dict__'):
  33.             size += inner(vars(obj))
  34.         if hasattr(obj, '__slots__'): # can have __slots__ with __dict__
  35.             size += sum(inner(getattr(obj, s)) for s in obj.__slots__ if hasattr(obj, s))
  36.         return size
  37.     return inner(obj_0)
  38.  
  39. class Plane(object):
  40.     def __init__(self, name, properties):
  41.         self.name = name
  42.         self.properties = properties
  43.  
  44.     @classmethod
  45.     def from_idx(cls, idx):
  46.         if idx == 0:
  47.             return cls("PaperPlane", [{"canFly": True}, {"isWaterProof": False}])
  48.         if idx == 1:
  49.             return cls("AirbusA380", [{"canFly": True}, {"isWaterProof": True}, {"hasPassengers": True}])
  50.  
  51.  
  52. class PlaneLookup(object):
  53.     def __init__(self):
  54.         self.plane_dict = {}
  55.  
  56.     def generate(self, n_planes):
  57.         for i in range(n_planes):
  58.             plane_id = uuid.uuid4().hex
  59.             self.plane_dict[plane_id] = Plane.from_idx(np.random.randint(0, 2))
  60.  
  61.     def save(self, filename):
  62.         with gzip.open(filename, 'wb') as f:
  63.             pickle.dump(self.plane_dict, f, pickle.HIGHEST_PROTOCOL)
  64.  
  65.     @classmethod
  66.     def from_disk(cls, filename):
  67.         pl = cls()
  68.         with gzip.open(filename, 'rb') as f:
  69.             pl.plane_dict = pickle.load(f)
  70.         return pl
  71.  
  72. if __name__ == '__main__':
  73.     pl = PlaneLookup()
  74.     pl.generate(1000000)
  75.     print(getsize(pl))
  76.     pl.save("SOMEPATH")
  77.  
  78.     print(getsize(Plane.from_idx(0)))
  79.     print(getsize(Plane.from_idx(1)))
  80.     print(getsize(pl))
  81.     pl=PlaneLookup.from_disk("SOMEPATH")
  82.     print(getsize(pl))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement