Skip to content

Instantly share code, notes, and snippets.

@mittenchops
mittenchops / supertop
Created March 18, 2013 14:46
Trying to learn awk and better command line usage, I made a simple one-line replica of the unix program top.
watch -n 0.1 "ps aux | awk 'NR>1{print \$2 \"\t\" \$3 \"\t\" \$10 \"\t\" \$11}' | sort -nrk 2"
@mittenchops
mittenchops / Python dynamic dict out of set
Last active December 15, 2015 16:59
Python make a dynamic dictionary
x = set(['a', 'b'])
y = {}
y.update({r:'' for r in x if r is not ''})
@mittenchops
mittenchops / k_folds.R
Last active December 16, 2015 01:28
Found on SO somewhere, most useful testing function for sampling I've ever seen
f_K_fold <- function(Nobs,K=5){
rs <- runif(Nobs)
id <- seq(Nobs)[order(rs)]
k <- as.integer(Nobs*seq(1,K-1)/K)
k <- matrix(c(0,rep(k,each=2),Nobs),ncol=2,byrow=TRUE)
k[,1] <- k[,1]+1
l <- lapply(seq.int(K),function(x,k,d)
list(train=d[!(seq(d) %in% seq(k[x,1],k[x,2]))],
test=d[seq(k[x,1],k[x,2])]),k=k,d=id)
return(l)
@mittenchops
mittenchops / sumlist reduce operator
Last active December 16, 2015 02:59
playing with functional programming in python
reduce(operator.add,map(lambda x: math.sqrt(x**3), range(4+1)))
# http://ua.pycon.org/static/talks/kachayev/#/11
@mittenchops
mittenchops / testurl.R
Created April 12, 2013 19:15
test whether Urls are valid in R, uses a fancy Filter funcion
mylist = c('www.google.com','www.google.comascascasdasdnasmdamsd')
isgoodURL <- function(url){if(system(paste('curl -o /dev/null --silent --head --fail ', url)) == 0) return(TRUE) else return(FALSE)}
Filter(isgoodURL,mylist)
@mittenchops
mittenchops / reducesum.R
Created April 12, 2013 19:31
Remembering syntax for Reduce in R.
add <- function(x,y)(return(x+y))
Reduce(add,c(1,2,3,4,5))
@mittenchops
mittenchops / fred-has-classy-pants.py
Last active December 16, 2015 08:39
Make a class from the command line and show quirks of inheritance after instantiation
>>> from __future__ import print_function
>>> class People: pass
>>>
>>> Fred = People()
>>> Fred.has_pants = True
# Now I'd like to create a method that says "I have pants" if Fred has pants. I make the function:
>>> f = lambda self: print("I have pants") if self.has_pants else None
@mittenchops
mittenchops / Nested Dicts by JSON Dot Notation.py
Last active June 2, 2024 12:17
Lets you access nested dictionaries in Python the same way you access nested items in JSON notation. This means, however, that you cannot use dots in key names. Most useful for accessing named variables according to the results of variety.js.
def getByDot(obj, ref):
"""
Use MongoDB style 'something.by.dot' syntax to retrieve objects from Python dicts.
This also accepts nested arrays, and accommodates the '.XX' syntax that variety.js
produces.
Usage:
>>> x = {"top": {"middle" : {"nested": "value"}}}
>>> q = 'top.middle.nested'
@mittenchops
mittenchops / subclass.py
Created July 11, 2013 14:30
Subclassing in python
class Superclass(object):
def __init__(self):
print 'Do something'
class Subclass(Superclass):
def __init__(self):
super(Subclass, self).__init__()
print 'Do something else'
@mittenchops
mittenchops / validation.py
Last active December 19, 2015 15:28
Python validators. The general form comes exactly from Python in Practice: Create Better Programs Using Concurrency, Libraries, and Patterns Very useful: http://my.safaribooksonline.com/book/programming/python/9780133373271/2dot-structural-design-patterns-in-python/ch02lev1sec4_html
def ensure(name, validate, doc=None):
def decorator(Class):
privateName = "__" + name
def getter(self):
return getattr(self, privateName)
def setter(self, value):
validate(name, value)
setattr(self, privateName, value)
setattr(Class, name, property(getter, setter, doc=doc))
return Class