Skip to main content

Python Class Utilities

pipeline status coverage report documentation PyPI

class-tools is a collection of utilities for Python classes that the author frequently needs.

What can I do with class-tools?

Quickly Add Properties to Classes

from class_tools.decorators import wrapper_property

@wrapper_property("name", default=lambda: "Bob", doc="The dog's name")
@wrapper_property("toy", default=lambda: "ball")
@wrapper_property("sharp_teeth", default=lambda: 4, type=int)
class Dog:
    pass

dog = Dog()
dog.__doc__
# "The dog's name"
dog.name
# 'Bob'
dog.toy
# 'ball'
dog.sharp_teeth = "4" # (set as a string value)
dog.sharp_teeth
# 4 (returned as int)

Add a Pretty __repr__ Method

from class_tools.decorators import *

@with_init_from_properties()
@with_repr_like_init_from_properties()
@wrapper_property("name")
@wrapper_property("furcolor")
class Cat:
    pass

cat = Cat(name="Lucy", furcolor="white")
cat
# Cat(
#     furcolor = 'white',
#     name = 'Lucy'
# )

Make Classes Comparable by Properties

from class_tools.decorators import *

@with_eq_comparing_properties()
@wrapper_property("name")
@wrapper_property("furcolor")
class Cat:
    pass

lucy = Cat()
lucy.name = "Lucy"
lucy.furcolor = "white"
lucy_clone = Cat()
lucy_clone.name = "Lucy"
lucy_clone.furcolor = "white"
gary = Cat()
gary.name = "Gary"
lucy == gary
# False
lucy == lucy_clone
# True

All of the Above

from class_tools.propertyobject import PropertyObject
from class_tools.decorators import wrapper_property

@wrapper_property("name")
@wrapper_property("height", type=float)
@wrapper_property("friends", type=list, default=list)
class Giraffe(PropertyObject):
		pass

matt = Giraffe(name="Matt")
matt.height = 3
gunther = Giraffe(name="Gunther")
gunther.friends.append(matt)
gunther
# Giraffe(
#     friends=[Giraffe(
#         friends=[],
#         height=3.0,
#         name='Matt'
#     )],
#     height=None,
#     name='Gunther'
# )

Installation

The class-tools package is best installed via pip. Run from anywhere:

python3 -m pip install --user class-tools

This downloads and installs the package from the Python Package Index.

You may also install class-tools from the repository root:

python3 -m pip install --user .

Documentation

Documentation of the class-tools package can be found here on GitLab.

Also, the command-line help page python3 -m class-tools -h is your friend.

Metadata

Release files for class-tools 0.2.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 class-tools 0.2.0
File Size Uploaded Provenance
class-tools-0.2.0.tar.gz 18.9 kB Without provenance Details

Release files / class-tools-0.2.0.tar.gz

Download URL class-tools-0.2.0.tar.gz
Size 18.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4039b5895ac8e9d2f0d1fd3641afbdac5017f72f800440db0faf87fd9bc29c58
BLAKE2b-256 checksum
How to use checksums
76e6d473d6898fc3a308f22f3b7504f26d55022da40a9b65156a246461306506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

Security for class-tools 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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