pyTooling.Decorators
Decorators controlling visibility of entities in a Python module.
Hint
See high-level help for explanations and usage examples.
Variables
Functions
export(): Register the given function or class as publicly accessible in a module.notimplemented(): Mark a method as not implemented and replace the implementation with a new method raising aNotImplementedError.InheritDocString(): Copy the doc-string from given base-class to the method this decorator is applied to.
Classes
readonly: Marks a property as read-only.
Variables
- pyTooling.Decorators.Param
A parameter specification for function or method
~Param
- pyTooling.Decorators.RetType
Type variable for a return type
~RetType
- pyTooling.Decorators.Func
Type specification for a function
typing.Callable[~Param, ~RetType]
- pyTooling.Decorators.T
A type variable for a classes or functions.
~T
Functions
- pyTooling.Decorators.export(entity)[source]
Register the given function or class as publicly accessible in a module.
Creates or updates the
__all__attribute in the module in which the decorated entity is defined to include the name of the decorated entity.to_export.pyanother_file.pyfrom pyTooling.Decorators import export @export def exported(): pass def not_exported(): pass
from .to_export import * # 'exported' will be listed in __all__ assert "exported" in globals() # 'not_exported' won't be listed in __all__ assert "not_exported" not in globals()
- Parameters:
entity (
TypeVar(T, bound=Union[Type,FunctionType])) – The function or class to include in __all__.- Return type:
TypeVar(T, bound=Union[Type,FunctionType])- Returns:
The unmodified function or class.
- Raises:
AttributeError – If parameter
entityhas no__module__member.TypeError – If parameter
entityis not a top-level entity in a module.TypeError – If parameter
entityhas no__name__.
- pyTooling.Decorators.notimplemented(message)[source]
Mark a method as not implemented and replace the implementation with a new method raising a
NotImplementedError.The original method is stored in
<method>.__wrapped__and it’s doc-string is copied to the replacing method. In additional the field<method>.__notImplemented__is added.example.pyclass Data: @notimplemented def method(self) -> bool: '''This method needs to be implemented''' return True
- Parameters:
method – Method that is marked as not implemented.
message (str)
- Return type:
- Returns:
Replacement method, which raises a
NotImplementedError.
See also
- pyTooling.Decorators.InheritDocString(baseClass, merge=False)[source]
Copy the doc-string from given base-class to the method this decorator is applied to.
example.pyfrom pyTooling.Decorators import InheritDocString class Class1: def method(self): '''Method's doc-string.''' class Class2(Class1): @InheritDocString(Class1) def method(self): super().method()
Classes
- class pyTooling.Decorators.readonly[source]
Marks a property as read-only.
The doc-string is taken from the getter-method, like
propertydoes.A plain
propertyhands out<property>.setterand<property>.deleter, so a property declared as read-only could be made writable again further down the class body. Both methods therefore raise anAttributeErrorinstead.See also
propertyA decorator to convert getter, setter and deleter methods into a property applying the descriptor protocol.
Inheritance
- setter(fset)[source]
Reject attaching a setter to a read-only property.
- Parameters:
fset (
Callable) – The setter-method that was to be attached.- Raises:
AttributeError – Always, because a read-only property can’t have a setter.
- Return type:
- deleter(fdel)[source]
Reject attaching a deleter to a read-only property.
- Parameters:
fdel (
Callable) – The deleter-method that was to be attached.- Raises:
AttributeError – Always, because a read-only property can’t have a deleter.
- Return type:
- __delete__(instance, /)
Delete an attribute of instance.
- __get__(instance, owner=None, /)
Return an attribute of instance, which is of type owner.
- __getattribute__(name, /)
Return getattr(self, name).
- __init__(*args, **kwargs)
- classmethod __new__(*args, **kwargs)
- __set__(instance, value, /)
Set an attribute of instance to value.
- __set_name__(owner, name, /)
Method to set name of a property.
- getter(object, /)
Descriptor to obtain a copy of the property with a different getter.