Skip to main content

A dataframe-base ORM

Project description

Jardin is a pandas.DataFrame-based ORM for Python applications.

Getting started

In your working directory (the root of your app), create a file named jardin_conf.py:

# jardin_conf.py

DATABASES = {
  'my_first_database': 'https://username:password@database.url:port',
  'my_second_database': 'https://username:password@database.url:port'
}

LOG_LEVEL = logging.DEBUG

WATERMARK = 'My Great App'

Then, in your app, say you have a table called users:

# app.py
import jardin

class Users(jardin.Model):
  db_names = {'read': 'my_first_database', 'write': 'my_second_database'}

In the console:

>>> from app import Users
>>> users = Users.last(4)
# /* My Great App */ SELECT * FROM users ORDER BY u.created_at DESC LIMIT 4;
>>> users
id   name    email              ...
0    John    john@beatl.es      ...
1    Paul    paul@beatl.es      ...
2    George  george@beatl.es    ...
3    Ringo   ringo@beatl.es     ...

The resulting object is a pandas dataframe:

>>> import pandas
>>> isinstance(users, pandas.DataFrame)
True
>>> isinstance(users, jardin.Model)
True

Queries

SELECT queries

Here is the basic syntax to select records from the database

>>> users = Users.select(select = ['id', 'name'], where = {'email': 'paul@beatl.es'},
                         order = 'id ASC', limit = 1)
# /* My Great App */ SELECT u.id, u.name FROM users u WHERE u.email = 'paul@beatl.es' ORDER BY u.id ASC LIMIT 1;
>>> users
id   name
1    Paul

Arguments

  • select – The list of columns to return. If not provided, all columns will be returned.

  • where – conditions. Many different formats can be used to provide conditions. See docs.

  • inner_join, left_join – List of tables to join with their join condition. Can also be a list of classes if the appropriate associations have been declared. See docs.

  • order – order clause

  • limit – limit clause

  • group – grouping clause

  • scopes – list of pre-defined scopes. See docs.

where argument

Here are the different ways to feed a condition clause to a query. * where = "name = 'John'" * where = {'name': 'John'} * where = {'id': (0, 3)} – selects where id is between 0 and 3 * where = {'id': [0, 1, 2]} – selects where id is in the array * where = [{'id': (0, 10), 'instrument': 'drums'}, ["created_at > %(created_at)s", {'created_at': '1963-03-22'}]]

inner_join, left_join arguments

The simplest way to join another table is as follows

>>> Users.select(inner_join = ["instruments i ON i.id = u.instrument_id"])

If you have configured your models associations, see here, you can simply pass the class as argument:

>>> Users.select(inner_join = [Instruments])

Individual record selection

You can also look-up a single record by id:

>>> Users.find(1)
# /* My Great App */ SELECT * FROM users u WHERE u.id = 1;
{'id': 1, 'name': 'Paul', 'email': 'paul@beatl.es', ...}

Note that the returned object is a Record object which allows you to access attributes in those way:

>>> user['name']
Paul
>>> user.name
Paul

INSERT queries

>>> user = Users.insert(name = 'Pete', email = 'pete@beatl.es')
# /* My Great App */ INSERT INTO users (name, email) VALUES ('Pete', 'pete@beatl.es') RETURNING id;
# /* My Great App */ SELECT u.* FROM users WHERE u.id = 4;
>>> user
id   name    email
4    Pete    pete@beatl.es

UPDATE queries

>>> users = Users.update(values = {'hair': 'long'}, where = {'name': 'John'})
# /* My Great App */ UPDATE users u SET (u.hair) = ('long') WHERE u.name = 'John' RETURNING id;
# /* My Great App */ SELECT * FROM users u WHERE u.name = 'John';

DELETE queries

>>> Users.delete(where = {'id': 1})
# /* My Great App */ DELETE FROM users u WHERE u.id = 1;

Associations

It is possible to define associations between models. For example, if each user has multiple instruments:

# app.py

import jardin

class MyModel(jardin.Model):
  db_names = {'read': 'my_first_database', 'write': 'my_second_database'}

class Instruments(MyModel):
  belongs_to = {'users': 'user_id'}

class Users(MyModel):
  has_many = [Instruments]

and then you can query the associated records:

>>> users = Users.select()
# /* My Great App */ SELECT * FROM users u;
>>> instruments = users.instruments()
# /* My Great App */ SELECT * FROM instruments i WHERE i.id IN (0, 1, ...);

Or you can declare joins more easily

>>> users = Users.select(inner_join = [Instruments])

Scopes

Queries conditions can be generalized across your app:

# app.py

class Users(jardin.Model):
  scopes = {
    'alive': {'name': ['Paul', 'Ringo']},
    'guitarists': {'name': ['John', 'George']}
  }

The key is the name of the scope, and the value is the conditions to be applied. Anything that can be fed to the where argument of Model#select can be used to define a scope.

Use them as such:

>>> users = Users.select(scopes = ['alive'], ...)
# /* My Great App */ SELECT * FROM users u WHERE u.name IN ('Paul', 'Ringo') AND ...;

Misc

Watermark and trace

Multiple databases

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

jardin-0.13.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jardin-0.13.0-py2-none-any.whl (17.9 kB view details)

Uploaded Python 2

File details

Details for the file jardin-0.13.0.tar.gz.

File metadata

  • Download URL: jardin-0.13.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jardin-0.13.0.tar.gz
Algorithm Hash digest
SHA256 24040ca077fb696732ef72b9a167f4d1f31beece65cce58670cc84f245548090
MD5 4fe800309bed7f1525e68a8c80938c7f
BLAKE2b-256 0a5bda0e812ef22be9be8eff5c3f4755a6d87f05e2a38a14d84ba0bd00ebfac5

See more details on using hashes here.

File details

Details for the file jardin-0.13.0-py2-none-any.whl.

File metadata

File hashes

Hashes for jardin-0.13.0-py2-none-any.whl
Algorithm Hash digest
SHA256 04e4d9a038e33b7b058626da4a2b4a0ade5e96c3a7a9d34a676783563a3e2370
MD5 ba6502ac68bd4295d73e338b298fe810
BLAKE2b-256 b5efba2aba05539fcea4e93c8934cada8fe36e209a426e821948522a3c0fe403

See more details on using hashes here.

Supported by

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