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.0b2.tar.gz (24.6 kB view details)

Uploaded Source

Built Distribution

stencila_types-2.0.0b2-py3-none-any.whl (22.7 kB view details)

Uploaded Python 3

File details

Details for the file stencila_types-2.0.0b2.tar.gz.

File metadata

  • Download URL: stencila_types-2.0.0b2.tar.gz
  • Upload date:
  • Size: 24.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.17.3 CPython/3.12.4 Linux/6.5.0-1025-azure

File hashes

Hashes for stencila_types-2.0.0b2.tar.gz
Algorithm Hash digest
SHA256 a3f5b7f0e07ebe17f7f3710affe1666ee61110637eb50b319ba91f1d785316fa
MD5 b3f719459d650aa63d6c418ed8280dc9
BLAKE2b-256 ac47aba6ce583af824c66b7651647ea80e5a2a1a0c752948d7352917db3d3183

See more details on using hashes here.

File details

Details for the file stencila_types-2.0.0b2-py3-none-any.whl.

File metadata

  • Download URL: stencila_types-2.0.0b2-py3-none-any.whl
  • Upload date:
  • Size: 22.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: pdm/2.17.3 CPython/3.12.4 Linux/6.5.0-1025-azure

File hashes

Hashes for stencila_types-2.0.0b2-py3-none-any.whl
Algorithm Hash digest
SHA256 8145ae6f06a1bbfc732c9e70552b1b16b87fdc5f7269ef185f68b9fe3958e882
MD5 a1bcad599018542c0313d6e9d1fb4b8f
BLAKE2b-256 d48ba37c3c536e337e6fcb90ae79d8e98e0c20b2de341d8a2edd9664e3c349e3

See more details on using hashes here.

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