code:
class CameraDisplay(QtGui.QLabel):
  def __init__(self):
    super(CameraDisplay, self).__init__()

  def updateFrame(self, image):
    self.setPixmap(QtGui.QPixmap.fromImage(image))

class ControlCenter(QtGui.QWidget):
  up_camera_signal = QtCore.Signal(QtGui.QImage)
  up_camera = None

  def __init__(self):
    super(ControlCenter, self).__init__()
    self.up_camera = CameraDisplay()
    self.up_camera_signal.connect(self.up_camera.updateFrame)

    grid = QtGui.QGridLayout()
    grid.setSpacing(10)

    grid.addWidget(self.up_camera, 0, 0)

    self.setLayout(grid)

    self.setGeometry(300, 300, 350, 300)
    self.setWindowTitle('Control Center')
    self.show()

  def up_camera_callback(self, data):
    '''This function gets called by an external thread'''
    try:
      image = QtGui.QImage(data.data, data.width, data.height, QtGui.QImage.Format_RGB888)
      self.up_camera_signal.emit(image)

    except Exception, e:
      print(e)

if __name__ == "__main__":
  app = QtGui.QApplication(sys.argv)
  ex = ControlCenter()
  sys.exit(app.exec_())

rawDescription: Displaying a video stream in QLabel with PySide


code:
listpack_list = (["data", "data", "data"], ["test", "test", "test"], ["sof", "sof", "sof"])
import random
rem = random.randrange(3)
varToBePrinted, Main1, Main2 = listpack_list[rem]
listpack_list = tuple(item for index, item in enumerate(listpack_list) if index != rem)
print varToBePrinted

rawDescription: Randomly choosing a tuple set and then removing itself from the orignal tuple list


code:
import re
with open("input") as f:
    for line in f:
        line = line.strip()
        # chop a line into expressions of the form: str [OP str]
        exprs = re.split(r'([\w\d]+\s*(?:[!<>=]=?\s*[\w\d.]*)?\s*,?\s*)', line)
        for expr in exprs:
            # chop each expression into tokens and get the str part
            tokens = re.findall(r'([\w\d]+)\s*(?:[!<>=]=?\s*[\w\d.]*)?,?', expr)
            if tokens: print tokens

rawDescription: string split with python regex

description: String split with python regex


code:
from openpyxl import load_workbook

wb2 = load_workbook('test.xlsx')
print wb2.get_sheet_names()

rawDescription: How do I check a 3rd party Python utility library for malware?

description: Check a 3rd party Python utility library for malware


code:
for a, b in izip_longest(l1, l2, fillvalue=''):
    print "{0:20s}\t{1:20s}".format(a, b)

rawDescription: How to print a table from lists with different lengths in Python

description: Print a table from lists with different lengths in Python


code:
def foo(a, b, *c):
  print a, b, c

foo(1, 2, 3, 4, 5)

rawDescription: How can I create a Python function dynamically which takes a specified number of arguments?

description: Create a Python function dynamically which takes a specified number of arguments


code:
from google.appengine.api import datastore

entity_key = 'ag1lbmdlbG1pbmFzd2VicgoLEgRVc2VyGGIM'
entity = datastore.Get(entity_key)
print 'my_property' in entity

rawDescription: appengine: how can I check if a property from an entity exists in the datastore?

description: Appengine: how can I check if a property from an entity exists in the datastore


code:
import maya.cmds as cmds
from functools import partial

storedDataDic = {}
storedDataDic["weapon"] = ["Pistol", "Shotgun", "SMG", "Sniper Rifle", "RPG"]
storedDataDic["weaponSelected"] = storedDataDic["weapon"][0]
storedDataDic["Shotgun_preset"] = [(1,4,2), (1,50,25)]
storedDataDic["Pistol_preset"] = [(1,18,2), (1,25,10)]

if (cmds.window("Bullet_Spray_Generator", exists = True)):
    cmds.deleteUI("Bullet_Spray_Generator")
if (cmds.window("BSG2", exists = True)):
    cmds.deleteUI("BSG2")

cmds.select(all=True)
cmds.delete()

def goShoot(numOfShots, distToTarget, *pArgs):
    print "Begin"
    cmds.deleteUI("BSG2")
    createWall()

def cancelShoot(*pArgs):
    print "cancel"
    cmds.deleteUI("Bullet_Spray_Generator")

def createWall():
    cmds.select(all=True)
    cmds.delete()
    wall = cmds.polyCube(h=10, w=15, d=1, name='wall')
    cmds.move(0,5,0, 'wall')

def createGunUI(gunType, *pArgs):
    if (gunType == 'Pistol'):
        createWeaponUI()
    elif (gunType == 'Shotgun'):
        createWeaponUI()
    elif (gunType == 'SMG'):
        createSMGUI()
    elif (gunType == 'Sniper Rifle'):
        createSniperUI()
    elif (gunType == 'RPG'):
        createRPGUI()
    else:
        print "Something went wrong"
        cancelShoot()

def createPistolUI():
    cmds.window("Pistol")
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
    minValue=1, maxValue=9, value=4, field=True)

    DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=1, maxValue=50, value=25, field=True)

    cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
    query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("Pistol")

def createShotgunUI():
    cmds.window("Shotgun")
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
    minValue=1, maxValue=4, value=2, field=True)

    DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=1, maxValue=50, value=25, field=True)

    cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
    query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("Shotgun")

def createSMGUI():
    cmds.window("SMG")
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
    minValue=1, maxValue=20, value=4, field=True)

    DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=1, maxValue=50, value=25, field=True)

    cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
    query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("SMG")

def createSniperUI():
    cmds.window("Sniper")
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
    minValue=1, maxValue=2, value=2, field=True)

    DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=1, maxValue=50, value=25, field=True)

    cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
    query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("Sniper")

def createRPGUI():
    cmds.window("RPG")
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    NumBulletsCtrl = cmds.intSliderGrp(label='Number of Shots',
    minValue=1, maxValue=1, value=1, field=True)

    DistCtrl = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=1, maxValue=50, value=25, field=True)

    cmds.button(label = "Fire", command = lambda *args: goShoot(cmds.intSliderGrp(NumBulletsCtrl,
    query=True, value=True), cmds.intSliderGrp(DistCtrl, query=True, value=True), ))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("RPG")

def printNewMenuItem(item):
    print item
    storedDataDic["weaponSelected"] = item
    return storedDataDic["weaponSelected"]

def ui_refreshSelWeapon(fun, *args):
    createGunUI(storedDataDic["weaponSelected"])

def createUI():
    cmds.window("Bullet_Spray_Generator")
    cmds.columnLayout(adjustableColumn=True)

    GunSelectCtrl = cmds.optionMenu(label='Gun', changeCommand=printNewMenuItem)
    for i in storedDataDic["weapon"]:
        cmds.menuItem(label=i)

    cmds.button(label = "Continue", command = ui_refreshSelWeapon)

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow("Bullet_Spray_Generator")

createUI()

weapon_uiDic = {}

def createWeaponUI():

    weaponName = storedDataDic["weaponSelected"]

    if cmds.window(weaponName, exists=True):
        cmds.deleteUI(weaponName)
    cmds.window(weaponName)
    cmds.columnLayout(adjustableColumn=True)

    cmds.deleteUI("Bullet_Spray_Generator")

    nbP= storedDataDic[weaponName + "_preset"][0]
    dcP = storedDataDic[weaponName + "_preset"][1]

    weapon_uiDic["NumBulletsCtrl"] = cmds.intSliderGrp(label='Number of Shots',
    minValue=nbP[0], maxValue=nbP[1], value=nbP[2], field=True)

    weapon_uiDic["DistCtrl"] = cmds.intSliderGrp(label='Distance to Target (metres)',
    minValue=dcP[0], maxValue=dcP[1], value=dcP[2], field=True)

    weapon_uiDic["fireButton"] = cmds.button(label = "Fire",
                                            command = lambda *args: goShoot(cmds.intSliderGrp(weapon_uiDic["NumBulletsCtrl"],
    query=True, value=True), cmds.intSliderGrp(weapon_uiDic["DistCtrl"], query=True, value=True)))

    cmds.button(label = "Cancel", command = cancelShoot)

    cmds.showWindow(weaponName)

rawDescription: Maya Python - Using data from UI

description: Maya Python - Using data from UI


code:
import itertools
A=np.array([0,1,0,0,-1,4,8,22,16,7,2,1,0,-1,-17,-20,-6,-1,0,1,0,2,1,0,8,-7,-1,0,0,1,0,1,-1,-17,-22,-40,16,1,3,14,17,19,8,2,0,1,3,2,3,1,0,0,-2,1,0,0,-1,22,4,0,-1,0])
B=np.hstack(([0,0],A,[0,0]))
B=np.asanyarray(zip(*[B[i:] for i in range(5)]))
C=(B*[0.25,0.5,1,0.5,0.25]).mean(axis=1) #C is the 5-element sliding windows exponentially smoothed signal
D=[]
for item in itertools.groupby(enumerate(C), lambda x: abs(x[1])>1.5):
    if item[0]:
        D.append(list(item[1])) #Get the indices where the signal are of magnitude >2. Change 1.5 to control the behavior.
E=[D[0]]
for item in D[1:]:
    if (item[0][0]-E[-1][-1][0]) <5: #Merge interesting regions if they are 5 or less indices apart. Change 5 to control the behavior.
        E[-1]=E[-1]+item
    else:
        E.append(item)
print [(item[0][0], item[-1][0]) for item in E]
[A[item[0][0]: item[-1][0]] for item in E if (item[-1][0]-item[0][0])>9] #Filter out the interesting regions <10 in length.

rawDescription: Finding several regions of interest in an array


code:
index_value = 0
while index_value < 7:
    if start_time < current_time < stop_time:
        print "program is half way"
        currenttime = self.parseDateTimeToMinutesSinceEpoch(start_date) # same as start_time?
        program_stop = self.parseDateTimeToMinutesSinceEpoch(stop_date) # same as stop_time?
        program_stop - currenttime # this statement doesn't do anything
        datetime.timedelta(0, 8100) # this statement doesn't do anything
        current_program_length = program_stop - currenttime # is currenttime the same as current_time?

        if current_program_length >= 30:
            program_button = [elem.control for elem in self.program_buttons]
            prog_id = list()
            posX = list()
            posY = list()

            for elem in program_button:
                prog_id.append(elem.getId())
                posX.append(elem.getX())
                posY.append(elem.getY())
            prog_id = map(str, prog_id)
            posX = map(str, posX)
            posY = map(str, posY)

            for posX, posY, prog_id in zip (posX, posY, prog_id):
                if int(posX) == 375:
                   for index in range(0, ):  # this statement doesn't do anything
                       print prog_id # this is never executed

    elif start_time < current_time >= stop_time:
        print "program has finished"
    index_value += 1

rawDescription: how to generate the index of value under the if statement

description: Generate the index of value under the if statement


code:
# simplified example from the tutorial
import gtk

def on_match_selected(completion, treemodel, treeiter):
  print treemodel[treeiter][completion.get_text_column()]

urls = [
    'http://www.google.com',
    'http://www.google.com/android',
    'http://www.greatstuff.com',
    'http://www.facebook.com',
    ]
liststore = gtk.ListStore(str)
for s in urls:
    liststore.append([s])

completion = gtk.EntryCompletion()
completion.set_model(liststore)
completion.set_text_column(0)

completion.connect('match-selected', on_match_selected)

entry = gtk.Entry()
entry.set_completion(completion)

# boilerplate
window = gtk.Window()
window.add(entry)

window.connect('destroy', lambda w: gtk.main_quit())
window.show_all()
gtk.main()

rawDescription: How to get entry from entrycompletion

description: Get entry from entrycompletion


code:
#!/usr/bin/env python

import sys
from xml.sax import handler, make_parser

class TagHandler(handler.ContentHandler):
    def __init__(self):
        handler.ContentHandler.__init__(self)

        self.stack = []

    def startElement(self, name, attrs):
        self.stack.append(name)

    def endElement(self, name):
        # TODO: might want to just confirm that the element matches the top of the stack here
        self.stack.pop()

    def finish_document(self):
        return "\n".join(["</%s>" % tag for tag in reversed(self.stack)])

parser = make_parser()
handler = TagHandler()
parser.setContentHandler(handler)

try:
    parser.parse(sys.argv[1])

except:
    # TODO: something more intelligent than just printing out the
    # constructed end of the document. Like appending it to the source
    # and repeating whatever you did to make this processing necessary.
    print handler.finish_document()

rawDescription: Pythonic way to fix broken xml

description: Pythonic way to fix broken xml


code:
def match(line):
    lfmt = '%Y-%m-%d %H:%M:%S'
    if line[0] == '[':
        return datetime.datetime.strptime(line[1:20], lfmt)

def retrieve_test_line(position):
    file.seek(position,0)
    file.readline()  # avoids reading partial line, which will mess up match attempt
    new_position = file.tell() # gets start of line position
    return file.readline(), new_position

def check_lower_bound(position):
    file.seek(position,0)
    new_position = file.tell() # gets start of line position
    return file.readline(), new_position

def find_line(target, lower_bound, upper_bound):
    trial = int((lower_bound + upper_bound) /2)
    inspection_text, position = retrieve_test_line(trial)
    if position == upper_bound:
        text, position = check_lower_bound(lower_bound)
        if match(text) == target:
            return position
        return # no match for target within range
    matched_position = match(inspection_text)
    if matched_position == target:
        return position
    elif matched_position < target:
        return find_line(target, position, upper_bound)
    elif matched_position > target:
        return find_line(target, lower_bound, position)
    else:
        return # no match for target within range

lfmt = '%Y-%m-%d %H:%M:%S'
# start_target =  # first line you are trying to find:
start_target =  datetime.datetime.strptime("2012-02-01 13:10:00", lfmt)
# end_target =  # last line you are trying to find:
end_target =  datetime.datetime.strptime("2012-02-01 13:39:00", lfmt)
file = open("log_file.txt","r")
lower_bound = 0
file.seek(0,2) # find upper bound
upper_bound = file.tell()

sequence_start = find_line(start_target, lower_bound, upper_bound)

if sequence_start or sequence_start == 0: #allow for starting at zero - corner case
    sequence_end = find_line(end_target, sequence_start, upper_bound)
    if not sequence_end:
        print "start_target match: ", sequence_start
        print "end match is not present in the current file"
else:
    print "start match is not present in the current file"

if (sequence_start or sequence_start == 0) and sequence_end:
    print "start_target match: ", sequence_start
    print "end_target match: ", sequence_end
    print
    print start_target, 'target'
    file.seek(sequence_start,0)
    print file.readline()
    print end_target, 'target'
    file.seek(sequence_end,0)
    print file.readline()

rawDescription: trim big log file

description: Trim big log file


code:
win.show_all()
#win.window.input_shape_combine_mask(img,0,0)
def set_mask(win):
    #b=gtk.gdk.bitmap_create_from_data(win.window,8,win.window.get_size())
    size=win.window.get_size()
    print size
    bitmap=gtk.gdk.Pixmap(win.window,size[0],size[1],1)

    cr = bitmap.cairo_create()
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.set_source_rgba(0.0,0.0,0.0,0.0)
    cr.rectangle((0,0)+size)
    cr.fill()

    win.window.input_shape_combine_mask(bitmap,0,0)
    print 'ready'
gobject.timeout_add(5000, set_mask, win)

rawDescription: How to click through gtk.Window?

description: Click through gtk.Window


code:
import re

for line in open('logfile.log'):
    match = re.search(r'-(.*):', line)
    if match:
        print match.group(1)
    else:
        match = re.search(r'-(.*)', line)
        if match:
            print match.group(1)
        else:
            print 'No match in line', line.strip()

rawDescription: python regex - last occurance before EOL

description: Python regex - last occurance before EOL


code:
import numpy as np
from scipy.integrate import odeint

def odeintz(func, z0, t, **kwargs):
    """An odeint-like function for complex valued differential equations."""

    # Disallow Jacobian-related arguments.
    _unsupported_odeint_args = ['Dfun', 'col_deriv', 'ml', 'mu']
    bad_args = [arg for arg in kwargs if arg in _unsupported_odeint_args]
    if len(bad_args) > 0:
        raise ValueError("The odeint argument %r is not supported by "
                         "odeintz." % (bad_args[0],))

    # Make sure z0 is a numpy array of type np.complex128.
    z0 = np.array(z0, dtype=np.complex128, ndmin=1)

    def realfunc(x, t, *args):
        z = x.view(np.complex128)
        dzdt = func(z, t, *args)
        # func might return a python list, so convert its return
        # value to an array with type np.complex128, and then return
        # a np.float64 view of that array.
        return np.asarray(dzdt, dtype=np.complex128).view(np.float64)

    result = odeint(realfunc, z0.view(np.float64), t, **kwargs)

    if kwargs.get('full_output', False):
        z = result[0].view(np.complex128)
        infodict = result[1]
        return z, infodict
    else:
        z = result.view(np.complex128)
        return z

if __name__ == "__main__":
    # Generate a solution to:
    #     dz1/dt = -z1 * (K - z2)
    #     dz2/dt = L - z2
    # K and L are fixed parameters.  z1(t) and z2(t) are complex-
    # valued functions of t.

    # Define the right-hand-side of the differential equation.
    def zfunc(z, t, K, L):
        z1, z2 = z
        return [-z1 * (K - z2), L - z2]

    # Set up the inputs and call odeintz to solve the system.
    z0 = np.array([1+2j, 3+4j])
    t = np.linspace(0, 4, 101)
    K = 3
    L = 1
    z, infodict = odeintz(zfunc, z0, t, args=(K,L), full_output=True)

    # For comparison, here is how the complex system can be converted
    # to a real system.  The real and imaginary parts are used to
    # write a system of four coupled equations.  The formulas for
    # the complex right-hand-sides are
    #   -z1 * (K - z2) = -(x1 + i*y1) * (K - (x2 + i*y2))
    #                  = (-x1 - i*y1) * (K - x2 + i(-y2))
    #                  = -x1 * (K - x2) - y1*y2 + i*(-y1*(K - x2) + x1*y2)
    # and
    #   L - z2 = L - (x2 + i*y2)
    #          = (L - x2) + i*(-y2)
    def func(r, t, K, L):
        x1, y1, x2, y2 = r
        dx1dt = -x1 * (K - x2) - y1*y2
        dy1dt = -y1 * (K - x2) + x1*y2
        dx2dt = L - x2
        dy2dt = -y2
        return [dx1dt, dy1dt, dx2dt, dy2dt]

    # Use regular odeint to solve the real system.
    r, infodict = odeint(func, z0.view(np.float64), t, args=(K,L), full_output=True)

    # Compare the two solutions.  They should be the same.  (As usual for
    # floating point calculations, there could be a small difference.)
    delta_max = np.abs(z.view(np.float64) - r).max()
    print "Maximum difference between the complex and real versions is", delta_max

    # Plot the real and imaginary parts of the complex solution.

    import matplotlib.pyplot as plt

    plt.clf()
    plt.plot(t, z[:,0].real, label='z1.real')
    plt.plot(t, z[:,0].imag, label='z1.imag')
    plt.plot(t, z[:,1].real, label='z2.real')
    plt.plot(t, z[:,1].imag, label='z2.imag')
    plt.xlabel('t')
    plt.grid(True)
    plt.legend(loc='best')
    plt.show()

rawDescription: scipy odeint with complex initial values

description: Scipy odeint with complex initial values


code:
def addToList(funcList):
    '''Decorator that adds the function to a given list'''
    def actual_decorator(f):
         funcList.append(f)
         return f
    return actual_decorator

class MyClass(object):
    funcList = []

    @addToList(funcList)
    def some_function(self, name):
        print 'Hello,', name

rawDescription: self in python decorators

description: Self in python decorators


code:
import string
my_input = "V 1 -2 3 4 5 7 8 9 10"
doctored_input = map(int,my_input.split()[1:])

print dict(zip(string.ascii_uppercase,doctored_input))
#result : {'A': 1, 'C': 3, 'B': -2, 'E': 5, 'D': 4, 'G': 8, 'F': 7, 'I': 10, 'H': 9}

rawDescription: assigning variables dynamically in a loop

description: Assigning variables dynamically in a loop


code:
from xml.dom.minidom import parse

def getText(nodelist):
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def getTextForTag(nodelist,tag):
    elements = nodelist.getElementsByTagName(tag)
    if (elements.length>0):
        return getText( elements[0].childNodes)
    return ''

def printValueForTag(parent, indent, tag, valueName=''):
    value = getTextForTag( parent,tag)
    if (len(value)>0):
        if (valueName==''):
            valueName = tag
        print indent + valueName+": "+value

def emitLogRotate(indent, rotator):
    print indent+"logrotate:"
    indent+='  '
    printValueForTag( rotator,indent, 'daysToKeep')
    printValueForTag( rotator,indent, 'numToKeep')
def emitProject(project):
    print "- project:"
    # all projects have log rotators, so no need to chec
    emitLogRotate("   ",project.getElementsByTagName('logRotator')[0])
    # next section...

dom = parse('config.xml')
emitProject(dom)

rawDescription: How to convert jenkins job configuration config.xml to YAML format in python to be used jenkins-job-builder?

description: Convert jenkins job configuration config.xml to YAML format in python to be used jenkins-job-builder


code:
import argparse
import sys

parser = argparse.ArgumentParser(description="Does some awesome things.")
parser.add_argument('message', type=str, help="pass a message into the script")

if __name__ == '__main__':
    args = parser.parse_args(sys.argv[1:])
    print args.message

rawDescription: How to run a python script with options

description: Run a python script with options


code:
from scrapy.spider import BaseSpider
from scrapy.selector import HtmlXPathSelector

class DmozSpider(BaseSpider):
    name = "dmoz.org"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Books/",
        "http://www.dmoz.org/Computers/Programming/Languages/Python/Resources/"
    ]
def parse(self, response):
    hxs = HtmlXPathSelector(response)
    sites = hxs.select('//ul/li')
    for site in sites:
        title = site.select('a/text()').extract()
        link = site.select('a/@href').extract()
        desc = site.select('text()').extract()
        print title, link, desc

rawDescription: Screen Scraping in Python

description: Screen Scraping in Python


code:
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read())

aa = soup.find_all(text="Design Team")

nexttr = aa.next_sibling

if nexttr.td.text == "Date of Employment:":
     print nexttr.td.next_sibling.text
else:
     print "No Date of Employment:"

rawDescription: Python + BeautifulSoup - Limiting text extraction on a specific table (multiple tables on a webpage)


code:
from BeautifulSoup import BeautifulSoup

txt = '''Natros Pharmacy
Natros Pharmacy'''
match = 'body_BusinessSearchResultSummaryList_repBusinessList_lnkBusinessProfile'

soup = BeautifulSoup(txt)
for a in soup.findAll('a'):
        if a.has_key('id') and a['id'].startswith(match):
               print a['href'], a.contents

rawDescription: Using BeautifulSoup to extract anchor tag values


code:
class TestEllipsis(object):
    def __getitem__(self, item):
        if item is Ellipsis:
            return "Returning all items"
        else:
            return "return %r items" % item

x = TestEllipsis()
print x[2]
print x[...]

rawDescription: How do you use the ellipsis slicing syntax in Python?

description: Use the ellipsis slicing syntax in Python


code:
import urllib2
from BeautifulSoup import BeautifulStoneSoup #Using bs3

url="https://api.projectnimbus.org/neaodataservice.svc/NowcastSet"
request = urllib2.Request(url)
request.add_header("accept", "*/*")
request.add_header('AccountKey', "OSJeROQjTg4v7Ec3kiecjw==")
request.add_header('UniqueUserID', "00000000000000000000000000000001")
result = urllib2.urlopen(request)
xml_str = result.read()

soup = BeautifulStoneSoup(xml_str)

prop_list = []
for content in soup.findAll("m:properties"):
    props = {}
    for prop in content.findChildren():
        props[prop.name[2:]] = prop.text
    prop_list.append(props)

for prop in prop_list:
    print "Area: %(area)s\nLat: %(latitude)s\nLong: %(longitude)s\n" % prop

rawDescription: python printing to a key pair value from a website using beautifulsoup

description: Python printing to a key pair value from a website using beautifulsoup


code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import multiprocessing
import sys
import numpy as np
import time

def f(data_slice, **kwargs):
    '''This is a time-intensive function, which we do not want to alter
    '''
    data = 0
    for row in range(data_slice.shape[0]):
        for col in range(data_slice.shape[1]):
            data += data_slice[row,col]**2
    time.sleep(0.1)
    return data, 3, 5, 3 # some dummy calculation results

def f_wrapper(row, data_slice,  **kwargs):
    results = f(data_slice, **kwargs)
    return row, results

class MainWindow(QMainWindow): #You can only add menus to QMainWindows

    def __init__(self):
        super(MainWindow, self).__init__()
        self.pool = multiprocessing.Pool(processes=4)

        button1 = QPushButton('Connect', self)
        button1.clicked.connect(self.apply_connection)
        self.text = QTextEdit()

        vbox1 = QVBoxLayout()
        vbox1.addWidget(button1)
        vbox1.addWidget(self.text)
        myframe = QFrame()
        myframe.setLayout(vbox1)

        self.setCentralWidget(myframe)
        self.show() #display and activate focus
        self.raise_()

    def apply_connection(self):
        self.rows_processed = list()
        self.max_size = 1000
        data = np.random.random(size = (100, self.max_size,self.max_size))
        kwargs = {'some_kwarg' : 1000}
        for row in range(data.shape[1]):
            slice = data[:,row, :]
            print "starting f for row ", row
            result = self.pool.apply_async(f_wrapper,
                                           args = (row, slice),
                                           kwds = kwargs,
                                           callback=self.update_gui)
            #~ result.get() # blocks gui, but raises errors for debugging

    def update_gui(self, result):
        row, func_result = result
        self.rows_processed.append(row)
        print len(self.rows_processed)
        print func_result# or do something more intelligent
        self.text.append('Applied connection. Row = %d\n' % row)
        if len(self.rows_processed) == self.max_size:
            print "Done!"

if __name__ == '__main__':
    app = QApplication(sys.argv)
    gui = MainWindow()
    app.exec_()

rawDescription: Joblib Parallel uses only one core if started from QThread


code:
table = [["spam",42],["eggs",451],["bacon",0]]
headers = ["item", "qty"]
print tabulate(table, headers, tablefmt="simple")

rawDescription: Headers in to_latex

description: Headers in to_latex


code:
class A:
    v = 'a'

class B:
    v = 'b'

def C(x):
    class C(x): pass
    return C
x = A
print C(x).v

rawDescription: Inheirit from a variable, Python?

description: Inheirit from a variable, Python


code:
from contextlib import contextmanager
from itertools import izip
from glob import iglob
from math import sqrt
from sys import exit

@contextmanager
def multi_file_manager(files, mode='rt'):
    files = [open(file, mode) for file in files]
    yield files
    for file in files:
        file.close()

# generator function to read, convert, and yield each value from a text file
def read_values(file, datatype=float):
    for line in file:
        for value in (datatype(word) for word in line.split()):
            yield value

# enumerate multiple egual length iterables simultaneously as (i, n0, n1, ...)
def multi_enumerate(*iterables, **kwds):
    start = kwds.get('start', 0)
    return ((n,)+t for n, t in enumerate(izip(*iterables), start))

DATA_FILE_PATTERN = 'data*.dat'
MIN_DATA_FILES = 2

with multi_file_manager(iglob(DATA_FILE_PATTERN)) as datfiles:
    num_files = len(datfiles)
    if num_files < MIN_DATA_FILES:
        print('Less than {} .dat files were found to process, '
              'terminating.'.format(MIN_DATA_FILES))
        exit(1)

    # determine number of rows and cols from first file
    temp = [line.split() for line in datfiles[0]]
    num_rows = len(temp)
    num_cols = len(temp[0])
    datfiles[0].seek(0)  # rewind first file
    del temp  # no longer needed
    print '{} .dat files found, each must have {} rows x {} cols\n'.format(
           num_files, num_rows, num_cols)

    means = []
    std_devs = []
    divisor = float(num_files-1)  # Bessel's correction for sample standard dev
    generators = [read_values(file) for file in datfiles]
    for _ in xrange(num_rows):  # main processing loop
        for _ in xrange(num_cols):
            # create a sequence of next cell values from each file
            values = tuple(next(g) for g in generators)
            mean = float(sum(values)) / num_files
            means.append(mean)
            means_diff_sq = ((value-mean)**2 for value in values)
            std_dev = sqrt(sum(means_diff_sq) / divisor)
            std_devs.append(std_dev)

print 'Average and (standard deviation) of values:'
with open('means.txt', 'wt') as averages:
    for i, mean, std_dev in multi_enumerate(means, std_devs):
        print '{:.2f} ({:.2f})'.format(mean, std_dev),
        averages.write('{:.2f}'.format(mean))  # note std dev not written
        if i % num_cols != num_cols-1:  # not last column?
             averages.write(' ')  # delimiter between values on line
        else:
            print  # newline
            averages.write('\n')

rawDescription: How to calculate the average of several .dat files using python?

description: Calculate the average of several .dat files using python


code:
# This code is in the Public Domain
# -- richard@mechanicalcat.net

class Map:
    def __init__(self, map, tiles):
        self.tiles = pygame.image.load(tiles)

        l = [line.strip() for line in open(map).readlines()]
        self.map = [[None]*len(l[0]) for j in range(len(l))]
        for i in range(len(l[0])):
            for j in range(len(l)):
                tile = l[j][i]
                tile = tile_coords[tile]
                if tile is None:
                    continue
                elif isinstance(tile, type([])):
                    tile = random.choice(tile)
                cx, cy = tile
                if random.choice((0,1)):
                    cx += 192
                if random.choice((0,1)):
                    cy += 192
                self.map[j][i] = (cx, cy)

    def draw(self, view, viewpos):
        '''Draw the map to the "view" with the top-left of "view" being at
           "viewpos" in the map.
        '''
        sx, sy = view.get_size()
        bx = viewpos[0]/64
        by = viewpos[1]/64
        for x in range(0, sx+64, 64):
            i = x/64 + bx
            for y in range(0, sy+64, 64):
                j = y/64 + by
                try:
                    tile = self.map[j][i]
                except IndexError:
                    # too close to the edge
                    continue
                if tile is None:
                    continue
                cx, cy = tile
                view.blit(self.tiles, (x, y), (cx, cy, 64, 64))

    def limit(self, view, pos):
        '''Limit the "viewpos" variable such that it defines a valid top-left
           rectangle of "view"'s size over the map.
        '''
        x, y = pos
        # easy
        x = max(x, 0)
        y = max(y, 0)

        # figure number of tiles in a view, hence max x and y viewpos
        sx, sy = view.get_size()
        nx, ny = sx/64, sy/64
        mx = (len(self.map[0]) - nx) * 64
        my = (len(self.map) - ny) * 64
        print y, my

        return (min(x, mx), min(y, my))

def main():
    pygame.init()
    win = pygame.display.set_mode((640, 480))

    map = Map('map.txt', 'tiles.png')
    viewpos = (0,0)
    move = False
    clock = pygame.time.Clock()
    sx, sy = win.get_size()
    while 1:
        event = pygame.event.poll()
        while event.type != NOEVENT:
            if event.type in (QUIT, KEYDOWN):
                sys.exit(0)
            elif event.type == MOUSEBUTTONDOWN:
                x, y = viewpos
                dx, dy = event.pos
                x += dx - sx/2
                y += dy - sy/2
                viewpos = map.limit(win, (x, y))
                move = True
            event = pygame.event.poll()

        win.fill((0,0,0))
        map.draw(win, viewpos)
        pygame.display.flip()

        clock.tick(30)

if __name__ == '__main__':
    main()

rawDescription: Loading a map in Pygame / Python

description: Loading a map in Pygame / Python


code:
nlist = [r for r in refList if r in sortList]
print nlist

rawDescription: Python search list according to another reference list


code:
last_location = (None, None)
location_mutex = threading.Lock()

def gpsInfo():
    global last_location
    while True:
        session.poll()
        time.sleep(5)
        if gps.PACKET_SET:
            session.stream
            with location_mutex:
                # DON'T Print from inside thread!
                last_location = session.fix.latitude, session.fix.longitude
            time.sleep(0.1)

def WifiDetect(p):
    if p.haslayer(Dot11):
        if p.type == 0 and p.subtype in stamgmtstypes:
            if p.addr2 not in observedclients:
                with location_mutex:
                    print p.addr2, last_location
                    observedclients.append((p.addr2, last_location))

rawDescription: Two simultaneous Python loops with one result


code:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from database_setup import Restaurant, Base, MenuItem
import cgi

class webServerHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            if self.path.endswith("/hello"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "Hello!"
                output += '''<form method='POST' enctype='multipart/form-data' action='/hello'>
What would you like me to say?
<input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +=""
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/hola"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += " &#161 Hola !  Back to Hello "
                output += '''<form method='POST' enctype='multipart/form-data' action='/hello'>
What would you like me to say?
<input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +=""
                self.wfile.write(output)
                print message
                return

            if self.path.endswith("/restaurants"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                engine = create_engine('sqlite:///restaurantmenu.db')
                Base.metadata.bind = engine
                DBSession = sessionmaker(bind = engine)
                session = DBSession()
                items = session.query(Restaurant).all()
                output = "
 Printing all Restaurants !
"
                for item in items:
                    output += "
" + item.name + "
"
                    output += "
  Edit
"
                    output += "
  Delete
"
                output += "
  Create new restaurant
"
                output += ""
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/new"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                output = ""
                output += "Make a New Restaurant"
                output += '''<form method='POST' enctype='multipart/form-data' action='/new'>
Name of new restaurant ?
<input name="message" type="text" ><input type="submit" value="Submit"> </form>'''
                output +=""
                self.wfile.write(output)
                print output
                return

            if self.path.endswith("/rename"):
                self.send_response(200)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                engine = create_engine('sqlite:///restaurantmenu.db')
                Base.metadata.bind = engine
                DBSession = sessionmaker(bind = engine)
                session = DBSession()
                x = self.path
                x = x[:-7]
                x = x[1:]
                item = session.query(Restaurant).filter_by(id = x).one()
                output = ""
                output += "
" + item.name + "
"
                output += """<form method='POST' enctype='multipart/form-data' action='/""" + str(item.id) + """/rename'>
New name of restaurant ?
<input name="message" type="text" ><input type="submit" value="Submit"> </form>"""
                output +=""
                self.wfile.write(output)
                print output
                return

        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)

    def do_POST(self):
        if self.path.endswith("/new"):
            self.send_response(301)
            self.send_header('Content-type','text/html')
            self.end_headers()
            ctype, pdict = cgi.parse_header(
            self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            engine = create_engine('sqlite:///restaurantmenu.db')
            Base.metadata.bind = engine
            DBSession = sessionmaker(bind = engine)
            session = DBSession()
            myrest = Restaurant(name = messagecontent[0])
            session.add(myrest)
            session.commit()
            output = "Restaurant added"
            output +="
 Back to restaurants
"
            output +=""
            self.wfile.write(output)
            print output

        if self.path.endswith("/rename"):
            self.send_response(301)
            self.send_header('Content-type','text/html')
            self.end_headers()
            y = self.path
            y = y[:-7]
            y = y[1:]
            ctype, pdict = cgi.parse_header(
            self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('message')
            engine = create_engine('sqlite:///restaurantmenu.db')
            Base.metadata.bind = engine
            DBSession = sessionmaker(bind = engine)
            session = DBSession()
            findrest = session.query(Restaurant).filter_by(id = y).one()
            findrest.name = messagecontent[0]
            session.add(findrest)
            session.commit()
            output = "Restaurant " + messagecontent[0] + " renamed"
            output +="
 Back to restaurants
"
            output +=""
            self.wfile.write(output)
            print output

def main():
    try:
        port = 8080
        server = HTTPServer(('', port), webServerHandler)
        print "Web Server running on port %s" % port
        server.serve_forever()
    except KeyboardInterrupt:
        print " ^C entered, stopping web server...."
        server.socket.close()

if __name__ == '__main__':
    main()

rawDescription: In python using SQL Alchemy ORM, trying to update a record in the database


code:
mi = MyItem()
DBSession.add(mi)
DBSession.flush()
print mi.id

rawDescription: SqlAlchemy, generated object Id and Zope transaction


code:
class readonly_dict(dict):
    def __setitem__(self, key, value):
        raise NotImplementedError

exec "print x; x = 3" in readonly_dict(x = 10)

rawDescription: How can I have python notify if an objected is deleted before the script finishes running?

description: Have python notify if an objected is deleted before the script finishes running


code:
x = raw_input('Type a word')
y = raw_input('Type another word')

if x:
    print x[-1] in y

rawDescription: Error for x in range: TypeError: 'type' object is not iterable


code:
from django.forms.fields import FilePathField
a = FilePathField(path='/path')
print a.choices

rawDescription: How to get all available values for FilePathField in Django 1.5? (Resolved)

description: Get all available values for FilePathField in Django 1.5? (Resolved)


code:
#!/usr/bin/env python

class PrintCallInfo:
    def __init__(self,f):
        self.f = f
    def __call__(self,*args,**kwargs):
        print "-->",self.f.__name__,args,kwargs
        r = self.f(*args,**kwargs)
        print "<--",self.f.__name__,"returned: ",r
        return r

# the condition to modify the function...
some_condition=True

def my_decorator(f):
    if (some_condition): # modify the function
        return PrintCallInfo(f)
    else: # leave it as it is
        return f

@my_decorator
def foo():
    print "foo"

@my_decorator
def bar(s):
    print "hello",s
    return s

@my_decorator
def foobar(x=1,y=2):
    print x,y
    return x + y

foo()
bar("world")
foobar(y=5)

rawDescription: Is it possible to replace a function/method decorator at runtime? [ python ]


rawDescription: Iterate dicts and extract values without a for loop


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector

class DmozSpider(CrawlSpider):
    name = "dmoz"
    allowed_domains = ["dmoz.org"]
    start_urls = [
        "http://www.dmoz.org/"
    ]

    rules = (
        Rule(
            SgmlLinkExtractor(allow_domains=("dmoz.org",)),
            callback='parse_page', follow=True
        ),
    )

    def parse_page(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.select('//ul[@class="directory-url"]/li/a/text()').extract()
        for t in titles:
            print "Title: ", t

rawDescription: Browse and scrape web pages recursively

description: Browse and scrape web pages recursively


code:
def parse_date(c):
    d = c["date"]
    print "%04d-%02d-%02d %02d:%02d:%02d" % tuple(map(str, (d["year"], d["month"], d["day"], c["hour"], c["minute"], c["second"])))

rawDescription: python parse java calendar to isodate

description: Python parse java calendar to isodate


code:
db=[]
file=open(filePath,'r')
for line in file:
    skill = []
    for word in line.split(','):
        skill.append(itDictionary[word.strip()])
    db.append(skill)
print db

rawDescription: Python List of Lists

description: Python List of Lists


code:
from xml.dom.minidom import parse, parseString
import os, glob, re
import cherrypy
class Root(object):
    def index(self):
        path = 'C:\Vestigo\XML'

        TOTALXML = len(glob.glob(os.path.join(path, '*.xml')))
        print TOTALXML
        i = 0

        for XMLFile in glob.glob(os.path.join(path, '*.xml')):
            xmldoc = parse(XMLFile)
            order_number = xmldoc.getElementsByTagName('Extrinsic')[0].firstChild.data
            order_name = xmldoc.getElementsByTagName('DeliverTo')[0].firstChild.data
            street1 = xmldoc.getElementsByTagName('Street1')[0].firstChild.data
            state = xmldoc.getElementsByTagName('State')[0].firstChild.data
            zip_code = xmldoc.getElementsByTagName('PostalCode')[0].firstChild.data
            OUTPUTi = order_number+' '+order_name+' '+street1+' '+state+' '+zip_code
            i += 1
            print OUTPUTi
        return (OUTPUTi, """

Quit""")
    index.exposed = True

    def exit(self):
        raise SystemExit(0)
    exit.exposed = True

def start():
    import webbrowser
    cherrypy.tree.mount(Root(), '/')
    cherrypy.engine.start_with_callback(
        webbrowser.open,
        ('http://localhost:8080/',),
        )
    cherrypy.engine.block()

if __name__=='__main__':
    start()

rawDescription: Return XML Values from Multiple Files in Python?


code:
import argparse

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
               help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
               const=sum, default=max,
               help='sum the integers (default: find the max)')

args = parser.parse_args()
print args.accumulate(args.integers)

rawDescription: Infinite Amount of Command Line Arguments in Python


code:
marked = [0] * 2000000     # <- just set up the list
value = 3                  # <- starting at 3 then checking only odds
s = 2                      # <- BUT include 2 since its the only even prime
while value < 2000000:
    if marked[value] == 0: # <- if number at index value is 0 it's prime
        s += value         #    so add value to s (the sum)
        i = value          # <- now mark off future numbers that are multiples of
        while i < 2000000: #    value up until 2mil
            marked[i] = 1  # <- number @ index i is a multiple of value so mark
            i += value     # <- increment value each time (looking for multiples)
    value += 2             # <- only check every odd number
print s

rawDescription: Explanation needed for sum of prime below n numbers


code:
import json, urllib
import pprint
pp = pprint.PrettyPrinter(indent=1, width=80)

front_url = "http://chroniclingamerica.loc.gov/search/titles/results/?city=&rows="
number_rows = "1"
middle_url = "&terms=&language=&lccn=&material_type=&year1=1690&year2=2016&labor=&county=&state=&frequency=&ethnicity=&page="
page = "1"
end_url = "&sort=relevance&format=json"

url = front_url + number_rows + middle_url + page + end_url

response = urllib.urlopen(url)
data = json.loads(response.read())

pp.pprint(data['items'][0]) # [0] to get the first item
print  data['items'][0]['essay'] # get the essay element of the first item
print  data['items'][0]['country'] # get the country element of the first item

rawDescription: Get JSON object from string in python

description: Get JSON object from string in python


code: print cgi.FieldStorage.__init__.__doc__

rawDescription: Getting empty form fields with Python cgi stdlib


code:
import os
filepath = '/path/to/file'
modifytime1 = os.path.getmtime(filepath)
fp = open(filepath)
modifytime2 = os.path.getmtime(filepath)
if modifytime1 != modifytime2:
    print "File modified after opening"

rawDescription: Can I get File Modification Time from a file open for reading (python)

description: Get File Modification Time from a file open for reading (python)


code:
from functools import wraps

class Decorator():

    def __init__(self, something=None): # you are missing the __ on the right
            self.something = something

    def __call__(self, func):
            print 'Here1'
            @wraps(func)
            def _wrapper(*args, **kwargs):
                return self.call(func, *args, **kwargs)
            return _wrapper

    def call(self, func, *args, **kwargs):
            print 'Here2'
            result = func(*args, **kwargs) # result was misspelled
            return result

if __name__ == '__main__':

    @Decorator() # Just a bit cleaner
    def do_the_thing(arg1='argOne', arg2='argTwo'):
            print 'Here3 {0} {1}'.format(arg1, arg2)

    do_the_thing() # func was never called.

rawDescription: What is the correct way to define a python decorator in a class?


code:
import csv

sample  = open("sampleData.txt", "r")
csv1 = csv.reader(sample, delimiter = ',')

sorted_list = []

for line in csv1:
    print '-- ORIG:', line
    tmp = sorted( [int(i) for i in line[1:]], reverse=True )  # eg: [99,78,56]
    stat_list = [round(sum(tmp)/float(len(tmp)), 2), min(tmp), max(tmp)]
    sorted_list.append( [line[0]] + tmp + stat_list )

for s in sorted_list: print '** NEW: ', s # has ['Fred',99,78,56,78.0,57,99]

rawDescription: Finding Min and Max of list generated from CSV file Python


rawDescription: Extracting an element and converting it to datetime


code:
from win32com.client import gencache
mod = gencache.GetModuleForProgID("JMP.Application")
app = mod.Application()
table = app.GetTableHandleFromName("Table1")
same_table = app.GetTableHandleFromName("Table1")
print table is same_table, table._oleobj_ is same_table._oleobj_
print table==same_table, table._oleobj_==same_table._oleobj_
# False, False
# True, True

rawDescription: getting underlying OLE object identity for win32com automation objects

description: Getting underlying OLE object identity for win32com automation objects


code:
ret = os.system('ls -l')
print ret

rawDescription: Checking the status of a process generated by os.system


code:
for row in csv.reader(csvfile):
    if row[0] == "fac2a47adace059aff113283a03f6760":
        print row[1]
        break

rawDescription: Taking a specific range of data in a CSV file (Python)


code:
import glob
import os
extension = ".gif"
drive = "/mnt/ssd0/"
dir_glob = "**/[0-9]/*"
paths = glob.glob(drive + dir_glob + extension)
if paths: # if the list is not empty
    print os.path.getsize(paths[0])

rawDescription: How to find the size of a partially known name file in a partially known name folder in a known drive

description: Find the size of a partially known name file in a partially known name folder in a known drive


code:
def func(obj):
    if not isinstance(obj,list):
        obj = [obj]
    for o in obj:
        print o

rawDescription: Printing out a list of unknown size in a function in python


code:
import math

mymath = dict()

for fun in ["sin", "cos"]:
    def _impl(val):
        print "calculating: %s" % fun
        return getattr(math, fun)(val)
    mymath[fun] = _impl

fun = 'tan'
# will print and calculate tan
print mymath["cos"](math.pi)

rawDescription: Understanding Python Closures

description: Understanding Python Closures


code:
def partition(A, start, end):
    pivot = A[end]
    pindex = start
    for i in range(start, end):
        if A[i] <= pivot:
            A[i], A[pindex] = A[pindex], A[i]
            pindex += 1

    A[pindex], A[end] = A[end], A[pindex]
    return pindex

def quick_sort(A, start, end):

    if start < end:
        pindex = partition(A, start, end)
        quick_sort(A, start, pindex - 1)
        quick_sort(A, pindex + 1, end)

    return A

print quick_sort([10, 1, 2, 1, 1, 1], 0, 5)

rawDescription: What partition algorithm does this use? (for quicksort)


code:
s="This | is || token || some ||| text"
items=s.split()
items
for n,i in enumerate(items):
    if "|" in i and i.count("|")!=2:
         items[n]=i.replace("|","\|")
print ' '.join(items)

rawDescription: Regular expression to ignore a certain number of character repetitions


code:
import os
import sys

# Import System libraries
import re
import urllib2

# Import Custom libraries
from BeautifulSoup import BeautifulSoup, Tag

job_location = lambda x: x.name == "div" and set([(u"id", u"content")]) <= set(x.attrs)
job_title_location = lambda x: set([(u"class", u"schema_title"), (u"itemprop", u"title")]) <= set(x.attrs)
organ_location = lambda x: set([(u"class", u"schema_hiringorganization"), (u"itemprop", u"name")]) <= set(x.attrs)
details_key_location = lambda x: x.name == "div" and bool(re.search("s.*heading", dict(x.attrs).get(u"class", "")))

def coll_up(ilist,base=0,count=0):
    '''
    Recursively collapse nested lists at depth base and above
    '''
    tlist = []
    if(isinstance(ilist,list) or isinstance(ilist,tuple)):
        for q in ilist:
            tlist += coll_up(q,base,count+1)
    else:
        if(base > count):
            tlist = ilist
        else:
            tlist = [ilist]
    return [tlist] if((count != 0) and (base > count)) else tlist

def info_extract(ilist, count=0):
    '''
    Recursively walk a nested list and upon finding a non iterable, return its string
    '''
    tlist = []
    if(isinstance(ilist, list)):
        for q in ilist:
            if(isinstance(q, Tag)):
                tlist += info_extract(q.contents, count+1)
            else:
                extracted_str = q.strip()
                if(extracted_str):
                    tlist += [extracted_str]
    return [tlist] if(count != 0) else tlist

def main():
    url = "http://www.sarkari-naukri.in/jobs-by-qualification/b-tech/sub-centre-manager.html"
    data = urllib2.urlopen(url).read()
    soup = BeautifulSoup(data)

    job_tags = soup.findAll(job_location)
    if(job_tags):
        job_tag = job_tags[0]
        job_title = info_extract(job_tag.findAll(job_title_location))[0]
        organ = info_extract(job_tag.findAll(organ_location))[0]
        details = coll_up(info_extract(job_tag.findAll(details_key_location)), 2)

        combined_dict = dict([tuple(["Job Title:"] + job_title)] + [tuple(["Organisation:"] + organ)] + [tuple(detail) for detail in details])
        combined_list = [["Job Title:"] + job_title, ["Organisation:"] + organ] + details
        postdata = [" ".join(x) for x in combined_list]
        print postdata

        fname = "postdata.txt"
        with open(fname, "w") as outf:
            outf.write("\n".join(postdata).encode("utf8"))

if __name__=="__main__":
    main()

rawDescription: Parse data using BeautifulSoup in python


code:
import sys
import time
import atexit
import signal
import subprocess
from functools import partial
from os.path import join, dirname, realpath

pc_tunnel_command = '-ssh -batch -pw xxxx -N -L 1234:host1:5678 user@host2'

def handle_exit(p, *args):
    print("killing it")
    p.terminate()
    sys.exit(0)

if __name__ == '__main__':
    p = subprocess.Popen(join(dirname(realpath(__file__)), 'plink.exe ') + pc_tunnel_command, shell=True)
    func = partial(handle_exit, p)
    signal.signal(signal.SIGTERM, func)
    atexit.register(func)
    print 'Should die now'

rawDescription: Daemon thread launching software won't die


code:
def parsePage(self, response):
    hxs = HtmlXPathSelector(response)
    urls_list_odd_id = hxs.select('//table[@class="iCIMS_JobsTable"]/tr/td[@class="iCIMS_JobsTableOdd iCIMS_JobsTableField_1"]/a/@href').extract()
    print urls_list_odd_id,">>>>>>>odddddd>>>>>>>>>>>>>>>>"
    urls_list_even_id = hxs.select('//table[@class="iCIMS_JobsTable"]/tr/td[@class="iCIMS_JobsTableEven iCIMS_JobsTableField_1"]/a/@href').extract()
    print urls_list_odd_id,">>>>>>>Evennnn>>>>>>>>>>>>>>>>"

    if not urls_list_odd_id or not urls_list_odd_id:
        from scrapy.shell import inspect_response
        inspect_response(response)

    urls_list = []
    urls_list.extend(urls_list_odd_id)
    urls_list.extend(urls_list_even_id)
    for i in urls_list:
        yield Request(i.encode('utf-8'), callback=self.parseJob)

rawDescription: Scrapy is not downloading the tags from the responses


code:
# Did this in Python 2.7
import re

# Assume your input is in this file
INPUT_FILE = 'text.txt'

# Regex to only pay attention to lines starting with Ib
# and capture the port info
regex = re.compile(r'^(Ib\s*\S+)')
result = [] # Store results in a list
with open(INPUT_FILE, 'r') as theFile:
  for line in theFile:
    match = regex.search(line)
    if not match: continue
    result.append(match.group())
print result

rawDescription: How to parse a CLI command output (table) in python?

description: Parse a CLI command output (table) in python


code:
# Store the lines in an array, this way you don't to reopen the file.
f = open('filename')
lines = f.readlines()

for line in lines:
    if line.split()[4] == 'D':
        print line

for line in lines:
    if line.split()[4] == 'E':
        print line

for line in lines:
    if line.split()[4] == 'A':
        print line

rawDescription: How to sort lines with respect to user defined specific order for one item?

description: Sort lines with respect to user defined specific order for one item


code:
if __name__ == '__main__':
    if len(sys.argv) > 1:
        folder = sys.argv[1]
    else:
        print "Syntax: %s folder" % sys.argv[0]
        sys.exit(1)
    folder = os.path.realpath(folder)
    if not os.path.isdir(folder):
        print "%s is not a folder. Terminating." % (folder,)
        sys.exit(1)
    # walk all directories inside `folder`
    for (dirname, subdirs, files) in os.walk(folder):
        subdir = os.path.realpath(dirname)
        if not os.path.isdir(subdir):
            continue
        reactor.callWhenRunning(process_folder, subdir)
        reactor.run()

rawDescription: how can i make u1-publish-folder (Ubuntu One) recursive into each folder

description: Make u1-publish-folder (Ubuntu One) recursive into each folder


code:
import Pyro4, time

global ns
global casetester

def connect():
    global ns
    global casetester
    ns = Pyro4.locateNS()
    casetester = Pyro4.Proxy("PYRONAME:thetest")

def disconnect():
    global ns
    global casetester
    del ns
    del casetester

while True:
    print "Accessing remote object:"
    connect()
    print casetester.askvalue(1)
    disconnect()
    print "staying busy"
    time.sleep(3)

rawDescription: Pyro4 does not allow more than two clients to access one URI


code:
import sys, os
from PyQt4 import QtCore, QtGui
app=QtGui.QApplication(sys.argv)

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self):
        QtCore.QAbstractTableModel.__init__(self)
        self.items=['One','Two','Three','Four','Five','Six','Seven']

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self.items)
    def columnCount(self, index=QtCore.QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid() or not (0<=index.row()<len(self.items)):
            return QtCore.QVariant()

        item=str(self.items[index.row()])

        if role==QtCore.Qt.DisplayRole:
            return item
        else:
            return QtCore.QVariant()

class MySortFilterProxyModel(QtGui.QSortFilterProxyModel):
    def __init__(self):
        super(MySortFilterProxyModel, self).__init__()
        self.cb_status=True

    def cbChanged(self, arg=None):
        self.cb_status=arg
        print self.cb_status
        self.invalidateFilter()

    def filterAcceptsRow(self, sourceRow, sourceParent):
        print_when_odd_flag = self.cb_status
        is_odd = True
        index = self.sourceModel().index(sourceRow, 0, sourceParent)
        print "My Row Data: %s" % self.sourceModel().data(index, role=QtCore.Qt.DisplayRole)

        if (sourceRow + 1) % 2 == 0:
            is_odd = False

        if print_when_odd_flag:
            if is_odd:
                return True
            else:
                return False
        else:
            if not is_odd:
                return True
            else:
                return False

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        mainLayout=QtGui.QHBoxLayout()
        self.setLayout(mainLayout)

        self.viewA=QtGui.QTableView()
        self.viewA.horizontalHeader().setResizeMode(QtGui.QHeaderView.Stretch)

        self.myModel=TableModel()

        self.sortModel = MySortFilterProxyModel()
        self.sortModel.setSourceModel(self.myModel)

        self.viewA.setModel(self.sortModel)

        self.checkBox=QtGui.QCheckBox("Show All")
        self.checkBox.stateChanged.connect(self.sortModel.cbChanged)
        self.checkBox.setChecked(self.sortModel.cb_status)

        mainLayout.addWidget(self.viewA)
        mainLayout.addWidget(self.checkBox)
        self.show()

view=Window()
sys.exit(app.exec_())

rawDescription: How to refresh QTableView when it is driven by model

description: Refresh QTableView when it is driven by model


code:
from __future__ import with_statement # needed in python 2.5 and earlier
import re
from itertools import ifilter

def count_root(file, regex=re.compile('root')):
   count = 0
   with open(file, 'r') as src:
       for i in ifilter(regex.search, src):
           count += 1
   return count

print count_root('file')

rawDescription: Python reading file

description: Python reading file


code:
class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.resize(640,480)

        menuBar = self.menuBar()
        menu = menuBar.addMenu("&File")
        action = menu.addAction("&Save", self.doAction)
        action.setShortcuts(QtGui.QKeySequence.Save)

        self.view = QtGui.QTableWidget(5,5)
        self.setCentralWidget(self.view)

    def doAction(self):
        print "Save"

rawDescription: How to pass along menu shortcuts from child widget holding focus in PyQt4?

description: Pass along menu shortcuts from child widget holding focus in PyQt4


code:
unicode_string = bytes.decode("utf-8")
print len(unicode_string)

rawDescription: python get unicode string size

description: Python get unicode string size


code:
import os

basedir = "V:/Inbox/"
dir = os.listdir(basedir)

for item in dir:
    itempath = os.path.join(basedir, item)
    if os.path.isdir(itempath):
        print "is Directory"
    else:
        print "is file"

rawDescription: Python not returning is directory value

description: Python not returning is directory value


code:
import itertools
list1 = ['event1','event2']   #up to event-n
list2 = ['desc1', 'desc2']    #up to desc-n
payload = {
   "outer_key" : []
}
for f, b in itertools.izip(list1, list2):
     payload["outer_key"].append({"key1" : f, "key2" : b})
print payload

rawDescription: iterate over multiple list and use it as a value growing dictionary

description: Iterate over multiple list and use it as a value growing dictionary


code:
class DuplicateFilterMiddleware(object):

    def __init__(self):
        connection = pymongo.Connection('localhost', 12345)
        self.db = connection['my_db']
        self.db.authenticate('scott', '*****')

        self.url_sets = {}

    def process_request(self, request, spider):
        if not self.url_sets.get(spider._site_name):
            self.url_sets[spider._site_name] = self.db.ad.find({'site': spider._site_name}).distinct('url')

        print "%s - process Request URL: %s" % (spider._site_name, request.url)
        if request.url in self.url_sets[spider._site_name]:
            raise IgnoreRequest("Duplicate --db-- item found: %s" % request.url)
        else:
            return None

rawDescription: Scrapy - Get spider variables inside DOWNLOAD MIDDLEWARE __init__


code:
for i in range(5):
    print i

range(5)
for item in [0, 1, 2, 3, 4]:
    print item

rawDescription: What does for item in list loop represent?


code:
import os
import sys
import time
import captouch as MPR121

# Create MPR121 instances
cap1 = MPR121.MPR121()
cap2 = MPR121.MPR121()
cap3 = MPR121.MPR121()
cap4 = MPR121.MPR121()

# Initialize communication with all 4 MPR121s
cap1.begin( 0x5a )
cap2.begin( 0x5b )
cap3.begin( 0x5d )
cap4.begin( 0x5c )

# Define send2Pd function
def send2Pd (message=' '):
    os.system("echo '" + message + "' | pdsend 3000");

# Start loop
while True:

    # Filtered and baseline data are commented out
    # filtered = [cap1.filtered_data(i) for i in range(12)]
    # print 'Filt:', '\t'.join(map(str, filtered))
    # base = [cap1.baseline_data(i) for i in range(12)]
    # print 'Base:', '\t'.join(map(str, base))

    # Difference for all 4 boards are calculated, printed in terminal and sent to Pure Data
    diff1 = [cap1.baseline_data(i)-cap1.filtered_data(i) for i in range(12)]
    print 'Diff1:', '\t'.join(map(str, diff1))
    send2Pd ('diff1: ' +  '\t'.join(map(str, diff1)) + ';')

    diff2 = [cap2.baseline_data(i)-cap2.filtered_data(i) for i in range(12)]
    print 'Diff2:', '\t'.join(map(str, diff2))
    send2Pd ('diff2: ' +  '\t'.join(map(str, diff2)) + ';')

    diff3 = [cap3.baseline_data(i)-cap3.filtered_data(i) for i in range(12)]
    print 'Diff3:', '\t'.join(map(str, diff3))
    send2Pd ('diff3: ' +  '\t'.join(map(str, diff3)) + ';')

    diff4 = [cap4.baseline_data(i)-cap4.filtered_data(i) for i in range(12)]
    print 'Diff4:', '\t'.join(map(str, diff4))
    send2Pd ('diff4: ' +  '\t'.join(map(str, diff4)) + ';')

    # Short pause before repeating loop
    time.sleep(0.1)

rawDescription: Python - pdsend datastream

description: Python - pdsend datastream


rawDescription: convert items within a tuple/list

description: Convert items within a tuple/list


code:
import os
for e in os.environ:
    if 'EMACS' in e:
        print e, os.environ[e]

rawDescription: how does a python program tell if it's being run within emacs?

description: How does a python program tell if it's being run within emacs


code:
def find(x):
    srch=str(x)
    x=urllib2.urlopen("http://dictionary.reference.com/browse/"+srch+"?s=t")
    x=x.read()
    items=re.findall('<meta name="description" content="'+".*$",x,re.MULTILINE)
    for x in items:
        y=x.replace('<meta name="description" content="','')
        z=y.replace(' See more."/>','')
        m=re.findall('at Dictionary.com, a free online dictionary with pronunciation,              synonyms and translation. Look it up now! "/>',z)
        if m==[]:
            if z.startswith("Get your reference question answered by Ask.com"):
                print "Word not found! :("
            else:
                print z
    else:
            print "Word not found! :("
x=raw_input("Enter word to find: ")
find(x)

rawDescription: Python define a word?

description: Python define a word


code:
import time
import urllib2

URL = 'http://twitter.com'

def request_time():
    start_time = time.time()
    urllib2.urlopen(URL).read()
    end_time = time.time()
    return end_time - start_time

def throttling_test(n):
    """Test if processing more than n requests is throttled."""
    experiment_start = time.time()
    for i in range(n):
        t = request_time()
        print 'Request #%d took %.5f ms' % (i+1, t * 1000.0)
    print '--- Throttling limit crossed ---'

    t = request_time()
    print 'Request #%d took %.5f ms' % (n+1, t * 1000.0)

throttling_test(3)

rawDescription: How to test a web API throttle limit in Python

description: Test a web API throttle limit in Python


code:
from bs4 import BeautifulSoup

page = "Hello world
Nice to see you
noHello babe"

soup = BeautifulSoup(page)

while len(soup.find_all('span')) > 0:
    soup.span.extract()
print soup

rawDescription: Remove certain type of tag from HTML (without string operations) with Python and BeautifulSoup


code:
import os

class Foo:
    def __init__(self):
        self.myLoc = os.path.abspath(__file__)

f = Foo()
print f.myLoc

rawDescription: Find location of python module source file from within class of module


code:
for line in file:
    line = line.strip()
    llist = line.split()

    if llist and llist[0] == 'NUMS':
        for line in file:
            line = line.strip()
            if line == 'END':
                break
            else:
                print line,
        print # newline

rawDescription: How to read ahead in a file when looping through it with Python?

description: Read ahead in a file when looping through it with Python


code:
# emails is a set of emails
for line in fileinput.input("csvfile.csv",inplace =1):
    parts = line.split(',')
    if parts[3] not in emails:
        print line

rawDescription: How to get the difference between two list based on substrings withing each string in the seperate lists

description: Get the difference between two list based on substrings withing each string in the seperate lists


code:
import HTMLParser
h = HTMLParser.HTMLParser()
s = h.unescape('© 2010')
s
print s
s = h.unescape('© 2010')
s

rawDescription: Parsing malformed string in python

description: Parsing malformed string in python


code:
with open("test.txt") as file:
    for line in file:
        line = line.rstrip()
        if line:
            print line

rawDescription: How do i remove trailing whitespace in python code , using python script

description: Remove trailing whitespace in python code , using python script


code:
from tornado.ioloop import IOLoop
import asyncmongo
db = asyncmongo.Client(pool_id='mydb', host='127.0.0.1', port=27017, maxcached=10, maxconnections=50, dbname='test')
def callback(result, error):
    print result
    IOLoop.instance().stop()
db.command('distinct', 'my_data', key='my_key', callback=callback)
IOLoop.instance().start()

rawDescription: Return distinct keys from MongoDB using asyncmongo


code:
import markdown
content = '''\
Footnotes[^1] have a label[^@#$%] and the footnote's content.

[^1]: This is a footnote content.
[^@#$%]: A footnote on the label: @#$%.
'''
print markdown.markdown( content, extensions=['footnotes'] )

rawDescription: Python Markdown with footnotes

description: Python Markdown with footnotes


code:
def listFrom(here):
    print "[DBG] here: " + here

def book(here, there, amount):
    print "[DBG] here: " + here + "; there: " + there + "; amount: " + str(amount)

book('LON', 'MAN', 8)
input = """book('LON', 'MAN', 8)"""
eval(input, {"__builtins__": {}, "listFrom": listFrom, "book": book})
eval("""listFrom('LON')""", {"__builtins__": {}, "listFrom": listFrom, "book": book})

rawDescription: Parameters not getting passed properly

description: Parameters not getting passed properly


code:
import urllib2
from bs4 import BeautifulSoup  # NOTE: importing beautifulsoup4 here

page = urllib2.urlopen("http://f10.5post.com/forums/showthread.php?t=1142017")
soup = BeautifulSoup(page, "html5lib")

print soup

rawDescription: Tags are converted to HTML entities?

description: Tags are converted to HTML entities


code:
from numpy import matrix
from numpy import linalg
A = matrix( [[1,2,3],[11,12,13],[21,22,23]]) # Creates a matrix.
x = matrix( [[1],[2],[3]] )                  # Creates a matrix (like a column vector).
y = matrix( [[1,2,3]] )                      # Creates a matrix (like a row vector).
print A.T                                    # Transpose of A.
print A*x                                    # Matrix multiplication of A and x.
print A.I                                    # Inverse of A.
print linalg.solve(A, x)     # Solve the linear equation system.

rawDescription: How to represent matrices in python

description: Represent matrices in python


code:
from collections import OrderedDict
packets = [
    ('S1', 'A'), ('S2', 'A'), ('S1', 'B'),
    ('S2', 'B'), ('S1', 'A'), ('S1', 'B')
]

# seen_packets = OrderedDict() # if order is required
seen_packets = dict()

for ssid, mac in packets:
    print "Considering SSID ", ssid, " and MAC ", mac
    ssid_mac = (ssid, mac)
    if ssid_mac in seen_packets:
        print "Seen this before - not adding"
    else:
        data_for_packet = True # your own data here (timestamp?)
        print "Never seen this SSID/MAC combo before"
        seen_packets[ssid_mac] = data_for_packet

print "Unique ssid,mac pairs with data:"
for (ssid, mac), data in seen_packets.iteritems():
    print ssid, mac, data

rawDescription: Stuck framing validation condition in python


code:
parentKey = winreg.OpenKey(root, dir)
i = 0
while True:
   try:
       key = winreg.EnumKey(parent, i)
       print key
       i += 1
   except WindowsError:
       break

rawDescription: Python how to get folder name in registry


code:
from stat import *
import os
import time
import shutil
import sys

for file in os.listdir(sys.argv[1]):
  ct = time.localtime()
  datestamp_on_filename = file.split('_')[1].split('.')[0]
  current_date_in_ddmmyy = str(ct.tm_mday) + (('0' + str(ct.tm_mon)) if ct.tm_mon < 10 else str(ct.tm_mon)) + str(ct.tm_year)[2:]
  if datestamp_on_filename != current_date_in_ddmmyy:
    print 'moving ' + file
    shutil.move(sys.argv[1] + "/" + file, 'archived')

rawDescription: Python - if directory contains file beginning with...move file


code:
def fetch_url(i, logfile):
    page = urllib2.urlopen("http://www.nydailynews.com/search-results/search-results-7.113?kw=&tfq=&afq=&page={}&sortOrder=Relevance&selecturl=site&q=the&sfq=&dtfq=seven_years".format(i))
    soup = BeautifulSoup(page.read())
    snippet = soup.find_all('h2')
    for h2 in snippet:
        for link in h2.find_all('a'):
            logfile.write("http://www.nydailynews.com" + link.get('href') + "\n")
    print "finished another url from page {}".format(i)

with open("dailynewsurls.txt", 'a') as logfile:
    threads = []
    for i in xrange(15500, 6100, -1):
        t = threading.Thread(target=fetch_url, args=(i, logfile))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()

rawDescription: Is this actually using threading to scrape for urls?


rawDescription: Remove character between two characters in Python


code:
for num in iter(lambda: int(raw_input('Your number is: ')), 0):
    print 'You entered', num

rawDescription: Accessing user input taken in while loop (Python)


code:
for x in numbers:
  if x == 237:
    break
  elif x % 2 == 0:
    print x

rawDescription: Print all even numbers in a list until a given number


code:
zoom=5
while True:
    where = raw_input('Where would you like a map of?')
    try:
        heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom)
        pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom)
    except AttributeError, GeocoderServiceError:
        print "Cannot create map with the given location. Please try again."
    else:
        break
while True:
    zoom_level = {'city':9, 'state':7, 'country':5, 'world':9}
    zoom_detail = raw_input('What level of detail would you like: city, state, country, or world?')
    try:
         zoom = zoom_level[str(zoom_detail)]
    except TypeError, IndexError:
        print 'Please enter: city, state, country, or world. Try again'
        continue
    else:
        break

heatmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom)
pointmap = folium.Map(location=geo(where), tiles='Stamen Toner', zoom_start=zoom)

rawDescription: How to error handle properly python

description: Error handle properly python


code:
q = SomeModel.objects.select_related().all()
print q.query

q = SomeModel.objects.all().select_related()
print q.query

rawDescription: order of select_related in chain

description: Order of select_related in chain


code:
print df
print df2
a = df.set_index(['file', df.groupby('file').cumcount()])
print a
b = df2.stack().to_frame('ID')
print b
c = a.join(b).reset_index(1, drop=True).reset_index()
print c

rawDescription: How to align dfs in pandas

description: Align dfs in pandas


code:
mylist = [(A,8), (A,3), (A,3), (A,3)]
def indices( mylist, value):
    return [i for i,x in enumerate(mylist) if x==value]

print indices(mylist, (A,3))
# [1, 2, 3]

rawDescription: Python: Indexing of Same Values in a List


code:
import re
p = re.compile("[a-z]")
for m in p.finditer('a1b2c3d4'):
    print m.start(), m.group()

rawDescription: Python Regex - How to Get Positions of Matches


code:
fields = [car, club]
for f in fields:
    z = getattr(profile, f)
    print list(z.all())

rawDescription: Loop in ManyToMany Fields in django model


code:
import csv
with open ("ann.csv", "rb") as csvfile:
    reader = csv.reader(csvfile)
    reader.next() # Skip the header row
    collected = []
    for row in reader:
        collected.append(row[0])
    print ",".join(collected)

rawDescription: Extract one column of csv into a comma separated list python


code:
from __future__ import division
import itertools

r = 20

for x in itertools.combinations(range(r + 3), 3):
    print x[0]/r, (x[1]-x[0]-1)/r, (x[2]-x[1]-1)/r, (r+2-x[2])/r

rawDescription: Finding all subsets of four numbers that sum to 1


code:
import Queue
import threading
import time

class ProducerThread(threading.Thread):

    def __init__(self, work_queue, shutdown_event):
        super(ProducerThread, self).__init__()

        self._work_queue     = work_queue
        self._shutdown_event = shutdown_event

    def run(self):
        for i in range(5):
            if self._shutdown_event.is_set():
                return
            else:
                self._work_queue.put(i)

class ConsumerThread(threading.Thread):

    def __init__(self, work_queue, shutdown_event):
        super(ConsumerThread, self).__init__()
        self._work_queue     = work_queue
        self._shutdown_event = shutdown_event

    def run(self):
        while True:
            try:
                data = self._work_queue.get(timeout=1)
            except Queue.Empty:
                if self._shutdown_event.is_set():
                    print 'returning from thread; queue is empty and ' \
                          'shutdown_event.is_set() is True'
                    return
                else:
                    continue
            if self._shutdown_event.is_set():
                print 'returning from thread; shutdown_event.is_set() is True'
                return
            else:
                print "%s got %s. Sleeping for %s seconds. %s" % (
                    self.getName(), data, data, time.time())
                time.sleep(1.0+data/10)
                print "%s woke up from sleep after %s seconds %s" % (
                    self.getName(), str(1.0+data/10), time.time())

if __name__ == '__main__':
    work_queue = Queue.Queue(maxsize = 1)
    shutdown_event = threading.Event()
    producer = ProducerThread(work_queue, shutdown_event)
    consumer = ConsumerThread(work_queue, shutdown_event)
    producer.start()
    consumer.start()
    p
    time.sleep(10)
    print 'MainThread: calling shutdown_event.set()'
    shutdown_event.set()
    producer.join()
    consumer.join()

rawDescription: remove the use of shouldStop variable in python producer thread

description: Remove the use of shouldStop variable in python producer thread


code:
#!/usr/bin/env python
import ctypes, re, sys

## Partial interface to ptrace(2), only for PTRACE_ATTACH and PTRACE_DETACH.
c_ptrace = ctypes.CDLL("libc.so.6").ptrace
c_pid_t = ctypes.c_int32 # This assumes pid_t is int32_t
c_ptrace.argtypes = [ctypes.c_int, c_pid_t, ctypes.c_void_p, ctypes.c_void_p]
def ptrace(attach, pid):
    op = ctypes.c_int(16 if attach else 17) #PTRACE_ATTACH or PTRACE_DETACH
    c_pid = c_pid_t(pid)
    null = ctypes.c_void_p()
    err = c_ptrace(op, c_pid, null, null)
    if err != 0: raise SysError, 'ptrace', err

pid = "18396"

ptrace(True, int(pid))
maps_file = open("/proc/"+pid+"/maps", 'r')
mem_file = open("/proc/"+pid+"/mem", 'r', 0)
for line in maps_file.readlines():  # for each mapped region
    m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line)
    if m.group(3) == 'r':  # if this is a readable region
        start = int(m.group(1), 16)
        end = int(m.group(2), 16)
        mem_file.seek(start)  # seek to region start
        chunk = mem_file.read(end - start)  # read region contents
        print chunk,  # dump contents to standard output
maps_file.close()
mem_file.close()
ptrace(False, int(pid))

rawDescription: Python read Linux memory process error (/proc/$pid/mem)


code:
import urllib2
import httplib

class SmartRedirectHandler(urllib2.HTTPRedirectHandler):
    """
    Stolen from http://www.diveintopython.net/http_web_services/redirects.html
    Originally defined in openanything.py

    """
    def http_error_301(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_301(
            self, req, fp, code, msg, headers)
        result.status = code
        return result

    def http_error_302(self, req, fp, code, msg, headers):
        result = urllib2.HTTPRedirectHandler.http_error_302(
            self, req, fp, code, msg, headers)
        result.status = code
        return result

inputURLs = ['http://apple.co/1ka9kIX', 'http://fb.com']
httplib.HTTPConnection.debuglevel = 1
opener = urllib2.build_opener(SmartRedirectHandler())

getRedirect = lambda url: opener.open(urllib2.Request(url)).url

print map(getRedirect, inputURLs)

rawDescription: Get list of redirect urls

description: Get list of redirect urls


code:
import os
l=["sets","relation_and_functions","Trig","Complex_Quad","Linear_inequalities","Permutation","Binomial","Sequence","Straight","conic","Three_d","Limit_Derivative","Stats","Probability","Math_reasoning"]
r=["https://www.youtube.com/playlist?list=PLD5EF274490578CC4","https://www.youtube.com/playlist?list=PL548FAD237A4B6D2E","https://www.youtube.com/playlist?list=PL42123C3873AED16F","https://www.youtube.com/playlist?list=PL880E3116D67E42FD","https://www.youtube.com/playlist?list=PLC5D1199BAE318878","https://www.youtube.com/playlist?list=PL812413BD6B55AA6E","https://www.youtube.com/playlist?list=PL78FAFAFA8496BCE1","https://www.youtube.com/playlist?list=PL6F57725E2DA8B557","https://www.youtube.com/playlist?list=PLA243228EA688A835","https://www.youtube.com/playlist?list=PL41B47FB4E23B392A","https://www.youtube.com/playlist?list=PLa2X112u1cdjdl-dsLJC1HaCeGrOarZZz","https://www.youtube.com/playlist?list=PLF6640F0A9F39F7BA","https://www.youtube.com/playlist?list=PL6FDE1AB3AE32E614","https://www.youtube.com/playlist?list=PL7FF5AD1CFF0981E5","https://www.youtube.com/playlist?list=PL13B63CD6FFAB9EA8"]
c=[]
for i in l:
    c.append(i)
print c
for i in range(len(l)):
    q="mkdir "+l[i]
    l[i]=q
for j in range(len(r)):
    z="youtube-dl "+r[j]
    r[j]=z
print c
for i in range(len(l)):
    a=l[i]
    e=c[i]
    b=r[i]
    os.system(a)
    os.chdir(e)
    os.system(b)
    os.chdir("..")

rawDescription: Using python to run bash commands and get Output


code:
from xlrd import open_workbook

def grabexcelfile():
    wb = open_workbook('leg.xls',encoding_override='latin-1')
    log = []
    txt = ''
    for s in wb.sheets():
        for row in range(s.nrows):
            values = []
            for col in range(s.ncols):
                 #next line is changed
                 txt = sheet.cell(row,col).value
                 txt.replace('-',' ',10)
                 log.append(txt)
    return log

x = grabexcelfile()
print type(x[0]),x[0]

rawDescription: How to convert a string with unicode in it to unicode using python

description: Convert a string with unicode in it to unicode using python


code:
html = r'Why Multi-armed Bandit algorithms are superior to A/B testing (with Math), yummyfajitas, 11 comments, '

from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(html) #sort the html
bowl = soup.findAll('a') #find all links in the html

link = bowl[0]['href'] #find the first 'a' tags href
text = bowl[0].contents[0] #find the first tags url
user_id = bowl[1]['href'].split('?id=')[1] #split on '?id=' and take the second  value. could be [-1] too
item_id = bowl[2]['href'].split('?id=')[1]

print 'link:', link
print 'text:', text
print 'user_id:', user_id
print 'item_id:', item_id

rawDescription: How to scrape this using Beautiful Soup in Python?

description: Scrape this using Beautiful Soup in Python


code:
import gtk

d = gtk.Dialog()
d.add_buttons(gtk.STOCK_YES, 1, gtk.STOCK_NO, 2)

label = gtk.Label('Do you like GTK?')
label.show()
d.vbox.pack_start(label)

answer = d.run()

print answer

rawDescription: How to create a pop window using pygtk?

description: Create a pop window using pygtk


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

def TempPressHumid(time_start, time_end, date_start, date_end, df):

    temp = df[date_start:date_end]
    temp = df.between_time(time_start,time_end)

    out = {'temp_avg':np.mean(temp['temp']),
    'temp_std':np.std(temp['temp']),
    'press_avg':np.mean(temp['press']),
    'press_std':np.std(temp['press']),
    'humid_avg':np.mean(temp['humid']),
    'humid_std':np.std(temp['humid'])}
    print out

df = pd.DataFrame({'temp':[random.randint(50, 80) for x in range(51841)],
    'press':[random.randint(20, 40) for x in range(51841)],
    'humid':[random.randint(20, 80) for x in range(51841)]},
    index = pd.date_range(start = '2014-01-01', end = '2014-01-04', freq = '5S'))

TempPressHumid(time_start = '01:00:00', time_end = '23:00:00', date_start = '2014-01-02', date_end = '2014-01-03', df = df)

rawDescription: Selecting a range of time-series data and performing data analysis


rawDescription: Adding values in a tuple that is in a list in python


code: print '\n' + second_name

rawDescription: Python print statement on new line

description: Python print statement on new line


code:
for key, d1_list in dict_1.iteritems():
    for element in d1_list:
        for ind, d2_list in enumerate(dict_2[key]):
            if element in d2_list:
                print element, key, ind

rawDescription: Iterating through two dictionaries

description: Iterating through two dictionaries


code:
for key,group in itertools.groupby(created, key=lambda x: x[1].strftime('%Y%m%d')[:11]):
    print 'Date of signup: ', key
    num_signups = (sum(1 for _ in group))
    print '# of signups: ', num_signups
    temp[key].append(num_signups)

rawDescription: Python defaultdict, counting a list not working


code:
import bisect

def quantize(num, quant):
    mids = [(quant[i] + quant[i + 1]) / 2.0
            for i in xrange(len(quant) - 1)]
    ind = bisect.bisect_right(mids, num)
    return quant[ind]

quantnum = [5, 10, 30, 60, 120, 180]

inputnum = [10, 20, 60, 120]

for n in inputnum:
    print quantize(2 * n, quantnum)

# Output:
#30
#30
#120
#180

rawDescription: "Quantize" when rounding numbers Python 3.2


rawDescription: How to use regular expression to match square brackets of values and store in python dictionary?

description: Use regular expression to match square brackets of values and store in python dictionary


code:
class Vector2D(object):
    def __new__(cls, *args, **kw):
        print "Testing new"
        return super(Vector2D,cls).__new__(cls)

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

    def __str__(self):
        return "X:" + str(self.x) + ",Y:" + str(self.y)

rawDescription: Using __new__ in python

description: Using __new__ in python


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("-c","--config", type=str, help='help text here')
args = parser.parse_args()
if args.config:
    print "the value of config is {}".format(args.config)

rawDescription: how to extract python argparse optional value

description: Extract python argparse optional value


code:
import re

st='''\
The ancient Romans influenced countries and civilizations in the following centuries.
Their language, Latin, became the basis for many other European languages. They stayed in Roma for 3 month.'''

deletions=('and','in','the')
repl={"ancient": "old", "month":"years", "centuries":"years"}

tgt='|'.join(r'\b{}\b'.format(e) for e in deletions)
st=re.sub(tgt,'',st)
for word in repl:
    tgt=r'\b{}\b'.format(word)
    st=re.sub(tgt,repl[word],st)

print st

rawDescription: Remove some words replace some other words from a txt file


code:
@cost_time
def dbdump_all():
    "导出数据库所有数据至当前目录下以年月日命名的sql文件"
    filename=datetime.datetime.now().strftime("%Y-%m-%d")+".sql"
    cmd="""mysqldump -u root -pzhoubt --opt --quick --database search >./%s"""%filename
    print cmd
    p=subprocess.Popen(cmd,shell=True,cwd=os.getcwd())
    sts = os.waitpid(p.pid, 0)[1]
    print "返回状态%s"%sts
    print "已将数据库表结构和数据导出到%s"%filename

rawDescription: Running mysqldump through Python's subprocess module is slow and verbose


code:
def rowAverageSum(filename):
    import numpy as np
    FullMean = 0
    li = [map(int, x) for x in [i.strip().split() for i in open(filename).readlines()]]
    i=0
    while i<len(li):
        for k in li:
            print "Mean of row ",i+1,":",np.mean(k)
            FullMean+=np.mean(k)
            i+=1
    print "***************************"
    print "Grand Average:",FullMean
    print "***************************"

rawDescription: Finding row wise average and average of all rows of space delimited file


code:
b_string = "1100101"
ib_string = ""

for bit in b_string:
  if bit == "1":
    ib_string += "0"
  else:
    ib_string += "1"

print ib_string

rawDescription: Python - Flipping Binary 1's and 0's in a String


code:
x = [1,1,2,2,3]
y = [2,3,1,3,1]

fwd = set( zip(x,y) )
rev = set( zip(y,x) )
print ' not bi: ', fwd.difference(rev)

rawDescription: Directed graph in pandas

description: Directed graph in pandas


code:
import random
my_input = ['beleriand','mordor','hithlum','eol','morgoth','melian','thingol']
def random_generator(array,x,y):
    random.shuffle(array)
    result = []
    count = 0
    while count < x:
        section = []
        y1 = y * count
        y2 = y * (count + 1)
        for i in range (y1,y2):
            section.append(array[i])
        result.append(section)
        count += 1
    return result
print random_generator(my_input,3,2)

rawDescription: Mutually exclusive random sampling from a list. Python


code:
for i in range(len(letters)):
    letter = raw_input()
    index = letters.index(letter)
    blanks = blanks[:index] + letter + blanks[index + 1:]
    print blanks

rawDescription: Python Replace Blanks With Letter

description: Python Replace Blanks With Letter


code:
def cfor(first,test,update):
    while test(first):
        yield first
        first = update(first)

def example(blah):
    print "do some stuff"
    for i in cfor(0,lambda i:i<blah,lambda i:i+1):
        print i
    print "done"

rawDescription: Simulating C-style for loops in python

description: Simulating C-style for loops in python


code:
from twisted.internet import protocol
from twisted.internet import reactor
import os

class Writer(protocol.ProcessProtocol):
  def __init__(self, data):
    self.data = data
  def connectionMade(self):
    print "Writer -- connection made"
    self.transport.writeToChild(0, self.data)
    self.transport.closeChildFD(0)
  def childDataReceived(self, fd, data):
    pass
  def processEnded(self, status):
    pass

class Reader(protocol.ProcessProtocol):
  def __init__(self):
    pass
  def connectionMade(self):
    print "Reader -- connection made"
    pass
  def childDataReceived(self, fd, data):
    print "Reader -- childDataReceived"
    self.received = data
  def processEnded(self, status):
    print "process ended, got:", self.received

class WriteRead(protocol.ProcessProtocol):
  def __init__(self, data):
    self.data = data
  def connectionMade(self):
    self.transport.writeToChild(0, self.data)
    self.transport.closeChildFD(0)
  def childDataReceived(self, fd, data):
    self.received = data
    print "got data:", data
  def processEnded(self, status):
    print "process ended - now what?"

def test1(data):
    # just call wc
    p2 = reactor.spawnProcess(WriteRead(data), "wc", ["wc"], env=None, childFDs={0: "w", 1: "r"})
    reactor.run()

def test2(data):
    rfd, wfd = os.pipe()
    p1 = reactor.spawnProcess(Writer(data), "cat", ["cat"], env=None, childFDs={0:"w", 1: wfd })
    p2 = reactor.spawnProcess(Reader(),     "wc", ["wc", "-w"], env=None, childFDs={0: rfd, 1: "r"})
    os.close(rfd)
    os.close(wfd)
    reactor.run()

test2("this is a test")

rawDescription: Twisted spawnProcess, send output of one process to input of another


code:
import httplib, sys, re

def spider(link, depth=0):
    if(depth > 2): return []

    try:
        conn = httplib.HTTPConnection(link)
        conn.request("GET", "/")
        r2 = conn.getresponse()
        data = r2.read().split('\n')
        links = []
        for x in data[:]:
            if link in x:
                a=''.join(re.findall("href=([^ >]+)",x))
                a=a.translate(None, '"' + "'")
                if a:
                    links.append(a)

        # Recurse for each link
        for link in links:
            links += spider(link, (depth + 1))

        return links

    except:
        exit(1)

print spider("http://www.yahoo.com")

rawDescription: How to return multiple values in python

description: Return multiple values in python


code:
def keyPressEvent(self, event):
    if event.modifiers() == QtCore.Qt.AltModifier:
        if event.key() == QtCore.Qt.Key_A:
            print 'QMainWindow: Alt + a'
    QtGui.QMainWindow.keyPressEvent(self, event)

rawDescription: How to overlay QKeyEvents

description: Overlay QKeyEvents


code:
from scapy.all import *
from datetime import datetime
import csv

PROBE_REQUEST_TYPE = 0
PROBE_REQUEST_SUBTYPE = 4

WHITELIST = ['de:ad:be:ef:ca:fe',] # Replace this with your phone's MAC address

PACKET_FILE_PATH = 'logmacs.csv'

class PacketHandler(object):
    def __init__(self, packet_file_path):
        self.packet_file_path = packet_file_path
        self.handled_packets = set()

    def __enter__(self):
        self.csv_file = open(self.packet_file_path, 'ab')
        self.csv_writer = csv.writer(self.csv_file)
        return self

    def __exit__(self, *exc_info):
        self.csv_file.close()

    def handle_packet(self, pkt):
        if pkt.haslayer(Dot11) and pkt.type == PROBE_REQUEST_TYPE and pkt.subtype == PROBE_REQUEST_SUBTYPE and (pkt.addr2.lower() not in WHITELIST and pkt.addr2.upper() not in WHITELIST) and pkt.addr2 not in self.handled_packets:
            self.add_packet(pkt)

    def add_packet(self, pkt):
        self.handled_packets.add(pkt.addr2)
        try:
            signal_strength = -(256 - ord(pkt.notdecoded[-4:-3]))
        except Exception, e:
            signal_strength = -100
            print "No signal strength found"
        self.csv_writer.writerow([datetime.now().strftime('%Y-%m-%d'),
                                  datetime.now().strftime('%H:%M:%S'),
                                  pkt.addr2,
                                  pkt.getlayer(Dot11ProbeReq).info,
                                  signal_strength])
        print "Added: %s SSID: %s" % (pkt.addr2, pkt.getlayer(Dot11ProbeReq).info)

def main():
    packet_handler = PacketHandler(PACKET_FILE_PATH)
    print "[%s] Starting scan" % datetime.now()
    print "Scanning..."
    with packet_handler as ph:
        sniff(iface=sys.argv[1], prn=ph.handle_packet)

if __name__=="__main__":
    main()

rawDescription: Logging MAC addresses into a csv using scapy and python


code:
import requests
import lxml.html as lh

def gender_genie(text, genre):
    url = 'http://bookblog.net/gender/analysis.php'
    caption = 'The Gender Genie thinks the author of this passage is:'

    form_data = {
        'text': text,
        'genre': genre,
        'submit': 'submit',
    }

    response = requests.post(url, data=form_data)

    tree = lh.document_fromstring(response.content)

    return tree.xpath("//b[text()=$caption]", caption=caption)[0].tail.strip()

if __name__ == '__main__':
    print gender_genie('I have a beard!', 'blog')

rawDescription: Submit data via web form and extract the results


code:
from soup import BeautifulSoup as bs
from scrapy.http import Request
from scrapy.spider import BaseSpider
from hn.items import HnItem

class HnSpider(BaseSpider):
    name = 'hn'
    allowed_domains = []
    start_urls = ['http://news.ycombinator.com']

    def parse(self, response):
        if 'news.ycombinator.com' in response.url:
            soup = bs(response.body)
            items = [(x[0].text, x[0].get('href')) for x in
                     filter(None, [
                         x.findChildren() for x in
                         soup.findAll('td', {'class': 'title'})
                     ])]

            for item in items:
                print item
                hn_item = HnItem()
                hn_item['title'] = item[0]
                hn_item['link'] = item[1]
                try:
                    yield Request(item[1], callback=self.parse)
                except ValueError:
                    yield Request('http://news.ycombinator.com/' + item[1], callback=self.parse)

                yield hn_item

rawDescription: Using HTML parser to get contents of a particular div


code:
places= ["Home","In-n Out Burger", "John's house", "Santa Monica Pier", "Staples center",  "LA Dodgers stadium", "Home" ]

def placesCount(places):
    placesToGo = 0
    for i in range (len(places)):
        if places[i] == "LA Dodgers stadium":
            placesToGo = placesToGo + 1
            print placesToGo, "places to LA dodgers stadium"
        else:
            placesToGo = placesToGo + 1
def multiCount(places):
    spaces = 0
    multiWords = 0
    for j in range (len(places)):
        word = places[j]
        for characters in word:
            if characters == " ":
                spaces = spaces + 1
        if spaces != 0:
            multiWords = multiWords + 1
            spaces = 0
    print multiWords, "multi-word names"

placesCount(places)
multiCount(places)

rawDescription: How to count the #of of items in a list without using python's method while ignoring the items that come after a certain item

description: Count the #of of items in a list without using python's method while ignoring the items that come after a certain item