protobase package

Subpackages

Submodules

protobase.annotations module

protobase.core module

class protobase.core.AttrInfo(name, type, default, annotations)

Bases: NamedTuple

annotations: dict[str, Any]

Alias for field number 3

default: Any

Alias for field number 2

name: str

Alias for field number 0

type: type

Alias for field number 1

class protobase.core.Object(*args, **kwargs)

Bases: Trait, object

Base class for all protobase classes.

class protobase.core.ObjectMeta(name: str, bases: tuple[type, ...], namespace: dict[str, Any], **kwargs)

Bases: type

Metaclass for protobase classes.

This metaclass is responsible for: - Generating the __slots__ attribute of the class. - Generating the __kwdefaults__ attribute of the class.

NOTE: The default object instantiation protocol is overriden in protobase classes. The __new__ function is responsible for calling the __init__ function. This behaviour is choosen to allow Consign objects to be pickleable.

class protobase.core.Trait(*args, **kwargs)

Bases: object

Base class for all traits. Traits are classes whose instances are meant to be used as mixins for other classes.

protobase.core.fields_of(cls: type[protobase.core.Object]) mappingproxy[str, Any]

Get the fields of a protobase class or object.

protobase.core.is_trait(cls: type[protobase.core.Object]) bool
class protobase.core.proto_method

Bases: object

protobase.core.protobase()
class protobase.core.trait_method(trait_fn: Callable[[Args], RType] = None)

Bases: Generic[Args, RType]

Example: >>> class MyTrait(proto.Trait): … @trait_method … def my_meth(self, x: int) -> str: … … >>> @MyTrait.my_meth.implementer() … async def _my_meth_impl(cls: type[MyTrait]): … fields = attrs(cls).keys() … return compile_function( … ‘my_meth’, … ‘def my_meth(self): return “HelloWorld”’ … )

Parameters:
  • proto_fn (Callable[Args, RType], optional) – The prototype

  • method. (function to be associated with the) –

impl(self) Callable[[Callable[[type[Base]]], Callable[Args, RType]]]
Decorator for setting the implementation function.
implementer(implementer_fn: Callable[[type[protobase.core.Object]], Callable[[Args], RType]]) Callable

protobase.settings module

protobase.utils module

protobase.utils.can_import(module: str) bool
protobase.utils.compile_function(*source, locals: dict[str, Any] | None = None, globals: dict[str, Any] | None = None, **kwargs) Callable

Compile a function from source code.

Parameters:
  • name (str) – The name of the function.

  • source – The source code of the function.

  • locals (dict[str, Any] | None, optional) – Local variables to be used during execution. Defaults to None.

  • globals (dict[str, Any] | None, optional) – Global variables to be used during execution. Defaults to None.

  • kwargs – Additional keyword arguments to be set as attributes of the compiled function.

Returns:

The compiled function.

Return type:

Callable

Example

>>> fn = compile_function(
...     "def foo(x: int, y: int) -> int:",
...     "    return x + y",
... )
>>> fn(1, 2)
3
>>> fn.__source__
def foo(x: int, y: int) -> int:

return x + y

protobase.utils.mro_of_bases(bases: Sequence[type])
protobase.utils.slots_of(cls: type)

Returns a tuple of all the slots defined in the class and its base classes.

Parameters:

cls (type) – The class to retrieve slots from.

Returns:

A tuple containing all the slots defined in the class and its base classes.

Return type:

tuple

Example

>>> class Base:
...     __slots__ = ("a", "b")
>>> class Sub(Base):
...     __slots__ = ("c", "d")
>>> slots_of(Sub)
('c', 'd', 'a', 'b')
protobase.utils.try_import(module: str) module

Module contents