Skip to main content

A lightweight Python utility for defining non-instantiable, attribute-frozen, privacy-aware namespace classes.

Project description

nomens

A lightweight utility for representing "namespaces" in Python. By inheriting from the namespace class, you can easily create classes that are non-instantiable, support attribute freezing, and can hide private attributes from normal access.

Features

  • Prevents instantiation: Calling a class that inherits from namespace with () raises a TypeError, so instances cannot be created. It is intended to be used purely as a "namespace" for grouping constants and functions.
  • Attribute freezing (frozen): By default, attributes cannot be added, edited, or deleted after the class is defined. This prevents accidental modification of the namespace's contents.
  • Partial unfreezing: Attributes specified in unfrozen_attrs can still be modified even while frozen.
  • Private attributes: Attributes specified in priv_attrs are hidden from normal attribute access (an AttributeError is raised instead).
  • Temporary unfreezing: Using the with namespace.unfrozen(): context manager, you can make attributes editable only within a specific scope.
  • Duplicate definition detection: Defining an attribute with the same name twice within the class body raises a ValueError.

Basic usage

from nomens import namespace

# Since it represents a namespace, snake_case is recommended for its name.
class config(namespace):
    DEBUG = False
    VERSION = "1.0.0"

# Normal reads are fine
print(config.VERSION)  # "1.0.0"

# Since it's frozen, modification raises an error
config.DEBUG = True  # AttributeError: Cannot assign to DEBUG in namespace config

# Instantiation also raises an error
config()  # TypeError: namespace config is not intended to be instantiated.

Class arguments

When inheriting from namespace, you can customize behavior with the following class arguments:

class config(namespace, frozen=True, unfrozen_attrs=["CACHE"], priv_attrs=["_secret"]):
    DEBUG = False
    CACHE = {}
    _secret = "hidden"
Argument Type Default Description
frozen bool True Whether to prohibit adding, editing, and deleting attributes.
unfrozen_attrs Sequence[str] | None None A list of attribute names that remain editable even when frozen=True.
priv_attrs Sequence[str] | None None A list of attribute names to hide from normal attribute access.

Note: namespace must be inherited from directly (multi-level inheritance is not allowed). Attempting multi-level inheritance raises a TypeError.

Temporarily unfreezing

By combining the unfrozen() class method with a with statement, you can allow attribute editing only within a specific scope. Upon exiting the with block, the original frozen state is automatically restored.

class config(namespace):
    COUNT = 0

# Temporarily unfreeze all attributes
with config.unfrozen():
    config.COUNT += 1

# Temporarily unfreeze only the specified attribute
with config.unfrozen("COUNT"):
    config.COUNT += 1

Private attributes

Attributes specified in priv_attrs raise an AttributeError on normal access.

class config(namespace, priv_attrs=["_secret"]):
    _secret = "hidden"

config._secret  # AttributeError: config has no public attribute _secret.

Class methods

Method Description
is_frozen() Returns whether the namespace is currently frozen, as a bool.
unfrozen_attrs() Returns the list of attribute names that remain operable while frozen, as a list[str].
private_attrs() Returns the list of attribute names hidden as private attributes.
unfrozen(*args) A context manager for temporarily unfreezing.

Intended use cases

  • Grouping configuration values or constants while preventing them from being accidentally modified
  • Grouping global utility functions in a form that doesn't require instantiation
  • Allowing only certain values to be updated at runtime while keeping everything else immutable

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

nomens-1.1.0.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

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

nomens-1.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file nomens-1.1.0.tar.gz.

File metadata

  • Download URL: nomens-1.1.0.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for nomens-1.1.0.tar.gz
Algorithm Hash digest
SHA256 3ba28192d67108283582cb8e43edc9c72785ede9ecfb1981777c9bc2579957ca
MD5 5838e37df263ff32b64ddab41d792fd6
BLAKE2b-256 5ae0864b39a02d78989da7652cadc228eb33f92f50d20a5bd60ec330e9f93d55

See more details on using hashes here.

File details

Details for the file nomens-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: nomens-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for nomens-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8b413a3e5b001da9597818a24d8184d596df4cad04d2f300f2a4128fbdbd0860
MD5 acf3b4f3f0a7867022b5381b3685eaf4
BLAKE2b-256 b6e6f702eabc6a5be7b149c67123c627d7ac7e827b973436caf381ff04bed8c2

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