Skip to content

Instantly share code, notes, and snippets.

View jrdmcgr's full-sized avatar

Jared McGuire jrdmcgr

  • Qgiv
  • Central Florida, USA
  • 14:56 (UTC -04:00)
View GitHub Profile
@jrdmcgr
jrdmcgr / LocalDateTime.py
Created November 5, 2012 14:58
timezone validator
class LocalDateTime(formencode.FancyValidator):
""" This class is a formencode validator that will localize a date-time.
By default python date-time objects don't have any time zone information
associated with them, but they provide abstract support for time zones.
The pytz module provides concrete timezones to use with date-time objects.
This class will ensure that all dates have a timezone associated with them.
It will wite dates to the DB with the UTC time zone; it will convert dates
read from the DB to the client's local time zone defined in their config
#!/usr/bin/env python
concurrency_impl = 'gevent' # single process, single thread
##concurrency_impl = 'threading' # single process, multiple threads
##concurrency_impl = 'multiprocessing' # multiple processes
if concurrency_impl == 'gevent':
import gevent.monkey; gevent.monkey.patch_all()
import logging
import time
@jrdmcgr
jrdmcgr / concurrency-bench.py
Created February 14, 2013 19:13
Time some trivial work using various concurrency mechanisms.
""" Time some trivial work using various concurrency mechanisms. """
import time
from threading import Thread
from multiprocessing import Process
def timeit(f):
""" Time a function with this decorator. """
def timed(*args, **kw):
start_time = time.time()

This now works with the latest Selenium bindings. (pip install selenium)

from selenium import webdriver
driver = webdriver.Firefox() # or webdriver.Chrome()
driver.get('http://user:password@example.com')

The eqivalent with Splinter doesn't work.

from splinter import Browser
browser = Browser()
//
// How to Extend Validity
//
// Setup validity and get the private validation function.
$.validity.setup({debugPrivates: true})
var validate = $.validity.__private.validate
// Add our message
$.validity.messages.myCustomValidationMessage = 'You fail.'
from types import MethodType
from functools import wraps
def decorate_methods(decorator):
"""
Return a class decorator that will decorate public methods with the given decorator.
"""
def class_decorator(klass):
for name in dir(klass):
if name.startswith('_'):
import re
from operator import *
def lisp(s_exp):
""" Evaluate pseudo-lisp.
`s_exp` should be a string of an s-expression.
If the first item in the s-exp isn't callable then the s-exp will be
treated like a literal list.
@jrdmcgr
jrdmcgr / mustache-recursive.html
Created June 19, 2013 18:28
Recursive partial in mustache.js
<html>
<body>
<div id="hierarchy"></div>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mustache.js/0.7.2/mustache.min.js"></script>
<script type="text/template" id="recursive-partial">
{{#children}}
<li>{{name}}
@jrdmcgr
jrdmcgr / halbig.py
Created October 7, 2013 13:33
A programming challenge
"""
Write logic that dynamically outputs a string of 35 characters.
The string is composed of 7 distinct letters. Each letter is used 5 times.
Any subset of 3 characters must be unique from any other subset of 3 characters.
Any subset of 3 characters can only have zero or ONE of the same letter.
"""
from collections import Counter
from itertools import permutations
def sequences(seq, n=3):
@jrdmcgr
jrdmcgr / sdl-test.c
Created October 31, 2013 16:49
Move a red square around with the arrow keys.
#include "SDL2/SDL.h"
#include <stdio.h>
void clear(SDL_Renderer * renderer) {
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}