Navigation Menu

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 / better_random.py
Last active January 4, 2016 10:19
My thoughts on enhancements to make random support a broader set of iterables.
import random
import itertools
def sample(population, k):
"How random.sample should really be defined."
if callable(getattr(population, '__len__', None)) and callable(getattr(population, '__getitem__', None)):
return random.sample(population, min(k, len(population)))
p = iter(population)
@cbsmith
cbsmith / endian.cpp
Last active December 30, 2015 13:29
My demonstration of how byte swap can still be useful despite http://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html
// -*- compile-command: "g++ -O3 -march=native -std=c++11 -Wall -Werror endian.cpp -o endian" -*-
#include <fstream>
#include <ostream>
#include <istream>
#include <cstdint>
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <limits>
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 21:19
Attempt to create an operator||=
// -*- compile-command: "clang++ -Weverything -Wno-c++98-compat -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
#include <type_traits>
template <typename T, typename U, bool boolable = std::is_convertible<T,bool>::value, bool assignable = std::is_assignable<T, U>::value >
struct or_assign_helper;
template <typename T, typename U>
struct or_assign_helper<T, U, true, true> {
@cbsmith
cbsmith / test.cpp
Last active December 23, 2015 20:29
C++11 lambda sorcery
// -*- compile-command: "clang++ -ggdb -o test -std=c++11 -stdlib=libc++ test.cpp" -*-
#include <iostream>
float add(float x, float y) {
return x+y;
}
auto add_fourty_two = +[](float x) { return add(x, 42); };
auto add_pie_and_fourty_two = +[]() { return add_fourty_two(3.1415926); };
@cbsmith
cbsmith / two_stars_98.cpp
Created May 21, 2013 17:09
C++98 version of two_stars
// -*- compile-command: "clang++ -ggdb -o two_stars_98 -std=c++98 -stdlib=libc++ two_stars_98.cpp" -*-
#include <algorithm>
#include <iostream>
using namespace std;
bool points_to_two(int* x) {
return *x == 2;
}
@cbsmith
cbsmith / two_stars.cpp
Created May 21, 2013 16:41
Demonstration of how two_star programming works with STL containers and algorithms.d
// -*- compile-command: "clang++ -ggdb -o two_stars -std=c++0x -stdlib=libc++ two_stars.cpp" -*-
#include <algorithm>
#include <iostream>
using namespace std; //laziness
int main(int argc, char** argv) {
int a = 1;
int b = 2;
int c = 3;
@cbsmith
cbsmith / random_selection.cpp
Last active August 9, 2022 12:29
Hopefully serves as a reference implementation on how to do random selection of an element from a container.
// -*- compile-command: "clang++ -ggdb -o random_selection -std=c++0x -stdlib=libc++ random_selection.cpp" -*-
//Reference implementation for doing random number selection from a container.
//Kept for posterity and because I made a surprising number of subtle mistakes on my first attempt.
#include <random>
#include <iterator>
template <typename RandomGenerator = std::default_random_engine>
struct random_selector
{
//On most platforms, you probably want to use std::random_device("/dev/urandom")()
@cbsmith
cbsmith / cleanup.py
Last active December 16, 2015 16:59
Python code that traverses one or more directories, removing any orphaned .pyc's.
"""Traverses the directory tree deleting any .pyc's who do not have a source .py.
Helpful when switching between revisions with source control."""
from os.path import join
from os import walk, unlink
import re
import sys
PYTHON_RE = re.compile(R'^(.*\.py)(c)?$')
global count
@cbsmith
cbsmith / the_nullptr_story.cpp
Last active December 16, 2015 02:19
A simple example of how it is perfectly reasonable to have a pointer that may or may not be nullptr, and therefore delete a pointer that may or may not be nullptr. We've got people of type Person who, when they get married, may choose to take on their spouse's last name. These people always have a given last name, called "lastname", but they may…
// -*- compile-command: "clang++ -pedantic -g -O3 -o the_nullptr_story -std=c++0x -stdlib=libc++ the_nullptr_story.cpp" -*-
// Simple example of how it is perfectly reasonable to have a pointer that may or may not be nullptr,
// and therefore delete a pointer that may or may not be nullptr.
// Formatting is a bit unusual/dense to make it easy to focus on the important bits.
#include <string>
#include <ostream>
class Person {
public:
//Moved to the top so nobody misses it