Stats attr docstrings

This commit is contained in:
Mahmoud Hashemi 2015-04-08 05:21:53 -07:00
parent 024bfe46a4
commit 7a4be7bdce
1 changed files with 27 additions and 8 deletions

View File

@ -71,14 +71,16 @@ dilemma. ``statsutils`` also includes several robust statistical methods:
Online and Offline Statistics Online and Offline Statistics
----------------------------- -----------------------------
`Online`_ statistics have nothing to do with computer Unrelated to computer networking, `online`_ statistics involve
networks. Rather, it involves calculating statistics in a `streaming`_ calculating statistics in a `streaming`_ fashion, without all the data
fashion, without all the data being available. The :class:`Stats` type being available. The :class:`Stats` type is meant for the more
is meant to be used in the more traditional fashion, for offline traditional offline statistics when all the data is available. For
statistics when all the data is available. pure-Python online statistics accumulators, look at the `Lithoxyl`_
system instrumentation package.
.. _Online: https://en.wikipedia.org/wiki/Online_algorithm .. _Online: https://en.wikipedia.org/wiki/Online_algorithm
.. _streaming: https://en.wikipedia.org/wiki/Streaming_algorithm .. _streaming: https://en.wikipedia.org/wiki/Streaming_algorithm
.. _Lithoxyl: https://github.com/mahmoud/lithoxyl
""" """
from math import floor, ceil from math import floor, ceil
@ -90,6 +92,10 @@ class _StatsProperty(object):
self.func = func self.func = func
self.internal_name = '_' + name self.internal_name = '_' + name
doc = func.__doc__ or ''
pre_doctest_doc, _, _ = doc.partition('>>>')
self.__doc__ = pre_doctest_doc
def __get__(self, obj, objtype=None): def __get__(self, obj, objtype=None):
if obj is None: if obj is None:
return self return self
@ -123,6 +129,12 @@ class Stats(object):
_StatsProperty)] _StatsProperty)]
self._pearson_precision = 0 self._pearson_precision = 0
def __len__(self):
return len(self.data)
def __iter__(self):
return iter(self.data)
def _get_sorted_data(self): def _get_sorted_data(self):
"""When using a copy of the data, it's better to have that copy be """When using a copy of the data, it's better to have that copy be
sorted, but we do it lazily using this method, in case no sorted, but we do it lazily using this method, in case no
@ -337,8 +349,10 @@ class Stats(object):
of a list of values. This has the effect of limiting the of a list of values. This has the effect of limiting the
effect of outliers. effect of outliers.
NOTE: this operation is in-place and does not return a copy. .. note:
This operation modifies the data in-place. It does not
make or return a copy.
""" """
trim = float(amount) trim = float(amount)
if not 0.0 <= trim <= 1.0: if not 0.0 <= trim <= 1.0:
@ -349,6 +363,7 @@ class Stats(object):
if size_diff == 0.0: if size_diff == 0.0:
return return
self.data = self._get_sorted_data()[size_diff:-size_diff] self.data = self._get_sorted_data()[size_diff:-size_diff]
self.clear_cache()
def _get_pow_diffs(self, power): def _get_pow_diffs(self, power):
""" """
@ -358,7 +373,7 @@ class Stats(object):
return [(v - m) ** power for v in self.data] return [(v - m) ** power for v in self.data]
def get_conv_func(attr_name): def _get_conv_func(attr_name):
def stats_helper(data, default=0.0): def stats_helper(data, default=0.0):
return getattr(Stats(data, default=default, use_copy=False), return getattr(Stats(data, default=default, use_copy=False),
attr_name) attr_name)
@ -367,10 +382,14 @@ def get_conv_func(attr_name):
for attr_name, attr in Stats.__dict__.items(): for attr_name, attr in Stats.__dict__.items():
if isinstance(attr, _StatsProperty): if isinstance(attr, _StatsProperty):
func = get_conv_func(attr_name) func = _get_conv_func(attr_name)
func.__doc__ = attr.func.__doc__ func.__doc__ = attr.func.__doc__
globals()[attr_name] = func globals()[attr_name] = func
delattr(Stats, '_calc_' + attr_name) delattr(Stats, '_calc_' + attr_name)
# cleanup
del attr
del attr_name
del func
if __name__ == '__main__': if __name__ == '__main__':