code:
n = T.iscalar()
n_triu_entries = (n * (n + 1)) / 2
r = T.arange(n)

tmp_mat = r[np.newaxis, :] + (n_triu_entries - n - (r * (r + 1)) / 2)[::-1, np.newaxis]
triu_index_matrix = T.triu(tmp_mat) + T.triu(tmp_mat).T - T.diag(T.diagonal(tmp_mat))

triu_vec = T.fvector()
sym_matrix = triu_vec[triu_index_matrix]

f_triu_index_matrix = theano.function([n], triu_index_matrix)
f_dynamic_sym_matrix = theano.function([triu_vec, n], sym_matrix)

print f_triu_index_matrix(5)
# [[ 0  1  2  3  4]
#  [ 1  5  6  7  8]
#  [ 2  6  9 10 11]
#  [ 3  7 10 12 13]
# [ 4  8 11 13 14]]
print f_dynamic_sym_matrix(np.arange(1., 16.).astype(np.float32), 5)
# [[  1.   2.   3.   4.   5.]
#  [  2.   6.   7.   8.   9.]
#  [  3.   7.  10.  11.  12.]
#  [  4.   8.  11.  13.  14.]
#  [  5.   9.  12.  14.  15.]]

rawDescription: Initializing a symmetric Theano dmatrix from its upper triangle


code:
def capital(uinput):
    slice4 = uinput[:4]
    result = ""
    for i in slice4:
       j = ord(i)
       j = j - 32
       k = chr(j)
       result += k
    print result

rawDescription: How do i get the output in one line as a string?

description: Get the output in one line as a string


rawDescription: python: help in calculation a proportion of 1's in binary sequence

description: Python: help in calculation a proportion of 1's in binary sequence


code:
# module/script.py

def main():
    try:
        import requests
        print 'requests is present. kudos!'
    except ImportError:
        raise RuntimeError('how the heck did you install this?')

rawDescription: Include python module (dependencies) installation in your python script


code:
lst = ['100  0.0', '101  0.7', '102  0.8', '103  0.7', '104  0.0', '105  0.7', '106  0.8', '107  0.0']
start = False
results = []
prevEndPos = -1
for e in lst :
    elems = e.split()
    pos = int(elems[0])
    score = float(elems[1])
    print pos, score
    if score >= 0.6 :
        if start == False :
            start = True
            startPos = pos
        prevEndPos = pos
    else :
        if start :
            start = False
            endPos = prevEndPos
            results.append((startPos, endPos))

print results

rawDescription: specifying start and end positions from python list

description: Specifying start and end positions from python list


code:
all(x>5 for x in xrange(3))
# <generator object <genexpr> at 0x1153bf7d0>

all([x>5 for x in xrange(3)])
# False

if all(x>5 for x in xrange(3)):
   print True
else:
   print False
# prints True

if all([x>5 for x in xrange(3)]):
   print True
else:
   print False
# prints False

rawDescription: Why sorted list detection does not work in this situation?


code:
def callme (func, *args, **kwargs):
    print "I will call", func
    print "With arguments:", args
    print "And keyword arguments:", kwargs
    func(*args, **kwargs)

def dateandtime (date, time, zone=None):
    print "The date:", date
    print "The time:", time
    print "Zone:", zone

callme(dateandtime, "01.01.1881. AC", "00:00:00")

rawDescription: Python OOP: Practice passing results of one method into another


rawDescription: Printing first field and (and only) matching fields in record, using awk


code:
from test1 import Params
# from test2 import Params
print Params.LINK_IP_ADDR

rawDescription: Python- how can I set the value for params from another file


code:
def replacer(instring, mapping):

    item = ''

    for char in instring:
        item += char
        yield item[:-5]
        item = item[-5:]
        if item in mapping:
            yield mapping[item]
            item = ''

    yield item

old_values = ('0000}', '0000J', '0000K', '0000L', '0000M', '0000N')
new_values = ('   -0', '   -1', '   -2', '   -3', '   -4', '   -5')
value_map = dict(zip(old_values, new_values))

file_snippet = '00000000000000010000}0000000000000000000200002000000000000000000030000J0000100000000000000500000000000000000000000' # each line is >7K chars long and there are over 6 gigs of text data

result = ''.join(replacer(file_snippet, value_map))
print result

rawDescription: multiple replacement values in python3

description: Multiple replacement values in python3


code:
class Student:
    sports = []
    def __init__(self, row):
       self.lname, self.fname, self.ID, self.sport = row
       self.sports.append(self.sport)
    def get(self):
       return (self.lname, self.fname, self.ID, self.sport)

reader = csv.reader(open('copy-john.csv'), delimiter=',', quotechar='"')
print "%-14s|%-10s|%-5s|%-11s" % tuple(reader.next()) # read header line from csv
print "-" * 45
students = list(map(Student, reader)) # read all remaining lines
for student in students:
    print "%-14s|%-10s|%-5s|%3s" % student.get()

# Printing all sports that are specified by students
for s in set(Student.sports): # class attribute
    print s, Student.sports.count(s)

# Printing sports that are not picked
allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
for s in set(allsports) - set(Student.sports):
    print s, 0

rawDescription: python csv into dictionary

description: Python csv into dictionary


code:
for i in tokenizer('abcd xvc  23432 "exampe" 366'):
  print i

rawDescription: Tokenizing User Input in Python

description: Tokenizing User Input in Python


code:
from timeit import Timer
from itertools import izip

n = 300

a = [str(f) for f in range(n)]
b = [str(f) for f in range(n)]

def func1():
    return ', '.join([aa+'='+bb for aa in a for bb in b if a.index(aa) == b.index(bb)])

def func2():
    list = []
    for i in range(len(a)):
        list.append('%s=%s' % (a[i], b[i]))
    return ', '.join(list)

def func3():
    return ', '.join('%s=%s' % t for t in zip(a, b))

def func4():
    return ', '.join('%s=%s' % t for t in izip(a, b))

def func5():
    pat = n * '%s=%%s, '
    return pat % tuple(a) % tuple(b)

d = dict(zip((1,2,3,4,5),('heavy','append','zip','izip','% formatting')))
for i in xrange(1,6):
    t = Timer(setup='from __main__ import func%d'%i, stmt='func%d()'%i)
    print 'func%d = %s  %s' % (i,t.timeit(10),d[i])

rawDescription: smartest way to join two lists into a formatted string

description: Smartest way to join two lists into a formatted string


code:
def solver(aList):
    if aList[0] == 10:
        print "SOLVED!"
        return None

    print "NOT SOLVED..."
    return next(aList)

def next(aList):
    # do some stuff
    # check some stuff
    aList[0] += 1
    return solver(aList)

if __name__ == "__main__":
    data = [0]
    solver(data)
    print data

rawDescription: Strange recursion behaviour in python

description: Strange recursion behaviour in python


code:
column = pd.MultiIndex(levels=[['A', 'B', 'C'], ['x', 'y', 'z']],
                       labels=[[i for i in range(3) for _ in range(3)], [0, 1, 2]*3])

df = pd.DataFrame(np.random.randint(1,10, size=(3, 9)),
                  columns=column, index=[1, 2, 3])

print df

#    A        B        C
#    x  y  z  x  y  z  x  y  z
# 1  5  7  4  7  7  8  9  1  9
# 2  8  5  1  8  5  9  4  4  2
# 3  4  9  6  2  1  4  6  1  6

rawDescription: How to Pandas read_csv multiple records per line


code:
class profile(object):
    #class variable used as a stack of subs list
    stack = [[]]

    def __init__(self, f):
        self.f = f

    def __call__(self, *args, **kw):
        func = dict(fname = self.f.__name__)

        #append the current function in the latest pushed subs list
        profile.stack[-1].append(func)

        #push a new subs list in the stack
        profile.stack.append([])

        #execution time of the actual call
        t0 = time.time()
        out = self.f(*args, **kw)
        func['etime'] = time.time() - t0

        #pull the subs list from the stack
        func['subs'] = profile.stack.pop()

        return out

    @classmethod
    def show(cls):
        import json #useful to prettify the ouput
        for func in cls.stack[0]:
            print json.dumps(func, sort_keys=True, indent=4)

rawDescription: Is it possible to do "extended" time monitoring in Python using a decorator?


code:
def A():
    print 'Processing in A!'

def B():

    choice = ''
    print 'Processing in B!'

    while choice.lower().strip() != 'r':
        choice = raw_input("Press R to repeat, RETURN to exit: ").lower().strip()
        if choice == '':
            return False
        if choice  == 'r':
            return True

while B():
    A()

rawDescription: Conditionally call function A after finishing function B


code:
import json

traffic = json.load(open('xxx.json'))

someitem = traffic.itervalues().next()
columns = list(someitem.keys())
print columns

rawDescription: Convert JSON to SQLite in Python - How to map json keys to database columns properly?


code:
import sys, shlex, readline, os, string
List, assign, call, add, sub, div, Pow, mul, mod, fac, duf, read,\
kill, clr, STO, RET, fib, curs = {}, "set", "get", "+", "-", "/", "^", "*",\
"%", "fact", "func", "read", "kill", "clear", ">", "@", "fib", "vars"
funcdict = {}
def fact(num):
    if num == 1: return 1
    else: return num*fact(num-1)
def builtin_op(op, stack):
    global List
    global funcdict
    if op == mul: stack.append(float(stack.pop())*float(stack.pop()))
    elif op == div: stack.append(float(stack.pop())/float(stack.pop()))
    elif op == sub: stack.append(float(stack.pop())-float(stack.pop()))
    elif op == add: stack.append(float(stack.pop())+float(stack.pop()))
    elif op == Pow: stack.append(float(stack.pop())**float(stack.pop()))
    elif op == assign: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == call: Eval(funcdict[stack.pop()], stack)
    elif op == fac: stack.append(fact(stack.pop()))
    elif op == duf: name = stack.pop(); funcdict[name] = stack.pop(); stack.append(name)
    elif op == mod: stack.append(float(stack.pop())%float(stack.pop()))
    elif op == kill: del List[stack.pop()]
    elif op == clr: os.system("clear")
    elif op == STO: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == RET: stack.append(List[stack.pop()])
    elif op == curs: stack.append(List)
    elif op == read: prompt = stack.pop(); List[prompt] = Eval(raw_input("%s "%prompt)); stack.append(List[prompt])
def Eval(expr, stack=None):
    ops = "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s"%(mul, add, sub, div, Pow, assign, call, fac, duf, mod, read, kill, clr, STO, RET, curs)
    if stack is None:
        stack = []
    expr, ops = shlex.split(string.lower(expr)), ops.split()
    for i in expr:
        if i[0] != ';':
            if i not in ops: stack.append(i)
            elif i in ops: builtin_op(i, stack)
        else: stack.append("ok")
    return stack[0]
def shell():
    try:
        x = ""
        while x != "quit":
            x = raw_input("star>   ")
            try: l = Eval(x)
            except KeyError: l = "does not exist"
            except: l = "parse error!"
            if l != None: print "   =>",l,"\n"
    except (EOFError, KeyboardInterrupt): print
if len(sys.argv) > 1:
    x = open(sys.argv[1], 'r'); l = x.readlines(); x.close()
    for i in l:
        if i[0] != ";":
            i = ' '.join(i.split())
            x = Eval(i)
            if x != None: print i,"\n   =>",x,"\n"
        else: pass
    shell()
else: shell()

rawDescription: small language in python

description: Small language in python


code:
from inspect import getframeinfo, stack

def debuginfo(message):
    caller = getframeinfo(stack()[1][0])
    print "%s:%d - %s" % (caller.filename, caller.lineno, message)

def grr(arg):
    debuginfo(arg)

grr("aargh")

rawDescription: Python debugging: get filename and line number from which a function is called?


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("cmd", help="Execute a command",
                    action="store", nargs='*')
args = parser.parse_args()
if args.cmd:
    cmd, name = args.cmd
    print "'%s' was '%s'-ed to the list of names." % (name, cmd)
else:
    print "Just executing the program baby."

rawDescription: Python argparse: How to change `--add` into `add` while still being an optional argument?


code:
import json

class MyObject(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return str(self.value)

print json.dumps(
    {
        'simple_string': 'i encode easily',
        'simple_int': 42,
        'complex': MyObject('this is a test')
    }, default=str, indent=2)

rawDescription: Is there a Python JSON encoder which JUST works?


code:
N=5
for n in range(0,N):
    for i in range(0,(n+1)):
        for j in range(i,(n+1)):
            print i, j - i, n - j

rawDescription: General bars and stars

description: General bars and stars


code:
from datetime import datetime

for s in re.findall(p, test_str):
    digits = re.sub(ur'\D', '', s)
    print datetime.strptime(digits, '%d%m%Y')

rawDescription: Regex: Match a malformed date

description: Regex: Match a malformed date


code:
headers = ['product', 'today', 'yesterday' ,' change', 'low', 'high']
for tr in soup.select("table.mtg-rates tr.rate-row"):
    item = dict(zip(headers, [td.get_text(strip=True) for td in tr.find_all("td")]))
    print item

rawDescription: Scraping data with BeautifulSoup issue

description: Scraping data with BeautifulSoup issue


code:
#!/usr/bin/env python

import gtk, glib

class ClipboardParse:
    def __init__(self):
        window = gtk.Window()
        window.set_title("example")
        window.resize(600,400)
        box = gtk.HBox(homogeneous = True, spacing = 2)
        self.buf = gtk.TextBuffer()
        textInput = gtk.TextView(self.buf)
        self.lbl = gtk.Label()
        box.add(self.lbl)
        window.add(box)
        window.connect("destroy", gtk.main_quit)
        window.show_all()
        self.clipboard = gtk.Clipboard()
        self.clipboard.connect("owner-change", self.renderText)

    def renderText(self, clipboard, event):
        print 'C {0} | E {1}'.format(clipboard, event)
        txt = self.clipboard.wait_for_text()
        self.lbl.set_text(txt)
        print txt
        return False

if __name__ == '__main__':
    ClipboardParse()
    gtk.main()

rawDescription: Get notified on clipboard change via D-Bus


code: print "I am being imported"

rawDescription: How to prevent a module from being imported twice?

description: Prevent a module from being imported twice


code:
word = word_list[0]      # first tuple
print word               # first tuple

rawDescription: Tuple indexing and printing

description: Tuple indexing and printing


code:
print repr('test\ttest 2'.expandtabs(8))
print repr('tessst\ttest 2'.expandtabs(8))
# output: 'test    test 2'
#         'tessst  test 2'

rawDescription: Length of tab character

description: Length of tab character


code:
import numpy as np
from scipy.optimize import curve_fit

def func(X, a, b, c):
    x,y = X
    return np.log(a) + b*np.log(x) + c*np.log(y)

# some artificially noisy data to fit
x = np.linspace(0.1,1.1,101)
y = np.linspace(1.,2., 101)
a, b, c = 10., 4., 6.
z = func((x,y), a, b, c) * 1 + np.random.random(101) / 100

# initial guesses for a,b,c:
p0 = 8., 2., 7.
print curve_fit(func, (x,y), z, p0)

rawDescription: Python curve_fit with multiple independent variables


code:
response = 'Hello there. You have no ID. Your name is: Mud.'
before, matched, after = response.partition('Your ID is: ')
if after:
    print after

rawDescription: Read characters in a string only if I have found the part that I need? [Python]


code:
from BeautifulSoup import BeautifulSoup

ht = '''

    some long text goes  here  and hopefully it
    will get picked up by the parser as content

'''
soup = BeautifulSoup(ht)

anchors = soup.findAll('a')
for a in anchors:
  a.previousSibling.replaceWith(a.previousSibling + a.string)

results = soup.findAll(text=lambda(x): len(x) > 20)

print results

rawDescription: Using Beautiful Soup Python module to replace tags with plain text


code:
count_line = 0
n = 0
for line in open('test.txt'):
    count_line = count_line +1
    fields = line.rstrip('\n').split('\t')
    ##print count_line, fields
    already_text = open('test_words.txt').read() if n > 0 else ''
    file = open('test_words.txt', 'w+')
    count_word = 0
    file.write(already_text)
    for words in fields:
        wordsplit = words.split()
        for word in wordsplit:
             count_word = count_word + 1
             print count_word, word
             file.write(str(count_word) + " " + word + '\n')
        file.close()
    n += 1

rawDescription: How to write words from list to file?

description: Write words from list to file


code:
# Matrix multiplication
def mult(matrix1,matrix2):
    # Matrix multiplication
    if len(matrix1[0]) != len(matrix2):
        # Check matrix dimensions
        print 'Matrices must be m*n and n*p to multiply!'
    else:
        # Multiply if correct dimensions
        new_matrix = zero(len(matrix1),len(matrix2[0]))
        for i in range(len(matrix1)):
            for j in range(len(matrix2[0])):
                for k in range(len(matrix2)):
                    new_matrix[i][j] += matrix1[i][k]*matrix2[k][j]
        return new_matrix

rawDescription: Rotation, scale and translate function for python


code:
if bool(a) != bool(b): # a xor b
    if a:
        print "a and not b"
    else:
        print "not a and b"

rawDescription: More pythonic way to handle this if condition?


code:
class SortAdaper(object):
    def __init__(self, obj ):
        self.obj = obj

class TimeLineSorter(SortAdaper):
    """ sorts in a timeline """
    def __lt__(self, other):
        return self.obj.timeFrom < other.obj.timeTo

class NameSorter(SortAdaper):
    """ sorts by name """
    def __lt__(self, other):
        return self.obj.name < other.obj.name

print sorted( myList, key=TimeLineSorter)
print sorted( myList, key=NameSorter)

rawDescription: What is the best way to sort items on one of the items values


code:
def permutations(A, B = ''):
    assert len(A) >= 0
    assert len(A) == len(set(A))
    if len(A) == 0:
        return [B]
    else:
        res = []
        for i in range(len(A)):
            res.extend(permutations(A[0:i] + A[i+1:], B + A[i]))

        return res

print permutations('word')

rawDescription: Recursive Python function to produce a list of anagrams


code:
t1 = [[3, 41], [3, 41], [5, 82], [10, 31], [11, 34], [14, 54]]
t2 = [[3,41], [3,41], [161, 160], [169, 260], [187, 540], [192, 10], [205, 23]]

nt1 = map(tuple, t1)
nt2 = map(tuple, t2)

st1 = set(nt1)
st2 = set(nt2)

from collections import defaultdict
d1 = defaultdict(int)
d2 = defaultdict(int)
for i in nt1:
    d1[i] += 1#counting element occurrence from first list

for i in nt2:
    d2[i] += 1 #counting element occurrence from second list

result_list = []

for i in st1.intersection(st2):
    min_count = min(d1[i], d2[i]) #selecting the minimum one to multiply
    result_list+=map(lambda x:list(i), xrange(0, min_count))

print result_list

rawDescription: Common elements between two lists of lists (intersection of nested lists)


code:
def main():
    fullName = raw_input("Please input your name: ")
    txt = raw_input("Type in any string: ")
    chars = getInitials(fullName)
    sv = removeChars(txt, chars)
    print "Full name is %s. Original String is %s" % (fullName, txt)
    print "Initials are %s. Resulting string is %s" % (chars, sv)

def getInitials(fullName):
    chars = ''.join(name[0].upper() for name in fullName.split())
    return chars

def removeChars(txt, chars):
    for char in txt:
        if char in chars:
            txt = txt.replace(char, '')
    return txt

main()

rawDescription: How to remove parameter from string?

description: Remove parameter from string


code:
l = ['GIS_FPC_PP,PERIMETER,MAT,LIGHTS,PARK,SPACES,LAT,LNG\n',
     '8266.99157657,453.7255798,Paved,1,American Legion,20,40.0188044212,-75.0547647126\n',
     '20054.5870679,928.20201772,Paved,1,Barnes Foundation Museum, ,39.9610355788,-75.1725011285\n']

newList = []
for i in range(0, len(l)):
    item = l[i]
    tempList = []
    if i != 0:
        itemSplit = item.split(',')
        tempList.append(itemSplit[-2].strip())
        tempList.append(itemSplit[-1].strip())
        newList.append(tuple(tempList))
print newList

rawDescription: Parse list to other list

description: Parse list to other list


code:
import re

text = """
November 5 - December 10
September 23 - December 16
"""

matches = re.findall("\w+\s\d+\s\-\s\w+\s\d+", text)
print matches

rawDescription: regex to find a specific pattern in python

description: Regex to find a specific pattern in python


rawDescription: python 2.7 XML lxml namespace prefix attribute issues

description: Python 2.7 XML lxml namespace prefix attribute issues


code:
ds = [('user1',['film','film','books']), ('user2',['film','books','books'])]
ds1 = map(lambda (x,y):(x,tuple(set((z,y.count(z)) for z in y))),ds)
print ds1

rawDescription: spark python reduced by key

description: Spark python reduced by key


code:
def bag_of_bigrams_words(words, score_fn=BigramAssocMeasures.chi_sq, n=200):
    bigram_finder = BigramCollocationFinder.from_words(words)
    bigrams = bigram_finder.nbest(score_fn, n)
    print type(words)
    print type(bigrams)
    return bag_of_words(words + bigrams)

rawDescription: NLTK bag of bigrams words function raises "Don't know how to concatenate types" error - python


code:
ra_list = [5,5,6,7,7]
e_list = [(7, 'A'), (7, 'B'), (6, 'C'), (5,'D')]

cate_dict = {}

#Creating dictionary of categories
for key,val in e_list:
    cate_dict.setdefault(key,[]).append(val)

import random
for i in ra_list:
    cate = random.choice(cate_dict.get(i,[0])) #To handle cases where `i` does not have category, if this is not the case then you do not need the second argument to `.get`. Use something other than `0` if that is one of the categories.
    if cate != 0:  #To handle cases where `cate` is someother value that can be false in boolean context.
        print i,cate

rawDescription: Python list within list

description: Python list within list


code:
for k, v in d.items():
    if v.count('s1')>=4 and v.count('s2')>=4:
        print k

rawDescription: using data from txt file to answer questions in python

description: Using data from txt file to answer questions in python


code:
class Node():
    def __init__(self, parent=None, key=None):
        self.parent = parent if parent != None else None
        self.left = None
        self.right = None
        self.key = key if key != None else None

class BST():
    def __init__(self):
        self.root = Node()

    def insertKey (self, key):
        #pdb.set_trace()
        # transverse till we find empty position
        if (self.root.key == None):
            self.root.key = key
        else:
            node = self.root
            while 1:
                if node.key < key:
                    if node.right is None:
                        node.right = Node(node, key)
                        break
                    else:
                        node = node.right
                else:
                    if node.left is None:
                        node.left = Node(node, key)
                        break
                    else:
                        node = node.left

    def inOrder (self, node):
        #pdb.set_trace()
        if node != None:
            self.inOrder (node.left)
            print node.key
            self.inOrder (node.right)

    def printLeft (self, node):
        if node != None:
            self.printLeft (node)
            print node.key

    def debugAll (self):
        self.inOrder (self.root)
        #self.printLeft (self.root)

    def fromArray (self, numbers):
        srt = sorted(numbers)
        print srt
        length = len(srt)
        mid = length/2
        rootEle = srt[mid]
        self.insertKey (rootEle)
        for i in range (1, mid+1):
            try:
                #pdb.set_trace()
                self.insertKey (srt[mid-i])
                self.insertKey (srt[mid+i])
            except IndexError:
                pass

bst = BST()
bst.fromArray ([1,2,4,3,6,5,10,8,9])
bst.debugAll ()

rawDescription: Inorder tree walk not working

description: Inorder tree walk not working


code:
print [map(str.strip, line.split(':')) for line in re.split('<.*?>', string) if ':' in line]
# [['Your Hunger Level', 'Very Hungery'], ['Food You Crave', 'Tomato']]

rawDescription: How to match the following with regex in python?

description: Match the following with regex in python


code:
print "%s(%d): %s" % (bname, lineno,
                      linecache.getline(filename, lineno)),

rawDescription: Python trace module - Trace lines as they are executed, but save to file, rather than stdout


code:
import pandas as pd
import numpy as np
import itertools

df = pd.DataFrame({
"strings":["the dog", "lazy dog", "The fox jump"],
"value":["a","b","c"]})

w = df.strings.str.split()
c = w.map(len)
idx = np.repeat(c.index, c.values)
#words = np.concatenate(w.values)
words = list(itertools.chain.from_iterable(w.values))
s = pd.Series(words, index=idx)
s.name = "words"
print df.join(s)

rawDescription: How can I create new rows in a pandas data frame containing the words in a string of an existing row?

description: Create new rows in a pandas data frame containing the words in a string of an existing row


code:
for x in list1:
    y = max(list2, key=lambda y: sum(1 for w in x if w in y))
    print x, y

rawDescription: How to find highest probable matches between arrays

description: Find highest probable matches between arrays


code:
import re
print re.split(r"\n(?=Layer)",x)

rawDescription: How can I split this string in Python?

description: Split this string in Python


code:
from datetime import datetime
import pytz
import calendar

dt = datetime.now(pytz.utc)
secs = calendar.timegm(dt.utctimetuple())
print dt, secs

rawDescription: Convert date to second from a reference - Python


code:
inp = """
def return_words(string):
    return [i for i in string if i.isdigit()]

return_words(string)
   """

list_of_inputs = ['inputstring1', 'inp2']

for s in list_of_inputs:
    print '{}'.format(s), '->', timeit(stmt=inp,
                                       number=1000000,
                                       setup="string = '{}'".format(s))

rawDescription: Time function with arguments in python

description: Time function with arguments in python


code:
import random

class ManualSrc(object):
    def get(self):
        return float(raw_input('gimme a number - '))

class RandomSrc(object):
    def get(self):
        return random.random()

class BreakingSrc(object):
    pass

def create_source(choice):
    if choice == 0:
        return ManualSrc()
    elif choice == 1:
        return RandomSrc()
    elif choice == 2:
        return BreakingSrc()
    else:
        raise ValueError('source choice parameter {} not valid'.format(choice))

if __name__ == '__main__':
    for use_src in range(4):
        print 'using source choice {}'.format(use_src)
        src = create_source(use_src)
        print src.get()

rawDescription: Duck-typable classes, ABC, iheritance, __new__


code:
def highlight_many(text, keywords):
    replacement = "\033[91m" + "\\1" + "\033[39m"
    text = re.sub("(" + "|".join(map(re.escape, keywords)) + ")", replacement, text, flags=re.I)
    print text

rawDescription: Highlight multiple words using Regex function


code:
from bs4 import BeautifulSoup

data = """

          Put me Elsewhere

"""

replacement = """

{text}

"""

soup = BeautifulSoup(data, 'html.parser')
for td in soup.select('td.thisIsMyClass'):
    td.replace_with(BeautifulSoup(replacement.format(text=td.a.text), 'html.parser'))

print soup.prettify()

rawDescription: Replacing tags of one kind with tags of another in BeautifulSoup


code:
import multiprocessing

def myfunction(i, event):
    if not event.is_set():
        print i
    if i == 20:
        event.set()

if __name__ == "__main__":
    p= multiprocessing.Pool(10)
    m = multiprocessing.Manager()
    event = m.Event()
    for i in range(100):
        p.apply_async(myfunction , (i, event))
    pool.close()

    event.wait()  # We'll block here until a worker calls `event.set()`
    pool.terminate() # Terminate all processes in the Pool

rawDescription: Kill all Pool workers in python multiprocess


code:
number = input()
if number >= 0:
   if number % 2 == 0:
       print "even"
   else:
       print "odd"
else:
   print "negative"
print "all done"

rawDescription: The equivalent of a GOTO in python

description: The equivalent of a GOTO in python


code:
result = []
for i in range(len(fruits)):
    total = 0
    for j in range(len(values)):
        total += values[j][i]
    result.append([total, fruits[i]])
print result

rawDescription: Sum float values at each index point in nested lists


code:
candidate_proxies = ['http://proxy1.example.com:1234',
                     'http://proxy2.example.com:1234',
                     'http://proxy3.example.com:1234']
for proxy in candidate_proxies:
    print "Trying HTTP proxy %s" % proxy
    try:
        result = urllib.urlopen("http://www.google.com", proxies={'http': proxy})
        print "Got URL using proxy %s" % proxy
        break
    except:
        print "Trying next proxy in 5 seconds"
        time.sleep(5)

rawDescription: How can I open a website with urllib via proxy in Python?

description: Open a website with urllib via proxy in Python


code:
a = "Kitty Bates"
sev=[ ord(x) for x in a ]
S= sum(sev)

while(1):
    try:
        print chr(S)
        break   # will break on valid ASCII

    except:
        S= S-44

rawDescription: Subtracting depending on an if function in python


code:
from flask import jsonify

@app.route('/load_ajax', methods=["GET", "POST"])
def load_ajax():
    if request.method == "POST":
        data = request.get_json()
        print list(data)
        return jsonify(location=url_for('thankyou'))

@app.route('/thankyou')
def thankyou():
    return render_template('thankyou.html')

rawDescription: Where do I put my redirect or return_template


code:
all_scores = []

for x in range(5):
   all_score.append( int(input("score: ")) )

print all_scores

rawDescription: Looping test scores in file input/output


code:
def TitleSearch():
    titleSearch = input("Please type in Title, \n")
    for row in everything:
        title = row[2]
        if title.find(titleSearch) != -1:
            print row

rawDescription: Python sketch not working as intended (reading data from CSV)


code:
def read_images(path, sz=None):
    X, y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for filename in filenames:
            subject_path = os.path.join(dirname, filename)
            try:
                im = Image.open(subject_path)
            except IOError as err:
                print "I/O error: {e}: {f}".format(e=err, f=subject_path)
            except:
                print "Unexpected error:", sys.exc_info()[0]
                raise
            else:
                im = im.convert("L")
                # resize to given size (if given)
                if (sz is not None):
                    im = im.resize(sz, Image.ANTIALIAS)
                X.append(np.asarray(im, dtype=np.uint8))
                y.append(subject_path)
    return [X, y]

rawDescription: Face Recognition - How to return the correct image?


code:
from __future__ import division
import multiprocessing as mp

def read_occupation( matrix,i ):
    matrix[i]=[1,2,3]

def main():
    manager=mp.Manager()
    matrix=manager.list([ [0] ] * 4)

    for index in xrange(0,3,2):
        p1 = mp.Process( target=read_occupation, args=(matrix,index) )
        p2 = mp.Process( target=read_occupation, args=(matrix,index+1) )

        p1.start()
        p2.start()

        p1.join()
        p2.join()

    print matrix

if __name__ == '__main__':
    main()

rawDescription: modified a given list inside a function

description: Modified a given list inside a function


code:
import os

def findup(filename):
    drive, thisdir = os.path.splitdrive(os.getcwd())
    while True:
        fullpath = os.path.join(drive, thisdir, filename)
        if os.path.isfile(fullpath):
            return fullpath
        if thisdir == os.path.sep: #root dir
            raise LookupError('file not found: %r' % filename)
        thisdir = os.path.dirname(thisdir)

os.makedirs('/tmp/foo/bar/baz/qux')
open('/tmp/foo/findme.txt', 'w').close()
os.chdir('/tmp/foo/bar/baz/qux')
print findup('findme.txt')

rawDescription: How to find a file upwardly in Python?

description: Find a file upwardly in Python


code:
today = datetime.date(2013, 06, 26)
dates = [today + datetime.timedelta(days=i) for i in range(-7 - today.weekday(), 14 - today.weekday())]

rawDescription: Build array of dates in last week, this week and next week


code:
d = dict()

d['that']= 13
d['the']=9
d['to']=8
d['we']=8
d['here']=8
d['a']=7
d['and']=6
d['nation']=5
d['not']=5
d['for']=5
d['can']=5
d['of']=5
d['have']=5

ordered = dict()
for e in d:
  if d[e] not in ordered:
    ordered[d[e]] = []
  ordered[d[e]].append(e)

for e in reversed(sorted(ordered.keys())):
  for v in sorted(ordered[e]):
    print e, v

rawDescription: How would I sort this Dictionary by value then if the same sort it by key value?


code:
datain, lonsin = addcyclic(np.squeeze(Q), lons)
lons, Q = m.shiftdata(lonsin, datain = np.squeeze(Q), lon_0=180.)
print lons
lon, lat = np.meshgrid(lons, lats)
x,y = m(lon, lat)
cs = m.pcolor(x,y,datain,cmap=plt.cm.RdBu_r)

rawDescription: Plotting netCDF data with Python: How to change grid?


code: print json.dumps(myvar)

rawDescription: Accessing nested JSON elements in Python


rawDescription: Making a function wich argument is a list and return a tuple with the number of 'twos' in it. EG:


code:
def dict_compare(da, db):
    for k, v in da.iteritems():
        if isinstance(v, dict): #if the value is another dict:
            dict_compare(v, db[k]) #enter into the comparison function again!
        else:
            if v != db[k]:
                print 'values not equal at', k

rawDescription: Python: Tool to compare pairs of dicts of varying deepness?


rawDescription: pick the minimum value out of a list of sublists and display another value in that sublist with the minimum

description: Pick the minimum value out of a list of sublists and display another value in that sublist with the minimum


code:
from pandas import DataFrame
data2 = []
for i in range(0,len(power_data)):
    data2.append({'TIME': power_data[i][0], 'Watts': power_data[i][1]})

powd = DataFrame(data2)
print powd

# the above can be changed with this line:
# powd = DataFrame(power_data, columns=['TIME', 'Watts'])
# see sugestion in comments

print ggplot(aes(x='TIME', y='Watts'), data=powd) + \
    geom_point(color='lightblue') + \
    geom_line(alpha=0.25) + \
    stat_smooth(span=.05, color='black') + \
    ggtitle("Power comnsuption over 13 hours") + \
    xlab("Time") + \
    ylab("Watts")

rawDescription: Python: How can I use ggplot with a simple 2 column array?


code:
import itertools

def f(x,y): return x+y
def g(x,y): return 2*x-y

def outer_product_thing(F,M):
    keys = M.keys()
    ranges = [M[k] for k in keys]

    for rx in itertools.product(*ranges):
        mapping = dict(zip(keys, rx))
        value   = 1

        for func in F:
            terms =   F[func]
            value *=  func(mapping[terms[0]],
                           mapping[terms[1]])

        print "Evaluated: ", mapping
        yield value

F = {f:("a","b"),
     g:("b","c")}
M = {"a":range(0,2),
     "b":range(0,3),
     "c":range(0,7)}

for result in outer_product_thing(F,M):
    print result

rawDescription: Dynamic number of loops

description: Dynamic number of loops


code:
from math import sqrt
n = raw_input("Example Number? ")
n = float(n)
sqaureRootOfN = sqrt(n)

print '-'*75
print ' # of Decimals', ' ' * 8, 'New Root', ' ' * 10, 'Percent error'
print '-'*75
for a in range(0,10):
    preRoot = float(int(sqaureRootOfN * 10**a))
    newRoot = preRoot/10**a
    percentError = (n - newRoot**2)/n*100
    print " {: <20}{: <25}{: <18}".format(a, newRoot, str(percentError) + ' %')

rawDescription: Formatting in Python 2.7

description: Formatting in Python 2.7


code: print f.read    # this prints out the object reference

rawDescription: Python file.read() function output?

description: Python file.read() function output


code:
self.hands = {}
self.hands["hand1"] = Hand([card1, card2])
self.hands["hand2"] = Hand([card1, card2])
print self.hands["hand1"]

rawDescription: dynamic class instantiation confusion in python

description: Dynamic class instantiation confusion in python


code:
from itertools import product
print [sum(items, ()) for items in product(a, b)]

rawDescription: Generate a lattice using python

description: Generate a lattice using python


code:
import csv
import random

class Node(object):
    _index = 0
    __slots__ = ('id', 'name', 'parent')

    def __init__(self, name, parent=None):
        self.id = Node._index
        Node._index += 1

        self.name = name
        self.parent = parent

def write_roots(roots, writer):
    nodes = []
    for x in xrange(roots):
        node = Node('root{}'.format(x))
        root_nodes.append(node)
        writer.writerow([node.id, node.name, ''])
    return nodes

def write_row(parent_nodes, writer, children):
    nodes = []
    for parent_node in parent_nodes:
        for x in xrange(children):
            node = Node('cat{}-{}'.format(parent_node.id, x), parent_node.id)
            nodes.append(node)
            writer.writerow([node.id, node.name, node.parent])
    return nodes

roots = 1000

with open("data.csv", "wb") as f:
    writer = csv.writer(f)

    nodes = write_roots(roots, writer)

    for i in xrange(9):
        print 'Writing row {}'.format(i + 1)
        nodes = write_row(nodes, writer, random.randrange(1, 11))

rawDescription: Write an efficient python adjacency tree generation script


code: print ''.join([i[i.index(' : ')+3:] for i in open("Sample.txt").readlines() if i[:i.index(' : ')] in A])

rawDescription: Reading Specific Python list from a text file


code:
import socket

CRLF = "\r\n"

request = [
    "GET / HTTP/1.1",
    "Host: www.example.com",
    "Connection: Close",
    "",
    "",
]

# Connect to the server
s = socket.socket()
s.connect(('www.example.com', 80))

# Send an HTTP request
s.send(CRLF.join(request))

# Get the response (in several parts, if necessary)
response = ''
buffer = s.recv(4096)
while buffer:
    response += buffer
    buffer = s.recv(4096)

# HTTP headers will be separated from the body by an empty line
header_data, _, body = response.partition(CRLF + CRLF)

print header_data

rawDescription: Getting a raw, unparsed HTTP response

description: Getting a raw, unparsed HTTP response


code:
import numpy as np

width=3
data = np.array([4,2,5,6,7,5,4,3,5,7])

result = data[:(data.size // width) * width].reshape(-1, width).mean(axis=1)

print result

rawDescription: Binning a numpy array

description: Binning a numpy array


code:
cat = Categories.objects.get(id=1)
print cat.get_spamwords_as_list()

rawDescription: Split field to array when accessed

description: Split field to array when accessed


code:
list1, list2, list3 = [1, 2, 3], [2, 3, 1], [1, 2, 3, 4]
print set(list1) == set(list2)    # True
print set(list2) == set(list3)    # False
print set(list1) == set(list3)    # False

rawDescription: Python compare tuples

description: Python compare tuples


code:
from datetime import datetime
from itertools import groupby
def min_max_per_hour(iterable):
    for dayhour, grouped in groupby(iterable, lambda dt: (dt.date(), dt.hour)):
        minimum = next(grouped)  # first object is the minimum for this hour
        maximum = minimum  # starting value
        for dt in grouped:
            maximum = dt   # last assignment is the maximum within this hour
        yield (minimum, maximum)
textfile = '''\
16-07-2015 18:08:20
16-07-2015 18:08:22
16-07-2015 18:08:30
16-07-2015 18:08:40
17-07-2015 10:04:01
17-07-2015 10:14:31
17-07-2015 10:14:59
17-07-2015 12:24:11
'''.splitlines()
datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in textfile)
for mindt, maxdt in min_max_per_hour(datetimes):
    print mindt, maxdt

rawDescription: Minimum and maximum timestamps per hour bucket


code:
import csv

counter = {}
data = []

with open('report.csv','rb') as infile:
  for row in csv.reader(infile):
    key = row[0]
    if key not in counter:
      counter[key] = 1
    else:
      counter[key] += 1
    row.insert(0,counter[key])
    data.append(row)

for row in sorted(data,key=lambda x: (x[1],x[0])):
  print row

rawDescription: python read csv file as dictionary and sort and increment a counter

description: Python read csv file as dictionary and sort and increment a counter


code:
for i, n in enumerate([10,5,30]):
    print i,n

rawDescription: Python: is index() buggy at all?

description: Python: is index() buggy at all


code:
import re

s = '''(ArrayOfNotificationData){NotificationData[] = (NotificationData){Id = 1 Title = "notification 1" Message = "bla bla." Published = 2000-01-01 00:00:00}, (NotificationData){Id = 2 Title = "notification 2" Message = "bla bla." Published = 2000-01-01 00:00:00},}'''

def f(d):
    for k, v in d.items():
        if v is None:
            d[k] = ''
    return d

def g(reg, rep):
    c1 = s
    c2 = ''
    while c1 != c2:
        c2 = c1
        c1 = re.sub(reg, lambda m: rep.format(**f(m.groupdict())), c1)
    print c1

g('(?P<m>\w+)\s+=\s+(?:(?P<v>\\d+-\\d+-\\d+ \\d+:\\d+:\\d+|\w+)|"(?P<v3>[^"]*)")|(?:(?:\\w|\\[|\\])+\\s*=\\s*)?\\((?P<m2>\w+)\\){(?P<v2>[^}{]*)}\s*,?', '<{m}{m2}>{v}{v2}{v3}</{m}{m2}>')

rawDescription: soap response to xml

description: Soap response to xml


code:
doc = minidom.parse('data.xml')
commandsTagList = doc.getElementsByTagName("virtualMachine")
for command in commandsTagList:
    scriptToBeRanPython = command.getElementsByTagName("scriptToBeRanPython")[0]
    parameter = scriptToBeRanPython.getElementsByTagName("paramaters")[0].firstChild.nodeValue
    source = scriptToBeRanPython.getElementsByTagName("sourceName")[0].firstChild.nodeValue
    destination = scriptToBeRanPython.getElementsByTagName("destinationName")[0].firstChild.nodeValue
    commandString = source + ' ' + parameter + ' ' + destination
    print commandString

rawDescription: Python - xml parsing with Minidom - How do I iterate through each <parent> and get a list of <child> for that <parent>?


code:
import re, itertools

def guess_decode(s):
    encodings = ['cp1251', 'cp1252', 'utf8']

    for steps in range(2, 10, 2):
        for encs in itertools.product(encodings, repeat=steps):
            r = s
            try:
                for enc in encs:
                    r = r.encode(enc) if isinstance(r, unicode) else r.decode(enc)
            except (UnicodeEncodeError, UnicodeDecodeError) as e:
                continue
            if re.match(ur'^[\w\sа-яА-Я]+$', r):
                print 'debug', encs, r
                return r

print guess_decode(u'ZÐµÐ¼Ñ„Ð¸Ñ€Ð°')
print guess_decode(u'Zåìôèðà')
print guess_decode(u'ZÃ¥Ã¬Ã´Ã¨Ã°Ã\xA0')

rawDescription: How to decode cp1252 string?

description: Decode cp1252 string


code:
data = csv.reader(open("my_text.csv"))
columns = zip(*data)
dataMap = {d[0]:d[1:] for d in columns}
print dataMap["Timestamp"] # or whatever

rawDescription: Python: creating variable name from header row in csv read


code:
from datetime import datetime

def benchmark(fn):
    print "Benching %s (run 1000 times)" % fn.__name__

    start = datetime.now()
    for x in xrange(1000):
        # run key function on something approx 100 chars long
        fn('asdf1234sdfg234jhd88123j2134 - 123d34123djfsk'*2)
    print "\t%s" % (datetime.now() - start)

benchmark(regex_key)
benchmark(fast_key)
benchmark(faster_key)

rawDescription: python natural comparison between strings?

description: Python natural comparison between strings


code:
def change(c):
    a = c
    print a

change(4)
print a

rawDescription: Django persistent API connections between requests


code:
import inspect
import toto

for name, value in inspect.getmembers(toto):
    # First ignore python defined variables
    if name.startswith('__'):
        continue

    # Now only browse classes
    if not inspect.isclass(value):
        continue
    print "Found class %s with doctring \"%s\"" % (name, inspect.getdoc(value))

    # Only browse functions in the current class
    for sub_name, sub_value in inspect.getmembers(value):
        if not inspect.ismethod(sub_value):
            continue
        print "  Found method %s with docstring \"%s\"" % \
            (sub_name, inspect.getdoc(sub_value))

rawDescription: Embedded python interpreter, generate stub source code for autocompletion


code:
import itertools

def valid_combination(combination):
    return len(combination)>0 and combination[0]==2

def product_with_validation(validation_func, *element_list):
    return (combination for combination in itertools.product(*element_list)
           if valid_combination(combination))
print list(product_with_validation(valid_combination, range(10), range(10)))

rawDescription: Permutating with repetition and rules between possible values


code:
d = {}

for x in itertools.chain(data.values()):
        for k,v in x.items():
            if k in d:
                d[k].append(v)
            else:
                d[k] = [v]

av = {}

for k,v in d.items():
    av[k]=sum(v)/len(v)

print av

rawDescription: Average from two list of dictionaries

description: Average from two list of dictionaries


code:
masks = [('A', r'[a-zA-Z]'), ('B', r'\d'), ('C', r'\d')];
print pd.concat([~(df_1[col].astype(str).str.contains(regex)) for col, regex in masks], axis=1)

rawDescription: Substitute values from one pandas data frame to another based on condition


code:
from rdflib.plugins.parsers.ntriples import NTriplesParser, Sink
# The NTriplesParser is what is used for a format="nt" parsing as found:
#  https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/rdflib/plugins/parsers/nt.py#L2

# Example NT file from:
#  https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/test/nt/anons-01.nt

class StreamSink(Sink):
    """
    A sink is used to store the results of parsing, this almost matches the sink
    example shown in ntriples:
      https://github.com/RDFLib/rdflib/blob/395a40101fe133d97f454ee61da0fc748a93b007/rdflib/plugins/parsers/ntriples.py#L43
    """
    def triple(self, s, p, o):
        self.length += 1
        print "Stream of triples s={s}, p={p}, o={o}".format(s=s, p=p, o=o)

if __name__ == "__main__":
    # Create a new parser and try to parse the example NT file.
    n = NTriplesParser(StreamSink())
    with open("./anons-01.nt", "r") as anons:
        n.parse(anons)

rawDescription: Using python rdflib parsers without the graph object


code: print i.rstrip('\n')

rawDescription: file.readlines() leaving blank lines

description: File.readlines() leaving blank lines


code: print sum(x == 4 for x in my_dict.values())

rawDescription: How to find number of matches in the array or dictionary?

description: Find number of matches in the array or dictionary


code:
def __init__(self, x):
    self.x = x

def printX(self):
    print self.x

Test = type('Test', (object,), {'__init__': __init__, 'printX': printX})

rawDescription: Dynamically creating a class from file in Python


code:
urls = [d['href'] for d in soup.select("p.start-download [href]")]

print urls[0]

rawDescription: How to filter and take only one download link?

description: Filter and take only one download link


code:
y="A %{foo} is a %{bar}"
for a,b,c,d in y._formatter_parser(): print b

rawDescription: Check if a string to interpolate provides expected placeholders


rawDescription: printing and formatted list in python

description: Printing and formatted list in python


code:
user_choice = raw_input('Please click ENTER button to close application')
if not user_choice:
    print "ABORTED"
    quit()

rawDescription: Can I use xfvbwrapper with selenium to simulate a headless chromium

description: Use xfvbwrapper with selenium to simulate a headless chromium


code:
import string
import collections

def hapax(text):
    # Remove Punctuation from the Input text.
    text = text.translate(string.maketrans("",""), string.punctuation)
    print "Debug 1- After remove Punctuation:", text

    # ignore:- Lower/upper/mix cases
    text = text.lower()
    print "Debug 2- After converted to Lower case:", text

    #- Create Default dictionary. Key is word and value
    word_count = collections.defaultdict(int)
    print "Debug 3- Collection Default Dictionary:", word_count

    #- Split text and update result dictionary.
    for word in text.split():
        if word:#- Ignore whitespace.
            word_count[word] += 1

    print "Debug 4- Word and its count:", word_count

    #- List which save word which value is 1.
    hapax_words = list()
    for word, value in word_count.items():
        if value==1:
            hapax_words.append(word)

    print "Debug 5- Final Hapax words:", hapax_words

hapax('yo i went jogging then yo i went jogging tuesday wednesday some punctuation ? I and & ')

rawDescription: Writing a program to print hapax's from a string


code:
# The method that gets called when a new email arrives.
# Replace it with something better.
def dosync(self):
    print "Got an event!"

rawDescription: Find new messages added to an imap mailbox since I last checked with python imaplib2?


code:
for inner_lst,inner_tup in zip(lsts,tups):
   for ind,depth in enumerate(inner_lst):
          depth[depth.index(0)]= inner_tup[ind]

print lsts

rawDescription: Iterating through list of lists of lists and list of tuples


code:
import csv
from collections import defaultdict

# a dictionary whose value defaults to a list.
data = defaultdict(list)
# open the csv file and iterate over its rows. the enumerate()
# function gives us an incrementing row number
for i, row in enumerate(csv.reader(open('data.csv', 'rb'))):
    # skip the header line and any empty rows
    # we take advantage of the first row being indexed at 0
    # i=0 which evaluates as false, as does an empty row
    if not i or not row:
        continue
    # unpack the columns into local variables
    _, zipcode, level = row
    # for each zipcode, add the level the list
    data[zipcode].append(float(level))

# loop over each zipcode and its list of levels and calculate the average
for zipcode, levels in data.iteritems():
    print zipcode, sum(levels) / float(len(levels))

rawDescription: Python CSV - Need to Group and Calculate values based on one key


code:
if ranking_list:
    print ranking_list

rawDescription: Scrapy:newbie attempts to pass the null value


code:
permissions_bytype={}
for permission_entry in permissions: #iterating over something like: [{'user': 1, 'type': u'read'}, {'user': 4, 'type': u'admin'}, {'user': 4, 'type': u'read'}]
    if permission_entry["type"] not in permissions_bytype:
        permissions_bytype[permission_entry["type"]] = {}
    for key,val in permission_entry.iteritems():
        if key <> "type": #is user or group
            if key not in permissions_bytype[permission_entry["type"]]:
                permissions_bytype[permission_entry["type"]][key] = []
            permissions_bytype[permission_entry["type"]][key].append({"id":val})

rawDescription: Python dictionary group by a field

description: Python dictionary group by a field


code:
import pandas as pd

idx = pd.Index([u'Newal', u'Saraswati Khera', u'Tohana'])
print idx
Index([u'Newal', u'Saraswati Khera', u'Tohana'], dtype='object')

print idx.tolist()
[u'Newal', u'Saraswati Khera', u'Tohana']

print list(idx)
[u'Newal', u'Saraswati Khera', u'Tohana']

rawDescription: how to convert Index into list?

description: Convert Index into list


code:
from datetime import date

def display_birthday_wishes(name, month, year):
    p = current_year = date.today().year
    current_age = (int(p)-int(year))

    return "Hi, " + name + ". You are " + str(current_age) +\
    " years old this year! Here's a bouquet of flowers for you!"

print display_birthday_wishes("David", 4, 1998)

rawDescription: Wrap code in a function

description: Wrap code in a function


code:
url = 'http://www.bhaskar.com/'
data = urllib2.urlopen(url).read().decode("utf-8-sig")
regex = re.compile(ur"[^\u0900-\u097F]+")
hindionly = regex.sub("foo", data)
print hindionly.encode("utf-8")

rawDescription: Extract Only Unicode Characters from a String using Regular Expressions


rawDescription: why do I keep getting the same answer with this conditional, python pandas

description: Why do I keep getting the same answer with this conditional, python pandas


code:
import feedparser
d = feedparser.parse('/path/to/your/xmlfile.xml')
for entry in d.entries:
    print entry.title

rawDescription: How to parse the xml file in the google-blogger in python?

description: Parse the xml file in the google-blogger in python


code:
from xml.etree import cElementTree as ET
##ET.register_namespace('tst', 'urn:test')
country = ET.Element("country")
name = ET.SubElement(country, "{urn:test}name")
name.text = "Canada"
population = ET.SubElement(country, "{urn:test}population")
population.text = "34M"
print prettify(country)

rawDescription: More compact ElementTree or lxml Namespaces


code:
import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(6,6)
fig = plt.figure(1)
fig.clf()
ax = fig.add_subplot(1,1,1)
cax = ax.imshow(data, interpolation='nearest', vmin=0.5, vmax=0.99)
print "Before adding the colorbar:"
print fig.axes
fig.colorbar(cax)
print "After adding the colorbar:"
print fig.axes

rawDescription: matplotlib: any way to get existing colorbars?

description: Matplotlib: any way to get existing colorbars


code:
with open('abc') as f:
    next(f)  #skip header
    lis = [[city,float(val)]  for city, val in( line.split() for line in f)]
    print lis
    #[['Mumbai', 98.3], ['London', 23.23], ['Agra', 12.22]]

rawDescription: Sort the top ten results

description: Sort the top ten results


code:
def invert(dico):
    # map values to keys, note the nested comprehension
    return {i: k for k, v in dico.items() for i in v}

def decipher(code, dico):
    # note that this result can be cached or even just
    # computed outside the function
    decoder = invert(dico)

    # pass each code through the decoder map
    return [decoder[c] for c in code]

print decipher([1, 9, 108], {
    'A': [1, 9],
    'B': [12, 19],
    'C': [78, 108]
})

rawDescription: AttributeError in dictionary

description: AttributeError in dictionary


code:
import requests
import lxml, lxml.html

r = requests.get('http://www.lldj.com/pastresult.php')

html = lxml.html.fromstring(r.text)

imgs =  html.cssselect('img')

for x in imgs:
    src = x.attrib['src']
    if src.startswith('images/Balls'):
        print src[-6:-4]

rawDescription: Is it possible to scrap images from a webpage, convert them to numbers and save them to a file?


code:
from itertools import groupby
from operator import itemgetter
list_of_dicts = [{'timestamp': u'1389631816', 'value': u'0'},
 {'timestamp': u'1389633136', 'value': u'0'},
 {'timestamp': u'1389633256', 'value': u'1'},
 {'timestamp': u'1389633316', 'value': u'1'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633196', 'value': u'0'},
 {'timestamp': u'1389633316', 'value': u'1'}]
for value, group in groupby(list_of_dicts, key=itemgetter('value')):
    print 'Group for value {}'.format(value)
    for d in group:
        print d

rawDescription: Group dict values into chunks with python


code:
def printOutput(start, end, makeList):
  if start == end == None:
      return
  else:
      outputFile = open('out'+uniprotID+'.txt','w')#file for result output
      while start <= end:
          print start
          print end
          inRange = makeList[start]
          start += 1
          outputFile.write(inRange)

rawDescription: file output error with correct values

description: File output error with correct values


code:
from sqlalchemy import create_engine
engine = create_engine("mssql://me:pass@localhost/testdb")
for row in engine.execute("select user_id, user_name from users"):
    print row.user_id, row.user_name

rawDescription: MSSQL in python 2.7

description: MSSQL in python 2.7


code:
from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    pool = Pool(processes=4)              # start 4 worker processes
    result = pool.apply_async(f, [10])    # evaluate "f(10)" asynchronously
    print result.get(timeout=1)           # prints "100" unless your computer is *very* slow
    print pool.map(f, range(10))          # prints "[0, 1, 4,..., 81]"

rawDescription: Python multiprocessing an enourmous amount of data


code:
num_stars = 10
dtype = numpy.dtype([('x', float), ('y', float), ('colour', float)])
a = numpy.recarray(num_stars, dtype=dtype)
a.colour = numpy.arange(num_stars)
print a.colour

rawDescription: Getting attributes from arrays of objects in NumPy


code:
print 'Before the loop'
for i in range(5):
    print 'Start of the loop, i = {}'.format(i)
    if i == 2:
        print 'i is set to 2, continuing with the next iteration'
        continue
    if i == 3:
        print 'i is set to 3, breaking out of the loop'
        break
    print 'End of the loop'
print 'Loop has completed'

rawDescription: functionalities of continue/ break on the code in Python and its output

description: Functionalities of continue/ break on the code in Python and its output


code:
import os
import zipfile

with zipfile.ZipFile('foobar.zip', 'r') as inzipfile:
    for infile in (name for name in inzipfile.namelist() if name[-1] != '/'):
        if 'foo' in os.path.split(infile)[0]:
            print inzipfile.open(infile,'r').read(),

rawDescription: Selective extracting and opening for zipfile in python


code:
import re
import json

s = """Format= {
Window_Type="Tabular",
Tabular= { Num_row_labels=5 } }"""

s = "{" + re.sub(r'([a-zA-Z0-9_]+)=',r'"\1":', s) + "}"
obj = json.loads(s)

if obj['Format']['Window_Type'] == "Tabular":
    print "Its tabular!"
    numrows = obj['Format']['Tabular']['Num_row_labels']
    print "Numrows:", numrows
    if numrows >= 5:
        print "Print this fileName"

rawDescription: Parse and Print out the atttribute values from a text file using Python 3.2


code:
import os
from os.path import isdir, join

def cats_and_subs(root='notes'):
    """
    Collect categories and subcategories.
    """
    categories = filter(lambda d: isdir(join(root, d)), os.listdir(root))
    sub_categories = []
    for c in categories:
        sub_categories += filter(lambda d: isdir(join(root, c, d)),
            os.listdir(join(root, c)))

    # categories and sub_categories are arrays,
    # categories would hold stuff like 'science', 'maths'
    # sub_categories would contain 'Quantum Mechanics', 'Linear Algebra', ...
    return (categories, sub_categories)

if __name__ == '__main__':
    print cats_and_subs(root='/path/to/your/notes')

rawDescription: How to parse a directory tree in python?

description: Parse a directory tree in python


code:
def optfunc():
    e0 = array1[:, 0]
    e1 = array1[:, 1]
    e2 = array1[:, 2]
    e3 = array1[:, 3]

    ar0 = array2[:, 0]
    ar1 = array2[:, 1]

    As = 1./(e0 * e1)
    Bfactors = -0.5 * (1 / e0**2)
    Cfactors = -0.5 * (1 / e1**2)

    lst = []
    for i, elem in enumerate(array1):
        B = ((elem[2] - ar0) ** 2) * Bfactors[i]
        C = ((elem[3] - ar1) ** 2) * Cfactors[i]

        value = As[i]*np.exp(B+C)

        lst.append(value)

    return lst

print np.allclose(optfunc(), func())

# time function.
func_time = timeit.timeit(func, number=10)
opt_func_time = timeit.timeit(optfunc, number=10)
print "%.3fs --> %.3fs" % (func_time, opt_func_time)

rawDescription: Improve performance of function without parallelization


code:
from bs4 import BeautifulSoup
htmlFile = BeautifulSoup('

')
newContent = """This is my content \n with a line break."""
parent = htmlFile.p
lines = newContent.splitlines()
parent.append(htmlFile.new_string(lines[0]))
for line in lines[1:]:
    parent.append(htmlFile.new_tag('br'))
    parent.append(htmlFile.new_string(line))

print htmlFile.prettify()

rawDescription: Modifying a BeautifulSoup .string with line breaks


code:
def get_b(a):
    return (a/500.0) * 360

print get_b(500)
print get_b(250)
print get_b(125)

rawDescription: Calculate value relative to another value


code:
lst = ["a", "b", "c", "d", "e", "f"]
for elem in lst[3:] + lst[:3]:
    print elem,

rawDescription: Full iteration over a list from a specific index in python


code:
import os

def dir_or_file_decorator(func):
    def newFunc(path, *args, **kwargs):
        if os.path.isdir(path):
            filenames = os.listdir(path)
            for filename in filenames:
                filepath = os.path.join(path,filename)
                func(filepath, *args, **kwargs)
        else:
            func(path, *args, **kwargs)
    return newFunc

@dir_or_file_decorator
def print_file_name_and_args(path, extra):
    print extra, path

#We can use the parameter order in the function (our decorator assumes path is the first one)
print_file_name_and_args(r'c:\testdir', 'extra for test 1')
#Or we can just be safe and use named arguments (our decorator assumes the argument is named path)
print_file_name_and_args(extra='extra for test 1', path=r'c:\testdir')
#A combination of both is possible too (but I feel it's more complicated and hence more prone to error)
print_file_name_and_args(r'c:\testdir', extra='extra for test 1')

#Output (in all 3 cases):
# extra for test 1 c:\testdir\testfile1.txt
# extra for test 1 c:\testdir\testfile2.txt

rawDescription: python decorator for directory

description: Python decorator for directory


rawDescription: Get the order of parameters for python function?


code:
import colorama
colorama.init()
start = "\033[1;31m"
end = "\033[0;0m"
print "File is: " + start + "<placeholder>" + end

rawDescription: Python: How can I make the ANSI escape codes to work also in Windows?


code:
from Tkinter import *

features = ['a', 'b', 'c', 'd']
master = Tk()
row = 0
mappe = {}
v = {}

def saver(each):
    print(each, v[each].get())
    mappe[each] = v[each].get()
    print mappe

MODES = [ ("String", "str"), ("Number","num")]

for each in features:

    if row >4 :
        continue

    Label(master, text=each).grid(row=row, column=0)
    v[each] = StringVar()
    v[each].set("default")
    col = 1

    for text, mode in MODES:
        Radiobutton(master, text=text, variable=v[each],value = mode, command=lambda feature=each: saver(feature)).grid(row=row,column=col)
        col+=1

    row += 1

master.mainloop()

rawDescription: Store the values of Radiobuttons and Checkbuttons


code:
import scipy.sparse
import numpy as np

# Make the result reproducible...
np.random.seed(1977)

def generate_random_sparse_array(nrows, ncols, numdense):
    """Generate a random sparse array with -1 or 1 in the non-zero portions"""
    i = np.random.randint(0, nrows-1, numdense)
    j = np.random.randint(0, ncols-1, numdense)
    data = np.random.random(numdense)
    data[data <= 0.5] = -1
    data[data > 0.5] = 1
    ij = np.vstack((i,j))
    return scipy.sparse.coo_matrix((data, ij), shape=(nrows, ncols))

A = generate_random_sparse_array(4, 300000, 1000)
B = generate_random_sparse_array(300000, 5, 1000)

C = A * B

print C.todense()

rawDescription: Matrix multiplication for sparse matrices in Python


code:
import select
import sys
import paramiko
import Xlib.support.connect as xlib_connect
import os
import socket
import subprocess

# for connecting to netcat running remotely
from multiprocessing import Process
import time

# data
import getpass
SSHServerPort=22
SSHServerIP = "localhost"
# get username/password interactively, or use some other method..
user = getpass.getuser()
pwd = getpass.getpass("enter pw for '" + user + "': ")
NETCAT_PORT = 3333
FIREFOX_CMD="/path/to/firefox &"
#FIREFOX_CMD="xclock&"#or this :)

def run_stuff_in_xterm():
    time.sleep(5)
    s = socket.socket(socket.AF_INET6 if ":" in SSHServerIP else socket.AF_INET, socket.SOCK_STREAM)
    s.connect((SSHServerIP, NETCAT_PORT))
    s.send("echo \"Hello there! Are you watching?\"\n")
    s.send(FIREFOX_CMD + "\n")
    time.sleep(30)
    s.send("echo bye bye\n")
    time.sleep(2)
    s.close()

# run xming
XmingProc = subprocess.Popen("C:/Program Files (x86)/Xming/Xming.exe :0 -clipboard -multiwindow")
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(SSHServerIP, SSHServerPort, username=user, password=pwd)
transport = ssh_client.get_transport()
channelOppositeEdges = {}

local_x11_display = xlib_connect.get_display(os.environ['DISPLAY'])
inputSockets = []

def x11_handler(channel, (src_addr, src_port)):
    local_x11_socket = xlib_connect.get_socket(*local_x11_display[:3])
    inputSockets.append(local_x11_socket)
    inputSockets.append(channel)
    channelOppositeEdges[local_x11_socket.fileno()] = channel
    channelOppositeEdges[channel.fileno()] = local_x11_socket
    transport._queue_incoming_channel(channel)

session = transport.open_session()
inputSockets.append(session)
session.request_x11(handler = x11_handler)
session.exec_command("xterm -e \"nc -l 0.0.0.0 %d | /bin/bash\"" % NETCAT_PORT)
p = Process(target=run_stuff_in_xterm)
transport.accept()
p.start()

while not session.exit_status_ready():
    readable, writable, exceptional = select.select(inputSockets,[],[])
    if len(transport.server_accepts) > 0:
        transport.accept()
    for sock in readable:
        if sock is session:
            while session.recv_ready():
                sys.stdout.write(session.recv(4096))
            while session.recv_stderr_ready():
                sys.stderr.write(session.recv_stderr(4096))
        else:
            try:
                data = sock.recv(4096)
                counterPartSocket  = channelOppositeEdges[sock.fileno()]
                counterPartSocket.sendall(data)
            except socket.error:
                inputSockets.remove(sock)
                inputSockets.remove(counterPartSocket)
                del channelOppositeEdges[sock.fileno()]
                del channelOppositeEdges[counterPartSocket.fileno()]
                sock.close()
                counterPartSocket.close()

p.join()
print 'Exit status:', session.recv_exit_status()
while session.recv_ready():
    sys.stdout.write(session.recv(4096))
while session.recv_stderr_ready():
    sys.stdout.write(session.recv_stderr(4096))
session.close()
XmingProc.terminate()
XmingProc.wait()

rawDescription: Remote executing of program via xterm run using paramiko python ssh library


code:
import re

def odd_even(x):
    global a,b
    if x.group(1) == 'a':
        a += 2
        return str(a)
    else:
        b += 2
        return str(b)

a = 0
b = -1
source = "(a,1),(b,1),(a,1),(b,5),(a,1),(b,2),(a,1),(b,1),(a,2),(b,6),(a,2)"

print re.sub("([ab])", odd_even, source)

rawDescription: Replace multiple characters in a string with one from a generator


code:
import functools

def f_base(name):
    print "I am %s" % name

f_list = []
for name in ['A', 'B']:
    f_list.append(
        functools.partial(f_base, name)
    )

f_list[0]()
f_list[1]()

rawDescription: Function object in Python

description: Function object in Python


code:
from numpy import genfromtxt
from time import time
from datetime import datetime
from sqlalchemy import Column, Integer, Float, Date
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

def Load_Data(file_name):
    data = genfromtxt(file_name, delimiter=',', skiprows=1, converters={0: lambda s: str(s)})
    return data.tolist()

Base = declarative_base()

class Price_History(Base):
    #Tell SQLAlchemy what the table name is and if there's any table-specific arguments it should know about
    __tablename__ = 'Price_History'
    __table_args__ = {'sqlite_autoincrement': True}
    #tell SQLAlchemy the name of column and its attributes:
    id = Column(Integer, primary_key=True, nullable=False)
    date = Column(Date)
    opn = Column(Float)
    hi = Column(Float)
    lo = Column(Float)
    close = Column(Float)
    vol = Column(Float)

if __name__ == "__main__":
    t = time()

    #Create the database
    engine = create_engine('sqlite:///csv_test.db')
    Base.metadata.create_all(engine)

    #Create the session
    session = sessionmaker()
    session.configure(bind=engine)
    s = session()

    try:
        file_name = "t.csv" #sample CSV file used:  http://www.google.com/finance/historical?q=NYSE%3AT&ei=W4ikVam8LYWjmAGjhoHACw&output=csv
        data = Load_Data(file_name)

        for i in data:
            record = Price_History(**{
                'date' : datetime.strptime(i[0], '%d-%b-%y').date(),
                'opn' : i[1],
                'hi' : i[2],
                'lo' : i[3],
                'close' : i[4],
                'vol' : i[5]
            })
            s.add(record) #Add all the records

        s.commit() #Attempt to commit all the records
    except:
        s.rollback() #Rollback the changes on error
    finally:
        s.close() #Close the connection
    print "Time elapsed: " + str(time() - t) + " s." #0.091s

rawDescription: using sqlalchemy to load csv file into a database

description: Using sqlalchemy to load csv file into a database


code:
visited = {}
for line in open('3GFT.pdb'):
    list = line.split()
    id = list[0]
    if id == 'ATOM':
        type = list[2]
        if type == 'CA':
            residue = list[3]
            type_of_chain = list[4]
            atom_count = int(list[5])
            position = list[6:8]
            if atom_count >= 0:
                if type_of_chain not in visited:
                    visited[type_of_chain] = 1
                    print residue,type_of_chain,atom_count,' '.join(position)

rawDescription: Parsing a .pdb file in Python

description: Parsing a .pdb file in Python


code:
import wx

instructions = """
How to use.
1) Hover your mouse over the pink panel.
2) Click down (left click)
3) While holding down drag mouse around
4) Release mouse button to stop.

"""

class AppPanel(wx.Panel):

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.settings_sizer = wx.BoxSizer(wx.HORIZONTAL)
        p = MovablePanel(self, -1)

        self.c = wx.CheckBox(self, -1, label = "Ghosting On?")
        self.p = p
        self.i = 0

        self.settings_sizer.Add(self.c)

        self.sizer.Add(self.settings_sizer)
        self.sizer.Add(self.p)
        self.SetSizer(self.sizer)
        self.Layout()

        self.Bind(wx.EVT_CHECKBOX, self.OnCheck, self.c)
        self.Bind(wx.EVT_PAINT, self.OnPaint, self)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnErase, self)

    def OnCheck(self, event):
        print "CHECK\n\n\n\n\n"
        v = self.c.GetValue()
        self.p.r = v
        print v

    def OnErase(self, event):
        pass

    def OnPaint(self, event):
        print "Drawing"
        dc = wx.PaintDC(self)
        print "Panel Rect, ", self.p.GetPosition(),
        print self.p.GetSize()
        print "Clipping Rect", dc.GetClippingBox()
        print "Client Update Rect", self.GetUpdateClientRect()
        print "----------------------------"
        self.i = self.i+10
        c = self.i % 255
        c = (0, 0, c)
        dc.SetPen(wx.Pen(c))
        dc.SetBrush(wx.Brush(c))

        dc.DrawRectangle(0, 0, 10000,10000)
        self.SetBackgroundColour(c)

        dc.SetPen(wx.Pen("WHITE"))
        dc.SetBrush(wx.Brush("WHITE"))

        dc.DrawRectangle(0, 0, self.GetSize()[0], self.c.GetSize()[1])

class MovablePanel(wx.Panel):

    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)
        self.SetMinSize((300,300))
        self.SetSize((300,300))

        txt = wx.StaticText(self, -1, label = "CLICK AND DRAG ME!")
        inst = wx.StaticText(self, -1, label = instructions)

        font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD)
        txt.SetFont(font)
        inst.SetFont(font)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(txt, flag = wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_CENTRE_HORIZONTAL)
        sizer.Add(inst, flag = wx.ALIGN_CENTRE_VERTICAL | wx.ALIGN_CENTRE_HORIZONTAL)
        self.SetSizer(sizer)

        self.SetBackgroundColour("PINK")
        self.LEFT_DOWN = False
        self.r = False
        self.Bind(wx.EVT_MOTION, self.OnMove, self)

        self.Bind(wx.EVT_LEFT_DOWN,
                         self.OnClickDown,
                         self)

        self.Bind(wx.EVT_LEFT_UP,
                         self.OnClickUp,
                         self)

    def OnClickUp(self, event):
        self.LEFT_DOWN = False
        self.Refresh()

    def OnClickDown(self, event):
        self.LEFT_DOWN = True
        self.Refresh()

    def OnMove(self, event):
        if self.LEFT_DOWN:
            p = self.GetTopLevelParent().ScreenToClient(wx.GetMousePosition())
            self.SetPosition(p)
            if not self.r:
                self.GetParent().Refresh()

if __name__ == "__main__":
    app = wx.App(False)
    f   = wx.Frame(None, -1, size = (700, 700))
    p   = AppPanel(f, -1)
    f.Show()
    app.MainLoop()

rawDescription: wxPython Paint Damaged, Clipped area

description: WxPython Paint Damaged, Clipped area


code: print root

rawDescription: Retrieving All Articles Titles from an XML Wiki Dump - Python


code:
def function(beta):
    print beta + 'aei'

rawDescription: Give a function an operation in python (JES)


code:
mycount = 0
for item in List1:
    mycount = mycount + item.lower().count('b')

if mycount == 0:
    print "It did not work"
else:
    print "The letter b appears:", mycount, "times"

rawDescription: search substring in list in python

description: Search substring in list in python


rawDescription: BeautifulSoup find function unusual behaviour


rawDescription: read two columns in Excel using python

description: Read two columns in Excel using python


code:
import timeit

loc = [('a', 'b'),('c', 'd'), ('e', 'f')]

s1 = lambda: ''.join([''.join(row) for row in loc])
s2 = lambda: ''.join([x for row in loc for x in row])

print 'Time required by s1:', timeit.timeit(s1)
print 'Time required by s2:', timeit.timeit(s2)

rawDescription: Join 2d list of characters into a string


code:
for item in numbers:
    print "In first list: ", item
    for num in item:
        print "    Getting number: ", num

rawDescription: how to loop through my 2d array in python?

description: Loop through my 2d array in python


code:
for k, v in itertools.groupby(a):
  print k, v

for k, v in itertools.groupby(a):
  print k, "".join(v)

rawDescription: Split if two consecutive characters are not same


code:
f = ndarray.min
print f(a), f(b)

rawDescription: How to instantiate a class function for not instantiated objects?

description: Instantiate a class function for not instantiated objects


code: print subprocess.check_output(["java", "-version"], stderr=subprocess.STDOUT)

rawDescription: How to get 'java -version' output through python

description: Get 'java -version' output through python


code:
class Wrapper(object):
    def __init__(self,o):
        self.o = o
    def __getitem__(self,key):
        try:
            return getattr(self.o, key)
        except AttributeError:
            raise KeyError, key

rawDescription: Get query result as a tuple to make substitution


code:
class MyCustomWidget(Widget):
    def on_touch_down(self, touch):
        # is the touch is touching on me ?
        if not self.collide_point(*touch.pos):
            return False
        if touch.is_double_tap:
            print 'its a double tap!'
        else:
            print 'its a tap'
        return super(MyCustomWidget, self).on_touch_down(touch)

rawDescription: Python GUI for accessibility Application


code: print str(count) + '    ' + str(conv)

rawDescription: How do I add space between two variables after a print in Python

description: Add space between two variables after a print in Python


code:
import sys
import pycurl

class ContentCallback:
        def __init__(self):
                self.contents = ''

        def content_callback(self, buf):
                self.contents = self.contents + buf

t = ContentCallback()
curlObj = pycurl.Curl()
curlObj.setopt(curlObj.URL, 'http://www.google.com')
curlObj.setopt(curlObj.WRITEFUNCTION, t.content_callback)
curlObj.perform()
curlObj.close()
print t.contents

rawDescription: Python http download page source

description: Python http download page source


code:
import sys

class A(object):
  def test(self):
    print message

class B(A):
  pass

if __name__ == "__main__":
  import mod2
  message = "hello"
  sys.modules["mod1"].message = message
  A().test()
  B().test()
  mod2.C().test()

rawDescription: How does name resolution work when classes are derived across modules?


code:
import itertools
print itertools.__doc__
print itertools.permutations.__doc__

rawDescription: How to print module documentation in Python

description: Print module documentation in Python


code:
x = 1.0/9
print decimal.Decimal(x) # prints exact value
print x # truncated

rawDescription: Decimal(str(my_float)) seems to be better than Decimal(my_float), what's going on?


code:
import ConfigParser

config = ConfigParser.ConfigParser()
config.read('example.cfg')

print config.get('Section1', 'db_username')

rawDescription: Where to store data I don't want in code?


code:
def test():
    test.show = not getattr(test, "show", True)
    print int(test.show)

test()
test()
test()

rawDescription: How can I set a true/false variable with if?

description: Set a true/false variable with if


code:
import logging
import sys

import nose

from nose.tools import with_setup

import setEnvironment

def custom_setup():
    #in setup() directly
    print "env =", setEnvironment.env

@with_setup(custom_setup)
def test_pg():
    pass

if __name__ == '__main__':
    module_name = sys.modules[__name__].__file__

    logging.debug("running nose for package: %s", module_name)
    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            '-s',
                            '--nologcapture',
                            '--set-env=prod'
                            ],
                      addplugins=[setEnvironment.EnvironmentSelector()],)
    logging.info("all tests ok: %s", result)

rawDescription: Using nose plugin to pass a boolean to my package


rawDescription: Extracting number from string in Python with regex


code:
import numpy as np
from scipy.optimize import curve_fit
from pylab import *

def myFunc(t, V, W, k):
    y = V * t - ((V - W) * (1 - np.exp(-k * t)) / k)
    return y

# this generates some fake data to fit. For youm just read in the
# data in CSV or whatever you've
x = np.linspace(0,4,50)
y = myFunc(x, 2.5, 1.3, 0.5)
# add some noise to the fake data to make it more realistic. . .
yn = y + 0.2*np.random.normal(size=len(x))

#fit the data, return the best fit parameters and the covariance matrix
popt, pcov = curve_fit(myFunc, x, yn)
print popt
print pcov

#plot the data
clf()
plot(x, yn, "rs")
#overplot the best fit curve
plot(x, myFunc(x, popt[0], popt[1], popt[2]))
grid(True)
show()

rawDescription: Solve equation with a set of points

description: Solve equation with a set of points


code:
class A(object):
    name = 7
    description = 8
    color = 9

class Hider(object):
    def __get__(self,instance,owner):
        raise AttributeError, "Hidden attribute"

    def __set__(self, obj, val):
        raise AttributeError, "Hidden attribute"

class B(A):
    color = Hider()

rawDescription: Remove class attribute in inherited class Python


code:
import numpy as np
with open('matrix.txt', 'r') as f:
    x = []
    for line in f:
        x.append(map(int, line.split()))
print x
print np.array(x)

rawDescription: How do I read a text file containing an array and place it in an array? (Python)

description: Read a text file containing an array and place it in an array? (Python)


code:
for i in a:
     print i[0]

rawDescription: Accessing grouped items in arrays

description: Accessing grouped items in arrays


code:
print "Wins: " + str(wins)
print "Losses: " + str(losses)

rawDescription: How do I make a score counter for my game in python?

description: Make a score counter for my game in python


code:
from lxml import html
from lxml import etree

text = """<TextFormat>06</TextFormat>
<Text><![CDATA[
Ducdame was John Cowper Powys
other text

  ]]></Text>"""

tree = html.fromstring(text)
tags = tree.xpath('.//text')

text_tag = tags[-1]
print etree.tostring(text_tag)

rawDescription: Getting the whole content within the tag including html tags


code:
strings = raw_input("type something")
while(True):
    MyString = ""
    if strings == "stop": break
    for string in strings.split():
        for char in string:
            if(char.isalnum()): MyString += char
        MyString += " "
    print MyString
    strings = raw_input("continue : ")

rawDescription: Converting a list into a string

description: Converting a list into a string


code:
for element in myarray:
    elementstring=''.join(element)
    with open('path/to/myfile') as myfile:
        for line in myfile:
            if elementstring in line:
                print line

rawDescription: Python nested for loop to match strings in array elements to a file


code:
if job in ("mechanic", "tech"):
    print "awesome"
elif job in ("tool", "rock"):
    print "dolt"

rawDescription: Using OR comparisons with IF statements

description: Using OR comparisons with IF statements


code:
dim=5000
a=np.random.rand(dim,dim)

k=50
t=time.time()
rows,cols=np.triu_indices(dim,k=k)
print time.time()-t
0.913508892059

t=time.time()
rows2,cols2,=fast_triu_indices(dim,k=k)
print time.time()-t
0.16515994072

print np.allclose(rows,rows2)
True

print np.allclose(cols,cols2)
True

rawDescription: Efficient way to find the index of the max upper triangular entry in a numpy array?


code:
if not set(): print "I am empty"

if set([1]): print "I am not empty"

rawDescription: How can I test to see if a defaultdict(set) is empty in Python

description: Test to see if a defaultdict(set) is empty in Python


code:
def gen_url(prefix, suffix, id, places=3):
    pattern = "{}{{:0{}d}}{}".format(prefix, places, suffix)
    return pattern.format(id)

url = "www.testpage.com/"
a = [ gen_url(url + l, "&show=more", n) for l in "xyz" for n in range(1,4) ]
print a

rawDescription: How to apply n-times a function with map

description: Apply n-times a function with map


code:
from string import *
from os import getpid
from subprocess import check_output, STDOUT

pid = getpid()

lsof = (check_output(['/usr/sbin/lsof', '-p', str(pid)], stderr=STDOUT)).split("\n")
for line in lsof[1:]:
    print line

rawDescription: How to get the path to the opened file from a script inside a OS X .app bundle?

description: Get the path to the opened file from a script inside a OS X .app bundle


code:
for m in re.finditer("(Dr\.|med\.)\s([A-Z][a-z]+)", sentence):
    print '%02d-%02d: %s' % (m.start(), m.end(), m.group(2))

rawDescription: Some problems finding specific Words in Text


code:
import re

in_dict = {
    'A1': 'active_rate1',
    'A2': 'purchase_per_active1',
    'B1': 'active_rate2',
    'B2': 'purchase_per_active2'
}

formula = {
    'total_purchase': 'B1*B2',
    'total_purchase2': 'A1*A2'
}

def lookup(cell):
    return in_dict[cell.group(1)]

for key, value in formula.items():
    formula[key] = re.sub("([a-zA-Z]\d+)", lookup, value)

print formula

rawDescription: Search and replace characters in string from the list


code:
a = [(k, str(v)) for k, v in occurrences.items() if k != "consonants" and v > 0]
print ", ".join(x[0] for x in a), "were inserted",
print ", ".join(x[1] for x in a), "times respectively"

rawDescription: Join two for loops, separated by a print statement into one


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--version', '-V', action='version', version="%(prog)s " + __version__)
version_action = next((action for action in parser._get_optional_actions() if isinstance(action, argparse._VersionAction)), None)
print version_action.version if version_action else 'unknown'

rawDescription: Get version string from argparse

description: Get version string from argparse


code:
import numpy as np
print np.std((a,b,c), axis=0, ddof=1)

rawDescription: Numpy Element-wise Standard Deviation

description: Numpy Element-wise Standard Deviation


code:
try:
  x
except NameError:
  print False
else:
  print True

x = None
try:
  x
except NameError:
  print False
else:
  print True

del x
try:
  x
except NameError:
  print False
else:
  print True

rawDescription: pythonic way to check if something exists?

description: Pythonic way to check if something exists


code:
import timeit

reps = 100

pythonTest = timeit.Timer('a = [0.] * 1000000')
numpyTest = timeit.Timer('a = numpy.zeros(1000000)', setup='import numpy')
uninitialised = timeit.Timer('a = numpy.empty(1000000)', setup='import numpy')
# empty simply allocates the memory. Thus the initial contents of the array
# is random noise

print 'python list:', pythonTest.timeit(reps), 'seconds'
print 'numpy array:', numpyTest.timeit(reps), 'seconds'
print 'uninitialised array:', uninitialised.timeit(reps), 'seconds'

rawDescription: Why is numpy.array so slow?

description: Why is numpy.array so slow


code:
import csv

with open(yourfile, 'rb') as infile:
    reader = csv.DictReader(infile)
    for row in reader:
        print row

rawDescription: reading of csv into dictionary, first line becomes the name

description: Reading of csv into dictionary, first line becomes the name


code:
result=[]
tmp=[]
last_direction=0;
last_element=0;
for x in testcase:
    direction= (x-last_element)/abs(x-last_element) #math trick - divide by the magnitude to get the direction (-1/1)
    last_element= x
    if (direction!=last_direction) and tmp:
        result.append(tmp)
        tmp=[]
    last_direction= direction
    tmp.append(x)
if tmp:
    result.append(tmp)

print result

rawDescription: Finding all maximal monotone subsequences in python


code:
from itertools import islice
lis = range(20)
for x in islice(lis, 10, None, 1):
    print x

rawDescription: Efficient iteration over slice in Python


code:
# you can assign a name to a setter to use multiple times
foosetter = LocationProxy()['foo']

# set normal dictionary items
set_my_dict_to_x(foosetter)

# And also set subdictionary items
set_my_dict_to_x(LocationProxy()['subdict']['sub1'])

# Set items that don't exist
set_my_dict_to_x(LocationProxy()['new_item'])

print 'my_dict', my_dict

# my_dict is a class, use the same function
set_my_dict_to_x(LocationProxy().myproperty)

print 'myproperty', my_dict.myproperty

# it works with lists
set_to_x(LocationProxy()[1], my_list)

print 'my_list', my_list

rawDescription: Descend through an object and sub-objects to set an arbitrary item or attribute in the hierarchy


code:
my_data = [{"Name":"John","Age":15}, {"Name":"Bob","Age":16}]
print my_data[0]['Name']
print my_data[0]['Age']
print my_data[1]['Name']
print my_data[1]['Age']

rawDescription: Python data structure for specific storage


rawDescription: Effective way to get part of string until token


code:
load_profile = open('users/file.txt', "r")
read_it = load_profile.read().splitlines()[0]
print read_it

rawDescription: How do you read a specific line of a text file in Python?

description: Read a specific line of a text file in Python


code:
import logging
print 'inside main.py',
print '-'*50
def logger():

      print 'initializing logger....'
      logPath = '.'
      fileName = 'temp'

      # configure log formatter
      logFormatter = logging.Formatter("%(asctime)s [%(filename)s] [%(funcName)s] [%(levelname)s] [%(lineno)d] %(message)s")

      # configure file handler
      fileHandler = logging.FileHandler("{0}/{1}.log".format(logPath, fileName))
      fileHandler.setFormatter(logFormatter)

      # configure stream handler
      consoleHandler = logging.StreamHandler()
      consoleHandler.setFormatter(logFormatter)

      # get the logger instance
      logger = logging.getLogger(__name__)

      # set the logging level
      logger.setLevel(logging.DEBUG)

      print 'adding handlers- '

      #if not len(logger.handlers):
      logger.addHandler(fileHandler)
      logger.addHandler(consoleHandler)

      print 'logger initialized....\n'
      print 'associated handlers - ', len(logger.handlers)
      for handler in logger.handlers:
            print handler
      print
      return logger

main_logger = logger()
main_logger.info('utilizing main.py logger.')
print 'exiting main.py',
print '-'*50

rawDescription: python logging ensure a handler is added only once

description: Python logging ensure a handler is added only once


code:
from itertools import combinations

data = {'A': [('AD', 4.0), None, None], 'C': [('ADBFGC', 14.5), None, None], 'B': [('BF', 0.5), None, None], 'E': [('ADBFGCE', 17.0), None, None], 'D': [('AD', 4.0), None, None], 'G': [('BFG', 6.25), None, None], 'F': [('BF', 0.5), None, None], 'ADBFG': [('ADBFGC', 6.25), ('AD', 4.25), ('BFG', 2.0)], 'BF': [('BFG', 5.75), ('B', 0.5), ('F', 0.5)], 'ADBFGC': [('ADBFGCE', 2.5), ('ADBFG', 6.25), ('C', 14.5)], 'ADBFGCE': [None, ('ADBFGC', 2.5), ('E', 17.0)], 'BFG': [('ADBFG', 2.0), ('BF', 5.75), ('G', 6.25)], 'AD': [('ADBFG', 4.25), ('A', 4.0), ('D', 4.0)]}

def get_path(tree,leaf):
  path = []
  location = leaf
  while True:
    path.append(location)
    parent = tree.get(location)[0]
    if parent:
      location = parent[0]
    else:
      break
  return path

def get_leaves(tree):
  return [ x for (x,y) in tree.items() if y[1] is None and y[2] is None ]

def leafDistances(tree):
  paths = {}
  leaves = get_leaves(tree)
  for leaf in leaves:
    paths[leaf] = get_path(tree,leaf)
  results = {}
  for l1,l2 in combinations(leaves,2):
    commonAncestor = [ x for (x,y) in zip(paths[l1][::-1],paths[l2][::-1]) if x == y ][-1]
    distance = paths[l1].index(commonAncestor) + paths[l2].index(commonAncestor)
    results[(l1,l2)] = distance
    print "%s <-> %s Ancestor == %s, distance == %s\nPath of %s == %s\nPath of %s == %s" % (l1,l2,commonAncestor,distance,l1,paths[l1],l2,paths[l2])
  return results

leafDistances(data)

rawDescription: How to use a dictionary of dictionaries as output from Python?

description: Use a dictionary of dictionaries as output from Python


code:
class MyCustomAuthentication(Authentication):
    pass

class SpecializedResource(ModelResource):
    class Meta:
        authentication = MyCustomAuthentication()

class TestResource(SpecializedResource):
    class Meta(SpecializedResource.Meta):
        # just inheriting from parent meta
        pass
    print Meta.authentication

rawDescription: How to access the meta attributes of a superclass in Python?

description: Access the meta attributes of a superclass in Python


code:
from evdev import InputDevice, categorize  # import * is evil :)
dev = InputDevice('/dev/input/event1')

# Provided as an example taken from my own keyboard attached to a Centos 6 box:
scancodes = {
    # Scancode: ASCIICode
    0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8',
    10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'Q', 17: u'W', 18: u'E', 19: u'R',
    20: u'T', 21: u'Y', 22: u'U', 23: u'I', 24: u'O', 25: u'P', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL',
    30: u'A', 31: u'S', 32: u'D', 33: u'F', 34: u'G', 35: u'H', 36: u'J', 37: u'K', 38: u'L', 39: u';',
    40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'Z', 45: u'X', 46: u'C', 47: u'V', 48: u'B', 49: u'N',
    50: u'M', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 100: u'RALT'
}
for event in dev.read_loop():
    if event.type == evdev.ecodes.EV_KEY:
        data = evdev.categorize(event)  # Save the event temporarily to introspect it
        if data.keystate == 1:  # Down events only
            key_lookup = scancodes.get(data.scancode) or u'UNKNOWN:{}'.format(data.scancode)  # Lookup or return UNKNOWN:XX
            print u'You Pressed the {} key!'.format(key_lookup)  # Print it all out!

rawDescription: How can I get a String from HID device in Python with evdev?

description: Get a String from HID device in Python with evdev


code:
def get_qty(self, item):
    log.msg("Start adding %s to a cart." % item['internal_id'], log.DEBUG)
    logging.basicConfig(level=logging.DEBUG)
    sess = requests.Session()
    sess.keep_alive = False
    adapter = HTTPAdapter(max_retries=50)
    sess.mount('http://', adapter)
    r = sess.get(item['url'])
    cookie_cart_user = dict(r.cookies)
    sel = Selector(text=r.text, type="html")
    session = sel.xpath('//input[@name="_dynSessConf"]/@value').extract()[0]
    add_to_cart_url = 'http://www.sds.com.au/common/ajaxResponse.jsp;jsessionid=%s?_DARGS=/browse/product.jsp.addToCartForm' % cookie_cart_user['JSESSIONID']
    headers = {
        'User-Agent': USER_AGENT,
        'Accept': 'application/json, text/javascript, */*; q=0.01',
    }
    r = sess.post(add_to_cart_url,
                      data=self.generate_form_data(item, 10000, session),
                      cookies=cookie_cart_user,
                      headers=headers,
                      )
    log.msg("End adding %s to a cart." % item['internal_id'], log.DEBUG)
    try:
        response = r.json()
        r.close()
        quantity = int(re.findall(u'\d+', response['formErrors'][0]['errorMessage'])[0])
        log.msg("Success. quantity of %s is %s." % (item['internal_id'], quantity), log.DEBUG)
        return quantity
    except Exception, e:
        log.msg('Error getting data-cart-item on product %s. Error: %s' % (item['internal_id'], str(e)), log.ERROR)
        with open("log/%s.html" % item['internal_id'], "w") as myfile:
            myfile.write('%s' % r.text.encode('utf-8'))

rawDescription: Requests stops when multiple requests sent in a row


code:
with open('p7.erl', 'r') as f:
    lines = f.readlines()
    print type(lines)
    print len(lines)

rawDescription: How to see how many members are there in a list?

description: See how many members are there in a list


code:
#!/usr/bin/env python2.3

from distutils.core       import setup, Extension
from distutils.sysconfig  import get_config_vars

includes = []
includes.extend(get_config_vars('INCLUDEDIR'))
includes.extend(get_config_vars('INCLUDEPY'))
includes.append('/usr/include/SDL')

print 'DBG> include =', includes

setup(name='surfutils',
      version='1.0',
      ext_modules=[Extension(
                    'surfutils',
                    ['src/surfutils.c'],
                    include_dirs=includes,
                  )],
     )

rawDescription: Pygame cannot find include file "sdl.h"

description: Pygame cannot find include file "sdl.h"


code:
#!/usr/bin/env python
from mpi4py import MPI
import time

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

if rank == 0:
   data = {'a': 7, 'b': 3.14}
   time.sleep(3)
   comm.send(data, dest=1, tag=11)
elif rank == 1:
   while not comm.Iprobe(source=0, tag=11):
        print 'rank 1 Doing some work...'
        time.sleep(1)
   rdata = comm.recv(source=0, tag=11)
   print 'rank 1: got ', rdata

rawDescription: non blocking way of receiving a pickled item in mpi4py

description: Non blocking way of receiving a pickled item in mpi4py


code:
DataCaptured = csv.reader(DataFile, delimiter=',', skipinitialspace=True)

Category, Year = [], []
for row in DataCaptured:
    if row[0] not in Year:
        Year.append(row[0])
    if row[1] not in Category:
        Category.append(row[1])

print Category, Year
# ['Category1', 'Category2', 'Category3'] ['1994', '1995', '1996', '1998']

rawDescription: How to create a list in Python with the unique values of a CSV file?

description: Create a list in Python with the unique values of a CSV file


code:
from nltk import bigrams
from nltk import collocations
from nltk import FreqDist
from nltk.collocations import *
from nltk import WordPunctTokenizer

content_part = """test. foo 0 test. foo 1 test.
               foo 2 test. foo 3 test. foo 4 test, foo 4 test."""

tokens = WordPunctTokenizer().tokenize(content_part)

bigram_measures = collocations.BigramAssocMeasures()
word_fd = FreqDist(tokens)
bigram_fd = FreqDist(bigrams(tokens))
finder = BigramCollocationFinder(word_fd, bigram_fd)

finder.apply_word_filter(lambda w: w in ('.', ','))

scored = finder.score_ngrams(bigram_measures.raw_freq)

print tokens
print sorted(finder.nbest(bigram_measures.raw_freq,2),reverse=True)

rawDescription: Python nltk: Find collocations without dot-separated words


code:
import inspect
class Reaction(object):
    Var1 = "lorem"
    Var2 = "impsum"
    Var3 = "dolor"
    def __iter__(self):
        return (v for k, v in inspect.getmembers(self) if not k.startswith('__'))

for value in Reaction():
    print value

rawDescription: Iterate of a Class´ attributes

description: Iterate of a Class´ attributes


code:
import re

s = "goooooooooooooood"
print re.sub(r'(.)\1{2,}', r'\1', s)

rawDescription: Replacing multiple occurences of a character in a string with python


code:
class MyRubylikeThing(object):
    #...

    def __getattr__(self, name):
        def _missing(*args, **kwargs):
            print "A missing method was called."
            print "The object was %r, the method was %r. " % (self, name)
            print "It was called with %r and %r as arguments" % (args, kwargs)
        return _missing

r = MyRubylikeThing()
r.hello("there", "world", also="bye")

rawDescription: Ruby’s “method_missing” in Python

description: Ruby’s “method_missing” in Python


code:
from apt.debfile import DebPackage
ssh = DebPackage('openssh-server_5.5p1-6+squeeze1_amd64.deb')
print ssh.control_content('conffiles').split('\n')

[u'/etc/init.d/ssh', u'/etc/default/ssh', u'/etc/network/if-up.d/openssh-server', u'/etc/ufw/applications.d/openssh-server', u'/etc/pam.d/sshd', u'']

rawDescription: Get a List of conffiles in a deb package in python


code:
def convertToDict(my_list):
    result_dict = {}
    for item in my_list:
        result_dict.setdefault(item[0], []).append(item[1])
    return result_dict

print convertToDict(my_list)
# {'id2': ['name3', 'name4'], 'id1': ['name1', 'name2']}

rawDescription: How to extract from this list of tuples and convert into this dictionary?

description: Extract from this list of tuples and convert into this dictionary


code:
import collections
c = collections.Counter()
with open('planet-names.txt') as file_object: # this will automatically close
    for planet_name in file_object:
        c.update(collections.Counter(planet_name))

print c

rawDescription: first python program doesn't look right

description: First python program doesn't look right