Table Of Contents

Previous topic

Macaron: Python O/R Mapper

Next topic

Tutorial

This Page

Macaron: O/R Mapper

Overview

Macaron is a small and simple object-relational mapper (ORM) for SQLite and Python. It is distributed as a single file module which has no dependencies other than the Python Standard Library.

Macaron provides provides easy access methods to SQLite database. And it supports Bottle web framework through plugin mechanism.

Example:

>>> import macaron
>>> macaron.macaronage(dbfile="members.db")
>>> team = Team.create(name="Houkago Tea Time")
>>> team.members.append(first_name="Ritsu", last_name="Tainaka", part="Dr")
<Member object 1>
>>> mio = team.members.append(first_name="Mio", last_name="Akiyama", part="Ba")
>>> print mio
<Member 'Mio Akiyama : Ba'>
>>> for member in team.members: print member
...
<Member 'Ritsu Tainaka : Dr'>
<Member 'Mio Akiyama : Ba'>

Macaron supports Many-To-One relationships and reverse reference. Many-To-Many relationships have not been supported yet. To realize simple implementation, Macaron does not provide methods for creation of tables.

MacaronPlugin class for Bottle web framework is implemented.

External resources

Installation and Dependencies

tar zxvf macaron-0.3.0.tar.gz
cd macaron-0.3.0
python setup.py

or using easy_install:

easy_install macaron

Use for Web Applications

Macaron in the Bottle

Bottle is a lightweight web framework for Python. Macaron can be used with Bottle through MacaronPlugin, which is tested with Bottle 0.10.9.

Example

#!/usr/bin/env python
from bottle import *
import macaron

install(macaron.MacaronPlugin("address.db"))

class Address(macaron.Model):
    _table_name = "address"

@route("/hello")
def index():
    addr = Address.get(1)
    return "<h1>Hello!!</h1>My address is %s" % addr.address

run(host="localhost", port=8080)

Implementation

MacaronPlugin create lazy connection. So the sqlite3.Connection object is create at call Macaron methods. In case of no use the methods in bottle.route(), any connection is created.

License

Code and documentation are available according to the MIT License.

Copyright (c) 2012, Nobuo Okazaki.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.