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.1.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.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pystructlight-0.1.1.tar.gz.

File metadata

  • Download URL: pystructlight-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 e7bbb952608479cabbbb2b90defb440b558e871b7ed83eaa920d8e2944f177d4
MD5 00b0aa8d2f9fc02c6da466f8a81a1563
BLAKE2b-256 7261d3c813ad6764c67d57f684cfb45d44772d704cd33f6dbf3354753c60fb2f

See more details on using hashes here.

File details

Details for the file pystructlight-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pystructlight-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0254e5b30fc2e74f511b73593c84141a702044605a7d631da6138a3cc114511a
MD5 4ea1cafcd4f4c062a398d5934caa2279
BLAKE2b-256 7f3e683dbe37fb535e2aa895d81c20b1a3aa320152ffeb1a1ec3089ed78221dd

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