.NET-like Attributes implemented as Python decorators.
Project description
pyAttributes
The Python package pyAttributes
offers implementations of .NET-like attributes
realized with Python decorators. The package also offers a mixin-class to ease
using classes having annotated methods.
In addition, an ArgParseAttributes
module is provided, which allows users to
describe complex argparse command-line argument parser structures in a declarative
way.
Attributes can create a complex class hierarchy. This helps in finding and filtering for annotated properties and user-defined data. These search operations can be called globally on the attribute classes or locally within an annotated class. Therefore the provided helper-mixin should be inherited.
Use Cases
Annotate properties and user-defined data to methods.
Derived use cases:
- Describe a command line argument parser (argparse).
See pyAttributes Documentation -> ArgParse Examples - Mark class members for documentation.
See SphinxExtensions -> DocumentMemberAttribute
Planned implementations:
- Annotate user-defined data to classes.
- Describe test cases and test suits to get a cleaner syntax for Python's unit tests.
Technique
The annotated data is stored in an additional __dict__
entry for each
annotated method. By default, the entry is called __pyattr__
. Multiple
attributes can be applied to the same method.
Creating new Attributes
Simple User-Defined Attribute
class SimpleAttribute(Attribute):
pass
User-Defined Attribute with Data
class DataAttribute(Attribute):
data: str = None
def __init__(self, data:str):
self.data = data
@property
def Data(self):
return self.data
Applying Attributes to Methods
class ProgramWithHelper(AttributeHelperMixin):
@SimpleAttribute()
def Method_1(self):
"""This method is marked as simple."""
@DataAttribute("hello world")
def Method_2(self):
"""This method as annotated data."""
Finding Methods with Attributes
Finding Methods with Global Search
methods = SimpleAttribute.GetMethods()
for method, attributes in methods.items():
print(method)
for attribute in attributes:
print(" ", attribute)
Finding Methods with Class-Wide Search
class ProgramWithHelper(AttributeHelperMixin):
@SimpleAttribute()
def Method_1(self):
"""This method is marked as simple."""
@DataAttribute("hello world")
def Method_2(self):
"""This method as annotated data."""
def test_GetMethods(self):
methods = self.GetMethods(filter=DataAttribute)
for method, attributes in methods.items():
print(method)
for attribute in attributes:
print(" ", attribute)
def test_GetAttributes(self):
attributes = self.GetAttributes(self.Method_1)
for attribute in attributes:
print(" ", attribute)
Contributors
- Patrick Lehmann (Maintainer)
- and more...
License
This Python package (source code) licensed under Apache License 2.0.
The accompanying documentation is licensed under Creative Commons - Attribution 4.0 (CC-BY 4.0).
SPDX-License-Identifier: Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pyAttributes-2.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8c0202948feae2e4d3f0b506e8790e55148987db01096fddd344b6eafd63f6e |
|
MD5 | 51f120fb5da508bbcdf539a74c40c299 |
|
BLAKE2b-256 | a06d6348b443775f5f93b6ca1e2a19b6dc277144df6d8a30fce8454585753fdf |