Skip to main content

SQLAlchemy helpers for working in Tornado

Project description

https://badge.fury.io/py/tornado-sqlalchemy.svg https://travis-ci.org/siddhantgoel/tornado-sqlalchemy.svg?branch=stable https://readthedocs.org/projects/tornado-sqlalchemy/badge/?version=latest

Python helpers for using SQLAlchemy with Tornado.

Installation

$ pip install tornado-sqlalchemy

Usage

from tornado.gen import coroutine
from tornado.web import Application, RequestHandler
from tornado_sqlalchemy import as_future, make_session_factory, SessionMixin

class NativeCoroutinesRequestHandler(SessionMixin, RequestHandler):
    async def get(self):
        with self.make_session() as session:
            count = await as_future(session.query(UserModel).count)

        self.write('{} users so far!'.format(count))

class GenCoroutinesRequestHandler(SessionMixin, RequestHandler):
    @coroutine
    def get(self):
        with self.make_session() as session:
            count = yield as_future(session.query(UserModel).count)

        self.write('{} users so far!'.format(count))

class SynchronousRequestHandler(SessionMixin, RequestHandler):
    def get(self):
        with self.make_session() as session:
            count = session.query(UserModel).count()

        self.write('{} users so far!'.format(count))

handlers = (
   (r'/native-coroutines', NativeCoroutinesRequestHandler),
   (r'/gen-coroutines', GenCoroutinesRequestHandler),
   (r'/sync', SynchronousRequestHandler),
)

app = Application(
   handlers,
   session_factory=make_session_factory('postgres://user:password@host/database')
)

Documentation

Documentation is available at Read The Docs.

Development

To work on this package, please make sure you have Python 3.5+ and pipenv installed.

  1. Git clone the repository - git clone https://github.com/siddhantgoel/tornado-sqlalchemy
  2. Install the packages required for development - pipenv install --dev
  3. That’s basically it. You should now be able to run the test suite - py.test tests/.

Project details


Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
tornado_sqlalchemy-0.5.0-py3-none-any.whl (4.2 kB) Copy SHA256 hash SHA256 Wheel py3
tornado_sqlalchemy-0.5.0.tar.gz (5.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page