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 0.11.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 0.11.0
File Size Uploaded
python_datamodel-0.11.0.tar.gz 2.1 MB Details

Built distributions (wheels)

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

Total release size: 30.8 MB

Release files / python_datamodel-0.11.0.tar.gz

Download URL python_datamodel-0.11.0.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
82af024c60ed14be11bfa069fc60d86333e7d0592016da5d7e40f68d3a1a8dbe
BLAKE2b-256 checksum
How to use checksums
7018002a2de90e33d7396244acce37e1901ef8c4fedb7f144b814135a45e9c9d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.0-cp314-cp314-win_amd64.whl
Size 1.1 MB
Tags CPython 3.14 Windows x86-64
SHA-256 checksum
How to use checksums
4b8cb3db28894d722afc5d3fabf9b89f9d255b999cabc6ebd1e7dbfac8a0a2d2
BLAKE2b-256 checksum
How to use checksums
f0f992e9527b33041e9e22c6ed435926e65ce6a095f19cc2d7aa24b57bd9c3ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.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
beb85a00dbcf0fce1387b4bec5631ace2ff6b73b4302bcd28fa9a6c15e30f87e
BLAKE2b-256 checksum
How to use checksums
940226e81576ce61dfe4e00a991bf57141fb57357b9ebc43d639dea0e25c2854
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

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

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

Download URL python_datamodel-0.11.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
6e0cefeaf97dd6dd9b572cabf846a9c9892d536a3ba7d98160e2198957474f1c
BLAKE2b-256 checksum
How to use checksums
3679309c54dbd5e988dfc96fd33a02d98465fd550c634b3b6ba73641ab218e53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.0-cp312-cp312-win_amd64.whl
Size 1.1 MB
Tags CPython 3.12 Windows x86-64
SHA-256 checksum
How to use checksums
e1385e944979b8c789b5541f420048a13feda30ee6d4f484bf4e64866ca5c208
BLAKE2b-256 checksum
How to use checksums
70b22ddf1b86cf96b3072e3d7d5fdcd31f2238d81f5b2ade11531fa904f0a876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.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
35ffdd1ed4b89d0c0069c4127787a3c7a5203ae0eff209e0c5624fffe711beea
BLAKE2b-256 checksum
How to use checksums
f8ee0bdab57cf45154939ee5117d785c13c14bdce03fb76da6a99ddce0312e53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.0-cp311-cp311-win_amd64.whl
Size 1.1 MB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
1f8c8119a98c3f2626efcb08259f4f8cfd54c05d8e01d407b3b06640ba9727e6
BLAKE2b-256 checksum
How to use checksums
8ce3fbb418257f5b3fafa05057bce242348fe6f50de9f6962cf38fdabc452fbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.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
0f6fd8da6362c561bb571c4eab18598c19c14a507dc9fc089589d332b659d448
BLAKE2b-256 checksum
How to use checksums
09d32321533665a1a1d1fae1aa2628d53eec1a564d277777e77c462c99f6f5f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.0-cp310-cp310-win_amd64.whl
Size 1.1 MB
Tags CPython 3.10 Windows x86-64
SHA-256 checksum
How to use checksums
fb2159c508c3f02909de130668dbd6f89faa6b4fc13578b5d409f664376aac37
BLAKE2b-256 checksum
How to use checksums
96f4d36b2e0bfdcbc05cd4464e96d839249d6b6b3c276a6a763ec8841a892431
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

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

Download URL python_datamodel-0.11.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
12d2264be44f200d448b2a0cc7669978be2f14f5a708f2d87663842f95c25633
BLAKE2b-256 checksum
How to use checksums
88e282de860762bf30258123627e8e6929bb65310b1370e5ff20c0017328c584
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.11.0 This release

11 release files

0.9.4

12 release files

0.9.3

12 release files

0.9.2

12 release files

0.9.1

12 release files

0.9.0

12 release files

0.8.3

12 release files

0.8.2

12 release files

0.8.1

12 release files

0.8.0

12 release files

0.7.2

11 release files

0.7.1

11 release files

0.7.0

11 release files

0.6.18

2 release files

0.6.17

2 release files

0.6.16

3 release files

0.6.15

3 release files

0.6.14

3 release files

0.6.13

3 release files

0.6.12

3 release files

0.6.11

3 release files

0.6.10

3 release files

0.6.9

3 release files

0.6.8

3 release files

0.6.7

3 release files

0.6.6

3 release files

0.6.5

3 release files

0.6.4

3 release files

0.6.3

3 release files

0.6.2

3 release files

0.6.1

3 release files

0.5.0

3 release files

0.4.1

3 release files

0.4.0

3 release files

0.3.15

3 release files

0.3.14

3 release files

0.3.12

3 release files

0.3.11

3 release files

0.3.10

3 release files

0.3.8

3 release files

0.3.7

3 release files

0.3.6

3 release files

0.3.5

3 release files

0.3.4

3 release files

0.3.3

3 release files

0.3.2

3 release files

0.3.1

3 release files

0.3.0

3 release files

0.2.9

3 release files

0.2.8

3 release files

0.2.7

3 release files

0.2.6

3 release files

0.2.5

5 release files

0.2.4

3 release files

0.2.3

3 release files

0.2.2

3 release files

0.2.1

3 release files

0.2.0

3 release files

0.1.23

2 release files

0.1.22

2 release files

0.1.21

2 release files

0.1.20

2 release files

0.1.18

2 release files

0.1.17

2 release files

0.1.16

2 release files

0.1.14

2 release files

0.1.13

2 release files

0.1.12

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.40

2 release files

0.0.39

2 release files

0.0.38

2 release files

0.0.37

2 release files

0.0.30

2 release files

0.0.27

4 release files

0.0.26

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

3 release files

0.0.22

2 release files

0.0.21

2 release files

0.0.20

2 release files

0.0.19

2 release files

0.0.18

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.15

2 release files

0.0.14

2 release files

0.0.13

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.1

2 release files

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