Skip to main content

Immutable constant namespaces with dataclass-like ergonomics

Project description

classicle

Provides immutable constant namespaces with dataclass-like ergonomics. See the Python discussion thread for context, motivation, and alternative approaches.

Example:

from classicle import FrozenSpace

class Config(FrozenSpace):
    HOST = "localhost"
    PORT: int = 8080  # add type hints only when you want them
    DEBUG = True


print(Config.HOST)  # "localhost"; Type checker knows this is a str
print(Config.PORT)  # 8080
config_dict = dict(Config)
# {'HOST': 'localhost', 'PORT': 8080, 'DEBUG': True}

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

Features

  • Type checker friendly
  • No instantiation needed: The class itself is the namespace (cannot be instantiated)
  • Immutable: Attributes cannot be modified (at the top level) or deleted after creation
  • Mapping protocol: Supports dict(), len(), in, iteration, .items(), .keys(), .values()

Immutability

class Constants(FrozenSpace):
    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 namespace"

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

# Cannot instantiate FrozenSpace classes
try:
    instance = Constants()
except TypeError as e:
    print(e)  # "Cannot instantiate Constants..."

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.2.1.tar.gz (21.0 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.2.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for classicle-0.2.1.tar.gz
Algorithm Hash digest
SHA256 92293abce5b1b2bba6600d488cee01134736a0f1514a8254aeb6f91310ac2b95
MD5 552b55c554945aa48a7e506ee0629c20
BLAKE2b-256 63c9d9f9ad2fd647f9d947c61975417c3ed7878c03c5a5c3acf70cdef1f39e7f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for classicle-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1f4b1e672b959ef97231053b1866d26a57be9a608166f9bb325964f75ac1f4c6
MD5 d94b4e1b596d62db8f927de677ef9ac4
BLAKE2b-256 47d0f6bf9cb88229181ae6fdf813582b67de9c4b984f20a623e7e2bb2cb656c0

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