Skip to content

Instantly share code, notes, and snippets.

@89465127
89465127 / perudo.py
Last active December 14, 2015 15:38
Perudo Game Assistant
#! /usr/bin/python
import operator
import re
import numpy as np
class Perudo():
def __init__(self):
print "-- New Game --\nNumber of players?"
num_players = int(self.user_input())
@89465127
89465127 / gloss.py
Last active December 14, 2015 17:19
Wikipedia glossary generator
#! /usr/bin/python
import bs4
import json
import urllib2
import html2text
import nltk
import collections
# to do, lets turn this into a full class (that can be imported)
# and use argparse (which invokes serializing it to a file)
@89465127
89465127 / deepcopy.py
Created March 26, 2013 23:32
recurses over a mixed data structure of tuples, lists, and dicts, and returns a copy.
# this simple example recurses over a mixed data structure of tuples, lists, and dicts, and returns a copy.
num_recursions = 0
def r(current):
global num_recursions
num_recursions += 1
if isinstance(current, list):
return [r(i) for i in current]
elif isinstance(current, dict):
@89465127
89465127 / csv_dictreader_example.py
Created March 29, 2013 20:25
Uses csv.DictReader and StringIO to read a sample csv file. No external file is required.
import csv
import StringIO
# cStringIO is faster, but cannot handle unicode characters
def main():
f = StringIO.StringIO('language,greeting\nenglish,hello\nspanish,hola')
for _dict in csv.DictReader(f):
print _dict
if __name__ == '__main__':
@89465127
89465127 / lambda_map.py
Created March 30, 2013 05:25
A couple ways to use map() when you need nested function calls.
# Core functions
def triple(num):
return num*3
def double(num):
return num*2
# Input values
nums = [1,2,3,4,5,6]
@89465127
89465127 / map_example.py
Created March 30, 2013 05:45
Examples of map() built-in that follow the official python documentation at http://docs.python.org/2/library/functions.html#map
# map(function, iterable, ...)
#
# http://docs.python.org/2/library/functions.html#map
nums = range(5)
extended_nums = range(10)
double = lambda x: x*2
concat = lambda x,y: "{x}{y}".format(x=x, y=y)
@89465127
89465127 / locate_site_packages.py
Created April 1, 2013 04:31
python site-packages location
print 'If you are running ubuntu, the site-packages is in:'
print '/usr/local/lib/pythonX.X/dist-packages'
print
print 'Otherwise it\'s here:'
from distutils.sysconfig import get_python_lib;
print get_python_lib()
@89465127
89465127 / reduce_example.py
Created April 1, 2013 06:33
Examples of how to use reduce() in python
# The reduce() function applies a function which will reduce the sequence to a single value.
# There are a number of special-purpose reduce functions that we’ve already seen.
# These reductions include sum(), any(), all().
def red_max(x, y):
return x if x > y else y
def red_sum(x, y):
return x + y
@89465127
89465127 / Jongo.java
Last active December 15, 2015 18:49
The "Hello World" of Java and MongoDB
// Jongo.java
import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
@89465127
89465127 / maven_scala.bash
Created April 18, 2013 22:03
Setting up & running a simple hello world project with Scala and Maven
# first we'll create a new project
mvn archetype:generate -DarchetypeGroupId=org.scala-tools.archetypes -DarchetypeArtifactId=scala-archetype-simple -DgroupId=myGroup -DartifactId=myTestProject -Dversion=1.0 -DinteractiveMode=false
# then we will package it to create a jar, and populate target/
cd myTestProject
optional: rm -rf src/test
mvn package