Skip to content

Instantly share code, notes, and snippets.

View drocco007's full-sized avatar

Daniel Rocco drocco007

  • BrightLink Technology
  • Atlanta, GA
View GitHub Profile
@drocco007
drocco007 / conftest.py
Last active December 16, 2015 16:49
pytest fixture for SQLAlchemy tests to wrap each test in its own transaction, which is automatically rolled back when the test completes. Drop into a test module to enable for each test in that module. Or, put it into a package's conftest.py to enable it for every test in that package.
@pytest.fixture(autouse=True)
def close_transaction(request):
session.begin(subtransactions=True)
request.addfinalizer(session.rollback)
@drocco007
drocco007 / manager.py
Last active December 18, 2015 17:39
initial thoughts on creating test extension managers
class TestExtensionManagerMixin(object):
"""Mixin base class for test extension managers."""
def over(self, extensions):
"""Set the Extensions used by this manager.
:param extensions: Pre-configured Extension instances to use
instead of loading them from entry points.
:type extensions: list of :class:`~stevedore.extension.Extension`
:returns: this extension manager instance to allow chaining on
@drocco007
drocco007 / comm.py
Created August 5, 2013 12:08
Python generator rate limiter using token bucket
from time import time, sleep
_128k = 128 * 1024
_256k = 256 * 1024
_512k = 512 * 1024
_1024k = 1024 * 1024
class TokenBucket(object):
@drocco007
drocco007 / block_test.py
Last active December 21, 2015 05:09
Kajiki basic/nested blocks
parent = u"""
<!DOCTYPE html>
<html>
<body>
<div py:block="content">Eat your veggies!</div>
</body>
</html>
"""
child = u"""
@drocco007
drocco007 / _question.rst
Last active January 11, 2016 12:20
Short version: what is the best way to create fixture factories?

I'm working on a user search feature and would like some feedback about the best way to structure the creation of test data using pytest. Below I have defined a test file, test_basic.py, that defines fixture functions to create users and add them to my "database" of users:

@pytest.fixture
def user_alice():
    alice = User('Alice')
    users.append(alice)
    return alice
@drocco007
drocco007 / conftest.py
Last active January 25, 2016 22:45
py.test + SQLAlchemy transactional testing
import pytest
from turbogears.database import get_engine, session
@pytest.fixture(autouse=True)
def wrap_transaction(request):
"""Automatically wrap test cases in a transaction, rolled back on
completion. Uses an "external," non-ORM transaction that is durable
against inner transactions used by the ORM. Thanks to @sontek
# ∅MQ multi-publisher message bus
#
# modeled on http://zguide.zeromq.org/py:msgqueue but with XPUB/XSUB instead
# of ROUTER/DEALER
#
# usage: julia bus.jl <inbound_port> <outbound_port>
#
using ZMQ
"""pywin32 native dialog demo
Explanation here: http://auralbits.blogspot.com/2014/02/a-native-win32-application-in-python.html #noqa
The core of this demo comes from the pywin32 win32gui_dialog demo:
http://pywin32.hg.sourceforge.net/hgweb/pywin32/pywin32/file/tip/win32/Demos/win32gui_dialog.py # noqa
"""

When I need to remember the spelling for a list comprehension with more than one loop in Python, I find the following mnemonic helpful:

write the for statements in the same order you would write a nested loop

For example, suppose we have a nested list that we wish to flatten:

>>> nested_list = [[1, 2, '5!'], (3, 'sir!')]
@drocco007
drocco007 / eight_queens.py
Last active August 29, 2015 14:00
Eight queens solver (single solution). Adapted from Wirth's pseudo-Algol 60 algorithm presented in *Program Development by Stepwise Refinement*. http://oberoncore.ru/_media/library/wirth_program_development_by_stepwise_refinement2.pdf
"""Eight queens solver (single solution)
Adapted from Wirth's pseudo-Algol 60 algorithm presented in *Program
Development by Stepwise Refinement*.
http://oberoncore.ru/_media/library/wirth_program_development_by_stepwise_refinement2.pdf
"""