latest pushes
This commit is contained in:
@@ -2,15 +2,95 @@
|
||||
|
||||
```
|
||||
from bellande_format import Bellande_Format
|
||||
from core.types import SchemaDefinition
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
bellande_formatter = Bellande_Format()
|
||||
# Initialize formatter
|
||||
formatter = Bellande_Format()
|
||||
|
||||
# Parse a Bellande file
|
||||
parsed_data = bellande_formatter.parse_bellande("path/to/your/file.bellande")
|
||||
# Example 1: Basic Usage
|
||||
data = {
|
||||
"name": "Project X",
|
||||
"version": 1.0,
|
||||
"created_at": datetime.now(),
|
||||
"settings": {
|
||||
"debug": True,
|
||||
"max_retries": 3
|
||||
},
|
||||
"users": [
|
||||
{"name": "John", "role": "admin"},
|
||||
{"name": "Jane", "role": "user"}
|
||||
]
|
||||
}
|
||||
|
||||
# Write data to a Bellande file
|
||||
data_to_write = {"key": "value", "list": [1, 2, 3]}
|
||||
bellande_formatter.write_bellande(data_to_write, "path/to/output/file.bellande")
|
||||
# Write data
|
||||
formatter.write_bellande(data, "config.bellande")
|
||||
|
||||
# Read data
|
||||
loaded_data = formatter.parse_bellande("config.bellande")
|
||||
|
||||
# Example 2: Schema Validation
|
||||
user_schema = SchemaDefinition(
|
||||
type="object",
|
||||
properties={
|
||||
"name": SchemaDefinition(type="string", pattern=r"^[a-zA-Z\s]+$"),
|
||||
"age": SchemaDefinition(type="integer", minimum=0, maximum=150),
|
||||
"email": SchemaDefinition(type="string", pattern=r"^[\w\.-]+@[\w\.-]+\.\w+$")
|
||||
},
|
||||
required=["name", "email"]
|
||||
)
|
||||
|
||||
formatter.register_schema("user", user_schema)
|
||||
|
||||
# Validate data
|
||||
user_data = {
|
||||
"name": "John Doe",
|
||||
"age": 30,
|
||||
"email": "john@example.com"
|
||||
}
|
||||
|
||||
result = formatter.validate(user_data, "user")
|
||||
print(f"Validation result: {result.is_valid}")
|
||||
|
||||
# Example 3: Encryption and Compression
|
||||
key = os.urandom(32) # Generate encryption key
|
||||
|
||||
# Encrypt data
|
||||
encrypted = formatter.encrypt(data, key)
|
||||
|
||||
# Decrypt data
|
||||
decrypted_data = formatter.decrypt(encrypted, key)
|
||||
|
||||
# Compress data
|
||||
compressed = formatter.compress(data)
|
||||
|
||||
# Decompress data
|
||||
decompressed_data = formatter.decompress(compressed)
|
||||
|
||||
# Example 4: Custom Types
|
||||
class Point2D:
|
||||
def __init__(self, x: float, y: float):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
# Register custom type
|
||||
formatter.type_registry.register(
|
||||
"point2d",
|
||||
Point2D,
|
||||
lambda p: f"{p.x},{p.y}",
|
||||
lambda s: Point2D(*map(float, s.split(',')))
|
||||
)
|
||||
|
||||
# Use custom type
|
||||
location_data = {
|
||||
"points": [
|
||||
Point2D(1.0, 2.0),
|
||||
Point2D(3.0, 4.0)
|
||||
]
|
||||
}
|
||||
|
||||
formatter.write_bellande(location_data, "locations.bellande")
|
||||
```
|
||||
|
||||
## Website PYPI
|
||||
|
Reference in New Issue
Block a user