Wiki & Ontology API¶
The pycharter.wiki module provides types, parsers, and validators for field-level semantic annotations, concept vocabularies, lineage tracking, and collaborative governance.
See Wiki & Ontology guide for the full conceptual overview.
Core ontology models¶
OntologyArtifact
dataclass
¶
OntologyArtifact(
version: str, fields: dict[str, FieldOntology] = dict()
)
A complete ontology artifact for a data contract.
Attributes:
| Name | Type | Description |
|---|---|---|
version |
str
|
Ontology version string |
fields |
dict[str, FieldOntology]
|
Mapping of field name to its semantic annotation |
to_dict
¶
FieldOntology
dataclass
¶
FieldOntology(
concept: str,
broader: str | None = None,
definition: str | None = None,
tags: list[str] = list(),
owner: str | None = None,
description: str | None = None,
deprecated: bool = False,
lineage: Lineage | None = None,
relationships: list[Relationship] = list(),
)
Semantic annotation for a single field.
Attributes:
| Name | Type | Description |
|---|---|---|
concept |
str
|
The semantic concept this field represents |
broader |
str | None
|
Parent concept in the hierarchy (deprecated -- use ontologies.yaml) |
definition |
str | None
|
Human-readable definition (deprecated -- use ontologies.yaml) |
tags |
list[str]
|
Tags for categorization |
owner |
str | None
|
Team or person owning this field |
description |
str | None
|
Field-specific business description |
deprecated |
bool
|
Whether this field mapping is deprecated |
lineage |
Lineage | None
|
Data lineage information |
relationships |
list[Relationship]
|
Relationships to other fields |
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/wiki/ontology/models.py
Lineage
dataclass
¶
Tracks the origin of a field.
Attributes:
| Name | Type | Description |
|---|---|---|
derived_from |
str | None
|
Field this was derived from (if any) |
source_system |
str | None
|
System that produces this field |
Relationship
dataclass
¶
Relationship(target: str, type: RelationshipType)
A relationship between two fields.
Attributes:
| Name | Type | Description |
|---|---|---|
target |
str
|
Target field path (e.g., "customers.id") |
type |
RelationshipType
|
Type of relationship |
ConceptDefinition
dataclass
¶
ConceptDefinition(
id: str,
label: str,
definition: str | None = None,
broader: str | None = None,
tags: list[str] = list(),
alt_labels: list[str] = list(),
notation: str | None = None,
examples: list[str] = list(),
exact_match: str | None = None,
deprecated: bool = False,
replaced_by: str | None = None,
node_kind: str | None = None,
)
Full definition of a single concept in the vocabulary.
Mirrors the extended ontologies.yaml schema and can be losslessly converted to/from SKOS JSON-LD.
to_dict
¶
Convert to dictionary (round-trip safe with YAML schema).
Source code in src/pycharter/wiki/ontology/models.py
ConceptScheme
dataclass
¶
ConceptScheme(
id: str,
title: str,
version: str,
description: str = "",
base_uri: str | None = None,
concepts: list[ConceptDefinition] = list(),
)
A complete concept scheme (vocabulary).
Mirrors the top-level scheme: + concepts: structure in
ontologies.yaml.
to_dict
¶
Convert to dictionary.
Source code in src/pycharter/wiki/ontology/models.py
Enumerations¶
ConceptTier
¶
Bases: str, Enum
Governance tier for concepts.
NodeKind
¶
Bases: str, Enum
Classification of concept nodes in the knowledge graph.
RelationshipType
¶
Bases: str, Enum
Types of relationships between fields or concepts.
REFERENCES
class-attribute
instance-attribute
¶
Foreign key or reference relationship.
DERIVED_FROM
class-attribute
instance-attribute
¶
Field is computed from another field.
PRECEDES
class-attribute
instance-attribute
¶
X happens before Y (temporal ordering).
IMPLEMENTS
class-attribute
instance-attribute
¶
X realizes or is a concrete form of Y.
DESCRIBES
class-attribute
instance-attribute
¶
X is a property or attribute of Y.
EQUIVALENT_TO
class-attribute
instance-attribute
¶
X represents the same thing as Y.
Parsing functions¶
parse_ontology
¶
parse_ontology(
ontology_data: dict[str, Any],
) -> OntologyArtifact
Parse an ontology dictionary into an OntologyArtifact.
Expected structure: { "version": "1.0.0", "fields": { "customer_id": { "concept": "Customer Identity", "broader": "Entity", ... } } }
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_data
|
dict[str, Any]
|
Ontology data as dictionary |
required |
Returns:
| Type | Description |
|---|---|
OntologyArtifact
|
OntologyArtifact with parsed fields |
Raises:
| Type | Description |
|---|---|
OntologyError
|
If the data is not a valid dictionary |
Source code in src/pycharter/wiki/ontology/parser.py
parse_ontology_file
¶
parse_ontology_file(file_path: str) -> OntologyArtifact
Load and parse an ontology file (YAML or JSON).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to ontology file |
required |
Returns:
| Type | Description |
|---|---|
OntologyArtifact
|
OntologyArtifact with parsed fields |
Raises:
| Type | Description |
|---|---|
OntologyError
|
If the file cannot be read or parsed |
Source code in src/pycharter/wiki/ontology/parser.py
Validation¶
validate_ontology
¶
Validate raw ontology data before parsing.
Checks structural requirements: - Must have 'version' field - Must have 'fields' dict - Each field must have 'concept' - Relationship types must be valid - No circular broader references
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ontology_data
|
dict[str, Any]
|
Raw ontology dictionary |
required |
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of error dicts with 'path' and 'message' keys. |
list[dict[str, str]]
|
Empty list means valid. |
Source code in src/pycharter/wiki/ontology/validator.py
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
Exceptions¶
PyCharterWikiError
¶
Bases: Exception
Base exception for all pycharter-wiki errors.
Catch this to handle any pycharter-wiki-related failure without depending on specific exception types. Subclasses provide more specific handling.
Example
try: artifact = parse_ontology_file("ontology.yaml") except PyCharterWikiError as e: logger.error("Ontology operation failed: %s", e)
Import¶
# Top-level convenience imports
from pycharter.wiki import (
OntologyArtifact,
FieldOntology,
Lineage,
Relationship,
ConceptTier,
RelationshipType,
parse_ontology,
parse_ontology_file,
validate_ontology,
PyCharterWikiError,
)
# Extended models
from pycharter.wiki.ontology.models import (
ConceptDefinition,
ConceptScheme,
NodeKind,
)
Relationship inverse map¶
Each RelationshipType has a defined inverse, accessible via:
from pycharter.wiki.ontology.models import resolve_inverse
print(resolve_inverse("is_a")) # "has_subtype"
print(resolve_inverse("derived_from")) # "derives"
print(resolve_inverse("precedes")) # "follows"