code:
import random
import copy

#************************************
# construct some sample data
#headingline = ["timestamp","x","Pdc1","Pdc1","x","Pdc1","Pdc2","x","Pdc1","Pdc2","Pdc3","x"]

headingtext = 'Date;Time;INV;Pac;DaySum;Status;Error;Pdc1;Pdc2;Udc1;Udc2;Uac;INV;Pac;DaySum;Status;Error;Pdc1;Pdc2;Udc1;Udc2;Uac;INV;Pac;DaySum;Status;Error;Pdc1;Pdc2;Udc1;Udc2;Uac;INV;Pac;DaySum;Status;Error;Pdc1;Pdc2;Pdc3;Udc1;Udc2;Udc3;Uac;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc;INV;Pac;DaySum;Status;Error;Pdc1;Udc;Uac;Idc'
headingline = headingtext.split(";")

print headingline

NROWS = 30

# this is one line of data
dataline = [i for i in range(len(headingline))]
# data table has some copies of the dataline
datatable = [copy.copy(dataline) for i in range(NROWS)]
# insert some random values in the time column - when a smaller value is encountered, that's
# like rolling over an hour
for j in datatable:
    j[1] = random.random()

#************************************
# scan through headingline, looking for Pdcx
# scanfields will be a list of lists - one list for
#  each sequence of adjacent columns starting with Pdc1
scanfields = []
i = 0
while i < len(headingline):
    if headingline[i] == "Pdc1":
        # start collecting
        thisvalue = [i]
        i += 1
        while i < len(headingline) and "Pdc" in headingline[i] and "Pdc1" not in headingline[i]:
            thisvalue.append(i)
            i += 1
        # add the list of adjacent columns as one entry in the scanfields
        scanfields.append(thisvalue)
    else:
        i += 1
print "Input headings are:",headingline
print "columns to accumulate are:",scanfields

#************************************
# scan through the data, constructing the results
# initialise results to 0
wr = [0 for i in scanfields]
nreadings = 0
# put in an impossible value for lasttimestamp so that a rollover
# can't happen on the first reading
lasttimestamp = -1
for line in datatable:
    timestamp = line[1]
    if timestamp < lasttimestamp:
        # we have rolled over an hour
        # output the results
        print lasttimestamp,wr
        # reset for start of accumulation
        wr = [0 for i in scanfields]
        nreadings = 0
    # accumulate some data
    for i,columns in enumerate( scanfields ):
        for column in columns:
            wr[i] += line[column]
    nreadings += 1
    lasttimestamp = timestamp

# if there is some, print any remaining data
if nreadings>0:
    print timestamp,wr

rawDescription: how to work with several 'Pdcx' (x = 1 to 3) columns in csv file with python

description: Work with several 'Pdcx' (x = 1 to 3) columns in csv file with python


rawDescription: Python - Retrieving last 30 days data from dataframe pandas


code:
import scipy.optimize as so
import numpy as np

def fitfunc(x,p):
   if x>p:
      return x-p
   else:
      return -(x-p)

fitfunc_vec = np.vectorize(fitfunc) #vectorize so you can use func with array

def fitfunc_vec_self(x,p):
  y = np.zeros(x.shape)
  for i in range(len(y)):
    y[i]=fitfunc(x[i],p)
  return y

x=np.arange(1,10)
y=fitfunc_vec_self(x,6)+0.1*np.random.randn(len(x))

popt, pcov = so.curve_fit(fitfunc_vec_self, x, y) #fitting routine that gives error
print popt
print pcov

rawDescription: Fitting piecewise function in Python

description: Fitting piecewise function in Python


code:
d = 2
a = np.random.random((2,3,4))
for i in np.ndindex(a.shape[:d]):
    print i, a[i]

rawDescription: Iterating over first d axes of numpy array


code:
'''
Sources:
http://opencv.willowgarage.com/documentation/python/cookbook.html
http://www.lucaamore.com/?p=638
'''

#Python 2.7.2
#Opencv 2.4.2
#PIL 1.1.7

import cv #Opencv
import Image #Image from PIL
import glob
import os

def DetectFace(image, faceCascade, returnImage=False):
    # This function takes a grey scale cv image and finds
    # the patterns defined in the haarcascade function
    # modified from: http://www.lucaamore.com/?p=638

    #variables
    min_size = (20,20)
    haar_scale = 1.1
    min_neighbors = 3
    haar_flags = 0

    # Equalize the histogram
    cv.EqualizeHist(image, image)

    # Detect the faces
    faces = cv.HaarDetectObjects(
            image, faceCascade, cv.CreateMemStorage(0),
            haar_scale, min_neighbors, haar_flags, min_size
        )

    # If faces are found
    if faces and returnImage:
        for ((x, y, w, h), n) in faces:
            # Convert bounding box to two CvPoints
            pt1 = (int(x), int(y))
            pt2 = (int(x + w), int(y + h))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 5, 8, 0)

    if returnImage:
        return image
    else:
        return faces

def pil2cvGrey(pil_im):
    # Convert a PIL image to a greyscale cv image
    # from: http://pythonpath.wordpress.com/2012/05/08/pil-to-opencv-image/
    pil_im = pil_im.convert('L')
    cv_im = cv.CreateImageHeader(pil_im.size, cv.IPL_DEPTH_8U, 1)
    cv.SetData(cv_im, pil_im.tostring(), pil_im.size[0]  )
    return cv_im

def cv2pil(cv_im):
    # Convert the cv image to a PIL image
    return Image.fromstring("L", cv.GetSize(cv_im), cv_im.tostring())

def imgCrop(image, cropBox, boxScale=1):
    # Crop a PIL image with the provided box [x(left), y(upper), w(width), h(height)]

    # Calculate scale factors
    xDelta=max(cropBox[2]*(boxScale-1),0)
    yDelta=max(cropBox[3]*(boxScale-1),0)

    # Convert cv box to PIL box [left, upper, right, lower]
    PIL_box=[cropBox[0]-xDelta, cropBox[1]-yDelta, cropBox[0]+cropBox[2]+xDelta, cropBox[1]+cropBox[3]+yDelta]

    return image.crop(PIL_box)

def faceCrop(imagePattern,boxScale=1):
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load('haarcascade_frontalface_alt.xml')

    imgList=glob.glob(imagePattern)
    if len(imgList)<=0:
        print 'No Images Found'
        return

    for img in imgList:
        pil_im=Image.open(img)
        cv_im=pil2cvGrey(pil_im)
        faces=DetectFace(cv_im,faceCascade)
        if faces:
            n=1
            for face in faces:
                croppedImage=imgCrop(pil_im, face[0],boxScale=boxScale)
                fname,ext=os.path.splitext(img)
                croppedImage.save(fname+'_crop'+str(n)+ext)
                n+=1
        else:
            print 'No faces found:', img

def test(imageFilePath):
    pil_im=Image.open(imageFilePath)
    cv_im=pil2cvGrey(pil_im)
    # Select one of the haarcascade files:
    #   haarcascade_frontalface_alt.xml  <-- Best one?
    #   haarcascade_frontalface_alt2.xml
    #   haarcascade_frontalface_alt_tree.xml
    #   haarcascade_frontalface_default.xml
    #   haarcascade_profileface.xml
    faceCascade = cv.Load('haarcascade_frontalface_alt.xml')
    face_im=DetectFace(cv_im,faceCascade, returnImage=True)
    img=cv2pil(face_im)
    img.show()
    img.save('test.png')

# Test the algorithm on an image
#test('testPics/faces.jpg')

# Crop all jpegs in a folder. Note: the code uses glob which follows unix shell rules.
# Use the boxScale to scale the cropping area. 1=opencv box, 2=2x the width and height
faceCrop('testPics/*.jpg',boxScale=1)

rawDescription: Detect face then autocrop pictures

description: Detect face then autocrop pictures


code:
import math
import random
import time
import sys

ball_min_radius = 16.0 #world coordinates
ball_max_radius = 128.0  #world coordniates
number_balls = 200

world_min_x = -200.0*number_balls**.5  # minimum x in world coordinates
world_max_x = +200.0*number_balls**.5  # maximum x in world coordinates
world_min_y = -200.0*number_balls**.5  # minimum y in world coordinates
world_max_y = +200.0*number_balls**.5  # maximum y in world coordinates

class Ball:
    """
    Implements a point/ball
    """

    def __init__(self):
          self.x = random.uniform(world_min_x,world_max_x)
          self.y = random.uniform(world_min_y,world_max_y)
          self.radius = int(random.uniform(ball_min_radius,ball_max_radius))
    def __lt__(self, other):
        return self.id < other.id

    def __str__(self):
        return 'x={x} y={y} r={r}'.format(x=self.x, y=self.y, r=self.radius)

def main():

    balls = [Ball() for i in range(number_balls)]

    dict_of_balls = {}
    ball_collection = []
    for b in balls:
        xb = int(b.x/256)
        yb = int(b.y/256)
        key = (xb, yb)
        dict_of_balls[key] = b

        ball_collection.append((key, b))

    print 'length of dictionary:{}'.format(len(dict_of_balls.keys()))
    print 'length of collection:{}'.format(len(ball_collection))

rawDescription: python - divide world into bins

description: Python - divide world into bins


code:
import random
def binarySearch(alist, item):
        first = 0
        last = len(alist) - 1
        found = False

        while first<=last and not found:
            midpoint = (first + last)//2
            if alist[midpoint] == item:
                found = True
            else:
                if item < alist[midpoint]:
                    last = midpoint-1
                else:
                    first = midpoint+1
        return found

def findThisNum(mynum):

    testlist = [x for x in range(listlength)]

    print "testlist = ", testlist
    print "finding number ", mynum

    if (binarySearch(testlist, findnum)) == True:
        print "found %d" %mynum
    else:
        print "Not found %d" %mynum

#### Main_Function ####

if __name__ == "__main__":
    #

    #Search 1 [ Even numbered list ]
    listlength = 10
    findnum = random.randrange(0,listlength)
    findThisNum(findnum)

    #Search 2 [ [ Odd numbered list ]
    listlength = 13
    findnum = random.randrange(0,listlength)
    findThisNum(findnum)

    #search 3  [ find item not in the list ]

    listlength = 13
    findnum = random.randrange(0,listlength) + listlength
    findThisNum(findnum)

rawDescription: Binary search of a number within a list in Python


code:
for x in range(10):
    print data[0]

rawDescription: Is it possible to display only first list element in for loop?


code:
n = 6
for i in xrange(16):
    print '{:0{}b}'.format(i, n)
    # OR  print '{:06b}'.format(i)

    # OR  print '{:b}'.format(i)  if you want no leading 0s.

rawDescription: Binary numbers of N digits

description: Binary numbers of N digits


code:
from itertools import product
option1 = {1, 2, 3}
option2 = {4, 5}
option3 = {2, 3}
for tup in product(option1, option2, option3):
    print tup

rawDescription: all possible combinations from a configuration space model

description: All possible combinations from a configuration space model


code:
import socket
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("localhost", 8220))

for index in xrange(5):
    data = "GET\nSONAR%d\n\n" % index
    print 'send to server: ' + data
    client_socket.send(data)
    while client_socket.recv(2048) != "ack":
        print "waiting for ack"
    print "ack received!"

#send disconnect message
dmsg = "disconnect"
print "Disconnecting"
client_socket.send(dmsg)

client_socket.close()

rawDescription: Repeating/Continuous python server and client


code:
import re
from lxml import html

def replace_link(link):
    if re.match(r"\.\./[^/]+/$", link):
        link += "index.html"
    return link

print html.rewrite_links(your_html_text, replace_link)

rawDescription: Python and re.compile return inconsistent results


rawDescription: Method to read data from a file when plot histogram with python?


code:
def _getdep(item):
    local_list, temp_list = [], []
    temp_list.extend(internal_dep[item])
    temp_list.extend(external_dep[item])
    local_list.extend(temp_list)
    for new_item in temp_list:
        local_list.extend(_getdep(new_item))
    return local_list

build_dep_list = []
for item in mainlist:
    build_dep_list.append(item)
    build_dep_list.extend(_getdep(item))

print build_dep_list

['12345', '01242', '34567', '23456', '56789', '32345', '33456', '11111', '23456', '33456', '09768', '12222', '34333']

rawDescription: Recursive algorithm for generating a acyclic directed graph


code:
import locale

def guess_notepad_encoding(filepath, default_ansi_encoding=None):
    with open(filepath, 'rb') as f:
        data = f.read(3)
    if data[:2] in ('\xff\xfe', '\xfe\xff'):
        return 'utf-16'
    if data == u''.encode('utf-8-sig'):
        return 'utf-8-sig'
    # presumably "ANSI"
    return default_ansi_encoding or locale.getpreferredencoding()

if __name__ == "__main__":
    import sys, glob, codecs
    defenc = sys.argv[1]
    for fpath in glob.glob(sys.argv[2]):
        print
        print (fpath, defenc)
        with open(fpath, 'rb') as f:
            print "raw:", repr(f.read())
        enc = guess_notepad_encoding(fpath, defenc)
        print "guessed encoding:", enc
        with codecs.open(fpath, 'r', enc) as f:
            for lino, line in enumerate(f, 1):
                print lino, repr(line)
                print lino, repr(line.rstrip('\r\n'))

rawDescription: how to read a file that can be saved as either ansi or unicode in python?

description: Read a file that can be saved as either ansi or unicode in python


code:
with open('foo.txt') as f:
    for line in f:
        a, b = line.split('<', 1)
        b = '#@ <' + b.rstrip('. \n')
        print b
        print a.rstrip() + '.'

rawDescription: Transform text in python

description: Transform text in python


code:
h = Handler()
print h.CLIENT_ID
s = SubHandler()
print s.CLIENT_ID

rawDescription: How to find the name of a property from a Python class

description: Find the name of a property from a Python class


code:
import datetime
import re

class RaceTimedelta(datetime.timedelta):
    def __new__(cls, timestr=''):
        m = re.match(r'(\d+):(\d+\.\d+)', timestr)
        if m:
            mins, secs = int(m.group(1)), float(m.group(2))
            return super(RaceTimedelta, cls).__new__(cls,
                                                minutes=mins, seconds=secs)
        else:
            raise ValueError('timestr not in format u"mm:ss.d"')

print RaceTimedelta(u'24:45.7')

rawDescription: Python: how to extend datetime.timedelta


code:
import re

dicreg = re.compile('(?<=\{)[^}]*}')

kvregx = re.compile('[ \r\n]*'
                    '(" *)?((location)|[^:]+?)(?(1) *")'
                    '[ \r\n]*'
                    ':'
                    '[ \r\n]*'
                    '(?(3)|(" *)?)([^:]*?)(?(4) *")'
                    '[ \r\n]*(?:,(?=[^,]+?:)|\})')

checking_dict = {}
checking_list = []

filename = 'zzz.txt'

with open(filename) as f:

    ######## First part: to gather all the keys in all the dictionaries

    prec,chunk = '','go'
    ecr = []
    while chunk:
        chunk = f.read(120)
        ss = ''.join((prec,chunk))
        ecr.append('\n\n------------------------------------------------------------\nss   == %r' %ss)
        mat_dic = None
        for mat_dic in dicreg.finditer(ss):
            ecr.append('\nmmmmmmm dictionary found in ss mmmmmmmmmmmmmm')
            for mat_kv in kvregx.finditer(mat_dic.group()):
                k,v = mat_kv.group(2,5)
                ecr.append('%s  :  %s' % (k,v))
                if k in checking_list:
                    checking_dict[k] += 1
                else:
                    checking_list.append(k)
                    checking_dict[k] = 1
        if mat_dic:
            prec = ss[mat_dic.end():]
        else:
            prec += chunk

    print '\n'.join(ecr)
    print '\n\n\nchecking_dict == %s\n\nchecking_list        == %s' %(checking_dict,checking_list)

    ######## The keys are sorted in order that the less frequent ones are at the end
    checking_list.sort(key=lambda k: checking_dict[k], reverse=True)
    posis = dict((k,i) for i,k in enumerate(checking_list))
    print '\nchecking_list sorted == %s\n\nposis == %s' % (checking_list,posis)

    ######## Now, the file is read again to build a list of rows

    f.seek(0,0)  # the file's pointer is move backed to the beginning of the file

    prec,chunk = '','go'
    base = [ '' for i in xrange(len(checking_list))]
    rows = []
    while chunk:
        chunk = f.read(110)
        ss = ''.join((prec,chunk))
        mat_dic = None
        for mat_dic in dicreg.finditer(ss):
            li = base[:]
            for mat_kv in kvregx.finditer(mat_dic.group()):
                k,v = mat_kv.group(2,5)
                li[posis[k]] = v
            rows.append(li)
        if mat_dic:
            prec = ss[mat_dic.end():]
        else:
            prec += chunk

    print '\n\n%s\n%s' % (checking_list,30*'___')
    print '\n'.join(str(li) for li in rows)

rawDescription: Reading a file with unproperly marked dictionaries


code:
def parse(x):
    operators = set('+-*/')
    op_out = []    #This holds the operators that are found in the string (left to right)
    num_out = []   #this holds the non-operators that are found in the string (left to right)
    buff = []
    for c in x:  #examine 1 character at a time
        if c in operators:
            #found an operator.  Everything we've accumulated in `buff` is
            #a single "number". Join it together and put it in `num_out`.
            num_out.append(''.join(buff))
            buff = []
            op_out.append(c)
        else:
            #not an operator.  Just accumulate this character in buff.
            buff.append(c)
    num_out.append(''.join(buff))
    return num_out,op_out

print parse('3/2*15')

rawDescription: parsing math expression in python and solving to find an answer

description: Parsing math expression in python and solving to find an answer


code:
from stemming.porter2 import stem
from nltk.corpus import stopwords
stopwords_ =  stopwords.words('english')
def addString(x):
   flag = True
   y = [stem(j).lower() for j in x.split() if j.lower() not in stopwords_]
   for i in section:
      i = [stem(j).lower() for j in i.split() if j.lower() not in stopwords_]
      if y==i:
         flag = False
         break
   if flag:
      section.append(x)
      print "\tNew Section Added"

rawDescription: Need String Comparison 's solution for Partial String Comparison in Python


code: print venue.id, venue.name

rawDescription: Retrieving Venue Information with pylast and last.fm


code:
def change(x):
    x[0] = 3

x = [1]
change(x)
print x

rawDescription: Python passing an integer by reference

description: Python passing an integer by reference


code:
from scipy import optimize

def func(x):
    return (x - 2) * (x + 2)**2

def func2(x):
    return (x - 2) * x * (x + 2)**2

min = 0
max = 1

res1 = optimize.fminbound(func, min, max)
res2 = optimize.minimize_scalar(func, bounds=(min,max))
res3 = optimize.fminbound(func2, min, max)
res4 = optimize.minimize_scalar(func2, bounds=(min,max))

print res1, res2.x
print res3, res4.x

import matplotlib.pyplot as plt
import numpy as np

xaxis = np.arange(-15,15)

plt.plot(xaxis, func(xaxis))
plt.plot(xaxis, func2(xaxis))
plt.scatter(res2.x, res2.fun)
plt.scatter(res4.x, res4.fun)
plt.show()

rawDescription: What is the difference between scipy.optimize.fminbound and scipy.optimize.minimize_scalar(bounds=(0,1))


code:
if choice_day == "monday" and choice_period == "NO":
    print '\n'.join(sorted(monday.values()))

rawDescription: Shortening down printing single values in a dictionary


code:
from threading import Thread

class myClassA(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'A'

class myClassB(Thread):
    def __init__(self):
        Thread.__init__(self)
        self.daemon = True
        self.start()
    def run(self):
        while True:
            print 'B'

myClassA()
myClassB()
while True:
    pass

rawDescription: Running infinite loops using threads in python


code:
import ctypes

# Drive types
DRIVE_UNKNOWN     = 0  # The drive type cannot be determined.
DRIVE_NO_ROOT_DIR = 1  # The root path is invalid; for example, there is no volume mounted at the specified path.
DRIVE_REMOVABLE   = 2  # The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.
DRIVE_FIXED       = 3  # The drive has fixed media; for example, a hard disk drive or flash drive.
DRIVE_REMOTE      = 4  # The drive is a remote (network) drive.
DRIVE_CDROM       = 5  # The drive is a CD-ROM drive.
DRIVE_RAMDISK     = 6  # The drive is a RAM disk.

# Map drive types to strings
DRIVE_TYPE_MAP = { DRIVE_UNKNOWN     : 'DRIVE_UNKNOWN',
                   DRIVE_NO_ROOT_DIR : 'DRIVE_NO_ROOT_DIR',
                   DRIVE_REMOVABLE   : 'DRIVE_REMOVABLE',
                   DRIVE_FIXED       : 'DRIVE_FIXED',
                   DRIVE_REMOTE      : 'DRIVE_REMOTE',
                   DRIVE_CDROM       : 'DRIVE_CDROM',
                   DRIVE_RAMDISK     : 'DRIVE_RAMDISK'}

# Return list of tuples mapping drive letters to drive types
def get_drive_info():
    result = []
    bitmask = ctypes.windll.kernel32.GetLogicalDrives()
    for i in range(26):
        bit = 2 ** i
        if bit & bitmask:
            drive_letter = '%s:' % chr(65 + i)
            drive_type = ctypes.windll.kernel32.GetDriveTypeA('%s\\' % drive_letter)
            result.append((drive_letter, drive_type))
    return result

# Test
if __name__ == '__main__':
    drive_info = get_drive_info()
    for drive_letter, drive_type in drive_info:
        print '%s = %s' % (drive_letter, DRIVE_TYPE_MAP[drive_type])
    removable_drives = [drive_letter for drive_letter, drive_type in drive_info if drive_type == DRIVE_REMOVABLE]
    print 'removable_drives = %r' % removable_drives

rawDescription: How do I search for a file only on USB flash drives?

description: Search for a file only on USB flash drives


code:
print "Y"

def f1():
    pass

def f2():
    pass

rawDescription: import behaviour with already loaded modules

description: Import behaviour with already loaded modules


code:
import xml.etree.ElementTree as etree
document = etree.parse(open("your.xml"))

secondIdentifier = document.find(".//product/identifier[2]")
firstContributor = document.find(".//product/contributor[1]")
print secondIdentifier, firstContributor

rawDescription: Targeting specific sub-elements when parsing XML with Python


code:
def to_class(func):
    """ returns a decorator
    aimed to force the result to be of class cls. """
    def wrapper(cls, *args, **kwargs):
        return cls(func(*args, **kwargs))
    return classmethod(wrapper)

class TestClass(object):
    def __init__(self, value):
        self._value = (value, value)
    def __str__(self):
        return str(self._value)
    @to_class
    def test_func(value):
        return value

TestClass.test_func('foo')
print TestClass.test_func('foo')

rawDescription: Trying to program a "to_class" general decorator


code:
pat = re.compile(r'(^#[0-9]{2,})\s*\n((?:[^#].*\n)*)', re.MULTILINE)
for t, e in pat.findall(s):
    print t, e.count('\n')

rawDescription: Regex - matching all text between two strings


code:
import sys
from select import select

print "Press any key to configure or wait 5 seconds..."
timeout = 5
rlist, wlist, xlist = select([sys.stdin], [], [], timeout)

if rlist:
    print "Config selected..."
else:
    print "Timed out..."

rawDescription: Python wait x secs for a key and continue execution if not pressed


rawDescription: Apply non-linear regression for multi dimension data samples in Python


code:
import timeit
import numpy as np
print 'Numpy v%s' %np.version.full_version
stmt = "np.all(x)"
for ii in xrange(9):
    setup = "import numpy as np; x = np.zeros(%d,dtype=np.bool); np.setbufsize(%d)" %(10**ii, max(8192, min(10**ii, 10**7)))
    timer = timeit.Timer(stmt,setup)
    n,r = 1,3
    t = np.min(timer.repeat(r,n))
    while t < 0.2:
        n *= 10
        t = np.min(timer.repeat(r,n))
    t /= n
    if t < 1E-3:
        timestr = "%1.3f us" %(t*1E6)
    elif t < 1:
        timestr = "%1.3f ms" %(t*1E3)
    else:
        timestr = "%1.3f s" %t
    print "Array size: 1E%i, %i loops, best of %i: %s/loop" %(ii,n,r,timestr)

rawDescription: Why is numpy.any so slow over large arrays?


code:
class Foo(object):
  def baz(self):
    print 'Original Foo.baz'
class Bar(Foo): pass
def newprint(self):
  print 'New Foo.baz'
x = Foo()
y = Bar()
Foo.baz = newprint
a = Foo()
b = Bar()
map(lambda k: k.baz(), (x, y, a, b))

rawDescription: Thead-safe object and dict types in python


code:
print list(itertools.chain.from_iterable(l))

#output =[1, 2, 3, 4, 5, 6]

rawDescription: How to collect list of tuples into one tuple in Python?

description: Collect list of tuples into one tuple in Python


code:
class NestedContext(object):
    _current = None
    def __init__(self, name):
        self._name = name
    def __repr__(self):
        return '<NestedContext({._name!r})>'.format(self)
    def __enter__(self):
        self.parent = self._current
        NestedContext._current = self
    def __exit__(self, _type, _value, _tb):
         NestedContext._current = self.parent

with NestedContext('outer'):
    print NestedContext._current
    with NestedContext('inner'):
        print NestedContext._current
        print NestedContext._current.parent

rawDescription: Class variable in python context class

description: Class variable in python context class


code: print max_gap(L3)

rawDescription: Calculating the gap between pairs of twin primes in python


code:
from bs4 import BeautifulSoup, Tag

l = ['test', 'test
', '
']
for item in l:
    soup = BeautifulSoup(item, 'html.parser')
    print item, any(isinstance(element, Tag) for element in soup)

rawDescription: Use python to find html or js tags. (regex?)


rawDescription: How can I print variable and string on same line in Python?

description: Print variable and string on same line in Python


code:
dog_sense = wn.synsets('dog')[0] # It's http://goo.gl/b9sg9X
jb_sense = wn.synsets('James_Baldwin')[0] # It's http://goo.gl/CQQIG9

# Features to check against whether the 'dubious' concept is a human or an animal
human = wn.synset('person.n.01') # i.e. [+human]
animal = wn.synset('animal.n.01') # i.e. [+animal]

if dog_sense.wup_similarity(animal) > dog_sense.wup_similarity(human):
  print "Dog is more of an animal than human"
elif dog_sense.wup_similarity(animal) < dog_sense.wup_similarity(human):
  print "Dog is more of a human than animal"

rawDescription: Wordnet selectional restrictions in NLTK


code:
def wrapper1(func, *args): # with star
    func(*args)

def wrapper2(func, args): # without star
    func(*args)

def func2(x, y, z):
    print x+y+z

wrapper1(func2, 1, 2, 3)
wrapper2(func2, [1, 2, 3])

rawDescription: Call a function with argument list in python


code:
t = 'vertical \tab'  # see what I did there?
print t
# notice the actual tab above
t.replace('\\', '')  # trying to remove literal slash
# that didn't work
s.replace('\\', '')  # try it on the string above which has an actual slash

rawDescription: Why appears backslash in text?

description: Why appears backslash in text


code:
# use the attribute now that we know it
print signed_data.timestamp
# make it pretty
print datetime.datetime.fromtimestamp(float(signed_data.timestamp))

rawDescription: python gnupg timestamp

description: Python gnupg timestamp


code:
frob = lambda f, x: x if str(x) == str(x)[::-1] else f(f, x + int(str(x)[::-1]))

print frob(frob, 29)

rawDescription: Generating list until certain condition is met using a single line


code:
import requests
import lxml.html

url = "https://www.virustotal.com/tr/file/7b0140ef63a604abaad37538e58fc2d4f8a50228818bf12afbdbcb9d51b44b96/analysis/"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:29.0) Gecko/20100101 Firefox/29.0",
}

response = requests.get(url, headers=headers)

print response # status

html = lxml.html.fromstring(response.content)

print html.cssselect('title')[0].text

rawDescription: Python Get Data From HTML Page

description: Python Get Data From HTML Page


code:
def calculate(self, red=None, blue=None, yellow=None):
    red, blue, yellow = map(
        lambda (a, m): m if a is None else a,
        zip([red, blue, yellow], [self.red, self.blue, self.yellow]))
    return red + blue + yellow

rawDescription: Python: How to override data attributes in method calls?


code: print all(function(data) for function in (x, y))

rawDescription: Logical and of two functions

description: Logical and of two functions


code:
import re
filename = "NRC_401653_XL3213456321_NRCE_KR.pdf"
print re.sub(r"\w{3}_\d+_(\w+)_NRCE_KR\.pdf", r"\1", filename)

rawDescription: Python regex - strip out beginning and end and leave middle untouched


code:
def print_everything(*args):
        print '{0}. {1}'.format(count, thing)
print_everything('apple', 'banana', 'cabbage')

rawDescription: *args and **kwargs?

description: *args and **kwargs


code:
from glob import glob
from sys import argv

for filename in glob(argv[1]):
    print filename

rawDescription: Passing multple files with asterisk to python shell in Windows


code:
def main():
  with open('data.txt', 'r') as searchfile:
    for line in searchfile:
      line = line.strip()
      # This works because Python checks this substring in the line
      if ("Run=true") in line:
        firstSplit = line.split("Idle=")[1]
        # Splitting again to get the first element before the whitespace
        idleNum = int(firstSplit.split('\s')[0])
        # This had to be changed as you are comparing integers while
        # the data is still of type string
        if (idleNum >=10):
          print line

if __name__ == '__main__':
  main()

rawDescription: Searching for numeric values and conditions in text file (Python)


code:
def a(b): #Line 1
  print "a",b
def c(b):
  n = globals().copy()
  if b is None:
    def a2(b):
      print 'local',b
    n['a'] = a2
  n['a'](b) #Line 8

rawDescription: What is the pythonic way to have conditional shadowing?


code:
class Int(int):
    'Make comparisons visible'
    def __cmp__(self, other):
        print 'Comparing %s to %d' % (self, other)
        return int.__cmp__(self, other)

ele1 = Int(1)
ele2 = Int(2)
ele3 = Int(3)
ele4 = Int(4)
ele5 = Int(5)

alist = [ele1, ele2, ele3, ele4, ele5]
if ele3 in alist:
  print "found"

rawDescription: Does Python's 'in' operator for lists have an early-out for successful searches


code:
groups      = []
enterprise  = []
workinglist = []

parse_csv(filename, groups, enterprise, workinglist)

print groups # should contain the data read from CSV

rawDescription: Python Access Variables Outside of Function


code:
sock = urllib2.urlopen(request)
ch = sock.read()
sock.close()

print '\n'.join(str(i) + '  ' + repr(line)
                for i,line in enumerate(ch.splitlines(True)))

rawDescription: Read contents of <script> with BeautifulSoup


code:
total_rate = 0
vrates = 0
funrate = 0
rates5 = 0

def food():
    global rates5
    # files are used
    rates5 = 1
    # calculate the rate and store the total rate in rates5

def recreation():
    global funrate
    # files are used
    funrate = 2
    # calculate the rate and store the total rate in funrate

def transport():
    global vrates
    # files are used
    vrates = 3
    # calculate the rate and store the total rate in vrates

food()
recreation()
transport()
total_rate = rates5 + vrates + funrate
print"The total amount =", total_rate

rawDescription: Something wrong with this file program?

description: Something wrong with this file program


code:
after_short_program = [
    'Evgeni Plushenko',
    'Evan Lysacek',
    'Daisuke Takahashi',
    'Nobunari Oda',
    'Stephane Lambiel',
]

after_free_skate = [
    'Evan Lysacek',
    'Daisuke Takahashi',
    'Evgeni Plushenko',
    'Stephane Lambiel',
    'Nobunari Oda',
]

for i,item in enumerate(after_free_skate):
    diff = after_short_program.index(item)-i
    print "%s (%+d)"%(item,diff)

rawDescription: Positional Comparisons in Python

description: Positional Comparisons in Python


code:
while True:
    name = raw_input("What is your name?")
    if name == "Jane":
        print name + " is awesome"
        break
    else:
        print name + " is not awesome"

rawDescription: Raw input not working as I expected - am I missing a module?


code:
import os
for i in list1:
    fileName, fileExtension = os.path.splitext(i)
    print fileExtension

rawDescription: Get all characters after a certain character?


code:
class Animal(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def description(self):
        print self.name
        print self.age
hippo = Animal('Alex', 12)
hippo.description()

rawDescription: How do I print methods of a class

description: Print methods of a class


code:
import os
import signal

PID = os.getpid()

def do_nothing(*args):
    pass

def foo():
    print "Initializing..."
    a=10
    os.kill(PID, signal.SIGUSR1)
    print "Variable value is %d" % (a)
    print "All done!"

signal.signal(signal.SIGUSR1, do_nothing)

foo()

rawDescription: Debugging: stepping through Python script using gdb?


code:
for s in get_strings(["dead", "beef"], "0", 12):
    print s

rawDescription: Generate a list of strings in python

description: Generate a list of strings in python


code:
os.sys.path.remove("")
import redis
os.sys.path.insert(0, "")

class RedisExtension(Extension):
    def __init__(self, config):
        print redis

class Redis(Facade):
    pass

rawDescription: importing from venv folder with same file name

description: Importing from venv folder with same file name


code:
gen = growing_sequence()
next(gen)
next(gen)
for i, lst in enumerate(gen):
    print i, lst
    if i == 2: break

rawDescription: Most Pythonic way to iteratively build up a list?


code:
def human_infoz():

    name = raw_input("Enter human name> ")
    address = raw_input("Human Address> ")
    phone = raw_input("Human Phone Number> ")
    email = raw_input("Human Email> ")
    listinfo = ["", name, address, phone, email, ""]
    return ' '.join(listinfo),name

go = human_infoz()
namez = go[1]
print namez

rawDescription: Print function attribute

description: Print function attribute


code:
import os
from subprocess import call

if  "OTHER_PROGRAM" in os.environ.keys():
    program_name =  os.environ["OTHER_PROGRAM"]
    print "will execute",   program_name
    call([program_name ,"some_file.pro"])
else:
    print "OTHER_PROGRAM env variable not defined"

rawDescription: How can I specify an alias in ERIC Python IDE?

description: Specify an alias in ERIC Python IDE


code:
alerts = {'ils.txdatasource.dbpool': '10435', 'SEND_PIX_TO_EXTERNAL_HOST_VIA_IFEE': '10423', 'SEND_ShipConfirm_TO_EXTNL_HOST_VIA_IFEE': '10421', 'alert4': 'id4'}
for name in alerts:
    print 'name: {0}, id: {1}'.format(name, alerts[name])

rawDescription: Using dictionary as a list

description: Using dictionary as a list


code:
bar = False
def someFunction():
    global bar
    bar = True
someFunction()
print bar

rawDescription: Change a variable from a function

description: Change a variable from a function


rawDescription: Fill datetimeindex gap by NaN

description: Fill datetimeindex gap by NaN


code:
important = ['Username', 'Name']
reordered = important + [c for c in df.columns if c not in important]
df = df[reordered]
print df

rawDescription: Preserving Column Order - Python Pandas and Column Concat


code:
class Foo:
    def foo(self): return 'foo'

class Bar(Foo):
    def bar(self): return self.foo() + 'bar'

foo = Foo()

foo.__class__ = Bar

print foo.bar()
isinstance(foo, Bar)
isinstance(foo, Foo)

rawDescription: extend an instance at runtime

description: Extend an instance at runtime


code:
try:
    while True: pass
except KeyboardInterrupt:
    print "b.py got ctrl-c"

rawDescription: Python sub process Ctrl+C

description: Python sub process Ctrl+C


code:
import socket

addr_range = "192.168.1.%d"

ip_address_up = []

# Use UDP.
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

s.settimeout(2.0)

for i in range(1, 254):
    try:
        ip = addr_range % i
        socket.gethostbyaddr(ip)
        ip_address_up.append(ip)
    except socket.herror as ex:
        pass

print ip_address_up

rawDescription: How to find out how many clients are on a certain address range?

description: Find out how many clients are on a certain address range


code:
import pandas as pd
import io

text = """      id        date  0000  0600  1200  1800
 0  APPL  01.01.2014    12    15    17    19
 1  APPL  02.01.2014    21    23    25    27
 2  MSFT  01.01.2014     1     2     3     4
 3  MSFT  02.01.2014     5     6     7     8"""

df = pd.read_csv(io.BytesIO(text), delim_whitespace=True)
df = df.set_index(["id", "date"]).stack().unstack(0)

def f(key):
    date, hour = key
    time = "{} {}:{}:00".format(date, hour[:2], hour[2:])
    return pd.to_datetime(time, dayfirst=True)

df.index = df.index.map(f)
print df

rawDescription: How to convert wide to long format with hourly values and datetime index?

description: Convert wide to long format with hourly values and datetime index


code:
# make: 00:00:00 stop watch timer format
def update_timeText():
  if (state):
    global timer
    timer[2] += 1
    if (timer[2] >= 60):
        timer[2] = 0
        timer[1] += 1
    if (timer[1] >= 60):
        timer[0] += 1
        timer[1] = 0
    timeString = pattern.format(timer[0], timer[1], timer[2])
    return timeString

# Step 1
global state
global timer
state = True
timer = [0, 0, 0]
pattern = '{0:02d}:{1:02d}:{2:02d}'

# Step 2
print update_timeText()
print update_timeText()
print update_timeText()
print update_timeText()

rawDescription: Python - how to make the stop watch format as hh:mm:ss instead of mm:ss:centisecond?


code:
import atexit
def save_cache():
    print 'save cache'
atexit.register(save_cache)

rawDescription: Is there a way to detect when a python program is going to end?


code:
def complex_file_to_dict(filename):
    out = dict()
    for line in open(filename):
        line = line.strip()
        if line.startswith('d.complex'):
            name = line
            out[name] = set()
        elif line:
            out[name].add(line)

    return out

res_1 = complex_file_to_dict('file1.txt')
res_2 = complex_file_to_dict('file2.txt')

for k1, set_1 in res_1.iteritems():
    for k2, set_2 in res_2.iteritems():
      print k1
      print k2
      for v in set_1 - set_2:
        print v
      print

rawDescription: Unable to get set intersection to work

description: Unable to get set intersection to work


code:
def merge(lsts):
  sets = [set(lst) for lst in lsts if lst]
  merged = 1
  while merged:
    merged = 0
    results = []
    while sets:
      common, rest = sets[0], sets[1:]
      sets = []
      for x in rest:
        if x.isdisjoint(common):
          sets.append(x)
        else:
          merged = 1
          common |= x
      results.append(common)
    sets = results
  return sets

lst = [[65, 17, 5, 30, 79, 56, 48, 62],
       [6, 97, 32, 93, 55, 14, 70, 32],
       [75, 37, 83, 34, 9, 19, 14, 64],
       [43, 71],
       [],
       [89, 49, 1, 30, 28, 3, 63],
       [35, 21, 68, 94, 57, 94, 9, 3],
       [16],
       [29, 9, 97, 43],
       [17, 63, 24]]
print merge(lst)

rawDescription: Python: simple list merging based on intersections


code: print word,

rawDescription: I need to make the output be in one line


code:
import oauth2 as oauth
import oauth2.clients.smtp as smtplib

consumer = oauth.Consumer('anonymous', 'anonymous')
token = oauth.Token('1/MI6B2DqJP4FEkDRLUKrD5l46sQ0758-2ucEKBY-DeB0', 'NysqNqVTulFsdHpSRrPP56sF')

url = "https://mail.google.com/mail/b/testing.oauth.1@gmail.com/smtp/"

conn = smtplib.SMTP('smtp.googlemail.com', 587)
conn.set_debuglevel(True)
conn.ehlo('test')
conn.starttls()

conn.authenticate(url, consumer, token)

header = 'To:testing.oauth.1@gmail.com\n' + 'From: testing.oauth.1@gmail.com\n' + 'Subject:testing \n'
msg = header + '\n this is test msg from me \n\n'
try:
    conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)
except Exception as ex:
    print str(ex)
    print 'retying ...'
    conn.authenticate(url, consumer, token)
    conn.sendmail('testing.oauth.1@gmail.com', 'testing.oauth.1@gmail.com', msg)

rawDescription: python & smtplib: Is sending mail via gmail using oauth2 possible?

description: Python & smtplib: Is sending mail via gmail using oauth2 possible


code:
tickets = [{"summary" : "a"}, {"summary" : "a"}, {"summary" : "b"}]
issue = {"title" : "a"}
setupString = "from __main__ import tickets, issue"
from timeit import timeit
print timeit("[ticket for ticket in tickets if ticket['summary'] == issue['title']][0]", setup=setupString)
print timeit('filter(lambda ticket: ticket["summary"] == issue["title"], tickets)[0]', setup=setupString)
print timeit("next(ticket for ticket in tickets if ticket['summary'] == issue['title'])", setup=setupString)

rawDescription: Find a dictionary in a list of dictionaries


code:
import numpy as np
x = np.array([3,5,7,1,9,8,6,6])
y = np.array([2,1,5,10,100,6])

index = np.argsort(x)
sorted_x = x[index]
sorted_index = np.searchsorted(sorted_x, y)

yindex = np.take(index, sorted_index, mode="clip")
mask = x[yindex] != y

result = np.ma.array(yindex, mask=mask)
print result

rawDescription: Numpy: For every element in one array, find the index in another array


code:
import urllib2
from bs4 import BeautifulSoup

# get languages and links
soup = BeautifulSoup(urllib2.urlopen('http://en.wikipedia.org/wiki/Stack_Overflow'))
links = [(el.get('lang'), el.get('href')) for el in soup.select('li.interlanguage-link > a')]

for language, link in links:
    soup = BeautifulSoup(urllib2.urlopen('http:' + link))
    print language, soup.title.text

rawDescription: python wikipedia scraping - getting the links to same page in other languages?

description: Python wikipedia scraping - getting the links to same page in other languages


code:
import re
import lxml.html

NS = 'http://exslt.org/regular-expressions'
tree = lxml.html.fromstring(DOC)

# select sibling table nodes after matching node
path = "//*[re:test(text(), 'spe.*al')]/following-sibling::table"
print tree.xpath(path, namespaces={'re': NS})

# select all sibling nodes after matching node
path = "//*[re:test(text(), 'spe.*al')]/following-sibling::*"
print tree.xpath(path, namespaces={'re': NS})

rawDescription: locate element using lxml.html vs BeautifulSoup

description: Locate element using lxml.html vs BeautifulSoup


code:
with open('new.txt' ,'r') as f :
    lines=f.read()
    print "**** start logging ****"+ lines.split("**** start logging ****")[-1]

rawDescription: Python / read a specific part in a log file


code:
process.stdin.write("4\n")
process.stdin.flush()
output = process.stdout.readline()
print output

rawDescription: making python programs "chat" via pipe

description: Making python programs "chat" via pipe


rawDescription: Sorting and re-numbering values in Python dictionary


code:
buf_offset = 0
while buf_offset < sysctl_buf_len.value:
   msg_offset = buf_offset
   ifmsg = if_msghdr2.from_buffer(sysctl_buf, msg_offset)
   buf_offset += ifmsg.ifm_msglen
   if ifmsg.ifm_type != RTM_IFINFO2 or ifmsg.ifm_flags & IFF_LOOPBACK:
      continue
   sdl_offset = msg_offset + c_sizeof(if_msghdr2)
   sdl = sockaddr_dl.from_buffer(sysctl_buf, sdl_offset)
   if sdl.sdl_family != AF_LINK:
      continue
   print sdl.sdl_data[:sdl.sdl_nlen]
   print ifmsg.ifm_data.ifi_ibytes, ifmsg.ifm_data.ifi_obytes

rawDescription: Garbage uint64 values from ctypes

description: Garbage uint64 values from ctypes


code:
class Employee(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.addresses = []

    @property
    def __dict__(self):
        return {
            "name": self.name,
            "age": self.age,
            "addresses": [addr.__dict__ for addr in self.addresses]
        }

class Address(object):
    def __init__(self, state, district, city):
        self.state = state
        self.district = district
        self.city = city

employee = Employee("Gunjan", 22)
employee.addresses.append(Address("West Bengal", "Jalpaiguri", "Kolkata"))
employee.addresses.append(Address("West Bengal", "Darjeeling", "Siliguri"))

print employee.__dict__

rawDescription: Json serialization python

description: Json serialization python


code:
s = """In Newtonian physics, free fall is any motion
of a body where its weight is the only force acting
upon it. In the context of general relativity where
gravitation is reduced to a space-time curvature,
a body in free fall has no force acting on it and
it moves along a geodesic. The present article
concerns itself with free fall in the Newtonian domain."""

import re

def say_me(word,li=re.split('\s+',s)):
    for i,w in enumerate(li):
        if w==word:
            print '\n%s at index %d followed by\n%s' % (w,i,li[i+1:])

say_me('free')

rawDescription: how to write a Python program that reads from a text file, and builds a dictionary which maps each word

description: Write a Python program that reads from a text file, and builds a dictionary which maps each word


code:
mystr = 'Hello world'
for i in range(len(mystr)+1):
    print mystr[:i]

rawDescription: Python iterate over each string?

description: Python iterate over each string


code:
list_of_lists = [a, b]
print set.union(*map(set, list_of_lists))

rawDescription: Given a bunch of ranges of numbers, get all the numbers within those ranges?


code:
import numpy as np

def f1():
    "np.array + nested lists"
    my_array = np.array([[1, 3], [2.4, -1]])

def f2():
    "np.array + nested tuples"
    my_array = np.array(((1, 3), (2.4, -1)))

def f3():
    "Completely unrolled"
    my_array = np.empty((2,2),dtype=float)
    my_array[0,0] = 1
    my_array[0,1] = 3
    my_array[1,0] = 2.4
    my_array[1,1] = -1

def f4():
    "empty + ravel + list"
    my_array = np.empty((2,2),dtype=float)
    my_array.ravel()[:] = [1,3,2.4,-1]

def f5():
    "empty + ravel + tuple"
    my_array = np.empty((2,2),dtype=float)
    my_array.ravel()[:] = (1,3,2.4,-1)

def f6():
    "empty + slice assignment"
    my_array = np.empty((2,2),dtype=float)
    my_array[0,:] = (1,3)
    my_array[1,:] = (2.4,-1)

def f7():
    "empty + index assignment"
    my_array = np.empty((2,2),dtype=float)
    my_array[0] = (1,3)
    my_array[1] = (2.4,-1)

def f8():
    "np.array + flat list + reshape"
    my_array = np.array([1, 3, 2.4, -1]).reshape((2,2))

def f9():
    "np.empty + ndarray.flat  (Pierre GM)"
    my_array = np.empty((2,2), dtype=float)
    my_array.flat = (1,3,2.4,-1)

def f10():
    "np.fromiter (Bi Roco)"
    my_array = np.fromiter((1,3,2.4,-1), dtype=float).reshape((2,2))

import timeit
results = {}
for i in range(1,11):
    func_name = 'f%d'%i
    my_import = 'from __main__ import %s'%func_name
    func_doc = globals()[func_name].__doc__
    results[func_name] = (timeit.timeit(func_name+'()',
                                        my_import,
                                        number=100000),
                          '\t'.join((func_name,func_doc)))

for result in sorted(results.values()):
    print '\t'.join(map(str,result))

rawDescription: Building a small numpy array from individual values: Fast and readable method?


code:
from collections import Counter

a = [{'val': 10, 'count': 1}]
b = [{'val': 10, 'count': 4}, {'val': 20, 'count': 2}]
c = Counter()

[c.update({d['val']:d['count']}) for d in a + b]
print [{'val': k, 'count': v} for k, v in c.iteritems()]

rawDescription: combining two lists of dicts to one list of dicts by specific key

description: Combining two lists of dicts to one list of dicts by specific key


code:
def cleave_by_change(stream):
    def generator():
        head = stream[0]
        while stream and stream[0] == head:
            yield stream.pop(0)
    while stream:
        yield generator()

for g in cleave_by_change([1,1,1,2,2,3,2,2,2,2]):
    print list(g)

rawDescription: Can yield produce multiple consecutive generators?


code:
import util
hash = '62e907b15cbf27d5425399ebf6f0fb50ebb88f18'
print util.hash_to_address("00".decode('hex'), hash.decode('hex'))

rawDescription: How to get Bitcoin address from pubkey_hash?

description: Get Bitcoin address from pubkey_hash


code:
temp = TemplateX("$a$a")
newstring = temp.substitute(a='a')
print temp.getvalues(newstring)

rawDescription: In python extending the string.Template class for easy regular expressions


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

def main():
    fig, ax = plt.subplots()
    cs = ax.contour(np.random.random((10,10)))

    callback = ContourCallback(cs)
    plt.setp(cs.collections, picker=5)
    fig.canvas.mpl_connect('pick_event', callback)

    plt.show()

class ContourCallback(object):
    def __init__(self, cs):
        self.lookup = dict(zip(cs.collections, cs.levels))

    def __call__(self, event):
        print self.lookup[event.artist]

main()

rawDescription: matplotlib - extracting values from contour lines

description: Matplotlib - extracting values from contour lines


code:
norooms = int(input('How many rooms does the floor have?'))
length = dict()
width = dict()
for i in range(norooms):
    length[i] = input('what is the length of room?')
    width[i] =input('what is the width of room?')
    print('that was room number:',i+1)
total=0
for i in range(norooms):
    total += float(length[i])*float(width[i])

print 'total is:' ,total

rawDescription: calculating total floor size (Python)

description: Calculating total floor size (Python)


code:
def _go(self, _):
    dialog = Gtk.MessageDialog(Gtk.Window(),
               Gtk.DialogFlags.MODAL,
               Gtk.MessageType.QUESTION,
               Gtk.ButtonsType.YES_NO,
               "RESPONSE REQUIRED")
    dialog.format_secondary_text("are you having fun?")
    nested_dialog = NestedDialog(dialog)
    response = nested_dialog.run()
    print "your response is: " + str(response)
    time.sleep(4)
    print "left _go"

rawDescription: gtk MessageDialog not closing until enclosing method finishes

description: Gtk MessageDialog not closing until enclosing method finishes


code:
dict_results = dict((d['name'], d) for d in dict1[results])
if name in dict_results:
    print dict_results[name]['gpa']

rawDescription: How to check if list item is in dictionary. If so, print the data associated with it

description: Check if list item is in dictionary. If so, print the data associated with it


code:
class MyTester(object):
    def __init__(self, request, arg=["var0", "var1"]):
        self.request = request
        self.arg = arg
        # self.use_arg_to_init_logging_part()

    def dothis(self):
        print "this"

    def dothat(self):
        print "that"

rawDescription: py.test: Pass a parameter to a fixture function

description: Py.test: Pass a parameter to a fixture function


code:
import threading
import Queue
from time import sleep

dataQue = Queue.Queue()
stopQue = Queue.Queue()

def DAQThread(ue9ipAddr):
    print 'Executing in DAQThread\n'
    count = 5
    while count > 0:
        dataQue.put('data: %s' % count)
        count -= 1
        sleep(5)
    stopQue.put(1)
    print 'Exiting DAQThread\n'

def listenThread(counter):
    while True:
        if dataQue.empty() != True:
            outcome = dataQue.get()
            print outcome
            counter -=1
        if stopQue.empty() != True:
            break
    print 'Exiting Listening Thread'

print "Opening device..."
ipAdd = '192.168.1.127'
feedbackThread = threading.Thread(target=DAQThread, args=(ipAdd,))
feedbackThread.setDaemon(True)
feedbackThread.start()

print "Listening for data..."
queryThread = threading.Thread(target=listenThread, args = (10,))
queryThread.setDaemon(True)
queryThread.start()

print "Done"

rawDescription: Correct usage of multithreading and Queue module in data collection application written in Python


code:
# foo_level.py
def do_stuff():
    print "foo"

rawDescription: Import same python module more than once


code:
list(indexed_date_groups(original))
original = [None, datetime(2013, 6, 4), datetime(2014, 5, 12), None, None, datetime(2012, 5, 18), None]
for index, group in indexed_date_groups(original):
    print index, group

rawDescription: Split list on None and record index

description: Split list on None and record index


code:
last = None
for row in csvin:
    data = float(row[1])
    if last is not None:
        print "difference -> %f" % (data - last)
    last = data

rawDescription: How to operate with values in the same column in .csv files

description: Operate with values in the same column in .csv files


code:
mystr = u'It doesn\u2019t look lake a controversial new case management system is going anywhere. So\xa0the city plans to spend the next few months helping local social assistance workers learn to live with it.'
print mystr

rawDescription: python convert unicode into it's "print" form

description: Python convert unicode into it's "print" form


rawDescription: Pandas Python: Merging every two rows in one dataframe


code:
import sys
from SimpleHTTPServer import SimpleHTTPRequestHandler
import BaseHTTPServer

def test(HandlerClass=SimpleHTTPRequestHandler,
         ServerClass=BaseHTTPServer.HTTPServer):

    protocol = "HTTP/1.0"
    host = ''
    port = 8000
    if len(sys.argv) > 1:
        arg = sys.argv[1]
        if ':' in arg:
            host, port = arg.split(':')
            port = int(port)
        else:
            try:
                port = int(sys.argv[1])
            except:
                host = sys.argv[1]

    server_address = (host, port)

    HandlerClass.protocol_version = protocol
    httpd = ServerClass(server_address, HandlerClass)

    sa = httpd.socket.getsockname()
    print "Serving HTTP on", sa[0], "port", sa[1], "..."
    httpd.serve_forever()

if __name__ == "__main__":
    test()

rawDescription: Is it possible to run python SimpleHTTPServer on localhost only?


code:
import os
import argparse

def intOrUnderscore(s):
    if s != '_':
        return int(s)
    cases = (n for n in os.listdir(".") if n.startswith("file."))
    return max(int(c[c.rindex(".")+1:]) for c in cases)

parser = argparse.ArgumentParser()
parser.add_argument('case', type=intOrUnderscore)

args = parser.parse_args()
print args.case

rawDescription: Specifying types and patterns using argparse choices


code:
import re
haystack = 'abababa baba alibababa'
needle = 'baba'
matches = re.finditer(r'(?=(%s))' % re.escape(needle), haystack)
print [m.start(1) for m in matches]

rawDescription: How can I find the number of overlapping sequences in a String with Python?

description: Find the number of overlapping sequences in a String with Python


code:
import time
start_time = time.time()

def s(n):
    tsum = 1
    for p in xrange(2,n):
        d = 1
        # divide out all factors p of n
        while n % p == 0:
            n = n//p
            d = p*d + 1
        tsum *= d
        if p*p > n: # n = 1, or n is prime
            break
    if n > 1:       # one last prime factor to account for
        tsum *= 1 + n
    return tsum
def divisorSums(n):
    dsums = [0] + [1]*n
    for k in xrange(2, n+1):
        for m in xrange(k, n+1, k):
            dsums[m] += k
    return dsums

MAXM = 6
MAXNUM = 400000

dsums = divisorSums(MAXNUM)
def memo_s(n):
    if n <= MAXNUM:
        return dsums[n]
    return s(n)

i = 2

perc = 0
perc1 = 0
perf = 0
multperf = 0
supperf = 0
while i <= MAXNUM:
    pert = perc1
    num = i
    for m in xrange(1, MAXM + 1):
        tsum = memo_s(num)
        if tsum % i == 0:
            perc1 += 1
            k = tsum / i
            mes = "%d is a (%d-%d)-perfect number" % (i, m, k)
            if m == 1:
                multperf += 1
                if k == 2:
                    perf += 1
                    print mes + ", that is a perfect number"
                else:
                    print mes + ", that is a multiperfect number"
            elif m == 2 and k == 2:
                supperf += 1
                print mes + ", that is a superperfect number"
            else:
                print mes
        num = tsum
    i += 1
    if pert != perc1: perc += 1
print "Found %d distinct (m-k)-perfect numbers (%.5f per cent of %d ) in %d occurrences" % (
perc, float(perc) / MAXNUM * 100, MAXNUM, perc1)
print "Found %d perfect numbers" % perf
print "Found %d multiperfect numbers (including perfect numbers)" % multperf
print "Found %d superperfect numbers" % supperf
print time.time() - start_time, "seconds"

rawDescription: How would I go about improving/making this run faster?


code:
from itertools import groupby

def canonicalize_dict(x):
    "Return a (key, value) list sorted by the hash of the key"
    return sorted(x.items(), key=lambda x: hash(x[0]))

def unique_and_count(lst):
    "Return a list of unique dicts with a 'count' key added"
    grouper = groupby(sorted(map(canonicalize_dict, lst)))
    return [dict(k + [("count", len(list(g)))]) for k, g in grouper]

a = [{'text': 'hallo world', 'num': 1},
     #....
     {'text': 'hallo world', 'num': 1}]

print unique_and_count(a)

rawDescription: python 2.6-removing and counting duplicates in a list of dictionaries effeciently

description: Python 2.6-removing and counting duplicates in a list of dictionaries effeciently


code:
import pytest
import grequests

@pytest.fixture
def urls():
    return [
        "https://www.google.com",
        "https://www.facebook.com/",
        "http://www.facebook.com/",
        "http://www.google.com",
        "http://fakedomain/",
        "http://python-tablib.org"
    ]

# This function will execute set of instructions when responses come:
def proc_response(response, **kwargs):
    # do something ..
    print "========Processing response=============", response.request.url
    print response
    if response.status_code != 200:
        print response.request.url
        print response.content

# Request exception handler:
def my_except_handler(request, exception):
    print "Request failed : " + request.url
    print request.response

def test_it(urls):
    # Here is the list of tasks we build and run in parallel later:
    actions_list = []

    # Tasks list building:
    for url in urls:
        action_item = grequests.get(url, hooks={'response': proc_response})
        actions_list.append(action_item)

    # Run grequests:
    print grequests.map(actions_list, exception_handler=my_except_handler)

rawDescription: Python: grequest and request give different response


code:
def check_for_duplicates(filename):
    with open(filename, 'r') as fil:
        lines = fil.readlines()
    lines = [line.strip().split() for line in lines]

    for row in lines:
        if len(set(row)) != len(row):
            print 'Duplicates in row: ', row

    for col in zip(*lines):
        if len(set(col)) != len(col):
            print 'Duplicates in column: ', col

rawDescription: Finding duplicates in each row and column


code:
import subprocess

print "Starting process..."
process = subprocess.Popen(["echo", "a"], shell=False)
process.wait()
print "Done"

rawDescription: Calling external program through Python and passing the output to stdout


code:
print 'hello\n'
print 'hi'

rawDescription: More elegant way to produce a space python


code:
class SomeSuperclass(object):
  def some_method(self, *args, **kwargs):
    print 'SomeSuperclass: args=%s kwargs=%s' % (args, kwargs)

class SomeClass(SomeSuperclass):
  def some_method(self, *args, **kwargs):
    my_kwarg = kwargs.pop('my_kwarg', 42) # 42 is the default
    print 'SomeClass: my_kwarg=%s' % my_kwarg
    super(SomeClass, self).some_method(*args, **kwargs)

SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5, my_kwarg=8)
SomeClass().some_method('arg 0', 'arg 1', some_kwargs=5)

rawDescription: Python: What is an elegant idiom for extending method with one or more keyword arguments?


code:
import threading
import time

class MyThread (threading.Thread):  # note capitalization change
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.q = q
        self.__exitFlag = False
        self.__signal_lock = threading.Lock()
    def run(self):
        print "Starting " + self.name
        self.process_data()
        print "Exiting " + self.name
    def stop(self):
        with self.__signal_lock:
            self.__exitFlag = True
    def process_data(self):
        while not self.__exitFlag:
            if not self.q.empty():
                data = self.q.get()
                print "%s processing %s" % (self.name, data)
                time.sleep(3)
                print "%s finished processing %s" % (self.name, data)
            time.sleep(1)

rawDescription: python - global variables

description: Python - global variables


code:
for row in result:
    print row[0]

rawDescription: Result of type long from a SqlProxyRow result in string


code:
# saved as greeting.py
import Pyro4

class GreetingMaker(object):
    def get_fortune(self, name):
        return "Hello, {0}. Here is your fortune message:\n" \
               "Tomorrow's lucky number is 12345678.".format(name)

greeting_maker=GreetingMaker()

daemon=Pyro4.Daemon()                 # make a Pyro daemon
ns=Pyro4.locateNS()                   # find the name server
uri=daemon.register(greeting_maker)   # register the greeting object as a Pyro object
ns.register("example.greeting", uri)  # register the object with a name in the name server

print "Ready."
daemon.requestLoop()                  # start the event loop of the server to wait for calls

rawDescription: how to connect to local machine using remote client machine

description: Connect to local machine using remote client machine


code:
from bson.tz_util import utc
from pymongo import ASCENDING
cursor = db.collection.find({'$or': [
    {'col1': 1, 'col2': 'foo'},
    {'col3': {'$gt': datetime(2012, 01, 01, tzinfo=utc)}},
    {'col3': {'$lt': datetime(2012, 01, 02, tzinfo=utc)}},
]}, fields=['col1', 'col2', 'col4', 'col5'])
cursor.sort([('col4', ASCENDING), ('col5', ASCENDING)])

rawDescription: Build mongoDB queries based on JSON from a user using Python


code:
class Ship(object):

    def __init__(self, name="Enterprise", fuel=0):
        self.name = name
        self._fuel = fuel
        print "The spaceship", name, "has arrived!"

    def status(self):
        print "Name: ", self.name
        print "Fuel level: ", self.fuel

    @property
    def fuel(self):
        return self._fuel

    @fuel.setter
    def fuel(self,level):
        if level < 0:
            self._fuel = 0
        else:
            self._fuel = level

rawDescription: Spaceship objects

description: Spaceship objects


code:
import enchant
b = enchant.Broker()
b.set_ordering("en_US","aspell,myspell")
print b.describe()
d=b.request_dict("en_US")
print d.provider

s = 'alllow'
d.check(s)
print d.suggest(s)
d.store_replacement(s, 'alloy')
print d.suggest(s)

rawDescription: Pyenchant store_replacement doesn't work?


rawDescription: Add an object to a python list

description: Add an object to a python list


code:
print list(reversed(a[::2]))
print list(reversed(b[::2]))

rawDescription: every other element in a list then reverse

description: Every other element in a list then reverse


code:
from django.db.models import Q, Avg
from itertools import groupby
from myapp.models import Profile, Ratings

def iterator_filtered_by_average(dictionary):
    qr = Ratings.objects.values('profile', 'category', 'points').order_by(
            'profile', 'category').annotate(points_avg=Avg('points'))
    f = Q()
    for k, v in dictionary.iteritems():
        f |= Q(category=k, points_avg__gte=v)
    for profile, grp in groupby(qr.filter(f).values('profile')):
        if len(list(grp)) == len(dictionary):
            yield profile

#example
FILTER_DATA = {1:category_1_avg_val, 2:category_2_avg_val, 3:category_3_avg_val,
               4:category_4_avg_val, 5:category_5_avg_val}
for row in iterator_filtered_by_average(FILTER_DATA):
    print row

rawDescription: Django - Complex query

description: Django - Complex query


code:
for term in read_valid_terms("terms.txt"):
    print term,
print

rawDescription: Pythonic String Testing

description: Pythonic String Testing


code:
for photo in favs.find('photos').findall('photo'):
    print photo.get('id')

rawDescription: Retrieving flickr favorites

description: Retrieving flickr favorites


code:
for thing in p:
  print thing

rawDescription: R nested for loop in Python?

description: R nested for loop in Python


code:
x = 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000'
a = x # for having more successive variables
b = a.decode('hex')
print repr(b)
# -> '\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00'

rawDescription: How can I convert a 256-bit big endian integer to little endian in Python?

description: Convert a 256-bit big endian integer to little endian in Python


code:
from collections import defaultdict

colors = ['Red', 'Yellow', 'Green', 'Blue']
mapColors = defaultdict(str)

def okToColor(Map, country, color):
    for c in Map[country]:
        if mapColors[c] == color:
            return False
    return True

def explore(Map, country, color):
    if cindex[country] >= len(Map):
        return True
    if okToColor(Map, country, color):
        mapColors[country] = color
        for color in colors:
            try:
                next_country = countries[cindex[country] + 1]
            except IndexError:
                return True
            if explore(Map, next_country, color):
                return True
    return False

def printMap():
    for c in mapColors:
        print c, mapColors[c]

countries = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
cindex = dict(zip(countries, range(len(countries))))
Map = [[1, 4, 2, 5], [0, 4, 6, 5], [0, 4, 3, 6, 5], [2, 4, 6],
      [0, 1, 6, 3, 2], [2, 6, 1, 0], [2, 3, 4, 1, 5]]
Map = {countries[idx]: [countries[n] for n in item] for idx,
       item in enumerate(Map)}
result = explore(Map, 'A', 'Red')
print(result)
printMap()

rawDescription: A better way to write this program

description: A better way to write this program


code:
def foo(msg):
    s = str(msg)
    print s, type(s)

rawDescription: Monkey Patching in python: When we need it?


code:
def foo(arg1='ham', arg2='spam'):
    print arg1, arg2
arguments = dict(arg2='foo', arg1='bar')
foo(**arguments)

rawDescription: what is the asterisk in "replace(*something)" for? (python)

description: What is the asterisk in "replace(*something)" for? (python)


rawDescription: How to split a line of txt file into 3 values per line

description: Split a line of txt file into 3 values per line


code:
z, pts = zip(*[(z, pt) for z, pt in zip(z, pts) if z <= 3])
print z, pts

rawDescription: efficient algorithm for list edits

description: Efficient algorithm for list edits


code:
from BeautifulSoup import BeautifulSoup
soup = BeautifulSoup(open('myNotes.xml', 'r'))

source = soup.source
print source

#<source>
# mobile.iphone
#</source>

source = soup.source
print source.string

# mobile.iphone

rawDescription: Removing a tag and its contents in xml using BeautifulSoup an lxml in Python


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

import argparse

parser = argparse.ArgumentParser(description='Computes a moving standard deviation for a data file')
parser.add_argument('--number', '-n', help='Window size (number of points to either side)', type=int)
parser.add_argument('dataFile', help='Name of data file', type=str)

args = parser.parse_args()
n = args.number

# I hope you have numpy, it makes things much easier
import numpy as np

# assuming two-column time/datum format
data = np.loadtxt(args.dataFile)

for ii in range(n,len(data)-n):
    print data[ii,0], np.std(data[ii-n:ii+n+1,1])

rawDescription: Moving Standard Deviation Gnuplot

description: Moving Standard Deviation Gnuplot


code:
a = [['jimmy', '25', 'pancakes'], ['tom', '23', 'brownies'], ['harry', '21', 'cookies']]
for i in a:
    if (i[1] == '25' or i[1] == '26'):
        print 'yes'
else:
    print 'Not found'

rawDescription: Python if a loop produces same output string for each iteration, how to print only once?


code:
import re
r = re.compile(r'(?<=PDFObject).*?url:.*?(http.*?)"', re.DOTALL)
s = '''var myPDF = new PDFObject({
url: "http://www.site.com/doc55.pdf",
  id: "pdfObjectContainer",
  width: "100%",
  height: "700px",
  pdfOpenParams: {
    navpanes: 0,
    statusbar: 1,
    toolbar: 1,
    view: "FitH"
  }
}).embed("pdf_placeholder"); '''

print r.findall(s)

rawDescription: Retrieving a string using REGEX in Python 2.7.2


code: print ws.columns[col][row].value

rawDescription: read two columns in Excel using python

description: Read two columns in Excel using python


code:
pattern = re.compile(r'(A*B)*')
for s in "AABAABB", "ABCAAB", "B", "BBB", "AABAABBx", "xAABAABB":
    m = pattern.match(s)
    if m is not None and m.group() == s:
        print "Matched:", s

rawDescription: Check if a string is a complete match for a regex expression in python


code:
filename = "simple%d%d.csv" % (minLength,minBF)
print filename

rawDescription: Include changing variables into name of output file in Python


code: print out

rawDescription: getting output of command in subprocess

description: Getting output of command in subprocess


code:
for message in mbox:
    print parseaddr(message['from'])

rawDescription: Determine unique "from" email addresses in Maildir folder


code:
cursewords = ['javascript', 'php', 'windows']
if any(curseword in input for curseword in cursewords):
    print 'onoes'

rawDescription: How to find if part of a list is in str

description: Find if part of a list is in str


code:
import config
class test2():
    def __init__(self):
        configObj = config.Config()
        print configObj.color

rawDescription: How to pass global variables between files in python

description: Pass global variables between files in python


code:
#!/usr/bin/env python

name = raw_input("What is your name?\n")
print "Hello {}, my name is Bot1".format(name)
raw_input() # Bot2: Hello Bot1

rawDescription: Python: Communicate with always running subprocesses


code:
class Onselect():

    def __init__(self):
        self.coords = {}

    def __call__(self, xmin, xmax):
        indmin, indmax = np.searchsorted(x, (xmin, xmax))
        indmax = min(len(x)-1, indmax)

        thisx = x[indmin:indmax]
        thisy = y[indmin:indmax]
        self.coords['x'] = thisx
        self.coords['y'] = thisy
        line2.set_data(thisx, thisy)
        ax2.set_xlim(thisx[0], thisx[-1])
        ax2.set_ylim(thisy.min(), thisy.max())
        fig.canvas.draw()

onselect = Onselect()

# set useblit True on gtkagg for enhanced performance
span = SpanSelector(ax, onselect, 'horizontal', useblit=True,
                    rectprops=dict(alpha=0.5, facecolor='red') )

plt.show()

print 'working with x an y'
print onselect.coords['x'][:10]
print onselect.coords['y'][:10]

rawDescription: Return a portion of a plot using matplotlib


code:
prog_Xtime = dict(zip(program_X,program_time))
for pos_X in prog_Xtime:
    if pos_X == '375':
       if prog_Xtime[pos_X] >= '30':
          print "hello 1"

rawDescription: code keep firing like 7 times when using the for loops

description: Code keep firing like 7 times when using the for loops


code:
import pyodbc

DBfile = 'C:/Users/stage1/Documents/test.accdb'
conn = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb,    *.accdb)};DBQ=C:/Users/stage1/Documents/test.accdb;")
cursor = conn.cursor()

source_table = 'naam'
dest_table = 'testtable'
column_name = 'naam'

if (cursor.columns(table=source_table, column=column_name).fetchone() and
    cursor.columns(table=dest_table, column=column_name).fetchone()):
    result = cursor.execute("SELECT naam FROM naam")
    for row in result:
        k = row.naam
        print 'Got k: {!r}'.format(k)
        cursor.execute("insert into testtable (naam) values (?)", (k,))
    conn.commit()
else:
    # tables and/or columns missing
    show_tables = show_source_columns = show_dest_columns = False

    if not cursor.tables(table=source_table).fetchone():
        print 'No source table named "{}"'.format(source_table)
        show_tables = True
    elif not cursor.columns(table=source_table, column=column_name).fetchone():
        show_source_columns = True

    if not cursor.tables(table=dest_table).fetchone():
        print 'No destination table named "{}"'.format(source_table)
        show_tables = True
    elif not cursor.columns(table=dest_table, column=column_name).fetchone():
        show_dest_columns = True

    if show_tables:
        print '\n\nAvailable tables are:'
        for row in cursor.tables():
            print row

    if show_source_columns:
        print '\n\nSource table "{}" missing column "{}". Available columns:'.format(source_table, column_name)
        for row in cursor.columns(table=source_table):
            print '{} : {}'.format(row.column_name, row.data_type)

    if show_dest_columns:
        print '\n\nDestination "{}" table missing column "{}". Available columns:'.format(dest_table, column_name)
        for row in cursor.columns(table=dest_table):
            print '{} : {}'.format(row.column_name, row.data_type)

cursor.close()
conn.close()

rawDescription: How to import access table to another access table using Python

description: Import access table to another access table using Python


code:
import re

reg = re.compile( "[A-Z ]{6,}[A-Z ]{6,}")
string = "...˜ÃÕ=òaãNÜ ß§#üxwáã MESSAGE HIDDEN IN HERE ŸÎ=N‰çÈ^XvU…”vN˜..."

print reg.findall(string)

rawDescription: Python Regex Alphabet and spaces

description: Python Regex Alphabet and spaces


code:
import rethinkdb as r
conn = r.connect()
cursor = r.table('30715780').filter(r.row['valid_to'] < r.now()).count().run(conn)
print list(cursor)

rawDescription: REQL Date and time queries

description: REQL Date and time queries


code:
from pyparsing import *
TAB = White('\t')

# each value consists of printable words separated by at most a
# single space (a space that is not followed by another space)
value = Combine(OneOrMore(~TAB + (Word(printables) | White(' ',max=1) + ~White())))

# each line has one or more of these values
linedefn = OneOrMore(value)
# do not expand tabs before parsing
linedefn.parseWithTabs()

data = "Library\tSSHClient    with name\tnode"

# replace tabs with 2 spaces
#data = data.replace('\t', '  ')

print linedefn.parseString(data)

linedefn = OneOrMore(locatedExpr(value))('values')
# do not expand tabs before parsing
linedefn.parseWithTabs()
print linedefn.parseString(data).dump()

rawDescription: pyparsing whitespace match issues

description: Pyparsing whitespace match issues


code:
a = "\t\tindented string"
leading_spaces = len(a) - len(a.lstrip())
print leading_spaces

rawDescription: Python check string indentation

description: Python check string indentation


code: print a.decode('utf-8')

rawDescription: How to covert this binary string to a normal string?

description: Covert this binary string to a normal string


code:
for line in grouper(3, my_list):
    print ', '.join(filter(None, line))

rawDescription: Outputting list to a "matrix" type format in python


code:
from my_cython import function
print function(10)

rawDescription: How to use function written in C in Cython

description: Use function written in C in Cython


code:
import pandas as pd

mydict = {'ID':[1,2,2,3,4,5,5,6,7],
             'Name':['A','B','C','D','E','F','G','H','I'],
             'Rate':[65.5,67.3,78.8,65,45.3,52,66,34,2]}

df=pd.DataFrame(mydict)
print df

df =  pd.concat([df]*10000).reset_index(drop=True)

df1 = df.copy()
df2 = df.copy()

def jez(df):
    def f(x):
        #print x
        x['Secondname'] = x['Name'].shift(1).combine_first(x['Name'].shift(-1))
        return x

    return df.groupby('ID').apply(f)

def jez2(df):
    #print df.duplicated('ID', keep='first')
    #print df.duplicated('ID', keep='last')
    df.loc[ df.duplicated('ID', keep='first'), 'first'] = df['Name']
    df.loc[ df.duplicated('ID', keep='last'), 'last'] = df['Name']
    #print df

    df['SecondName'] = df['first'].shift(-1).combine_first(df['last'].shift(1))
    df = df.drop(['first', 'last'], axis=1)
    return df

def rom(df):

    # cpIDs = True if the next row has the same ID
    df['cpIDs'] = df['ID'][:-1] == df['ID'][1:]
    # fill in the last row (get rid of NaN)
    df.iloc[-1,df.columns.get_loc('cpIDs')] = False
    # ShiftName == Name of the next row
    df['ShiftName'] = df['Name'].shift(-1)
    # fill in SecondName
    df.loc[df['cpIDs'], 'SecondName'] = df.loc[df['cpIDs'], 'ShiftName']
    # remove columns
    del df['cpIDs']
    del df['ShiftName']
    return df

print jez(df1)
print jez2(df2)
print rom(df)

rawDescription: Modify pandas dataframe in python based on multiple rows


code:
cat1 = {"james":{1,2,3}, "bob":{3,4,5}}
for x in cat1['james']:
    print x

rawDescription: How to add multiple values per key in python dictionary

description: Add multiple values per key in python dictionary


code:
a = ["Wednesday-Morning-Go bowling", "Sunday-Really late at night-Sleep",
     "July-Noon-BBQ"]
a.sort(key=lambda x: x.split("-", 2)[-1])
print a

rawDescription: In python, how would I sort a list of strings where the location of the string comparison changes?


rawDescription: serching for nested item in nested list [python]

description: Serching for nested item in nested list [python]


code: print dir(request.user)

rawDescription: In Django, how can I view all the properties of my user?


rawDescription: How to sub with matched groups and variables in Python

description: Sub with matched groups and variables in Python


code: print findItByColor("green")

rawDescription: adding arrays inside dictionaries, inside more arrays?

description: Adding arrays inside dictionaries, inside more arrays


code:
# new version
from lxml import etree
import lxml.html

collected = [] #list-tuple of [(col1, col2...), (col1, col2...)]
dom = lxml.html.parse("http://gbgfotboll.se/serier/?scr=table&ftid=57108")
#all table rows
xpatheval = etree.XPathDocumentEvaluator(dom)
rows = xpatheval("""//div[@id="content-primary"]/table[
    contains(concat(" ", @class, " "), " clTblStandings ")]/tbody/tr""")

for row in rows:
    collumns = row.findall("td")
    collected.append((
        collumns[0].find("a").text.encode("utf8"), # Lag
        collumns[1].text, # S
        collumns[5].text, # GM-IM
        collumns[7].text, # P - last collumn
    ))

for i in collected: print i

rawDescription: Extracting information from a table on a website using python, LXML & XPATH


rawDescription: Loop through list of tuples, call elements independently


code:
def has_roles(user, roles):
    return user.profile.user_types.filter(pk__in=roles).count() == len(roles)

print has_roles(user, [6,7,8])

rawDescription: Check for multiple values in a M2M relationship in Django


code:
x = raw_input("Please insert some text...\n")
print x

rawDescription: Why isn't my while loop, looping?

description: Why isn't my while loop, looping


rawDescription: Pythonic way of iterating over 3D array

description: Pythonic way of iterating over 3D array


code:
import pandas as pd
import numpy as np

df = pd.DataFrame([[1,  {'name':'Jack','email':'abc'} ],
                     [2, np.nan],
                     [3,  {'name':'Ram','email':'xyz'}  ],
                     ], columns=['_id','page_view_count'])

print df[df['page_view_count'].isnull()].index
#Int64Index([1], dtype='int64')

print pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count')
#1    {}
#Name: page_view_count, dtype: object

df['page_view_count'] = df['page_view_count'].fillna(pd.Series([{}], index=df[df['page_view_count'].isnull()].index , name='page_view_count'))
print df
#   _id                       page_view_count
#0    1  {u'name': u'Jack', u'email': u'abc'}
#1    2                                    {}
#2    3   {u'name': u'Ram', u'email': u'xyz'}

df1 = pd.DataFrame([x for x in df['page_view_count']]).join(df['_id'], how='right')
df1 = df1.sort_index(1)
print df1
#   _id email  name
#0    1   abc  Jack
#1    2   NaN   NaN
#2    3   xyz   Ram

rawDescription: How to convert a particular dtype object column's field into column of data frame in pandas

description: Convert a particular dtype object column's field into column of data frame in pandas


code:
for sent in sentences:
  print sent, sum(sent.count(col) for col in color)

rawDescription: while loop counter comparing two lists

description: While loop counter comparing two lists


code:
f = open(PATH)
for line in f.readlines():
    print line[:-1]

while True:
    time.sleep(5)
    try:
        line_start = f.tell()
        new_lines = f.read()
        last_n = new_lines.rfind('\n')
        if last_n >= 0:
            # We got at least one full line
            line_start += last_n + 1
            print new_lines[:last_n]
        else:
            # No complete line yet
            print 'no line'
        f.seek(line_start)
    except KeyboardInterrupt:
        notifier.stop()
        break

rawDescription: Read new line with pynotify

description: Read new line with pynotify


code:
import json
from collections import OrderedDict
data = json.loads('{"foo":1, "bar": 2}', object_pairs_hook=OrderedDict)
print json.dumps(data, indent=4)

rawDescription: Can I get JSON to load into an OrderedDict in Python?

description: Get JSON to load into an OrderedDict in Python


code:
u = unique(label)
new_coord = empty((len(u), 4))

i = 0
for j in u:
    rects = compress(label == j, coord, 0)

    mn = rects.min(0)
    mx = rects.max(0)

    new_coord[i] = (mn[0], mx[1], mx[2], mn[3])

    i += 1

print new_coord

rawDescription: Modify an array by merging some rows

description: Modify an array by merging some rows


code:
def many_funcs(x):
    def many_funcs_y(y):
        def many_funcs_z(z):
            def many_funcs_q(q):
                return x + y * z ** q
            return many_funcs_q
        return many_funcs_z
    return many_funcs_y

print many_funcs(1)(2)(3)(4)  # prints 163

rawDescription: Function Calling With 3 or More Argument Input Fields - function()()()


code:
import os
import shutil

src_dir = "/some/path/"
target_dir = "/some/other/path/"
searchstring = "h10v03"

for f in os.listdir(src_dir):
   if searchstring in f and os.path.isfile(os.path.join(src_dir, f)):
      shutil.copy2(os.path.join(src_dir, f), target_dir)
      print "COPY", f

rawDescription: Search files in folder using part of the name and save/copy to different folder using Python


code:
myString = "These are the links http://www.google.com  and http://stackoverflow.com/questions/839994/extracting-a-url-in-python"
print re.findall(r'(https?://[^\s]+)', myString)

rawDescription: How do you extract a url from a string using python?

description: Extract a url from a string using python


code:
def censored(sentence, *args):
  for word in args:
    if word in sentence:
        sentence = sentence.replace(word, "*" * len(word))
  return sentence
print censored("foo off", "foo", "bar")

rawDescription: How to make an argument in python accept multiple inputs

description: Make an argument in python accept multiple inputs


code:
from collections import OrderedDict
d = OrderedDict()
d["A"] = "10"
d["B"] = "100"
d["C"] = "20"
d["D"] = "12.5"
for k in d:
    print k

rawDescription: Iterating dictionary in the order I added items, python


code:
import re
wordlist = ['hypothesis' , 'test' , 'results' , 'total']
sentence = "The total results for the test confirm the hypothesis"
for word in wordlist:
    if re.search(r'\b' + word + r'\b', sentence):
            print word

rawDescription: Formatting regular expressions in Python


code:
for i in range(min(len(a), len(b))):
    v1, v2 = text_to_vector(a[i]), text_to_vector(b[i])
    cosine = get_cosine(v1, v2)
    print 'Cosine:', cosine

rawDescription: Extracting each line from two separate lists to work out cosine similarity


rawDescription: Using multiple lists with formulas

description: Using multiple lists with formulas


code: print theCPPlibrary.CONST1

rawDescription: Swig-generated Constants in Python

description: Swig-generated Constants in Python


code:
df = DataFrame({'a': rand(10) > 0.5, 'b': randn(10)}, columns=list('ab'))
df2 = DataFrame({'a': Series([nan], dtype=object), 'b': [1.0]}, columns=df.columns, index=[len(df)])
df3 = df.append(df2)
print df3
print
print df3.dtypes

rawDescription: Preserving the distinctions between bools and floats when adding NaN to a pandas Series?


code:
import numpy as np
import math

# Define points in great circle 1
p1_lat1 = 32.498520
p1_long1 = -106.816846
p1_lat2 = 38.199999
p1_long2 = -102.371389

# Define points in great circle 2
p2_lat1 = 34.086771
p2_long1 = -107.313379
p2_lat2 = 34.910553
p2_long2 = -98.711786

# Convert points in great circle 1, degrees to radians
p1_lat1_rad = ((math.pi * p1_lat1) / 180.0)
p1_long1_rad = ((math.pi * p1_long1) / 180.0)
p1_lat2_rad = ((math.pi * p1_lat2) / 180.0)
p1_long2_rad = ((math.pi * p1_long2) / 180.0)

# Convert points in great circle 2, degrees to radians
p2_lat1_rad = ((math.pi * p2_lat1) / 180.0)
p2_long1_rad = ((math.pi * p2_long1) / 180.0)
p2_lat2_rad = ((math.pi * p2_lat2) / 180.0)
p2_long2_rad = ((math.pi * p2_long2) / 180.0)

# Put in polar coordinates
x1 = math.cos(p1_lat1_rad) * math.cos(p1_long1_rad)
y1 = math.cos(p1_lat1_rad) * math.sin(p1_long1_rad)
z1 = math.sin(p1_lat1_rad)
x2 = math.cos(p1_lat2_rad) * math.cos(p1_long2_rad)
y2 = math.cos(p1_lat2_rad) * math.sin(p1_long2_rad)
z2 = math.sin(p1_lat2_rad)
cx1 = math.cos(p2_lat1_rad) * math.cos(p2_long1_rad)
cy1 = math.cos(p2_lat1_rad) * math.sin(p2_long1_rad)
cz1 = math.sin(p2_lat1_rad)
cx2 = math.cos(p2_lat2_rad) * math.cos(p2_long2_rad)
cy2 = math.cos(p2_lat2_rad) * math.sin(p2_long2_rad)
cz2 = math.sin(p2_lat2_rad)

# Get normal to planes containing great circles
# np.cross product of vector to each point from the origin
N1 = np.cross([x1, y1, z1], [x2, y2, z2])
N2 = np.cross([cx1, cy1, cz1], [cx2, cy2, cz2])

# Find line of intersection between two planes
L = np.cross(N1, N2)

# Find two intersection points
X1 = L / np.sqrt(L[0]**2 + L[1]**2 + L[2]**2)
X2 = -X1
i_lat1 = math.asin(X1[2]) * 180./np.pi
i_long1 = math.atan2(X1[1], X1[0]) * 180./np.pi
i_lat2 = math.asin(X2[2]) * 180./np.pi
i_long2 = math.atan2(X2[1], X2[0]) * 180./np.pi

# Print results
print i_lat1, i_long1, i_lat2, i_long2

rawDescription: python - intersection point of two great circles (lat/long)

description: Python - intersection point of two great circles (lat/long)


rawDescription: process multi-line data as a single record

description: Process multi-line data as a single record


code:
import traceback
def reader(args):
    try:
        # Insert stuff to be multiprocessed here
        return args[0]['that']
    except:
        print "FATAL: reader({0}) exited while multiprocessing".format(args)
        traceback.print_exc()

rawDescription: Python multiprocessing - Pipe vs Queue

description: Python multiprocessing - Pipe vs Queue


code:
def read_text(text):
    for line in text.split('\n'):
        docid, termid, val = map(int,line.split())
        if docid < previd or docid-1 > previd:
            print line # I guess this is a debug feature
            return # or raise Exception("line not in running order", line)
        yield (docid, termid, val)

rawDescription: Check that matrix keys are in running order - python


code:
def foo():
    x = dict()
    i = 0
    while i < 10:
        x[i] = x*2
        i += 1
        yield x[i]

for i in foo():
    print i

rawDescription: Return multiple variables in a loop using dict()


code:
class A(object):
    def a1(self):
        print "Hello from Superclass"

class B(A):
    pass

B().a1() # => prints 'Hello from Superclass'

rawDescription: Python using methods from other classes

description: Python using methods from other classes


code:
import re
# assume all_lines = somefile.readlines() or use it in the for loop below.
# but simplying to this
all_lines = [
"00:00:29:02 00:00:35:00 text 1",
"text 2",
"00:00:36:04 00:00:44:08 text 3",
"text 4",
"00:00:44:12 00:00:48:00 text 5",
"00:00:49:17 00:00:52:17 text 6",
"text 7",  # added for interest
"text 8",  # added for interest
]
new_lines = []
for line in all_lines:
    if not re.match('(?:(?:\d\d:){3}(?:\d\d) ){2}.*', line):
        # line did not start with a timestamp
        new_lines.append(new_lines.pop() + ' ' + line)
    else:
        new_lines.append(line)
print '\n'.join(new_lines)

rawDescription: concatenate lines in Python if next line not matches pattern

description: Concatenate lines in Python if next line not matches pattern


rawDescription: Create 64-bit value from datetime object


code:
obj = data.json()
type(obj)
obj.keys()
print obj['kind']
type(obj['data'])
obj['data'].keys()
type(obj['data']['children'])

rawDescription: How to filter a big chunk of text which contains no line breaks in python?

description: Filter a big chunk of text which contains no line breaks in python


code:
class Echo(Protocol):
    def connectionLost(self, reason):
        print reason
        # which is crude, there should be a more elegant answer
        reactor.stop()

rawDescription: Python Twisted with callInThread

description: Python Twisted with callInThread


code:
#print all the namespaces used in the xml doc
print root.nsmap

rawDescription: Parse a SOAP message with namespace in Python


code:
print "Celex number:", celex.contents[0].splitlines()[1]
print "Authentic language:", auth_lang.contents[0].splitlines()[0]
print "Type of procedure:", procedure.contents[4].splitlines()[1]

rawDescription: Extracting data from HTML-files with BeautifulSoup and Python


code:
timeTotal = 0
timeResolution = 0.5
waitUntil = 90
while timeTotal < waitUntil:
    time.sleep(timeResolution)
    timeTotal += timeResolution
    print "\r{} seconds left".format(waitUntil - timeTotal),

rawDescription: Create a stopwatch (not a countdown or timer) without using Tk python


code:
img_links = soup.findAll("a", {"class":"image"})
for img_link in img_links:
    print img_link.img['src']

rawDescription: Fetching Image from URL using BeautifulSoup


code:
#!/usr/bin/env python

from Crypto.PublicKey import RSA
key = RSA.generate(1024)
print key.exportKey('PEM')

pub_key = key.publickey()
print pub_key.exportKey('PEM')

rawDescription: 'exportKey' function not recognized in pycrypto-2.6.1(python 2.7.6) ubuntu14.04 LTS


code:
import argparse, sys

parser = argparse.ArgumentParser()
parser.add_argument('infile', nargs='?',
                    type=argparse.FileType('r'),
                    default=sys.stdin)

opts = parser.parse_args()

print opts.infile.read()

rawDescription: input redirection for python 2.7

description: Input redirection for python 2.7


code:
myList = [25,3,26,27,56]
for i in range(0, len(myList) - 1, 2):
    myList[i], myList[i + 1] = myList[i + 1], myList[i]
print myList

rawDescription: How to swap one value with next value in Python?

description: Swap one value with next value in Python


code:
import urllib2, re
from BeautifulSoup import BeautifulSoup

turl = "http://rads.stackoverflow.com/amzn/click/0743273567"
toppage = urllib2.urlopen(turl)
soup = BeautifulSoup(toppage)

review_tag  = {'class':re.compile("mt9 reviewText")}
helpful_tag = {'class':re.compile("hlp")}

all_reviews = soup.findAll(attrs=review_tag)
all_helpful = soup.findAll(attrs=helpful_tag)

for text,info in zip(all_reviews, all_helpful):
    print info.string.strip()
    print '\n'.join(text.findAll(text=True)).strip()
    print "*******************************************"

rawDescription: Extracting content of div with BeautifulSoup


code:
class Foo(object):
    def __str__(self):
            return "str of Foo"
    def __repr__(self):
            return "Foo()"
f = Foo()
f
print f
eval(repr(f))

rawDescription: Python - What happens when I write statements in the interpreter without 'print'?


code:
class Menu(object):
   def __init__(self):
      # Ideally, self._options would be an empty dict for the base class.
      # This is just for the sake of example.
      self._options = {'a': self.optionA,
                       'b': self.optionB}

   def handle_options(self, option):
      if option not in self._options:
         print "Invalid option"
         # re-draw
         return

      self._options[option]()

   def optionA(self):
      print "option A"

   def optionB(self):
      print "option B"

rawDescription: How to create a series of text menus in Python

description: Create a series of text menus in Python


code:
with open("file.txt", "r") as manipulated_file:
    manipulated_file_lines = manipulated_file.readlines()
with open("file.txt", "w") as  manipulated_file:

    for line in manipulated_file_lines:
        line=line.split()
        list_to_be_written=[]
        for i in line:
            i=i.replace("['","")
            i=i.replace("'","")
            i=i.replace(",",":")
            i=i.replace("]","")
            list_to_be_written.append(i)
        list_to_be_written=" ".join(list_to_be_written)
        print list_to_be_written

        manipulated_file.write(list_to_be_written)

rawDescription: Manipulating data of file several times

description: Manipulating data of file several times


code:
from xml.etree import ElementTree as et
import sys
print sys.argv[1]
xmldoc = et.parse('/root/%s.xml' % sys.argv[1])

print(sorted({n.get("name") for n in xmldoc.findall(".//record[@name]")}))

rawDescription: Python: Find Unique XML Attributes

description: Python: Find Unique XML Attributes


code:
for features, label in train_documents:
    label, features
    break

print len(train_documents)
print len(test_documents)
# Get the number of documents in movie_review corpus
num_docs_in_corpus = len(list(chain(*[movie_reviews.fileids(categories=[cat]) for cat in movie_reviews.categories()])))
print len(train_documents) + len(test_documents) == num_docs_in_corpus

rawDescription: My Maxent Classifier works fine with gis algorithm but does not work with iis algorithm. It is not throwing any error, just some warnings


code:
def load_tests(loader, tests, pattern):
    print 'load_tests called'
    suite = unittest.TestSuite()
    f = ['a','b']  # data.csv contains three lines: "a\nb\nc"
    for line in f:
        tc = Foo()
        tc.setup(line)
        suite.addTest(tc)
    return suite

rawDescription: Python unit_test load_tests function failing


code:
names = [name_level_1, name_level_2, name_level_3, name_level_4]
translated_names = [util.translate("iw", "en", name.encode('utf-8')) for name in names]
for name in translated_names:
    print name

rawDescription: Basic Python question: referencing original variable inside for loop?


code:
from bs4 import BeautifulSoup
import requests

respond = requests.get("http://physics.nist.gov/PhysRefData/XrayMassCoef/ElemTab/z30.html")
soup = BeautifulSoup(respond.text)
l = soup.find('table').find('pre').get_text()[145:].split("\n")
print l

rawDescription: Accessing web table using Python - NIST website


rawDescription: Change stride of numpy array (altering data)


code:
def quickSort(arr):
    #Leftside
    less = []
    #pivot
    pivotList = []
    #rightside
    more = []
    #if the length of the array is one. then, there is no point in sorting it
    if len(arr) <= 1:
        return arr
    else:
    #sorting :)
        #Defines the pivot as the first element
        pivot = arr[0]
        for i in arr:
            #for each element in the array verify:
            if i < pivot:
                less.append(i)
            elif i > pivot:
                more.append(i)
            else:
                pivotList.append(i)
        #Define the Lists less (left side) and more (right side)
        less = quickSort(less)
        more = quickSort(more)
        #Return the actual array
        return less + pivotList + more

a = [7, 2, 5, 1, 29, 6, 4, 19, 11]
a = quickSort(a)
print a

rawDescription: Python implementation of Quicksort fails to sort the entire array


code:
for value in item.itervalues():
    print value

rawDescription: How to convert an associative array in python?

description: Convert an associative array in python


code:
query1 = Blog.objects.filter(comment__created__gt=from_dt).filter(comment__created__lt=till)
print query1.query

rawDescription: Django Queryset: Efficiency of Equivalent Filter Clauses


code:
#! /usr/bin/env python

sample_interval = 10
interface="eth1"

from scapy.all import *
from collections import Counter

# Counter is a *much* better option for what you're doing here. See
# http://docs.python.org/2/library/collections.html#collections.Counter
traffic = Counter()
# You should probably use a cache for your IP resolutions
hosts = {}

def human(num):
    for x in ['', 'k', 'M', 'G', 'T']:
        if num < 1024.: return "%3.1f %sB" % (num, x)
        num /= 1024.
    # just in case!
    return  "%3.1f PB" % (num)

def traffic_monitor_callback(pkt):
    if IP in pkt:
        pkt = pkt[IP]
        # You don't want to use sprintf here, particularly as you're
        # converting .len after that!
        # Here is the first place where you're happy to use a Counter!
        # We use a tuple(sorted()) because a tuple is hashable (so it
        # can be used as a key in a Counter) and we want to sort the
        # addresses to count mix sender-to-receiver traffic together
        # with receiver-to-sender
        traffic.update({tuple(sorted(map(atol, (pkt.src, pkt.dst)))): pkt.len})

sniff(iface=interface, prn=traffic_monitor_callback, store=False,
      timeout=sample_interval)

# ... and now comes the second place where you're happy to use a
# Counter!
# Plus you can use value unpacking in your for statement.
for (h1, h2), total in traffic.most_common(10):
    # Let's factor out some code here
    h1, h2 = map(ltoa, (h1, h2))
    for host in (h1, h2):
        if host not in hosts:
            try:
                rhost = socket.gethostbyaddr(host)
                hosts[host] = rhost[0]
            except:
                hosts[host] = None
    # Get a nice output
    h1 = "%s (%s)" % (hosts[h1], h1) if hosts[h1] is not None else h1
    h2 = "%s (%s)" % (hosts[h2], h2) if hosts[h2] is not None else h2
    print "%s/s: %s - %s" % (human(float(total)/sample_interval), h1, h2)

rawDescription: Calculate bandwidth usage per IP with scapy, iftop-style