Skip to content
Snippets Groups Projects
structures.rst 6.51 KiB

Structures

SparseMatrix

Heaps

Heap

This essentially allows you to have a heap object that will pretty much behave like the heapq library.

SetHeap

A heap with additional invariant that no two elements on the heap are the same. This is optimized for fast pushes() and membership checks.

TimeBasedHeap

Time-based heap is a good structure if you have many callbacks set to fire at a particular time in the future. It functions very like a normal Heap.

TimeBasedSetHeap

A combination of TimeBasedHeap and SetHeap:

Mixins

Mixins are classes whose constructor you do not need to invoke. They magically endow your class with chosen properties, often by overloading their specific special methods.

HashableMixin

ComparableAndHashableByInt

OmniHashableMixin

If you need quick __hash__ and __eq__ operators from listed fields of the class.

ComparableAndHashableBy

StrEqHashableMixin

A class that outfits your class with __eq__ and __hash__ based off the str() value of the class. So you got to define __str__ at the very least!

ReprableMixin

If you need to provide a quick __repr__ for your classes:

ComparableEnum

HashableIntEnum

An enum.IntEnum that can be __hash__ed

ComparableIntEnum

An enum.IntEnum that you can compare by it's values

Immutable

Make your classes immutable. Normal assignment is only supported in the constructor, anywhere else it's a TypeError.

Immutable inherits from abc.ABCMeta, so it's safe to use abstract base classes here.

class Test(Immutable, metaclass=ABCMeta):

    attr: str = None

    def __init__(self):
        self.attr = 'value'

    def illegal_op(self):
        self.attr = 'test'  # this will TypeError

Singletons

Singleton

Makes the resulting object's __init__() be called at most once, then caches the object and returns the same upon each instantiation.

SingletonWithRegardsTo

Sometimes you just need an almost-singleton class, ie. class whose instance will depend on first n arguments. This function makes it easy:

It will remember instances already created and return you a previously created instance, keying on the first n arguments.

There are also two functions to help you with managing your SingletonWithRegardsTo:

Dictionaries

CountingDict

LRU

ExclusiveWritebackCache

Use it as you would a normal dictionary.

DictObject

DictObject is an object constructed out of a dict, that allows it's values to be obtained as getattr(), and not only getitem().

You can use the following function to recursively turn every dict into a DictObject

frozendict

A dictionary that can't be modified. I didn't import the one from _frozendict PyPI package, because it failed on Python 3.9. It is additionally hashable and __eq__-able

DictionaryView

CacheDict

LRUCacheDict

SelfCleaningDefaultDict

ExpiringEntryDict

TwoWayDictionary

DirtyDict

A dictionary that has also a flag called dirty that says if it's been last modified since that flag was cleared.

The flag is initially (after the dict has been created) set to False.

KeyAwareDefaultDict

Other structures

typednamedtuple

It's a named tuple, but it has typed fields. You will get a TypeError if you try to assign something else there.

HashableWrapper

Ranking

SortedList

SliceableDeque

Proxy

Subqueue