Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import uuid
- import numpy as np
- import gzip
- import pickle
- import sys
- from numbers import Number
- from collections import Set, Mapping, deque
- try: # Python 2
- zero_depth_bases = (basestring, Number, xrange, bytearray)
- iteritems = 'iteritems'
- except NameError: # Python 3
- zero_depth_bases = (str, bytes, Number, range, bytearray)
- iteritems = 'items'
- def getsize(obj_0):
- """Recursively iterate to sum size of object & members."""
- def inner(obj, _seen_ids = set()):
- obj_id = id(obj)
- if obj_id in _seen_ids:
- return 0
- _seen_ids.add(obj_id)
- size = sys.getsizeof(obj)
- if isinstance(obj, zero_depth_bases):
- pass # bypass remaining control flow and return
- elif isinstance(obj, (tuple, list, Set, deque)):
- size += sum(inner(i) for i in obj)
- elif isinstance(obj, Mapping) or hasattr(obj, iteritems):
- size += sum(inner(k) + inner(v) for k, v in getattr(obj, iteritems)())
- # Check for custom object instances - may subclass above too
- if hasattr(obj, '__dict__'):
- size += inner(vars(obj))
- if hasattr(obj, '__slots__'): # can have __slots__ with __dict__
- size += sum(inner(getattr(obj, s)) for s in obj.__slots__ if hasattr(obj, s))
- return size
- return inner(obj_0)
- class Plane(object):
- def __init__(self, name, properties):
- self.name = name
- self.properties = properties
- @classmethod
- def from_idx(cls, idx):
- if idx == 0:
- return cls("PaperPlane", [{"canFly": True}, {"isWaterProof": False}])
- if idx == 1:
- return cls("AirbusA380", [{"canFly": True}, {"isWaterProof": True}, {"hasPassengers": True}])
- class PlaneLookup(object):
- def __init__(self):
- self.plane_dict = {}
- def generate(self, n_planes):
- for i in range(n_planes):
- plane_id = uuid.uuid4().hex
- self.plane_dict[plane_id] = Plane.from_idx(np.random.randint(0, 2))
- def save(self, filename):
- with gzip.open(filename, 'wb') as f:
- pickle.dump(self.plane_dict, f, pickle.HIGHEST_PROTOCOL)
- @classmethod
- def from_disk(cls, filename):
- pl = cls()
- with gzip.open(filename, 'rb') as f:
- pl.plane_dict = pickle.load(f)
- return pl
- if __name__ == '__main__':
- pl = PlaneLookup()
- pl.generate(1000000)
- print(getsize(pl))
- pl.save("SOMEPATH")
- print(getsize(Plane.from_idx(0)))
- print(getsize(Plane.from_idx(1)))
- print(getsize(pl))
- pl=PlaneLookup.from_disk("SOMEPATH")
- print(getsize(pl))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement