Skip to content

Instantly share code, notes, and snippets.

@pawl
pawl / already_in_session_test.py
Last active March 15, 2017 03:35
tests whether sqlalchemy automatically uses relations loaded into session
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
@pawl
pawl / in_loading.py
Created March 9, 2017 07:09
testing in_ loading strategy from: http://stackoverflow.com/a/39073859
from sqlalchemy import create_engine, Column, ForeignKey, Integer, TEXT
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True,
echo=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
import codecs
import requests
import time
# this could be different, but it works for my application, but the content-type definitely matters
fileHeaders = {
'Content-Type': 'application/octet-stream;charset=text/html;charset=UTF-8'
}
url = 'www.yourfile.com/your.zip'
@pawl
pawl / gist:65d525f3d9defcb34870ad8a815a0b09
Last active February 16, 2017 05:45
saving zip file
sess = requests.session()
# you may have other get/post requests before you can get to the download page for the file
url = 'https://example.com/report.zip'
# this may be different for you, i just copied this from an existing request (using fiddler)
file_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)',
@pawl
pawl / zen_of_eager_loading.py
Last active March 9, 2017 07:07
zen of eager loading
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
@pawl
pawl / joined_subquery.py
Last active March 9, 2017 07:07
sqlalchemy joined subquery example
from sqlalchemy import create_engine, Column, ForeignKey, Integer
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root@localhost/test?charset=utf8mb4',
convert_unicode=True)
session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
@pawl
pawl / 1_uuid_type.py
Created February 13, 2017 01:13
sqlalchemy-utils UUIDType performance issues example
import cProfile
import pstats
import contextlib
import uuid
from sqlalchemy import create_engine, Column, ForeignKey, Binary
from sqlalchemy.orm import relationship, scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import UUIDType
@pawl
pawl / 1_sqlalchemy_session_execute.py
Last active January 20, 2017 01:13
example showing whether commit is required for session.execute
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
engine = create_engine('mysql://root@localhost/test')
db_session = scoped_session(sessionmaker(bind=engine))
db_session.execute('''
INSERT INTO user (first_name, last_name, username, email)
VALUES ("blah", "blah", "blah", "blah@blah.com");
''')
<Node id="4" name="Front Door Lock" location="" basic="4" generic="64" specific="3" type="Secure Keypad Door Lock" listening="false" frequentListening="true" beaming="true" routing="true" max_baud_rate="40000" version="4" secured="true" query_stage="Complete">
<Manufacturer id="3b" name="Schlage">
<Product type="6341" id="5044" name="BE469NXCEN Touchscreen DeadBolt" />
</Manufacturer>
<CommandClasses>
<CommandClass id="32" name="COMMAND_CLASS_BASIC" version="1" request_flags="4" issecured="true" mapping="98">
<Instance index="1" />
</CommandClass>
<CommandClass id="34" name="COMMAND_CLASS_APPLICATION_STATUS" version="1" request_flags="4" innif="true">
<Instance index="1" />
@pawl
pawl / gist:382c655f31dad631ab29775a6c8cad05
Created November 27, 2016 06:04
new Schema object benchmark
from jsonschema import validate, Schema
# A sample schema, like what we'd get from json.load()
schema = {
"type": "object",
"properties": {
"price": {"type": "number"},
"name": {"type": "string"},
},
}