satella.coding.structures.mixins package

Submodules

satella.coding.structures.mixins.enums module

class satella.coding.structures.mixins.enums.ComparableEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

An enum whose compare will try to convert value you compare it against to its instance. Handly for writing code like:

>>> a = 'test'
>>> class Enum(ComparableEnum):
>>>     A = 'test'
>>> assert Enum.A == a

Comparison order doesn’t matter, so either are True:

>>> Enum.A == 'test'
>>> 'test' == Enum.A

Note, however, that following won’t work:

>>> 'test' in (Enum.A, )

You can even compare enums across classes

>>> class A(ComparableEnum):
>>>     A = 1
>>> class B(ComparableEnum):
>>>     A = 1
>>> assert A.A == B.A
class satella.coding.structures.mixins.enums.ComparableIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: HashableIntEnum

An enum.IntEnum that implements comparison, stemming from its values, as well as hashability.

It it has an int() method, it’s fair game as comparison argument for this class.

class satella.coding.structures.mixins.enums.HashableIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

An enum.IntEnum that implements hashability, stemming from its values, as well as hashability

class satella.coding.structures.mixins.enums.OnStrOnlyName

Bases: object

A mix-in to add the following functionality to your class.

tl;dr - the name will be used instead of ClassName.name.

>>> from enum import Enum
>>> class MyEnum(OnStrOnlyName, Enum):
>>>     A = 0
>>>     B = 1
>>>     C = 'test'
>>> assert str(MyEnum.A) == 'A'
>>> assert str(MyEnum.B) == 'B'
>>> assert str(MyEnum.C) == 'test'

satella.coding.structures.mixins.eqable module

class satella.coding.structures.mixins.eqable.DictionaryEQAble

Bases: object

A class mix-in that defines __eq__ and __ne__ to be:

  • both the same exact type (so subclassing won’t work)

  • have the exact same __dict__

satella.coding.structures.mixins.hashable module

class satella.coding.structures.mixins.hashable.ComparableAndHashableBy

Bases: object

A mix-in. Provides comparision (lt, gt, ge, le, eq) and hashing by a field of this class. Hashing is done by invoking hash() on the value of the field, and comparison is done by directly comparing given field’s values.

Example:

>>> class Vector(ComparableAndHashableBy):
>>>     _COMPARABLE_BY = 'length'
>>>     @property
>>>     def length(self):
>>>         return 0
>>>
>>> assert Vector() > Vector()
class satella.coding.structures.mixins.hashable.ComparableAndHashableByInt

Bases: object

A mix-in. Provides comparison (lt, gt, ge, le, eq) and hashing by __int__ of this class.

class satella.coding.structures.mixins.hashable.ComparableAndHashableByStr

Bases: object

A mix-in. Provides comparision (lt, gt, ge, le, eq) and hashing by __str__ of this class. Also, will make this class equal to strings that return the same value.

class satella.coding.structures.mixins.hashable.HashableMixin

Bases: object

Make a class hashable by its ID.

Just remember to add the following to your class definition if you’re overriding __eq__:

>>> class MyClass(HashableMixin):
>>>     __hash__ = HashableMixin.__hash__
class satella.coding.structures.mixins.hashable.OmniHashableMixin

Bases: object

A mix-in. Provides hashing and equal comparison for your own class using specified fields.

Example of use:

>>> class Point2D(OmniHashableMixin):
>>>    _HASH_FIELDS_TO_USE = ('x', 'y')
>>>    def __init__(self, x, y):
>>>        ...

and now class Point2D has defined __hash__ and __eq__ by these fields. Do everything in your power to make specified fields immutable, as mutating them will result in a different hash.

This will also check if the other value is an instance of this instance’s class.

_HASH_FIELDS_TO_USE can be also a single string, in this case a single field called by this name will be taken.

Note that if you’re explicitly providing __eq__ in your child class, you will be required to insert:

>>>     __hash__ = OmniHashableMixin.__hash__

for this to work in your class

satella.coding.structures.mixins.strings module

class satella.coding.structures.mixins.strings.ReprableMixin

Bases: object

A sane __repr__ default.

This takes the values for the __repr__ from repr’ing list of fields defined as class property _REPR_FIELDS.

Set an optional class property of _REPR_FULL_CLASSNAME for __repr__ to output the repr alongside the module name.

Example:

>>> class Test(ReprableMixin):
>>>     _REPR_FIELDS = ('v', )
>>>     def __init__(self, v, **kwargs):
>>>         self.v = v
>>>
>>> assert repr(Test(2)) == "Test(2)"
>>> assert repr(Test('2') == "Test('2')")
class satella.coding.structures.mixins.strings.StrEqHashableMixin

Bases: object

A mix-in that outfits your class with an __eq__ and __hash__ operator that take their values from __str__ representation of your class.

Module contents

class satella.coding.structures.mixins.ComparableAndHashableBy

Bases: object

A mix-in. Provides comparision (lt, gt, ge, le, eq) and hashing by a field of this class. Hashing is done by invoking hash() on the value of the field, and comparison is done by directly comparing given field’s values.

Example:

>>> class Vector(ComparableAndHashableBy):
>>>     _COMPARABLE_BY = 'length'
>>>     @property
>>>     def length(self):
>>>         return 0
>>>
>>> assert Vector() > Vector()
class satella.coding.structures.mixins.ComparableAndHashableByInt

Bases: object

A mix-in. Provides comparison (lt, gt, ge, le, eq) and hashing by __int__ of this class.

class satella.coding.structures.mixins.ComparableAndHashableByStr

Bases: object

A mix-in. Provides comparision (lt, gt, ge, le, eq) and hashing by __str__ of this class. Also, will make this class equal to strings that return the same value.

class satella.coding.structures.mixins.ComparableEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

An enum whose compare will try to convert value you compare it against to its instance. Handly for writing code like:

>>> a = 'test'
>>> class Enum(ComparableEnum):
>>>     A = 'test'
>>> assert Enum.A == a

Comparison order doesn’t matter, so either are True:

>>> Enum.A == 'test'
>>> 'test' == Enum.A

Note, however, that following won’t work:

>>> 'test' in (Enum.A, )

You can even compare enums across classes

>>> class A(ComparableEnum):
>>>     A = 1
>>> class B(ComparableEnum):
>>>     A = 1
>>> assert A.A == B.A
class satella.coding.structures.mixins.ComparableIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: HashableIntEnum

An enum.IntEnum that implements comparison, stemming from its values, as well as hashability.

It it has an int() method, it’s fair game as comparison argument for this class.

class satella.coding.structures.mixins.DictionaryEQAble

Bases: object

A class mix-in that defines __eq__ and __ne__ to be:

  • both the same exact type (so subclassing won’t work)

  • have the exact same __dict__

class satella.coding.structures.mixins.HashableIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

An enum.IntEnum that implements hashability, stemming from its values, as well as hashability

class satella.coding.structures.mixins.HashableMixin

Bases: object

Make a class hashable by its ID.

Just remember to add the following to your class definition if you’re overriding __eq__:

>>> class MyClass(HashableMixin):
>>>     __hash__ = HashableMixin.__hash__
class satella.coding.structures.mixins.OmniHashableMixin

Bases: object

A mix-in. Provides hashing and equal comparison for your own class using specified fields.

Example of use:

>>> class Point2D(OmniHashableMixin):
>>>    _HASH_FIELDS_TO_USE = ('x', 'y')
>>>    def __init__(self, x, y):
>>>        ...

and now class Point2D has defined __hash__ and __eq__ by these fields. Do everything in your power to make specified fields immutable, as mutating them will result in a different hash.

This will also check if the other value is an instance of this instance’s class.

_HASH_FIELDS_TO_USE can be also a single string, in this case a single field called by this name will be taken.

Note that if you’re explicitly providing __eq__ in your child class, you will be required to insert:

>>>     __hash__ = OmniHashableMixin.__hash__

for this to work in your class

class satella.coding.structures.mixins.OnStrOnlyName

Bases: object

A mix-in to add the following functionality to your class.

tl;dr - the name will be used instead of ClassName.name.

>>> from enum import Enum
>>> class MyEnum(OnStrOnlyName, Enum):
>>>     A = 0
>>>     B = 1
>>>     C = 'test'
>>> assert str(MyEnum.A) == 'A'
>>> assert str(MyEnum.B) == 'B'
>>> assert str(MyEnum.C) == 'test'
class satella.coding.structures.mixins.ReprableMixin

Bases: object

A sane __repr__ default.

This takes the values for the __repr__ from repr’ing list of fields defined as class property _REPR_FIELDS.

Set an optional class property of _REPR_FULL_CLASSNAME for __repr__ to output the repr alongside the module name.

Example:

>>> class Test(ReprableMixin):
>>>     _REPR_FIELDS = ('v', )
>>>     def __init__(self, v, **kwargs):
>>>         self.v = v
>>>
>>> assert repr(Test(2)) == "Test(2)"
>>> assert repr(Test('2') == "Test('2')")
class satella.coding.structures.mixins.StrEqHashableMixin

Bases: object

A mix-in that outfits your class with an __eq__ and __hash__ operator that take their values from __str__ representation of your class.