Package: base-descriptor
base-descriptorA simple, fast, typed, and tested abstract and base classes for a python3.6+
Non Data Descriptor, Data Descriptor, and Slottable Data Descriptor. The goal
is to
aid in the creation of descriptors allowing other developers to make
descriptors for their own use case.Key Features:Easy: Flexable and easy to inherit the prebuilt Non Data Descriptors,
Data Descriptors, and Slottable Data Descriptors to create your own
descriptors.Great Developer Experience: Being fully typed makes it great for editor
support.Fully Tested: Our test suit fully tests the functionality to ensure that
all of the classes in this module run as expected.Installationpip install base-descriptorTable of ContentsObjects Provided in this ModuleNon Data DescriptorsData DescriptorsSlottable Data DescriptorsNon Data Descriptor ExamplesAbstractNonDataDescriptorData Descriptor ExamplesReadOnlyDefaultDescriptorBaseDataDescriptorAbstractDataDescriptorSlottable Data DescriptorsSlottableDefaultDescriptorBaseSlottableDataDescriptorReferencesObjects Provided in this ModuleNon Data DescriptorsA non-data descriptor in Python is a type of descriptor that only implements
the__get__()method. Descriptors are a way to customize attribute access in
Python. When an attribute is
accessed on an object, Python checks if a descriptor exists for that attribute
in the class or its ancestors. If found, the descriptor's__get__()method is
called to determine the final
value of the attribute.ClassDescriptionAbstractNonDataDescriptorAbstract Base Class for creating Non Data Descriptors.Data DescriptorsA data descriptor in Python is a type of descriptor that implements
both__get__()and either__set__()or__delete__(). Data descriptors
allow you to define custom behavior for
attribute
access, including setting or deleting the attribute in addition to retrieving
its value.ClassDescriptionAbstractDataDescriptorAbstract Base Class for creating Data Descriptors.BaseDataDescriptorBase Class for creating Data Descriptors. Provides the same functionality as a standard attribute.DefaultDescriptorA Data Descriptor that has a default value.ReadOnlyA Data Descriptor that is read only.Slottable Data DescriptorsA Data Descriptor that plays well with__slots__. This module was inspired by
Dr. Fred Baptistefbaptiste.The slottable data descriptor has the following advantages:Has instance specific storageDoes not use the instance for storage, thus works withslots.__slots__ = "__weakref__"must be set.Handles non hashable instancesData storage is clean.ClassDescriptionAbstractSlottableDataDescriptorAbstract Base Class for creating Slottable Data Descriptors.BaseSlottableDataDescriptorBase Class for creating Slottable Data Descriptors. Provides the same functionality as a standard attribute.SlottableDefaultDescriptorA Slottable Data Descriptor that has a default value.Non Data Descriptor ExamplesAbstractNonDataDescriptorSimple way to Inherit fromAbstractNonDataDescriptorto create your own Non
Data Descriptor.frombase_descriptorimportAbstractNonDataDescriptorclassSquareDescriptor(AbstractNonDataDescriptor):def__get__(self,instance,owner=None):ifinstanceisNone:# Access through the class, return the descriptor itselfreturnselfreturninstance._value**2classMyClass:square=SquareDescriptor()def__init__(self,value):self._value=value# Create an instance of MyClassmy_instance=MyClass(5)# Access the square attribute using the descriptor# This will calculate and return the squareprint(my_instance.square)# 25Data Descriptor ExamplesReadOnlyfrombase_descriptorimportReadOnlyclassPerson:name=ReadOnly("Guido")person=Person()print(person.name)# Guidoperson.name="Raymond"# raises AttributeErrorDefaultDescriptorDefault Descriptor that provides a default value for the attribute.frombase_descriptorimportDefaultDescriptorclassPerson:name=DefaultDescriptor(default="Guido")print(Person.name)# <data_descriptor.DefaultDescriptor object at ...>person=Person()print(person.name)# Guidoperson.name="Raymond"print(person.name)# RaymondBaseDataDescriptorProvides the same functionality as a standard attribute.
It enables you to create your own Data Descriptor by overriding
the__set_name__(),__set__(),__get__(),
or__delete__()methods to match your requirements.frombase_descriptorimportBaseDataDescriptorclassPlus2(BaseDataDescriptor):def__set__(self,instance,value):value=value+2instance.__dict__[self._property_name]=valueclassFoo:bar=Plus2()foo=Foo()foo.bar=1print(foo.bar)# 3AbstractDataDescriptorProvides an Abstract Base Class that can be inherited from to help create your
own Data Descriptors.frombase_descriptorimportAbstractDataDescriptorclassMyDataDescriptor(AbstractDataDescriptor):def__init__(self):self._value=Nonedef__get__(self,instance,owner):returnself._valuedef__set__(self,instance,value):self._value=valuedef__delete__(self,instance):self._value=NoneclassMyClass:my_data_descriptor=MyDataDescriptor()obj=MyClass()obj.my_data_descriptor=1print(obj.my_data_descriptor)# 1Slottable Data DescriptorsSlottableDefaultDescriptorSlottable Default Descriptor that provides a default value for the attribute.frombase_descriptorimportSlottableDefaultDescriptorclassPerson:__slots__="__weakref__"first_name=SlottableDefaultDescriptor(default="Guido")person=Person()print(person.first_name)# Guidoperson.first_name="Raymond"print(person.first_name)# RaymondBaseSlottableDataDescriptorProvides the same functionality as a standard attribute.
It enables you to create your own Slottable Data Descriptor by overriding
the__set_name__(),__set__(),__get__(),
or__delete__()methods to match your requirements.frombase_descriptorimportBaseSlottableDataDescriptorclassMyDescriptor(BaseSlottableDataDescriptor):def__set__(self,instance,value):print(f"Setting{self._property_name}to{value}")instance.__dict__[self._property_name]=valueclassFoo:__slots__="__weakref__"bar=MyDescriptor()foo=Foo()foo.bar=1print(foo.bar)# 1ReferencesThis module was heavily inspired by the following resources:Python Deep Dive: Part 4Author: Dr. Fred Baptiste (fbaptiste)Year: 2023Course
Title:Python Deep Dive: Part 4Platform: UdemyDescriptor HowTo GuideSource: Python DocumentationURL:Descriptor HowTo Guide