pympler.classtracker_stats

Provide saving, loading and presenting gathered ClassTracker statistics.

Classes

class pympler.classtracker_stats.Stats(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Presents the memory statistics gathered by a ClassTracker based on user preferences.

__init__(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Initialize the data log structures either from a ClassTracker instance (argument tracker) or a previously dumped file (argument filename).

Parameters
  • tracker – ClassTracker instance

  • filename – filename of previously dumped statistics

  • stream – where to print statistics, defaults to sys.stdout

dump_stats(fdump: Union[str, IO[bytes]], close: bool = True) None

Dump the logged data to a file. The argument file can be either a filename or an open file object that requires write access. close controls if the file is closed before leaving this method (the default behaviour).

load_stats(fdump: Union[str, IO[bytes]]) None

Load the data from a dump file. The argument fdump can be either a filename or an open file object that requires read access.

reverse_order() pympler.classtracker_stats.Stats

Reverse the order of the tracked instance index self.sorted.

sort_stats(*args: str) pympler.classtracker_stats.Stats

Sort the tracked objects according to the supplied criteria. The argument is a string identifying the basis of a sort (example: ‘size’ or ‘classname’). When more than one key is provided, then additional keys are used as secondary criteria when there is equality in all keys selected before them. For example, sort_stats('name', 'size') will sort all the entries according to their class name, and resolve all ties (identical class names) by sorting by size. The criteria are fields in the tracked object instances. Results are stored in the self.sorted list which is used by Stats.print_stats() and other methods. The fields available for sorting are:

‘classname’

the name with which the class was registered

‘name’

the classname

‘birth’

creation timestamp

‘death’

destruction timestamp

‘size’

the maximum measured size of the object

‘tsize’

the measured size during the largest snapshot

‘repr’

string representation of the object

Note that sorts on size are in descending order (placing most memory consuming items first), whereas name, repr, and creation time searches are in ascending order (alphabetical).

The function returns self to allow calling functions on the result:

stats.sort_stats('size').reverse_order().print_stats()
class pympler.classtracker_stats.ConsoleStats(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Presentation layer for Stats to be used in text-based consoles.

__init__(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Initialize the data log structures either from a ClassTracker instance (argument tracker) or a previously dumped file (argument filename).

Parameters
  • tracker – ClassTracker instance

  • filename – filename of previously dumped statistics

  • stream – where to print statistics, defaults to sys.stdout

print_stats(clsname: Optional[str] = None, limit: float = 1.0) None

Write tracked objects to stdout. The output can be filtered and pruned. Only objects are printed whose classname contain the substring supplied by the clsname argument. The output can be pruned by passing a limit value.

Parameters
  • clsname – Only print objects whose classname contain the given substring.

  • limit – If limit is a float smaller than one, only the supplied percentage of the total tracked data is printed. If limit is bigger than one, this number of tracked objects are printed. Tracked objects are first filtered, and then pruned (if specified).

print_summary() None

Print per-class summary for each snapshot.

class pympler.classtracker_stats.HtmlStats(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Output the ClassTracker statistics as HTML pages and graphs.

__init__(tracker: Optional[ClassTracker] = None, filename: Optional[str] = None, stream: Optional[IO] = None)

Initialize the data log structures either from a ClassTracker instance (argument tracker) or a previously dumped file (argument filename).

Parameters
  • tracker – ClassTracker instance

  • filename – filename of previously dumped statistics

  • stream – where to print statistics, defaults to sys.stdout

create_html(fname: str, title: str = 'ClassTracker Statistics') None

Create HTML page fname and additional files in a directory derived from fname.