Ejemplo n.º 1
0
    def test_globals(self):
        '''Test globals examples'''
        self._printf('%sasizeof(%s, limit=%s, code=%s) ... %s', os.linesep, 'globals()', 'MAX', False, '-glob[als]')
        asizeof.asizeof(globals(), limit=self.MAX, code=False, stats=1)
        self._print_functions(globals(), 'globals()', opt='-glob[als]')

        self._printf('%sasizesof(%s, limit=%s, code=%s) ... %s', os.linesep, 'globals(), locals()', 'MAX', False, '-glob[als]')
        asizeof.asizesof(globals(), locals(), limit=self.MAX, code=False, stats=1)
        asizeof.asized(globals(), align=0, detail=self.MAX, limit=self.MAX, code=False, stats=1)
Ejemplo n.º 2
0
 def test_iterator(self):
     '''Test iterator examples'''
     self._printf('%sasizeof(%s, code=%s) ... %s', os.linesep, '<iterator>', False, '-iter[ator]')
     o = iter('0123456789')
     e = iter('')
     d = iter({})
     i = iter(asizeof._items({1:1}))
     k = iter(asizeof._keys({2:2, 3:3}))
     v = iter(asizeof._values({4:4, 5:5, 6:6}))
     l = iter([])
     t = iter(())
     asizeof.asizesof(o, e, d, i, k, v, l, t, limit=0, code=False, stats=1)
     asizeof.asizesof(o, e, d, i, k, v, l, t, limit=9, code=False, stats=1)
Ejemplo n.º 3
0
 def test_slots(self):
     '''Test slots examples'''
     self._printf('%sasizeof(%s, code=%s) ... %s', os.linesep, '<__slots__>', False, '-slots')
     class Old:
         pass  # m = None
     class New(object):
         __slots__ = ('n',)
     class Sub(New):
         __slots__ = {'s': ''}  # duplicate!
         def __init__(self):
             New.__init__(self)
      # basic instance sizes
     o, n, s = Old(), New(), Sub()
     asizeof.asizesof(o, n, s, limit=self.MAX, code=False, stats=1)
      # with unique min attr size
     o.o = 'o'
     n.n = 'n'
     s.n = 'S'
     s.s = 's'
     asizeof.asizesof(o, n, s, limit=self.MAX, code=False, stats=1)
      # with duplicate, intern'ed, 1-char string attrs
     o.o = 'x'
     n.n = 'x'
     s.n = 'x'
     s.s = 'x'
     asizeof.asizesof(o, n, s, 'x', limit=self.MAX, code=False, stats=1)
      # with larger attr size
     o.o = 'o'*1000
     n.n = 'n'*1000
     s.n = 'n'*1000
     s.s = 's'*1000
     asizeof.asizesof(o, n, s, 'x'*1000, limit=self.MAX, code=False, stats=1)
Ejemplo n.º 4
0
    def test_asizesof(self):
        '''Test asizeof.asizesof()
        '''
        self.assertEqual(list(asizeof.asizesof()), [])
        self.assertRaises(KeyError, asizeof.asizesof, **{'all': True})

        objs = [Foo(42), ThinFoo("spam"), OldFoo(67)]
        sizes = list(asizeof.asizesof(*objs))
        objs.reverse()
        rsizes = list(asizeof.asizesof(*objs))
        self.assertEqual(len(sizes), 3)
        rsizes.reverse()
        self.assertEqual(sizes, rsizes, (sizes, rsizes))
        objs.reverse()
        isizes = [asizeof.asizeof(obj) for obj in objs]
        self.assertEqual(sizes, isizes, (sizes, isizes))
Ejemplo n.º 5
0
    def test_asizeof(self):
        '''Test asizeof.asizeof()
        '''
        self.assertEqual(asizeof.asizeof(), 0)

        objs = [Foo(42), ThinFoo("spam"), OldFoo(67)]
        total = asizeof.asizeof(*objs)
        sizes = list(asizeof.asizesof(*objs))
        sum = 0
        for sz in sizes:
            sum += sz
        self.assertEqual(total, sum, (total, sum))
Ejemplo n.º 6
0
    def test_asizeof(self):
        '''Test asizeof.asizeof()
        '''
        self.assertEqual(asizeof.asizeof(), 0)

        objs = [Foo(42), ThinFoo("spam"), OldFoo(67)]
        total = asizeof.asizeof(*objs)
        sizes = list(asizeof.asizesof(*objs))
        sum = 0
        for sz in sizes:
            sum += sz
        self.assertEqual(total, sum, (total, sum))
Ejemplo n.º 7
0
    def test_asizesof(self):
        '''Test asizeof.asizesof()
        '''
        self.assertEqual(list(asizeof.asizesof()), [])
        self.assertRaises(KeyError, asizeof.asizesof, **{'all': True})

        objs = [Foo(42), ThinFoo("spam"), OldFoo(67)]
        sizes = list(asizeof.asizesof(*objs))
        objs.reverse()
        rsizes = list(asizeof.asizesof(*objs))
        self.assertEqual(len(sizes), 3)
        rsizes.reverse()
        self.assertEqual(sizes, rsizes, (sizes, rsizes))
        objs.reverse()
        isizes = [asizeof.asizeof(obj) for obj in objs]
        self.assertEqual(sizes, isizes)
        sizer = asizeof.Asizer()
        asizer_sizes = sizer.asizesof(*objs)
        self.assertEqual(list(asizer_sizes), sizes)
        code_sizes = sizer.asizesof(*objs, **dict(code=True))
        self.failIfEqual(list(code_sizes), sizes)
Ejemplo n.º 8
0
def slots_memory(num=0):
    attrs = list(ascii_letters[:num])

    class Unslotted(object):
        pass

    class Slotted(object):
        __slots__ = attrs

    unslotted = Unslotted()
    slotted = Slotted()
    for attr in attrs:
        unslotted.__dict__[attr] = 0
    exec('slotted.%s = 0' % attr, globals(), locals())
    memory_use = asizesof(slotted, unslotted, unslotted.__dict__)
    return memory_use
Ejemplo n.º 9
0
    def test_slots(self):
        '''Test slots examples'''
        self._printf('%sasizeof(%s, code=%s) ... %s', os.linesep,
                     '<__slots__>', False, '-slots')

        class Old:
            pass  # m = None

        class New(object):
            __slots__ = ('n', )

        class Sub(New):
            __slots__ = {'s': ''}  # duplicate!

            def __init__(self):
                New.__init__(self)

        # basic instance sizes
        o, n, s = Old(), New(), Sub()
        asizeof.asizesof(o, n, s, limit=self.MAX, code=False, stats=1)
        # with unique min attr size
        o.o = 'o'
        n.n = 'n'
        s.n = 'S'
        s.s = 's'
        asizeof.asizesof(o, n, s, limit=self.MAX, code=False, stats=1)
        # with duplicate, intern'ed, 1-char string attrs
        o.o = 'x'
        n.n = 'x'
        s.n = 'x'
        s.s = 'x'
        asizeof.asizesof(o, n, s, 'x', limit=self.MAX, code=False, stats=1)
        # with larger attr size
        o.o = 'o' * 1000
        n.n = 'n' * 1000
        s.n = 'n' * 1000
        s.s = 's' * 1000
        asizeof.asizesof(o,
                         n,
                         s,
                         'x' * 1000,
                         limit=self.MAX,
                         code=False,
                         stats=1)
Ejemplo n.º 10
0
        recipient="recipient.example.com",
        body="Hello, World!",
    )
    if sys.version_info.minor >= 7:
        simple = DataClassMessage(
            sender="*****@*****.**",
            recipient="recipient.example.com",
            body="Hello, World!",
        )
        slotted = SlotMessage(
            sender="*****@*****.**",
            recipient="recipient.example.com",
            body="Hello, World!",
        )
        print("Dataclass %d, Slotted dataclass %d" %
              asizeof.asizesof(simple, slotted))
    attrs = AttrsMessage(
        sender="*****@*****.**",
        recipient="recipient.example.com",
        body="Hello, World!",
    )
    print("NamedTuple %d,  Attrs %d" % asizeof.asizesof(pos, attrs))

    print(
        timeit(
            """m = Message(sender="*****@*****.**",
                recipient="recipient.example.com",body="Hello, World!",)
            """,
            globals=globals(),
        ))
    if sys.version_info.minor >= 7:
Ejemplo n.º 11
0
    def to_db(self, server = 'beavis.ph.utexas.edu'):
        print 'in to_db'
        sim_blob = {'author': 'Dmitry',
         'tags': ['fusion', 'in', '50 years'],
         'date': datetime.utcnow()}
        for key, value in self.__dict__.iteritems():
            try:
                print key, value.__class__, sys.getsizeof(value), asizeof(value)
            except:
                print key

            if type(value) not in ban_types:
                sim_blob[key] = value
            elif type(value) == type(np.array([12])):
                print key, value.nbytes
                if value.ndim == 1:
                    sim_blob[key] = value.tolist()
                elif value.size == 1:
                    sim_blob[key] = value
                else:
                    print 'adding to the db obj', key, type(value), value.shape, value.size
                    if value.size< 1600000:
                        sim_blob[key] = value
                    else:
                        print 'nevermind, ', key, ' is too big'

        print 'before: ', asizeof(sim_blob), ' ', sys.getsizeof(sim_blob), ' ', flatsize(sim_blob)
        cutlist = ['pos_i','kx','ky','pdf_x','df_x','v']
        #cutlist = ['pos_i','kx','ky','pdf_x','df_x','v']
        #cutlist = ['t_stop','dx']

        keep_list = ['nz','nx','alpha_c']
        
        for k in cutlist:
            try:
                print 'k: ',(sim_blob[k])
                sim_blob.pop(k, None)
            except:
                print 'not found'
        #new_blob = {}
    # for k in keep_list:
        #     new_blob.pop(k, None)

        # print 'look for bugs'
        # for k in sim_blob:
        #     print 'trying: ',k#,sim_blob[k]
        #     new_blob[k] = sim_blob[k]
        #     ser_dict = self.serialize(new_blob)
        #     #print sys.getsizeof(new_dict), asizeof(ser_dict), ' ', asizesof(sim_blob)
        #     print BSON.encode(ser_dict).__sizeof__()

        # exit()

        #sim_blob = new_blob

        print 'after: ', asizeof(sim_blob), ' ', sys.getsizeof(sim_blob), ' ', flatsize(sim_blob)
        print 'coarse_x' in sim_blob
        print 'df_x' in sim_blob
        #print type(sim_blob['coarse_x']), sim_blob['coarse_x'][0].__class__
        #exit()
    
        ser_dict = self.serialize(sim_blob)
        # for elem in ser_dict:
        #     print elem,': ',type(ser_dict[elem])m
        #print ser_dict
        print sys.getsizeof(ser_dict), asizeof(ser_dict), ' ', asizesof(sim_blob)
        print BSON.encode(ser_dict).__sizeof__()
        #exit()
        c = MongoClient(host=server)
        db = c.new_database
        print 'db info'
        print db.command('collstats', 'alpha_runs')
        alpha_runs = db.alpha_runs
        alpha_runs.ensure_index('md5', unique=True, dropDups=True)
        #db.things.ensureIndex({'source_references.key' : 1}, {unique : true, dropDups : true})

        try:
            #print ser_dict.keys()
            alpha_runs.insert(ser_dict, db)
        except mongoErr.DuplicateKeyError:
            alpha_runs.remove({'path': ser_dict['path']})
            alpha_runs.remove({'md5': ser_dict['md5']})
            
            #alpha_runs.remove({ "$and" :[{'path': ser_dict['path']},{'md5': ser_dict['md5']}]})
            alpha_runs.insert(ser_dict, db)
            print 'Duplicate run not adding to db, but updating'
Ejemplo n.º 12
0
    lat: float


@dataclass
class SlotPosition:
    __slots__ = ['name', 'lon', 'lat']
    name: str
    lon: float
    lat: float


#%%
from pympler import asizeof
simple = SimplePosition('London', -0.1, 51.5)
slot = SlotPosition('Madrid', -3.7, 40.4)
asizeof.asizesof(simple, slot)

#%%
from timeit import timeit
timeit('slot.name',
       setup="slot=SlotPosition('Oslo', 10.8, 59.9)",
       globals=globals())
# 0.13451020000502467
timeit('simple.name',
       setup="simple=SimplePosition('Oslo', 10.8, 59.9)",
       globals=globals())
# 0.13348109999787994

# no difference...

#%%
Essentially, slots are defined using .__slots__ to list the variables on a class

Variables or attributes not present in .__slots__ may not be defined

Furthermore, a slots class may not have default values

The benefit of adding such restrictions is that
certain optimizations may be done

For instance, slots classes take up less memory, as can be
measured using Pympler:
"""

simple = SimplePosition('London', -0.1, 51.5)
slot = SlotPosition('Madrid', -3.7, 40.4)
print(asizeof.asizesof(simple, slot))
"""
Similarly, slots classes are typically faster to work with.

The following example measures the speed of attribute access on a
slots data class and a regular data class using timeit from the 
standard library.
"""

print(
    timeit('slot.name',
           setup="slot=SlotPosition('Oslo', 10.8, 59.9)",
           globals=globals()))

print(
    timeit('simple.name',
Ejemplo n.º 14
0
    def play(self, fun = 4):
        print("Yaaaaa!")
        self.boredom -= fun
        if self.boredom < 0:
            self.boredom = 0
        self.__pass_time()

b = Critter2("Anthon")
b.talk()
b.eat()
b.play()
b.talk()
print()
print(b.__slots__)
print()
print(asizeof.asizesof(a))
print(asizeof.asizesof(b))

#Вывод: при использовании слотов, класс занимает в 2.5 раза меньше памяти(в данном случае)

#______________________________________________________________________
def main():
    crit_name = input("Critter name: ")
    crit = Critter(crit_name)

    choice = None
    while choice != "0":
        print("""
        0 - Go out
        1 - Health
        2 - Feed
Ejemplo n.º 15
0
def main():
    
    
    # A named tuple is the previous to the data class
    NamedTupleCard = namedtuple('NameTupleCard', ['rank','suit'])
    
    # Set the NamedTupleCard
    queen_of_hearts = NamedTupleCard('Q','Hearts')
    print(queen_of_hearts.rank)
    
    # Initialize a data class
    @dataclass
    class Position:
        name: str
        lon: float
        lat: float

    pos = Position('Oslo',10.8, 59.9)
    print(f'{pos.name} is at {pos.lat} N, {pos.lon} E')

    '''What's the benefit of a data class? 
    A data class is a regular Python class except for the fact that a class has model methods like .__init__(),
    .__repr__(), .__eq__() whereas a data class does not require these mandatory methods.'''


    class PositionSetStandardValues:
        def __init__(self, name, lon, lat):
            self.name = name
            self.lon  = lon
            self.lat  = lat

    @dataclass
    class PositionSetStandardValues:
        name: str
        lon: float = 0.0
        lat: float = 0.0

        

    @dataclass
    class PlayingCard:
        rank: str
        suit: str

    @dataclass
    class Deck:
        cards: List[PlayingCard]

    RANKS = '2 3 4 5 6 7 8 9 10 J Q K A'.split()
    SUITS = '\u2660 \u2663 \u2665 \u2666'.split() #UTF-8 icons

    def make_french_deck():
        return [PlayingCard(r, s) for s in SUITS for r in RANKS]

    #print(Deck())

    @dataclass
    class Deck:

        cards: List[PlayingCard] = field(default_factory=make_french_deck)
        def __repr__(self):
            cards = ', '.join(f'{c!s}' for c in self.cards)
            return f'{self.__class__.__name__}({cards})'

    print(Deck())
    # The field specifier is used to customize each data class individually,
    # that is, we can have multiple decks of different symbols.

    # Inheritance with data classes

    @dataclass
    class Capital(Position):
        country: str
    
    Capital('Oslo', 10.8, 59.9, 'Norway')

    # Re-ordering inheritance with data classes

    @dataclass
    class Position:
        name: str
        lon: float = 0.0
        lat: float = 0.0

    @dataclass
    class Capital(Position):
        country: str = 'Unknown'
        lat : float = 40.0

    # the data class looks for name, lon, lat and country
    # and Berlin is contained in name
    Capital('Berlin', country='Germany') 

    # Optimizing data classes

    # you can use slots for classes to make them faster and use less memory

    @dataclass
    class SlotPosition:
        __slot__ = ['name', 'lon', 'lat']
        name: str
        lon: float
        lat: float


    @dataclass
    class SimplePosition:
        name: str
        lon: float
        lat: float

    simple = SimplePosition('London', -0.1, 51.5)
    slot = SlotPosition('Madrid', -3.7, 40.4)
    print(asizeof.asizesof(simple, slot))