Foundational class factories and decorators.
Project description
🏭 A Python library package which provides foundational class factories and decorators for providing classes with attributes immutability and concealment and other custom behaviors.
Key Features ⭐
🔧 Class Customization: Composable decorators and metaclasses providing classes with customized behaviors.
🔒 Attribute Immutability: Standard collection of metaclasses and decorators to provide immutability for class and instance attributes, with support for selective mutability via names, regexes, or predicates.
👁️ Attribute Concealment: Standard collection of metaclasses and decorators to provide concealment for class and instance attributes, with support for selective visibility via names, regexes, or predicates.
📚 Dataclass Integration: Decorators like dataclass_with_standard_behaviors make standard Python dataclasses with immutability and concealment. (Unlike dataclass( frozen = True ), attributes can still be manipulated via __post_init__ when one of these decorators is applied.)
🧩 Protocol Support: Immutable protocol classes for nominal and structural subtyping, compatible with typing.Protocol. Immutability and concealment are inherited by subclassed implementations of these protocols.
🗂️ Module Reclassification: Apply immutability and concealment to entire modules.
Installation 📦
Method: Install Python Package
Install via uv pip command:
uv pip install classcore
Or, install via pip:
pip install classcore
Note on Immutability 📢
Enforcement of immutability is quite difficult in Python. While this library encourages immutability by default, it can be circumvented by anyone who has intermediate knowledge of Python machinery and who is determined to circumvent the immutability. Use the library in the spirit of making programs safer, but understand that it cannot truly prevent unwanted state tampering.
Examples 💡
Please see the examples directory for greater detail.
Standard Behaviors 🔒
Produce classes which have immutable and concealed attributes and whose instances have immutable and concealed attributes. Can be normal classes, dataclasses, protocol classes, etc….
from dataclasses import field
from math import sqrt
from classcore.standard import DataclassObject
class Point2d( DataclassObject ):
x: float
y: float
hypotenuse: float = field( init = False )
def __post_init__( self ) -> None:
x, y = self.x, self.y
self.hypotenuse = sqrt( x*x + y*y )
Point2d.x = 3 # ❌ Error, immutable class attribute.
point = Point2d( x = 3, y = 4 )
point.x = 42 # ❌ Error, immutable instance attribute.
point.hypotenuse # Result: 5
dir( point ) # Result: ['hypotenuse', 'x', 'y']
Decorate classes so that their instances will have immutable and concealed attributes.
from dataclasses import field
from math import sqrt
from classcore.standard import dataclass_with_standard_behaviors
@dataclass_with_standard_behaviors( )
class Point2d:
x: float
y: float
hypotenuse: float = field( init = False )
def __post_init__( self ) -> None:
x, y = self.x, self.y
self.hypotenuse = sqrt( x*x + y*y )
point = Point2d( x = 5, y = 12 )
point.x = 42 # ❌ Error, immutable instance attribute.
point.hypotenuse # Result: 13
dir( point ) # Result: ['hypotenuse', 'x', 'y']
Module Reclassification 📦
Make modules (or entire packages) immutable to prevent direct modification of their attributes and to present only their public attributes via dir.
from classcore.standard import reclassify_modules
reclassify_modules( __name__, recursive = True )
Use Cases 🎯
📊 Data Transfer Objects: Ensure data integrity with immutable DTOs.
🏛️ API Interfaces: Define stable, well-controlled interfaces.
🧩 Plugin Systems: Plugin systems with controlled extension points.
📦 Frameworks: Frameworks with controlled extension and modification.
Contribution 🤝
Contribution to this project is welcome! However, it must follow the code of conduct for the project.
Please file bug reports and feature requests in the issue tracker or submit pull requests to improve the source code or documentation.
For development guidance and standards, please see the development guide.
Additional Indicia
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file classcore-1.10.tar.gz.
File metadata
- Download URL: classcore-1.10.tar.gz
- Upload date:
- Size: 27.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01f0b16bb3508cc08f03de1f25e3a6b94dcba43f625616cc0e3aa7860cf0f1e9
|
|
| MD5 |
49ebbceff8ead15f5c55e6d5913b2fe1
|
|
| BLAKE2b-256 |
fa99d290855e6f430f615eddb36c33e0c1b7dd8e01586210890c275cd3b4bcd5
|
Provenance
The following attestation bundles were made for classcore-1.10.tar.gz:
Publisher:
releaser.yaml on emcd/python-classcore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
classcore-1.10.tar.gz -
Subject digest:
01f0b16bb3508cc08f03de1f25e3a6b94dcba43f625616cc0e3aa7860cf0f1e9 - Sigstore transparency entry: 560616019
- Sigstore integration time:
-
Permalink:
emcd/python-classcore@65302824f632aac8a152633778c1d8ca329720ea -
Branch / Tag:
refs/tags/v1.10 - Owner: https://github.com/emcd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
releaser.yaml@65302824f632aac8a152633778c1d8ca329720ea -
Trigger Event:
push
-
Statement type:
File details
Details for the file classcore-1.10-py3-none-any.whl.
File metadata
- Download URL: classcore-1.10-py3-none-any.whl
- Upload date:
- Size: 38.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1efbd8e29bf2f177a12f5e54c64e748194efaef1a1115016ae82f96bead7ce7f
|
|
| MD5 |
836a32a8828555fa1ad6f04204180c35
|
|
| BLAKE2b-256 |
3e6de02c7188102c3cdd7767ef4b90256708c0b4266879c2e2a6763c6f456c75
|
Provenance
The following attestation bundles were made for classcore-1.10-py3-none-any.whl:
Publisher:
releaser.yaml on emcd/python-classcore
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
classcore-1.10-py3-none-any.whl -
Subject digest:
1efbd8e29bf2f177a12f5e54c64e748194efaef1a1115016ae82f96bead7ce7f - Sigstore transparency entry: 560616032
- Sigstore integration time:
-
Permalink:
emcd/python-classcore@65302824f632aac8a152633778c1d8ca329720ea -
Branch / Tag:
refs/tags/v1.10 - Owner: https://github.com/emcd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
releaser.yaml@65302824f632aac8a152633778c1d8ca329720ea -
Trigger Event:
push
-
Statement type: