cmonkey.sizes (version 5.12 (Apr 27, 2009)) | index /home/weiju/Projects/ISB/cmonkey-python/cmonkey/sizes.py |
This module exposes 10 functions and 2 classes to obtain lengths
and sizes of Python objects (for Python 2.2 or later [1]).
The main changes in this version are new function calcsize(),
use gc.get_objects() to get all objects and improvements in
this documentation.
Public Functions [2]
Function asizeof calculates the combined (approximate) size
in bytes of one or several Python objects.
Function asizesof returns a tuple containing the (approximate)
size in bytes for each given Python object separately.
Function asized returns for each object an instance of class
Asized containing all the size information of the object and a
tuple with the referents.
Functions basicsize and itemsize return the basic respectively
item size of the given object.
Function flatsize returns the flat size of a Python object in
bytes defined as the basic size plus the item size times the
length of the given object.
Function leng returns the length of an object, like standard
len but extended for several types, e.g. the leng of a multi-
precision int (or long) is the number of digits [3]. The length
of most mutable sequence objects includes an estimate of the
over-allocation and therefore, the leng value may differ from
the standard len result.
Function refs returns (a generator for) the referents of the
given object, i.e. the objects referenced by the given object.
Function calcsize is equivalent to standard struct.calcsize
but handles format characters 'z' for signed C type Py_ssize_t
and 'Z' for unsigned C type size_t.
Certain classes are known to be sub-classes of or to behave as
dict objects. Function adict can be used to install other
class objects to be treated like dict.
Public Classes [2]
An instance of class Asized is returned for each object sized
with the asized function or method.
Class Asizer can be used to accumulate the results of several
asizeof or asizesof calls. After creating an Asizer instance,
use methods asizeof and asizesof to size additional objects.
Call methods exclude_refs and/or exclude_types to exclude
references to or instances or types of certain objects.
Use one of the print\_... methods to report the statistics.
Duplicate Objects
Any duplicate, given objects are sized only once and the size
is included in the combined total only once. But functions
asizesof and asized do return a size value respectively an
Asized instance for each given object, the same for duplicates.
Definitions [4]
The size of an object is defined as the sum of the flat size
of the object plus the sizes of any referents. Referents are
visited recursively up to a given limit. However, the size
of objects referenced multiple times is included only once.
The flat size of an object is defined as the basic size of the
object plus the item size times the number of allocated items.
The flat size does include the size for the items (references
to the referents), but not the referents themselves.
The flat size returned by function flatsize equals the result
of the asizeof function with options code=True, ignored=False,
limit=0 and option align set to the same value.
The accurate flat size for an object is obtained from function
sys.getsizeof() where available. Otherwise, the length and
size of sequence objects as dicts, lists, sets, etc. is based
on an estimate for the number of allocated items. As a result,
the reported length and size may substantially differ from the
actual length and size.
The basic and item sizes are obtained from the __basicsize__
respectively __itemsize__ attribute of the (type of the) object.
Where necessary (e.g. sequence objects), a zero __itemsize__
is replaced by the size of a corresponding C type.
The basic size (of GC managed objects) objects includes the
overhead for Python's garbage collector (GC) as well as the
space needed for refcounts (only in certain Python builds).
Optionally, sizes can be aligned to any power of 2 multiple.
Size of (byte)code
The (byte)code size of objects as classes, functions, methods,
modules, etc. can be included by setting option code.
Iterators are handled similar to sequences: iterated object(s)
are sized like referents if the recursion limit permits. Also,
function gc.get_referents() must return the referent object
of iterators.
Generators are sized as (byte)code only, but generated objects
are never sized.
Old- and New-style Classes
All old- and new-style class, instance and type objects, are
handled uniformly such that (a) instance and class objects can
be distinguished and (b) instances of different old-style
classes can be dealt with separately.
Class and type objects are represented as <class ....* def>
respectively as <type ... def> where an '*' indicates an old-
style class and the def suffix marks the definition object.
Instances of old-style classes are shown as new-style ones but
with an '*' at the end of the name, like <class module.name*>.
Ignored Objects
To avoid excessive sizes, several object types are ignored [4]
by default, e.g. built-in functions, built-in types and classes
[5], function globals and module referents. However, any
instances thereof are sized and module objects will be sized
when passed as given objects. Ignored object types are included
if option ignored is set accordingly.
In addition, many __...__ attributes of callable objects are
ignored, except crucial ones, e.g. class attributes __dict__,
__doc__, __name__ and __slots__. For more details, see the
type-specific _..._refs() and _len_...() functions below.
Option all can be used to size all Python objects and/or get
the referents from gc.get_referents() and override the type-
specific __..._refs() functions.
Notes
[1] Tested with Python 2.2.3, 2.3.7, 2.4.5, 2.5.1, 2.5.2, 2.6.2,
3.0.1 or 3.1a2 on CentOS 4.6, SuSE 9.3, MacOS X 10.4.11 Tiger
(Intel) and 10.3.9 Panther (PPC), Solaris 10 (Opteron) and
Windows XP all 32-bit Python and on RHEL 3u7 and Solaris 10
(Opteron) both 64-bit Python.
[2] The functions and classes in this module are not thread-safe.
[3] See Python source file .../Include/longinterp.h for the
C typedef of digit used in multi-precision int (or long)
objects. The size of digit in bytes can be obtained in
Python from the int (or long) __itemsize__ attribute.
Function leng (rather _len_int) below deterimines the
number of digits from the int (or long) value.
[4] These definitions and other assumptions are rather arbitrary
and may need corrections or adjustments.
[5] Types and classes are considered built-in if the module of
the type or class is listed in _builtin_modules below.
Modules | ||||||
|
Classes | ||||||||||||||||||
|
Functions | ||
|
Data | ||
__all__ = ['adict', 'asized', 'asizeof', 'asizesof', 'Asized', 'Asizer', 'basicsize', 'flatsize', 'itemsize', 'leng', 'refs', 'calcsize'] __version__ = '5.12 (Apr 27, 2009)' |