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 / async_to_greenlet_to_async.py
Last active January 22, 2024 15:35
Write a program in asyncio, that calls into a library that knows nothing about asyncio, which then calls out to a database adapter that is only asyncio
"""Proof of concept of an adapter that will wrap a blocking IO library in
greenlets, so that it can be invoked by asyncio code and itself make calls out
to an asyncio database library.
hint: any Python ORM or database abstraction tool written against the DBAPI
could in theory allow asyncio round trips to a pure async DB driver like
asyncpg.
The approach here seems too simple to be true and I did not expect it to
collapse down to something this minimal, so it is very possible I am totally
@zzzeek
zzzeek / simpler_asyncio.py
Last active October 12, 2023 13:53
a simpler version of async->greenlet->async written by @CaselIT
"""This is a simpler version of the greenlet
example at https://gist.github.com/zzzeek/4e89ce6226826e7a8df13e1b573ad354
Instead of the "await" keyword, we use the "await_()" function to interact with
the greenlet context. the greenlet context itself is 23 lines of code right
here.
"""
import asyncio
@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 / asyncio_plus_greenlet.py
Last active July 5, 2023 16:32
An asyncio program that runs rows into a Postgresql database, using blocking style code to actually run the database commands
"""This program is exactly the same as that of
https://gist.github.com/zzzeek/33943060f7a08cf9e82bf8df1f0f75de ,
with the exception that the add_and_select_data function is written in
synchronous style.
UPDATED!! now includes refinements by @snaury and @Caselit . SIMPLER
AND FASTER!!
@zzzeek
zzzeek / test.py
Last active June 7, 2023 20:42
issues with pydantic dataclasses / sqlalchemy
from __future__ import annotations
from typing import TYPE_CHECKING
import pydantic.dataclasses
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
@zzzeek
zzzeek / record.sh
Created December 29, 2022 15:01
the record.sh script
#!/usr/bin/env bash
# notes:
# tuning camera: gtk-v4l package, v4l2-ctl
# we also have:
# ffmpeg, mpg123
# linux
#RESOLUTION="640x480"
@zzzeek
zzzeek / msg373145.rst
Last active October 22, 2022 12:36
asyncio support for SQLAlchemy (and Flask, and any other blocking-IO library)

This is a cross post of something I just posted on the Python bug tracker at https://bugs.python.org/msg373145.

I seem to have two cents to offer so here it is. An obscure issue in the Python bug tracker is probably not the right place for this so consider this as an early draft of something that maybe I'll talk about more elsewhere.

> This basically divides code into two islands - async and non-async

@zzzeek
zzzeek / gist:8479592
Last active October 12, 2022 02:27
cx_oracle vs. SQLAlchemy core, revised
from sqlalchemy.testing.profiling import profiled
from sqlalchemy import create_engine
from sqlalchemy import event
engine = create_engine('oracle://scott:tiger@localhost/xe')
# disregard all output type handling - Python unicodes
# will no longer be supported
@event.listens_for(engine, "connect")
@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 / gist:d608913fce4d14e924553c91f4c6ecc0
Created June 7, 2022 21:03
list(dict.items()) atomic or not
import threading
def one(obj):
while True:
skip = set()
try:
{k: v for k, v in obj.__dict__.items() if k not in skip}
except RuntimeError as err:
print("%s" % err)