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.2.tar.gz (21.9 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.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for classicle-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7e439164c8a644871c129cac5d2eaaddc4690fcf76a0ec54b48361dfee87db77
MD5 70a67e0f71df0786e0e4918bd34aecf4
BLAKE2b-256 98017d22a824036fa26f740a63c5b2af2337d9985de8e7fd6a30343c855acecd

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for classicle-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 8685bf857d03a455df72f8ddbc482c5ee2dcf2b2e0b1eaeb89336a862f8fabad
MD5 e3778b88a2d8ad4f4f0d773d4d145e3b
BLAKE2b-256 77994885bf560088cf36493ce8676bf71146e179d92f41ecbf48dc76f50a430b

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