Created
January 10, 2018 20:32
-
-
Save aisurfer/4768fa32f214bb6498f92e84387b6c96 to your computer and use it in GitHub Desktop.
Test of various ways to store fields in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# vim: set fileencoding=utf-8 | |
import sys, os, re, glob, copy | |
from collections import namedtuple | |
# | |
# It's critical to inherit all classes from "object" base class | |
def get_size(obj, seen=None): | |
"""Recursively finds size of objects""" | |
size = sys.getsizeof(obj) | |
if seen is None: | |
seen = set() | |
obj_id = id(obj) | |
if obj_id in seen: | |
return 0 | |
# Important mark as seen *before* entering recursion to gracefully handle | |
# self-referential objects | |
seen.add(obj_id) | |
if isinstance(obj, dict): | |
size += sum([get_size(v, seen) for v in obj.values()]) | |
size += sum([get_size(k, seen) for k in obj.keys()]) | |
elif hasattr(obj, '__dict__'): | |
size += get_size(obj.__dict__, seen) | |
elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)): | |
size += sum([get_size(i, seen) for i in obj]) | |
return size | |
TupleObj = (0.0, 1.0, 2.0, 3.0, 4.0) | |
class ClassEmpty(object): | |
def __init__(self): | |
pass | |
class ClassFields(object): | |
def __init__(self): | |
self.Field0 = 0.0 | |
self.Field1 = 1.0 | |
self.Field2 = 2.0 | |
self.Field3 = 3.0 | |
self.Field4 = 4.0 | |
class ClassFieldsSlots(object): | |
__slots__ = ('Field0', 'Field1', 'Field2', 'Field3', 'Field4') | |
def __init__(self): | |
self.Field0 = 0.0 | |
self.Field1 = 1.0 | |
self.Field2 = 2.0 | |
self.Field3 = 3.0 | |
self.Field4 = 4.0 | |
class ClassNamedTuple(namedtuple('ClassNamedTuple', ['Field0', 'Field1', 'Field2', 'Field3', 'Field4'])): | |
__slots__ = () | |
pass | |
""" | |
It was bad idea | |
class ClassVector(object): | |
PosField0 = 0 | |
PosFieldsCount = PosField0 + 1 | |
def __init__(self): | |
self.Values = [0.0] * self.PosFieldsCount | |
Field0 = property() | |
@Field0.getter | |
def Field0(self): return self.Values[self.PosField0] | |
@Field0.setter | |
def Field0(self, v): self.Values[self.PosField0] = v | |
""" | |
print "namedtuple", get_size(namedtuple('N', [])()) | |
print "Tuple size 5", get_size(TupleObj) | |
print "ClassEmpty", get_size(ClassEmpty()) | |
print "ClassFields", get_size(ClassFields()) | |
print "ClassFieldsSlots", get_size(ClassFieldsSlots()) | |
print "ClassNamedTuple", get_size(ClassNamedTuple(0.0, 1.0, 2.0, 3.0, 4.0)) | |
cf = ClassFields() | |
cf.Fields5 = 5.0 | |
cs = ClassFieldsSlots() | |
#cs.Fields5 = 5.0 error | |
cn = ClassNamedTuple(0.0, 1.0, 2.0, 3.0, 4.0) | |
#cn.Fields5 = 5.0 # error | |
print "Tuple size 5", dir(TupleObj) | |
print "ClassEmpty", dir(ClassEmpty()) | |
print "ClassFields", dir(cf) | |
print "ClassFieldsSlots", dir(cs) | |
print "ClassNamedTuple", dir(cn) | |
""" | |
namedtuple 352 | |
Tuple size 5 216 | |
ClassEmpty 344 | |
ClassFields 679 | |
ClassFieldsSlots 88 | |
ClassNamedTuple 727 | |
Tuple size 5 ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index'] | |
ClassEmpty ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] | |
ClassFields ['Field0', 'Field1', 'Field2', 'Field3', 'Field4', 'Fields5', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__'] | |
ClassFieldsSlots ['Field0', 'Field1', 'Field2', 'Field3', 'Field4', '__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__'] | |
ClassNamedTuple ['Field0', 'Field1', 'Field2', 'Field3', 'Field4', '__add__', '__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__getstate__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_asdict', '_fields', '_make', '_replace', 'count', 'index'] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment