Skip to main content

A simple package that allows having class-level properties and validator for typical configuration classes.

Project description

Class utilities

Author: David Salac https://github.com/david-salac

Simple package with helpful utilities for classes. That includes class level properties and filters for configuration classes.

Note: this package does not require any external dependencies.

How to install package

Use PIP command:

pip install classutilities

How to use class level properties

Class level properties are properties defined on class level. They behave exactly the same as normal properties (but allow being called on class level and not only on instance level).

Consider the following example (defining class level properties):

import classutilities

class SomeClass(classutilities.ClassPropertiesMixin):
    _some_variable = 8  # Some class variable

    @classutilities.classproperty
    def some_variable(cls):  # Example of getter
        return cls._some_variable

    @some_variable.setter
    def some_variable(cls, value):  # Example of setter
        cls._some_variable = value

ClassPropertiesMixin allows you to use a setter for properties. However, if you only need a getter, it will work even without this mixin.

Usage of class-level properties:

# Getter:
value = SomeClass.some_variable
print(value)  # >>> 8

# Setter:
new_value = 9
SomeClass.some_variable = new_value
print(SomeClass.some_variable)  # >>> 9
print(SomeClass._some_variable)  # >>> 9

As you can see, class-level properties behave very naturally.

Configuration classes

Configuration classes usually follow standard patterns, mainly:

  • All members have to be upper case (if they do not start with an underscore).
  • There is no constructor in the class.
  • There is no standard (instance) method in the class. Only class methods and static methods are allowed.

It is beneficial to have some validator for a configuration class that checks if all these conditions are followed. This is precisely what ConfigClassMixin does. Consider the following use-case:

import classutilities

class SomeConfigClass(classutilities.ConfigClassMixin):
    # This is OK:
    DATABASE_HOST = "localhost"  # OK: variable name in uppercase
    DATABASE_NAME = "testing"  # OK: variable name in uppercase
    # ...
    
    # This is NOT OK (class variables must be uppercase):
    database_password = "pass"  # NO: must not be lowercase
    
    # This is OK (underscore on the beginning):
    _api_version = "1.0.0"  # OK: starts with an underscore
    
    # This is OK:
    @classmethod
    def connect_to_database(cls):  # Class methods are acceptable
        return ...
    
    # This is OK:
    @staticmethod
    def check_status():  # Static methods are acceptable
        return ...
    
    # This is NOT OK:
    def check_parameters(self):  # No instance-level methods
        return ...

Mixin ConfigClassMixin can be used together with mixin for class-level properties. Class level properties are also acceptable (this filter allows them).

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

classutilities-0.2.1.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classutilities-0.2.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file classutilities-0.2.1.tar.gz.

File metadata

  • Download URL: classutilities-0.2.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for classutilities-0.2.1.tar.gz
Algorithm Hash digest
SHA256 bd9863cac66af7e729f706e4afbce11028080208e2af58d1630f101d19c31797
MD5 75ba8a060e42256544d93d1038c8ec6c
BLAKE2b-256 120013f9efddb2aff86ed2538116398908605dbd9753d91cf864822ac9148179

See more details on using hashes here.

File details

Details for the file classutilities-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: classutilities-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for classutilities-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89de1c6467ed9a1569f59f7ddcaa858362a88519a58e98ad12c273116fba5990
MD5 3d4c790adb72027b3d84de8187604eb2
BLAKE2b-256 29683b6167e6dd006a628ce06a0a86502de1d70bf93e64fa93effa0b6548ab98

See more details on using hashes here.

Supported by

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