A package for all streamlining enthusiasts. Apply universal Decorators and Metaclasses to your codebase.
Project description
[!WARNING]
This project is a PoC. So, expect things to fail. In case you have remarks / questions I am happy to discuss them.
once - Simplify Your Codebase with Universal Decorators and Metaclasses
Welcome to once
, the ultimate Python package for streamlining your code! With once, you can define decorators and metaclasses in a single location and apply them globally to functions and classes. This powerful tool enhances code maintainability, readability, and efficiency, making your development process smoother and more productive.
Features
- Apply decorators to all functions, methods, and classes automatically.
- Enforce coding guidelines consistently across large codebases.
- Simple integration with existing projects.
- Flexible and customizable to fit various coding standards.
Installation
To install the once
package, simply run:
pip install once
Usage
Use once
at the entrypoint of your application. The example showcases how you can enforce coding guidelines globally.
The example-code can be found here. The example is a modification of an example from ArjanCodes.
Modify Functions:
With once
you can define custom decorators and apply them to all functions in your codebase.
import once
import logging
from coding_guidelines import log_exceptions # <- This is a decorator
from some_custom_package import my_module
def main():
result = my_module.do_stuff_1()
my_module.do_stuff_2(result)
if __name__ == "__main__":
(
once.and_for_all.Functions()
.apply_decorator(log_exceptions)
)
main()
Modify classes:
You can also apply decorators to classes or their methods. In addition you can make use of metaclasses.
import once
from coding_guidelines import (
log_exceptions, # <- This is a decorator
CheckMethodNames, # <- This is a metaclass
)
from some_custom_package import my_module
def main():
result_handler = my_module.ResultHandler()
result_1 = my_module.do_stuff_1()
result_2 = my_module.do_stuff_2(result_1)
result_handler.handle_results(result_1, result_2)
if __name__ == "__main__":
(
once.and_for_all.Functions()
.apply_decorator(log_exceptions)
)
(
once.and_for_all.Classes()
.and_only_their_methods()
.apply_decorator(log_exceptions) # <- apply the decorator only to the methods and not the class itself
)
(
once.and_for_all.Classes()
.apply_metaclass(CheckMethodNames)
)
main()
Configuration
You can exclude specific functions and classes from the application of a decorator / metaclass. In addition you can limit the application to a given package.
import once
from coding_guidelines import (
log_exceptions, # <- This is a decorator
CheckMethodNames, # <- This is a metaclass
)
from some_custom_package import my_module_1, my_module_2
def main():
result_handler = my_module_1.ResultHandler()
result_publisher = my_module_2.ResultPublisher()
result_1 = my_module_1.do_stuff_1()
result_2 = my_module_1.do_stuff_2(result)
combined_results = result_handler.handle_results(result_1, result_2)
result_publisher.publish_results(combined_result)
if __name__ == "__main__":
(
once.and_for_all
.Functions()
.exclude(names=["do_stuff_2"]) # <- Don't apply the decorator to function `do_stuff_2`
.apply_decorator(log_exceptions)
)
(
once.and_for_all
.Classes()
.in_module(my_module_1) # <- only apply the metaclass `CheckMethodNames` to classes in `my_module_1`
.apply_metaclass(CheckMethodNames)
)
main()
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
File details
Details for the file once_and_for_all-0.1.0.tar.gz
.
File metadata
- Download URL: once_and_for_all-0.1.0.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.9.13 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d54c67d7512c3025668b133b6897670e4a724d19e13ed35995140ee0bfbe9cea |
|
MD5 | b26c7e855d8d7f4214f7eeeb5401b4e8 |
|
BLAKE2b-256 | e15e183e130f9782f44a14368b7eb547a3161bb67b4257463536ce81ef8f42e5 |
File details
Details for the file once_and_for_all-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: once_and_for_all-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.9.13 Darwin/23.5.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | be080343e6b8cff79376d91c5905e23f442408956c1ff57e7fa0f3bd139ec167 |
|
MD5 | 53c881b10c6c517a54e18b218a477ea3 |
|
BLAKE2b-256 | 13c792494fe968110c5267f4962bbe9a44830851fd5d08611d75e5bb12ef9eb8 |