protobase.traits package
Submodules
protobase.traits.cmp module
- class protobase.traits.cmp.Eq(*args, **kwargs)
Bases:
Trait,objectA trait that enables equality operators (__eq__, __ne__) based on the fields of a proto object.
Example
>>> from protobase.core import Obj >>> from protobase.trait import Eq >>> >>> class Point(Obj, Eq): ... x: int ... y: int ... >>> p1 = Point(x=1, y=2) >>> p2 = Point(x=2, y=1) >>> p3 = Point(x=1, y=2) >>> p1 == p2 False >>> p1 == p3 True >>> p1 != p2 True >>> p1 != p3 False
protobase.traits.consed module
- class protobase.traits.consed.Consed(*args, **kwargs)
Bases:
Hash,Eq,Inmutable,Init,Weak,Trait,objectTrait for a class that is a hash-consed object. Conses objects are objects that are unique for each set of field values. They have a fixed identity and can be compared with ‘is’.
Example
>>> class Foo(Base, Consed): ... a: int ... b: int ... c: int >>> a = Foo(1, 2, 3) >>> b = Foo(1, 2, 3) >>> a is b True >>> c = Foo(1, 2, 4) >>> a is c False
- protobase.traits.consed.consed_count(cls: type[protobase.traits.consed.Consed]) int
Returns the number of instances of Consed class.
- Parameters:
cls (type[Consed]) – The Consed class.
- Returns:
The number of instances of Consed class.
- Return type:
int
Example
>>> class Foo(Consed): ... pass >>> Foo() Foo() >>> consed_count(Foo) 1 >>> Foo() Foo() >>> consed_count(Foo) 1
protobase.traits.dynamic_attrs module
protobase.traits.hash module
- class protobase.traits.hash.Hash(*args, **kwargs)
-
Trait that implements the __hash__ function in a class.
This implementation of __hash__ hashes all the field values of the object.
Is the class has the slot __hash_cache__, the hash is cached in that slot. This is done to avoid recalculating the hash of the object every time it is hashed.
Example
>>> class Foo(Object, Hash): ... a: int ... b: int ... c: int >>> foo = Foo(1, 2, 3) >>> hash(foo) 3713081631934410656
protobase.traits.init module
- class protobase.traits.init.Init(*args, **kwargs)
Bases:
Trait,objectTrait for initializing fields on a class. This trait automatically generates an __init__ method for the class, with keyword-only arguments for each field.
Example
>>> class Foo(Base, Init): ... x: int = 1 ... y: int = 2 >>> foo = Foo(x=3) >>> foo.x 3 >>> foo.y 2
protobase.traits.inmutable module
- class protobase.traits.inmutable.Inmutable(*args, **kwargs)
-
Trait for making a class readonly.
This trait overrides the __setattr__ method to raise an AttributeError.
A Inmutable class can be used as a key in a dict or as an element in a set.
Hashing is cached for Inmutable classes. This means that if the hash of an Inmutable object is calculated, it will be stored in the __hash_cache__ slot of the object. This is done to avoid recalculating the hash of the object every time it is hashed.1
Example
>>> class Foo(Base, Inmutable): ... a: int ... b: int >>> foo = Foo(1, 2) >>> foo.a 1 >>> foo.a = 2 Traceback (most recent call last): ... AttributeError: Cannot set attribute a. Foo is readonly.
- protobase.traits.inmutable.is_inmutable(cls: type) bool
Check if a class is immutable.
- Parameters:
cls (type) – The class to check.
- Returns:
True if the class is immutable, False otherwise.
- Return type:
bool
Example
>>> is_inmutable(int) True >>> is_inmutable(str) True >>> is_inmutable(list) False >>> is_inmutable(dict) False
- protobase.traits.inmutable.know_inmutable(tp: type)
Adds the given type to the set of known immutable types. This can be used as a decorator.
- Parameters:
type (type) – The type to be added.
- Returns:
The added type.
- Return type:
type
Example
>>> @know_inmutable ... class Foo: ... pass >>> is_inmutable(Foo)
protobase.traits.repr module
- class protobase.traits.repr.Repr(*args, **kwargs)
Bases:
Trait,objectTrait that implements the __repr__ function in a class. This implementation of __repr__ uses the string representation of all field values of the object. .. rubric:: Example
>>> class Foo(Base, Repr): ... a: int ... b: int >>> foo = Foo(a=1, b=2) >>> foo Foo(a=1, b=2)