scorekeeper package

Submodules

scorekeeper.scorekeeper module

class scorekeeper.scorekeeper.Scorekeeper[source]

Bases: scorekeeper.scorekeeper._ScorekeeperSharedState

Scorekeeper is generally used as a superclass, with subclasses being able to isolate state (score) and having the ability to be configured with defaults. That being said, it’s definitely possible to just use the Scorekeeper class in very simple applications where, for example, only a single state needs to be maintained. This can cause complications, especially if the package is not used in an isolated environment such as a virtualenv.

A Scorekeeper object accumulates points and stores them as a score. It can have a decay which is the number of seconds it takes for a point of score to decay. Finally, it has a threshold which is the score that must be exceeded in order to perform a callback.

Examples:
>>> scorekeeper = Scorekeeper()
>>> scorekeeper.score
0
>>> scorekeeper(10)
>>> scorekeeper.score
10
>>> @score(Scorekeeper, 10)
>>> def check_api():
...    if not api_call().successful:
...        return False
...    return True
...
>>> scorekeeper = Scorekeeper()
>>> scorekeeper.score
0
>>> check_api()
False
>>> scorekeeper.score
10
Commonly overridden attributes for subclasses of Scorekeeper are:
  • default_threshold
  • default_decay
  • default_callback
Attributes:
default_threshold (int): The default threshold which is the score that needs
to be exceeded in order for the provided callback to be called.
default_decay (int, float): The default decay which is the number of seconds
it takes for the score to be reduced by 1 point. For example, if the current score is 20 and the decay is 5, after 5 seconds the score will be 19 and after 10 seconds the score will be 18, etc. This is optional.
Args:

threshold (int): Replaces any default threshold. Optional. decay (int, float): Replaces any default decay. Optional. callback (callable): Is called when threshold is exceeded, otherwise raises

a ScoreExceededError. Optional.
default_callback()[source]

The default callback that is called when the score exceeds the threshold.

default_decay = None
default_threshold = 100
reset_score()[source]

Reset score to 0 and persist to the filesystem.

scorekeeper.scorekeeper.score(scorekeeper_class, points, **kwargs)[source]

Execute the decorated function and add points to the provided Scorekeeper’s score if the function returns Falsey, otherwise do nothing.

args:
scorekeeper_class (Scorekeeper): A Scorekeeper or subclass of
Scorekeeper that will be used to store the score.
points (int): The number of points that will be added to the score of
scorekeeper_class if the decorated function returns Falsey.
**kwargs: Arbitrary arguments that are passed to the scorekeeper_class
__call__ method.

Module contents

class scorekeeper.Scorekeeper[source]

Bases: scorekeeper.scorekeeper._ScorekeeperSharedState

Scorekeeper is generally used as a superclass, with subclasses being able to isolate state (score) and having the ability to be configured with defaults. That being said, it’s definitely possible to just use the Scorekeeper class in very simple applications where, for example, only a single state needs to be maintained. This can cause complications, especially if the package is not used in an isolated environment such as a virtualenv.

A Scorekeeper object accumulates points and stores them as a score. It can have a decay which is the number of seconds it takes for a point of score to decay. Finally, it has a threshold which is the score that must be exceeded in order to perform a callback.

Examples:
>>> scorekeeper = Scorekeeper()
>>> scorekeeper.score
0
>>> scorekeeper(10)
>>> scorekeeper.score
10
>>> @score(Scorekeeper, 10)
>>> def check_api():
...    if not api_call().successful:
...        return False
...    return True
...
>>> scorekeeper = Scorekeeper()
>>> scorekeeper.score
0
>>> check_api()
False
>>> scorekeeper.score
10
Commonly overridden attributes for subclasses of Scorekeeper are:
  • default_threshold
  • default_decay
  • default_callback
Attributes:
default_threshold (int): The default threshold which is the score that needs
to be exceeded in order for the provided callback to be called.
default_decay (int, float): The default decay which is the number of seconds
it takes for the score to be reduced by 1 point. For example, if the current score is 20 and the decay is 5, after 5 seconds the score will be 19 and after 10 seconds the score will be 18, etc. This is optional.
Args:

threshold (int): Replaces any default threshold. Optional. decay (int, float): Replaces any default decay. Optional. callback (callable): Is called when threshold is exceeded, otherwise raises

a ScoreExceededError. Optional.
default_callback()[source]

The default callback that is called when the score exceeds the threshold.

default_decay = None
default_threshold = 100
reset_score()[source]

Reset score to 0 and persist to the filesystem.

scorekeeper.score(scorekeeper_class, points, **kwargs)[source]

Execute the decorated function and add points to the provided Scorekeeper’s score if the function returns Falsey, otherwise do nothing.

args:
scorekeeper_class (Scorekeeper): A Scorekeeper or subclass of
Scorekeeper that will be used to store the score.
points (int): The number of points that will be added to the score of
scorekeeper_class if the decorated function returns Falsey.
**kwargs: Arbitrary arguments that are passed to the scorekeeper_class
__call__ method.