Skip to main content

Lightweight python datamapper ORM (Object-relational mapper).

Project description

Ascetic exists as a super-lightweight datamapper ORM (Object-Relational Mapper) for Python.

Ascetic ORM based on “Data Mapper” pattern. It also supports “Active Record” pattern, but only as a wrapper, the model class is fully free from any service logic. Ascetic ORM follows the KISS principle. Has automatic population of fields from database (see the example below) and minimal size. You do not have to specify the columns in the class. This follows the DRY principle. Ascetic ORM as small as possible.

Inside ascetic.contrib (currently under development) you can find the next solutions:

All extensions support composite primary/foreign keys.

Identity Map” has SERIALIZABLE isolation level by default.

What Ascetic ORM does not? Ascetic ORM does not make any data type conversions (use connection features like this), and does not has “Unit of Work”. I recommend using a Storm ORM, if you need these features.

Ascetic ORM is released under the MIT License (see LICENSE file for details).

This project is currently under development, and not stable. If you are looking for stable KISS-style ORM, pay attention to Storm ORM.

PostgreSQL Example

Using these tables:

CREATE TABLE ascetic_tests_models_author (
    id serial NOT NULL PRIMARY KEY,
    first_name VARCHAR(40) NOT NULL,
    last_name VARCHAR(40) NOT NULL,
    bio TEXT
);
CREATE TABLE books (
    id serial NOT NULL PRIMARY KEY,
    title VARCHAR(255),
    author_id integer REFERENCES ascetic_tests_models_author(id) ON DELETE CASCADE
);

You can configure in one the following ways:

1. Put in your PYTHONPATH file ascetic_settings.py with your settings. See file ascetic/settings.py for more details.

2. Define settings module in evironment variable ASCETIC_SETTINGS.

3. Call ascetic.settings.configure(), for example:

import ascetic.settings.configure
ascetic.settings.configure({
    'DATABASES': {
        'default': {
            'engine': "postgresql",
            'user': "devel",
            'database': "devel_ascetic",
            'password': "devel",
            'debug': True,
            'initial_sql': "SET NAMES 'UTF8';",
        }
    }
})

We setup our objects like so:

from ascetic.model import Model
from ascetic.mappers import get_mapper
from ascetic.relations import ForeignKey, OneToMany

class Author(Model):
    class Mapper(object):
        defaults = {'bio': 'No bio available'}
        validations = {'first_name': (
            lambda v: len(v) > 1 or "Too short first name",
            lambda self, key, value: value != self.last_name or "Please, enter another first name",
        )}

class Book(Model):
    author = ForeignKey(Author, related_name='books')

    class Mapper(object):
        db_table = 'books'

Now we can create, retrieve, update and delete entries in our database. Creation

james = Author(first_name='James', last_name='Joyce')
get_mapper(Author).save(james)  # Datamapper way

u = Book(title='Ulysses', author_id=james.id)
u.save()  # Use ActiveRecord wrapper

Retrieval

a = Author.get(1)
a.first_name # James
a.books      # Returns list of author's books

# Returns a list, using LIMIT based on slice
a = Author.q[:10]   # LIMIT 0, 10
a = Author.q[20:30] # LIMIT 20, 10

Updating

a = Author.get(1)
a.bio = 'What a crazy guy! Hard to read but... wow!'
a.save()

Deleting

a.delete()

SQLBuilder integration

object_list = Book.q.tables(
    (Book.s & Author.s).on(Book.s.author_id == Author.s.id)
).where(
    (Author.s.first_name != 'James') & (Author.s.last_name != 'Joyce')
)[:10]

Query object based on sqlbuilder.smartsql, see more info.

Signals support

  • pre_init

  • post_init

  • pre_save

  • post_save

  • pre_delete

  • post_delete

  • class_prepared

More info

See more info in docs: https://ascetic.readthedocs.io/

Web

You can use Ascetic ORM with lightweight web-frameworks, like wheezy.web, Bottle, Tornado, pysi, etc.

Gratitude

Other projects

See also:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ascetic-0.7.2.40.tar.gz (40.6 kB view details)

Uploaded Source

File details

Details for the file ascetic-0.7.2.40.tar.gz.

File metadata

  • Download URL: ascetic-0.7.2.40.tar.gz
  • Upload date:
  • Size: 40.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ascetic-0.7.2.40.tar.gz
Algorithm Hash digest
SHA256 b313297f1c3377b628155586e093c220cd8a1dfc6c6c7b7aa3e871b0a4f3abe9
MD5 50d752cca178a420b7b8c59abd3d4940
BLAKE2b-256 f91fb99e21acfc7620b2f5189ab1319240aa10483e0bfa5927e7c6afb993840d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page