Skip to content

Instantly share code, notes, and snippets.

View miohtama's full-sized avatar
🏠
https://tradingstrategy.ai

Mikko Ohtamaa miohtama

🏠
https://tradingstrategy.ai
View GitHub Profile
import string
from urllib.parse import urlparse
import random
from sqlalchemy import (
Column,
Index,
Integer,
String, DateTime, ForeignKey)
from pyramid_web20.models import Base, utc, now, DBSession
@miohtama
miohtama / __init__.py
Last active August 29, 2015 14:22
Deferred declared_attr for Pyramid and SQLAlchemy. This is an example how to detact your SQLAlchemy models from declarative base class and database session, so that you can made them configurable for the third party. Your library can contain declared_attr directives which can be filled in by Pyramid configuration. E.g. your library expects the h…
"""websauna.referral library initialization code."""
import logging
from sqlalchemy.ext.declarative import instrument_declarative
from pyramid_web20.system.model import Base
from . import models
@miohtama
miohtama / uwsgi.ini
Last active August 29, 2015 14:23
Newrelic + Pyramid + uWSGI
def wrap_wsgi_app(self, app):
"""Perform any necessary WSGI application wrapping.
Wrap WSGI application to another WSGI application e.g. for the monitoring support. By default support New Relic.
"""
if "NEW_RELIC_CONFIG_FILE" in os.environ:
# Wrap for New Relic
# libgcc_s.so.1 must be installed for pthread_cancel to work
import newrelic.agent
@miohtama
miohtama / crud.py
Last active August 29, 2015 14:23
Mapping objects to Pyramid traversing ids and vice vrsa
"""CRUD based on SQLAlchemy and Deform."""
from abc import abstractmethod, abstractproperty
from websauna.system.core import traverse
from . import mapper
class CRUD(traverse.Resource):
"""Define create-read-update-delete interface for an model.
@miohtama
miohtama / compat.py
Created July 6, 2015 04:37
Backporting backposts.typing to Pythons < 3.5
"""Compatibility layer between different Python versions."""
import sys
if sys.version_info >= (3, 5):
import typing
else:
from backports import typing
# Then from compat import typing
@miohtama
miohtama / gist:ef56fcf0b9f264226250
Created July 19, 2015 21:33
Splinter + py.test functional testing of Pyramid
from websauna.system.model import DBSession
EMAIL = "example@example.com"
PASSWORD = "ToholamppiMadCowz585"
def get_user():
from websauna.system.user.models import User
return DBSession.query(User).get(1)
@miohtama
miohtama / test_facebook_login.py
Created August 11, 2015 11:35
py.test + splinter + authomatic + Facebook login functional test for Python
import os
import pytest
def do_facebook_login(browser):
fb_user = os.environ.get("FACEBOOK_USER")
assert fb_user, "Please configure your Facebook secrets as environment variables to run the tests"
fb_password = os.environ["FACEBOOK_PASSWORD"]
class ServerThread(threading.Thread):
""" Run WSGI server on a background thread.
Pass in WSGI app object and serve pages from it for Selenium browser.
"""
def __init__(self, app, hostbase=HOST_BASE):
threading.Thread.__init__(self)
self.app = app
self.srv = None
class ServerThread(threading.Thread):
""" Run WSGI server on a background thread.
Pass in WSGI app object and serve pages from it for Selenium browser.
"""
def __init__(self, app, hostbase=HOST_BASE):
threading.Thread.__init__(self)
self.app = app
self.srv = None
@miohtama
miohtama / conftest.py
Last active August 29, 2015 14:27
How to past INI configuration file for your py.test based Pyramid unit tests
import os
import pyramid.testing
import pytest
import transaction
from sqlalchemy.orm.session import Session
from pyramid.paster import (
get_appsettings,
setup_logging,