This file contains 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
"""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 |
This file contains 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
"""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. |
This file contains 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
################################################################### | |
# OLD WAY: | |
q = session.query(User).options(selectinload(User.addresses)) | |
# result_iter is an iterator | |
result_iter = iter(q.yield_per(5)) | |
################################################################### |
This file contains 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
""" | |
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. | |
""" |
This file contains 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 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 |
This file contains 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
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) |
This file contains 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
""" "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. |
This file contains 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 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. |
This file contains 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
# 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 |
This file contains 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
# 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 |