agate.utils

This module contains a collection of utility classes and functions used in agate.

class agate.utils.Patchable

Adds a monkeypatching extensibility pattern to subclasses.

Calling Class.monkeypatch(AnotherClass) will dynamically add AnotherClass as a base class of Class. This effective is global–even existing instances of the class will have the new methods.

This can only be used to add new methods. It can not be used to override the implementation of an existing method on the patched class.

classmethod monkeypatch(patch_cls)

Dynamically add patch_cls as a base class of this class.

Parameters:patch_cls – The class to be patched on.
class agate.utils.NullOrder

Dummy object used for sorting in place of None.

Sorts as “greater than everything but other nulls.”

class agate.utils.Quantiles(quantiles)

A class representing quantiles (percentiles, quartiles, etc.) for a given column of Number data.

locate(value)

Identify which quantile a given value is part of.

agate.utils.memoize(func)

Dead-simple memoize decorator for instance methods that take no arguments.

This is especially useful since so many of our classes are immutable.

agate.utils.median(data_sorted)

Finds the median value of a given series of values.

Parameters:data_sorted – The values to find the median of. Must be sorted.
agate.utils.max_precision(values)

Given a series of values (such as a Column) returns the most significant decimal places present in any value.

Parameters:values – The values to analyze.
agate.utils.make_number_formatter(decimal_places)

Given a number of decimal places creates a formatting string that will display numbers with that precision.

agate.utils.round_limits(minimum, maximum)

Rounds a pair of minimum and maximum values to form reasonable “round” values suitable for use as axis minimum and maximum values.

Values are rounded “out”: up for maximum and down for minimum, and “off”: to one higher than the first significant digit shared by both.

See unit tests for examples.