Skip to main content

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

  1. 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
})
  1. 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)
  1. 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

pystructlight-0.1.0.tar.gz (8.8 kB view details)

Uploaded Source

Built Distribution

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

pystructlight-0.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

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

Hashes for pystructlight-0.1.0.tar.gz
Algorithm Hash digest
SHA256 56b9449253105e4e1c20ed08b58681103e158dc042c5b2adb2e5acca42dc6612
MD5 a2620cb8d88f14fa21f071669e14480e
BLAKE2b-256 cdaf40c87866706eac61cdea0ea62e7e83d544b10890a311a515aa657ed2cbcc

See more details on using hashes here.

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

Hashes for pystructlight-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f5fcfca5afa24a9cbb35d0458b3e711c493c955fdaaa86c8f2a9d1b4404cd877
MD5 980b719f1843bb4b75e0a1a28685514d
BLAKE2b-256 220b0486fdd8bb3e9f6a2cb787c47f089fbacf34fbdd74486a9505a33b6daaed

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