code:
while count > 0:                         # because of `count -= 1` below,
                                         # will run loop count times

    print "retrieving: {0}".format(url)  # just prints out the next web page
                                         # you are going to get

    page = urllib.urlopen(url)           # urls reference web pages (well,
                                         # many types of web content but
                                         # we'll stick with web pages)

    soup = BeautifulSoup(page)           # web pages are frequently written
                                         # in html which can be messy. this
                                         # package "unmessifies" it

    tag = soup('a')                      # in html you can highlight text and
                                         # reference other web pages with <a>
                                         # tags. this get all of the <a> tags
                                         # in a list

    name = tag[position-1].string        # This gets the <a> tag at position-1
                                         # and then gets its text value

    names.append(name)                   # this puts that value in your own
                                         # list.

    url = tag[position-1]['href']        # html tags can have attributes. On
                                         # and <a> tag, the href="something"
                                         # attribute references another web
                                         # page. You store it in `url` so that
                                         # its the page you grab on the next
                                         # iteration of the loop.
    count -= 1

rawDescription: Following links in python

description: Following links in python


code:
from collections import OrderedDict
d = OrderedDict()
d[2] = 3
d[1] = 4
d[5] = 2
print d
for i in d:
    print i

rawDescription: Dictionary.py def load(): mixes up variables


code:
#Okay lets do this
#your dictionary be:

dict = {'T':'1','U':'2','V':'3'}
#and let the string be:
str = "1do hello2 yes32 1243 for2"

new = ''
for letter in str:
    if letter in dict.values():
            new += dict.keys()[dict.values().index(letter)]
    else:
            new += letter

print new

rawDescription: Python Dictionary: Get associated 'Key' from 'Value'?


code:
from itertools import izip_longest

alist = ['foo','bar','duh']
blist = ['ofo','ardavak','dot','dotdat']

combined_list = list(izip_longest(alist, blist, fillvalue=""))

for (i,j),(iplus1,jplus1) in izip_longest(combined_list, combined_list[1:], fillvalue=("", "")):
    print i,j
    print i,j+jplus1
    print i+iplus1,j
    print i+iplus1,j+jplus1

rawDescription: How to access 2 list at a time while looking ahead with izip_longest? -Python

description: Access 2 list at a time while looking ahead with izip_longest? -Python


code:
io.use_plugin('pil', 'imread')
a = io.imread('C:\Users\Dimitrios\Desktop\polimesa\\arizona.jpg')

B = np.zeros((len(a)/2 +1, len(a[0])/2 +1))

for i in xrange(0, len(a), 2):
    for j in xrange(0, len(a[0]), 2):
        x.append(a[i][j])
        if i+1 < len(a):
            x.append(a[i+1][j])
        if j+1 < len(a[0]):
           x.append(a[i][j+1])
        if i+1 < len(a) and j+1 < len(a[0]):
           x.append(a[i+1][j+1])
        B[i/2][j/2] = np.std(x)
        x[:] = []

rawDescription: Blockproc like function for Python image processing


code:
for tweet in reversed(user_tweets):
    print tweet.text

rawDescription: Retrieving tweets in chronological order using tweepy for python


code: print subprocess.check_output(['fastboot', 'getvar', 'all'], stderr=subprocess.STDOUT)

rawDescription: Output from Python and Android Fastboot

description: Output from Python and Android Fastboot


code:
with open(filename, "r") as text:
    for line in text:
        print line[6:]

rawDescription: Python code to replace specific list of strings


code:
from tkinter import Tk, Canvas
from itertools import product

class Board(Tk):
    def __init__(self, width, height, cellsize):
        Tk.__init__(self)
        self.cellsize = cellsize
        self.canvas = Canvas(self, width=width, height=height)
        self.canvas.bind("<Button-1>", self.onclick)
        self.canvas.pack()
    def draw_rectangle(self, x1, y1, x2, y2, color):
        self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
    def draw_circle(self, x1, y1, x2, y2, color):
        self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
    def onclick(self, event):
        i = int(event.x / self.cellsize)
        j = int(event.y / self.cellsize)
        print "You clicked on cell (%s, %s)" % (i, j)

if __name__=="__main__":
    size = 40
    board = Board(400, 400, size)
    board.title("Draughts")
    logicBoard = [[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
                  [-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
                  [0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
                  [-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                  [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                  [1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
                  [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
                  [1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
    for (i, j) in product(range(10), range(10)):
        coordX1 = (i * size)
        coordY1 = (j * size)
        coordX2 = coordX1 + size
        coordY2 = coordY1 + size
        color = "white" if i%2 == j%2 else "black"
        board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, color)
        cell = logicBoard[i][j]
        if cell != 0:
            pawnColor = "red" if cell > 0 else "blue"
            board.draw_circle(coordX1, coordY1, coordX2, coordY2, pawnColor)
    board.mainloop()

rawDescription: Tkinter, Draughts board

description: Tkinter, Draughts board


code:
import math
from datetime import datetime, timedelta, date
from random import gauss

how_many_responses = 1000
start_date = date(2008, 5, 1)
end_date = date(2008, 6, 1)
num_days = (end_date - start_date).days + 1
timeline = [start_date + timedelta(i) for i in xrange(num_days)]

def weibull(x, k, l):
    return (k / l) * (x / l)**(k-1) * math.e**(-(x/l)**k)

dev = 0.1
samples = [i * 1.25/(num_days-1) for i in range(num_days)]
probs = [weibull(i, 2, 0.5) for i in samples]
noise = [gauss(0, dev) for i in samples]
simdata = [max(0., e + n) for (e, n) in zip(probs, noise)]
events = [int(p * (how_many_responses / sum(probs))) for p in simdata]

histogram = zip(timeline, events)

print '\n'.join((d.strftime('%Y-%m-%d ') + "*" * c) for d,c in histogram)

rawDescription: How do i generate a histogram for a given probability distribution (for functional testing a server)?

description: Generate a histogram for a given probability distribution (for functional testing a server)


code:
class N(object):
  def __init__(self, value):
    self.value = value
n = N(3)
value = n.value
print value, n.value
value = 4
print value, n.value

rawDescription: cross module variable

description: Cross module variable


code:
def oddn(x,y,z):
    odd_number_keeper = []
    for item in [x,y,z]:
        if item % 2==1:
            odd_number_keeper.append(item)
    if not odd_number_keeper:
        print 'No odd number is found'
        return
    return max(odd_number_keeper)

rawDescription: Find out the biggest odd number from three arguments in a function [Python]


code:
def succesfulTrials(List1, List2):
    List3 = {}
    # Grab data
    for item in List2:
        List3[item] = List1.count(item)

    # Print data
    for (key, value) in List3.items():
        print value, key,

rawDescription: Python Merging Ordered Lists' Output

description: Python Merging Ordered Lists' Output


code:
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Tree(QtGui.QTreeWidget):
    def __init__(self, *args, **kwargs):
        super(Tree, self).__init__()
        for name in ['Item_A0', 'Item_A1', 'Item_A2']:
            itemA=QtGui.QTreeWidgetItem([name])

            self.addTopLevelItem(itemA)
            if name=='Item_A1':
                item_B0=QtGui.QTreeWidgetItem(['Item_B0'])
                itemA.insertChild(0, item_B0)
                itemA.insertChild(1, QtGui.QTreeWidgetItem(['Item_B1']))

                item_C0=QtGui.QTreeWidgetItem(['Item_C0'])
                item_B0.insertChild(0, item_C0)
                item_B0.insertChild(0, QtGui.QTreeWidgetItem(['Item_C1']))

        self.resize(360,240)
        self.show()

    def getItemsRecursively(self):
        total=[]
        def serchChildItem(item=None):
            if not item: return
            for m in range(item.childCount()):
                childItem=item.child(m)
                if not childItem: continue
                total.append(childItem)
                serchChildItem(childItem)

        for i in range(self.topLevelItemCount()):
            item=self.topLevelItem(i)
            if not item: continue
            total.append(item)
            serchChildItem(item)
        return total

tree=Tree()
total=tree.getItemsRecursively()
print 'Total number of Tree items: %s'%len(total)
sys.exit(app.exec_())

rawDescription: Recursive TreeItems Search

description: Recursive TreeItems Search


code:
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

class ButtonClickProcessor(object):
    def __init__(self, axes, label):
        self.button = Button(axes, label)
        self.button.on_clicked(self.process)

    def process(self, event):
        print self.button.label

fig = plt.figure()

axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = ButtonClickProcessor(axnext, "Next")

plt.show()

rawDescription: Python Matplotlib callback function with parameters


rawDescription: Python groupby issue

description: Python groupby issue


code:
from PyQt4 import QtCore, QtGui

has_panda = False
try:
  import pandas as pd
  has_panda = True
except:
  pass

class TableModel(QtCore.QAbstractTableModel):
    def __init__(self, parent=None, *args):
        super(TableModel, self).__init__()
        self.datatable = None
        self.headerdata = None

    def update(self, dataIn):
        print 'Updating Model'
        self.datatable = dataIn
        print 'Datatable : {0}'.format(self.datatable)
        if has_panda:
          headers = dataIn.columns.values
        else:
          headers = dataIn.columns
        header_items = [
                    str(field)
                    for field in headers
        ]
        self.headerdata = header_items
        print 'Headers'
        print self.headerdata

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

    def columnCount(self, parent=QtCore.QModelIndex()):
        if has_panda:
          return len(self.datatable.columns.values)
        else:
          return len(self.datatable.columns)

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role == QtCore.Qt.DisplayRole:
            i = index.row()
            j = index.column()
            return QtCore.QVariant('{0}'.format(self.datatable.iget_value(i, j)))
        else:
            return QtCore.QVariant()

    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        if index.column() == 4:
            self.datatable.iset_value(index.row(), 4, value)
            return value
        return value

    def headerData(self, col, orientation, role):
        if orientation == QtCore.Qt.Horizontal and role == QtCore.Qt.DisplayRole:
            return '{0}'.format(self.headerdata[col])

    def flags(self, index):
        if index.column() == 4:
            return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled
        else:
            return QtCore.Qt.ItemIsEnabled

class TableView(QtGui.QTableView):
    """
    A simple table to demonstrate the QComboBox delegate.
    """
    def __init__(self, *args, **kwargs):
        QtGui.QTableView.__init__(self, *args, **kwargs)
        self.setItemDelegateForColumn(4, CheckBoxDelegate(self))

class CheckBoxDelegate(QtGui.QStyledItemDelegate):
    """
    A delegate that places a fully functioning QCheckBox in every
    cell of the column to which it's applied
    """
    def __init__(self, parent):
        QtGui.QItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):
        '''
        Important, otherwise an editor is created if the user clicks in this cell.
        ** Need to hook up a signal to the model
        '''
        return None

    def paint(self, painter, option, index):
        '''
        Paint a checkbox without the label.
        '''

        checked = index.data().toBool()
        check_box_style_option = QtGui.QStyleOptionButton()

        if (index.flags() & QtCore.Qt.ItemIsEditable) > 0:
            check_box_style_option.state |= QtGui.QStyle.State_Enabled
        else:
            check_box_style_option.state |= QtGui.QStyle.State_ReadOnly

        if checked:
            check_box_style_option.state |= QtGui.QStyle.State_On
        else:
            check_box_style_option.state |= QtGui.QStyle.State_Off

        check_box_style_option.rect = self.getCheckBoxRect(option)

        # this will not run - hasFlag does not exist
        #if not index.model().hasFlag(index, QtCore.Qt.ItemIsEditable):
            #check_box_style_option.state |= QtGui.QStyle.State_ReadOnly

        check_box_style_option.state |= QtGui.QStyle.State_Enabled

        QtGui.QApplication.style().drawControl(QtGui.QStyle.CE_CheckBox, check_box_style_option, painter)

    def editorEvent(self, event, model, option, index):
        '''
        Change the data in the model and the state of the checkbox
        if the user presses the left mousebutton or presses
        Key_Space or Key_Select and this cell is editable. Otherwise do nothing.
        '''
        print 'Check Box editor Event detected : '
        print event.type()
        if not (index.flags() & QtCore.Qt.ItemIsEditable) > 0:
            return False

        print 'Check Box editor Event detected : passed first check'
        # Do not change the checkbox-state
        if event.type() == QtCore.QEvent.MouseButtonPress:
          return False
        if event.type() == QtCore.QEvent.MouseButtonRelease or event.type() == QtCore.QEvent.MouseButtonDblClick:
            if event.button() != QtCore.Qt.LeftButton or not self.getCheckBoxRect(option).contains(event.pos()):
                return False
            if event.type() == QtCore.QEvent.MouseButtonDblClick:
                return True
        elif event.type() == QtCore.QEvent.KeyPress:
            if event.key() != QtCore.Qt.Key_Space and event.key() != QtCore.Qt.Key_Select:
                return False
            else:
                return False

        # Change the checkbox-state
        self.setModelData(None, model, index)
        return True

    def setModelData (self, editor, model, index):
        '''
        The user wanted to change the old state in the opposite.
        '''
        print 'SetModelData'
        newValue = not index.data().toBool()
        print 'New Value : {0}'.format(newValue)
        model.setData(index, newValue, QtCore.Qt.EditRole)

    def getCheckBoxRect(self, option):
        check_box_style_option = QtGui.QStyleOptionButton()
        check_box_rect = QtGui.QApplication.style().subElementRect(QtGui.QStyle.SE_CheckBoxIndicator, check_box_style_option, None)
        check_box_point = QtCore.QPoint (option.rect.x() +
                            option.rect.width() / 2 -
                            check_box_rect.width() / 2,
                            option.rect.y() +
                            option.rect.height() / 2 -
                            check_box_rect.height() / 2)
        return QtCore.QRect(check_box_point, check_box_rect.size())

###############################################################################################################################
class Dataframe(dict):
  def __init__(self, columns, values):
    if len(values) != len(columns):
      raise Exception("Bad values")
    self.columns = columns
    self.values = values
    self.index = values[0]
    super(Dataframe, self).__init__(dict(zip(columns, values)))
    pass

  def iget_value(self, i, j):
    return(self.values[j][i])

  def iset_value(self, i, j, value):
    self.values[j][i] = value

if __name__=="__main__":
    from sys import argv, exit

    class Widget(QtGui.QWidget):
        """
        A simple test widget to contain and own the model and table.
        """
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)

            l=QtGui.QVBoxLayout(self)
            cdf = self.get_data_frame()
            self._tm=TableModel(self)
            self._tm.update(cdf)
            self._tv=TableView(self)
            self._tv.setModel(self._tm)
            for row in range(0, self._tm.rowCount()):
                self._tv.openPersistentEditor(self._tm.index(row, 4))
            self.setGeometry(300, 300, 550, 200)
            l.addWidget(self._tv)

        def get_data_frame(self):
            if has_panda:
              df = pd.DataFrame({'Name':['a','b','c','d'],
              'First':[2.3,5.4,3.1,7.7], 'Last':[23.4,11.2,65.3,88.8], 'Class':[1,1,2,1], 'Valid':[True, False, True, False]})
            else:
              columns = ['Name', 'First', 'Last', 'Class', 'Valid']
              values = [['a','b','c','d'], [2.3,5.4,3.1,7.7], [23.4,11.2,65.3,88.8], [1,1,2,1], [True, False, True, False]]
              df = Dataframe(columns, values)
            return df

    a=QtGui.QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())

rawDescription: PyQt - Column of Checkboxes in a QTableView


code:
record = set()
while True:  # just an example; you'll need to end the loop somehow
    message = raw_input('give me data! ')
    record.add(message)
print record

rawDescription: Python comparison of previous data

description: Python comparison of previous data


code:
from itertools import groupby
from operator import itemgetter

with open('somefile') as fin:
    lines = (line.split() for line in fin)
    for key, items in groupby(lines, itemgetter(1)):
        print list(items)

[['12', 'fish'], ['50', 'fish']]
[['1', 'turtle']]
[['11', 'dog'], ['34', 'dog'], ['12', 'dog']]

rawDescription: Memory efficient groupby in Python

description: Memory efficient groupby in Python


code:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt

df = pd.DataFrame(np.random.randn(10, 8), columns=list('abcdefgh'))

print df

#Round to two digits to print nicely
vals = np.around(df.values, 2)
#Normalize data to [0, 1] range for color mapping below
normal = (df - df.min()) / (df.max() - df.min())

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis('off')
the_table=ax.table(cellText=vals, rowLabels=df.index, colLabels=df.columns,
                   loc='center', cellColours=plt.cm.RdYlGn(normal),animated=True)
fig.savefig("table.png")

rawDescription: Conditional formatting for 2- or 3-scale coloring of cells of a table


code:
def getSolarFlowtemperature():
    #Open the temperature sensor, read it and process the result
    tfile = open("/sys/bus/w1/devices/28-000003086819/w1_slave")
    text = tfile.read()
    tfile.close()
    temperature_data = text.split()[-1]
    temperature = float(temperature_data[2:])
    temperature = temperature / 1000

    #This while function checks for the error temperatures, and tries to read the sensor again to get a proper value. After 10 tries it stops
    count = 0
    while temperature == -0.062 or temperature == -0.125:
            time.sleep(2)
            count = count + 1
            print 'Temperature error on 28-000003086819, retrying'
            tfile = open("/sys/bus/w1/devices/28-000003086819/w1_slave")
            text = tfile.read()
            tfile.close()
            temperature_data = text.split()[-1]
            temperature = float(temperature_data[2:])
            temperature = temperature / 1000
            if count > 10:
                 break
            else:
                 return(temperature)

rawDescription: Python indentation newbie

description: Python indentation newbie


rawDescription: Deconstructing nested tuples of tuples recursively into a flat list of connections


code:
x= "th i s. i s. a. n i^ c e. s s t. s t r i ng."
for i in range(len(x)):
    if x[i] == '^':
        j = i + 1
        if x[j] == ' ':
            j = j + 1
            while j < len(x) and x[j] != ' ':
                j = j + 1
                print x
            x= x[0:i] + x[i+1:]
            x= x[0:j-1] + "^" + x[j-1:]
            print x
            exit()

rawDescription: python move characters based on string position

description: Python move characters based on string position


code:
import datetime
s = '2013-07-01'
mydate = datetime.datetime.strptime(s, '%Y-%m-%d')
print mydate.strftime('%B %d, %Y')

rawDescription: convert an integer number in a date to the month name using python

description: Convert an integer number in a date to the month name using python


code:
from django.http import HttpResponse
from django.template import RequestContext, loader

def index(request):
    template = loader.get_template('homepage/index.html')
    for n in template:
        print n
    return HttpResponse(template)

rawDescription: Cannot locate view in Django MVC

description: Cannot locate view in Django MVC


code:
countries = ['foo', 'bar', 'baz']
takes = [re.compile(r'of\s+(the)?\s*%s$' % (c), re.I) for c in countries]
subs = [re.compile(r'%s$' % (c), re.I) for c in countries]

def remove_country(s):
    for regex in takes:
        if regex.search(s):
            return s
    for regex in subs:
        s = regex.sub('', s)
    return s

print remove_country('the bank of foo')
print remove_country('the bank of the baz')
print remove_country('the nation bar')

''' Output:
    the bank of foo
    the bank of the baz
    the nation
'''

rawDescription: Conditionals on replacement in a string

description: Conditionals on replacement in a string


code:
import argparse

def do_things(args):
    print args
    # Do your stuff

mainparser = argparse.ArgumentParser()
submainadder = mainparser.add_subparsers(title='subcommands')
parser_ut = submainadder.add_parser('unittest')
parser_ut.set_defaults(func=do_things)

if __name__ == "__main__":
    stuff = mainparser.parse_args()
    stuff.func(stuff)

rawDescription: How do I check for a particular subparser?

description: Check for a particular subparser


code:
import wx

class TestFrame(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, None)
        self.SetBackgroundColour("#FFFFFF")
        self.rootSizer = wx.BoxSizer(wx.VERTICAL)

        self.rootSizer.Add((80, 80)) # Spacer
        self.buttonActive = wx.Button(self,-1,"Activate",size=(100,100))
        self.buttonActive.Bind(wx.EVT_BUTTON, self.OnActivate)
        self.rootSizer.Add(self.buttonActive,0,wx.ALIGN_CENTER)

        self.rootSizer.Add((80,80)) # Spacer
        self.buttonDisable = wx.Button(self,-1, "Disable", size=(100,100))
        self.buttonDisable.Bind(wx.EVT_BUTTON, self.OnDisabled)
        self.rootSizer.Add(self.buttonDisable,0,wx.ALIGN_CENTER)

        self.SetSizer(self.rootSizer)

    def OnActivate(self,evt):
        print "Activate"
        self.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))

    def OnDisabled(self, evt):
        print "Disabled"
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

if __name__ == '__main__':

    app = wx.App(None)
    frame = TestFrame(None)
    frame.Show()
    app.MainLoop()

rawDescription: wxPython - Begin and End BusyCursor

description: WxPython - Begin and End BusyCursor


code:
filne = "in"
with open(filne, 'r+') as f:
    lines = f.readlines()
    for i in range(0, len(lines)):
        line = lines[i]
        print line
        if line[:5] == "anim ":
            ne = lines[i + 1] # you may want to check that i < len(lines)
            print ' ne ',ne,'\n'
            break

rawDescription: python read next()

description: Python read next()


code:
print apple
print banana
variables = {"apple" : "a rigid, juicy fruit", "banana" : "a soft, fleshy fruit"}
for variable,value in variables.iteritems():
 locals()[variable] = value

print apple
print banana

rawDescription: It there an equivalent to PHP's extract in Python?


code:
items = self.listWidgetName.findItems("dan",Qt.MatchExactly)

if len(items) > 0:

    for item in items:
        print "row number of found item =",self.listWidgetName.row(item)
        print "text of found item =",item.text()

rawDescription: How can I get QListWidget item by name?

description: Get QListWidget item by name


code:
from operator import itemgetter
from itertools import groupby

def convert(row): # convert each row to a list
    lst = row.split(",")
    return lst

with open("1891_2150.txt" , "r") as f:
    content = f.readlines()

content = content[1:] # remove the file head
rows = map(convert,content)

sorted(rows, key=itemgetter(2)) # sort by the Line

for key, group in groupby(rows, lambda x: x[2]):
    sorted(group,key=itemgetter(3)) #sort by the Point

for index in range(len(rows) - 1):
    if rows[index][2] == rows[index+1][2]:
        X = float(rows[index][7])
        X_next = float(rows[index + 1][7])
        print X - X_next

rawDescription: Comparing 1 column in 2 lines in Text file


code:
from sqlalchemy.orm import class_mapper, defer
def defer_everything_but(entity, cols):
    m = class_mapper(entity)
    return [defer(k) for k in
            set(p.key for p
                in m.iterate_properties
                if hasattr(p, 'columns')).difference(cols)]

s = Session()
print s.query(A).options(*defer_everything_but(A, ["q", "p"]))

rawDescription: SQLAlchemy: selecting which columns of an object in a query


code:
dir_pair_data = sc.wholeTextFiles(mypath)
dir_pair_data = (dir_pair_data
                 .map(lambda (x,y): (os.path.dirname(x), y))
                 .groupByKey()
                 .values()
                 .zipWithUniqueId()
                 .map(lambda x:(x[1], x[0]))
                 .flatMapValues(lambda x: x))

rawDescription: Spark: Change the Key of key value pair

description: Spark: Change the Key of key value pair


code:
count = 3
for i in range(0, count):
    exec("dict%d = %s" % (i + 1, {}))
print dict1
print dict2

rawDescription: How to create dynamic dictionary names

description: Create dynamic dictionary names


code:
def do_stuff(self, attr):
    for item in self._my_objects:
        print getattr(item, attr)

rawDescription: Reduce Code Duplication Class Design

description: Reduce Code Duplication Class Design


code:
import multiprocessing, re, subprocess, sys

CMD_LIST = [
    ["wget", "-qO-", "http://ipecho.net/plain"],
    ["curl", '-s', "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi"],
    ["curl", '-s', "v4.ident.me"],
    ["curl", '-s', "ipv4.icanhazip.com"],
    ["curl", '-s', "ipv4.ipogre.com"],
]

ip_pat = re.compile('[0-9.]{7,}')
pool = multiprocessing.Pool(5)
for output in pool.imap_unordered(subprocess.check_output, CMD_LIST):
    print 'output:',output
    m = ip_pat.search(output)
    if m:
        print 'GOT IP:', m.group(0)
        pool.terminate()
        sys.exit(0)

print 'no IP found'

rawDescription: Threading and interpreter shutdown

description: Threading and interpreter shutdown


code:
class DevNull(object):
    def write(self, arg):
        pass

import sys
sys.stdout = DevNull()
print "this goes to nirvana!"

rawDescription: What is the best way to toggle python prints?


code:
for line in a:
    print '%s, %s' % (line[0], line[1])

rawDescription: Python counter for counting the most commmon string listed in the output


code:
import subprocess

def monitor_deamons():
   print "starting monitor"

   ps = subprocess.Popen(('ps', '-ef'), stdout=subprocess.PIPE)
   grep = subprocess.Popen(('grep', '-v', 'grep'), stdin=ps.stdout, stdout=subprocess.PIPE)

   ps.stdout.close()  # Allow ps to receive a SIGPIPE if grep exits.

   grep_daemon = subprocess.Popen(('grep', 'daemon'), stdin=grep.stdout)

   grep.stdout.close() # Allow grep to receive a SIGPIPE if grep_daemon exits

   output = grep_daemon.communicate()[0]

   print output

monitor_deamons()

rawDescription: Check if daemon process is runnning

description: Check if daemon process is runnning


code:
json_data = json.loads(your_line)

# Usage exemple
for feature in json_data['features']:
    print feature['id']

rawDescription: Python: Parsing a single-line JSON file

description: Python: Parsing a single-line JSON file


code:
def shampoo_instructions(num_cycles):
    if num_cycles < 1:
        print 'Too few.'
    elif num_cyles > 4:
        print 'Too many.'
    else:
        for num_cycles in range(1,num_cycles+1):
            print num_cycles, 'lather and rinse.'
        print 'Done'

rawDescription: Shampoo cycle loop

description: Shampoo cycle loop


code:
import os
import random

for filepath in ['a.txt', 'b.txt', 'c.txt', 'd.txt', 'e.txt']:
    with open(filepath) as f:
        lines = [line.strip() for line in f]
    print os.path.splitext(os.path.basename(filepath))[0], ':', random.choice(lines)

rawDescription: Calling random line from multiple text files


code:
# urls.cfg
[URLs]
Google=www.google.com
Hotmail=www.hotmail.com
Yahoo=www.yahoo.com

# Scriptlet
import ConfigParser
filepath = '/home/me/urls.cfg'

config = ConfigParser.ConfigParser()
config.read(filepath)

print config.items('URLs') # Returns a list of tuples.
# [('hotmail', 'www.hotmail.com'), ('google', 'www.google.com'), ('yahoo', 'www.yahoo.com')]

rawDescription: Storing and retrieving a list of Tuples using ConfigParser


code:
class Obj(object):
    def __getattr__(self,x):
        setattr(self,x,Obj())
        return getattr(self,x)

o = Obj()
o.car.type = "family"
o.car.color = "red"

print o.car.color

rawDescription: Run a method instead of yelling AttributeError


code:
x = b"\u00f3"
print x
print x.decode('unicode_escape')

rawDescription: Python CSV encoding

description: Python CSV encoding


code:
song = ''  # Accumulators need to start empty
for _ in range(3):  # Don't really need the stanza variable
    animal = raw_input("What animal? ")
    animalSound = raw_input("What sound does a %s make? " %(animal))

    song += "Old MacDonald had an errno. EIEIO\n"
    song += "His farm had a " + animal + " EIEIO\n"
    song += "His " + animal + "made a noise: " + animalSound + "\n"
print song

rawDescription: Beginner Python: Accumulator loop function


code:
import ast
import codegen

class Visitor(ast.NodeTransformer):

    def visit_Num(self, node):
        return ast.Num(42)

x = Visitor()
t = ast.parse('x + y + z + 3')
out = x.visit(t)
print codegen.to_source(out)
# x + y + z + 42

rawDescription: Turn an Abstract Syntax Tree from the ast module into a new python file.


code:
class Foo(object):

    def get_bar(self):
        return "bar"

    bar = property(get_bar)

print Foo().bar # prints bar

rawDescription: Python property returning property object


code:
from azure import *
from azure.storage import *

blob_service = BlobService(account_name='<accountname>', account_key='<accountkey>')
next_marker = None
while True:
    blobs = blob_service.list_blobs('<containername>', maxresults=100, marker=next_marker)
    next_marker = blobs.next_marker
    print(next_marker)
    print(len(blobs))
    if next_marker is None:
        break
print "done"

rawDescription: Python Azure SDK: Using list_blobs to get more than 5.000 Results


code:
import socket
print socket.gethostbyname('localhost') # result from hosts file
print socket.gethostbyname('google.com') # your os sends out a dns query

rawDescription: How can I do DNS lookups in Python, including referring to /etc/hosts?

description: Do DNS lookups in Python, including referring to /etc/hosts


code:
import HTMLParser
h = HTMLParser.HTMLParser()
h.unescape("Hỗ trợ ngôn ngữ")
print h.unescape("Hỗ trợ ngôn ngữ")

rawDescription: iso-8859-1 and utf8 in python

description: Iso-8859-1 and utf8 in python


code:
values = T.vector('values')
nb_values = values.shape[0]

# For every element in 'values', obtain the total number of times
# its value occurs in 'values'.
# NOTE : I've done the broadcasting a bit more explicitly than
# needed, for clarity.
freqs = T.eq(values[:,None], values[None, :]).sum(0).astype("float32")

# Compute a vector containing, for every value in 'values', the
# probability of that value in the vector 'values'.
# NOTE : these probabilities do *not* sum to 1 because they do not
# correspond to the probability of every element in the vector 'values
# but to the probability of every value in 'values'. For instance, if
# 'values' is [1, 1, 0] then 'probs' will be [2/3, 2/3, 1/3] because the
# value 1 has probability 2/3 and the value 0 has probability 1/3 in
# values'.
probs = freqs / nb_values

entropy = -T.sum(T.log2(probs) / nb_values)
fct = theano.function([values], entropy)

# Will output 0.918296...
print fct([0, 1, 1])

rawDescription: Entropy and Probability in Theano

description: Entropy and Probability in Theano


code:
import os
import collections
extensions = collections.defaultdict(int)

for path, dirs, files in os.walk('/'):
   for filename in files:
       extensions[os.path.splitext(filename)[1].lower()] += 1

for key,value in extensions.items():
    print 'Extension: ', key, ' ', value, ' items'

rawDescription: Python - Acquiring a count of file extensions across all directories


code:
yourFile = open("yourFile.txt", "r")
yourText = yourFile.read()
textLines = yourText.split("\n")
locationDict = {}
for line in textLines:
    k1, k2, v = line.split(" ")
    if k1 not in locationDict.keys():
        locationDict[k1] = {}
    else:
        if k2 not in locationDict[k1].keys():
            locationDict[k1][k2] = int(v)
        else:
            locationDict[k1][k2] += int(v)

print locationDict

rawDescription: python: recursive dictionary of dictionary

description: Python: recursive dictionary of dictionary


code:
from re import findall as f

r="5"

level=3

a = f(r'(\d+)?-?(\d+)', r)[0]

try:
    print int(a[0]) <= level <= int(a[1])
except (IndexError, ValueError):
    print 0 <= level <= int(a[1])

rawDescription: check if value inside the range

description: Check if value inside the range


code:
import sys

class MyClass:

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

    def fn1(self):
        return self.fn2() # just as a simple example which calls other functions

    def fn2(self):
        return self.fn3()

    def fn3(self):
        return self.fn4()

    def fn4(self):
        return self.fn5()

    def fn5(self):
        return self.arg

def main():
    m = MyClass(arg=sys.argv[1])
    print m.fn1() # prints output based on command line argument

if __name__ == '__main__':
    main()

rawDescription: Using global variable for option passing?


code:
if len(data) > 102400:
  print "Skipping file which is too large, at %d bytes" % len(data)
else:
  process(data) # The normal processing

rawDescription: how do i get the byte count of a variable in python just like wc -c gives in unix

description: Get the byte count of a variable in python just like wc -c gives in unix


code:
from Tkinter import *

def add_item():

    def create_rec(item):

        def saveREC():
            print "hola"#for test purpose
            print typeSel.get()

        label2=Label(add_win, text="select item type:").pack()

        #radio buttons creation
        typeSel = StringVar()
        R1 = Radiobutton(add_win, text="meat", variable=typeSel, value="meat")
        R1.pack( anchor = W)
        typeSel.set('meat')

        saveBttn=Button(add_win, text="recognize this item in the future", command=saveREC).pack()

    def saveEntry():
        item=entry.get()
        a=False
        typeVar="hello"
        if a==True:
            print item, " is saved to ", typeVar

        #if item isn't recognized then user can add item to recognition list
        if a==False:
            create_rec(item)

    add_win=Tk()

    entry=Entry(add_win, width=30)
    entry.pack()
    entry.focus_set()

    saveBttn=Button(add_win, text="add item", width=25, command=saveEntry)
    saveBttn.pack()

    add_win.mainloop()

add_item()

rawDescription: typeSel.get() not working

description: TypeSel.get() not working


code:
d = {(dog, smurf): 40}
print d[(dog, smurf)] # 40

rawDescription: Python data structure similar to dictionary where key is two values?


code:
import functools

class decobar(object):
    registry = set()

    @classmethod
    def _func_key(cls, f):
        return '.'.join((f.__module__, f.func_name))

    @classmethod
    def present(cls, f):
        return cls._func_key(f) in cls.registry

    def __call__(self, f):
        self.registry.add(self._func_key(f))

        @functools.wraps(f)
        def wrap():
            return f() + "bar"
        return wrap

# Make the decorator singleton
decobar = decobar()

def decofu(f):
    @functools.wraps(f)
    def wrap():
        print decobar.present(f) # Tells me whether decobar was also applied
        return f() + "fu"
    return wrap

@decofu
@decobar
def important_task():
    return "abc"

rawDescription: passing variables between two python decorators

description: Passing variables between two python decorators


code:
import pyPdf

def getPDFContent(path):
    content = ""
    # Load PDF into pyPDF
    pdf = pyPdf.PdfFileReader(file(path, "rb"))
    # Iterate pages
    for i in range(0, pdf.getNumPages()):
        # Extract text from page and add to content
        content += pdf.getPage(i).extractText() + "\n"
    # Collapse whitespace
    content = " ".join(content.replace("\xa0", " ").strip().split())
    return content

for f in filelist:
    print keyword in getPDFContent(f)

rawDescription: How to search keywords in 400+ PDF files?

description: Search keywords in 400+ PDF files


code:
class MyClass(object):
  def __init__(self):
    print 'Created MyClass@{0}'.format(id(self))

def create_instance():
  return MyClass()

x = create_instance()

@mock.patch('__main__.MyClass')
def create_instance2(MyClass):
  MyClass.return_value = 'foo'
  return create_instance()

i = create_instance2()
i
def create_instance():
  print MyClass
  return MyClass()
create_instance2()
create_instance()

rawDescription: Mocking a class: Mock() or patch()?

description: Mocking a class: Mock() or patch()


code:
total_seconds = 40209
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
print '{:02d}:{:02d}:{:02d}'.format(hours, minutes, seconds)

rawDescription: How can I turn a number ( in seconds) to time format?

description: Turn a number ( in seconds) to time format


code:
import re

# returns (float,endpos)
def strtod(s, pos):
  m = re.match(r'[+-]?\d*[.]?\d*(?:[eE][+-]?\d+)?', s[pos:])
  if m.group(0) == '': raise ValueError('bad float: %s' % s[pos:])
  return float(m.group(0)), pos + m.end()

print strtod('(a+2.0)/1e-1', 3)
print strtod('(a+2.0)/1e-1', 8)

rawDescription: Python equivalent to C++ strtod

description: Python equivalent to C++ strtod


code:
import math, numpy, scipy.optimize, scipy.integrate

def integrand(t, args):
    w, p = args
    return math.sin(t * w)/t + p

def curve(w, p):
    res = scipy.integrate.quad(integrand, 0.0, math.pi, [w, p])
    return res[0]

vcurve = numpy.vectorize(curve, excluded=set([1]))

truexdata = numpy.asarray([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0])
trueydata = vcurve(truexdata, 1.0)

xdata = truexdata + 0.1 * numpy.random.randn(8)
ydata = trueydata + 0.1 * numpy.random.randn(8)

popt, pcov = scipy.optimize.curve_fit(vcurve,
                                      xdata, ydata,
                                      p0=[2.0])
print popt

rawDescription: Fitting data with integral function

description: Fitting data with integral function


code:
class lin_reg(object):
     def __init__(self):
         self.indep, self.dep = self.initialise_from_user()
         self.total = self.indep + self.dep
         print self.indep, self.dep, self.total
         self.n = len(self.total)

def initialise_from_user(self):
    analysis_type = raw_input('Type of Regression(linear-n,nonlinear-nl): ')
    n = int(raw_input('Number of Independent Variables: '))

    indep = [np.matrix(raw_input('Enter the array: ')) for _ in range(self.n)]
    dep =  np.matrix(raw_input('Enter the dependent array: '))

    return indep, dep

rawDescription: Unexpected output from list.append()

description: Unexpected output from list.append()


code:
def run(self):
    while not self.finished.isSet():
        print "Time: %s - timer running..." % time.asctime()

        self.resetted = True
        while self.resetted:
            print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
            self.resetted = False
            self.finished.wait(self.interval)

        if not self.finished.isSet():
            self.function(*self.args, **self.kwargs)
    print "Time: %s - timer finished!" % time.asctime()

rawDescription: Resettable timer in python repeats until cancelled


code:
results_list = [[1,2,3], [1,2,4]]
results_union = set().union(*results_list)
print results_union

rawDescription: Pythonic Way to Create Union of All Values Contained in Multiple Lists


code:
header = request[0][0:14]
hd = unpack("!6s6s2s", header)

dest_addr = hexlify(hd[0])
source_addr = hexlify(hd[1])
type = hexlify(hd[2])

print "destination: {0}".format(dest_addr)
print "source: {0}".format(source_addr)
print "destination: {0}".format(type)

rawDescription: How to unpack a packets' wlan layer

description: Unpack a packets' wlan layer


code:
def check_thread(self):
    # Still alive? Check again in half a second
    if self.mt.isAlive():
        self.after(500, self.check_thread)
    else:
        print "Step Three"

def my_function(self):
    self.mt = MyThread()
    self.mt.start()
    self.check_thread()

rawDescription: Python Tkinter: How do I make my GUI responsive as long as a thread runs?


code:
import constants

def use_my_constants():
    print constants.GOOD, constants.BAD, constants.AWFUL

rawDescription: What's the best way to initialise and use contants across Python classes?


code:
import random

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs")
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print nouns[num] + ' ' + verbs[num] + ' ' + adv[num] + ' ' + adj[num]

rawDescription: Generate random sentences in python

description: Generate random sentences in python


code:
class Descriptor(object):
    pass

class Meta(type):
    def __new__(cls, name, bases, attrs):
        obj = type.__new__(cls, name, bases, attrs)
        # obj is now a type instance

        # this loop looks for Descriptor subclasses
        # and instantiates them, passing the type as the first argument
        for name, attr in attrs.iteritems():
            if isinstance(attr, type) and issubclass(attr, Descriptor):
                setattr(obj, name, attr(obj))

        return obj

class FooDescriptor(Descriptor):
    def __init__(self, owner):
        owner.foo = 42

class BarClass(object):
    __metaclass__ = Meta
    foo_attribute = FooDescriptor # will be instantiated by the metaclass

print BarClass.foo

rawDescription: Can I get a reference to the 'owner' class during the __init__ method of a descriptor?

description: Get a reference to the 'owner' class during the __init__ method of a descriptor


code:
import wx
import wx.richtext as rt

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, 1, 'Testing strike-through')
        rtc = rt.RichTextCtrl(self, -1)
        rtc.WriteText("normal text")
        rtc.Newline()

        font = wx.FFont(12, wx.FONTFAMILY_DEFAULT, face='Tahoma', flags=wx.FONTFLAG_STRIKETHROUGH)

        info = font.GetNativeFontInfoDesc()
        info = self.setFontInfoStrikethrough(info)
        font.SetNativeFontInfoFromString(info)

        rtc.BeginFont(font)
        rtc.WriteText("This is strike-through")
        rtc.EndFont()

    def setFontInfoStrikethrough(self, info):
        print "orig:", info
        info = info.split(";")
        info[8] = r'1'
        info = ";".join(info)
        print "new :",info
        return info

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

rawDescription: wxPython RichTextCtrl with strikethrough

description: WxPython RichTextCtrl with strikethrough


code:
# A second-order decorator
def decdec(inner_dec):
    def ddmain(outer_dec):
        def decwrapper(f):
            wrapped = inner_dec(outer_dec(f))
            def fwrapper(*args, **kwargs):
               return wrapped(*args, **kwargs)
            return fwrapper
        return decwrapper
    return ddmain

def wrap(f):
    def wrapper():
        return " ".join(f())
    return wrapper

# Decorate upper (a decorator) with wrap (another decorator)
@decdec(wrap)
def upper(f):
    def uppercase(*args, **kargs):
        a,b = f(*args, **kargs)
        return a.upper(), b.upper()
    return uppercase

@upper
def hello():
    return "hello","world"

print hello()

rawDescription: decorating decorators: try to get my head around understanding it

description: Decorating decorators: try to get my head around understanding it


code:
import mechanize

class ReplacingOpener(mechanize.SeekableResponseOpener):
    def process_response_object(self, response):
        response = mechanize.SeekableResponseOpener.process_response_object(
            self, response)

        # Get the HTML from the response
        html = response.read()

        # Replace the HTML here with anything you feel like
        html = html.replace('</body>', '')

        # Set it back to the response
        response.set_data(html)
        return response

opener = mechanize.OpenerFactory(ReplacingOpener).build_opener()
response = opener.open('http://stackoverflow.com/questions/26793091/')

forms = mechanize.ParseResponse(response)
print forms

rawDescription: How do you click a label in a form in mechanize?

description: Click a label in a form in mechanize


code:
import sys
from StringIO import StringIO

buffer = StringIO()
sys.stdout = buffer

exec "print 'Hello, World!'"

#remember to restore the original stdout!
sys.stdout = sys.__stdout__

print buffer.getvalue()

rawDescription: Execute a block of python code with exec, capturing all its output?


code:
import ast, re

with open("your_script.py") as fp:
    tree = ast.parse(fp.read())

for node in ast.walk(tree):
    if (isinstance(node, ast.Str)
            and isinstance(node.s, str)
            and  re.search(r'[\x80-\xFF]', node.s)):
        print 'bad string %r line %d col %d' % (node.s, node.lineno, node.col_offset)

rawDescription: find non ascii bytestrings in python source code

description: Find non ascii bytestrings in python source code


code:
import os, sys
def set_exit_handler(func):
    if os.name == "nt":
        try:
            import win32api
            win32api.SetConsoleCtrlHandler(func, True)
        except ImportError:
            version = '.'.join(map(str, sys.version_info[:2]))
            raise Exception('pywin32 not installed for Python ' + version)
    else:
        import signal
        signal.signal(signal.SIGTERM, func)

if __name__ == '__main__':
    def on_exit(sig, func=None):
        f = open('log.txt', 'w')
        f.write('shutdown...')
        f.close()
        sys.exit()
    set_exit_handler(on_exit)
    print 'Press  to quit'
    raw_input()
    print 'quit!'

rawDescription: python on script unload event (destructor)

description: Python on script unload event (destructor)


code:
from collections import defaultdict

d = defaultdict(int)

for sub in outer:
  for val in sub:
    d[val] += 1

print sorted(d.keys(), key=lambda k: d[k], reverse = True)
# Output: [4, 2, 3, 1, 5, 6]

rawDescription: Get list based on occurrences in unknown number of sublists


code:
class Foo(object):
    @property
    def foo(self):
        return 'something'
    @foo.setter
    def foo(self, value):
        pass
with patch('__main__.Foo.foo', new_callable=PropertyMock) as mock_foo:
    mock_foo.return_value = 'mockity-mock'
    this_foo = Foo()
    print this_foo.foo
    this_foo.foo = 6
mock_foo.mock_calls

rawDescription: Python PropertyMock in Mock constructor

description: Python PropertyMock in Mock constructor


code:
def xxx():
    a=10
    b=15
    c=20
    return a,b

def yyy():
    a,b = xxx()
    print a        ### value a from xxx()
    print b        ### value b from xxx()

yyy()

rawDescription: How to Access Function variables in Another Function


rawDescription: Aligning a combination of strings in Python


code:
words = ["Hello", "Schilde\xc2\xadrung"]
words = [word.replace('\xc2\xad', '') for word in words]
print repr(words)
# Prints ['Hello', 'Schilderung']

rawDescription: Python: Remove soft hyphen(s)

description: Python: Remove soft hyphen(s)


code:
import pandas as pd

df = pd.DataFrame(['循环经济', '3D打印', '再生经济'], columns=['block_name'])

print df[df['block_name'].str.contains('经济')]

rawDescription: DataFrame filter data with string type use like


code:
lines = open('wordprob.txt','r').readlines()
words = open('StdWord.txt','r').readlines()
for i,line in enumerate(lines):
        v = [eval(s) for s in line.split()]
        if v[0] > v[1]:
                print words[i].strip()

rawDescription: How to cleanly loop over two files in parallel in Python

description: Cleanly loop over two files in parallel in Python


code:
def new_panel(self, nm):
    pnl = wx.Panel(self)
    # just to debug, I added a string attribute to the panel
    # don't you love dynamic languages? :)
    pnl.identifierTag = nm
    self.nb.AddPage(pnl, nm)
    self.sizer = wx.BoxSizer()
    self.sizer.Add(self.nb, 1, wx.EXPAND)
    self.SetSizer(self.sizer)

    pnl.Bind(wx.EVT_LEFT_DOWN, self.click)

def click(self, event):
    print 'Mouse click'
    # get the current selected page
    page = self.nb.GetPage(self.nb.GetSelection())
    # notice that it is the panel that you created in new_panel
    print page.identifierTag

rawDescription: AuiNotebook, where did the event happend


code:
from geopy import geocoders
gn = geocoders.GeoNames()
print gn.geocode("Cleveland, OH 44106")
gn.geocode("Cleveland, OH", exactly_one=False)[0]

rawDescription: Python module for getting latitude and longitude from the name of a US city?


rawDescription: fast search for integer list in much larger set of longer integer lists

description: Fast search for integer list in much larger set of longer integer lists


code:
import os
from datetime import datetime

MY_DIR = 'somedirectory'

# my_files = [ f for f in os.listdir(MY_DIR) if os.path.isfile(os.path.join(MY_DIR,f)) ]
my_files = [
    'ABC-031814_01.txt',
    'ABC-031214_02.txt',
    'DEF-010114_03.txt'
]
file_list = []

for file_name in my_files:
    company,_,rhs = file_name.partition('-')
    datestr,_,rhs = rhs.partition('_')
    file_date = datetime.strptime(datestr,'%m%d%y')
    file_list.append(dict(file_date=file_date,file_name=file_name,company=company))

for row in sorted(file_list,key=lambda x: (x.get('company'),x.get('file_date'))):
    print row

rawDescription: Sort by date and string in python

description: Sort by date and string in python


code:
def concatenate():
    files = sort() # input is an array of filenames
    df_list =[]
    #merged = pd.DataFrame()
    for file in files:
        print "concatinating" + file
        if file.endswith('FulltimeSimpleOpt.csv'): # only consider those filenames
            filenamearray = file.split("_")
            f = pd.read_csv(file, index_col=0)
            f.loc[:,'Vehicle'] = filenamearray[0].replace("veh", "")
            f.loc[:,'Year'] = filenamearray[1].replace("year", "")
            if "timelimit" in file:
                f.loc[:,'Timelimit'] = "1"
            else:
                f.loc[:,'Timelimit'] = "0"
            df_list.append(f)
    merged = pd.concat(df_list, axis=0)
    merged.to_csv('merged.csv')

rawDescription: Concatenating a large number of CSV files (30,000) in in Python Pandas


code:
import hashlib
m = hashlib.md5()
m.update("000005fab4534d05api_key9a0554259914a86fb9e7eb014e4e5d52permswrite")
print m.hexdigest()

rawDescription: How to get MD5 sum of a string?

description: Get MD5 sum of a string


code:
def test1(x):
    x=x**2

    return x

def test2(x):
    x=x**3

    return x

def test3(x):
    x=x+10

    return x

testlist = []
n=0
y_vals=[1,2,3,4,5]

x_fn = [test1, test2, test3]

for fun in x_fn:
    for y in y_vals:

        x = y*2
        z=fun(x)

        testlist.append(z)

print testlist

rawDescription: Looping through a function in python but redefining a variable when a function from an array of functions gets called


code:
import pandas as pd

csv_df = pd.read_csv('part.csv')
df_T = csv_df.T

print df_T['ItemID'].tolist()
print df_T.index.tolist()

rawDescription: Read Excel lines into Python

description: Read Excel lines into Python


code:
import re
import string

words = ['a.bc,,', 'cdd,gf.f.d,fe']

regex = re.compile('[%s]' % re.escape(string.punctuation))
for i in words:
    while regex.search(i):
        i = regex.sub('', i)
    print i

rawDescription: Multiple punctuation stripping

description: Multiple punctuation stripping


code:
import argparse

parser = argparse.ArgumentParser(usage="-h for full usage")
parser.add_argument('query', nargs='?')
args = parser.parse_args()
if not args.query:
    print 'no arguments'
elif args.query == 'foo':
    print 'foo argument'
else:
    print 'random string'

rawDescription: Problems on argparse in optional arguments


code:
import Image
im = Image.open("name_of_file.jpg")
list_of_pixels = list(im.getdata())
print list_of_pixels[0]

rawDescription: Fastest way to get pixel color in Python or C++?


code:
from random import randrange
from itertools import combinations
long_list = [randrange(0,10000) for r in xrange(10000)]

def powerset(it):
    return [(i[0], i[1], 29994 - sum(i)) for i in combinations(it, 2) if 29994 - sum(i) in it]

def main():
    print powerset(set(long_list))

import cProfile
cProfile.run("main()")

rawDescription: Fastest way to search through iterable chain combinations Python


code:
from PySide import QtGui, QtCore

class View(QtGui.QGraphicsView): # as separate class
    def BlahBlah():
        print dir()

class Scene_w_ellipse(QtGui.QGraphicsScene):
    def __init__(self):
        super(Scene_w_ellipse, self).__init__()
        self.setSceneRect(0,0,300,200)
        self.C = QtGui.QGraphicsEllipseItem(-4,-20,80,40)
        self.C.setPos(100,70)
        self.C.setFlags(QtGui.QGraphicsItem.ItemIsMovable)
        #self.C.mousePressEvent(QtGui.QGraphicsSceneMouseEvent)
        self.addItem(self.C)

    def mousePressEvent(self, event):
        print dir()

    def mouseMoveEvent(self, event):
        self.C.mouseMoveEvent(event)

    def mouseReleaseEvent(self, event):
        self.C.mouseReleaseEvent(event)

rawDescription: Convert QmouseEvent to QGraphicsSceneMouseEvent for a drag movement


code:
import re

line = u"008800002     Bruxelles-Nord$Br�ussel Nord$<deu>$Brussel Noord$<nld>"
m = re.search("(\d+)\s+(.*)", line, re.UNICODE)
id = m.group(1)
names = {}
for m in re.finditer("(.*?)<(.*?)>", m.group(2), re.UNICODE):
    names[m.group(2)] = m.group(1)
print id, names

rawDescription: Regular expression for multiple occurances in python


code:
print result
print repr(result)
result
print step
print repr(step)
step

rawDescription: Is there a way to show correct float value?


code:
letters="abcdefghijklmnopqrstuvwxyz"
alphabet_length = len(letters)
start_position = 0
for i in range(1,1834):
    x=((i**2)%9)
    end_position = start_position + x
    end_position = end_position % alphabet_length
    if end_position < start_position:
         print letters[start_position:alphabet_length-1] + letters[0:end_position]
    else:
         print letters[start_position:end_position]
    start_position = end_position

rawDescription: Python 3: Trying to iterate lines of alphabet based on function of i


code:
x=["fizz","bear","fizz"]

def fizz_count(x):
    count= 0
    for e in x:
        if e=='fizz':
            count = count + 1
    return count

print fizz_count(x)

rawDescription: Showing and returning the number of a repeated string on a function?


code:
while(True):
    try:
         value = float(raw_input(">"))     # Get the input from user
         if value > largest_so_far:        # Type cast to integer
            largest_so_far = value
    except ValueError as e:                # Handle ValueError
        print largest_so_far
        break                              # Break the infinite loop

rawDescription: Find largest number from user input

description: Find largest number from user input


code:
from datetime import datetime

start_date = 'Sun Sep 16 16:05:15 +0000 2012'
end_date = 'Sun Sep 17 23:55:20 +0000 2012'

def __datetime(date_str):
    return datetime.strptime(date_str, '%a %b %d %H:%M:%S +0000 %Y')

start = __datetime(start_date)
end = __datetime(end_date)

delta = end - start
print delta  # prints: 1 day, 7:50:05
print delta.total_seconds()  # prints: 114605.0

rawDescription: Python subtracting two date strings

description: Python subtracting two date strings


code:
import os, datetime

folder_name = "MY_PATH"
for file_name in os.listdir(folder_name):
    file_name_full = os.path.join(folder_name, file_name)
    if not os.path.isfile(file_name_full):
        continue
    timestamp = os.path.getctime(file_name_full)
    dt = datetime.datetime.fromtimestamp(timestamp)
    # print out the datestamp
    print dt

    # if you now want to put them into subfolders based on the year 2012
    # you can do something like the following
    yr = dt.year
    subfolder_name = ""
    if yr < 2012:
        subfolder_name = "before 2012"
    else:
        subfolder_name = "2012 and after"
    subfolder_name_full = os.path.join(folder_name, subfolder_name)
    new_file_name_full = os.path.join(subfolder_name_full, file_name)
    # I assume your folders are premade. If not, you can do a quick-and-dirty mkdir here

    print "Moving %s -> %s" % (file_name_full, new_file_name_full)
    os.rename(file_name_full, new_file_name_full)

rawDescription: Image sorter by created date in python

description: Image sorter by created date in python


code:
import lxml.html
import calendar
from datetime import datetime

exDate  = "2014-11-22"
symbol  = "LLY"
dt      = datetime.strptime(exDate, '%Y-%m-%d')
ym      = calendar.timegm(dt.utctimetuple())

url     = 'http://finance.yahoo.com/q/op?s=%s&date=%s' % (symbol, ym,)
doc     = lxml.html.parse(url)
table   = doc.xpath('//table[@class="details-table quote-table Fz-m"]/tbody/tr')

rows    = []
for tr in table:
     d = [td.text_content().strip().replace(',','') for td in tr.xpath('./td')]
     rows.append(d)

print rows

rawDescription: WebScraping with BeautifulSoup or LXML.HTML


code:
from infi.systray import SysTrayIcon
def say_hello(systray):
    print "Hello, World!"
menu_options = (("Say Hello", None, say_hello),)
systray = SysTrayIcon("icon.ico", "Example tray icon", menu_options)
systray.start()

rawDescription: How can I run python script on Windows 7, with ability to enable/disable apperance of window and on taskbar?

description: Run python script on Windows 7, with ability to enable/disable apperance of window and on taskbar


code:
dict1 = {'key1': 1, 'key2': 2, 'key3': 3}
dict2 = {'key1': 10.4, 'key2': 10, 'key3': 10}
dict3 = {'key1': 10.4, 'key2': 2, 'key3': 3}

finaldict = {}
for key in dict1.keys():
    denom = float(dict2[key]) - float(dict3[key])
    if denom == 0.0: # or denom <= 0.0
      continue # skips over 'key1' because it can't do division by 0
    finaldict[key] = dict1[key] / (denom * 100)

print finaldict
#  {'key3': 0.004285714285714286, 'key2': 0.0025}

rawDescription: compute expression for values of three dictionaries with similar keys

description: Compute expression for values of three dictionaries with similar keys


code:
for key_val, group in itertools.groupby(curs.fetchall(), lambda r: r[2]):
    print key_val
    for item in group:
        print "\t", item

rawDescription: Aggregate in SQLite or Python

description: Aggregate in SQLite or Python


code:
import sys

class Tee(object):
    def __init__(self, *files):
        self.files = files
    def write(self, obj):
        for f in self.files:
            f.write(obj)
            f.flush() # If you want the output to be visible immediately
    def flush(self) :
        for f in self.files:
            f.flush()

f = open('out.txt', 'w')
original = sys.stdout
sys.stdout = Tee(sys.stdout, f)
print "test"  # This will go to stdout and the file out.txt

#use the original
sys.stdout = original
print "This won't appear on file"  # Only on stdout
f.close()

rawDescription: Output on the console and file using python


code:
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Chat(LineReceiver):

    def __init__(self, users):
        self.users = users
        self.name = None
        self.state = "GETNAME"

    def connectionMade(self):
        self.sendLine("What's your name?")

    def connectionLost(self, reason):
        if self.users.has_key(self.name):
            del self.users[self.name]

    def lineReceived(self, line):
        if self.state == "GETNAME":
            self.handle_GETNAME(line)
        else:
            self.handle_CHAT(line)

    def handle_GETNAME(self, name):
        if self.users.has_key(name):
            self.sendLine("Name taken, please choose another.")
            return
        self.sendLine("Welcome, %s!" % (name,))
        self.name = name
        self.users[name] = self
        self.state = "CHAT"

    def handle_CHAT(self, message):
        message = "<%s> %s" % (self.name, message)
        for name, protocol in self.users.iteritems():
            if ':' in message:
                self.exc(message.split(':')[0])
            if protocol != self:
                protocol.sendLine(message)

    def exc(self, cmd):
        print cmd
        if cmd == 'who':
            for i in self.users:
                print i

class ChatFactory(Factory):

    def __init__(self):
        self.users = {} # maps user names to Chat instances

    def buildProtocol(self, addr):
        return Chat(self.users)

reactor.listenTCP(8123, ChatFactory())
reactor.run()

rawDescription: Python socket connection not working properly


code:
import lxml.etree as et

s="""
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder|Remind|Remain</heading>
<body>Don't forget me this weekend!</body>
</note>
"""

tree = et.fromstring(s)

print tree.text
query = r'%s'%raw_input("Enter text: ")

pth = r'''//*[contains(text(),'%s')]'''%query

for t in tree.xpath(pth):
    print t.tag

rawDescription: How to get the tag name given the value in python?

description: Get the tag name given the value in python


code:
import os
import urllib2
import urllib

cmServerURL = 'http://<serverURL>:<port>/<path-to-cache.manifest>'

# download file code taken from stackoverflow
# http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python
def loadURL(url, dirToSave):
        file_name = url.split('/')[-1]
        u = urllib2.urlopen(url)
        f = open(dirToSave, 'wb')
        meta = u.info()
        file_size = int(meta.getheaders("Content-Length")[0])
        print "Downloading: %s Bytes: %s" % (file_name, file_size)

        file_size_dl = 0
        block_sz = 8192
        while True:
                buffer = u.read(block_sz)
                if not buffer:
                        break

                file_size_dl += len(buffer)
                f.write(buffer)
                status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
                status = status + chr(8)*(len(status)+1)
                print status,

        f.close()

# download the cache.manifest file
# since this request doesn't include the Conent-Length header we will use a different api =P
urllib.urlretrieve (cmServerURL+ 'cache.manifest', './cache.manifest')

# open the cache.manifest and go through line-by-line checking for the existance of files
f = open('cache.manifest', 'r')
for line in f:
        filepath = line.split('/')
        if len(filepath) > 1:
                fileName = line.strip()
                # if the file doesn't exist, lets download it
                if not os.path.exists(fileName):
                                print 'NOT FOUND: ' + line
                                dirName = os.path.dirname(fileName)
                                print 'checking dirctory: ' + dirName
                                if not os.path.exists(dirName):
                                        os.makedirs(dirName)
                                else:
                                        print 'directory exists'
                                print 'downloading file: ' + cmServerURL + line,
                                loadURL (cmServerURL+fileName, fileName)

rawDescription: Best way to download all artifacts listed in an HTML5 cache.manifest file?

description: Download all artifacts listed in an HTML5 cache.manifest file


code:
import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen("http://www.icc-ccs.org/prc/piracyreport.php")
soup = BeautifulSoup(page)
for incident in soup('td', width="90%"):
    where, linebreak, what = incident.contents[:3]
    print where.strip()
    print what.strip()
    print

rawDescription: html to text conversion using python language

description: Html to text conversion using python language


code:
for i, (letter, word) in enumerate(seq_nested):
  print i, letter, word

rawDescription: Python: Unpacking an inner nested tuple/list while still getting its index number


code:
A = np.zeros((3,3), dtype=np.complex)
print A
A[0,0] = 1. + 2.j
print A

rawDescription: How to put complex into a numpy's array?

description: Put complex into a numpy's array


code:
L=[[19,18,14,9,4],[15,12,11,10,6,5],[8],[16,13,3,2],[17,7,1]]

n=len(L)

memo = {}
def f(i,j,last):
  if (i,j,last) in memo:
    return memo[(i,j,last)]
  if j==3:
    return 1
  if i==n:
    return 0
  res=0
  # take one from L[i]
  for x in L[i]:
    if last > x:
      res+=f(i+1,j+1,x)
  # don't take any element from L[i]
  res += f(i+1,j,last)
  memo[(i,j,last)] = res
  return res

BIG = 10**9
print f(0,0,BIG)

rawDescription: Dynamic Programming/Memoization (counting triplets)


code:
#!/usr/bin/env python
from pyrfc import Connection, ABAPApplicationError, ABAPRuntimeError, LogonError, CommunicationError
from ConfigParser import ConfigParser
from pprint import PrettyPrinter

def main():

    try:

        config = ConfigParser()
        config.read('sapnwrfc.cfg')
        params_connection = config._sections['connection']
        conn = Connection(**params_connection)

        options = [{ 'TEXT': "FCURR = 'USD'"}]
        pp = PrettyPrinter(indent=4)
        ROWS_AT_A_TIME = 10
        rowskips = 0

        while True:
            print u"----Begin of Batch---"
            result = conn.call('RFC_READ_TABLE', \
                                QUERY_TABLE = 'TCURR', \
                                OPTIONS = options, \
                                ROWSKIPS = rowskips, ROWCOUNT = ROWS_AT_A_TIME)
            pp.pprint(result['DATA'])
            rowskips += ROWS_AT_A_TIME
            if len(result['DATA']) < ROWS_AT_A_TIME:
                break

    except CommunicationError:
        print u"Could not connect to server."
        raise
    except LogonError:
        print u"Could not log in. Wrong credentials?"
        raise
    except (ABAPApplicationError, ABAPRuntimeError):
        print u"An error occurred."
        raise

if __name__ == '__main__':
    main()

rawDescription: How/where to specify Open SQL in PyRFC call to SAP RFC_READ_TABLE


code:
import urllib2

completed_courses_url = 'http://ranger.uta.edu/~kamangar/CSE-1310-FA13/LinkedDocuments/sample_completed_courses.txt'
required_courses_url = 'http://ranger.uta.edu/~kamangar/CSE-1310-FA13/LinkedDocuments/required_courses.csv'

completed_courses_set = set([x.strip() for x in urllib2.urlopen(completed_courses_url)])
for required_course in urllib2.urlopen(required_courses_url):
    if required_course[0] != '#':
        (course,category,prerequisites,notes) = required_course.split(',')
        if set(prerequisites.split('|')).intersection(completed_courses_set):
             print required_course

rawDescription: certain lines to print

description: Certain lines to print


code:
string = "The , world , is , a , happy , place "
array = string.split(",")
for word in array:
    print word

rawDescription: python find substrings based on a delimiter

description: Python find substrings based on a delimiter


code:
a = [2, 3, 5, 6, 6, 7, 10, 11, 13, 14, 15, 16, 16, 17, 18, 20, 21]

seqlist = [] # List of Sequences
seq = []     # Current Sequence
last = -1

for item in a:
   # Start a new sequence if the gap from the last item is too big
   if item - last > 1:
       seqlist.append(seq)
       seq = []

   # only add item to the sequence if it's not the same as the last
   if item != last:
        seq.append(item)

   last = item

# Print longest sequence found
print max(seqlist)

rawDescription: Longest distinct consecutive list in Python


code:
import os

print os.getcwd() #Your current working directory
os.chdir(os.getcwd() + '/folder1/folder2/')
print os.getcwd() #Your new wordking dir

create_zip_path = "my_zip.zip"
file_to_add_to_zip = "my_file.txt"

zip_file(create_zip_path, file_to_add_to_zip)

rawDescription: Prevent zipfile writing directory to archive


code:
import scipy.misc, scipy.ndimage

# Read the image
img = scipy.misc.imread("Skel.png")

# Retain only the skeleton
img[img!=255] = 0
img = img.astype(bool)

# Fill the holes
img2 = scipy.ndimage.binary_fill_holes(img)

# Compare the two, an image without cycles will have no holes
print "Cycles in image: ", ~(img == img2).all()

# As a test break the cycles
img3 = img.copy()
img3[0:200, 0:200] = 0
img4 = scipy.ndimage.binary_fill_holes(img3)

# Compare the two, an image without cycles will have no holes
print "Cycles in image: ", ~(img3 == img4).all()

rawDescription: How can i find cycles in a skeleton image with python libraries?

description: Find cycles in a skeleton image with python libraries


code: print '{} * {} = 1'.format(x, y)

rawDescription: how do I format mathematical operators to str

description: Format mathematical operators to str


code:
class student:
    def __init__(self, student_name):
        self.name = student_name

class homework:

    def __init__(self,student_name,assignment_name,assignment_subject,grade):
        self.student = student(student_name)
        self.assignment_name = assignment_name
        self.assignment_subject = assignment_subject
        self.grade = grade

    @classmethod
    def new_hw(cls, line):
        return cls(line[0],line[1],line[2],line[3])

    @classmethod
    def get_all_grades(cls, student_name, homework_list):
        return [ x.grade for x in homework_list if student_name is x.student.name]

lines = [["abc","a","b","A"],["abc","c","d","B"],["ebc","c","d","B"]]
hw_list = [homework.new_hw(line) for line in lines]
print homework.get_all_grades("abc",hw_list)

rawDescription: class or object instead of dictionaries in Python 2

description: Class or object instead of dictionaries in Python 2


code:
import inspect

class MyUtil(object):
    @staticmethod
    def log(message, level):
        frame, module, line, function, context, index = inspect.stack()[1]
        self_name = frame.f_code.co_varnames[0]  # Name of the first argument (usually 'self')
        instance = frame.f_locals[self_name]
        class_name = instance.__class__.__name__
        print class_name

class Deployer(object):
    def deploy(self):
        MyUtil.log ("Starting deployment", "info")

Deployer().deploy()

rawDescription: How to detect callee class of staticmethod/classmethod in Python?

description: Detect callee class of staticmethod/classmethod in Python


code:
from Tkinter import *
import os
import time

class League_Jungle_Timer(Frame):

    def __init__(self):
        Frame.__init__(self)

        # initializes window
        self.master.title("Jungle Timer by BabyAchilles")
        self.master.geometry("550x300")

        self.pack(expand=YES, fill=BOTH)

        # Start Tkinter GUI Labels
        self.label1 = Label(self, text="Blue Buff")
        self.label1.pack()

        self.label2 = Label(self, text="Red Buff")
        self.label2.pack()

        self.label3=Label(self, text="Dragon", font=("Helvetica", 16))
        self.label3.pack()

        # Start Tkinter GUI Buttons
        self.Button1 = Button(self, text="Blue Buff", fg="Blue", command=self.my_timeBB)
        self.Button1.pack(side="left")

        self.Button2 = Button(self, text="Red Buff", fg="Red", command=self.my_timeRB)
        self.Button2.pack(side="right", padx=50)

        self.Button3 = Button(self, text=" Dragon ", fg="Pink", bg="Purple", command=self.my_timeDrag)
        self.Button3.pack(side="bottom",pady=50)

        self.Quit = Button(self, text="Quit", command=self.destroy)
        self.Quit.pack()

        self.label1.bind("<Button-1>", self.label_clicked)
        self.label2.bind("<Button-3>", self.label2_clicked)
        self.label3.bind("<Double-Button-1>",self.label3_clicked)

        self.timer_id_DR = None
        self.timer_id_RB = None
        self.timer_id_BB = None

    #######################################################
    # creates a timer starting at 5 min , counts down to 0 then repeats

    def my_timeDrag(self):
        self.my_timeDrag_time = [4, 59]
        if self.timer_id_DR:
            self.after_cancel(self.timer_id_DR)
            self.timer_id_DR = None
        self.my_timeDrag_after()

    def my_timeDrag_after(self):
        m, s = self.my_timeDrag_time
        self.label3.configure(text="{0}:{1:02}".format(m, s))
        self.label3.update()
        #os.system('cls')
        print "DR", m, "Minutes", s, "Seconds"

        s -= 1
        if s == 0:
           m -= 1
           s = 59
        elif m == 0:
           m = 5

        self.my_timeDrag_time = [m, s]

        self.timer_id_DR = self.after(1000, self.my_timeDrag_after)

    #######################################################
    # creates a timer starting at 5 min , counts down to 0 then repeats

    def my_timeBB(self):
        self.my_timeBB_time = [4, 59]
        if self.timer_id_BB:
            self.after_cancel(self.timer_id_BB)
            self.timer_id_BB = None
        self.my_timeBB_after()

    def my_timeBB_after(self):
        m, s = self.my_timeBB_time
        self.label1.configure(text="{0}:{1:02}".format(m, s))
        self.label1.update()
        #os.system('cls')
        print "BB", m, "Minutes", s, "Seconds"

        s -= 1
        if s == 0:
           m -= 1
           s = 59
        elif m == 0:
           m = 5

        self.my_timeBB_time = [m, s]

        self.timer_id_BB = self.after(1000, self.my_timeBB_after)

    #######################################################

    def my_timeRB(self):
        self.my_timeRB_time = [4, 59]
        #if self.timer_id_RB:
        self.after_cancel(self.timer_id_RB)
        #self.timer_id_RB = None
        self.my_timeRB_after()

    def my_timeRB_after(self):
        m, s = self.my_timeRB_time
        self.label2.configure(text="{0}:{1:02}".format(m, s))
        self.label2.update()
        #os.system('cls')
        print "RB", m, "Minutes", s, "Seconds"

        s -= 1
        if s == 0:
           m -= 1
           s = 59
        elif m == 0:
           m = 5

        self.my_timeRB_time = [m, s]

        self.timer_id_BB = self.after(1000, self.my_timeRB_after)

    #######################################################

    def label_clicked(self, event):
        self.label1.configure(self.my_timeBB())
        self.label1.update()
    def label2_clicked(self, event):
        self.label2.configure(self.my_timeRB())
        self.label2.update()
    def label3_clicked(self, even):
        self.label3.configure(self.my_timeDrag())
        self.label3.update()

##############################################################

League_Jungle_Timer().mainloop()

rawDescription: I want to have multiple click events that start timers occuring at the same time is this possible? If so how?


code:
class A(object):
    def meth(self):
        return "met"

    def __getattribute__(self, name):
        print "IN CLASS A"
        return super(A, self).__getattribute__(name)

rawDescription: print something when access to an attribute

description: Print something when access to an attribute


code:
def key(event):
    print(event.widget)
    if event.char.isdigit():
        for j, row in enumerate(board):
            for i, char in enumerate(row):
                if char.get() == event.char:
                    print "pressed", repr(event.char),i, j, blankx,blanky
                    play(i,j)
                    return # stop searching

rawDescription: Ignoring <Button-1> when <Key> is pressed Tkinter, Python


code:
lst = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
nchunks = 4
chunksize,remainder = divmod(len(lst),nchunks)
sizes = [chunksize]*nchunks
if remainder:
    sizes[-remainder:] = [chunksize+1 for x in xrange(remainder)]
sizes
idx = 0
result = []
for s in sizes:
    result.append(lst[idx:idx+s])
    idx += s

print result

rawDescription: Python split a list into X number of chunks


code:
import inspect
import new

def f():
    x, y = 1, 2
    def get():
        print 'get'
    def post():
        print 'post'

for c in f.func_code.co_consts:
    if inspect.iscode(c):
        f = new.function(c, globals())
        print f # Here you have your function :].

rawDescription: Introspecting a given function's nested (local) functions in Python


code:
def make_method(params):
    def impl(*args, **kwargs):
        # do stuff with args & kwargs
        print "I was called with {args} and {kwargs}".format(
            args=args,
            kwargs=kwargs)
        pass
    name = 'some_name'
    scope = dict(__impl=impl)
    exec """\
def {name}({pos_arg_defs}):
    'Very helpful function.'
    return __impl({pos_arg_names})
""".format(name=name,
           pos_arg_defs=', '.join('='.join((k, repr(v)))
                                  for k, v in params.iteritems()
                                  if v is not None),
           pos_arg_names=', '.join(k
                                   for k, v in params.iteritems()
                                   if v is not None)) in scope
    return scope[name]

rawDescription: Setting parameter names and defaults in method created at runtime


code:
try:
    # Note that listdir and error are globals in this module due
    # to earlier import-*.
    names = listdir(top)
except error, err:
    if onerror is not None:
        onerror(err)
    return

dirs, nondirs = [], []
for name in names:
    if isdir(join(top, name)):
        dirs.append(name)
    else:
        nondirs.append(name)

rawDescription: Does os.walk take advantage of the file type returned by the OS for efficiency?


code:
import csv
import itertools
import operator
import sys

def main():
    reader = csv.reader(sys.stdin)
    for key, bloc in itertools.groupby(reader, key=operator.itemgetter(0)):
        print '-----' + key
        print list(bloc)

if __name__ == '__main__':
    main()

rawDescription: processing stdin data blocs with python and groupby

description: Processing stdin data blocs with python and groupby


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

#original data
temp=u"""col1,col2,col3,col4,col5
1,2,3,4,5
3,4,-5,6,7
5,6,-7,8,9"""

df1 = pd.read_csv(io.StringIO(temp))
print df1
#   col1  col2  col3  col4  col5
#0     1     2     3     4     5
#1     3     4    -5     6     7
#2     5     6    -7     8     9

#copy data for next processing
df = df1.copy()

#set data by conditions
df1['col1'] = np.where(df1['col3'] > 0,  df1['col1'] + df1['col3'], np.NaN)
df1['col5'] = np.where(df1['col3'] > 0,  'blah', np.NaN)

#drop rows with NaN
df1 = df1.dropna()

#append to original df
result = pd.concat([df, df1], ignore_index=True)
print result
#   col1  col2  col3  col4  col5
#0     1     2     3     4     5
#1     3     4    -5     6     7
#2     5     6    -7     8     9
#3     4     2     3     4  blah

rawDescription: How to speed up append to an existing dataframe

description: Speed up append to an existing dataframe


code:
a = ['a.pdf','b.pdf']
b = {'a':['email1', 'email2'], 'b':['email3']}
c = {x: dict((y,)) for x, y in zip(a, b.items())}
print c

rawDescription: Creating a nested dictionary

description: Creating a nested dictionary


code:
def generator():
    while 1:
        yield random.randint(-1, 1), datetime.datetime.now()

mynumber = 1000
for random_number, current_time in generator():
    mynumber += random_number
    print mynumber, current_time

rawDescription: basic code debug python

description: Basic code debug python


code:
from collections import Counter
def is_valid_word(word, hand, list):
    if word in my_list:
        return len(Counter(word) - Counter(hand)) == 0
    return False

my_list = ['peach', 'chayote']
hand = {'a': 1, 'c': 2, 'u': 2, 't': 2, 'y': 1, 'h': 1, 'z': 1, 'o': 2}

print is_valid_word("chayote", hand, my_list)
# False
print is_valid_word("peach", hand, my_list)
# False

rawDescription: If Statement on Dict

description: If Statement on Dict


rawDescription: format the different types of date in CSV using python 2.6

description: Format the different types of date in CSV using python 2.6


code:
id = link["post"].value
print id

rawDescription: Passing parameters to a CGI program using the URL (python)


code:
import hashlib
from binascii import a2b_hex

def Encrypt_Pass(password, authenticator, secret):
    m = hashlib.md5()
    m.update(secret + a2b_hex(authenticator))
    return "".join(chr(ord(x) ^ ord(y)) for x, y in zip(password.ljust
       (16,'\0')[:16], m.digest()[:16]))

result = Encrypt_Pass("cisco123","344c71c77a2b845b8856ffa968740b73","sharedsecret")
ciphertext = "6ed3a35440abe69b2e8698109b809932"#plaintext is cisco123
print result.encode("hex")

rawDescription: Unable to encrypt plaintext to password

description: Unable to encrypt plaintext to password


code:
import sys
message = sys.stdin.readlines()
print message

rawDescription: How to finish sys.stdin.readlines() input?

description: Finish sys.stdin.readlines() input


code:
import pickle

class TasksError(Exception):
    def __init__(self, value):
        self.value = value
    def __str__(self):
        return repr(self.value)

class Task(object):
    def __init__(self, task = () ):
        if task ==():
            raise TasksError('Empty task.')
        self.name = task[0]
        self.date = task[1]
        self.priority = task[2]

    def __str__(self):
        output = '''Name: %s
Date: %s
Priority: %s
''' % ( self.name,
        self.date,
        self.priority )
        return output

class Tasks(object):
    def __init__(self, container = []):
        self.container = [ Task(todo) for todo in container ]

    def find_by_priority(self, priority = 'high'):
        # example method to seek and print high priority tasks
        # using this example, you will be able to conduct many
        # types of searches
        results = [ task
                   for task in self.container
                   if task.priority == priority ]
        return results

    def sort_by_date(self):
        # example method of how to sort, again, many other
        # ways of sorting can be implemented, this is just
        # to demonstrate the principle
        # for more info on sorting,
        # visit:  http://wiki.python.org/moin/HowTo/Sorting
        self.container = sorted(self.container,
                                key=lambda task: task.date)

    def add(self, task):
        if task == '':
            raise TasksError('Empty task')
        self.container.append( Task(task) )

    def save(self):
        try:
            output = open('tasks.pkl', 'wb')
            pickle.dump(self.container, output)
            output.close()
        except:
            raise TasksError('Failed to save.')

    def load(self):
        try:
            pkl_file = open('tasks.pkl', 'rb')
            self.container = pickle.load(pkl_file)
            pkl_file.close()
        except:
            raise TasksError('Failed to load')

    def __str__(self):
        output = '\n'.join( [ str(todo) for todo in self.container ] )
        return output

if __name__== "__main__":
    divider = '-' * 30 + '\n'

    tasks = Tasks( [("birthday","20121111","high"),
                    ("christmas","20121225","medium"),
                    ("easter","20120405","low")] )
    print 'Three original tasks:\n'
    print tasks # prints out three tasks
    print divider

    tasks.add( ("new-task","20120320","high") )
    print 'Three original plus one new task:\n'
    print tasks # prints out four tasks
    print divider

    tasks.save() # pickles the task list and saves to disk

    tasks = Tasks( container = [] ) # creates a new, empty task list
    print 'Empty task list:\n'
    print tasks # prints out empty list
    print divider

    tasks.load() # loads the pickled list
    print 'The four pickled tasks, reloaded:\n'
    print tasks # prints out four tasks
    print divider

    while True:
        print divider, '''Make your selection:
1. Add new task
2. Print all tasks
3. Save tasks
4. Load tasks from disk
5. Find high priority tasks
6. Sort by date
<ENTER> to quit
'''
        try:
            menu_choice = int(input("Select a number from the menu: "))
        except:
            print 'Goodbye!'
            break

        if menu_choice == 1:
            # note: no error checking here
            # even an empty input is accepted
            task = raw_input (">>> Task: ")
            date = raw_input (">>> Date as string YYYYMMDD: ")
            priority = raw_input (">>> Priority: ")
            todo = (task, date, priority)
            # note that here you should add a task
            # your method created a NEW task list
            # and replaced the old one
            tasks.add( todo )
            print tasks
        elif menu_choice == 2:
            print divider, 'Printing all tasks'
            print tasks
        elif menu_choice == 3:
            print divider, 'Saving all tasks'
            tasks.save()
        elif menu_choice == 4:
            print divider, 'Loading tasks from disk'
            tasks.load()
        elif menu_choice == 5:
            print divider, 'Finding tasks by priority'
            results = tasks.find_by_priority(priority='high')
            for result in results: print result
        elif menu_choice == 6:
            print divider, 'Sorting by date'
            tasks.sort_by_date()
            print tasks

rawDescription: how to load a file as a list and place it in a different class to the one its loading in

description: Load a file as a list and place it in a different class to the one its loading in


code:
import socket, struct

def ip2long(ip):
    packed = socket.inet_aton(ip)
    return struct.unpack("!L", packed)[0]

def long2ip(n):
    unpacked = struct.pack('!L', n)
    return socket.inet_ntoa(unpacked)

def expandrange(rng):
    # expand '1.1.1.1-7' to ['1.1.1.1', '1.1.1.7']
    start, end = [ip.split('.') for ip in rng.split('-')]
    return map('.'.join, (start, start[:len(start) - len(end)] + end))

def compressrange((start, end)):
    # compress ['1.1.1.1', '1.1.1.7'] to '1.1.1.1-7'
    start, end = start.split('.'), end.split('.')
    return '-'.join(map('.'.join,
          (start, end[next((i for i in range(4) if start[i] != end[i]), 3):])))

def strings_to_ints(ranges):
    # turn range strings into list of lists of ints
    return [map(ip2long, rng) for rng in map(expandrange, ranges)]

def ints_to_strings(ranges):
    # turn lists of lists of ints into range strings
    return [compressrange(map(long2ip, rng)) for rng in ranges]

def consolodate(ranges):
    # join overlapping ranges in a sorted iterable
    iranges = iter(ranges)
    startmin, startmax = next(iranges)
    for endmin, endmax in iranges:
        # leave out the '+ 1' if you want to join overlapping ranges
        # but not consecutive ranges.
        if endmin <= (startmax + 1):
            startmax = max(startmax, endmax)
        else:
            yield startmin, startmax
            startmin, startmax = endmin, endmax
    yield startmin, startmax

def convert_consolodate(ranges):
    # convert a list of possibly overlapping ip range strings
    # to a sorted, consolodated list of non-overlapping ip range strings
    return list(ints_to_strings(consolodate(sorted(strings_to_ints(ranges)))))

if __name__ == '__main__':
    ranges = ('1.1.1.1-7',
              '2.2.2.2-10',
              '3.3.3.3-3.3.3.3',
              '1.1.1.4-25',
              '2.2.2.4-6')
    print convert_consolodate(ranges)
    # prints ['1.1.1.1-25', '2.2.2.2-10', '3.3.3.3-3']

rawDescription: Consolidate IPs into ranges in python

description: Consolidate IPs into ranges in python


code:
# loop through all pages in analysis
for p in Document.Pages:
    print p.Title, p.Id

# loop through all visuals on a page
page_index = 3    # integer index of the page (left to right; starts at 0)
for v in Document.Pages[page_index].Visuals:
    print v.Title, v.Id

# try to find a specific visual on a page by title
for p in Document.Pages:
    for v in p.Visuals:
        if v.Title == "sometitle": visual_id = v.Id

# or by Id, if you know it already
Document.Pages[1].Visuals.TryGetVisual(visual_id)

rawDescription: Spotfire Ironpy how to edit specific visualization


code:
from datetime import datetime, timedelta
a = "05:27:00"
b = datetime.strptime(a, "%H:%M:%S")
print b
c = timedelta(minutes=15)
print c
print b+c

rawDescription: Fetch time from mysql using python

description: Fetch time from mysql using python


code:
#!/usr/bin/env python
# vim:ai:et:ts=2:sw=2:bg=dark
from twisted.internet import protocol
from twisted.protocols.basic import LineOnlyReceiver
from twisted.application import service

from xml.etree import ElementTree as ET

from aMuleClass import amulecmd

class DialogueProtocol(LineOnlyReceiver):
  def connectionMade(self):
    print "Connected: %s" % self.transport.getPeer().host
  def lineReceived(self, line):
    parsed= ET.XML(line)
    if parsed.attrib['type'] == 'request':
      if parsed.attrib['prompt'] == 'results':
        self.transport.write(self.factory.mule.results())
      elif parsed.attrib['prompt'] == 'downloads':
        self.transport.write(self.factory.mule.downloads())
      else:
        print "Invalid request: %s\n" % line
    else:
      query= parsed.attrib['value']
      if parsed.attrib['type'] == 'search':
        print "must search for %s" % query
        self.factory.mule.search(query)
      elif parsed.attrib['type'] == 'cancel':
        print "must cancel %s" % query
        self.factory.mule.command("cancel %s" % query)
      elif parsed.attrib['type'] == 'download':
        print "must download %s" % query
        self.factory.mule.command("download %s" % query)

class MyService(service.Service):
  def __init__(self,port=14000):
    self.port = port
  def startService(self):
    self.factory = protocol.Factory()
    self.factory.protocol = DialogueProtocol
    from twisted.internet import reactor
    reactor.callWhenRunning(self.startListening)
  def startListening(self):
    self.factory.mule = amulecmd()
    from twisted.internet import reactor
    self.listener = reactor.listenTCP(self.port,self.factory)
    print "Started listening"
  def stopService(self):
    self.listener.stopListening()

if __name__ == '__main__':
  pass
else:
  application = service.Application("aMuleSocket")
  services = service.IServiceCollection(application)
  MyService().setServiceParent(services)

rawDescription: simple twisted server (twistd .tap)with a pexpect instance error

description: Simple twisted server (twistd .tap)with a pexpect instance error


code:
for event in pygame.event.get():
  if event.type == pygame.KEYDOWN:
    print pygame.key.name(event.key)

rawDescription: Pygame: Is there any easy way to find the letter/number of ANY alphanumeric pressed?


code:
import base64
import zlib

flash =  "eNrj4mZkrShgWdHBx1mUmJfNosQIACUhBCI="

original = '{"xp": 656398, "rank": 34}'
encoded = original.encode('zlib').encode('base64')
decoded = encoded.decode('base64').decode('zlib')
print original
print encoded
print decoded

rawDescription: Reading base64/zlib encoded string from actionscript in python


code:
import random

sys_random = random.SystemRandom()

def getRandomID():
    token = ''
    letters = "abcdefghiklmnopqrstuvwwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
    for i in range(1,36):
        token = token + sys_random.choice(letters)
    return token

print getRandomID()

rawDescription: random.choice() returns same value at the same second, how does one avoid it?

description: Random.choice() returns same value at the same second, how does one avoid it


rawDescription: How to add numbers in a sequence up to user input than add one?

description: Add numbers in a sequence up to user input than add one


code:
with open('input.txt') as f:
    isBeta = False
    beta = {}
    alpha = {}
    for line in f:
        if line.strip() == 'Beta:':
            isBeta = True
            continue
        elif line.strip() == 'Alpha:' or not line.strip():
            continue

        try:
            k,v = line.split()
        except:
            continue
        if isBeta:
            beta[k] = v
        else:
            alpha[k] =  v

    for k in sorted(set(alpha.keys()+beta.keys())):
        if k not in alpha:
            print '| -- | ' + k + '=' + beta[k] + '|'
        elif k not in beta:
            print '|' + k + '=' + alpha[k] + ' | -- |'
        elif alpha[k] == beta[k]:
            continue
        else:
            print '|' + k + '=' + alpha[k] + ' | ' + k + '=' + beta[k] + '|'

rawDescription: comparison between two lists. Is it possible to get an output as listed?

description: Comparison between two lists. Is it possible to get an output as listed


code:
import tornado.ioloop
import tornado.httpclient

class Fetcher(object):
    def __init__(self, ioloop):
        self.ioloop = ioloop
        self.client = tornado.httpclient.AsyncHTTPClient(io_loop=ioloop)

    def fetch(self, url):
        self.client.fetch(url, self.handle_response)

    @property
    def active(self):
        """True if there are active fetching happening"""

        return len(self.client.active) != 0

    def handle_response(self, response):
        if response.error:
            print "Error:", response.error
        else:
            print "Got %d bytes" % (len(response.body))

        if not self.active:
            self.ioloop.stop()

def main():
    ioloop = tornado.ioloop.IOLoop.instance()
    ioloop.add_callback(scrapeEverything)
    ioloop.start()

def scrapeEverything():
    fetcher = Fetcher(tornado.ioloop.IOLoop.instance())

    listOfIds = [23423, 52356, 63462, 34673, 67436]

    for id in listOfIds:
        fetcher.fetch("http://mydatasite.com/?data_id=%d" % id)

if __name__ == '__main__':
    main()

rawDescription: Launching asynchronous, Node.js-like HTTP request in Tornado


code:
def bind_method(**config):
    class InstagramAPIMethod(object):
        def __init__(self, *args, **kwargs):
            self.return_json = kwargs.pop("return_json", False)
    def _call(*args, **kwargs):
        method = InstagramAPIMethod(*args, **kwargs)
        return method.__dict__
    return _call

class InstAPI():
  result = bind_method(foo='bar')

api = InstAPI()
print api.result()

from mock import patch
#Patch directly InstAPI.result
with patch("__main__.InstAPI.result",return_value={'return_json': True}):
    api = InstAPI()
    print api.result()

#The wrapper of bind_method
def new_instapi_result(**kwargs):
    def functor(**config):
        d = bind_method(**config)()
        d.update(kwargs)
        return d
    return functor

#Patch and wrap bind_method
with patch("__main__.InstAPI.result",side_effect=new_instapi_result(return_json = True)):
    api = InstAPI()
    print api.result()

rawDescription: How to mock an instance variable in nested class

description: Mock an instance variable in nested class


code:
def sym(a):
    assert set(len(item) for item in a) == {len(a)}
    return all(a[i][j] == a[j][i] for i in range(len(a)) for j in range (len(a)))

a = [[1, 0], [0, 1]]
print sym(a)
# True
a = [[1, 0]]
print sym(a)
# AssertionError
a = [[1, 1], [0, 0]]
print sym(a)
# False

rawDescription: Determining if a 1-0 matrix that represents a relation is symmetric.


code:
url = '''http://test.nl/be/nl/product/abc-dadf-adsfsw-12234/'''
url = re.sub("/be/(?=.*/product/)", "/nl/", url)
# or using word boundary around `be` to handle .be/ .be. etc
# url = re.sub("\bbe\b(?=.*/product/)", "nl", url)
print url

rawDescription: replace word of first part string in python

description: Replace word of first part string in python


code:
import code
import sys

def DebugKeyboard(banner="Debugger started (CTRL-D to quit)"):

    # use exception trick to pick up the current frame
    try:
        raise None
    except:
        frame = sys.exc_info()[2].tb_frame.f_back

    # evaluate commands in current namespace
    namespace = frame.f_globals.copy()
    namespace.update(frame.f_locals)

    print "START DEBUG"
    code.interact(banner=banner, local=namespace)
    print "END DEBUG"

rawDescription: How can I start the python console within a program (for easy debugging)?

description: Start the python console within a program (for easy debugging)


code:
d = {}
for model in models:
    d[model] = True
for word in mshtv['item_2_name'].split(" "):
    if word in d:
        print word

rawDescription: Python Spider: Extract certain word form results


code:
import xmlrpclib, io, gzip

url = 'https://api.opensubtitles.org/xml-rpc'
server = xmlrpclib.Server(url)
token = server.LogIn('', '', 'en', 'OSTestUserAgent')['token']
sub_id = 1951894257
resp = server.DownloadSubtitles(token, [1951894257])
if resp['status'] == '200 OK':
    compressed_data = resp['data'][0]['data'].decode('base64')
    sub_text = gzip.GzipFile(fileobj=io.BytesIO(compressed_data)).read()
    print sub_text

rawDescription: Python: DownloadSubtitle method from opensubtitles api returning blank data


code:
import json
j = '{"#foo": 6}'
print json.loads(j)
print json.loads(j)[u'#foo']
print json.loads(j)['#foo']

rawDescription: Python how to handle # in a dictionary

description: Python how to handle # in a dictionary


rawDescription: python group a list of integer, with nearest values

description: Python group a list of integer, with nearest values