Skip to content

Instantly share code, notes, and snippets.

View Dowwie's full-sized avatar

Darin Gordon Dowwie

View GitHub Profile
@mikeyk
mikeyk / gist:1329319
Created October 31, 2011 22:56
Testing storage of millions of keys in Redis
#! /usr/bin/env python
import redis
import random
import pylibmc
import sys
r = redis.Redis(host = 'localhost', port = 6389)
mc = pylibmc.Client(['localhost:11222'])
@adamsanderson
adamsanderson / Gemfile
Last active January 23, 2024 14:08
Demonstration of hierarchical queries in Postgres using a materialized path. It will create a new database that you can later play around with.
source 'https://rubygems.org'
gem 'activerecord', '4.0.0.rc1'
@gavinwahl
gavinwahl / python_implementation
Last active April 24, 2020 23:23
Triggers to enforce referential integrity for foreign keys, if postgres didn't support them natively.
CREATE OR REPLACE FUNCTION check_fk_child() RETURNS trigger AS $$
DECLARE
fk_local TEXT := TG_ARGV[0];
parent_table TEXT := TG_ARGV[1];
fk_val INT;
is_valid BOOLEAN;
query TEXT;
BEGIN
-- fk_val = getattr(NEW, fk_local)
EXECUTE format('SELECT $1.%I', fk_local) USING NEW INTO fk_val;
SELECT * FROM (
SELECT generate_series('2012-11-20'::date, '2014-01-01'::date,'1 day'::interval)::date AS day)
AS dates
LEFT JOIN (
SELECT date_trunc('day', donations_donation.created) as day,
SUM(donations_donation.amount) as amount_raised
FROM donations_donation
WHERE created >= '2012-11-20' AND created <= '2014-01-01' group by 1 ) t
USING(day)
ORDER BY 1;
@Ficik
Ficik / monokai.sh
Created November 30, 2013 22:48
Monokai theme (colors as in sublime-text-2) for guake and gnome-terminal. Just copypaste to terminal
# Guake:
COLORS="#272728282222:#ffff29292929:#a6a6e2e22e2e:#e6e6dbdb7474:#6666d9d9efef:#f9f926267272:#bebe8484ffff:#fefefffffefe:#757571715e5e:#ffff29292929:#a6a6e2e22e2e:#fdfd97971f1f:#6666d9d9efef:#f9f926267272:#aeae8181ffff:#fefefffffefe"
FOREGROUND="#F6F6F5F5EEEE"
BACKGROUND="#232325252626"
gconftool-2 -s -t string /apps/guake/style/background/color $BACKGROUND
gconftool-2 -s -t string /apps/guake/style/font/palette $COLORS
gconftool-2 -s -t string /apps/guake/style/font/color $FOREGROUND
@hest
hest / gist:8798884
Created February 4, 2014 06:08
Fast SQLAlchemy counting (avoid query.count() subquery)
def get_count(q):
count_q = q.statement.with_only_columns([func.count()]).order_by(None)
count = q.session.execute(count_q).scalar()
return count
q = session.query(TestModel).filter(...).order_by(...)
# Slow: SELECT COUNT(*) FROM (SELECT ... FROM TestModel WHERE ...) ...
print q.count()
@Chaser324
Chaser324 / GitHub-Forking.md
Last active May 2, 2024 05:49
GitHub Standard Fork & Pull Request Workflow

Whether you're trying to give back to the open source community or collaborating on your own projects, knowing how to properly fork and generate pull requests is essential. Unfortunately, it's quite easy to make mistakes or not know what you should do when you're initially learning the process. I know that I certainly had considerable initial trouble with it, and I found a lot of the information on GitHub and around the internet to be rather piecemeal and incomplete - part of the process described here, another there, common hangups in a different place, and so on.

In an attempt to coallate this information for myself and others, this short tutorial is what I've found to be fairly standard procedure for creating a fork, doing your work, issuing a pull request, and merging that pull request back into the original project.

Creating a Fork

Just head over to the GitHub page and click the "Fork" button. It's just that simple. Once you've done that, you can use your favorite git client to clone your repo or j

# here's a variant that adds a decorator like that of
# https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/BakedQuery
from sqlalchemy.orm.query import QueryContext
class BakedQuery(object):
"""an object that can produce a 'baked' Query, that is one where
its ultimately generated SQL string is cached based on how the query
has been constructed.
SELECT
sum(view_homepage) AS viewed_homepage,
sum(enter_credit_card) AS entered_credit_card
FROM (
-- Get the first time each user viewed the homepage.
SELECT
user_id,
1 AS view_homepage,
min(time) AS view_homepage_time
FROM event
SELECT
sum(view_homepage) AS viewed_homepage,
sum(use_demo) AS use_demo,
sum(enter_credit_card) AS entered_credit_card
FROM (
-- Get the first time each user viewed the homepage.
SELECT
user_id,
1 AS view_homepage,
min(time) AS view_homepage_time