Skip to main content

Python decorators for str/repr, equality, immutability, and more.

Project description

Decorate-me

Version status License Python version compatibility Version on Docker Hub Version on Github Version on PyPi
Build (Actions) Documentation status Coverage (coveralls) Maintainability (Code Climate) Scrutinizer Code Quality

Python decorators to trim boilerplate.

Example 1: auto-add repr, str, hash, and eq

import decorateme as abcd

@abcd.auto_obj()
class Animal:
    def __init__(self, species: str, age: int):
        self.species = species
        self.age = age

luna1 = Animal("dog", 12)
luna2 = Animal("dog", 12)
str(luna)  # "Animal(species="dog", age=12)"
assert luna1 == luna2
assert hash(luna1) == hash(luna2)

Example 2: Customize

import decorateme as abcd

@abcd.auto_str(only=["species"])
class Animal:
    ...

Example 3: Add warnings about code quality

import decorateme as abcd


@abcd._status("immature")
def my_function():
  ...


my_function()  # warns with an ImmatureWarning

Example 4: Iterate, etc.

import decorateme as abcd
@abcd.sequence_over("members")
class Team:
    def __init__(self, members: list):
        self.members = members

team = Team(["john", "kerri"])
list(team)  # ["john", "kerri"]

@abcd.float_type("v")
class Size:
    def __init__(self, v: float):
        self.v = v
      
float(Size(6.2))  # 6.2

Example 5: clearly document behavior

import decorateme as abcd

@abcd.not_thread_safe
def run_command():
    ...

Example 6: combine things

import decorateme as abcd

@abcd.auto_obj()
@abcd.float_type("weight")
class Uno:
    def __init__(self, weight):
        self.weight = weight

light1, light2, heavy = Uno(3.1), Uno(3.1), Uno(12.8)
assert light1 == light2 != heavy
print(light1)  # 'Duo(weight=22.3)'
assert light1 * heavy == 39.68

Licensed under the Apache License, version 2.0. New issues and pull requests are welcome. Please refer to the contributing guide. Generated with Tyrannosaurus.

List of decorators

String-like methods

  • auto_repr_str
  • auto_str
  • auto_repr
  • auto_html (for display in Jupyter)
  • auto_info (add a .info method)

Equality

  • auto_eq
  • auto_hash
  • total_ordering (from functools)

Make your class smart

  • auto_obj (auto- for eq, str, and repr)
  • dataclass (from dataclasses)

Docstring-related

  • copy_docstring
  • append_docstring

Timing

  • takes_seconds
  • takes_seconds_named
  • auto_timeout

Allow a class to be used as a type

  • iterable_over
  • collection_over
  • sequence_over
  • float_type
  • int_type

Overriding / inheritance

  • final
  • overrides
  • override_recommended
  • ABC (from abc)
  • ABCMeta (from abc)
  • abstractmethod (from abc)

Mark purpose / use

  • internal
  • external
  • reserved

Multithreading

  • thread_safe
  • not_thread_safe

Mutability

  • mutable
  • immutable

Code maturity

  • status (code deprecation & immaturity warnings)

Singletons

  • auto_singleton

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

decorateme-0.3.0.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

decorateme-0.3.0-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file decorateme-0.3.0.tar.gz.

File metadata

  • Download URL: decorateme-0.3.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.13 Linux/5.15.0-1014-azure

File hashes

Hashes for decorateme-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7d71c5f74539a5e4edf81f4fe7a4f5de46375021680d6936636a58cd0632c410
MD5 a7b51f75ee6bf0c2d81de471ce4b7b9f
BLAKE2b-256 9586adb8713f425299a7abb245bfefc3b1bef296d2ca3b7bc223055668e0d784

See more details on using hashes here.

File details

Details for the file decorateme-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: decorateme-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.14 CPython/3.9.13 Linux/5.15.0-1014-azure

File hashes

Hashes for decorateme-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4b647ec83245e670558e1d7ca03d2a81d460f964fd3bb0a6430cfe721daa783b
MD5 d2043a87dc75968e40ed766bed636c22
BLAKE2b-256 2a730db629f5891edc78ae3eba045eb600e38f7a98d3297f9ea043b53070bcfb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page