Skip to content

Instantly share code, notes, and snippets.

View Ceasar's full-sized avatar

Ceasar Ceasar

View GitHub Profile
@Ceasar
Ceasar / connect.py
Created April 3, 2013 00:54
Connect and stayed connected to AirPennNet.
import sys
import time
from sh import networksetup
DEVICE = 'en0'
NETWORK = u'AirPennNet'
def connect_to_network(network):
@Ceasar
Ceasar / ex1.py
Last active December 14, 2015 04:28
Experiments with scope in Python.
"""
Python scope is not dynamic.
"""
def foo():
print x
x = 0
@Ceasar
Ceasar / local.py
Last active December 13, 2015 21:08
Extend fabric.api.local to run commands in the background.
import subprocess
import fabric
def local(command, capture=False, shell=False, bg=False):
if bg:
if capture:
with open("/dev/null", "w") as f:
subprocess.Popen(command.split(), stdout=f, shell=shell)
@Ceasar
Ceasar / crash.js
Created February 15, 2013 16:27
Crashes a page after 10 seconds and uses all of a CPU. Left uncorrected, it can crash multiple pages in Chrome.
$(document).ready(function () {
setTimeout(function(){while(1){};},10000);
});
@Ceasar
Ceasar / partition.py
Created February 8, 2013 15:52
Take a predicate and a list and return a pair of lists: those elements that do and do not satisfy the predicate, respectively.
def partition(pred, items):
"""
Take a predicate and a list and return a pair of lists: those elements
that do and do not satisfy the predicate, respectively.
>>> partition(str.isalpha, "ab1c")
(['a', 'b', 'c'], ['1'])
>>> partition(str.isalpha, [])
([], [])
@Ceasar
Ceasar / Logic.lhs
Created January 3, 2013 03:42
Logical functions.
> import Data.Set as S
> import Data.Map as M
> import Data.Maybe
> import Control.Monad (liftM, liftM2)
> import Test.QuickCheck
A statement is a sentence that is determinately true of determinately false independently of the circumstances in which it is used.
> data Proposition = Implies Proposition Proposition
@Ceasar
Ceasar / DnC.hs
Created January 3, 2013 00:10
Proof of concept abstraction of divide and conquer. Doesn't really work very well...
-- In Divide and Conquer, we solve a problem recursively, applying three steps at each level of recursion.
dnc :: (a -> [a]) -> (a -> b) -> ([b] -> b) -> a -> b
dnc divide con comb x = case divide x of
[] -> con x
xs -> comb $ map (dnc divide con comb) xs
@Ceasar
Ceasar / join.py
Last active December 10, 2015 11:08
Interface to use a CSV like a SQL table.
'''Crudely joins multiple tables together.'''
import sys
import itertools
import table
def join(tables, name=None):
if name is None:
name = "_".join([table.filename for table in tables])
joined = Table(name, [fieldname for table in tables
@Ceasar
Ceasar / parse.hs
Created December 1, 2012 22:17
A basic parser written in Haskell. Written as a learning exercise.
import Data.List
-- Evaluate a postfix expression
evalPostfix :: (Num a, Read a) => String -> a
evalPostfix = head . foldl comb [] . words
where
comb (x:y:ys) "*" = (x * y) : ys
comb (x:y:ys) "+" = (x + y) : ys
comb (x:y:ys) "-" = (y - x) : ys
@Ceasar
Ceasar / map.c
Created November 12, 2012 03:06
Example of how to implement a higher order function in C.
/*
* Example of higher order functions in C.
*/
#include <stdio.h>
void func ( void (*f)(int, int) ) {
int ctr = 0;
for (ctr; ctr < 5; ctr++) {
(*f)(ctr, ctr);