Package: schemantic
SchemanticCreate schemas from models or classes with homologous, grouped, or cultured paradigms.Best withpydantic.BaseModelinstances, but works with any Python class anddataclass/pydantic.dataclass!Classes of schemasHomologfrompydanticimportBaseModelfromordered_setimportOrderedSetfromschemantic.schemaimportHomologSchemaclassThief(BaseModel):stolen_goods:intsteals_only:strmy_homolog=HomologSchema.from_model(Thief,instance_names=OrderedSet(["copycat","pink_panther"]))GroupedYou can manage multiple schemas as a group:frompydanticimportBaseModelfromschemantic.schemaimportGroupSchemaclassBaker(BaseModel):baked_goods:intcitizen_of:strclassCop(BaseModel):years_of_service:intcitizen_of:strgroup_schema=GroupSchema.from_originating_types([Baker,Cop],)CultureYou can also manage multiple types of schemas under one culture:fromordered_setimportOrderedSetfromschemantic.schemaimportCultureSchemaCultureSchema(source_schemas=OrderedSet([homolog_schema,group_schema]))MethodsHomologSchema,GroupSchema, andCultureSchemahave the following methods..schema()Creates a dictionary, which represents the schema of the origin class/model.my_homolog.schema()Output:"class_name":"Thief""common":{steals_only:"jewelry"}"copycat":{}"pink_panther":{}"required":["stolen_goods","steals_only"]"field_to_info":{"stolen_goods":"integer"}.dump()Dump the dictionary from.schema()to a yaml or toml file.my_schema.dump("my/path/schema.yaml")# There is also toml supportmy_schema.dump("my/path/schema.toml").parse()"class_name":"Thief""common":{steals_only:"jewelry"}"copycat":{stolen_goods:10}"pink_panther":{stolen_goods:14}"required":["stolen_goods","steals_only"]"field_to_info":{"stolen_goods":"integer"}parsed=my_homolog.parse_schema("my/path/schema.yaml")# parsed["copycat"].stolen_goods == 10# parsed["pink_panther"].stolen_goods == 14# Both -> steals_only == "jewelry"Class configurationUseschemantic.projectmodule to control schemantic processing from the origin class/model side.Classes and dataclassesfromdataclassesimportdataclassfromtypingimportOptional,Unionfromschemantic.projectimportSchemanticProjectMixin@dataclassclassTestDataclass(SchemanticProjectMixin):must_be:intwe:str="n"n:None=Noneage:Optional[int]=Nonenew_age:Union[int,str,None]=Noneexclude_me:Optional[int]=None_exclude_me_too:Optional[float]=None@classmethod@propertydeffields_to_exclude_from_single_schema(cls)->set[str]:upstream=super().fields_to_exclude_from_single_schemaupstream.update(("exclude_me",))returnupstreamThis will excludeexclude_me(defined infields_to_exclude_from_single_schema) and_exclude_me_too(private).Same forpydantic.BaseModel:fromtypingimportOptional,UnionfrompydanticimportBaseModel,computed_fieldfromschemantic.projectimportSchemanticProjectModelMixinclassTestModel(SchemanticProjectModelMixin,BaseModel):must_be:intwe:str="n"n:None=Noneage:Optional[int]=Nonenew_age:Union[int,str,None]=Noneexclude_me:Optional[int]=None_exclude_me_too:Optional[float]=None@classmethod# type: ignore[misc]@computed_field(return_type=set[str])@propertydeffields_to_exclude_from_single_schema(cls)->set[str]:upstream=super().fields_to_exclude_from_single_schemaupstream.update(("exclude_me",))returnupstreamInstallpipinstallschemanticFortomloryamldumping and parsingpipinstallschemantic[toml]pipinstallschemantic[yaml]