A lightweight, immutable struct library with type enforcement.
Project description
A lightweight, memory-efficient, and type-safe immutable data structure for Python.
pystructlight provides a way to define schemas for your data objects and ensures that instances adhere to those types at runtime, all while maintaining a tiny memory footprint.
Features
Immutable: Once a struct instance is created, its attributes cannot be modified.
Runtime Type Enforcement: Automatically validates that the data provided matches your schema.
Memory Efficient: Leverages Python's __slots__ to prevent the overhead of instance dictionaries.
Flexible Initialization: Supports both positional and keyword arguments for creating instances.
Dictionary Support: Easily convert your structures back to standard Python dictionaries.
Installation
pip install pystructlight
Quick Start
- Define a Struct
Define a new data type by providing a name and a dictionary mapping field names to their expected types.
from pystructlight import Struct
# Create the blueprint
Book = Struct("Book", {
"title": str,
"author": str,
"pages": int,
"is_hardcover": bool
})
- Create Instances
You can create instances using either keyword arguments or positional arguments.
# Keyword mode
book1 = Book.new(
title="The Great Gatsby",
author="F. Scott Fitzgerald",
pages=180,
is_hardcover=False
)
# Positional mode
book2 = Book.new("1984", "George Orwell", 328, True)
- Usage & Safety
# Access fields via dot notation
print(book1.title) # Output: The Great Gatsby
# Instances are immutable
try:
book1.pages = 200
except AttributeError:
print("Cannot modify immutable struct!")
# Types are enforced at runtime
try:
Book.new("The Hobbit", "J.R.R. Tolkien", "many", True)
except TypeError as e:
print(e) # Field 'pages' must be int, got str
# Convert to dictionary
data = book1.to_dict()
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
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 pystructlight-0.1.0.tar.gz.
File metadata
- Download URL: pystructlight-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
56b9449253105e4e1c20ed08b58681103e158dc042c5b2adb2e5acca42dc6612
|
|
| MD5 |
a2620cb8d88f14fa21f071669e14480e
|
|
| BLAKE2b-256 |
cdaf40c87866706eac61cdea0ea62e7e83d544b10890a311a515aa657ed2cbcc
|
File details
Details for the file pystructlight-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pystructlight-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5fcfca5afa24a9cbb35d0458b3e711c493c955fdaaa86c8f2a9d1b4404cd877
|
|
| MD5 |
980b719f1843bb4b75e0a1a28685514d
|
|
| BLAKE2b-256 |
220b0486fdd8bb3e9f6a2cb787c47f089fbacf34fbdd74486a9505a33b6daaed
|