Skip to main content

A Python library to convert between Python data model paradigms

Project description

ninetales - A Python library to convert between Python data model paradigms

The fox spirit is an especially prolific shapeshifter

There are a few too many ways of describing a data model in Python--see below code chunk for a non-comprehensive list. All of them represent the same concept, but have different approaches in their philosophies and implementations. This library strives, to a reasonable and useful extent, to provide a seamless translation between them.

from dataclasses import dataclass
from typing import NamedTuple, TypedDict
from collections import namedtuple

import attrs
import msgspec
import pydantic

@dataclass
class FooDataclass:
    bar: str
    baz: int = 1


class FooNamedTuple(NamedTuple):
    bar: str
    baz: int = 1


# `typing.TypedDict` does not allow right hand side assignment
class FooNamedTuple(TypedDict):
    bar: str
    baz: int


# Attributes can be specified via:
# - a sequence of strings (as below)
# - a single string with each field name separated by whitespace 
# and/or commas (e.g., "bar baz", "bar, baz")
FooNamedTuple2 = namedtuple("FooNamedTuple2", ["bar", "baz"])


@attrs.define
class FooAttrs:
    bar: str
    baz: int = 1


FooAttrs2 = attrs.make_class(
    "FooAttrs2",
    {"bar": attrs.field(type=str), "baz": attrs.field(type=int, default=1)}
)


class FooMsgspec(msgspec.Struct):
    bar: str
    baz: int = 1


class FooPydantic(pydantic.BaseModel):
    bar: str
    baz: int = 1


FooPydantic2 = pydantic.create_model("FooPydantic2", bar=(str, ...), baz=(int, 1))

Etymology

Ninetales is a Pokemon loosely based on the nine-tailed fox, mythical fox entity in Chinese, Korean, Vietnamese, and Japanese folklore. "The fox spirit is an especially prolific shapeshifter", is the approach this library takes to converting between Python data model paragigms.

Installation

For the time being and until we have a stable enough API:

git clone git@github.com:gorkaerana/ninetales.git
cd ninetales
python3 -m venv .venv
. .venv/bin/activate
python3 -m pip install -U pip
python3 -m pip install .

Development

Im using rye with uv backend:

git clone git@github.com:gorkaerana/ninetales.git
cd ninetales
rye sync

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

ninetales-0.1.0.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

ninetales-0.1.0-py3-none-any.whl (3.0 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