Skip to content

Instantly share code, notes, and snippets.

@jvanasco
jvanasco / import_logger.py
Created October 24, 2016 16:20
Use the `import_logger.py` to "decorate" cPython's `import` statement, and log the actual memory grown to a file.
if True:
print("===> installing import_logger_orverride")
import os
import psutil
import pdb
import pprint
import __builtin__
import logging
import sys
"""
After a bit of thought, I rewrote our response caching.
I stripped it into this gist in case anyone needs a starting point in the future. it's still rather specialized and hacky... but a bit cleaner than the earlier approach
The general overiew is this:
* We needed to cache responses AND perform some light operations. 99% of a rendered page is cachable as-is.
* We wanted to consolidate where the configuration is, perferalbly with the route_name declaration - but in this case it was better off with the caching code.
* The cached output should be rendered, which mades the `decorators` egress perfect... however some setup code happens in the __init__ phase of view-based classes and the 'decorators' ingress happens too soon.
@jvanasco
jvanasco / renderers.py
Created September 11, 2013 20:50
pyramid renderer
from pyramid.config import Configurator
from zope.interface.registry import Components
import simplejson as json
import datetime
import decimal
from .. import lib
@jvanasco
jvanasco / app_celery.py
Created June 18, 2016 01:29
cache with temporary values.
@task
def get_ExpensiveCalculator_foo():
expensive_foo = app_shared.ExpensiveCalculator().expensive_foo()
app_pyramid.CheapCalculator().set_foo(expensive_foo)
@jvanasco
jvanasco / app_celery.py
Created June 18, 2016 01:29
cache with temporary values.
@task
def get_ExpensiveCalculator_foo():
expensive_foo = app_shared.ExpensiveCalculator().expensive_foo()
app_pyramid.CheapCalculator().set_foo(expensive_foo)
@jvanasco
jvanasco / deploy.py
Last active May 9, 2016 18:00
Automated (mostly) Trac Installation for Dreamhost
import os
username = 'johndoe'
svndomain = 'svn.example.com'
www_root = "/home/%(username)s/webroot/%(svndomain)s/" % {'username': username, 'svndomain': svndomain}
trac_root = "/home/%(username)s/svn_trac/" % {'username': username}
svn_root = "/home/%(username)s/svn" % {'username': username}
shared_chrome_dir = "/home/%(username)s/svn_trac/trac-deploy/htdocs" % {'username': username}
sites = [i.strip() for i in open('trac_sites.txt').readlines()]
@jvanasco
jvanasco / client.py
Last active April 20, 2016 19:18
statsd max connections
class StatsClient(StatsClientBase):
"""A client for statsd."""
def __init__(self, host='localhost', port=8125, prefix=None,
maxudpsize=512, ipv6=False):
"""Create a new client."""
fam = socket.AF_INET6 if ipv6 else socket.AF_INET
family, _, _, _, addr = socket.getaddrinfo(
host, port, fam, socket.SOCK_DGRAM)[0]
self._addr = addr
@jvanasco
jvanasco / sqlalchemy_relation.py
Last active April 4, 2016 23:33
sqlalchemy relationship
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
# Use this file to build your own SSCCE
# SSCCE = Short, Self Contained, Correct (Compatible) Example
# see http://sscce.org/
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
CREATE TABLE t_b (id INT PRIMARY KEY, timestamp_event INT);
CREATE TABLE t_a (id INT PRIMARY KEY, id_latest_b INT REFERENCES t_b(id));
CREATE TABLE t_a2b (id_a REFERENCES t_a(id), id_b REFERENCES t_b(id), PRIMARY KEY (id_a, id_b));
INSERT INTO t_a VALUES (1, NULL);
INSERT INTO t_a VALUES (2, NULL);
INSERT INTO t_a VALUES (3, NULL);
INSERT INTO t_a VALUES (4, NULL);
INSERT INTO t_b VALUES (1, 10);
INSERT INTO t_b VALUES (2, 20);
INSERT INTO t_b VALUES (3, 30);
if (typeof console === 'undefined') {
window.console = {
log: function () {}
};
}
function loggerFactory(label) {
return (function () {
var timestamp = function () {};
timestamp.toString = function () {