• Stars
    star
    197
  • Rank 197,722 (Top 4 %)
  • Language
    Python
  • License
    MIT License
  • Created over 6 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A file-based ORM for Python dataclasses.

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.

Travis CI AppVeyor Coveralls PyPI License PyPI Version PyPI Downloads Gitter

Some common use cases include:

  • Coercing user-editable files into the proper Python types
  • Storing program configuration and data 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.

More Repositories

1

memegen

The free and open source API to generate memes.
Python
1,313
star
2

template-python

My template for new Python libraries.
Python
656
star
3

gitman

Language-agnostic dependency manager using Git.
Python
194
star
4

yorm

Automatic object-YAML mapping for Python.
Python
27
star
5

verchew

System dependency version checker.
Python
25
star
6

template-python-demo

Live demo of my Python template.
Python
23
star
7

minilog

Minimalistic wrapper for Python logging.
Python
19
star
8

mine

Share application state across computers using Dropbox.
Python
17
star
9

template-django

My template for new Django projects.
Python
10
star
10

sappy

Single-page application server for end-to-end testing.
Python
10
star
11

template-flask

My template for new Flask projects.
Python
9
star
12

env-diff

Compares expected environment variables to those set in production.
Python
6
star
13

virtualboombox

Discover music playing nearby.
Python
6
star
14

dropthebeat

Music sharing using Dropbox.
Python
6
star
15

pomace

Dynamic page objects for browser automation.
Python
5
star
16

pytest-expecter

Better testing with expecter and pytest.
Python
5
star
17

battleship

Simulatation of a Battleship AI using random sampling.
Python
4
star
18

pomace-amazon

Reload your Amazon balance automatically.
Python
4
star
19

slackoff

Automatically sign out of Slack workspaces on macOS.
Python
3
star
20

coverage-space

A place to track your code coverage metrics.
Python
2
star
21

dotfiles

My shared dotfiles repository.
Shell
2
star
22

grwifi

Map of WiFi access points in Grand Rapids, Michigan.
HTML
2
star
23

gridcommand

Grid-based clone of Same Time Risk.
Python
2
star
24

memegen-flask

Flask app formerly powering https://memegen.link
Python
2
star
25

template-ruby

A template for new Ruby projects.
Python
1
star
26

crowdsorter

Crowdsource your decision making.
Python
1
star
27

enharmony

[TBD] Song matching based on textual comparison of attributes.
Python
1
star
28

mctweetyface

A Twitter bot to help name things.
Makefile
1
star
29

template-django-demo

A live demo of my Django template.
Python
1
star
30

jacebrowning.info

My personal website and blog.
CSS
1
star