luminal.interfaces.metadata
Module Contents
Classes
A decorator that turns a method into a class property. |
|
A metaclass that enables the ClassProperty decorator to work properly |
- class ClassProperty
Bases:
propertyA decorator that turns a method into a class property.
This class inherits from Python’s built-in
propertyclass and overrides its__get__()method to allow it to be used as a class property.Examples
To use
ClassProperty, simply decorate a method with it:>>> class MyClass: ... _prop = 'value' ... ... @ClassProperty ... def prop(cls): ... return cls._prop ... >>> MyClass.prop 'value'
- __get__(*args, **_)
Get the value of the class property for a given object or class.
- Returns:
The value of the class property.
- Return type:
- Raises:
AttributeError – If the property is accessed on an object that does not have an attribute with the same name as the property.
- Parameters:
args (tuple) –
- __set__(cls_or_instance, value)
Set the value of the class property.
This method is called when the class property is set on the class or an instance of the class. However, because this is a class property, this method will only be called when the property is set by the class itself, and is not intended for external use; or even by private methods.
- Parameters:
- Raises:
AttributeError – When attempting to set the class property on an instance of the class rather than the class itself.
- Return type:
None
- __delete__(cls_or_instance)
Delete the class property from the class.
- Parameters:
cls_or_instance (
object) – The class from which the class property is being deleted.- Raises:
AttributeError – When attempting to delete the class property from an instance of the class rather than the class itself.
- Return type:
None
- _get_class(cls_or_instance)
Get the class of an object, or return the class itself if given.
- Parameters:
- Returns:
type– The class of the object if one is provided, or the class itself if a class is provided.Raises
———-
TypeError – An exception if
cls_or_instanceis not a valid object or type.
- Return type:
- class MetaProperties
Bases:
typeA metaclass that enables the ClassProperty decorator to work properly with class-level operations like setting and deleting properties.
When using ClassProperty to create class properties, the containing class must use this metaclass in order for the property to work properly with class-level operations like setting and deleting the property.
For example, to use ClassProperty to define a read-only class property:
>>> class MyClass(metaclass=MetaProperties): ... @ClassProperty ... def prop(cls): ... return "value" ... >>> MyClass.prop # Get the value of the property. 'value' >>> MyClass.prop = "new value" # Attempt to set the value of the property. AttributeError: can't set attribute >>> del MyClass.prop # Attempt to delete the property. AttributeError: can't delete attribute
Note that attempting to set or delete a ClassProperty object will raise an
AttributeErrorby default, unless the__set__()and/or__delete__()methods are defined on theClassPropertyobject.See also
ClassPropertyThe decorator used to create class properties.
- __setattr__(name, value)
Override the
__setattr__()method to allow setting class properties.This method is called when an attribute of the class is set. If the attribute is a
ClassPropertyinstance (as determined by the presence of a__set__()method on the instance), it will call the__set__()method to set the property’s value. Otherwise, it will invoke the superclass implementation of__setattr__()to handle the attribute in the usual way.- Parameters:
- Raises:
AttributeError – If
namerefers to aClassPropertyobject and the__set__()method of that object raises anAttributeError.- Return type:
None
- __delattr__(name)
Override the
__delattr__()method to allow deleting of class properties.- Parameters:
name (
str) – The name of the attribute that’s being deleted.- Raises:
AttributeError – If
namerefers to aClassPropertyobject and the__delete__()method of that object raises anAttributeError.
Notes
This method is called when an attribute of the class is deleted. If the attribute is a
ClassPropertyinstance (as determined by the presence of a__delete__()method on the instance), it will call the__delete__()method to delete the property. Otherwise, it will invoke the superclass implementation of__delattr__()to handle the attribute in the usual way.
- _get_attribute(name)
Retrieves a
ClassPropertyattribute with the givennamefrom thisMetaPropertiesinstance.- Parameters:
name (
str) – The name of the attribute to retrieve.- Returns:
The
ClassPropertyobject for the attribute, if it exists. Otherwise, returnsNone.- Return type:
Optional[
ClassProperty]