clirm


Nameclirm JSON
Version 0.1 PyPI version JSON
download
home_pageNone
SummaryCommand line-oriented ORM.
upload_time2024-04-08 14:23:12
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseNone
keywords database cli orm
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # clirm

Command-Line ORM (clirm) is a library for creating simple ORMs that
can be used in command-line programs that allow users to manipulate
objects in an interactive context, and that also regularly run scripts
that iterate over the entire database.

Features include:

- Changes are always committed to the database immediately, so that
  there is no need to worry about a separate later "save" step.
- There is always only one object per database row, so that users
  do not need to worry about editing one copy and leaving another
  ORM object corresponding to the same row unchanged.
- All columns in a row are always fetched together, to simplify
  implementation of the above point.
- Tight integration with the type system. Fields can be declared
  as `Field[T]()`, where `T` is a normal Python type.

Clirm requires that every table has an `id` column containing
a unique identifier.

## Usage

As an example, we will create a simple database containing
animal taxa:

```python
import enum
import sqlite3
from typing import Self

from clirm import Clirm, Field, Model

class Status(enum.Enum):
    living = 1
    recently_extinct = 2
    fossil = 3

CLIRM = Clirm(sqlite3.connect("taxon.db"))

class Taxon(Model):
    clirm = CLIRM
    clirm_table_name = "taxon"

    name = Field[str]()  # string field
    status = Field[Status]()  # enum field
    common_name = Field[str | None]()  # nullable string field
    parent = Field[Self | None]()  # foreign key to self; can also write "Taxon | None"

if __name__ == "__main__":
    txn1 = Taxon.create(
        name="Mammalia", status=Status.living, common_name="Mammals"
    )
    txn2 = Taxon.create(
        name="Rodentia", status=Status.living, common_name="Rodents",
        parent=txn1
    )
    tnx3 = Taxon.create(
        name="Multituberculata", status=Status.fossil, parent=txn1
    )

    living_taxa = Taxon.select().filter(Taxon.status == Status.living)
    assert living_taxa.count() == 2
    assert {txn.common_name for txn in living_taxa} == {"Mammals", "Rodents"}
    for txn in living_taxa:
        txn.common_name = txn.common_name + "!"

    # Change is immediately visible
    assert txn1.common_name == "Mammals!"
```

## Supported types

The following field types are currently supported:

- Primitive types, e.g., `int`, `str`, `bool`, which are passed
  directly to the database
- Enums, which are converted to their value before being passed to
  the database
- Foreign keys to other clirm models, which are stored as their
  IDs
- Foreign keys to the current class, which can be expressed with `typing.Self`
- Nullable versions of any of the above, expressed by adding `| None`
  to the type

Additional types can be supported by subclassing `Field` and overriding
the `deserialize` and `serialize` methods.

## Backends

For now only SQLite is supported as a backend.

## Changelog

Version 0.1 (April 8, 2024)

- Initial release


            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "clirm",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "database, cli, orm",
    "author": null,
    "author_email": "Jelle Zijlstra <jelle.zijlstra@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/4c/47/6f7d4150223e869967250c16a7f77b0db4daf767906942b717d17e3b8ed6/clirm-0.1.tar.gz",
    "platform": null,
    "description": "# clirm\n\nCommand-Line ORM (clirm) is a library for creating simple ORMs that\ncan be used in command-line programs that allow users to manipulate\nobjects in an interactive context, and that also regularly run scripts\nthat iterate over the entire database.\n\nFeatures include:\n\n- Changes are always committed to the database immediately, so that\n  there is no need to worry about a separate later \"save\" step.\n- There is always only one object per database row, so that users\n  do not need to worry about editing one copy and leaving another\n  ORM object corresponding to the same row unchanged.\n- All columns in a row are always fetched together, to simplify\n  implementation of the above point.\n- Tight integration with the type system. Fields can be declared\n  as `Field[T]()`, where `T` is a normal Python type.\n\nClirm requires that every table has an `id` column containing\na unique identifier.\n\n## Usage\n\nAs an example, we will create a simple database containing\nanimal taxa:\n\n```python\nimport enum\nimport sqlite3\nfrom typing import Self\n\nfrom clirm import Clirm, Field, Model\n\nclass Status(enum.Enum):\n    living = 1\n    recently_extinct = 2\n    fossil = 3\n\nCLIRM = Clirm(sqlite3.connect(\"taxon.db\"))\n\nclass Taxon(Model):\n    clirm = CLIRM\n    clirm_table_name = \"taxon\"\n\n    name = Field[str]()  # string field\n    status = Field[Status]()  # enum field\n    common_name = Field[str | None]()  # nullable string field\n    parent = Field[Self | None]()  # foreign key to self; can also write \"Taxon | None\"\n\nif __name__ == \"__main__\":\n    txn1 = Taxon.create(\n        name=\"Mammalia\", status=Status.living, common_name=\"Mammals\"\n    )\n    txn2 = Taxon.create(\n        name=\"Rodentia\", status=Status.living, common_name=\"Rodents\",\n        parent=txn1\n    )\n    tnx3 = Taxon.create(\n        name=\"Multituberculata\", status=Status.fossil, parent=txn1\n    )\n\n    living_taxa = Taxon.select().filter(Taxon.status == Status.living)\n    assert living_taxa.count() == 2\n    assert {txn.common_name for txn in living_taxa} == {\"Mammals\", \"Rodents\"}\n    for txn in living_taxa:\n        txn.common_name = txn.common_name + \"!\"\n\n    # Change is immediately visible\n    assert txn1.common_name == \"Mammals!\"\n```\n\n## Supported types\n\nThe following field types are currently supported:\n\n- Primitive types, e.g., `int`, `str`, `bool`, which are passed\n  directly to the database\n- Enums, which are converted to their value before being passed to\n  the database\n- Foreign keys to other clirm models, which are stored as their\n  IDs\n- Foreign keys to the current class, which can be expressed with `typing.Self`\n- Nullable versions of any of the above, expressed by adding `| None`\n  to the type\n\nAdditional types can be supported by subclassing `Field` and overriding\nthe `deserialize` and `serialize` methods.\n\n## Backends\n\nFor now only SQLite is supported as a backend.\n\n## Changelog\n\nVersion 0.1 (April 8, 2024)\n\n- Initial release\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Command line-oriented ORM.",
    "version": "0.1",
    "project_urls": null,
    "split_keywords": [
        "database",
        " cli",
        " orm"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "096336aedfd68fbdc26d3f714a019a8dc75be7d6c9415cdfa447cc226d45f496",
                "md5": "794db4a035cb7a5a5033177af5102e4f",
                "sha256": "b124f692e09f95e00d49d8d89aa3169902e999a4b160bbd7e75afd3f051d9ac3"
            },
            "downloads": -1,
            "filename": "clirm-0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "794db4a035cb7a5a5033177af5102e4f",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 7327,
            "upload_time": "2024-04-08T14:23:11",
            "upload_time_iso_8601": "2024-04-08T14:23:11.661031Z",
            "url": "https://files.pythonhosted.org/packages/09/63/36aedfd68fbdc26d3f714a019a8dc75be7d6c9415cdfa447cc226d45f496/clirm-0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4c476f7d4150223e869967250c16a7f77b0db4daf767906942b717d17e3b8ed6",
                "md5": "25ed1da6294b4319e5eeeed81246381f",
                "sha256": "9afb9e7cdb8c6da8fc9e3dd5f40c6754bcd108c4d2056d13e6cd747271e4ac4b"
            },
            "downloads": -1,
            "filename": "clirm-0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "25ed1da6294b4319e5eeeed81246381f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 7372,
            "upload_time": "2024-04-08T14:23:12",
            "upload_time_iso_8601": "2024-04-08T14:23:12.568212Z",
            "url": "https://files.pythonhosted.org/packages/4c/47/6f7d4150223e869967250c16a7f77b0db4daf767906942b717d17e3b8ed6/clirm-0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-08 14:23:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "clirm"
}
        
Elapsed time: 0.20754s