Skip to content

Instantly share code, notes, and snippets.

#lambda
#lambda parameter: expression
f = lambda x, y: x + y
f(15,1)
#=> 16
#using within a high order function
def my_function(x):
return lambda arr: [x * each for each in arr]
fives = my_function(5)
print fives([1,2,3,4])
def factory(x):
def multiply(my_array):
my_array2 = []
for each in my_array:
my_array2.append(each*x)
return my_array2
return multiply
#using lambda
def factory(x):
return lambda arr: [x * el for el in arr]
@GitMoIO
GitMoIO / uniqify_list.py
Created October 25, 2015 04:32
From http://www.peterbe.com/plog/uniqifiers-benchmark Various functions to uniqify a list in python. Some preserve the orders other don't.
def f1(seq):
# not order preserving
set = {}
map(set.__setitem__, seq, [])
return set.keys()
def f2(seq):
# order preserving
checked = []
for e in seq:
>>> def table_things(**kwargs):
... for name, value in kwargs.items():
... print '{0} = {1}'.format(name, value)
...
>>> table_things(apple = 'fruit', cabbage = 'vegetable')
cabbage = vegetable
apple = fruit
>>> def print_everything(*args):
for count, thing in enumerate(args):
... print '{0}. {1}'.format(count, thing)
...
>>> print_everything('apple', 'banana', 'cabbage')
0. apple
1. banana
2. cabbage
<form> <label>TestDynamicPanel</label> <fieldset submitButton="false"> <input type="dropdown" token="show1"> <label>Dropdown1</label> <populatingSearch fieldForLabel="temp" fieldForValue="temp">|gentimes start=-1 | eval temp="ColumnChart" | table temp</populatingSearch> </input> <input type="dropdown" token="show2"> <label>Dropdown2</label> <populatingSearch fieldForLabel="temp" fieldForValue="temp">|gentimes start=-1 | eval temp="LineChart" | table temp</populatingSearch> </input> <input type="dropdown" token="show3"> <label>Dropdown3</label> <populatingSearch fieldForLabel="temp" fieldForValue="temp">|gentimes start=-1 | eval temp="Table" | table temp</populatingSearch> </input> </fieldset> <row> <panel> <chart depends="$show1$"> <title>Details for $submitted:sourcetype|s$</title> <searchString>index=_internal | timechart count</searchString> <earliestTime>-15m</earliestTime> <latestTime>now</latestTime
import mechanize
br = mechanize.Browser()
br.add_password("http://blah.blah/", "adm", "mps")
response = br.open("http://blah.blah/somepage.html")
br.select_form(nr=1)
br["junk"]=["hatethis"]
response3=br.form.click(name="applybutton",nr=1)
import os
import cPickle as pickle
def cached_data():
cache_path = "/tmp/cached-data.pickle"
if not os.path.exists(cache_path):
# The cache doesn't exist, create it and populate it
result = generate_data()
cache_file = open(cache_path,'wb')
# Write it to the result to the file as a pickled object
#!/usr/bin/env python
# I wasn't happy with any of the GitHub libraries for Python that I tried so I
# just used the GitHub API directly. If someone would like to rewrite this
# using a library please be my guest
import argparse
import base64
import getpass
import json
@GitMoIO
GitMoIO / stream_data.py
Created June 22, 2014 17:26
Streaming data from Twitter using Tweepy
import tweepy
import json
# Authentication details. To obtain these visit dev.twitter.com
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
# This is the listener, resposible for receiving data