Skip to content

Instantly share code, notes, and snippets.

View cbsmith's full-sized avatar

Christopher Smith cbsmith

View GitHub Profile
@cbsmith
cbsmith / sql_join.py
Last active August 29, 2015 13:56
An attempt to help this reddit poster use databases more effectively: http://www.reddit.com/r/Python/comments/1y521t/using_dictionaries_to_join_to_sql_results/
#!/usr/bin/env python
import sqlite3
from_location_group = {
'OBD1': 'Online',
'OBD2': 'Online',
'CB07': 'Retail',
'CB08': 'Retail',
'CB09': 'Retail',
@cbsmith
cbsmith / tad_test.cpp
Created March 1, 2014 02:38
Some examples of template argument deduction
// -*- compile-command: "clang++ -ggdb -std=c++11 -stdlib=libc++ -Wall -Werror tad_test.cpp -o tad_test" -*-
#include <iostream>
using namespace std;
template <typename T, typename U>
struct Foo {
T v;
U w;
@cbsmith
cbsmith / Copy.java
Created March 25, 2014 05:38
A simply gist demonstrating how to copy from stdin to stdout in Java, in response to discussion of: https://news.ycombinator.com/item?id=7463671
public class Copy {
public static void main(String[] args) throws Throwable {
int next;
while ((next = System.in.read()) != -1) {
System.out.write(next);
}
System.out.flush();
}
}
@cbsmith
cbsmith / format_strings.py
Last active August 29, 2015 14:13
An example of how one could wrap pyodbc's wildcard prepared statements with Python's format string to have keyword based prepared statements.
from string import Formatter
import sys
def keywords_to_questionmarks(format_string, *args, **kwargs):
'''Convert a format string keyword formatted query to a ? wildcarded prepared statement
that is pyodbc compatible, along with the args tuple to pass to it.
* format_string: Python format compatible representation of a query
* args: positional args for the format string
* kwargs: keyword args for the format string
{
"detail_output": {
"content.article-lease": {
"lease_type": 2,
"node_identifier": {
"node_name": "content.article-lease"
},
"timestamp": 1422677331539
},
"content.article.shingles-lease": {
@cbsmith
cbsmith / gevent.monkey_patch_all.py
Created February 18, 2015 22:21
monkey patch all
def patch_all(socket=True, dns=True, time=True, select=True, thread=True, os=True, ssl=True, httplib=False,
subprocess=False, sys=False, aggressive=True, Event=False):
"""Do all of the default monkey patching (calls every other function in this module."""
# order is important
if os:
patch_os()
if time:
patch_time()
if thread:
patch_thread(Event=Event)
@cbsmith
cbsmith / gist:1493e6686c8d1be74310
Created March 6, 2015 16:41
Clinton Article from AP Feed
<ns0:entry xmlns:ns0="http://www.w3.org/2005/Atom" xmlns:ns1="http://ap.org/schemas/03/2005/apcm" xmlns:ns2="http://ap.org/schemas/03/2005/apnm">
<ns0:id>urn:publicid:ap.org:f655319a9d09472495e3d7cc400da16a</ns0:id>
<ns0:title>US-DEM-2016-Clinton</ns0:title>
<ns0:updated>2015-03-05T22:05:43.580Z</ns0:updated>
<ns0:published>2015-03-05T22:05:36Z</ns0:published>
<ns0:author>
<ns0:name>AP</ns0:name>
</ns0:author>
<ns0:rights>Copyright 2015 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.</ns0:rights>
<ns0:content type="text/xml">
@cbsmith
cbsmith / joyent-instant-centos-salt-minion.sh
Created April 29, 2012 03:22
Instant CentOS Minion on Joyent
#!/bin/bash
#
# Setups up a new CentOS machine on Joyent running a Salt minion talking to your salt master. From there, you can do the rest of your setup work from the Salt Master.
# Requires Joyent Command Line SDK be installed as well as Node's jsontool (npm install jsontool)
# If you don't provide a Joyent ID, the code assumes the master is labeled with metadata "salt-role=master" or with "role=salt-master"
# Tweak these variables should have done anything creative with your salt setup
SALT_ROOT=/
SALT_MASTER_UID=root
@cbsmith
cbsmith / dont_terminate.cpp
Created August 8, 2012 23:50
The problem with std::uncaught_exception illustrated
// -*- compile-command: "clang++ -ggdb -o dont_terminate -std=c++0x -stdlib=libc++ dont_terminate.cpp" -*-
// Demonstration of the problem that can happen with using std::uncaught_exception() to determine
// whether it is safe to throw from within a destructor
// Problem identified, as always, by GOTW: http://www.gotw.ca/gotw/047.htm
#include <stdexcept>
#include <iostream>
#include <cassert>
using namespace std;
@cbsmith
cbsmith / brace_initializer.cpp
Created November 27, 2012 04:55
Demonstration of brace initialization as well as variadic templates.
// -*- compile-command: "clang++ -ggdb -o brace_initializer -std=c++0x -stdlib=libc++ brace_initializer.cpp" -*-
#include <string>
#include <ostream>
#include <memory>
#include <iostream>
#include <sstream>
#include <new>
class Foo {