Skip to content

Instantly share code, notes, and snippets.

View Flushot's full-sized avatar
👀

Chris Lyon Flushot

👀
View GitHub Profile
@Flushot
Flushot / csv_transform.php
Created June 14, 2013 22:31
Transforms a hash table into a CSV. This is useful for dumping results from database queries. Includes an optional row transformer, which can pre-process rows with complex computations (such as decryption).
<?php namespace chrisl\utils;
function transform_csv(
$rows,
array $cols=[],
Closure $row_transformer=null,
array $opts=[
'skip_header' => false,
'col_sep' => ',',
'quote' => '"',
@Flushot
Flushot / grep_except.sh
Last active December 18, 2015 12:49
Recursively greps a directory, omitting specified files and subdirectories.
#!/bin/sh
find -x . \
-type d \( \
-path ./.git -o \
-path ./logs -o \
-path ./temp \
\) -prune -o \
-exec grep -snH "$1" {} \;
@Flushot
Flushot / chunks.py
Created June 14, 2013 22:46
Yields n-sized chunks from a list l
def chunks(l, n):
"""Yields successive :n:-sized chunks from list :l:"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def uniq(seq):
seen = set()
seen_add = seen.add
return [ x for x in seq if x not in seen and not seen_add(x) ]
@Flushot
Flushot / import_non_local.py
Created June 14, 2013 22:50
Imports a module that would also have the same name as a local module, but exists in a different location.
def import_non_local(name, customName=None):
import imp
customName = customName or name
f, pathName, desc = imp.find_module(name, sys.path[1:])
module = imp.load_module(customName, f, pathName, desc)
f.close()
return module
@Flushot
Flushot / retry.py
Created June 14, 2013 22:55
Retries a function call on failure
def retry(fn, times=1, delay=3, exc=[Exception]):
"""Calls :fn: and retries up to :times: times with a delay of :delay: while exceptions :exc: are raised"""
import time
for i in range(times):
try:
return fn()
except tuple(exc), ex:
if i < times:
if delay is not None:
time.sleep(delay)
@Flushot
Flushot / sqlalchemy_aware_datetime.py
Last active December 18, 2015 12:49
Factory method that creates an aware DateTime column (includes timezone info and automatically converts). Works with MySQL dialects, which only support naive datetime types.
import datetime
from dateutil import tz
from sqlalchemy import types
from naive_aware import isAware, makeAware, makeNaive
def AwareDateTime(tz=None, localtz=tz.tzlocal()):
if tz is None:
tz = tz.tzutc()
elif isinstance(tz, str):
tz = tz.gettz(tz)
@Flushot
Flushot / jinja2_dateformat.py
Created June 14, 2013 23:09
Jinja2 filter that formats a date and optionally localizes it to client timezone
from dateutil import tz
from jinja2 import Undefined
client_timezone = tz.gettz('PST8PDT') # timezone to translate to
@app.template_filter()
def dateformat(value, format=None, rebase=True):
if value is None or isinstance(value, Undefined):
return ''
@Flushot
Flushot / sandbox.hs
Created June 14, 2013 23:12
Messing around with Haskell
addVectors :: (Num a) => (a, a) -> (a, a) -> (a, a)
addVectors (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
first :: (a, b, c) -> a
first(x, _, _) = x
second :: (a, b, c) -> b
second (_, y, _) = y
third :: (a, b, c) -> c
@Flushot
Flushot / naive_aware.py
Created June 14, 2013 23:15
Functions for dealing with naive and aware dates
import datetime
from dateutil import tz
def isAware(dt):
return dt.tzinfo is not None and \
dt.tzinfo.utcoffset(dt) is not None
def makeAware(dt, tzinfo=None):
if dt is None: return None
if isAware(dt): return dt # already aware