YORM v0.4 Released
17 May 2015Yesterday, I released an important milestone of my file-based object relational mapper for Python, YORM. This release provides support for unlimited nesting of container-like attributes. This is a feature I’ve wanted for a while, but was actually quite difficult to implement. The API for YORM is also starting to stabilize after some breaking changes from the previous release.
Some Background
Lately, I’ve been running into many situations where I’d like to store program configuration and/or data in version control. YORM was born to provide automatic, bidirectional, and human-friendly mappings of Python object attributes to YAML files.
Traditional object serializes don’t provide output fit for human modification and ORM databases aren’t fit for storage in version control. YORM supports additional uses beyond typical object serialization and mapping including:
- bidirectional conversion between basic YAML and Python types
- attribute creation and type inference for new attributes
- storage of content in text files optimized for version control
- extensible converters to customize formatting on complex classes
An Example
Given an existing class:
class Student:
def __init__(self, name, school, number, year=2009):
self.name = name
self.school = school
self.number = number
self.year = year
self.gpa = 0.0
an attribute mapping is defined mapping attributes to converter classes and instances to a file pattern:
import yorm
from yorm.converters import String, Integer, Float
@yorm.attr(name=String, year=Integer, gpa=Float)
@yorm.sync("students/{self.school}/{self.number}.yml")
class Student:
...
Modifications to each object’s mapped attributes:
>>> s1 = Student("John Doe", "GVSU", 123)
>>> s2 = Student("Jane Doe", "GVSU", 456, year=2014)
>>> s1.gpa = 3
are automatically reflected on the filesytem:
$ cat students/GVSU/123.yml
name: John Doe
gpa: 3.0
school: GVSU
year: 2009
Modifications and new content in each mapped file:
$ echo "name: John Doe
> gpa: 1.8
> year: 2010
> expelled: true
" > students/GVSU/123.yml
are automatically reflected in their corresponding object:
>>> s1.gpa
1.8
>>> s1.expelled
True
Current Uses
Right now I’m using YORM to:
- store program state in Dropbox: mine
- simplify configuration file loading: GitMan
- prototype a RESTful game API: GridCommand
See a typo? Help me edit this post.
Find a problem with yorm
? Please submit an issue or contribute!