Skip to main content

Python types for Stencila

Project description

Stencila Types for Python

stencila_types

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stencila_types-2.0.0a28.tar.gz (22.9 kB view hashes)

Uploaded Source

Built Distribution

stencila_types-2.0.0a28-py3-none-any.whl (21.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page