Helpful abstract tools (functions, classes, ... ) for coding in Python. Accessible via, for example, import tools; tools.Object()
. Particular highlights:
Object
(and its siblingsSortedObject
,OrderedObject
), which are dictionaries which can behave a bit like classes, by allowing for.
attribute look up in place of[]
notation.Record
, which can be thought of as anamedtuple
with mutable entries.deepgetattr
anddeepsetattr
which allow for getting and setting attributes of (subsubsub-...)subattributes.set_context_variable
, which allows for easily temporarily setting variables to particular values in contexts.loc_count
, for counting the number of lines of code, comments and whitespace of the files in a directory. Also comes in a script form: just runpython -m tools.scripts.loc_count
in whatever directory you like!
Plus a few more exotic things:
subclass_tracker
, which allows for keeping track of and subsequently locating particular subclasses of a given class.DynamicSubclassingMixin
which allows for dynamically changing the class of an instance on the fly.WithAdder
, which is a mixin that allows for+
-ing contexts together.classproperty
, which marks a method as being both a classmethod and a property. (It's not possible to mix the two usual decorators.)combomethod
, which marks a method as being both a classmethod and an instance method.
And a few boring-but-useful things:
rangeinf
, which is arange
that allows forinf
to be passed as the second argument.nonneg_list
, which is a list that doesn't allow negative indexing.deldict
, which is a dictionary that doesn't mind baddel
attempts.re_sub_recursive
for recursively regex'ing a string.AddBase
, whose__add__
method always returns whatever the other thing added to it was.- Some basic geometry collisions.
...and other stuff besides!