SyPy June 2013
● Introduction to SQLAlchemy - Roger Barnes
● Using Robot Framework for testing,
communication and world domination - Dylan Jay
● Configuration management with Salt Stack -
Daniel Bryan
● + lightning talks
● Pub
SyPy June 2013
Next SyPy - Aug 1, 2013
http://bit.ly/11kHTmB
Introduction to
SQLAlchemy
Roger Barnes
SyPy June 2013
roger@mindsocket.com.au @mindsocket
About Me
15 years software development
4 years Python/Django
4 months SQLAlchemy
SQLAlchemy is...
Python SQL toolkit
&
Object Relational Mapper
About SQLAlchemy
● Full featured
● Mature, robust, documented, maintained
● Flexible
– Use what you need
– Extend when you need
– Database oriented
– Doesn't dictate schema design
– ...
Enterprise!
DB support
● SQLite
● Postgresql
● MySQL
● Oracle
● MS-SQL
● Firebird
● Sybase
● ...
Python support
cPython 2.5+
cPython 3+
Jython 2.5+
Pypy 1.5+
Compared with Django ORM?
● Both are good
● Key philosophical design difference
Django → Web applications
SQLAlchemy → Database applications
Compared with Django ORM?
● Django ORM
– easier to learn
– less flexible
– well suited for most web apps
– comes with a free admin app
Compared with Django ORM?
● SQLAlchemy
– harder to learn (but only a little)
– massively flexible
– suited to a wider range of
applications, especially those that
benefit from the power of an
RDBMS
Project Structure
SQLAlchemy is like an onion...
… or a cake
Structure
Core
● Abstraction over Python's DBAPI
● SQL language via generative
Python expressions
Core
● Good for DB performance
– bulk operations
– complex queries
– fine-tuning
– connection/tx management
Core
● Schema
– Managed by a Metadata object
– Declare tables, columns and
other attributes
– Closely aligned with Data
Definition Language
Core
● SQL Expressions
– INSERT, UPDATE, DELETE
– SELECT, joins, grouping
– Functions, expressions, operators
● Connection pooling
● Backend DB dialects
Demo
ORMs in General
● Simple case – translate between a
domain object and a table row
● More complex – arbitrary rows
mapped to an object
● Handle relationships
ORMs in General
● Query in terms of objects
● Represent class inheritance
● Data validation
SQLAlchemy ORM
● Layer on top of core
● Session based
● Session manages object state
SQLAlchemy ORM
● 2 ways to use
– Classical Mapping
● Define table
● Define object
● Configure mapper
– Modern - Declarative
● Shorthand combining table metadata and
class mapping
● Similar to Django ORM
Demo
Questions?
● Other resources
– SQLAlchemy - http://sqlalchemy.org/
– Longer (better) introduction -
http://bit.ly/13UQXVV
● Contact
roger@mindsocket.com.au
@mindsocket

Introduction to SQL Alchemy - SyPy June 2013

  • 1.
    SyPy June 2013 ●Introduction to SQLAlchemy - Roger Barnes ● Using Robot Framework for testing, communication and world domination - Dylan Jay ● Configuration management with Salt Stack - Daniel Bryan ● + lightning talks ● Pub
  • 3.
    SyPy June 2013 NextSyPy - Aug 1, 2013 http://bit.ly/11kHTmB
  • 4.
    Introduction to SQLAlchemy Roger Barnes SyPyJune 2013 roger@mindsocket.com.au @mindsocket
  • 5.
    About Me 15 yearssoftware development 4 years Python/Django 4 months SQLAlchemy
  • 6.
    SQLAlchemy is... Python SQLtoolkit & Object Relational Mapper
  • 7.
    About SQLAlchemy ● Fullfeatured ● Mature, robust, documented, maintained ● Flexible – Use what you need – Extend when you need – Database oriented – Doesn't dictate schema design – ...
  • 8.
  • 9.
    DB support ● SQLite ●Postgresql ● MySQL ● Oracle ● MS-SQL ● Firebird ● Sybase ● ...
  • 10.
    Python support cPython 2.5+ cPython3+ Jython 2.5+ Pypy 1.5+
  • 11.
    Compared with DjangoORM? ● Both are good ● Key philosophical design difference Django → Web applications SQLAlchemy → Database applications
  • 12.
    Compared with DjangoORM? ● Django ORM – easier to learn – less flexible – well suited for most web apps – comes with a free admin app
  • 13.
    Compared with DjangoORM? ● SQLAlchemy – harder to learn (but only a little) – massively flexible – suited to a wider range of applications, especially those that benefit from the power of an RDBMS
  • 14.
    Project Structure SQLAlchemy islike an onion... … or a cake
  • 15.
  • 16.
    Core ● Abstraction overPython's DBAPI ● SQL language via generative Python expressions
  • 17.
    Core ● Good forDB performance – bulk operations – complex queries – fine-tuning – connection/tx management
  • 18.
    Core ● Schema – Managedby a Metadata object – Declare tables, columns and other attributes – Closely aligned with Data Definition Language
  • 19.
    Core ● SQL Expressions –INSERT, UPDATE, DELETE – SELECT, joins, grouping – Functions, expressions, operators ● Connection pooling ● Backend DB dialects
  • 20.
  • 21.
    ORMs in General ●Simple case – translate between a domain object and a table row ● More complex – arbitrary rows mapped to an object ● Handle relationships
  • 22.
    ORMs in General ●Query in terms of objects ● Represent class inheritance ● Data validation
  • 23.
    SQLAlchemy ORM ● Layeron top of core ● Session based ● Session manages object state
  • 24.
    SQLAlchemy ORM ● 2ways to use – Classical Mapping ● Define table ● Define object ● Configure mapper – Modern - Declarative ● Shorthand combining table metadata and class mapping ● Similar to Django ORM
  • 25.
  • 26.
    Questions? ● Other resources –SQLAlchemy - http://sqlalchemy.org/ – Longer (better) introduction - http://bit.ly/13UQXVV ● Contact roger@mindsocket.com.au @mindsocket