Skip to main content

Python class utilities

Project description

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:

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

Add a Pretty __repr__ Method

from class_tools.decorators import *

class Cat:

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

Make Classes Comparable by Properties

from class_tools.decorators import *

class Cat:

lucy = Cat() = "Lucy"
lucy.furcolor = "white"
lucy_clone = Cat() = "Lucy"
lucy_clone.furcolor = "white"
gary = Cat() = "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("height", type=float)
@wrapper_property("friends", type=list, default=list)
class Giraffe(PropertyObject):

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


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 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.

Project details

Download files

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

Files for class-tools, version 0.2.0
Filename, size File type Python version Upload date Hashes
Filename, size class-tools-0.2.0.tar.gz (18.9 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page