Skip to main content

DataModel

DataModel is a simple library based on python +3.8 to use Dataclass-syntax for interacting with Data, using the same syntax of Dataclass, users can write Python Objects and work with Data in the same way (like ORM's), is a reimplementation of python Dataclasses supporting true inheritance (without decorators), true composition and other good features.

The key features are:

  • Easy to use: No more using decorators, concerns abour re-ordering attributes or common problems with using dataclasses with inheritance.
  • Extensibility: Can use other dataclasses, Data objects or primitives as data-types.
  • Fast: DataModel is a replacement 100% full compatible with dataclasses, without any overhead.

Requirements

Python 3.10+ (3.10, 3.11, 3.12, 3.13 and 3.14 are supported).

Binary wheels are published for Linux (manylinux x86_64) and Windows (win_amd64); other platforms fall back to a source install.

Installation

$ pip install python-datamodel
---> 100%
Successfully installed datamodel

Optional uvloop acceleration

uvloop is not a hard dependency: install it as the optional uvloop extra on supported non-Windows platforms:

$ pip install "python-datamodel[uvloop]"

Importing datamodel never changes the application's event-loop policy. Opt in explicitly, typically at application startup:

from datamodel.libs.uvloop import install_uvloop

install_uvloop()

Quickstart

from datamodel import Field, BaseModel
from dataclasses import dataclass, fields, is_dataclass


# This pure Dataclass:
@dataclass
class Point:
    x: int = Field(default=0, min=0, max=10)
    y: int = Field(default=0, min=0, max=10)

point = Point(x=10, y=10)
print(point)
print(fields(point))
print('IS a Dataclass?: ', is_dataclass(point))

# Can be represented by BaseModel
class newPoint(BaseModel):
    x: int = Field(default=0, min=0, max=10)
    y: int = Field(default=0, min=0, max=10)

    def get_coordinate(self):
        return (self.x, self.y)

point = newPoint(x=10, y=10)
print(point)
print(fields(point))
print('IS a Dataclass?: ', is_dataclass(point))
print(point.get_coordinate())

Supported types

DataModel support recursive transformation of fields, so you can easily work with nested dataclasses or complex types.

DataModel supports automatic conversion of:

  • datetime objects. datetime objects are encoded to str exactly like orjson conversion, any str typed as datetime is decoded to datetime. The same behavior is used to decoding time, date and timedelta objects.

  • UUID objects. They are encoded as str (JSON string) and decoded back to uuid.UUID objects.

  • Decimal objects. They are also encoded as float and decoded back to Decimal.

Also, "custom" encoders are supported.

import uuid
from typing import (
    List,
    Optional,
    Union
)
from dataclasses import dataclass, field
from datamodel import BaseModel, Field

@dataclass
class Point:
    x: int = Field(default=0, min=0, max=10)
    y: int = Field(default=0, min=0, max=10)

class coordinate(BaseModel, intSum):
    latitude: float
    longitude: float

    def get_location(self) -> tuple:
        return (self.latitude, self.longitude)

def auto_uid():
    return uuid.uuid4()

def default_rect():
    return [0,0,0,0]

def valid_zipcode(field, value):
    return value > 1000

class Address(BaseModel):
    id: uuid.UUID = field(default_factory=auto_uid)
    street: str = Field(required=True)
    zipcode: int = Field(required=False, default=1010, validator=valid_zipcode)
    location: Optional[coordinate]
    box: List[Optional[Point]]
    rect: List[int] = Field(factory=default_rect)


addr = Address(street="Calle Mayor", location=(18.1, 22.1), zipcode=3021, box=[(2, 10), (4, 8)], rect=[1, 2, 3, 4])
print('IS a Dataclass?: ', is_dataclass(addr))

print(addr.location.get_location())
# returns
Address(id=UUID('24b34dd5-8d35-4cfd-8916-7876b28cdae3'), street='Calle Mayor', zipcode=3021, location=coordinate(latitude=18.1, longitude=22.1), box=[Point(x=2, y=10), Point(x=4, y=8)], rect=[1, 2, 3, 4])
  • Fast and convenience conversion from-to JSON (using orjson):
import orjson

b = addr.json()
print(b)
{"id":"24b34dd5-8d35-4cfd-8916-7876b28cdae3","street":"Calle Mayor","zipcode":3021,"location":{"latitude":18.1,"longitude":22.1}, "box":[{"x":2,"y":10},{"x":4,"y":8}],"rect":[1,2,3,4]}
# and re-imported from json
new_addr = Address.from_json(b) # load directly from json string
# or using a dictionary decoded by orjson
data = orjson.loads(b)
new_addr = Address(**data)

Inheritance

python-datamodel supports inheritance of classes.

import uuid
from typing import Union, List
from dataclasses import dataclass, field
from datamodel import BaseModel, Column, Field


def auto_uid():
    return uuid.uuid4()

class User(BaseModel):
    id: uuid.UUID = field(default_factory=auto_uid)
    name: str
    first_name: str
    last_name: str


@dataclass
class Address:
    street: str
    city: str
    state: str
    zipcode: str
    country: Optional[str] = 'US'

    def __str__(self) -> str:
        """Provides pretty response of address"""
        lines = [self.street]
        lines.append(f"{self.city}, {self.zipcode} {self.state}")
        lines.append(f"{self.country}")
        return "\n".join(lines)

class Employee(User):
    """
    Base Employee.
    """
    role: str
    address: Address # composition of a dataclass inside of DataModel is possible.

# Supporting multiple inheritance and composition
# Wage Policies
class MonthlySalary(BaseModel):
    salary: Union[float, int]

    def calculate_payroll(self) -> Union[float, int]:
        return self.salary

class HourlySalary(BaseModel):
    salary: Union[float, int] = Field(default=0)
    hours_worked: Union[float, int] = Field(default=0)

    def calculate_payroll(self) -> Union[float, int]:
        return (self.hours_worked * self.salary)

# employee types
class Secretary(Employee, MonthlySalary):
    """Secretary.

    Person with montly salary policy and no commissions.
    """
    role: str = 'Secretary'

class FactoryWorker(Employee, HourlySalary):
    """
    FactoryWorker is an employee with hourly salary policy and no commissions.
    """
    role: str = 'Factory Worker'

class PayrollSystem:
    def calculate_payroll(self, employees: List[dataclass]) -> None:
        print('=== Calculating Payroll === ')
        for employee in employees:
            print(f"Payroll for employee {employee.id} - {employee.name}")
            print(f"- {employee.role} Amount: {employee.calculate_payroll()}")
            if employee.address:
                print('- Sent to:')
                print(employee.address)
            print("")

jane = Secretary(name='Jane Doe', first_name='Jane', last_name='Doe', salary=1500)
bob = FactoryWorker(name='Bob Doyle', first_name='Bob', last_name='Doyle', salary=15, hours_worked=40)
mitch = FactoryWorker(name='Mitch Brian', first_name='Mitch', last_name='Brian', salary=20, hours_worked=35)

payroll = PayrollSystem()
payroll.calculate_payroll([jane, bob, mitch])

A sample of output:

```console
=== Calculating Payroll ===
Payroll for employee 745a2623-d4d2-4da6-bf0a-1fa691bafd33 - Jane Doe
- Secretary Amount: 1500
- Sent to:
Rodeo Drive, Rd
Los Angeles, 31050 CA
US

Contributing

First of all, thank you for being interested in contributing to this library. I really appreciate you taking the time to work on this project.

  • If you're just interested in getting into the code, a good place to start are issues tagged as bugs.
  • If introducing a new feature, especially one that modifies the public API, consider submitting an issue for discussion before a PR. Please also take a look at existing issues / PRs to see what you're proposing has already been covered before / exists.
  • I like to follow the commit conventions documented here

License

This project is licensed under the terms of the BSD v3. license.

Metadata

Release files for python-datamodel 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-datamodel 1.0.0
File Size Uploaded Provenance Actions
python_datamodel-1.0.0.tar.gz 2.2 MB Without provenance Details for python_datamodel-1.0.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for python-datamodel 1.0.0
File Actions
python_datamodel-1.0.0-cp314-cp314-win_amd64.whl CPython 3.14 CPython 3.14 Windows x86-64 Details for python_datamodel-1.0.0-cp314-cp314-win_amd64.whl
python_datamodel-1.0.0-cp314-cp314-manylinux_2_28_x86_64.whl CPython 3.14 CPython 3.14 Linux glibc 2.28+ x86-64 Details for python_datamodel-1.0.0-cp314-cp314-manylinux_2_28_x86_64.whl
python_datamodel-1.0.0-cp314-cp314-macosx_11_0_arm64.whl CPython 3.14 CPython 3.14 macOS 11.0+ ARM64 Details for python_datamodel-1.0.0-cp314-cp314-macosx_11_0_arm64.whl
python_datamodel-1.0.0-cp313-cp313-win_amd64.whl CPython 3.13 CPython 3.13 Windows x86-64 Details for python_datamodel-1.0.0-cp313-cp313-win_amd64.whl
python_datamodel-1.0.0-cp313-cp313-manylinux_2_28_x86_64.whl CPython 3.13 CPython 3.13 Linux glibc 2.28+ x86-64 Details for python_datamodel-1.0.0-cp313-cp313-manylinux_2_28_x86_64.whl
python_datamodel-1.0.0-cp313-cp313-macosx_11_0_arm64.whl CPython 3.13 CPython 3.13 macOS 11.0+ ARM64 Details for python_datamodel-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
python_datamodel-1.0.0-cp312-cp312-win_amd64.whl CPython 3.12 CPython 3.12 Windows x86-64 Details for python_datamodel-1.0.0-cp312-cp312-win_amd64.whl
python_datamodel-1.0.0-cp312-cp312-manylinux_2_28_x86_64.whl CPython 3.12 CPython 3.12 Linux glibc 2.28+ x86-64 Details for python_datamodel-1.0.0-cp312-cp312-manylinux_2_28_x86_64.whl
python_datamodel-1.0.0-cp312-cp312-macosx_11_0_arm64.whl CPython 3.12 CPython 3.12 macOS 11.0+ ARM64 Details for python_datamodel-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
python_datamodel-1.0.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details for python_datamodel-1.0.0-cp311-cp311-win_amd64.whl
python_datamodel-1.0.0-cp311-cp311-manylinux_2_28_x86_64.whl CPython 3.11 CPython 3.11 Linux glibc 2.28+ x86-64 Details for python_datamodel-1.0.0-cp311-cp311-manylinux_2_28_x86_64.whl
python_datamodel-1.0.0-cp311-cp311-macosx_11_0_arm64.whl CPython 3.11 CPython 3.11 macOS 11.0+ ARM64 Details for python_datamodel-1.0.0-cp311-cp311-macosx_11_0_arm64.whl
python_datamodel-1.0.0-cp310-cp310-win_amd64.whl CPython 3.10 CPython 3.10 Windows x86-64 Details for python_datamodel-1.0.0-cp310-cp310-win_amd64.whl
python_datamodel-1.0.0-cp310-cp310-manylinux_2_28_x86_64.whl CPython 3.10 CPython 3.10 Linux glibc 2.28+ x86-64 Details for python_datamodel-1.0.0-cp310-cp310-manylinux_2_28_x86_64.whl
python_datamodel-1.0.0-cp310-cp310-macosx_11_0_arm64.whl CPython 3.10 CPython 3.10 macOS 11.0+ ARM64 Details for python_datamodel-1.0.0-cp310-cp310-macosx_11_0_arm64.whl

Total release size: 37.5 MB

Release files / python_datamodel-1.0.0.tar.gz

Download URL python_datamodel-1.0.0.tar.gz
Size 2.2 MB
Tags Source
SHA-256 checksum
How to use checksums
453070a9189f810fb984303d098316e10769d4555b5b1d66011a2c908734def2
BLAKE2b-256 checksum
How to use checksums
157ebb9d256f2ab1c462cce787d22353f9f682adbe60894da7bbd44f40526ca2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp314-cp314-win_amd64.whl

Download URL python_datamodel-1.0.0-cp314-cp314-win_amd64.whl
Size 1.1 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
1932887c2178911121feb01890dc02403b4acab7c1e05954ace8be2b4ab8ccf2
BLAKE2b-256 checksum
How to use checksums
8c5291c1ad59d51def2d7a4d88329a5375bd728fa21a52110e1c223b075818f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp314-cp314-manylinux_2_28_x86_64.whl

Download URL python_datamodel-1.0.0-cp314-cp314-manylinux_2_28_x86_64.whl
Size 4.6 MB
Tags CPython 3.14 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
8cab8e89d591fd0146b49bf11b586bc920aecc79d622e14dfaef7273077f2129
BLAKE2b-256 checksum
How to use checksums
bc89d0f78511ec6256477803b1f4b57402ca84a5257b9db497dd685aace79f6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp314-cp314-macosx_11_0_arm64.whl

Download URL python_datamodel-1.0.0-cp314-cp314-macosx_11_0_arm64.whl
Size 1.3 MB
Tags CPython 3.14 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
7530ef4879a3fda3c48f0648ad26df0c1d67f1809403f0a3b0a5ae6d71931548
BLAKE2b-256 checksum
How to use checksums
f71427763f25f864cd81291c02a73a700523aa47e12ac5eed6b6831d129ce6df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp313-cp313-win_amd64.whl

Download URL python_datamodel-1.0.0-cp313-cp313-win_amd64.whl
Size 1.1 MB
Tags CPython 3.13 Windows x86-64
SHA-256 checksum
How to use checksums
d50a75a76a1db1e3f05c8c61110ce94c8bf4a5553aefec6db9fd16d1b7d8f0b0
BLAKE2b-256 checksum
How to use checksums
58b643f3cbd56f9621f44fc07ed73c74eba9518fcf7d7ac259954cfe43a5bee3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp313-cp313-manylinux_2_28_x86_64.whl

Download URL python_datamodel-1.0.0-cp313-cp313-manylinux_2_28_x86_64.whl
Size 4.6 MB
Tags CPython 3.13 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
af214da779a94d28f7264a5ef4dd5d14e31caf3a9ce455e613276ff9af27c83a
BLAKE2b-256 checksum
How to use checksums
c3d1f90d5725c100d7d6672fe2b57e78771d3c393680b402b08a20800301ea2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp313-cp313-macosx_11_0_arm64.whl

Download URL python_datamodel-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Size 1.3 MB
Tags CPython 3.13 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
994f6e06573b61873ba8bee78477b9a305ffbd055deddaad461a599f79cf6ae0
BLAKE2b-256 checksum
How to use checksums
23ef5c5c09e6f1b3d5c92349c09e9ce9aa188eec3976552a4db4e95fe131905c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp312-cp312-win_amd64.whl

Download URL python_datamodel-1.0.0-cp312-cp312-win_amd64.whl
Size 1.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
0d7316549b3807e7fd7f94f0a23dc033f92e471651dc804795847997946fd815
BLAKE2b-256 checksum
How to use checksums
62b6c06216a85c9f94453045e9ca9f281813496488771ddb65d13313f3410bf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp312-cp312-manylinux_2_28_x86_64.whl

Download URL python_datamodel-1.0.0-cp312-cp312-manylinux_2_28_x86_64.whl
Size 4.7 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
4cb6b7c094b231d6dc5b9c36b6aac3d89414b9a0f0301e57d41e56654ae0e0f2
BLAKE2b-256 checksum
How to use checksums
36a727e6b326e9967f41338934ec420145ef6a570c7dc6e6b8d202880bcdc7cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp312-cp312-macosx_11_0_arm64.whl

Download URL python_datamodel-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Size 1.3 MB
Tags CPython 3.12 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
6dc948c1181448a8af02d7ecde29ab79e94a7c52772efe551458c309c3bbd2c1
BLAKE2b-256 checksum
How to use checksums
501834b6dcca4ac9db6472ba517cb9775efd6ac8d9409d731c350bbbab2e42bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp311-cp311-win_amd64.whl

Download URL python_datamodel-1.0.0-cp311-cp311-win_amd64.whl
Size 1.1 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
b6be0ccef33c95d0c9a80e05f4abab4cdcd3b7346c64bc491b9727f8116c1ca8
BLAKE2b-256 checksum
How to use checksums
08e89ca772be5a6d24686fbf704d138bae8e4f6b260299d3b483ed2d857cf0f8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp311-cp311-manylinux_2_28_x86_64.whl

Download URL python_datamodel-1.0.0-cp311-cp311-manylinux_2_28_x86_64.whl
Size 4.8 MB
Tags CPython 3.11 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
1f047edb3efe81c3f590bcc3db21bccb00c9dabd261244fdc2c51d88bac16c39
BLAKE2b-256 checksum
How to use checksums
3b91234e2dd4708353f670515a6f2bff2311ced070f210169b111a2c955aa774
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp311-cp311-macosx_11_0_arm64.whl

Download URL python_datamodel-1.0.0-cp311-cp311-macosx_11_0_arm64.whl
Size 1.3 MB
Tags CPython 3.11 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
a10eb3124970ce4274fba8e7f60337c361421e4c54bb45be1c04a9fb07ad0819
BLAKE2b-256 checksum
How to use checksums
31edbc259a31d8ebfda497aa1da6f2c81144a429a35424ab9b0156da73f633c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp310-cp310-win_amd64.whl

Download URL python_datamodel-1.0.0-cp310-cp310-win_amd64.whl
Size 1.1 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
78e407b22760134e6af7e88bb7fc0ba7de8fb1382909d53a5ce6442ef017e57b
BLAKE2b-256 checksum
How to use checksums
66fac94538c4477148fe0b06273e9facf5d0be45e4ab2ef2ba6e0fea0229b3d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp310-cp310-manylinux_2_28_x86_64.whl

Download URL python_datamodel-1.0.0-cp310-cp310-manylinux_2_28_x86_64.whl
Size 4.6 MB
Tags CPython 3.10 Linux glibc 2.28+ x86-64
SHA-256 checksum
How to use checksums
433b8fe566b51465e4e67d144d98763667152e9e5c917bef86c0ae4770da9851
BLAKE2b-256 checksum
How to use checksums
f51029275ed848fcc419145a31b6ec1caca6389fe664676f7bf9b87d162ada0e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Release files / python_datamodel-1.0.0-cp310-cp310-macosx_11_0_arm64.whl

Download URL python_datamodel-1.0.0-cp310-cp310-macosx_11_0_arm64.whl
Size 1.3 MB
Tags CPython 3.10 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
61b19ce9a8295ad159213315c6ae0e2493accb782bcfe07281051ed06d0bef64
BLAKE2b-256 checksum
How to use checksums
a3dc581882341b2969c31d30aa5874db2dfa74cf0cfc463f1cb2db74947ec7e4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.4

Security for python-datamodel 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

16 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page