This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import time | |
| from sqlalchemy import Column, Integer, TIMESTAMP, func, String, ForeignKey | |
| from sqlalchemy import create_engine | |
| from sqlalchemy import event | |
| from sqlalchemy.ext.declarative import as_declarative, declared_attr, AbstractConcreteBase | |
| from sqlalchemy.orm import scoped_session, sessionmaker, relationship | |
| engine = create_engine('sqlite://', convert_unicode=True, echo=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ########## CASE IN UPDATE STATEMENT ############ | |
| from sqlalchemy import case | |
| # single value modification (the 'else' is not mandatory) | |
| session.query(User).update({User.status : case([(User.status == "pending", "approved")], else_=User.status)}, False) | |
| # multiple values modification | |
| session.query(User).update({User.status : case([(User.status == "pending", "approved"), | |
| (User.status == "waiting", "deprecated_status")])}, False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sqlalchemy as sa | |
| import sqlalchemy.orm as orm | |
| from sqlalchemy.ext.declarative import declarative_base | |
| from sqlalchemy.ext.declarative import declared_attr | |
| from sqlalchemy.orm import scoped_session, sessionmaker | |
| DBSession = scoped_session(sessionmaker()) | |
| class BaseMixin(object): | |
| query = DBSession.query_property() | |
| id = sa.Column(sa.Integer, primary_key=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # iterative implementation of binary search in Python | |
| def binary_search(a_list, item): | |
| """Performs iterative binary search to find the position of an integer in a given, sorted, list. | |
| a_list -- sorted list of integers | |
| item -- integer you are searching for the position of | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # iterative implementation of binary search in Python | |
| def binary_search(a_list, item): | |
| """Performs iterative binary search to find the position of an integer in a given, sorted, list. | |
| a_list -- sorted list of integers | |
| item -- integer you are searching for the position of | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from datetime import date | |
| from sqlalchemy import cast, DATE | |
| Match.query.filter(cast(Match.date_time_field, DATE)==date.today()).all() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from sqlalchemy import Column, Time, Integer, Boolean, create_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.ext.declarative import declarative_base | |
| # SQL statements and expressions API | |
| # cf. http://docs.sqlalchemy.org/en/rel_0_7/core/expression_api.html | |
| from sqlalchemy import and_, extract |