Skip to main content

Datafiles: A file-based ORM for Python dataclasses

Datafiles is a bidirectional serialization library for Python dataclasses to synchronize objects to the filesystem using type annotations. It supports a variety of file formats with round-trip preservation of formatting and comments, where possible. Object changes are automatically saved to disk and only include the minimum data needed to restore each object.

Linux Build Windows Build Code Coverage PyPI License PyPI Version PyPI Downloads

Some common use cases include:

  • Coercing user-editable files into the proper Python types
  • Storing program configuration and state in version control
  • Loading data fixtures for demonstration or testing purposes
  • Synchronizing application state using file sharing services
  • Prototyping data models agnostic of persistence backends

Watch my lightning talk for a demo of this in action!

Overview

Take an existing dataclass such as this example from the documentation:

from dataclasses import dataclass

@dataclass
class InventoryItem:
    """Class for keeping track of an item in inventory."""

    name: str
    unit_price: float
    quantity_on_hand: int = 0

    def total_cost(self) -> float:
        return self.unit_price * self.quantity_on_hand

and decorate it with a directory pattern to synchronize instances:

from datafiles import datafile

@datafile("inventory/items/{self.name}.yml")
class InventoryItem:
    ...

Then, work with instances of the class as normal:

>>> item = InventoryItem("widget", 3)
# inventory/items/widget.yml

unit_price: 3.0

Changes to the object are automatically saved to the filesystem:

>>> item.quantity_on_hand += 100
# inventory/items/widget.yml

unit_price: 3.0
quantity_on_hand: 100

Changes to the filesystem are automatically reflected in the object:

# inventory/items/widget.yml

unit_price: 2.5 # <= manually changed from "3.0"
quantity_on_hand: 100
>>> item.unit_price
2.5

Objects can also be restored from the filesystem:

>>> from datafiles import Missing
>>> item = InventoryItem("widget", Missing)
>>> item.unit_price
2.5
>>> item.quantity_on_hand
100

Installation

Install this library directly into an activated virtual environment:

$ pip install datafiles

or add it to your Poetry project:

$ poetry add datafiles

Documentation

To see additional synchronization and formatting options, please consult the full documentation.

Metadata

Release files for datafiles 2.6

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

Source distribution (sdist)

Source distribution for datafiles 2.6
File Size Uploaded
datafiles-2.6.tar.gz 24.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for datafiles 2.6
File Interpreter ABI Platform
datafiles-2.6-py3-none-any.whl Python 3 none any Details

Total release size: 56.6 kB

Release files / datafiles-2.6.tar.gz

Download URL datafiles-2.6.tar.gz
Size 24.0 kB
Tags Source
SHA-256 checksum
How to use checksums
11af45ec55e0b5b29d2fd5e4ad5d4cde9c3bb61fd137d2c5a8231a3b3d2525b7
BLAKE2b-256 checksum
How to use checksums
9c681b0faf2a8b8502a0391ad931187e45555c40673083fa78862b7c1e482018
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.11.17 Darwin/24.6.0

Release files / datafiles-2.6-py3-none-any.whl

Download URL datafiles-2.6-py3-none-any.whl
Size 32.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d06572269f664aeb5590b516c923ec976664ec410efce572809c461798fa12f9
BLAKE2b-256 checksum
How to use checksums
90ad8d0b7903743060876b4fa0d9878d61ad8b214d3501683f7d4e284e64495b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.5.1 CPython/3.11.17 Darwin/24.6.0

Release history Release notifications | RSS feed

This release

2.6 This release

2 release files

2.5

2 release files

2.4

2 release files

2.3.4

2 release files

2.3.3

2 release files

2.3.2

2 release files

2.3.1

2 release files

2.3

2 release files

2.2.3

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2

2 release files

2.1.2

2 release files

2.1.1

2 release files

2.1

2 release files

2.0

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0

2 release files

0.15

2 release files

0.14

2 release files

0.13.3

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.13

2 release files

0.12

2 release files

0.11.1

2 release files

0.11

2 release files

0.10

2 release files

0.9

2 release files

0.8.1

2 release files

0.8

2 release files

0.7

2 release files

0.6

2 release files

0.5.1

2 release files

0.5

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.3

2 release files

0.2

2 release files

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