Skip to content

Instantly share code, notes, and snippets.

View zzzeek's full-sized avatar
💭
SQLAlchemy 2.0 is released!

Michael Bayer zzzeek

💭
SQLAlchemy 2.0 is released!
View GitHub Profile
@zzzeek
zzzeek / test.py
Last active March 4, 2020 16:41
using oslo.db enginefacade with API methods that retry full transactions on failure
"""oslo.db enginefacade with intelligent retries demonstration.
This script illustrates three API functions:
* run_api_call_always_outermost - this API call is always outside of any
transactional context. A decorator asserts that this is the case.
Database errors that occur within it will trigger
the whole function to be retried.
* run_internal_api_call - this API call is always called inside of a
@zzzeek
zzzeek / test.py
Last active March 2, 2020 16:18
pep 409 / 415 don't really solve the problem for libraries
"""pep-409 and pep-415 add support for "raise exception from None" so that
libraries need not expose internal exception cases.
However, this is too broad in scope as it breaks user-land code that would
like to still see the context of where *their* application was handling
the error.
This makes pep-409/415 more or less useless and the problem they'd like
to solve still remains unsolved.
###################################################################
# OLD WAY:
q = session.query(User).options(selectinload(User.addresses))
# result_iter is an iterator
result_iter = iter(q.yield_per(5))
###################################################################
@zzzeek
zzzeek / coovscoe.py
Last active January 9, 2020 19:37
Copy-on-operate vs. copy on evaluate
"""
this demonstration illustrates how a library like pandas could
theoretically (or maybe it does already with some flag?) not require
a copy of the data when an operation takes place on the structure.
this is based on the article https://pythonspeed.com/articles/minimizing-copying/
which illustrates specific programming techniques that can be used with a numpy
array in order to minimize data copying; this gist presents an alternative by
which the library could perhaps sheild this implementation detail from the
end-user.
"""
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
@zzzeek
zzzeek / gist:be2daac39dd5c5079003fa0d80404d47
Created October 5, 2019 22:18
what happens if you have duplicate label names in a derived selectable
test=> create table foo (a integer, b integer, c integer);
CREATE TABLE
test=> insert into foo (a, b, c) values (1, 2, 3);
INSERT 0 1
test=> -- dupe column names are OK
test=> select a as a, b as b, c as c, a as a, b as a, b as b from foo;
a | b | c | a | a | b
---+---+---+---+---+---
1 | 2 | 3 | 1 | 2 | 2
(1 row)
@zzzeek
zzzeek / sql.py
Last active August 15, 2023 10:00
The SQL is just as easy as an ORM challenge
""" "Writing SQL is just as fast as using an ORM" proof of concept
Below is a simple Python object model, where we represent a database that
stores the names of employees at a company, some of whom are "engineers",
and a list of the jobs they do and the programming languages they use.
We'd like to persist the state represented by this Python object model
in a relational database, using our Python objects as a start. Then we'd
like to write SQL queries for rows in this database, and we get back instances
of Python objects exactly as they were created.
@zzzeek
zzzeek / eventlet_pymysql.py
Created June 11, 2019 20:00
PyMySQL + eventlet authenticaiton timeout
import random
import time as blocking_time
import eventlet
import pymysql
# monkeypatch Python network libraries so that they are non blocking. all
# Python code that uses the pure Python network IO functions will be non
# blocking, where all IO waiting will defer back to eventlet where it will
# pass off work to another greenlet.
@zzzeek
zzzeek / test.py
Created May 17, 2019 02:28
Python getargspec, getfullargspec, Signature performance comparison
# So interestingly, Signature seems to be twice as fast in Python 3.6 and 3.7 as it
# was in Python 3.3 and 3.4. However, it is still 6-18 times slower than the old
# getargspec or getfullargspec that was in Python 3.3.
Python 2.7 getargspec (doesn't use Signature) : specimen_one (6 args) : 0.0503
Python 2.7 getargspec (doesn't use Signature) : specimen_two (0 args) : 0.0496
Python 2.7 getargspec (doesn't use Signature) : specimen_three (6 args) : 0.0500
Python 2.7 getargspec (doesn't use Signature) : specimen_four (13 args) : 0.0652
Python 3.3 getfullargspec (doesn't use Signature) : specimen_one (6 args) : 0.0539
@zzzeek
zzzeek / buildy_w_lambdas.py
Last active October 31, 2020 06:34
buildything with lambdas
# note: we are using SQLAlchemy that includes _cache_key
# currently at:
# https://gerrit.sqlalchemy.org/#/c/sqlalchemy/sqlalchemy/+/1204/5/
import typing
from sqlalchemy import bindparam
from sqlalchemy import Column
from sqlalchemy import func
from sqlalchemy import inspection