corm
corm wraps nested dictionaries from parsed JSON or YAML in lightweight Python
entities. If you've ever needed to get the parent of a nested model, corm gives
the child a back-reference to its parent. It also resolves ID references through
an in-memory identity map. corm wraps the input dictionaries in place without
copying or validating them. Undeclared keys are preserved, and dict() exports
the data as dictionaries again.
Example
A project API returns users and sprints. Each sprint embeds its tasks, and each
task stores an assignee_id. The example follows both links, moves a task to a
new sprint, and checks that the export preserves the task's assignee and
undeclared keys such as labels.
Download the runnable project example.
from corm import Entity, Field, KeyNested, Nested, Relationship, Storage
class User(Entity):
id: int = Field(pk=True)
name: str
tasks: list["Task"] = Relationship()
class Task(Entity):
title: str
assignee: User = KeyNested(User.id, origin="assignee_id", back_populates="tasks")
sprint: "Sprint | None" = Relationship()
class Sprint(Entity):
name: str
tasks: list[Task] = Nested(back_populates="sprint")
class Project(Entity):
name: str
users: list[User] = Nested()
sprints: list[Sprint] = Nested()
payload = {
"name": "Customer portal",
"archived": False,
"users": [{"id": 42, "name": "Amina", "timezone": "UTC"}],
"sprints": [
{
"name": "Sprint 12",
"tasks": [
{
"title": "Add invoice download",
"assignee_id": 42,
"labels": ["billing"],
}
],
},
{"name": "Sprint 13", "tasks": []},
],
}
project = Project(payload, Storage())
current_sprint, next_sprint = project.sprints
(task,) = current_sprint.tasks
(user,) = project.users
assert project.name == "Customer portal"
assert task.title == "Add invoice download"
assert task.sprint is current_sprint
assert task.assignee is user
assert user.tasks == [task]
task.sprint = next_sprint
assert current_sprint.tasks == []
assert next_sprint.tasks == [task]
assert task.sprint is next_sprint
assert task.assignee is user
assert user.tasks == [task]
assert project.dict() == {
"name": "Customer portal",
"archived": False,
"users": [{"id": 42, "name": "Amina", "timezone": "UTC"}],
"sprints": [
{"name": "Sprint 12", "tasks": []},
{
"name": "Sprint 13",
"tasks": [
{
"title": "Add invoice download",
"assignee_id": 42,
"labels": ["billing"],
}
],
},
],
}
When to use corm
Use corm to edit a document in memory when your code often navigates from a child to its parent or follows references by ID. corm is not a validation library and is not optimized for speed. Choose a validation or decoding library for untrusted input, a dot-access wrapper for simple attribute access, or SQLite with an ORM when you need queries, joins, or transactions.
| Tool | Validation | Back-references | References by ID | Preserves unknown keys | Typed attribute access |
|---|---|---|---|---|---|
| corm | No | Built in with back_populates |
Built in with KeyNested |
Yes, by default | Yes, for declared fields |
| Pydantic | Yes | Application code | Application code | With extra="allow" |
Yes |
| attrs + cattrs / msgspec | attrs validators / typed decoding in msgspec | Application code | Application code | Requires explicit handling | Yes |
| python-box | No | Application code | Application code | Yes, as mapping keys | Dynamic dot access |
| In-memory SQLite + ORM | Database constraints | With mapped relationships | With foreign keys and joins | Explicit storage, such as a JSON column | With typed ORM models |
Installation
Install with pip install corm. corm is in early development, and its API may
change.
See the entity guide to define your own entities, or read the changelog for recent changes.