Change the future

Pony Object-Relational Mapper

Alexey Malashkevich

Audience level:
Intermediate
Category:
Databases/NoSQL

Description

Pony is an object-relational mapper implemented in Python. It allows writing advanced SQL queries using plain Python in the form of a generator expression. This way queries look very concise.

Abstract

Pony is an object-relational mapper implemented in Python. Pony allows a developer to represent and manipulate data using entities in an easy and intuitive way. An entity is an object plus its relationships.

The main feature of Pony is to provide a method to write declarative queries to databases in pure Python using generators. For this purpose Pony analyzes the abstract syntax tree of a generator and translates it to its SQL equivalent.

Following is a sample of a query in Pony:

fetch(p for p in Product 
        if p.name.startswith('A') and p.cost <= 1000)

This query translates to SQL using a specific database dialect. Currently Pony works with SQLite, MySQL, PostgreSQL and Oracle databases.

Using entities instead of tables avoids the necessity of explicit joins: one can simply traverse relationships between entities. This way queries are concise and easier to understand than SQL. Developers also have an option of using pure SQL when necessary.