Skip to main content

Immutable constant namespaces with dataclass-like ergonomics

Project description

classicle

Immutable constant namespaces with dataclass-like ergonomics.

Usage

The frozen_instance decorator creates an immutable, already-instantiated class that behaves like a constant namespace. This is perfect for configuration objects, constants, and other immutable data structures.

Basic Example

from classicle import frozen_instance

@frozen_instance
class Config:
    HOST = "localhost"
    PORT = 8080
    DEBUG = True

# Access attributes
print(Config.HOST)  # "localhost"
print(Config.PORT)  # 8080

# Convert to dictionary
config_dict = dict(Config)
# {'HOST': 'localhost', 'PORT': 8080, 'DEBUG': True}

# Iterate over attributes
for name, value in Config:
    print(f"{name} = {value}")

With Type Hints

@frozen_instance
class DatabaseConfig:
    HOST: str = "localhost"
    PORT: int = 5432
    USERNAME: str = "admin"
    PASSWORD: str = "secret"
    DATABASE: str = "mydb"

# Type hints are preserved for static type checkers
print(DatabaseConfig.HOST)  # Type checker knows this is a str

Features

  • Already instantiated: No need to create an instance, the class itself is the instance
  • Immutable: Attributes cannot be modified or deleted after creation
  • Iterable: Works with dict(), can be used in for loops
  • Type hints: Supports optional type hints for better IDE support
  • Clean syntax: Similar to dataclasses but for constant values
  • No methods included: Only data attributes are exposed (methods are excluded)
  • No private attributes: Attributes starting with _ are not included

Immutability

@frozen_instance
class Constants:
    PI = 3.14159
    E = 2.71828

# This will raise an AttributeError
try:
    Constants.PI = 3.14
except AttributeError as e:
    print(e)  # "Cannot modify attribute 'PI' on frozen instance"

# This will also raise an AttributeError
try:
    del Constants.E
except AttributeError as e:
    print(e)  # "Cannot delete attribute 'E' on frozen instance"

Installation

Install classicle: We're on pypi, so uv add classicle. If working directly on this repo, consider using the simplest-possible virtual environment.

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

classicle-0.1.0.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

classicle-0.1.0-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file classicle-0.1.0.tar.gz.

File metadata

  • Download URL: classicle-0.1.0.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for classicle-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f451736188e46d7b729cee0b11858c372d524f0d10aa2657644f0eaa0300cd8d
MD5 7477801c345e6eac88942231172cb33c
BLAKE2b-256 1c51be6ff44096d623106a2656b9450bb9d9ab3e997e631dba10f4a3f5180135

See more details on using hashes here.

File details

Details for the file classicle-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: classicle-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for classicle-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25c5d1ec73c86fbc1f3bb2ea1767e5c1d47b30a971afa7cc0fb16af5f1ea40b1
MD5 21d4c077af75c1d01a383c318fd12e4a
BLAKE2b-256 2d55653dac6d7696af145ff6f892487804f1b15270ca793d385348d9b4fc2d6a

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