-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
70 lines (50 loc) · 1.87 KB
/
example.py
File metadata and controls
70 lines (50 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
"""
Example usage of SimpleSpec package.
"""
from dataclasses import dataclass
from enum import Enum
from pydantic import BaseModel, Field
from simplespec import generate_simple_spec
class Status(str, Enum):
ACTIVE = "active"
INACTIVE = "inactive"
@dataclass
class Location:
latitude: float
longitude: float
class Address(BaseModel):
street: str = Field(min_length=1, max_length=100, description="Street address")
city: str = Field(min_length=1, max_length=50, description="City name")
country: str = Field(default="US", description="Country code")
location: Location | None = None
class User(BaseModel):
"""A user in the system."""
name: str = Field(min_length=1, max_length=100, description="Full name")
email: str = Field(description="Email address")
age: int = Field(ge=18, le=120, description="Age in years")
status: Status = Field(default=Status.ACTIVE, description="User status")
address: Address | None = None
tags: list[str] = Field(default_factory=list, description="User tags")
def main():
"""Demonstrate SimpleSpec functionality."""
print("SimpleSpec Example")
print("=" * 50)
# Generate spec for the User model
spec = generate_simple_spec(User, max_depth=10)
print(spec)
print("\n" + "=" * 50)
print("Programmatic access:")
print(f"Root type: {spec.self.type}")
print(f"Root description: {spec.self.description}")
print(f"Number of referenced types: {len(spec.refs)}")
print("\nReferenced types:")
for ref in spec.refs:
print(f"- {ref.type}: {len(ref.properties)} properties")
print("\nUser properties:")
for prop_name, prop in spec.self.properties.items():
print(f"- {prop_name}: {prop.type}")
if prop.description:
print(f" Description: {prop.description}")
if __name__ == "__main__":
main()