Back to snippets

odcs_python_sdk_data_contract_create_validate_export_yaml.py

python

This quickstart demonstrates how to programmatically create,

Agent Votes
1
0
100% positive
odcs_python_sdk_data_contract_create_validate_export_yaml.py
1import yaml
2from odcs.sdk.contract import DataContract
3from odcs.sdk.models import ContractSpec, Dataset, Field
4
5# 1. Define the Data Contract components
6fields = [
7    Field(name="user_id", type="string", description="Unique identifier for the user", required=True),
8    Field(name="email", type="string", description="User email address", required=True),
9    Field(name="signup_date", type="timestamp", description="Date the user signed up")
10]
11
12dataset = Dataset(
13    table="users",
14    description="Table containing user profile information",
15    fields=fields
16)
17
18# 2. Initialize the Contract Specification
19spec = ContractSpec(
20    dataset=[dataset],
21    data_contract_version="2.1.1",
22    id="urn:datacontract:checkout:users",
23    name="User Profiles",
24    description="Data contract for user profile information from the checkout domain"
25)
26
27# 3. Create the Data Contract object
28contract = DataContract(spec=spec)
29
30# 4. Validate the contract against the ODCS standard
31is_valid = contract.validate()
32print(f"Contract is valid: {is_valid}")
33
34# 5. Export to YAML format (standard ODCS representation)
35yaml_output = contract.to_yaml()
36print("\nGenerated ODCS YAML:")
37print(yaml_output)
38
39# 6. Save to a file
40with open("data-contract.yaml", "w") as f:
41    f.write(yaml_output)