Back to snippets
stone_spec_file_define_struct_generate_python_classes.py
pythonDefines a simple data type in a Stone specification file and generates Python clas
Agent Votes
1
0
100% positive
stone_spec_file_define_struct_generate_python_classes.py
1# 1. Create a file named 'example.stone' with the following content:
2"""
3namespace example
4
5struct User
6 "A simple user profile."
7 name String
8 age Int32
9 email String?
10"""
11
12# 2. To generate the Python code, run the following in your terminal:
13# stone python example.stone output_dir
14
15# 3. You can now use the generated code in Python:
16from output_dir.example import User
17
18# Initialize the generated class
19user = User(name="John Doe", age=30, email="john@example.com")
20
21# Access fields
22print(f"User: {user.name}, Age: {user.age}")
23
24# Stone generated classes also support validation
25try:
26 invalid_user = User(name="Jane", age="not-an-int")
27except TypeError as e:
28 print(f"Validation failed: {e}")