Skip to main content

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()

Metadata

Release files for once-and-for-all 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for once-and-for-all 0.1.0
File Size Uploaded
once_and_for_all-0.1.0.tar.gz 3.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for once-and-for-all 0.1.0
File Interpreter ABI Platform
once_and_for_all-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / once_and_for_all-0.1.0.tar.gz

Download URL once_and_for_all-0.1.0.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
d54c67d7512c3025668b133b6897670e4a724d19e13ed35995140ee0bfbe9cea
BLAKE2b-256 checksum
How to use checksums
e15e183e130f9782f44a14368b7eb547a3161bb67b4257463536ce81ef8f42e5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.9.13 Darwin/23.5.0

Release files / once_and_for_all-0.1.0-py3-none-any.whl

Download URL once_and_for_all-0.1.0-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
be080343e6b8cff79376d91c5905e23f442408956c1ff57e7fa0f3bd139ec167
BLAKE2b-256 checksum
How to use checksums
13c792494fe968110c5267f4962bbe9a44830851fd5d08611d75e5bb12ef9eb8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.6.1 CPython/3.9.13 Darwin/23.5.0

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page