Metadata-Version: 2.4
Name: pystructlight
Version: 0.1.0
Summary: A lightweight, immutable struct library with type enforcement.
Author: Naphon jangjit
Project-URL: Homepage, https://github.com/NaphonJangjit/pystruct
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

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
``` bash
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.  

``` python
from pystructlight import Struct

# Create the blueprint
Book = Struct("Book", {
    "title": str,
    "author": str,
    "pages": int,
    "is_hardcover": bool
})
```
2. Create Instances

You can create instances using either keyword arguments or positional arguments.  
``` python
# 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)
```
3. Usage & Safety
``` python
# 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()

```
