Python types for Stencila
Project description
Stencila Types for Python
Introduction
This package provides Python classes for types in the Stencila Schema, shortcuts for easily constructing these types, and utilities for loading and saving the types to JSON.
⚡ Usage
Object types
Object types (aka product types) in the Stencila Schema are represented as a dataclass
.
For example, to construct an article with a single "Hello world!" paragraph, you can construct Article
, Paragraph
and Text
:
from stencila_types.types import Article, CreativeWork, Paragraph, Text, Thing
article = Article(content=[Paragraph(content=[Text(value="Hello world!")])])
assert isinstance(article, Article)
assert isinstance(article, CreativeWork)
assert isinstance(article, Thing)
assert isinstance(article.content[0], Paragraph)
assert isinstance(article.content[0].content[0], Text)
Union types
Union types (aka sum types) in the Stencila Schema are represented as typing.Union
. For example, the Block
union type is defined like so:
Block = Union[
Call,
Claim,
CodeBlock,
CodeChunk,
Division,
Figure,
For,
Form,
Heading,
...
Enumeration types
Enumeration types in the Stencila Schema are represented as StrEnum
. For example, the CitationIntent
enumeration is defined like so:
class CitationIntent(StrEnum):
"""
The type or nature of a citation, both factually and rhetorically.
"""
AgreesWith = "AgreesWith"
CitesAsAuthority = "CitesAsAuthority"
CitesAsDataSource = "CitesAsDataSource"
CitesAsEvidence = "CitesAsEvidence"
CitesAsMetadataDocument = "CitesAsMetadataDocument"
CitesAsPotentialSolution = "CitesAsPotentialSolution"
CitesAsRecommendedReading = "CitesAsRecommendedReading"
CitesAsRelated = "CitesAsRelated"
Shortcuts
Constructing complex Stencila types can be more easily constructed using the shortcuts module.
from stencila_types import types as T
from stencila_types import shortcuts as S
# As above
art1 = T.Article(content=[T.Paragraph(content=[T.Text(value="Hello world!")])])
# Using shortcuts
art2 = S.art(S.p("Hello world!"))
assert art1 == art2
Basic JSON support
import json
from stencila_types.utilities import from_json, to_json
# Using shortcuts
art1 = S.art(S.p("Hello world!"))
s = to_json(art1)
assert s.startswith('{"type": "Article", "id": null,')
art2 = from_json(s)
assert art1 == art2
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for stencila_types-2.0.0b2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8145ae6f06a1bbfc732c9e70552b1b16b87fdc5f7269ef185f68b9fe3958e882 |
|
MD5 | a1bcad599018542c0313d6e9d1fb4b8f |
|
BLAKE2b-256 | d48ba37c3c536e337e6fcb90ae79d8e98e0c20b2de341d8a2edd9664e3c349e3 |