code:
import numpy as np
import time

rows = 181
cols = 217
deps = 181

jac_k = np.random.rand(rows, cols, deps)*5

prev = time.time()
fft1 = np.fft.fftn(jac_k)
print time.time() - prev

prev = time.time()
fft2 = np.fft.rfftn(jac_k)

if deps%2 == 0:
    fft2Star = np.conj(fft2[:, :, -2:0:-1])
else:
    fft2Star = np.conj(fft2[:, :, -1:0:-1])

fft2Star[1::, :, :] = fft2Star[:0:-1, :, :]
fft2Star[:, 1::, :] = fft2Star[:, :0:-1, :]

fft2 = np.concatenate( (fft2, fft2Star), axis=2)
print time.time() - prev

print np.linalg.norm(fft1 - fft2)

rawDescription: Full Frequency Array Reconstruction after numpy.fft.rfftn


code:
import csv
import os
import re

# First
data = csv.DictReader(open('csv.csv', 'r'), delimiter = "\t")
id_map = {}
for row in data:
    for name in row.values():
        # find all sample IDs as a list in the cell, should only get 1 ID
        ids = re.findall(r'[A-H][0-9]{2}', name)
        if len(ids) != 1:
            print "Confused at " + name
        id_map[ids[0]] = name

# Second
folder = 'files/'
for root, dirs, files in os.walk(folder):
    for filename in files:
        fullname = os.path.join(root, filename)
        basename, extension = os.path.splitext(filename)
        # find all sample IDs in the basename, should only get 1 ID
        ids = re.findall(r'[A-H][0-9]{2}', basename)
        if len(ids) != 1:
            print "Confused at " + os.path.join(root, filename)
        if ids[0] in id_map:
            new_name = id_map[ids[0]] + extension
            os.rename(fullname, os.path.join(root, new_name))
        else:
            print "New name for " + fullname + " not found"

rawDescription: Iterating over the values from a dictionary to replace file names in a directory


code:
s = raw_input('Enter a word: ')
print isPalindrome(s)

rawDescription: implement raw_input in my algorithm

description: Implement raw_input in my algorithm


code: print [k for k in d if string in k]

rawDescription: Finding string in dictionary key where the string is not exactly the key in the dict


code:
import subprocess
import time

bin_path = 'sleep'
invocation_args = [[str(x*0.1)] for x in range(0,100)]

subprocs = []
for args in invocation_args:
    subprocs.append(subprocess.Popen([bin_path] + args))

while len(subprocs) > 0:
    subprocs = [p for p in subprocs if p.poll() is None]
    time.sleep(0.05)

print 'Finished running all subprocs'

rawDescription: How can I run a program hundreds of times using Python?

description: Run a program hundreds of times using Python


code:
print "CHK4"
for i in xrange(self.num_states):
    for j in xrange(self.num_symbols):
        for k in xrange(self.num_obSeq):
            self.setObSeq(self.obSeq[k])
            # print self.obSeq[k]
            sumP += self.computeAlpha()
            self.computeBeta()
            alpha_times_beta = self.alpha[:,i] * self.beta[:,i]
            numerator[k] = numpy.sum(alpha_times_beta[self.currentSeq == j])
            denominator[k] = numpy.sum(alpha_times_beta)
        denom = numpy.sum(denominator)
        newOutput[i,j] = numpy.sum(numerator) / (sumP * denom) + self.MIN_PROBABILITY
self.transition = newTransition
self.output = newOutput

rawDescription: Python numpy performing very slow

description: Python numpy performing very slow


code:
with open ('/etc/hosts', 'r') as hosts:
   for line in hosts:
       l = line.split()
       if len(l) > 1:
          IP = l[0].rstrip()
          print IP
          Hostname = l[1].rstrip()
          print Hostname

rawDescription: Can python read Server names from a file


code:
import networkx as nx

G1 = nx.Graph()
G1.add_edge(1, 2)
G2 = G1.copy()   # makes a deep copy of G1 into G2
G2.add_edge(1, 3)

print G1.nodes()    # prints [1, 2]
print G2.nodes()    # prints  [1, 2, 3]

rawDescription: Deep copy of a graph in python

description: Deep copy of a graph in python


code:
from threading import Timer

def hello():
    print "hello, world"

t = Timer(30.0, hello)
t.start() # after 30 seconds, "hello, world" will be printed

rawDescription: Python Time Delays

description: Python Time Delays


code: print 'Some Text',

rawDescription: Extra line breaks being displayed after SQL storage


code:
def foo(a=4, b=3):
    return a+b
def bar(func, *args, **kwargs):
    return func(*args, **kwargs)

print bar(foo, b=12) # prints 16

rawDescription: Python pass in variable assignments through a function wrapper


code:
import itertools as it
for i, j in it.permutations(range(5), 2):
    print i, j

rawDescription: python list iteration

description: Python list iteration


code:
link_1_parents = list(link1.parents)[::-1]
link_2_parents = list(link2.parents)[::-1]

common_parent = [x for x,y in zip(link_1_parents, link_2_parents) if x is y][-1]

print common_parent
print common_parent.name

rawDescription: BeautifulSoup lowest common ancestor

description: BeautifulSoup lowest common ancestor


code:
import shelve

from collections import defaultdict

db = shelve.open('/tmp/db')

grams = [
    ['I','go','to','work'],
    ['I','go','there','often'],
    ['it','is','nice','being'],
    ['I','live','in','NY'],
    ['I','go','to','work'],
]

for gram in grams:
    path = db.get(gram[0], defaultdict(int))

    def f(path, word):
        if not word in path:
            path[word] = defaultdict(int)
        return path[word]
    reduce(f, gram[1:-1], path)[gram[-1]] += 1

    db[gram[0]] = path

print db

db.close()

rawDescription: python populate a shelve object/dictionary with multiple keys

description: Python populate a shelve object/dictionary with multiple keys


code:
def main():
    def sub():
        a=5
        print a

main.__code__.co_consts
exec main.__code__.co_consts[1]

rawDescription: Can we access inner function outside its scope of outer function in python using outer function?


code:
import subprocess
import win32gui
import time

proc = subprocess.Popen(["notepad.exe"])
# lets wait a bit to app to start
time.sleep(3)

def enumWindowFunc(hwnd, windowList):
    """ win32gui.EnumWindows() callback """
    text = win32gui.GetWindowText(hwnd)
    className = win32gui.GetClassName(hwnd)
    #print hwnd, text, className
    if text.find("Notepad") >= 0:
        windowList.append((hwnd, text, className))

myWindows = []
# enumerate thru all top windows and get windows which are ours
win32gui.EnumWindows(enumWindowFunc, myWindows)

# now hide my windows, we can actually check process info from GetWindowThreadProcessId
# http://msdn.microsoft.com/en-us/library/ms633522(VS.85).aspx
for hwnd, text, className in myWindows:
    win32gui.ShowWindow(hwnd, False)

# as our notepad is now hidden
# you will have to kill notepad in taskmanager to get past next line
proc.wait()
print "finished."

rawDescription: Open a program with python minimized or hidden


code:
import os
import argparse

parse=argparse.ArgumentParser(description="Find some files in some dirs")
parse.add_argument('FILE_OR_DIR', nargs='+')
args = parse.parse_args()

target_dirs = [d for d in args.FILE_OR_DIR if os.path.isdir(d)] or ['.']
target_files = [f for f in args.FILE_OR_DIR if not os.path.isdir(f)]

for d in target_dirs:
    for root, _, files in os.walk(d):
        for file in files:
            if file in target_files:
                print os.path.join(root, file)

rawDescription: searching for a text file in the folders and return the path

description: Searching for a text file in the folders and return the path


code:
import re
nw = "01textttt2345"

xx = re.sub(r'\d+(textttt)\d+', r'\1 ', nw)
print '[%s]' % xx  # [textttt ]

xx = re.sub(r'\d(textttt)\d', r'\1 ', nw)
print '[%s]' % xx  # [0textttt 345]

rawDescription: clarification of Python regular expression

description: Clarification of Python regular expression


code:
def cc_quiz(file):
    su = 0
    for line in open(file):
        student_list = line.split()
        count = len(student_list)
        a = str(student_list)
        start_position = a.find('Quizzes')
        end_position = a.find('Individual')
        class_quiz = a[start_position + 8: end_position - 3]
        cq = class_quiz.replace('%', '')
        cq1 = cq.replace(',', '')
        cq2 = cq1.replace(';', '')
        cq3 = cq2.replace("'", '')
        c = cq3.split()
        d = sum(float(x) for x in c)
        su += d
    print su

rawDescription: Sum values in a text file from multiple lines (python)


code:
import requests
import sys
import time

def downloadFile(url, directory) :
  localFilename = url.split('/')[-1]
  with open(directory + '/' + localFilename, 'wb') as f:
    start = time.clock()
    r = requests.get(url, stream=True)
    total_length = r.headers.get('content-length')
    dl = 0
    if total_length is None: # no content length header
      f.write(r.content)
    else:
      for chunk in r.iter_content(1024):
        dl += len(chunk)
        f.write(chunk)
        done = int(50 * dl / total_length)
        sys.stdout.write("\r[%s%s] %s bps" % ('=' * done, ' ' * (50-done), dl//(time.clock() - start)))
        print ''
  return (time.clock() - start)

def main() :
  if len(sys.argv) > 1 :
        url = sys.argv[1]
  else :
        url = raw_input("Enter the URL : ")
  directory = raw_input("Where would you want to save the file ?")

  time_elapsed = downloadFile(url, directory)
  print "Download complete..."
  print "Time Elapsed: " + time_elapsed

if __name__ == "__main__" :
  main()

rawDescription: How to measure download speed and progress using requests?

description: Measure download speed and progress using requests


code:
numGraphs = input()

for x in xrange(0, numGraphs):
    numVerts = input()
    numEdges = input()
    adjMat = [[0 for x in xrange(numVerts)] for x in xrange(numVerts)]
    for x in xrange(0, numEdges):
        edges = raw_input()
        i, padding, j = edges.rpartition(" ")

        i = int(i)
        j = int(j)

        i -= 1
        j -= 1

        adjMat[i][j] = 1

    numPaths = [0 for x in xrange(numVerts)]
    numPaths[0] = 1

    currentPath = [0 for x in xrange(numVerts)]
    maxPath = 1
    minPath = numVerts -1

    for i in xrange(0, numVerts):
        for j in xrange(1, numVerts):
            if adjMat[i][j] == 1:
                numPaths[j] += numPaths[i]
                currentPath[j-i] += 1
            if (currentPath[j-i] is not 0):
                minPath = currentPath[j-i]
            maxPath = max(currentPath)

    print "shortest: %d, longest: %d, total %d" % (minPath, maxPath, numPaths[numVerts-1])

rawDescription: Shortest and longest path in a topologically sorted unweighted directed acyclic graph using an adjacency matrix (Python, or pseudo-code)


code:
def LeastSquaresValueAtX(points, knownX, xBar, x):
    '''
        Gets the value at a given x using the line of best fit
        (least square method) to determine the equation
    '''
    slope = SlopeOfPoints(points, knownX, xBar)
    yIntercept = YInterceptOfPoints(points, xBar, slope)
    return (slope * x) + yIntercept

def SlopeOfPoints(points, knownX, xBar):
    '''
        Gets the slope for a set of points using the formula:
        m=sum(x-avg(x)(y-avg(y))/sum(x-avg(x))**2
    '''
    yBar=dividend=divisor=0.0
    for i in points:
        yBar=yBar+i
    yBar=yBar/5
    for j in points:
        kx = knownX.pop()
        dividend+=((kx-xBar)*(j-yBar))
        divisor+=((kx-xBar)**2)
    return dividend / divisor

def YInterceptOfPoints(points, xBar, slope):
    '''
        Gets the y-intercept for a set of points using the formula:
        b-avg(y)-m(avg(x))
    '''
    yBar =0.0
    for i in points:
        yBar=yBar+i
    yBar=yBar/5
    return yBar - (slope * xBar)

def test(knownX, t):
    if t==1: return LeastSquaresValueAtX([4,13,10,22,20],[2011,2010,2009,2008,2007],2009,2012)
    if t==2: return LeastSquaresValueAtX([7,20,26,29,23],knownX, 2009,2012)
    if t==3: return LeastSquaresValueAtX([6,5,4,3,7], knownX, 2009,2012)

print test([2011,2010,2009,2008,2007], 1)##26.1
print test([2011,2010,2009,2008,2007], 2)##33.3
print test([2011,2010,2009,2008,2007], 3)##5.0

rawDescription: How do I recreate an Excel formula which calls TREND() in Python?

description: Recreate an Excel formula which calls TREND() in Python


code:
import threading

def ontimer():
    print threading.current_thread()

def main():
    threading.Timer(2, ontimer).start()
    print threading.current_thread()

if __name__=="__main__":
    main()

rawDescription: python threading.Timer: do i need to protect the variable accessed inside the callback?

description: Python threading.Timer: do i need to protect the variable accessed inside the callback


code:
import plistlib

result = plistlib.readPlist(filename)
print result['Entry Text']

rawDescription: Using ElementTree to find <key><string> information


code:
import Tkinter as tk

class App:
    def __init__(self):
        root = tk.Tk()
        root.bind_all("<1>", self.woot)
        label1 = tk.Label(text="Label 1", name="label1")
        label2 = tk.Label(text="Label 2", name="label2")
        entry1 = tk.Entry(name="entry1")
        entry2 = tk.Entry(name="entry2")
        label1.pack()
        label2.pack()
        entry1.pack()
        entry2.pack()
        root.mainloop()

    def woot(self, event):
        print "woot!", event.widget

app=App()

rawDescription: Tkinter Global Binding

description: Tkinter Global Binding


code:
file_list = ['bar.py',  'foo.py', 'foo.pyc', 'fooBar', 'fooBar.py', 'tar.py', 'tar.pyc']
print l[5].split('.')[0] == 'tar'

rawDescription: getting a filename in python

description: Getting a filename in python


code:
from itertools import combinations

numbers = [1,2,3,4]
for item in combinations(numbers, 3):
    print sorted(item)

rawDescription: Get all ordered combinations of list python


code:
import HTMLParser
h = HTMLParser.HTMLParser()
print h.unescape('Johnny's sports')

rawDescription: Decoding querystring parameter in Django view


code:
while 1:
    rlist,_,_=select([sys.stdin],[],[],0)
    content=""
    while rlist:
        content+=raw_input()
        rlist,_,_=select([sys.stdin],[],[],0)
    print "blocking task - content:"+content
    time.sleep(5)

rawDescription: Python 2.7: read from stdin without prompting


code:
f = open('N:\userFile', 'r')
for line in f:
        # process line
        print line
f.close()

rawDescription: How do I fetch information (an account name) from a save file (N:\userFile) on Python

description: Fetch information (an account name) from a save file (N:\userFile) on Python


code:
from enchant import DictWithPWL
from enchant.checker import SpellChecker

my_dict = DictWithPWL("en_US", "mywords.txt")
my_checker = SpellChecker(my_dict)

my_checker.set_text("This is sme sample txt with erors.")
for error in my_checker:
    print "ERROR:", error.word

rawDescription: PyEnchant: spellchecking block of text with a personal word list


code:
import itertools

groups = [(1,2),(3,4,5),(6,7)]
groupdxs = [i for i, group in enumerate(groups) for j in range(len(group))]
old_combo = []
for dx_combo in itertools.permutations(groupdxs):
    if dx_combo <= old_combo: # as simple filter
        continue
    old_combo = dx_combo
    iters = [iter(group) for group in groups]
    print [next(iters[i]) for i in dx_combo]

rawDescription: Python permutations with constraints

description: Python permutations with constraints


code:
from twisted.conch.ssh import transport
from twisted.internet import defer
from twisted.conch.ssh import keys, userauth
from twisted.conch.ssh import connection
from twisted.conch.ssh import channel, common
from twisted.internet import protocol, reactor

class ClientTransport(transport.SSHClientTransport):
    def verifyHostKey(self, pubKey, fingerprint):
        return defer.succeed(1)
    def connectionSecure(self):
        self.requestService(ClientUserAuth('USERHERE', ClientConnection()))

class ClientUserAuth(userauth.SSHUserAuthClient):
    def getPassword(self, prompt=None):
        return
    def getPublicKey(self):
        return keys.Key.fromString(data=publicKey)
    def getPrivateKey(self):
        return defer.succeed(keys.Key.fromString(data=privateKey))

class ClientConnection(connection.SSHConnection):
    def serviceStarted(self):
        self.openChannel(CatChannel(conn=self))

class CatChannel(channel.SSHChannel):
    name = 'session'
    def channelOpen(self, data):
        data = 'abcdefghijklmnopqrstuvwxyz' * 300
        self.return_data = ''
        self.conn.sendRequest(self, 'exec', common.NS('C:\helloworld %-10000s' % data), wantReply=True)
    def dataReceived(self, data):
        self.return_data += data
    def closed(self):
        print "got %d bytes of data back from Windows" % len(self.return_data)
        print self.return_data
        self.loseConnection()
        reactor.stop()

if __name__ == "__main__":
    factory = protocol.ClientFactory()
    factory.protocol = ClientTransport
    reactor.connectTCP('123.123.123.123', 22, factory)
    reactor.run()

rawDescription: python execute remote program

description: Python execute remote program


code:
import pygtk
pygtk.require('2.0')
import gobject

class Car(gobject.GObject):
    __gproperties__ = {
        'fuel' : (gobject.TYPE_FLOAT, 'fuel of the car',
                  'amount of fuel that remains in the tank',
                  0, 60, 50, gobject.PARAM_READWRITE)
        }

    __gsignals__ = {
        'engine-started' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
                            (gobject.TYPE_FLOAT,))
        }

    def __init__(self):
        gobject.GObject.__init__(self)
        self.fuel = 50

    def do_get_property(self, property):
        if property.name == 'fuel':
            return self.fuel
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_set_property(self, property, value):
        if property.name == 'fuel':
            self.fuel = value
        else:
            raise AttributeError, 'unknown property %s' % property.name

    def do_engine_started(self, remaining_fuel):
        print '***** Beginning of class closure *****'
        print 'The engine is ready and we still have %f of fuel' % self.fuel
        print '***** End of class closure *****'

    def start(self):
        self.emit('engine-started', self.get_property('fuel'))

gobject.type_register(Car)

rawDescription: Python GTK+: create custom signals?

description: Python GTK+: create custom signals


code:
db_get_data = Model.objects.all()
for cur in db_get_data:
    for field in cur._meta.fields: # field is a django field
        if field.name == 'id':
             print 'found primary key'

rawDescription: Django / Python, getting field name from database get object?


code:
from itertools import chain
from collections import defaultdict

romeo = {'alas':2, 'juliet':35, 'hello':1}
caesar = {'et':1, 'tu':3, 'cassius':12, 'hello':1}

dicts = defaultdict(dict)
dicts['romeo'] = romeo
dicts['caesar'] = caesar

columns = list(set(list(chain(romeo.keys(), caesar.keys()))))

matrix = defaultdict(dict)

for coll in ('romeo', 'caesar'):
    matrix[coll] = {}
    for key in columns:
        if dicts[coll].has_key(key):
            matrix[coll][key] = dicts[coll][key]
        else:
            matrix[coll][key] = 0

print columns

for coll in matrix.keys():
    for key in columns:
        print matrix[coll][key],
    print '\n'

rawDescription: Conceptual - How to build a 2D word-frequency matrix in Python?


code:
#! /bin/env python
import sys
import pymedia.video.muxer as muxer
import pymedia.video.vcodec as vcodec

def recodeVideo( inFile, outFile, outCodec ):
    dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] )
    f= open( inFile, 'rb' )
    fw= open( outFile, 'wb' )
    s= f.read( 400000 )
    r= dm.parse( s )
    v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )
    if len( v )== 0:
        raise 'There is no video stream in a file %s' % inFile

    v_id= v[ 0 ][ 'index' ]
    print 'Assume video stream at %d index: ' % v_id
    c= vcodec.Decoder( dm.streams[ v_id ] )
    e= None
    while len( s )> 0:
        for fr in r:
            if fr[ 0 ]== v_id:
                d= c.decode( fr[ 1 ] )
                if e== None and d:
                    params= c.getParams()
                    params[ 'id' ]= vcodec.getCodecID( outCodec )
                    # Just try to achive max quality( 2.7 MB/sec mpeg1 and 9.8 for mpeg2 )
                    if outCodec== 'mpeg1video':
                        params[ 'bitrate' ]= 2700000
                    else:
                        params[ 'bitrate' ]= 9800000
                    # It should be some logic to work with frame rates and such.
                    # I'm not aware of what it would be...
                    print 'Setting codec to ', params
                    e= vcodec.Encoder( params )
                if e and d:
                    dw= e.encode( d )
                    #print 'Frame size ', len( dw )
                    fw.write( dw )

        s= f.read( 400000 )
        r= dm.parse( s )

if __name__== '__main__':
  if len( sys.argv )!= 4:
    print "Usage: recode_video <in_file> <out_file> <format>\n\tformat= { mpeg1video | mpeg2video }"
  else:
    recodeVideo( sys.argv[ 1 ], sys.argv[ 2 ], sys.argv[ 3 ] )

rawDescription: Replace frame range with image in a video with python


code:
import urllib2
import re

URL = 'http://amazon.com'
page = urllib2.urlopen(URL)
stream = page.readlines()
flag = False
for line in stream:
    if re.search("<title>", line):
        print line
        if not re.search("</title>", line):
            flag = True
    elif re.search("</title>", line):
        print line
        flag = False
    elif flag == True:
        print line

rawDescription: Regex Selection of Strings

description: Regex Selection of Strings


code:
def wrapper(str):
    def decorator_factory(fn):
        def inner_function(*args):
            print 'Do something before'
            return fn(*args)
        return inner_function
    return decorator_factory

@my_decorator('test')
def foo(a, b):
    return a + b

# with decorator
print foo(1, 1)

# without decorator
print wrapper('str')(foo)(1, 1)

rawDescription: Python decorators

description: Python decorators


code:
df = {'dname':["123ask", "example92" , "what3ver"]}
df['numeric'] = [sum (x.isdigit()  for x in b) for b in df['dname']]
print df['numeric']
#>>> [3, 2, 1]

rawDescription: Python- Compute the sum of numerical characters of every string in a dataframe


code:
class A(int):
    def __str__(self):
        print "A.str"
        return int.__str__(self)

    def __unicode__(self):
        print "A.unicode"
        return int.__str__(self)

class B(int):
    def __str__(self):
        print "B.str"
        return int.__str__(self)

unicode(A(1)) # prints "A.unicode"
unicode(B(1)) # prints "B.str"

rawDescription: unicode class in Python

description: Unicode class in Python


code:
from collections import defaultdict
from itertools import chain

a = ['deal', 5, 'price', 4, 'deal', 6, 'deal', '4', 'huge', '2', '$', '2', 'won', '3']

d = defaultdict(int)
for x,y in zip(a[::2],a[1::2]):
    d[x] += int(y)

print list(chain.from_iterable((i,j) for i,j in d.items()))

[OUTPUT]
['huge', 2, 'price', 4, 'won', 3, '$', 2, 'deal', 15]

rawDescription: Python - Regarding lists,dictionarys and strings


code:
def t():
    while True:
        try:
            print q.get(timeout=1)
        except:
            break

rawDescription: How to communicate between process in real time?

description: Communicate between process in real time


code:
def main():
    loop=1
    choice=0  # @UnusedVariable
    while loop == 1:
        print("Welcome to Calculator Function!")
        print("Your options are:")
        print(" ")
        print("1/) Addition")
        print("2/) Subtraction")
        print("3/) Multiplication")
        print("4/) Division")
        print("5/) Quit CalculatorFunction.py")
        print(" ")

        choice = int(raw_input("Choose your option: ").strip())
        if choice == 1:
            add1 = input("Add what: ")
            add2 = input("To what: ")
            print add1, "+", add2, "=", add1+add2
        elif choice == 2:
            sub2 = input("Subtract what: ")
            sub1 = input("From what: ")
            print sub1, "-", sub2, "=", sub1-sub2
        elif choice == 3:
            mult1 = input("Multiply what: ")
            mult2 = input("To what: ")
            print mult1, "*", mult2, "=", mult1*mult2
        elif choice == 4:
            div2= input("Divide what: ")
            div1= input("From what: ")
            print div1, "/", div2, "=", div1/div2
        elif choice == 5:
            loop = 0
            print"Thank you for using CalculatorFunction.py have a good day!"
        else:
            print"No selection made, please try again."

if __name__ == '__main__':
    main()

rawDescription: Pydev Program Continually Terminating

description: Pydev Program Continually Terminating


code:
example = [(1,2), (1,1,1), (1,1), (1,1), (2,1), (3,4), (2,3,1), (1,2,3)]
RDD = sc.parallelize(example)
result = RDD.map(lambda x: list(set(x)))\
            .filter(lambda x: len(x)>1)\
            .map(lambda(x):(tuple(x), 1))\
            .reduceByKey(add)\
            .collect()
print result

rawDescription: Unique Combinations in a list of k,v tuples in Python


code:
from pandas import *

d = {'one' : Series([1., 2., 3.], index=['a', 'b', 'c']),
    'two' : Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}

df = DataFrame(d)

#print df

print "DF", type(df['one']), "\n", df['one']

dfList = df['one'].tolist()

print "DF list", dfList, type(dfList)

rawDescription: get list from pandas dataframe column

description: Get list from pandas dataframe column


code:
from bs4 import BeautifulSoup
import requests

response = requests.get('http://www.amazon.in/Life-What-Make-Preeti-Shenoy/dp/9380349300/ref=sr_1_6?s=books&ie=UTF8&qid=1424652069&sr=1-6',
                        headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'})

soup = BeautifulSoup(response.content)
print soup.select('div#bookDescription_feature_div noscript')[0].get_text(strip=True)

rawDescription: urllib2 / requests does not display iframe of the webpage

description: Urllib2 / requests does not display iframe of the webpage


code:
for el in tree.getiterator(tag='page'):
    page_id = el.get('id', None) # returns second arg if id not exists
    if page_id:
        print page_id, el.find('title').text
    else:
        pprint(el.attrib)

rawDescription: Parsing XML file etree module

description: Parsing XML file etree module


code:
import numpy as np

# A values
A = np.arange(0, 8)
# B values
B = np.arange(16, 24)
# The corresponding C Values, where C[n, m] corresponds to A_n, B_m
C = np.arange(8**2).reshape(8, 8)

# Creating an 3x8x8 Array
ABC = np.meshgrid(A, B) + [C,]
ABC = np.array(ABC)

print ABC

rawDescription: Python: creating a set of vector coordinates or a matrix from a multidimensional table


code:
import views

def app(environ, start_response):
    urls = url_map.bind_to_environ(environ)
    request = Request(environ)
    endpoint, params = urls.match()
    names = endpoint.split('.')
    view = views
    for name in names:
        if not hasattr(view, name):
            __import__(view.__name__, None, None, [name])
        view = getattr(view, name)
    try:
        response = view(request)
    except werkzeug.exceptions.HTTPException, exc:
        response = exc
    return response(environ, start_response)

rawDescription: werkzeug mapping urls to views (via endpoint)

description: Werkzeug mapping urls to views (via endpoint)


code:
import re

gpx_list = []
gpx = open('G:\\14022705.gpx', 'r')
gpx_list_out = open('G:\\Position_Data2.csv', 'w')

for line in gpx:
    if 'trkpt ' in line:
      print re.findall(r"[-+]?\d*\.\d+|\d+",line)
      numerical_value=re.findall(r"[-+]?\d*\.\d+|\d+",line)
      gpx_list_out.write(",".join(numerical_value))

gpx_list_out.close()

rawDescription: How to clean up .gpx data before writing to a .csv file in python

description: Clean up .gpx data before writing to a .csv file in python


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

class Disk:

    def __init__(self, center, radius, myid = None, figure=None, axes_object=None):
        """
        @ARGS
        CENTER : Tuple of floats
        RADIUS : Float
        """
        self.center = center
        self.radius = radius
        self.fig    = figure
        self.ax     = axes_object
        self.myid   = myid
        self.mypatch = None

    def onpick(self,event):
        if event.artist == self.mypatch:
            print "You picked the disk ", self.myid, "  with Center: ", self.center, " and Radius:", self.radius

    def mpl_patch(self, diskcolor= 'orange' ):
        """ Return a Matplotlib patch of the object
        """
        self.mypatch = mpl.patches.Circle( self.center, self.radius, facecolor = diskcolor, picker=1 )

        #if self.fig != None:
            #self.fig.canvas.mpl_connect('pick_event', self.onpick) # Activate the object's method

        return self.mypatch

def on_pick(disks, patches):
    def pick_event(event):
        for i, artist in enumerate(patches):
            if event.artist == artist:
                disk = disks[i]
                print "You picked the disk ", disk.myid, "  with Center: ", disk.center, " and Radius:", disk.radius
    return pick_event

def main():

    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.set_title('click on disks to print out a message')

    disk_list = []
    patches = []

    disk_list.append( Disk( (0,0), 1.0, 1, fig, ax   )   )
    patches.append(disk_list[-1].mpl_patch())
    ax.add_patch(patches[-1])

    disk_list.append( Disk( (3,3), 0.5, 2, fig, ax   )   )
    patches.append(disk_list[-1].mpl_patch())
    ax.add_patch(patches[-1])

    disk_list.append( Disk( (4,9), 2.5, 3, fig, ax   )   )
    patches.append(disk_list[-1].mpl_patch())
    ax.add_patch(patches[-1])

    pick_handler = on_pick(disk_list, patches)

    fig.canvas.mpl_connect('pick_event', pick_handler) # Activate the object's method

    ax.set_ylim(-2, 10);
    ax.set_xlim(-2, 10);

    plt.show()

if __name__ == "__main__":
    main()

rawDescription: Using Matplotlib's pick event properly

description: Using Matplotlib's pick event properly


code:
from bs4 import BeautifulSoup

html = """

            A
            B
            C
            D

"""
soup = BeautifulSoup(html)

header_row = soup.select('table > thead > tr.myClass')[0]

headers = header_row.find_all('th')
header = header_row.find('th', text='A')
print headers.index(header)  # prints 0

rawDescription: beautifulsoup: how to get indexes of elements in table headers

description: Beautifulsoup: how to get indexes of elements in table headers


code:
import subprocess

import tornado.httpserver
import tornado.ioloop
import tornado.iostream
import tornado.options
import tornado.web

from tornado.options import define, options

define("port", default=8888, help="run on the given port", type=int)
define(
    "inputfile",
    default="test.txt",
    help="the path to the file which we will 'tail'",
    type=str)

class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        print "GOT REQUEST"
        self.p = subprocess.Popen(
            ["tail", "-f", options.inputfile, "-n+1"],
            stdout=subprocess.PIPE)

        self.write("<pre>")
        self.write("Hello, world\n")
        self.flush()

        self.stream = tornado.iostream.PipeIOStream(self.p.stdout.fileno())
        self.stream.read_until("\n", self.line_from_nettail)

    def on_connection_close(self, *args, **kwargs):
        """Clean up the nettail process when the connection is closed.
        """
        print "CONNECTION CLOSED!!!!"
        self.p.terminate()
        tornado.web.RequestHandler.on_connection_close(self, *args, **kwargs)

    def line_from_nettail(self, data):
        self.write(data)
        self.flush()
        self.stream.read_until("\n", self.line_from_nettail)

def main():
    tornado.options.parse_command_line()
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(application)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main()

rawDescription: How would I implement "tail" over HTTP with Python Tornado?


code:
import re
t = 'foobar[[img src="picture.jpg" alt="Description" caption="Caption here..."]]helloworld'
p = r'(\[\[img.*?\"\]\])'
foo = re.findall(p,t)

t = t.replace(foo[0],'bar')
print t

rawDescription: Find text starting with ... endin with (shortcode style)


code:
#!/usr/bin/python

from xml.dom.minidom import parse
import xml.dom.minidom

# Open XML document using minidom parser
DOMTree = xml.dom.minidom.parse("movies.xml")
collection = DOMTree.documentElement
if collection.hasAttribute("shelf"):
    print "Root element : %s" % collection.getAttribute("shelf")

# Get all the movies in the collection
movies = collection.getElementsByTagName("movie")

# Print detail of each movie.
for movie in movies:
    title = movie.getAttribute("title")
    if title == "Enemy Behind":
         print "*****Movie*****"
         print "Title: %s" % title

         type = movie.getElementsByTagName('type')[0]
         print "Type: %s" % type.childNodes[0].data
         format = movie.getElementsByTagName('format')[0]
         print "Format: %s" % format.childNodes[0].data
         rating = movie.getElementsByTagName('rating')[0]
         print "Rating: %s" % rating.childNodes[0].data
         description = movie.getElementsByTagName('description')[0]
         print "Description: %s" % description.childNodes[0].data

rawDescription: How can I read a single XML item

description: Read a single XML item


code:
def i(I):
 try:
      return i(I[0])
 except:
      return I

Input = (((False, 2), 2), 2)
print i(Input)

rawDescription: Read innermost value in multiple tuple in python


code:
class FlatIzip(object):
    def __init__(self, *args):
        self.generators = args
    def __iter__(self):
        iters = [iter(x) for x in self.generators]
        while True:
            result = []
            for iterator in iters:
                element = iterator.next()
                if isinstance(element, tuple):
                    result += list(element)
                else:
                    result.append(element)
            yield tuple(result)

#Example:

from itertools import izip

g1 = xrange(10)
g2 = xrange(10,20)
g3 = xrange(20, 30)
g4 = izip(xrange(40,50), xrange(50,60))

lg = [g1, g2, g3, g4]

for x in FlatIzip(*lg):
    print x

rawDescription: izip within izip: unpacking generators?

description: Izip within izip: unpacking generators


code:
def func2 (list1,list2):
     value=10
     list1.append(value)
     list2.append(value)

listA = [1,2,3]
listB = [7,8,9]

func2(listA,listB)

print listA
print listB

rawDescription: Scope of list changes done in a function to called function


code:
X,Y = 10,1000
operations = ["+1","*2"]
def XtoY(operations,X,Y):
    steps_num = []
    steps = []
    steps_num.append(0)
    steps.append([])
    for i in range(X+1, Y+1):
        if i / 2 < X:
            steps_num.append(steps_num[-1] + 1)
            steps.append(list(steps[-1]) + ["+1"])
        elif i % 2 == 0 and steps_num[i / 2 - X] < steps_num[i - 1 - X]:
            steps_num.append(steps_num[i / 2 - X] + 1)
            steps.append(list(steps[i / 2 - X]) + ["*2"])
        else:
            steps_num.append(steps_num[-1] + 1)
            steps.append(steps[-1] + ["+1"])
    return steps[-1]

print XtoY(operations, X, Y)

rawDescription: forward dynamic programming for shortest path

description: Forward dynamic programming for shortest path


code:
import requests

url = "http://www.forbes.com/sites/samsungbusiness/2015/09/23/how-your-car-is-becoming-the-next-hot-tech-gadget/print/"
print requests.get(url, headers={"referer": url.replace("print/", "")}).content

rawDescription: Simulate clicking a link when scraping with Python and BeautifulSoup


code:
import json
import os
import urllib2
import IPython
from IPython.lib import kernel
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]

# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.x
if IPython.version_info[0] < 2:
    ## Not sure if it's even possible to get the port for the
    ## notebook app; so just using the default...
    notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))
    for nb in notebooks:
        if nb['kernel_id'] == kernel_id:
            print nb['name']
            break
else:
    sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
    for sess in sessions:
        if sess['kernel']['id'] == kernel_id:
            print sess['notebook']['name']
            break

rawDescription: How to I get the current IPython Notebook name

description: I get the current IPython Notebook name


code:
import random

def display_board(board):
    col = "   "
    cols = len(board[0])
    for index1 in range(cols):
        col += "%i   " % index1
    print col
    for index2 in range(len(board)):
        print str(index2) + ": " + " | ".join(board[index2]) + " |"
        print "  " + "---+" * cols

def make_computer_move(board, avail_cols):
    col = random.choice(avail_cols)
    for row in range(len(board)-1, -1, -1):            # counts from bottom of board and up
        if board[row][col] == " ":                      # if there is a blank space, put a "O" and break
            print "The pairing is (%i,%i)" % (row,col)  #Print the coordinates
            board[row][col] = 'O'
            break

    if row == 0:      #arrives to the last row
        return col

def main():
    board = [[" ", " ", " ", " ", " "] for i in range(5)]
    avail_cols = range(len(board[0]))
    display_board(board)
    for counter in range(25):
        filled = make_computer_move(board, avail_cols)
        display_board(board)
        if filled is not None: avail_cols.remove(filled)

main()

rawDescription: Python Connect Four. Making the computer move


code:
def savePractices(practices, MODELNAME):

    from ml1 import models

    dynamic_model = getattr(models, MODELNAME)
    for practice in practices:
        p = dynamic_model(**practice)
        try:
            p.save()
        except IntegrityError:
            print "error"

    return

rawDescription: Can you pass a model as a parameter for a view function in django?

description: Pass a model as a parameter for a view function in django


code:
import xml.etree.ElementTree as ET

def openEmptyXML():
    doc = ET.parse("Automatimport.xml")
    for atc in doc.findall(".//ATC"):
        versionname = atc.find("OriginalTestVersionName")
        tcName = versionname.text
        if tcName == 'REQPROD 232 Read IO':
            verdict1 = atc.find("Verdict")
            verdict2 = atc.find("Procedure/Verdict")
            verdict3 = atc.find("Procedure/Result/Verdict")
            print tcName
            print verdict1.text
            print verdict2.text
            print verdict3.text

openEmptyXML()

rawDescription: How to read out tc result verdict in an xml file with Python and ElementTree

description: Read out tc result verdict in an xml file with Python and ElementTree


code:
from math import pi, sqrt
def equation(variable1,variable2):
    calculation = ((sqrt(2)*variable1**(2*variable2+1))/(3*pi))*q(k=variable2)
    while variable2 >0:
        variable2 -= 1
        calculation += ((sqrt(2)*variable1**(2*variable2+1))/(3*pi))*q(k=variable2)
    return calculation

def main():
    variable2 = (input("Enter a Number for variable2: "))
    variable1 = (input("Enter a Number for variable1: "))
    return equation(variable1,variable2)

print main()

rawDescription: python loop dencrementing a variable for every iteration

description: Python loop dencrementing a variable for every iteration


code:
import numpy as np
from scipy import linalg
from numpy import dot

def nmf(X, latent_features, max_iter=100, error_limit=1e-6, fit_error_limit=1e-6):
    """
    Decompose X to A*Y
    """
    eps = 1e-5
    print 'Starting NMF decomposition with {} latent features and {} iterations.'.format(latent_features, max_iter)
    X = X.toarray()  # I am passing in a scipy sparse matrix

    # mask
    mask = np.sign(X)

    # initial matrices. A is random [0,1] and Y is A\X.
    rows, columns = X.shape
    A = np.random.rand(rows, latent_features)
    A = np.maximum(A, eps)

    Y = linalg.lstsq(A, X)[0]
    Y = np.maximum(Y, eps)

    masked_X = mask * X
    X_est_prev = dot(A, Y)
    for i in range(1, max_iter + 1):
        # ===== updates =====
        # Matlab: A=A.*(((W.*X)*Y')./((W.*(A*Y))*Y'));
        top = dot(masked_X, Y.T)
        bottom = (dot((mask * dot(A, Y)), Y.T)) + eps
        A *= top / bottom

        A = np.maximum(A, eps)
        # print 'A',  np.round(A, 2)

        # Matlab: Y=Y.*((A'*(W.*X))./(A'*(W.*(A*Y))));
        top = dot(A.T, masked_X)
        bottom = dot(A.T, mask * dot(A, Y)) + eps
        Y *= top / bottom
        Y = np.maximum(Y, eps)
        # print 'Y', np.round(Y, 2)

        # ==== evaluation ====
        if i % 5 == 0 or i == 1 or i == max_iter:
            print 'Iteration {}:'.format(i),
            X_est = dot(A, Y)
            err = mask * (X_est_prev - X_est)
            fit_residual = np.sqrt(np.sum(err ** 2))
            X_est_prev = X_est

            curRes = linalg.norm(mask * (X - X_est), ord='fro')
            print 'fit residual', np.round(fit_residual, 4),
            print 'total residual', np.round(curRes, 4)
            if curRes < error_limit or fit_residual < fit_error_limit:
                break

return A, Y

rawDescription: Python Non negative Matrix Factorization that handles both zeros and missing data?


code:
from suds.client import Client
url = 'https://109.231.73.12:8090/API?wsdl'
client = Client(url, username='qwerty123', password='qwerty123')
client.service.addition(1, 2)
client.service.hello('John')
client.service.xToThePowerOfy(2, 16)
print client # automagic documentation

rawDescription: Python webservice client

description: Python webservice client


code:
import threading
import Queue
import random
import math
import time
import Tkinter

random.seed(0)

class App:
    def __init__(self, queue, width=400, height=300):
        self.width, self.height = width, height
        self.canvas = Tkinter.Canvas(width=width, height=height, bg='black')
        self.canvas.pack(fill='none', expand=False)
        self._oid = []
        self.canvas.after(10, self.move)

        self.queue = queue
        self.canvas.after(50, self.check_queue)

    def check_queue(self):
        try:
            x, y, rad, outline = self.queue.get(block=False)
        except Queue.Empty:
            pass
        else:
            self.create_moving_ball(x, y, rad, outline)
        self.canvas.after(50, self.check_queue)

    def move(self):
        width, height = self.width, self.height
        for i, (oid, r, angle, speed, (x, y)) in enumerate(self._oid):
            sx, sy = speed
            dx = sx * math.cos(angle)
            dy = sy * math.sin(angle)
            if y + dy + r> height or y + dy - r < 0:
                sy = -sy
                self._oid[i][3] = (sx, sy)
            if x + dx + r > width or x + dx - r < 0:
                sx = -sx
                self._oid[i][3] = (sx, sy)
            nx, ny = x + dx, y + dy
            self._oid[i][-1] = (nx, ny)
            self.canvas.move(oid, dx, dy)
        self.canvas.update_idletasks()
        self.canvas.after(10, self.move)

    def create_moving_ball(self, x=100, y=100, rad=20, outline='white'):
        oid = self.canvas.create_oval(x - rad, y - rad, x + rad, y + rad,
                outline=outline)
        oid_angle = math.radians(random.randint(1, 360))
        oid_speed = random.randint(2, 5)
        self._oid.append([oid, rad, oid_angle, (oid_speed, oid_speed), (x, y)])

def queue_create(queue, running):
    while running:
        if random.random() < 1e-6:
            print "Create a new moving ball please"
            x, y = random.randint(100, 150), random.randint(100, 150)
            color = random.choice(['green', 'white', 'yellow', 'blue'])
            queue.put((x, y, random.randint(10, 30), color))
        time.sleep(0) # Effectively yield this thread.

root = Tkinter.Tk()
running = [True]

queue = Queue.Queue()

app = App(queue)
app.create_moving_ball()
app.canvas.bind('<Destroy>', lambda x: (running.pop(), x.widget.destroy()))

thread = threading.Thread(target=queue_create, args=(queue, running))
thread.start()

root.mainloop()

rawDescription: Mutli-threading python with Tkinter

description: Mutli-threading python with Tkinter


code:
stuff = range(5,0,-1)
it = enumerate(stuff)
print dict(it), dict(it) # first consumes all items, so there are none left for the 2nd call

seq = list(enumerate(stuff)) # creates a list of all the items
print dict(seq), dict(seq) # you can use it as often as you want

rawDescription: python dict function on enumerate object

description: Python dict function on enumerate object


code:
for prefix, event, value in parser:
    if event == 'map_key':
        print value

rawDescription: Python JSON keys without parse

description: Python JSON keys without parse


code:
import codecs

words = [word.strip() for word in codecs.open("hypernyms_en2hi.txt", "r", "utf-8")]
hypernyms = codecs.open("hindi_hypernym.txt", "r", "utf-8")
count_arr = []

for line in hypernyms:
    count_arr.append(0)
    for word in words:
        count_arr[-1] += (word in line)

for count in enumerate(count_arr):
    if count:
        print iterator, ' ', count

rawDescription: Python unicode search not giving correct answer


code:
import time
import requests
import threading

# Limit the number of threads.
pool = threading.BoundedSemaphore(2)

def worker(u):
    # Request passed URL.
    r = requests.get(u)
    print r.status_code
    # Release lock for other threads.
    pool.release()
    # Show the number of active threads.
    print threading.active_count()

def req():
    # Get URLs from a text file, remove white space.
    urls = [url.strip() for url in open('urllist.txt')]
    for u in urls:
        # Thread pool.
        # Blocks other threads (more than the set limit).
        pool.acquire(blocking=True)
        # Create a new thread.
        # Pass each URL (i.e. u parameter) to the worker function.
        t = threading.Thread(target=worker, args=(u, ))
        # Start the newly create thread.
        t.start()

req()

rawDescription: Limiting number of HTTP requests per second on Python


code:
from plone.app.testing import TEST_USER_NAME
from plone.app.testing import TEST_USER_PASSWORD
from plone.testing.z2 import Browser
from zope.component import getGlobalSiteManager
from zope.publisher.interfaces.browser import IBrowserView
from Products.Five.browser import BrowserView
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from zope.pagetemplate.pagetemplate import PageTemplate
from zope.interface import Interface
from zope.publisher.browser import IDefaultBrowserLayer

class MyPageTemplate(PageTemplate):
    instance = None
    def __init__(self, instance):
        self.instance = instance
    def pt_getContext(self, instance, request, **kw):
        namespace = super(MyPageTemplate, self).pt_getContext(**kw)
        namespace['view'] = self.instance
        namespace['context'] = self.instance.context
        namespace['request'] = self.instance.request
        return namespace

class MyView(BrowserView):
    def __init__(self, context, request):
        self.context = context
        self.request = request
    def render(self):
        template = MyPageTemplate(self)
        text = '''\

request works

context/id= works

view/my_var= works

            '''
        template.write(text)
        return template()
    def __call__(self):
        return self.render()
    def my_var(self):
        return 'my_value'

global_site_manager = getGlobalSiteManager()
global_site_manager.registerAdapter(MyView, (None, None), IBrowserView, 'my_view')

browser=Browser(app.Plone)
browser.addHeader('Authorization', 'Basic %s:%s' % (TEST_USER_NAME, TEST_USER_PASSWORD))
browser.open(app.Plone.absolute_url() + '/@@my_view')
print browser.contents

rawDescription: How to pass template text to a view programmatically without using a file?

description: Pass template text to a view programmatically without using a file


code:
def levenshtein_distance(first, second):
    """Find the Levenshtein distance between two strings."""
    if len(first) > len(second):
        first, second = second, first
    if len(second) == 0:
        return len(first)
    first_length = len(first) + 1
    second_length = len(second) + 1
    distance_matrix = [[0] * second_length for x in range(first_length)]
    for i in range(first_length):
       distance_matrix[i][0] = i
    for j in range(second_length):
       distance_matrix[0][j]=j
    for i in xrange(1, first_length):
        for j in range(1, second_length):
            deletion = distance_matrix[i-1][j] + 1
            insertion = distance_matrix[i][j-1] + 1
            substitution = distance_matrix[i-1][j-1]
            if first[i-1] != second[j-1]:
                substitution += 1
            distance_matrix[i][j] = min(insertion, deletion, substitution)
    return distance_matrix[first_length-1][second_length-1]

def percent_diff(first, second):
    return 100*levenshtein_distance(a, b) / float(max(len(a), len(b)))

a = "the quick brown fox"
b = "the quick vrown fox"
print '%.2f' % percent_diff(a, b)

rawDescription: Algorithm to calculate percent difference betweem two blobs of text


code:
from random import randint
num = randint(1,15000 / 5) * 5
print num

rawDescription: Generate random number X where X % Y = 0


code:
import numpy as np

a=np.array([1,])
b=np.array([1,])
c=np.array([1,2,3])

def foo(a,b,c):
    args=locals()
    return args.items()[np.array([i[1].size for i in args.items()]).argmax()][1]

biggest = foo(a,b,c)
print biggest
# [1,2,3]

rawDescription: Test which Numpy function argument has more than one element


code:
def display(self,**kwargs):
    head = kwargs.get("head", self.head)

    if not head:
        return

    print head,

    self.display(head=head.link) # you should always name an optional argument,
                                 # and you must name it if **kwargs is used.

rawDescription: Python default values for class member function parameters set to member variables


code:
from PySide.QtCore import *
from PySide.QtGui import *

import sys

class Main(QWidget):

    def __init__(self, parent=None):
        super(Main, self).__init__(parent)

        layout  = QHBoxLayout(self)

        picture = PictureLabel("pic.png", self)
        picture.pictureClicked.connect(self.anotherSlot)

        layout.addWidget(picture)
        layout.addWidget(QLabel("click on the picture"))

    def anotherSlot(self, passed):
        print passed
        print "now I'm in Main.anotherSlot"

class PictureLabel(QLabel):

    pictureClicked = Signal(str) # can be other types (list, dict, object...)

    def __init__(self, image, parent=None):
        super(PictureLabel, self).__init__(parent)
        self.setPixmap(image)

    def mousePressEvent(self, event):
        print "from PictureLabel.mousePressEvent"
        self.pictureClicked.emit("emit the signal")

a = QApplication([])
m = Main()
m.show()
sys.exit(a.exec_())

rawDescription: How to detect mouse click on images displayed in GUI created using PySide

description: Detect mouse click on images displayed in GUI created using PySide


code:
region_list = [
    ('us-west-2', 'vol-XXXXXXXX'),
    ('us-west-2', 'vol-YYYYYYYY'),
    ('us-west-2', 'vol-ZZZZZZZZ')
]

print [{"region": region, "name": name}
       for region, name in region_list]

rawDescription: Python Dict From List With Keys

description: Python Dict From List With Keys


code:
import itertools, operator

l = [{'dept':1, 'age':10, 'name':'Sam'},
        {'dept':1, 'age':12, 'name':'John'},
        {'dept':2,'age':20, 'name':'Mary'},
        {'dept':2,'age':11, 'name':'Mark'},
        {'dept':2,'age':11, 'name':'Tom'}]

groups = ['dept', 'age', 'name']

groups.reverse()
def hierachical_data(data, groups):
    g = groups[-1]
    g_list = []
    for key, items in itertools.groupby(data, operator.itemgetter(g)):
        g_list.append({key:list(items)})
    groups = groups[0:-1]
    if(len(groups) != 0):
        for e in g_list:
            for k,v in e.items():
                e[k] = hierachical_data(v, groups)

    return g_list

print hierachical_data(l, groups)

rawDescription: Grouping Python dictionaries in hierachical form


code:
#fake_thing.py
def compute_something():
    return 'fake_value'

#real_thing.py
def compute_something():
    return 'real_value'

#my_module.py
import real_thing
def my_function():
    return real_thing.compute_something()

#test_my_module.py
import sys

def test_my_function():
    import fake_thing
    sys.modules['real_thing'] = fake_thing
    import my_module
    print my_module.my_function()

test_my_function()

rawDescription: Python: Mocking a module that's being used by a module I'm testing


code:
from sys import exit
import random

print "Guess the number between 1 thru 10"
print "You have 3 tries"
guess = raw_input("Enter a guess-->")

tries = 3
random_number = random.randrange(1,10)
while tries > 1 and int(guess) != random_number:
    print "That's not the number, try again!"
    print "Tries remaining:", tries - 1
    guess = raw_input("Next guess?-->")
    tries = tries - 1

if int(guess) != random_number:
    print "Sorry, you lose!"
    print "The number was", random_number
    exit(0)

else:
    print "You guessed it!"

rawDescription: Python Beginner script not as expected

description: Python Beginner script not as expected


code:
a = np.array([[1,-3,1],[2,5,1],[-1,1,-2]])
b = np.array([10,4,-13])

x = np.linalg.solve(a, b)

print x

print np.allclose(np.dot(a, x), b)      # To check the solution is found

rawDescription: Gauss-Seidel Solver for Python 2.7

description: Gauss-Seidel Solver for Python 2.7


code:
class RecordProcessor(object):
    def __init__(self, recs):
        self.recs = recs
        self.processed_rec_count = 0
    def __call__(self):
        for r in self.recs:
            if r.sender('sender_of_interest'):
               self.processed_rec_count += 1
               # process record r...
               yield r  # processed record

def process_all_records(files):
    for f in files:
        fd = open(f,'r')
        recs_p = RecordProcessor(read_records(fd))
        write_records(recs_p)
        print 'records processed:', recs_p.processed_rec_count

rawDescription: Tracking how many elements we process in generator


code:
import boto

s3 = boto.connect_s3()
bucket = s3.lookup('mybucket')
key = bucket.lookup('mykey')

# Copy the key onto itself, preserving the ACL but changing the content-type
key.copy(key.bucket, key.name, preserve_acl=True, metadata={'Content-Type': 'text/plain'})

key = bucket.lookup('mykey')
print key.content_type

rawDescription: boto.s3: copy() on a key object loses 'Content-Type' metadata

description: Boto.s3: copy() on a key object loses 'Content-Type' metadata


code: print '%(url)s' % {'url': my_url}

rawDescription: String formatting named parameters?

description: String formatting named parameters


code:
entire_file = open('hello.txt', 'rb').read()

print 'number of \\n: %d, number of bytes %d' % (
    entire_file.count('\n'), len(entire_file))

rawDescription: Reading corrupted file in python

description: Reading corrupted file in python


code:
if set(a) <= set(b):
    print "a is in b"

rawDescription: How to check a list contained by another list without a loop?

description: Check a list contained by another list without a loop


code:
import numpy as np
mymatrix = np.matrix([[11,12,13],
                      [21,22,23],
                      [31,32,33]])
def myfunction( x ):
    return sum(x)

print np.apply_along_axis( myfunction, axis=1, arr=mymatrix )
#[36 66 96]

rawDescription: Iterating over Numpy matrix rows to apply a function each?


code:
def east(area):
    if area[-1] in '123':
        return area[:-1] + str(int(area[-1])+1)
    elif "4" == area[-1]:
        print 'no way'
        return area
    else:
        return 'incorrect input'# or throw and exception depends on what you really need

rawDescription: How to get new value from function in Python?

description: Get new value from function in Python


rawDescription: minute average csv files

description: Minute average csv files


code:
class Sample:
    variable=[2]

object1 = Sample()
object2 = Sample()
print object1.variable[0]; print object2.variable[0]
object1.variable[0]=1
print object1.variable[0]; print object2.variable[0]

rawDescription: Class attribute for two instances of the same class


code:
import requests, json

url = 'http://us.soccerway.com/a/block_competition_matches_summary?block_id=page_competition_1_block_competition_matches_summary_6&callback_params=%7B%22page%22%3A0%2C%22bookmaker_urls%22%3A%7B%2213%22%3A%5B%7B%22link%22%3A%22http%3A%2F%2Fwww.bet365.com%2Fhome%2F%3Faffiliate%3D365_308124%22%2C%22name%22%3A%22Bet%20365%22%7D%5D%7D%2C%22block_service_id%22%3A%22competition_summary_block_competitionmatchessummary%22%2C%22round_id%22%3A21322%2C%22outgroup%22%3Afalse%2C%22view%22%3A2%7D&action=changePage&params=%7B%22page%22%3A-1%7D'

r = requests.get(url)

#print r.text

data = json.loads(r.text)

print data

rawDescription: How do I scrape a table in webpage using python?

description: Scrape a table in webpage using python


rawDescription: Pythonic way to match up timestamps in list of objects


code:
class X(object):
  def __str__(self): return 'str'
  def __unicode__(self): return u'unicode'

x = X()
print u'%s' % x

rawDescription: Python string format character for __unicode__?


code:
for row in cursor.execute("select album_id, photo_id from photos where user_id=1"):
    print row.album_id, row.photo_id

row  = cursor.execute("select * from tmp").fetchone()
rows = cursor.execute("select * from tmp").fetchall()

count = cursor.execute("update photos set processed=1 where user_id=1").rowcount
count = cursor.execute("delete from photos where user_id=1").rowcount

rawDescription: Which cursor object is pyodbc's `Cursor.execute` returning?


code:
from matplotlib import pyplot as plt
bars = plt.bar([1,2,3],[3,4,5])
ax = plt.gca()
fig = plt.gcf()
b = bars[0].get_bbox()  # bbox instance

print b
# box in data coords
#Bbox(array([[ 1. ,  0. ],
#       [ 1.8,  3. ]]))

b2 = b.transformed(ax.transData)
print b2
# box in display coords
#Bbox(array([[  80.        ,   48.        ],
#       [ 212.26666667,  278.4       ]]))

print b2.transformed(ax.transData.inverted())
# box back in data coords
#Bbox(array([[ 1. ,  0. ],
#       [ 1.8,  3. ]]))

print b2.transformed(ax.transAxes.inverted())
# box in axes coordinates
#Bbox(array([[ 0.        ,  0.        ],
#       [ 0.26666667,  0.6       ]]))

rawDescription: Get bbox in data coordinates in matplotlib


code:
from collections import defaultdict

records = defaultdict(list)

file = 'test.csv'

data = csv.reader(open(file))
fields = data.next()

for row in data:
    records[row[0]].append(row[1])

#sorting by ids since keys don't maintain order
results = sorted(records.items(), key=lambda x: x[0])
print results

rawDescription: Need more efficient way to parse out csv file in Python


code:
for index, row in enumerate(c):
    if index%2 == 0: # Odd numbered row (since index starts from 0)
        # row contains out_time
        print row
    else:
        # row contains in_time
        print row

rawDescription: python code for callculation of total worked time of an employee from a given database

description: Python code for callculation of total worked time of an employee from a given database


code:
categories=CategoryAnswers.objects.values('category', 'brand').distinct()
for cat in categories:
    print CategoryAnswers.objects.filter(category=cat["category"], brand=cat["brand"]).values('category', 'brand').annotate(total=Sum('answer'))

rawDescription: Merge QuerySetValues object in Django

description: Merge QuerySetValues object in Django


code:
import itertools
from pprint import pprint

W,H,D = 3,4,5
p = [[[k*W*H + j*W+i for i in xrange(3)] for j in xrange(4)] for k in xrange(5)]
assert (W,H,D) == (len(p[0][0]),len(p[0]),len(p))
pprint(p)

print
for i, j, k in itertools.product(xrange(D), xrange(H), xrange(W)):
    print "p[%d][%d][%d] = %d" % (i, j, k, p[i][j][k])
print
for i in xrange(D):
    for j in xrange(H):
        for k in xrange(W):
            print "p[%d][%d][%d] = %d" % (i, j, k, p[i][j][k])
print
for col in p:
    for row in col:
        for elem in row:
            print elem
print
for i, col in enumerate(p):
    for j, row in enumerate(col):
        for k, elem in enumerate(row):
            print "p[%d][%d][%d] = %d" % (i, j, k, elem)

rawDescription: Alternative triple for Python

description: Alternative triple for Python


code:
from PyQt4 import QtCore, QtGui
import sys

class EditButtonsWidget(QtGui.QWidget):
    editCalled = QtCore.pyqtSignal(str)
    def __init__(self, row, col, parent=None,):
        super(EditButtonsWidget,self).__init__(parent)
        self.row = row
        self.col = col
        self.parent = parent
        btnsave = QtGui.QPushButton('Save')
        btnedit = QtGui.QPushButton('edit')
        btndelete = QtGui.QPushButton('delete')
        layout = QtGui.QHBoxLayout()
        layout.setContentsMargins(0,0,0,0)
        layout.setSpacing(0)
        layout.addWidget(btnsave)
        layout.addWidget(btnedit)
        layout.addWidget(btndelete)
        self.setLayout(layout)
        btnedit.clicked.connect(self.getAllCellVal)

    @QtCore.pyqtSlot()
    def getAllCellVal(self):
        itmVal = {}
        for col in range(0, 4):
            itm = self.parent.item(self.row, col).text()
            itmVal[col] = str(itm)
        if itmVal:
            self.editCalled.emit(str(itmVal))

class MainForm(QtGui.QDialog):
    def __init__(self,parent=None):
        super(MainForm,self).__init__(parent)
        self.resize(400,200)
        self.tableWidget = QtGui.QTableWidget()

        layout = QtGui.QVBoxLayout()
        layout.addWidget( self.tableWidget)
        self.setLayout(layout)
        self.init_main_form()

    def bb(self):
        button = QtGui.QApplication.focusWidget()
        index = self.tableWidget.indexAt(button.pos())
        if index.isValid():
            print(index.row(), index.column())

    def printEditVal(self, values):
        print values

    def init_main_form(self):
        data =[['a','b','c','d'],['z','y','x','w']]
        self.tableWidget.setColumnCount(5)
        for row in data:
            inx = data.index(row)
            self.tableWidget.insertRow(inx)
            self.tableWidget.setItem(inx,0,QtGui.QTableWidgetItem(str(row[0])))
            self.tableWidget.setItem(inx,1,QtGui.QTableWidgetItem(str(row[1])))
            self.tableWidget.setItem(inx,2,QtGui.QTableWidgetItem(str(row[2])))
            self.tableWidget.setItem(inx,3,QtGui.QTableWidgetItem(str(row[3])))
            buttonWid = EditButtonsWidget(inx,4, self.tableWidget)
            buttonWid.editCalled.connect(self.printEditVal)
            self.tableWidget.setCellWidget(inx,4,buttonWid)

def main():
    app = QtGui.QApplication(sys.argv)
    main_form = MainForm()
    main_form.show()
    app.exec_()
if __name__ == '__main__':
    main()

rawDescription: How do I get the contents of a row of QTableWidget with click on the button in python and PyQt

description: Get the contents of a row of QTableWidget with click on the button in python and PyQt


code:
for n in range (0,len(cds),3):
        if cds[n:n+3] in codontable:
                proteinsequence += codontable[cds[n:n+3]]
        sequence = ''
print proteinsequence

rawDescription: Incorrect output format when translating DNA to Protein


code:
# beginner

def fac(n):
    f = 1
    i = 1
    while i <= n:
        f *= i
        i += 1
    return f

# advanced beginner

def fac(n):
    return n * fac(n - 1) if n > 1 else 1

# intermediate

def fac(n):
    return reduce(lambda x, y: x * y, range(1, n + 1))

# advanced intermediate

import operator
def fac(n):
    return reduce(operator.mul, xrange(1, n + 1))

# professional

import math
print math.factorial(5)

# guru

import scipy.misc as sc
print sc.factorial(5, exact=True)

rawDescription: Factorial function in Python

description: Factorial function in Python


code:
from decimal import *
getcontext().prec = 160

summan = 0
lista = ['1/4','1/3','1/5','1/6','1/7','1/9','1/10','1/11']

for x in lista:
    den, num = x.split('/')
    print Decimal(den) / Decimal(num)

rawDescription: Many decimal places required in python calculations


code:
for item in mylist:
    if item in checklist:
        pass
    else:
       # do something
       print item

rawDescription: python - if not in list

description: Python - if not in list


code:
# The main function.
def main():
  mysum = 0
  while True:
    positive = int(input('Enter a positive number, or enter a\n\
negative number to end and calculate the sum: '))
    if positive < 0:
      print mysum
      return
    mysum = mysum + positive

# Call the main function.
main()

rawDescription: Sum of Numbers Exercise, Python 3

description: Sum of Numbers Exercise, Python 3


code:
from django.conf import settings
settings.DEBUG = True
from django.db import connection
Model.objects.count()
print len(connection.queries)

rawDescription: Get SQL query count during a Django shell session


code:
import gc

def run():
    return 1

s = run()
print len(gc.get_referrers(s))  # prints a rather big number, 41 in my case

obj = object()
print len(gc.get_referrers(obj))  # prints 1

lst = [obj]
print len(gc.get_referrers(obj))  # prints 2

lst = []
print len(gc.get_referrers(obj))  # prints 1 again

rawDescription: Python C API, High reference count for new Object


code:
import platform
print platform.linux_distribution()

rawDescription: How to know the system is Debian or CentOS in Python?

description: Know the system is Debian or CentOS in Python


code:
parser = OptionParser()
parser.add_option('-n', '--db_name', help='DB Name (comma separated if multiple DBs - no spaces)')
parser.add_option('-H', '--db_host', help='DB host (comma separated if multiple DBs - no spaces)')
parser.add_option('-p', '--db_port', help='DB port (optional)')
parser.add_option('-u', '--db_user', help='DB user')
parser.add_option('-w', '--db_pass', help='DB password')
parser.add_option('-o', '--output-file', help='output file')

options, args = parser.parse_args()

errors = []
error_msg = 'No %s specified. Use option %s'
if not options.db_name:
    errors.append(error_msg % ('database name', '-n'))
if not options.db_host:
    errors.append(error_msg % ('database host', '-H'))
if not options.db_user:
    errors.append(error_msg % ('database user', '-u'))
if not options.db_pass:
    errors.append(error_msg % ('database password', '-w'))
if not options.output_file:
    errors.append(error_msg % ('output file', '-o'))

if errors:
    print '\n'.join(errors)
    sys.exit(1)

rawDescription: Python parse arguments from command line


code:
for index, obj in enumerate(objects):
    print index

rawDescription: Is there any way in python while iterating through a collection to get the current counter numeric value?


code:
#!/usr/bin/ENV python
import numpy
from mpi4py import MPI

NUMARRAYS = 100
ARRAYSIZE = 10000

ASK_FOR_WORK_TAG = 1
WORK_TAG = 2
WORK_DONE_TAG = 3
DIE_TAG = 4

comm = MPI.COMM_WORLD
rank = comm.Get_rank()
size = comm.Get_size()
status = MPI.Status()

# Master
if rank == 0:
    data = numpy.empty(ARRAYSIZE, dtype=numpy.int32)
    sorted_data = numpy.empty([NUMARRAYS, ARRAYSIZE], dtype=numpy.int32)
    sorted_arrays = 0
    dead_workers = 0

    while dead_workers < size - 1:
        print "[Master] Probing"
        comm.Recv([data, ARRAYSIZE, MPI.INT], source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status)
        print "[Master] Probed"

        dest = status.Get_source()
        if status.Get_tag() == ASK_FOR_WORK_TAG:
            if sorted_arrays <= NUMARRAYS - 1:
                print "[Master] got request for work from worker %d" % dest

                data = numpy.random.random_integers(0, ARRAYSIZE, ARRAYSIZE).astype(numpy.int32)
                print "[Master] sending work to Worker %d" % dest
                comm.Send([data, ARRAYSIZE, MPI.INT], dest=dest, tag=WORK_TAG)
                print "[Master] sent work to Worker %d" % dest
            else:
                # Someone did more work than they should have
                print "[Master] Telling worker %d to DIE DIE DIE" % dest
                comm.Send([data, ARRAYSIZE, MPI.INT], dest=dest, tag=DIE_TAG)
                dead_workers += 1
                print "[Master] Already killed %d workers" % dead_workers

        elif status.Get_tag() == WORK_DONE_TAG:
            if sorted_arrays <= NUMARRAYS - 1:
                print "[Master] got results from Worker %d. Storing in line %d" % (status.Get_source(), sorted_arrays)
                sorted_data[sorted_arrays] = numpy.copy(data)
                numpy.savetxt("sample", data, newline=" ", fmt="%d")
                sorted_arrays += 1

# Slave
else:
    # Ask for work
    data = numpy.empty(ARRAYSIZE, dtype=numpy.int32)
    while True:
        print "[Worker %d] asking for work" % rank
        comm.Send([data, ARRAYSIZE, MPI.INT], dest=0, tag=ASK_FOR_WORK_TAG)
        print "[Worker %d] sent request for work" % rank

        comm.Recv([data, ARRAYSIZE, MPI.INT], source=0, tag=MPI.ANY_TAG, status=status)

        if status.Get_tag() == WORK_TAG:
            print "[Worker %d] got work" % rank

            print "[Worker %d] is sorting the array" % rank
            data.sort()
            print "[Worker %d] finished work. Sending it back" % rank
            comm.Send([data, ARRAYSIZE, MPI.INT], dest=0, tag=WORK_DONE_TAG)
        elif status.Get_tag() == DIE_TAG:
            print "[Worker %d] DIE DIE DIE" % rank
            break
        else:
            print "[Worker %d] Doesn't know what to to with tag %d right now" % (rank, status.Get_tag())

rawDescription: MPI apparently deadlocking when more than 5 workers are used


code:
import sys
import time
import threading

class progress_bar_loading(threading.Thread):

    def run(self):
            global stop
            global kill
            print 'Loading....  ',
            sys.stdout.flush()
            i = 0
            while stop != True:
                    if (i%4) == 0:
                        sys.stdout.write('\b/')
                    elif (i%4) == 1:
                        sys.stdout.write('\b-')
                    elif (i%4) == 2:
                        sys.stdout.write('\b\\')
                    elif (i%4) == 3:
                        sys.stdout.write('\b|')

                    sys.stdout.flush()
                    time.sleep(0.2)
                    i+=1

            if kill == True:
                print '\b\b\b\b ABORT!',
            else:
                print '\b\b done!',

kill = False
stop = False
p = progress_bar_loading()
p.start()

try:
    #anything you want to run.
    time.sleep(1)
    stop = True
except KeyboardInterrupt or EOFError:
         kill = True
         stop = True

rawDescription: Fabric get() progressbar

description: Fabric get() progressbar


code:
from lxml import etree

x = lxml.html.fromstring("...html text...").xpath("//div[@class='ArticleDetail']/p")
for elem in x:
    for child in elem.iterdescendants():
        print etree.tostring(child)

rawDescription: Python - Keeping some HTML tags with lxml Xpath feature


code:
class Node(object):
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None
        self.count = 1

    def __str__(self):
        return 'value: {0}, count: {1}'.format(self.value, self.count)

def insert(root, value):
    if not root:
        return Node(value)
    elif root.value == value:
        root.count += 1
    elif value < root.value:
        root.left = insert(root.left, value)
    else:
        root.right = insert(root.right, value)

    return root

def create(seq):
    root = None
    for word in seq:
        root = insert(root, word)

    return root

def search(root, word, depth=1):
    if not root:
        return 0, 0
    elif root.value == word:
        return depth, root.count
    elif word < root.value:
        return search(root.left, word, depth + 1)
    else:
        return search(root.right, word, depth + 1)

def print_tree(root):
    if root:
        print_tree(root.left)
        print root
        print_tree(root.right)

src = ['foo', 'bar', 'foobar', 'bar', 'barfoo']
tree = create(src)
print_tree(tree)

for word in src:
    print 'search {0}, result: {1}'.format(word, search(tree, word))

# Output
# value: bar, count: 2
# value: barfoo, count: 1
# value: foo, count: 1
# value: foobar, count: 1
# search foo, result: (1, 1)
# search bar, result: (2, 2)
# search foobar, result: (2, 1)
# search bar, result: (2, 2)
# search barfoo, result: (3, 1)

rawDescription: Python: Create a Binary search Tree using a list


rawDescription: How to transpose a collection of tuples to a dictionary?

description: Transpose a collection of tuples to a dictionary


code:
import wx

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        btnOne = wx.Button(panel, label="Button One", name="btnOne")
        btnOne.Bind(wx.EVT_ENTER_WINDOW, self.onEnter)
        btnTwo = wx.Button(panel, label="Button Two", name="btnTwo")
        btnTwo.Bind(wx.EVT_ENTER_WINDOW, self.onEnter)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(btnOne, 0, wx.ALL, 5)
        sizer.Add(btnTwo, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onEnter(self, event):
        """"""
        widget = event.GetEventObject()
        print widget.GetName()

# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

rawDescription: Ascertaining which widget triggered an EVT_MOUSE_ENTER in wxpython


code:
with open('test.txt') as f:
    for line in f:
        nextLine = next(f)
        if 'A' == line.strip():
            print nextLine

rawDescription: Read next line in Python

description: Read next line in Python


code:
import contextlib
with contextlib.closing(thing):
    print thing

rawDescription: What's the Pythonic way to write an auto-closing class?


code:
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
import PyQt4.uic as uic
import sys

class Person(object):
    def __init__(self, firstName, lastName):
        self.firstName = firstName
        self.lastName = lastName

class MyWidget(QtGui.QMainWindow):
    def __init__(self, people):
        QtGui.QMainWindow.__init__(self)
        uic.loadUi('MyWidget.ui', self)
        self.people=people
        self.addPersonButton.clicked.connect(self.addPerson)
        self.displayButton.clicked.connect(self.display)

        self.model = simpleModel(self.people)
        self.listView.setModel(self.model)

        self.show()

    def display(self):
        for i,p in enumerate(self.people):
            print "Person %i: %s %s"%(i, p.firstName, p.lastName)

    def addPerson(self):
        firstName = str(self.firstNameLineEdit.text())
        lastName = str(self.lastNameLineEdit.text())
        p = Person(firstName, lastName)
        self.model.insertRow(p)

class simpleModel(QtCore.QAbstractListModel):
    def __init__(self, dataList, parent=None):
        super(simpleModel, self).__init__(parent)
        self.list = dataList

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.list)

    def insertRow(self, data, parent=QtCore.QModelIndex()):
        self.beginInsertRows(parent, self.rowCount(), self.rowCount())
        self.list.append(data)
        self.endInsertRows()

    def data(self, index, role):
        if role == QtCore.Qt.DisplayRole:
            person = self.list[index.row()]
            return QtCore.QVariant("%s %s"%(person.firstName,person.lastName))
        elif role == QtCore.Qt.UserRole:
            person = self.list[index.row()]
            return person
        return QtCore.QVariant()

def main():
    people=[Person('John','Doe')]
    app = QtGui.QApplication(sys.argv)
    ex = MyWidget(people)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: Qt: Lists of user defined objects

description: Qt: Lists of user defined objects


code:
import numpy as np
np.random.seed(1)
lenB = 10
lenA = 20
B_start = np.random.rand(lenB)
B_end   = B_start + np.random.rand(lenB)
B_value = np.random.randint(100, 200, lenB)

A_start = np.random.rand(lenA)
A_end   = A_start + np.random.rand(lenA)

#if you use dataframe
#B_start = B["start"].values
#B_end = ...

mask = (A_start[:, None ] > B_start) & (A_end[:, None] < B_end)
r, c = np.where(mask)
result = pd.Series(B_value[c]).groupby(r).max()
print result

rawDescription: Finding overlapping segments in Pandas

description: Finding overlapping segments in Pandas


code:
import re
import collections
import sys

fname = "todo.txt"
jobs = {}
myset = set()

#Recursive printing. Also orders the dictionary and removes zzz
def rprint(d):
   a = collections.OrderedDict(sorted(d.items()))
   for key, value in a.iteritems():
      if isinstance(value, dict):
         rprint(value)
      else:
         # check to see if the line is a repitition, this can occour
         # when a line has more than one priority/project/context.
         for line in value:
             if line not in myset:
                 print str(line)
                 myset.add(line)

with open(fname) as f:
    for line in f:

      line = line.strip()
      words = line.split(' ')
      priority = [word for word in words if re.match('^\([A-Z]\)',word)]
      context = [word for word in words if word.startswith('@')]
      project = [word for word in words if word.startswith('+')]

      #Need to make sure that there is always a key otherwise it will cause
      #the dict issues, set key to zzz so that it will appear at the bottom when sorted
      if not priority: priority = ["zzz"]
      if not project:  project = ["zzz"]
      if not context:  context = ["zzz"]

      for i in context:
          for j in project:
              for k in priority:

                  if i not in jobs:
                      jobs[i] = {}
                  if j not in jobs[i]:
                      jobs[i][j] = {}
                  if k not in jobs[i][j]:
                      jobs[i][j][k] = []

                  jobs[i][j][k].append(line)

    rprint(jobs)

rawDescription: Sorting todo.txt lines by its properties


code:
from itertools import combinations,chain

lst = [1,2,3,4,5,6]
def group(a,n):
    return zip(*[iter(a)]*n)

for a,b in chain.from_iterable(combinations(grp, 2) for grp in group(lst,3)):
    print "{0} {1}".format(a,b)

rawDescription: Generate 2 item combinations of every 3 items in a list


code:
import requests
import BeautifulSoup

session = requests.session()

req = session.get('http://stackoverflow.com/questions/10807081/script-to-extract-data-from-wbpage')

doc = BeautifulSoup.BeautifulSoup(req.content)

print doc.findAll('a', { "class" : "gp-share" })

rawDescription: Script to Extract data from web page

description: Script to Extract data from web page


code:
resultlist=[]
for i in snipppets1:
 for j in snippets2:
  resultlist.append(i+j)
print resultlist

rawDescription: How to each item in list with all values of another list in Python

description: Each item in list with all values of another list in Python


code:
from datetime import datetime, time

windows = {}

window1_least = time(hour=0, minute=0)
window1_most = time(hour=3, minute=0)
window2_least = time(hour=3, minute=0)
window2_most = time(hour=6, minute=30)
window3_least = time(hour=6, minute=30)
window3_most = time(hour=11, minute=50)
window4_least = time(hour=11, minute=50)
window4_most = time(hour=14, minute=55)
window5_least = time(hour=14, minute=55)
window5_most = time(hour=16, minute=55)
window6_least = time(hour=16, minute=55)
window6_most = time(hour=20, minute=0)
window7_least = time(hour=20, minute=0)
window7_most = time(hour=0, minute=0)

def which_window(minutes):
    hour = minutes // 60
    minute = minutes % 60
    curent_time = time(hour=hour, minute=minute)
    if window1_least <= curent_time < window1_most:
        return 1
    elif window2_least <= curent_time < window2_most:
        return 2
    elif window3_least <= curent_time < window3_most:
        return 3
    elif window4_least <= curent_time < window4_most:
        return 4
    elif window5_least <= curent_time < window5_most:
        return 5
    elif window6_least <= curent_time < window6_most:
        return 6
    elif window7_least <= curent_time < window7_most:
        return 7

windows = {minute: which_window(minute) for minute in range(1440)}

def get_current_window():
    current_minute = datetime.now().hour * 60 + datetime.now().minute
    return windows[current_minute]

# Just for testing
print get_current_window()

rawDescription: Caching a function containing some constants, in Django


code:
import numpy as np
import pandas

d = np.array(np.arange(3*2*6).reshape(3,2,6))

dat = pandas.Panel(d, items=['temp', 'precip', 'humidity'],
                      major_axis=['yr1', 'yr2'],
                      minor_axis=['jan', 'feb', 'mar', 'apr', 'may', 'jun'])

print dat['temp']
print dat.major_xs('yr1')
print dat.minor_xs('may')

rawDescription: 3d numpy record array

description: 3d numpy record array


code:
for item in fle:
    if item.rstrip(',').strip() in sentence.split():
        # Match
        print item

rawDescription: String comparison using a file

description: String comparison using a file


code:
A = [0,0,1,1,0,0]
B = [0,1,0,1,0,1]

def match(A,B,n):
    r = len(A)
    if r != len(B):
        return False
    DP = [ [0]*(r+1) for i in range(r+1) ]
    # DP[a][b] is min insertions to A to turn A[:a] and B[:b] into the same string
    for b in range(r+1):
        for a in range(r+1):
            if a>0 and b>0:
                best = DP[a-1][b-1]
                if A[a-1]!=B[b-1]:
                    best += 2 # inserting into both
            elif a==0 and b==0:
                best = 0
            else:
                best = 2*n+1

            if a>0:
                best = min(best,1+DP[a-1][b]) # inserting into A
            if b>0:
                best = min(best,1+DP[a][b-1]) # inserting into B
            DP[a][b] = best
    x = DP[r][r] # we have needed to make x insertions to get A and B to match
    # A and B are now the same length, so we must have made x/2 insertions to each
    return x<=2*n

print match(A,B,2)

rawDescription: Insert/delete distance between lists

description: Insert/delete distance between lists


code:
import tornado.ioloop
import tornado.web
from tornado import gen

class MainHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.aborted = False

    @gen.coroutine
    def get(self):
        print "hello"

        self.write("A"*10000000)
        yield gen.Task(self.flush)  # This is asynchronous, so on_connection_close can run while this flushes.
        self.finish()
        if not self.aborted:
            print "world"

    def on_connection_close(self):
        self.aborted = True
        print("aborted")

application = tornado.web.Application([
    (r"/test", MainHandler),
])
application.listen(8888)

tornado.ioloop.IOLoop.instance().start()

rawDescription: How can I detect when the client drops while receiving a response

description: Detect when the client drops while receiving a response


code:
import multiprocessing as mp
import os
import random

class QueueWriter(object):
    def __init__(self, **kwargs):
        self.grid = kwargs.get("grid")
        self.path = kwargs.get("path")

    def __call__(self, q):
        print self.path
        log = open(self.path, "a", 1)
        log.write("QueueWriter called.\n")
        while 1:
            res = q.get()
            if res == 'kill':
                log.write("QueueWriter received 'kill' message. Closing Writer.\n")
                break
            else:
                log.write("This is where I'd write: {0} to grid file.\n".format(res))

        log.close()
        log = None

class Worker(object):
    def __init__(self, **kwargs):
        self.queue = kwargs.get("queue")
        self.grid = kwargs.get("grid")

    def __call__(self, idx):
        res = self.workhorse(idx)
        self.queue.put((idx,res))
        return res

    def workhorse(self,idx):
        #in reality a fairly complex operation
        return self.grid[idx] ** self.grid[idx]

if __name__ == '__main__':
#     log = open(os.path.expanduser('~/minimal.log'), 'w',1)
    path = os.path.expanduser('~/minimal.log')

    pool = mp.Pool(mp.cpu_count())
    manager = mp.Manager()
    q = manager.Queue()

    grid = [random.random() for _ in xrange(10000)]
    # in actuality grid is a shared resource, read by Workers and written
    # to by QueueWriter

    qWriter = QueueWriter(grid=grid, path=path)
#     watcher = pool.map(qWriter, (q,),1)
# Start the writer as a single process rather than a pool
    p = mp.Process(target=qWriter, args=(q,))
    p.start()
    wrkr = Worker(queue=q,grid=grid)
    result = pool.map(wrkr, range(10000), 1)
#     result.get()
# not required for pool
    q.put('kill')
    pool.close()
    p.join()
    pool.join()

rawDescription: Multiprocessing: Passing a class instance to pool.map


code:
k = 0
for i in s:
    if k == n-1:
       print i + '\n'
       k = 0
    else:
        print i
        k += 1

rawDescription: How to print strings from a list in sequence in Python?

description: Print strings from a list in sequence in Python


code:
import urllib2
import StringIO
import csv

url = "http://www.andrewpatton.com/countrylist.csv"
response = urllib2.urlopen(url).read()
output   = StringIO.StringIO(response)

cr       = csv.reader((line.replace('NUL','') for line in output), delimiter=",")

for row in cr:
        print row

rawDescription: How to display a data parsed from csv file through a link using Python

description: Display a data parsed from csv file through a link using Python


rawDescription: Iterating over date range between two pandas dataframes for category count


code:
from BeautifulSoup import BeautifulSoup
import urlparse

soup = BeautifulSoup(html)
links = soup.findAll('a', href=True)

for link in links:
  href = link['href']
  url = urlparse.urlparse(href)
  params = urlparse.parse_qs(url.query)
  if 'sessionid' in params:
    print params['sessionid'][0]

rawDescription: How to extract url GET parameter from <a> tag, from the full html text

description: Extract url GET parameter from <a> tag, from the full html text


code:
from pyparsing import *

data = '''
asdkjh                  1      120   203
kajshd                  230   2309  2309
Activated Attempts      230   2309  2309
Activated Attempts      230   2309  2309
aksjdh                               300
'''

eventParser = Group(Word(alphas) + Optional(Word(alphas)))
rowParser = Group(eventParser + delimitedList(Word(nums),White(" ")))
tableParser = ZeroOrMore(rowParser)

def getValue(attemptsList, term):
    value = 0
    for attempt in attemptsList:
        if ' '.join(attempt[0]) == term:
            value += int(attempt[1])
    return value

attempts = getValue(tableParser.parseString(data), "Activated Attempts")
print attempts

rawDescription: pyparsing keyword, how does it work?

description: Pyparsing keyword, how does it work


code:
#!/usr/bin/env python

def repeatOnError(*exceptions):
  def checking(function):
    def checked(*args, **kwargs):
      while True:
        try:
          result = function(*args, **kwargs)
        except exceptions as problem:
          print "There was a problem with the input:"
          print problem.__class__.__name__
          print problem
          print "Please repeat!"
        else:
          return result
    return checked
  return checking

@repeatOnError(ValueError)
def getNumberOfIterations():
  return int(raw_input("Please enter the number of iterations: "))

iterationCounter = getNumberOfIterations()
print "You have chosen", iterationCounter, "iterations."

rawDescription: Most Pythonic way to do input validation


code:
import re

# some tokenizers
def tokzr_WORD(txt): return ('WORD', re.findall(r'(?ms)\W*(\w+)', txt))  # split words
def tokzr_SENT(txt): return ('SENTENCE', re.findall(r'(?ms)\s*(.*?(?:\.|\?|!))', txt))  # split sentences
def tokzr_QA(txt):
    l_qa = []
    for m in re.finditer(r'(?ms)^[\s#\-\*]*(?:Q|Question)\s*:\s*(?P<QUESTION>\S.*?\?)[\s#\-\*]+(?:A|Answer)\s*:\s*(?P<ANSWER>\S.*?)$', txt):  # split (Q, A) sequences
        for k in ['QUESTION', 'ANSWER']:
            l_qa.append(m.groupdict()[k])
    return ('QA', l_qa)
def tokzr_QA_non_canonical(txt):  # Note: not supported by tokenize_recursively() as not canonical.
    l_qa = []
    for m in re.finditer(r'(?ms)^[\s#\-\*]*(?:Q|Question)\s*:\s*(?P<QUESTION>\S.*?\?)[\s#\-\*]+(?:A|Answer)\s*:\s*(?P<ANSWER>\S.*?)$', txt):  # split (Q, A) sequences
        for k in ['QUESTION', 'ANSWER']:
            l_qa.append((k, m.groupdict()[k]))
    return l_qa

dict_tokzr = {  # control string: tokenizer function
    'WORD'    : tokzr_WORD,
    'SENTENCE': tokzr_SENT,
    'QA'      : tokzr_QA,
}

# the core function
def tokenize_recursively(l_tokzr, work_on, lev=0):
    if isinstance(work_on, basestring):
        ctrl, work_on = dict_tokzr[l_tokzr[0]](work_on)  # tokenize
    else:
        ctrl, work_on = work_on[0], work_on[1:]  # get right part
    ret = [ctrl]
    if len(l_tokzr) == 1:
        ret.append(work_on)  # add right part
    else:
        for wo in work_on:  # dive into tree
            t = tokenize_recursively(l_tokzr[1:], wo, lev + 1)
            ret.append(t)
    return ret

# just for printing
def nestedListLines(aList, ind='    ', d=0):
    """ Returns multi-line string representation of \param aList.  Use \param ind to indent per level. """
    sRet = '\n' + d * ind + '['
    nested = 0
    for i, e in enumerate(aList):
        if i:
            sRet += ', '
        if type(e) == type(aList):
            sRet += nestedListLines(e, ind, d + 1)
            nested = 1
        else:
            sRet += '\n' + (d + 1) * ind + repr(e) if nested else repr(e)
    sRet += '\n' + d * ind + ']' if nested else ']'
    return sRet

# main()
inp1 = """
    * Question: I want try something.  Should I?
    * Answer  : I'd assume so.  Give it a try.
"""
inp2 = inp1 + 'Q: What is a good way to achieve this?  A: I am not so sure. I think I will use Python.'
print repr(tokzr_WORD(inp1))
print repr(tokzr_SENT(inp1))
print repr(tokzr_QA(inp1))
print repr(tokzr_QA_non_canonical(inp1))  # Really this way?
print

for ctrl, inp in [  # example control sequences
    ('SENTENCE-WORD', inp1),
    ('QA-SENTENCE', inp2)
]:
    res = tokenize_recursively(ctrl.split('-'), inp)
    print nestedListLines(res)

rawDescription: Writing a tokenizer in Python

description: Writing a tokenizer in Python


code:
resolved_urls = []
pool = multiprocessing.Pool(100)
res = pool.imap(resolve_url, checkin5)

for x in res:
    resolved_urls.append(x)
    print 'finished one'
    # ... whatever counting/tracking code you want here

rawDescription: Python: check count of processed elements when using multiprocessing.map


code:
with open(resultslocation + f2name, 'r') as f2, open(resultslocation + f3name, 'w') as f3:
    hits = f2.read()
    list = re.findall(r'name = (.+).', hits)
    for items in list:
        ip_addresses = socket.gethostbyname(items)
        with open(resultslocation + f4name, 'r') as f4:
            for items in f4:
                ports = int(items)
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                s.settimeout(1.0) # Set a timeout (value in seconds).
                try:
                    s.connect((ip_addresses, ports))
                    s.shutdown(2)
                    print 'port', ports, 'on', ip_addresses, 'is open'
                except:
                    # This will alse catch the timeout exception.
                    print 'port', ports, 'on', ip_addresses, 'is closed'

rawDescription: Python: Have to press ctrl+c to get an result


code:
with file('a.py','rU') as f:
  co=compile(f.read(),'foobar','exec')
  exec co in {'__name__':'__main__'}

rawDescription: Python - run external script

description: Python - run external script


rawDescription: How to approximate timestamp

description: Approximate timestamp


code:
def initUI(self):
    self.popup = popup = Toplevel(self)
    Label(popup, text="Please wait until the file is created").grid(
        row=0, column=0)
    self.progressbar = progressbar = ttk.Progressbar(popup,
        orient=HORIZONTAL, length=200, mode='indeterminate')
    progressbar.grid(row=1, column=0)
    progressbar.start()
    self.checkfile()

def checkfile(self):
    if os.path.exists("myfile.txt"):
        print 'found it'
        self.progressbar.stop()
        self.popup.destroy()
    else:
        print 'not created yet'
        self.after(100, self.checkfile) # Call this method after 100 ms.

rawDescription: Keep tkinter Progressbar running until a file is created


code:
records = [
    {'p_pnref': 'E78P2DFEA7E3', 'p_result': '12'},
    {'p_pnref': 'E78P2E93B933', 'p_result': '0'},
    {'p_pnref': 'E35P0A5578D3', 'p_result': '12'},
    {'p_pnref': 'E24P0AA506C3', 'p_result': '24'},
    {'p_pnref': 'E25P0AFF2C43', 'p_result': '24'},
    {'p_pnref': 'E34P0B4909A3', 'p_result': '24'},
]

for record in reversed(records):
    if record['p_result'] == '0':
        print record['p_pnref']
        break

rawDescription: How can I loop through a list of dicts, backwards, until a particular key value is found?

description: Loop through a list of dicts, backwards, until a particular key value is found


code:
from jinja2 import Template
template = Template('Hello {{ name }}!')
print template.render(name='John Doe')

rawDescription: Using app engine interactive console with jinja2 template


code:
import re
x="00ffabcafe4c"
print re.sub(r"(\w{2}(?!$))",r"\1:",x)

rawDescription: Python Include char every x positions

description: Python Include char every x positions


code:
import sys

file1=sys.argv[1]
file2=sys.argv[2]

dictA= dict()
with open(file1,'r') as f1:
    for line1 in f1:
        dictA[line1.rstrip('\n')] = 1

with open(file2,'r') as f2:
    for line1 in f2:
        listB = line1.rstrip('\n').split('\t')
        if listB[1] in dictA:
            print "%s\t%s" % (listB[0], listB[1])

rawDescription: Transforming perl script to python

description: Transforming perl script to python


code:
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
import sys
import codecs

class Downloader(QObject):
    # To be emitted when every items are downloaded
    done = Signal()

    def __init__(self, urlList, parent = None):
        super(Downloader, self).__init__(parent)
        self.urlList = urlList
        self.counter = 0
        # As you probably don't need to display the page
        # you can use QWebPage instead of QWebView
        self.page = QWebPage(self)
        self.page.loadFinished.connect(self.save)
        self.startNext()

    def currentUrl(self):
        return self.urlList[self.counter][0]

    def currentFilename(self):
        return self.urlList[self.counter][1]

    def startNext(self):
        print "Downloading %s..."%self.currentUrl()
        self.page.mainFrame().load(self.currentUrl())

    def save(self, ok):
        if ok:
            data = self.page.mainFrame().toHtml()
            with codecs.open(self.currentFilename(), encoding="utf-8", mode="w") as f:
                f.write(data)
            print "Saving %s to %s."%(self.currentUrl(), self.currentFilename())
        else:
            print "Error while downloading %s\nSkipping."%self.currentUrl()
        self.counter += 1
        if self.counter < len(self.urlList):
            self.startNext()
        else:
            self.done.emit()

urlList = [("http://news.google.com", "google.html"),
    ("http://www.stackoverflow.com","stack.html"),
    ("http://www.imdb.com", "imdb.html")]

app = QApplication(sys.argv)
downloader = Downloader(urlList)
# Quit when done
downloader.done.connect(app.quit)

# To view the pages
web = QWebView()
# To prevent user action that would interrupt the current page loading
web.setDisabled(True)
web.setPage(downloader.page)
web.show()

sys.exit(app.exec_())

rawDescription: Python QtWebKit save webpage to file

description: Python QtWebKit save webpage to file


rawDescription: How to add prefix and suffix to a string in python

description: Add prefix and suffix to a string in python


rawDescription: decode encoded JSON result in python

description: Decode encoded JSON result in python


code:
class StoreHandler(BaseHTTPRequestHandler):
    def do_POST(self):
        length = int(self.headers['content-length'])
        print length
        if length > 10000000:
            print "file to big"
            read = 0
            while read < length:
                read += len(self.rfile.read(min(66556, length - read)))
            self.respond("file to big")
            return
        else:
            form = cgi.FieldStorage(
                fp=self.rfile,
                headers=self.headers,
                environ={'REQUEST_METHOD':'POST',
                         'CONTENT_TYPE':self.headers['Content-Type'],
                         })
            filename = form['file'].filename
            data = form['file'].file.read()
            open("/tmp/%s"%filename, "wb").write(data)
            print self.headers['content-length']
            self.respond("uploaded %s, thanks")

rawDescription: python BaseHTTPServer file upload with maxfile-size

description: Python BaseHTTPServer file upload with maxfile-size


code:
time = '0.0425 hours'

# Extract time from string
time = time.split()[0]

# Convert time to a number of hours, then a number of seconds
time = float(time)
time = int(time * 3600)

# Compute the constituent elements of the time
hours = time // 3600
minutes = (time // 60) % 60
seconds = time % 60

# Print the result
print '{hours} Hours {minutes} minutos {seconds} seconds'.format(
    hours=hours, minutes=minutes, seconds=seconds)

rawDescription: Express time in hours units to verbose time


code:
if 'safari' in userAgentString.lower() and 'chrome' not in userAgentString.lower():
    print "Found Safari"

rawDescription: Efficient User-Agent Regex to find Safari in Python