A powerful serialization framework for Python objects with automatic type registration and validation
Project description
Serilux ๐ฆ
Serilux is a powerful, flexible serialization framework for Python objects. With its intuitive API and automatic type registration, you can easily serialize and deserialize complex object hierarchies with minimal code.
โจ Why Serilux?
- ๐ฏ Simple API: Just inherit from
Serializableand you're ready to go - ๐ Automatic Type Registration: Classes are automatically registered for deserialization
- ๐ก๏ธ Type Safety: Built-in validation ensures objects can be properly deserialized
- ๐ณ Nested Objects: Automatically handles nested Serializable objects, lists, and dictionaries
- ๐ง Callable Serialization: Full support for serializing functions, methods, and lambda expressions
- ๐ Security: Strict mode prevents deserialization of unknown fields
- โก Zero Dependencies: Pure Python with no external dependencies
- ๐ Easy to Use: Minimal boilerplate, maximum flexibility
๐ฏ Perfect For
- Object Persistence: Save and restore complex object states
- Configuration Management: Serialize configuration objects to JSON/YAML
- Data Transfer: Convert objects to dictionaries for API communication
- State Management: Save application state for recovery
- Workflow Orchestration: Serialize workflow definitions and states
- Testing: Create test fixtures from serialized objects
๐ฆ Installation
Quick Install (Recommended)
pip install serilux
That's it! You're ready to go.
Development Install
For development with all dependencies:
pip install -e ".[dev]"
# Or using Makefile
make dev-install
๐ Quick Start
Create Your First Serializable Class in 3 Steps
Step 1: Define a Serializable Class
from serilux import Serializable, register_serializable
@register_serializable
class Person(Serializable):
def __init__(self):
super().__init__()
self.name = ""
self.age = 0
# Register fields to serialize
self.add_serializable_fields(["name", "age"])
Step 2: Create and Use Objects
# Create an object
person = Person()
person.name = "Alice"
person.age = 30
# Serialize to dictionary
data = person.serialize()
print(data)
# {'_type': 'Person', 'name': 'Alice', 'age': 30}
Step 3: Deserialize
# Deserialize from dictionary
new_person = Person()
new_person.deserialize(data)
print(new_person.name) # "Alice"
print(new_person.age) # 30
๐ Done! You've created your first serializable class.
๐ก Key Features
๐ Automatic Type Registration
Classes decorated with @register_serializable are automatically registered:
@register_serializable
class MyClass(Serializable):
def __init__(self):
super().__init__()
self.add_serializable_fields(["field1", "field2"])
Class Name Conflict Detection: Serilux automatically detects and prevents class name conflicts.
If you try to register a different class with the same name, a ValueError is raised to prevent
incorrect deserialization:
@register_serializable
class Processor(Serializable):
def __init__(self):
super().__init__()
self.name = ""
self.add_serializable_fields(["name"])
# This will raise ValueError: Class name conflict
@register_serializable
class Processor(Serializable): # Different class, same name
def __init__(self):
super().__init__()
self.value = 0
self.add_serializable_fields(["value"])
๐ณ Nested Objects
Automatically handles nested Serializable objects:
@register_serializable
class Address(Serializable):
def __init__(self):
super().__init__()
self.street = ""
self.city = ""
self.add_serializable_fields(["street", "city"])
@register_serializable
class Person(Serializable):
def __init__(self):
super().__init__()
self.name = ""
self.address = None
self.add_serializable_fields(["name", "address"])
# Create nested objects
person = Person()
person.name = "Alice"
person.address = Address()
person.address.street = "123 Main St"
person.address.city = "New York"
# Serialize - nested objects are automatically handled
data = person.serialize()
๐ Lists and Dictionaries
Handles lists and dictionaries containing Serializable objects:
@register_serializable
class Team(Serializable):
def __init__(self):
super().__init__()
self.name = ""
self.members = [] # List of Person objects
self.add_serializable_fields(["name", "members"])
team = Team()
team.name = "Engineering"
team.members = [person1, person2, person3]
# Serialize - list items are automatically serialized
data = team.serialize()
๐ง Callable Serialization
Serilux supports serializing and deserializing callable objects (functions, methods, lambda expressions):
from serilux import serialize_callable, deserialize_callable, serialize_callable_with_fallback
# Serialize a function
def process_data(data):
return data.upper()
serialized = serialize_callable(process_data)
restored = deserialize_callable(serialized)
result = restored("hello") # Returns "HELLO"
# Serialize lambda expression
condition = lambda x: x.get("priority") == "high"
serialized_lambda = serialize_callable_with_fallback(condition)
# Returns: {"_type": "lambda_expression", "expression": "x.get('priority') == 'high'"}
Callable fields in Serializable objects are automatically serialized:
@register_serializable
class Processor(Serializable):
def __init__(self):
super().__init__()
self.handler = None # Will store a function
self.add_serializable_fields(["handler"])
processor = Processor()
processor.handler = process_data # Function is automatically serialized
data = processor.serialize()
๐ Strict Mode
Enable strict mode to prevent deserialization of unknown fields:
# Strict mode raises error for unknown fields
try:
person.deserialize(data, strict=True)
except ValueError as e:
print(f"Error: {e}")
โ Validation
Validate that objects can be properly deserialized:
from serilux import validate_serializable_tree
# Validate before serialization
validate_serializable_tree(person)
๐ Documentation
๐ Full documentation available at: serilux.readthedocs.io
Documentation Highlights
- ๐ User Guide: Comprehensive guide covering all features
- ๐ง API Reference: Complete API documentation
- ๐ป Examples: Real-world code examples
Build Documentation Locally
pip install -e ".[docs]"
cd docs && make html
๐ Examples
Check out the examples/ directory for practical examples:
basic_usage.py- Your first serializable classadvanced_usage.py- Nested objects, lists, and dictionariescallable_serialization.py- Serializing functions, methods, and lambda expressions
Run examples:
python examples/basic_usage.py
๐๏ธ Project Structure
serilux/
โโโ serilux/ # Main package
โ โโโ __init__.py # Package initialization
โ โโโ serializable.py # Core serialization classes
โโโ tests/ # Comprehensive test suite
โโโ examples/ # Usage examples
โโโ docs/ # Sphinx documentation
๐งช Testing
Serilux comes with comprehensive tests:
# Run all tests
make test-all
# Run with coverage
make test-cov
# Run specific test suite
pytest tests/
๐ค Contributing
We welcome contributions! Here's how you can help:
- Star the project โญ - Show your support
- Report bugs ๐ - Help us improve
- Suggest features ๐ก - Share your ideas
- Submit PRs ๐ง - Contribute code
๐ License
Serilux is licensed under the Apache License 2.0. See LICENSE for details.
๐ Links
- ๐ฆ PyPI: pypi.org/project/serilux
- ๐ Documentation: serilux.readthedocs.io
- ๐ GitHub: github.com/lzjever/serilux
- ๐ง Issues: github.com/lzjever/serilux/issues
โญ Show Your Support
If Serilux helps you build amazing applications, consider giving it a star on GitHub!
Built with โค๏ธ by the Serilux Team
Making object serialization simple, powerful, and fun.
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 serilux-0.2.1.tar.gz.
File metadata
- Download URL: serilux-0.2.1.tar.gz
- Upload date:
- Size: 54.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
83fcc7207043978da344d02d96863db55c142af96f31bd3995359b4eecb9eb42
|
|
| MD5 |
c699dc7f24ba00a52ed31f7da8ba29d2
|
|
| BLAKE2b-256 |
4bfe42ae2b3b0599cdbc8bcbfbdf0a4142b135c91a9a46bdfeb1c8109996fd57
|
File details
Details for the file serilux-0.2.1-py3-none-any.whl.
File metadata
- Download URL: serilux-0.2.1-py3-none-any.whl
- Upload date:
- Size: 20.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03889565b71edabb6f47ab7157cc47a19d4c701f6b8b04ef391c29e7cf266260
|
|
| MD5 |
4dbd759270fb26b8a989a20803b5912a
|
|
| BLAKE2b-256 |
b827d5d4b675627fe7d1b46171e4f03753375ded44e54453a002b4715c514d60
|