Skip to content

Instantly share code, notes, and snippets.

💭
trying to get sqlalchemy 1.4 going

mike bayer zzzeek

💭
trying to get sqlalchemy 1.4 going
Block or report user

Report or block zzzeek

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@zzzeek
zzzeek / test.py
Last active Mar 4, 2020
using oslo.db enginefacade with API methods that retry full transactions on failure
View test.py
"""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 Mar 2, 2020
pep 409 / 415 don't really solve the problem for libraries
View test.py
"""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.
View gist:64239e16d25ff799c8ea3276219ac239
###################################################################
# 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 Jan 9, 2020
Copy-on-operate vs. copy on evaluate
View coovscoe.py
"""
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.
"""
View outerjoin.py
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 Oct 5, 2019
what happens if you have duplicate label names in a derived selectable
View gist:be2daac39dd5c5079003fa0d80404d47
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 Aug 5, 2019
The SQL is just as easy as an ORM challenge
View sql.py
""" "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 Jun 11, 2019
PyMySQL + eventlet authenticaiton timeout
View eventlet_pymysql.py
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
Python getargspec, getfullargspec, Signature performance comparison
View test.py
# 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 Apr 28, 2019
buildything with lambdas
View buildy_w_lambdas.py
# 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
You can’t perform that action at this time.