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 details)

Uploaded Source

Built Distribution

ninetales-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file ninetales-0.1.0.tar.gz.

File metadata

  • Download URL: ninetales-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for ninetales-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7fa50fe4966987d05d33cba0bfe1508d2e67e9ed33a012033ebe443d58afecbe
MD5 4166ec01194b2cf571a5f85d1f994ed0
BLAKE2b-256 a5522baa5dd2cf23055b1412e672d7098e3efc983e2020a1d6e2385b58ddd2f5

See more details on using hashes here.

File details

Details for the file ninetales-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ninetales-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for ninetales-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 457e21dfdc1a71dd0d9f856dddeadd66dbea44b093d79e90388e4fcc9e0dc1a4
MD5 c45d69014e1ebb02fb1b1a2a1aa99066
BLAKE2b-256 f1c9a8bfd67ca3d105f4996ea144a2d4d96504258dcbc05d20d3a1f6978de892

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