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 / size.py
Created February 16, 2022 16:07
get the actual recursive size of arbitrary objects
# taken from https://code.activestate.com/recipes/577504/ **plus**
# the comment at https://code.activestate.com/recipes/577504/#c5
from sys import getsizeof, stderr
from itertools import chain
from collections import deque
from reprlib import repr
def total_size(o, handlers={}, verbose=False):
@zzzeek
zzzeek / A1_all_sqlalchemy.py
Last active January 27, 2022 09:10
comparing SQLAlchemy 1.4 async to "databases"
import asyncio
import contextlib
import cProfile
import io
import pstats
import random
from sqlalchemy import cast
from sqlalchemy import Column
from sqlalchemy import Integer
@zzzeek
zzzeek / gist:8443477
Last active January 27, 2022 03:18
expands upon the SQLAlchemy "test rollback fixure" at http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#joining-a-session-into-an-external-transaction to also support tests that have any combination of ROLLBACK/COMMIT within them, by ensuring that the Session always runs transactions inside of a savepoint.
from sqlalchemy import Column, Integer, create_engine
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
# a model
class Thing(Base):
__tablename__ = 'thing'
id = Column(Integer, primary_key=True)
@zzzeek
zzzeek / rolling_col_upgrade.py
Last active December 16, 2021 17:27
keeping two columns in sync across an old and new schema version
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
e = create_engine("sqlite://", echo=True)
# first, we're in the old version of the app
OldBase = declarative_base()
@zzzeek
zzzeek / test.py
Created October 7, 2021 01:52
adapt_on_lookup_mapping demo
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy.orm import aliased
from sqlalchemy.orm import declarative_base
@zzzeek
zzzeek / plain_asyncio.py
Last active November 13, 2021 00:01
An asyncio program that runs rows into a Postgresql database
"""A plain asyncio program that uses asyncpg to run some rows into a table and
select them.
This is a "control" program which we will compare to the one which uses calls
from an implicit IO greenlet at
https://gist.github.com/zzzeek/4e89ce6226826e7a8df13e1b573ad354.
Performance against a PG database over a wired network
Ran 40000 records in 40 concurrent requests, Total time 5.560306
@zzzeek
zzzeek / test.py
Last active October 7, 2021 01:52
adapt_on_names demo
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy.orm import aliased
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import Session
@zzzeek
zzzeek / test.py
Created June 3, 2021 16:29
python 3.10.0b2 segfault
import greenlet
import sqlite3
class _ErrorContainer(object):
error = None
def _expect_raises_fn(fn):
ec = _ErrorContainer()
"""this is getting close but SQLAlchemy is still not instrumenting __init__ such
that it can intercept Pydantic's operations, in particular that list coming
in which it wants to convert to instrumented list.
this probably could be made to work with some more effort but it will
be quite hacky in the end, not really worth it unless there was
some explicit support in pydantic.
"""
@zzzeek
zzzeek / test.py
Last active January 1, 2021 16:54
async sqlalchemy w/ yappi
import asyncio
import contextlib
import random
import yappi
# install sqlalchemy from github master:
# pip install git+https://github.com/sqlalchemy/sqlalchemy/
from sqlalchemy import cast
from sqlalchemy import Column