Skip to content

Instantly share code, notes, and snippets.

@atmb4u
atmb4u / snakecase
Last active February 19, 2019 03:20
anything to snakecase
#!/usr/bin/env python
"""
Usage: Put this script in any /bin folder in the PATH and run
snakecase "Hello World (123).csv"
OR
Download this snippet and run
python snakecase "Hello World (123).csv"
Output: hello_world_123.csv
"""
@atmb4u
atmb4u / similarish.py
Created March 9, 2017 17:32
Find if two different strings (names) are similar enough to make it a single person's name.
import re, itertools
def similarish(string1, string2):
"""
Find if two different strings (names) are similar enough to make it a single person's name.
Input
string1 : a person's full/partial name
string2 : same person's full/partial name or different formatting from name1
Output
Boolean
@atmb4u
atmb4u / timeit.scala
Created December 29, 2016 20:24
timeit for scala
// http://stackoverflow.com/questions/9160001/how-to-profile-methods-in-scala
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0) + "ns")
result
}
@atmb4u
atmb4u / speedtest.py
Last active December 27, 2016 18:09
A quick and dirty CPU benchmarking in python
from hashlib import md5
from multiprocessing import Pool
from time import time
"""
A quick and dirty CPU benchmarking in python
Calculates md5 for a text multiple times in
multiple processes to push CPU to its limits.
"""
@atmb4u
atmb4u / superscript.py
Created July 27, 2016 04:13
python function generate unicode superscript of a number and print it in command line
#!/usr/bin/python
# -*- coding: utf-8 -*-
power_dict = [
"⁰",
"¹",
"²",
"³",
"⁴",
"⁵",
@atmb4u
atmb4u / bootstrap.py
Created February 20, 2016 15:28
statistical bootstrap function in python
import numpy as np
import numpy.random as npr
import pylab
def bootstrap(data, num_samples, statistic, alpha):
"""Returns bootstrap estimate of 100.0*(1-alpha) CI for statistic."""
n = len(data)
idx = npr.randint(0, n, (num_samples, n))
samples = data[idx]
@atmb4u
atmb4u / data.py
Created January 17, 2016 04:51
converting dictionary to class
class Data:
def __init__(self, dictionary):
for k, v in dictionary.items():
setattr(self, k, v)
o = Data({'a': 1, 'b': 2})
o.a
@atmb4u
atmb4u / thinking_in_functions.py
Created October 3, 2015 05:22
Demo for thinking in functions workshop with imdb database
import csv
import itertools
from collections import Counter, defaultdict
import json
import re
"""
get Blockbuster Database dataset from http://www.crowdflower.com/data-for-everyone
"""
f = open("top_ten_movies_per_year_DFE.csv")
r = csv.DictReader(f)
@atmb4u
atmb4u / urls.py
Created September 24, 2015 04:30
version info for django projects with git
#add this to your urls file
url(r'^version/$', 'api.views.code_version', name='code_version'),
@atmb4u
atmb4u / use_bottle.py
Created April 12, 2015 09:39
Function Pipelines with persistent State (FPS) - FPS programming
b = WaterBottle()
b.add_water(300)
b.drink_water(200)
b.drink_water(700)
b = WineBottle()
b.add_wine(300)
b.drink_wine(200)
b.drink_wine(700)