code:
import Image

im = Image.open("path\to\nos_radar_285.gif")

try:
    i = 0
    while 1:
        im.seek(im.tell() + 1)

        # get color of each pixel
        pal_inds = list(im.getdata())
        pal_rgbs = list(im.convert('RGB').getdata())

        # change a specific color to another color
        # eg. (190, 5, 3) to (138, 218, 47)
        pal_rgbs = [color if color != (190, 5, 3) else (138, 218, 47) for color in pal_rgbs]

        # remake a new palette
        pal_map = dict((zip(pal_inds, pal_rgbs)))
        palette = [pal_map[i] for i in sorted(pal_map)]
        flat_palette = [ind for rgb in palette for ind in rgb]

        # put the palette back to image
        im.putpalette(flat_palette)
        im.show()
        im.save('name%d' %i, 'jpg')
        i+=1
except EOFError:
    print 'done'

rawDescription: Give pixels with certain color a different RGB value in python


code:
class Monster(yaml.YAMLObject):
    yaml_tag = u'!Monster'
    def __init__(self, name, hp, ac, attacks):
        self.name = name
        self.hp = hp
        self.ac = ac
        self.attacks = attacks
    def __repr__(self):
        return "%s(name=%r, hp=%r, ac=%r, attacks=%r)" % (
            self.__class__.__name__, self.name, self.hp, self.ac, self.attacks)

print yaml.load("""
--- !Monster
name: Cave spider
hp: [2,6]    # 2d6
ac: 16
attacks: [BITE, HURT]
""")

rawDescription: python : read a line, then instantiate a class named in the line

description: Python : read a line, then instantiate a class named in the line


code:
keys = [1,2,3,4,5,6,7,8,9,10,11,12]

def key_loop(key):
    test_df = pd.DataFrame(np.random.randn(1,4), columns=['a','b','c','d'])
    test_list = test_df.ix[0].tolist()
    return test_list

if __name__ == "__main__":
    try:
        pool = Pool(processes=8)
        resultset = pool.imap(key_loop, keys, chunksize=200)

        with open("C:\\Users\\mp_streaming_test.csv", 'w') as file:
            writer = csv.writer(file)
            for listitem in resultset:
                writer.writerow(listitem)

        print "finished load"
    except:
        print 'There was a problem multithreading the key Pool'
        raise

rawDescription: How to stream results from Multiprocessing.Pool to csv?

description: Stream results from Multiprocessing.Pool to csv


code:
def fetch_unique_tuples(original_set, tuple_size):
    from itertools import combinations

    good = []
    used = []
    for i in combinations(original_set,tuple_size):
        lst = list([tuple(sorted(j)) for j in combinations(i,2)])
        if not any(l in used for l in lst):
            used.extend(lst)
            good.append(tuple(sorted(i)))
    return sorted(good)

elements = ['CAGG', 'CTTC', 'ACCT', 'TGCA', 'CCTG', 'CAAA', 'TGCC', 'ACTT', 'TAAT', 'CTTG', 'CGGC', 'GGCC', 'TCCT', 'ATCC', 'ACAG', 'TGAA', 'TTTG', 'ACAA', 'TGTC', 'TGGA', 'CTGC', 'GCTC', 'AGGA', 'TGCT', 'GCGC', 'GCGG', 'AAAG', 'GCTG', 'GCCG', 'ACCA', 'CTCC', 'CACG', 'CATA', 'GGGA', 'CGAG', 'CCCC', 'GGTG', 'AAGT', 'CCAC', 'AACA', 'AATA', 'CGAC', 'GGAA', 'TACC', 'AGTT', 'GTGG', 'CGCA', 'GGGG', 'GAGA', 'AGCC', 'ACCG', 'CCAT', 'AGAC', 'GGGT', 'CAGC', 'GATG', 'TTCG']
uniques = fetch_unique_tuples(elements, 3)
print len(uniques)

rawDescription: Python: (sampling with replacement): efficient algorithm to extract the set of DISSIMILAR N-tuples from a set


code:
import time
print "Think of a number between 1 and 100"
print "Then I shall guess the number"

time.sleep(3)

print "I guess", computerguess
raw_input ("Is it lower or higher?")

rawDescription: Can I make a python delay?

description: Make a python delay


code:
import Queue
import subprocess
import threading

def checkStdout(p, q):
    s = p.stdout.read(1)
    if len(s) > 0:
        q.put(s)

def run():
    p = subprocess.Popen(['cat'], stdout=subprocess.PIPE, close_fds=True)
    print 'pid %d started' % p.pid
    q = Queue.Queue()
    t = threading.Thread(target=checkStdout, args=(p, q))
    t.daemon = True
    t.start()
    try:
        canGet = q.get(timeout=5)
        print 'has output'
    except Queue.Empty:
        print 'no output'
        # kill process
        p.kill()

if __name__ == '__main__':
    run()

rawDescription: Monitor status of a command I spawn using Popen


code:
from collections import defaultdict

_linked = defaultdict(set)

class Linkable(object):

    def link(self, *others):
        _linked[self].update(others)
        for each in others:
            _linked[each].add(self)

    def unlink(self, *others):
        _linked[self].difference_update(others)
        for each in others:
            _linked[each].discard(self)

    def links(self):
        return set(_linked[self])

class NameHierarchy(object):

    def __init__(self, name, parent=None):
        self.name = name
        self.parent = parent or self
        self.path = parent.path + [self] if parent else [self]

    def __str__(self):
        return '.'.join(each.name for each in self.path)

    def add(self, name):
        if name not in self.__dict__:
            setattr(self, name, self.__class__(name, self))

class LinkedNameHierarchy(Linkable, NameHierarchy):
    pass

Category = LinkedNameHierarchy('Category')

Category.add('MyComponent')
Category.MyComponent.add('Useful')

Category.add('Data')
Category.Data.add('All')
Category.Data.add('Count')

Event = LinkedNameHierarchy('Event')

Event.add('MyComponent')
Event.MyComponent.add('ComponentEvent')
Event.MyComponent.add('ComponentEventWCategory')

Event.MyComponent.ComponentEventWCategory.link(Category.MyComponent.Useful)

Event.add('Data')
Event.Data.add('Created')
Event.Data.add('Updated')
Event.Data.add('Deleted')

Event.Data.Created.link(Category.Data.All, Category.Data.Count)
Event.Data.Updated.link(Category.Data.All)
Event.Data.Deleted.link(Category.Data.All, Category.Data.Count)

def show_links():
    print
    print '----8<--------8<--------8<--------8<--------8<--------8<----'
    for k, v in sorted(_linked.iteritems(), key=lambda x:str(x[0])):
        print
        print k
        for each in sorted(v, key=str):
            print '        ', each

show_links()

Event.Data.Created.unlink(Category.Data.Count)

show_links()

rawDescription: Python Event-Category Mapping Design

description: Python Event-Category Mapping Design


code:
import sys, string, csv

#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict =  dict()
reader = csv.reader(text_file, delimiter='\t')
for line in reader:
    FID_GC_dict[line[0]] = int(line[1])
text_file.close()

#Importing neighbor list file for each FID value
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb")
Entries = Neighbors_file.readlines()
Neighbors_file.close()
Neighbors_List = map(string.split, Entries)
#print Neighbors_List

#creates a list of the current FID
FID = [x[0] for x in Neighbors_List]
#print FID

#Calculate when to end the simulations (neglible change in land use)
tot_cells = len(FID)
end_sim = tot_cells
p = 0.0001

#Performs cellular automata rules on land use grid codes
while (end_sim > tot_cells*p):
    gridList = []
    for nlist in Neighbors_List:
        row = []
        for item in nlist:
            row.append(FID_GC_dict[item])
        gridList.append(row)
    #print gridList

    i = iter(FID)
    count = 0
    for glist in gridList:
        Cur_FID = i.next()
        Cur_GC = glist[0]
        glist.sort()
        lr_Value = glist[-1]
        if lr_Value < 6:
            tie_LR = glist.count(lr_Value)
            if tie_LR >= 4 and lr_Value > Cur_GC:
                FID_GC_dict[Cur_FID] = lr_Value
                print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID]
                count += 1
    end_sim = count
    print count

rawDescription: looping until the number of cells changed is very small

description: Looping until the number of cells changed is very small


code:
myinput = "abcba"
print " ".join(bin(ord(character))[2:] for character in myinput)

rawDescription: Letter to Binary

description: Letter to Binary


code:
import pygtk
pygtk.require("2.0")
import gtk

class Display(object):

    def __init__(self):
        self.fail = "This will fail to display"
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", lambda w: gtk.main_quit())
        window.show()

class Test(object):

    def __init__(self, display):
        print display.fail

d = Display()
t = Test(d)

gtk.main()

rawDescription: How can I run gtk.main() asynchronsly in pygtk?

description: Run gtk.main() asynchronsly in pygtk


code:
score = raw_input('what is your score?')
try:
    score = float(score)
    if score >= 0.9:
        print "A"
    elif score >= 0.8:
        print "B"
    elif score >= 0.7:
        print "C"
    elif score >= 0.6:
        print "D"
    elif score < 0.6:
        print "F"
except:
    score = -1
    print 'Bad Score, Try Again'

rawDescription: Python printing more than I want

description: Python printing more than I want


code:
def element_count(p):
  q = p[:]
  count = 0
  while q:
    entry = q.pop()
    if isinstance(entry, list):
      q += entry
    count += 1
  return count

print element_count([1, [], 3])
print element_count([1, [1, 2, [3, 4]]])
print element_count([[[[[[[[1, 2, 3]]]]]]]])

rawDescription: Count all elements in list of arbitrary nested list without recursion


code:
from twisted.internet import defer

def twisAmaz(contents):
    stonesoup = BeautifulStoneSoup(contents)
    ret = {}
    if stonesoup.find("mediumimage") is None:
        ret['imageurl'] = "/images/notfound.png"
    else:
        ret['imageurl'] = stonesoup.find("mediumimage").url.contents[0]
    ret['usedPdata'] = stonesoup.find("lowestusedprice")
    ret['newPdata'] = stonesoup.find("lowestnewprice")
    ret['titledata'] = stonesoup.find("title")
    ret['reviewdata'] = stonesoup.find("editorialreview")
    if stonesoup.find("asin") is not None:
        ret['asin'] = stonesoup.find("asin").contents[0]
    else:
        ret['asin'] = 'None'
    return ret

callbacks = []
for tmpISBN in isbn:  #Go through ISBN numbers and get Amazon API information for each
    callbacks.append(getPage(fetchInfo(tmpISBN)).addCallback(twisAmazon))

def printResult(result):
    for e, (success, value) in enumerate(result):
        print ('[%r]:' % isbn[e]),
        if success:
            print 'Success:', value
        else:
            print 'Failure:', value.getErrorMessage()

callbacks = defer.DeferredList(callbacks)
callbacks.addCallback(printResult)

reactor.run()

rawDescription: Making a python program wait until Twisted deferred returns a value


code:
import xlrd

excel_sheet = xlrd.open_workbook("teacher.xlsx")
sheet1= excel_sheet.sheet_by_name('parents')

for i in range(0, sheet1.nrows):
    row = sheet1.row_slice(i)
    Gname = row[0].value
    Fname = row[1].value
    Lname = row[2].value

    print Gname
    print Fname
    print Lname

rawDescription: python xlrd fetch first row value

description: Python xlrd fetch first row value


code:
import json
sequence = '[%s]' % ','.join(sequence)
data = json.loads(sequence)
print data[0]['First'], data[0]['Second']

rawDescription: Extracting a string between specified characters in python


code:
from bs4 import BeautifulSoup
tree = BeautifulSoup('
Foo

Bar
<para>Some text.</para>', 'xml')
newpara = '<para>Some <emph type="bold">new</emph> text.</para>'
newpara = BeautifulSoup(newpara, 'xml')
tree.find('para', text='Some text.').replaceWith(newpara.para)
print tree

rawDescription: Replace a tag with another tag in BeautifulSoup


code:
e = d['entries'][0]
print e["media_thumbnail"][0]["url"]

rawDescription: How to get image from this BBC feed using Python and Universal Feed Parser

description: Get image from this BBC feed using Python and Universal Feed Parser


rawDescription: Decimal module: rounding

description: Decimal module: rounding


code:
input=open('scores.txt')
scores=input.read()
scores=scores.split()
print min(scores)

rawDescription: Finding the numerically lowest line in a text file


code:
import scipy.stats

print scipy.stats.f.isf(0.2, 1, 2)  # => 3.5555555555555576

rawDescription: Python/SciPy version of Excel FInv function


code:
filename = "Foo.txt";
print 'Now, I will write a header to the original file to display copyright.'

append_copy = open(filename, "r")
original_text = append_copy.read()
append_copy.close()

append_copy = open(filename, "w")
append_copy.write("Copyright -- Ryan -- 2014\n")
append_copy.write(original_text)
append_copy.close()

print 'File closed and data written!'

rawDescription: Appending string data to top of text file in Python?


code:
from datetime import datetime
import time
import functools

def t_access(method):
    @functools.wraps(method)
    def wrapper(self):
        self.timestamp = datetime.now()
        method(self)
    return wrapper

class Foo(object):
    @t_access
    def bar(self):
        print "method bar() called"

f = Foo()
f.bar()
print f.timestamp
time.sleep(5)
f.bar()
print f.timestamp

rawDescription: What's the most elegant way of keeping track of the last time a python object is accessed?


code:
import matplotlib.pyplot as plt
import numpy as np
import scipy
from sklearn import datasets, svm, metrics
import uuid
import os
from os import listdir
from os.path import isfile, join

def scipySaveImage(path,name,image):
    scipy.misc.imsave(path + name,image)

def scipyLoadImage(path,flatten=0):
        return scipy.misc.imread(path,flatten)

def saveAsUniqueImage(path,image,target):
        # Defining our parts
        target = str(target)
        unique = str(uuid.uuid1())
        extension = '.png'
        name = target + '-' + unique + extension
        scipySaveImage(path,name,image)

def saveDataSet(path,dataset):
    for i,image in enumerate(dataset.images):
        target = dataset.target[i]
        saveAsUniqueImage(path,image,target)

def predict(classifier,data):
        return classifier.predict(data[:len(data)])

def shape_data(data):
        n_samples = len(data)
        return data.reshape((n_samples, -1))

def train_classifer(data):
        n_samples = len(data)
        data = shape_data(data)
        classifier = svm.SVC(gamma=0.001)
        classifier.fit(data[:n_samples], digits.target[:n_samples])
        return classifier

# A Dataset Object
class dataset:
        def __init_():
                return

def loadTestDataset(path):
        data = dataset()
        targets = []
        filenames = []
        images = []

        imagePaths = [ path + f for f in listdir(path) if isfile(join(path,f))]
        imagePaths = sorted(imagePaths)

        for x in imagePaths:
                filename = os.path.basename(x)
                target = filename.split("-")
                target = target[0]
                target = int(target)

                targets.append(target)
                filenames.append(filename)
                images.append(scipy.misc.imread(x,1))

        data.target = np.asarray(targets)
        data.images = np.asarray(images)
        data.filenames = filenames

        return data

# The folder where my digit images will go in
training_path = 'Digits/'

# Saving the images in the digit dataset into the Digit folder
# Comment out this line if you already have the digits
saveDataSet(training_path,datasets.load_digits())

# Loading all the images from a folder into a dataset
digits = loadTestDataset(training_path)

# Reloads the images from our training folder
test_digits = loadTestDataset(training_path)

# Shaping the images, I believe this just makes the rows 1 pixel so an 8x8 image will now be 64x1
test_digits.images = shape_data(test_digits.images)

# Training our classifer so it knows how to classify digits
digits_model = train_classifer(digits.images)

# The target that our model thinks is being represented
prediction = predict(digits_model,test_digits.images[300])

# Printing the filename ( which includes the target ) and our models prediction
print test_digits.filenames[300]
print prediction

rawDescription: Opening images with OpenCV 2.4.11 to use as a dataset for sklearn


rawDescription: Remove last digit from integer and put zero in python


code:
import sys

class MyClass(object):
    def func1(self):
        frame = sys._getframe(1)
        if frame.f_code.co_name == 'func2':
            print "called from func2"
        else:
            print "called from elsewhere"
    def func2(self):
        self.func1()

c = MyClass()
c.func1()
c.func2()

rawDescription: python function calling within a class

description: Python function calling within a class


code:
import requests
import json
from bs4 import BeautifulSoup

# Use requests library to get the JSON data
JSONDATA = requests.request("GET", "http://www.websitehere.com/") #Make sure you include the http part
# Load it with JSON
JSONDATA = JSONDATA.json()

# Cycle through each `line` in the JSON
for line in JSONDATA['line']:
    # Load stripped html in BeautifulSoup
    soup = BeautifulSoup(line['text'])
    # Prints tidy html
    print soup.prettify()

rawDescription: Load a JSON file, correct the html and load into BeautifulSoup


code:
class mcs(type):
    def __repr__(self):
        return "test - from metaclass"

class a(object):
    __metaclass__ = mcs
    def __repr__(self):
        return "test"

print a    # test - from metaclass
print a()  # test

rawDescription: Return text from class without using any brackets (python)


code:
import datetime
import random
import numpy as np
import math
import sys
from openpyxl import Workbook
from openpyxl.cell import get_column_letter
from openpyxl import load_workbook
from tempfile import TemporaryFile
from random import normalvariate, expovariate, seed, gammavariate
from math import sqrt
from itertools import groupby

## Test Lists for testing code
lst_FC_error = []

## Using Lists and Excel
wb = Workbook()
dest_filename = r'RHF_Loop_test4py.xlsx'

EC_PlanData = []
sheet1 = wb.create_sheet(0)
sheet1.title = "EC_PlanData"

EC_FC_Data_original = []
sheet2 = wb.create_sheet(1)
sheet2.title = "EC_FC_Data_original"

FC_AccData = []
sheet5 = wb.create_sheet(4)
sheet5.title = "FC_Acc_Data"

EC_FC_Data_commited = []
sheet6 = wb.create_sheet(5)
sheet6.title = "EC_FC_Data_commited"

FC_Error = 0
alpha = 50
beta = 10
CurrentWeekRangeSize = 10
CurrentWeek = range(1,CurrentWeekRangeSize,1)
RemainingFCError = .2

for Global_counter in CurrentWeek:

    if Global_counter == 1:

        for index in range(Global_counter,Global_counter + 52 + CurrentWeekRangeSize,1):
            PlanWeek = index
            EC_demand = 500.0
            if  EC_demand <= 0:
                EC_demand = EC_demand *(-1)
            EC_PlanData.append(EC_demand)
            d = sheet1.cell(row = Global_counter, column = PlanWeek)
            print "hello", Global_counter, PlanWeek
            d2 = sheet2.cell(row = Global_counter, column = PlanWeek)
            d.value = EC_demand
            d2.value = EC_demand
        wb.save('RHF_Loop_test4py.xlsx')
        print "-------------"
    else:
        #this was the original logic:
        #for index in range(Global_counter,Global_counter + 52,1):
        for index in range(Global_counter,52,1):
            PlanWeek = index
            DiffWeek = PlanWeek - Global_counter
            Sigma = 1
            mu = 0
            FC_VZ = random.normalvariate(mu, Sigma)
            if FC_VZ > 0:
               FC_VZ = 1
            FC_Error = FC_Error+RemainingFCError   ## decrease demand
            fcerr = sheet5.cell(row = Global_counter, column = PlanWeek)
            fcerr.value = FC_Error

            Prev_Week = Global_counter - 1
            ECDD1 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
            print Prev_Week, PlanWeek, ECDD1
            ECDD2 = sheet1.cell(row=Prev_Week, column=PlanWeek).value
            EC_demand = ECDD1 - (ECDD2*FC_Error)

            FC_Error = 0
            ECPD = sheet1.cell(row = Global_counter, column=PlanWeek)
            ECPD.value = EC_demand
            FCAD = sheet6.cell(row = Global_counter, column=PlanWeek)
            FCAD.value = FC_Error
            IEFD = sheet2.cell(row = Global_counter, column=PlanWeek)
            IEFD.value = EC_demand
            EC_demand = 0
        wb.save('RHF_Loop_test4py.xlsx')

rawDescription: Python Looping Confusion

description: Python Looping Confusion


code:
import csv
matrix_file = open('data1.csv')
matrix_reader = csv.reader(matrix_file, delimiter="\t")
header=matrix_reader.next()[0].split() #now header is ['WHO', 'THING1', 'THING2']

for i in matrix_reader:
        line=i[0].split()
        print line[0], "\t",header[1], "\t",line[1]
        print line[0], "\t",header[2], "\t",line[2]

rawDescription: Python trouble exiting a 'while' loop for simple script


code:
from heapq import nlargest

data = [0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]
print nlargest(3, data)

rawDescription: How to find k biggest numbers from a list of n numbers assuming n > k

description: Find k biggest numbers from a list of n numbers assuming n > k


code:
def swap(ary, idx1, idx2):
    ary[idx1], ary[idx2] = [ary[i] for i in (idx2, idx1)]

def permutations(ordered, movements):
    size = len(ordered)
    for i in range(1, len(ordered)):
        for j in range(movements[i]):
            swap(ordered, i-j, i-j-1)
    return ordered

numberofcases = input()
for i in range(numberofcases):
    sizeofcase = input()
    movements = [int(s) for s in raw_input().split()]
    ordered = [str(i) for i in range(1, sizeofcase+1)]
    ordered = permutations(ordered, movements)
    output = " ".join(ordered)
    print output

rawDescription: Why my code is getting NZEC run time error?


code:
import itertools
lists = [[1,2,3,4],
         [1,2,3],
         [0,3,4]]
print [sum(x) for x in itertools.izip_longest(*lists, fillvalue=0)]
# prints [2, 7, 10, 4]

rawDescription: How to add uneven sub-lists in Python?

description: Add uneven sub-lists in Python


code:
while True:
    eventlet.sleep(dispatcher.getTimerResolution())
    try:
        dispatcher.handleTimerTick(time.time())
    except Exception:
        print 'Error while handling dispatcher tick!'
        raise

rawDescription: gevent and snmp in python

description: Gevent and snmp in python


code:
import Orange, orngStat

names = lbs     # labels of each technique
avranks = means # average ranking of each technique
number_of_datasets = 30 # number of datasets

# alpha = '0.1', '0.05' or '0.01'
cd = orngStat.compute_CD(avranks, number_of_datasets, alpha='0.1')
orngStat.graph_ranks("output.png", avranks, names, \
    cd=cd, width=6, textspace=1.5)
print cd
plt.show()

rawDescription: How can I generate CDdiagrams using Python/rpy2

description: Generate CDdiagrams using Python/rpy2


code:
for key, value in dictdates.items():
    print key
    for val in listdates:
       if val in value:
          print val, 'unavailable'
       else:
          print val, 'available'
    print "\n"

rawDescription: Compare list with dictionary and output true and false


code:
read_file = open ('C:\Users\Mahya\Desktop\\automate\Autosupports\\at1.txt','r')
content = read_file.readlines()
found = False
for line in content:
    if line.contains('===== BOOT TIME STATS ====='):
        if found:
            break # Exit the for loop when finding the string for the second time
        found = True
    if found:
        print line

rawDescription: file programming in python

description: File programming in python


code:
if search_request.starts_with("A="):
    seach_request = search_request[2:]  # strip off the selector "A="
    for row in reader:
        if search_request in row[0]:
            print row
        else:
            print("Sorry, could not be found")
else:
  print("Ooops, your selection (%s) is not supported right now" % search_result[:2])

rawDescription: Cannot obtain result from searching csv file in Python


code:
import re

s = "Size: 2247 MB People: 5,951 Hash: 9df49c5de014df3b5f202f51dc849b37cf82a3ad"
arr = re.split('Size: |People: |Hash: ', s)
arr = [x.strip() for x in arr][1:]
print arr

rawDescription: feedparser cannot get namespace values

description: Feedparser cannot get namespace values


code:
import fnmatch
import os

for file in os.listdir('.'):
    if not fnmatch.fnmatch(file, 'hello*.txt'):
        print file

rawDescription: listing files from a directory using glob python

description: Listing files from a directory using glob python


code:
def fail(sComment):
  print sComment
  sys.exit(1)
  return

sCmd = "p4 -G files //sw/...@=%s" % sChangeNr

stream = os.popen(sCmd, 'rb')
dictResult = []
try:
  while 1:
   dictResult.append(marshal.load(stream))
except EOFError:
  pass

stream.close()

failures = []
# check all files for tabs
for element in dictResult:
  depotFile =  element['depotFile']

sCmd = "p4 print -q %s@=%s" % (depotFile,sChangeNr)
content = os.popen(sCmd, 'rb').read()
if content.find('\t') != -1:
  failures.append(depotFile)

if len(failures) != 0:
  error = "Files contain tabulators (instead of spaces):\n"
  for i in failures:
    error = error + str(i) + "\n"
  fail(error)

rawDescription: Trigger to automatically remove EOL whitespace?


code:
class SpamMeta(type):

    def __new__(cls, name, bases, dct):
        SpamType = type.__new__(cls, name, bases, dct)
        SpamType.decorated = []
        return SpamType

class Spam(object):

    __metaclass__ = SpamMeta

    @classmethod
    def decorate(cls, funct):
        cls.decorated.append(funct)
        return funct

class Eggs(Spam):
    pass

@Eggs.decorate
def foo():
    print "spam and eggs"

print Eggs.decorated # [<function foo at 0x...>]
print Spam.decorated # []

rawDescription: Creating a decorator in a class with access to the (current) class itself


code:
suitName = ("hearts", "diamonds", "spades", "clubs")
rankName = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King")

def translate():
    cardNum = 0

    for x in rankName:
        rank = x

        for y in suitName:
            suit = y

            cards = ""
            cards += rank
            cards += " of "
            cards += suit
            i += 1

            print cardNum
            print "    "
            print cards

rawDescription: For loop in Python?

description: For loop in Python


code:
import urllib, urllib2, cookielib, os
import re # going to use this now!

username = 'somebody'
password = 'somepass'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : username, 'j_password' : password})
resp = opener.open('http://www.some.site/index.php', login_data)
content = resp.read()
print content

match = re.search(
    r"<a\s+href=\"(?P<file_link>http://www.some.site/excel_file.php?t=\d+)\">",
    content,
    re.IGNORECASE
)

assert match is not None, "Couldn't find the file link..."

file_link = match.group('file_link')
print "downloading {} with urllib2".format(file_link)
f = opener.open(file_link)
data = f.read()
with open("exceldoc.xls", "wb") as code:
    code.write(data)

rawDescription: can't download cookie protected file with python

description: Can't download cookie protected file with python


code:
for b in a:
    for fname in b:
        with open(fname, "r") as f:
            content = f.read()
        if EACH in content:
            print "There's a {0} in {1}".format(EACH, fname)

rawDescription: getting the values from function call and using them in function in python

description: Getting the values from function call and using them in function in python


code:
import re

value = "123456.33"

regex = re.search("^(\d\d\d)(\d\d\d)(?:\.(\d\d)){0,1}$", value)

if regex:
    print regex.group(1)
    print regex.group(2)
    if regex.group(3):
        print regex.group(3)
    else:
        print "3rd group not found"
else:
    print "value don't match regex"

rawDescription: How to get a capture group that doesnt always exist?

description: Get a capture group that doesnt always exist


code:
import xml.dom.minidom

file=('sample.xml')
xmldoc=xml.dom.minidom.parse(file)
computerNode = xmldoc.getElementsByTagName('computer')
for computerChild in computerNode:
    for parts in computerChild.childNodes:
       for partsChild in parts.childNodes:
          if partsChild.nodeType == xml.dom.Node.ELEMENT_NODE:
             if partsChild.tagName == 'text':
                print partsChild.childNodes[0].nodeValue

rawDescription: how to get direct child nodes not sub-child nodes with same tag name xml minidom python

description: Get direct child nodes not sub-child nodes with same tag name xml minidom python


code:
for ix, grouped_df in grouped:
    print grouped_df['status']

rawDescription: iterating dataframe coulmns after groupby

description: Iterating dataframe coulmns after groupby


code:
from subprocess import Popen, PIPE

prog = Popen("scriptA.exe", shell=True, stdin=PIPE, stdout=PIPE)

prog.stdin.write("This will go to script A\n")
print prog.stdout.read()

prog.wait() # Wait for scriptA to finish

rawDescription: Making two scripts communicate

description: Making two scripts communicate


code:
import a

def _new_print_message(message):
    print "NEW:", message

a.print_message = _new_print_message

import b
b.execute()

rawDescription: Override module method where from...import is used


code:
import sched, time
s = sched.scheduler(time.time, time.sleep)
def do_something(sc):
    print "Doing stuff..."
    # do your stuff
    sc.enter(60, 1, do_something, (sc,))

s.enter(60, 1, do_something, (s,))
s.run()

rawDescription: What is the best way to repeatedly execute a function every x seconds in Python?


code:
if foo: #{
    print "it's true"
#}
else: #{
    print "it's false!"
#}

rawDescription: Is it true that I can't use curly braces in Python?


code: print 'order:', x/25 + y/10 + z/25 ,' units'

rawDescription: In Python, how do you display the value of an equation inside of a print function?


code:
class Foo(object):
    def __del__(self):
        print "Foo died"

class Bar(object):
    def __init__(self):
        self.foo = Foo()
        self.baz = Baz(self)

class Baz(object):
    def __init__(self, bar):
        self.bar = bar

b = Bar()

del b

rawDescription: Why do circular references prevent the destructors of objects that aren't on the loop?


code:
from twython import Twython

# define application keys here
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""

twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret)
status = twitter.show_status(id='tweet_id')
print status['user']['id_str']

rawDescription: Twitter API Get User ID from Status ID

description: Twitter API Get User ID from Status ID


code:
for m in re.findall(r'<strong>([^<]+)</strong>', raw_string):
    print m

rawDescription: Loop and display substring in python

description: Loop and display substring in python


code:
import inspect

def bar():
    current_frame = inspect.currentframe()
    previous_frame = current_frame.f_back
    print previous_frame.f_code.co_name

def foo():
    bar()

foo()

rawDescription: Python: Get name of function which called this function


code:
import multiprocessing as multi
from multiprocessing import Manager

manager = Manager()

glob_data= manager.list([])

def func(a):
    glob_data.append(a)

map(func,range(10))
print glob_data  #[0,1,2,3,4 ... , 9]  Good.

p=multi.Pool(processes=8)
p.map(func,range(80))

print glob_data  # Super Good.

rawDescription: python parallel map (multiprocessing.Pool.map) with global data

description: Python parallel map (multiprocessing.Pool.map) with global data


code:
import itertools

def number_generator():
    for i in range(0, 3):
        print "yield", i
        yield i
    print "stopping"

function = lambda x: x > 0

numbers = number_generator()
print "itertools.ifilter:"
for n in itertools.ifilter(function, numbers):
    print n

print "\nfilter:"
numbers = number_generator()
for n in filter(function, numbers):
    print n

rawDescription: itertools.ifilter Vs. filter Vs. list comprehensions

description: Itertools.ifilter Vs. filter Vs. list comprehensions


code:
def move(src, dst):
    """Recursively move a file or directory to another location. This is
    similar to the Unix "mv" command.

    If the destination is a directory or a symlink to a directory, the source
    is moved inside the directory. The destination path must not already
    exist.

    If the destination already exists but is not a directory, it may be
    overwritten depending on os.rename() semantics.

    If the destination is on our current filesystem, then rename() is used.
    Otherwise, src is copied to the destination and then removed.
    A lot more could be done here...  A look at a mv.c shows a lot of
    the issues this implementation glosses over.

    """
    real_dst = dst
    if os.path.isdir(dst):
        if _samefile(src, dst):
            # We might be on a case insensitive filesystem,
            # perform the rename anyway.
            os.rename(src, dst)
            return

        real_dst = os.path.join(dst, _basename(src))
        if os.path.exists(real_dst):
            raise Error, "Destination path '%s' already exists" % real_dst
    try:
        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error, "Cannot move a directory '%s' into itself '%s'." % (src, dst)
            copytree(src, real_dst, symlinks=True)
            rmtree(src)
        else:
            copy2(src, real_dst)
            os.unlink(src)

rawDescription: Unbound method error in Python

description: Unbound method error in Python


code:
def split_number(n, sizes):
    sizes = sorted(sizes, reverse=True)
    ret = {}

    for size in sizes:
        if size <= n:
            ret[size], n = divmod(n, size)

    return ret

print split_number(45, [1, 2, 3, 4, 6, 8, 16])

rawDescription: Splitting an integer into smaller integers of defined sizes in Python


code:
import xml.etree.ElementTree as ET
tree = ET.parse("1.xml")
for rule in tree.iter("rule"):
    for child in rule:
        if child.tag == 'word':
            print "%s" % child.text
        elif child.tag == 'marker':
            print "("
            for subchild in child:
                if subchild.tag == 'word':
                    print "%s" % subchild.text
            print ")"

rawDescription: Replace tags into symbol by elementree

description: Replace tags into symbol by elementree


code:
data = {'foo': '@@x@@'}
print json.dumps(data)
print json.dumps(data).replace('"@@x@@"', 'some_variable_name')

rawDescription: Custom JSON encoder in Python 2.7 to insert plain JavaScript code


code:
vals = column_x.apply(lambda x: float(x.split('=')[1]))
print sum(vals <= 0.05) #number of rows with AF1000g=0.05 and less
print sum(vals >= 0.06) #number of rows with AF1000g=0.06 and greater

rawDescription: Calculate Numeric Value inside a String within a Pandas Column


code:
import re
string = '1-321231'
pattern = '\d[-]\d*'
match = re.search(pattern, string)
if match:
    print 'we\'ve got a match!'

rawDescription: How to deal with Regular expression in python?

description: Deal with Regular expression in python


code:
import hashlib
inp = open("inp.txt" , "r")
outputhash  = open("outputhashes.txt", "w")
for line in inp:            # Change this
    eachpwd = line.strip()  # Change this

    # Add this to understand the problem:
    print repr(line)

    sha_1 = hashlib.sha1()
    sha_1.update(eachpwd)
    outputhash.write(sha_1.hexdigest())
    outputhash.write("\n")

rawDescription: Compute SHA1 of Strings in python

description: Compute SHA1 of Strings in python


code:
import psutil

iocnt1 = psutil.disk_io_counters(perdisk=True)['sda1']
# do  something IO intensive
iocnt2 = psutil.disk_io_counters(perdisk=True)['sda1']

print 'Blocks written {0}'.format(iocnt2.write_count - iocnt1.write_count)
print 'Blocks read {0}'.format(iocnt2.read_count - iocnt1.read_count)

rawDescription: Using python to measure in-situ read/write speed for files


code:
def translate(x):
    if x == ' ': return '-'
    if 'a' <= x <= 'z': return x
    if 'A' <= x <= 'Z': return x
    if '0' <= x <= '9': return x

def mk_translator():
    translations = ''.join(translate(chr(c)) or chr(c) for c in xrange(256))
    deletions = ''.join(chr(c) for c in xrange(256) if translate(chr(c)) is None)
    return translations, deletions

def urlize(x, translator=mk_translator()):
    return x.translate(*translator)

print urlize('User Name')

rawDescription: What's the fastest way to convert a string of text into a url-safe variable?


code:
from instagram.client import InstagramAPI

access_token = "YOUR_ACCESS_TOKEN"
api = InstagramAPI(access_token=access_token)
recent_media, next_ = api.user_recent_media(user_id="userid", count=10)
for media in recent_media:
    if hasattr(media, 'tags'):
        print media.tags  # attempt to get tags associated with each media
    else:
        # you can check if there is tags attribute through this line
        print 'all the attributes of media are: ', repr(dir(media))

rawDescription: Extract tags from media received from Instagram API


code:
data_lists=[ {"Average execution" : [1, 2, 3, 2, 3]},
                             {"Top execution"     : [3, 4, 5, 7, 8, 11, 6]},
                             {"Current execution" : [1, 2, 1, 2, 1, 5]}       ]
c=Chart(data_lists,
        ("Large
rooster", "Rsync rooster", "Pool
Chicken", "Disease"),
        width=700, height=300)
print c.html.strip()

rawDescription: Python doctest: result with multiple lines


code:
import random

replay = True

while(replay):
    die1 = random.randrange(6) + 1
    die2 = random.randrange(6) + 1
    total = die1 + die2

    raw_input("Ready to roll dice 1? Press enter if you are.")
    print "You rolled a", die1, "with dice 1."

    raw_input("Ready to roll dice 2? Press enter if you are.")
    print "And a", die2, "with dice 2."

    print "Giving you a total of", total
    replay_option = raw_input("Press 1 to play again or press any key to exit")

    if replay_option == '1':
        replay = True
    else:
        replay = False

rawDescription: How do I loop a program based on user input?

description: Loop a program based on user input


rawDescription: Find most distant points in contour curve


code:
import thread, threading

def longjob():
    try:
        # do your job
        while True:
            print '*',
    except KeyboardInterrupt:
        # do your cleanup
        print 'ok, giving up'

def terminate():
    print 'sorry, pal'
    thread.interrupt_main()

time_limit = 5  # terminate in 5 seconds
threading.Timer(time_limit, terminate).start()
longjob()

rawDescription: Python: Restrict the code to be run for an hour


code:
prices = [u'\u20ac225,000', u'\u20ac1,000,000', u'\u20ac245,000']
print prices
for price in prices:
    print price

rawDescription: XPath & Python - Retrieving Currency

description: XPath & Python - Retrieving Currency


code:
from multiprocessing import Pool
from multiprocessing.managers import BaseManager, DictProxy
from collections import defaultdict

class MyManager(BaseManager):
    pass

MyManager.register('defaultdict', defaultdict, DictProxy)

def test(k, multi_dict):
    multi_dict[k] += 1

if __name__ == '__main__':
    pool = Pool(processes=4)
    mgr = MyManager()
    mgr.start()
    multi_d = mgr.defaultdict(int)
    for k in 'mississippi':
        pool.apply_async(test, (k, multi_d))
    pool.close()
    pool.join()
    print multi_d.items()

rawDescription: Using defaultdict with multiprocessing?

description: Using defaultdict with multiprocessing


code:
from mechanize import Browser
b = Browser()
b.open("http://forum.ubuntu-it.org/index.php")
b.select_form(nr=0)
b["user"] = "johnconnor"
b["passwrd"] = "hellohello"
b.submit()

response = b.response().read()
if "Salve <b>johnconnor</b>" in response:
    print "Logged in!"

rawDescription: How to download a webpage that require username and password?

description: Download a webpage that require username and password


code:
forecast = data.find('table',{'class':"table mrgn-bttm-md mrgn-tp-md     textforecast hidden-xs"}).find_all("tr")
for tr in forecast[1:]:
    print " ".join(tr.text.split())

rawDescription: Parsing Environment Canada Website

description: Parsing Environment Canada Website


code:
x=s.split(',')

if len(x)>1:
    x=x[1:] # since you are only concerned with what comes after a ','
    print x[0].strip() # remove the strip(), depends on the usage
else:
    print ''

rawDescription: How do I split or grab a string in python after a comma in python

description: Split or grab a string in python after a comma in python


code:
import cgi

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "
This is my first CGI script
"

form = cgi.FieldStorage()
if "name" not in form or "addr" not in form:
    print "
Error
"
    print "Please fill in the name and addr fields."
    return
print "
name:", form["name"].value
print "
addr:", form["addr"].value

rawDescription: Sending data from javascript to python

description: Sending data from javascript to python


code:
import numpy, scipy
from scipy import interpolate

kernelIn = numpy.array([
    [0,-2,0],
    [-2,11,-2],
    [0,-2,0]])

inKSize = len(kernelIn)
outKSize = 5

kernelOut = numpy.zeros((outKSize),numpy.uint8)

x = numpy.array([0,1,2])
y = numpy.array([0,1,2])

z = kernelIn

xx = numpy.linspace(x.min(),x.max(),outKSize)
yy = numpy.linspace(y.min(),y.max(),outKSize)

newKernel = interpolate.RectBivariateSpline(x,y,z, kx=2,ky=2)

kernelOut = newKernel(xx,yy)

print kernelOut
##[[  0.      -1.5     -2.      -1.5      0.    ]
## [ -1.5      5.4375   7.75     5.4375  -1.5   ]
## [ -2.       7.75    11.       7.75    -2.    ]
## [ -1.5      5.4375   7.75     5.4375  -1.5   ]
## [  0.      -1.5     -2.      -1.5      0.    ]]

rawDescription: Scipy interpolation how to resize/resample 3x3 matrix to 5x5?


code:
import random
count = 0
for x in range(20):
     a=random.randint(1000,2000)
     # b=(a>1500)// This is expected to give a boolean
     # print b
     if (a > 1500):
        count = count + 1

print count

rawDescription: Python Random function

description: Python Random function


code:
keys = []
def lzw(text):
      tokens = text.split()
      new_keys = dict.fromkeys(tokens).keys()
      keys.extend([key for key in new_keys if  key not in keys])
      encoded = ["%s"%keys.index(tok) for tok in tokens]
      for i,key in enumerate(keys):
           try:
              encoded[encoded.index(str(i))] = key
           except:
               pass
      return " ".join(encoded)

print lzw("abcd abcd fasf asda abcd fasf")
#outputs: abcd 0 fasf asda 0 2

rawDescription: Pattern finding LZW python

description: Pattern finding LZW python


code:
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class ImageLabel(QLabel):
    def __init__(self, image, parent=None):
        super(ImageLabel, self).__init__(parent)
        self.setPixmap(image)

    def mousePressEvent(self, event):
        print 'I was pressed'

class AppForm(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.create_main_frame()

    def create_main_frame(self):
        name_label = QLabel("Here's a clickable image:")
        img_label = ImageLabel(QPixmap('image.png'))

        vbox = QVBoxLayout()
        vbox.addWidget(name_label)
        vbox.addWidget(img_label)

        main_frame = QWidget()
        main_frame.setLayout(vbox)
        self.setCentralWidget(main_frame)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    form = AppForm()
    form.show()
    app.exec_()

rawDescription: resizing of button icons in pyqt4

description: Resizing of button icons in pyqt4


code:
import sys
x = sys.maxint             # set a variable to your systems maximum integer
print type(x)
x += 1                     # if you increase it, it gets converted into long
print type(x)

rawDescription: Why does this function return a long rather than an int?


code:
from Tkinter import *

def callback(var, index):
    print var, index

root = Tk()

for i in range(10):
    var = StringVar()
    uniqueID = i
    var.trace('w', lambda name,index,mode, var=var, uniqueID=uniqueID: callback(var, uniqueID))
    Entry(root, textvariable=var).pack()

mainloop()

rawDescription: StringVar Callback with Unique Identifier?


code:
import pylab as plb
import matplotlib.pyplot as plt
import matplotlib.axes as ax
import scipy as sp
from scipy.optimize import curve_fit
from matplotlib import rc
import numpy as np
rc('font', **{'family':'sans-serif', 'sans-serif':['Helvetica']})
rc('text', usetex=True)

# Fake data
x = np.arange(0, 70., 2.)
yl = 300 + 63*np.exp(-x/35.)

def func(x, a, b, c):
    return a*np.exp(-b*x) + c

popt, pcov = curve_fit(func, x, yl, p0=(40, 0.012, 250), maxfev=20000)
a, b, c = popt
print 'a=', a, 'b=', b, 'c=', c
print 'func=', func(x, a, b, c)

popt2, pcov2 = curve_fit(func, x, yl, p0=None, maxfev=20000)
a2, b2, c2 = popt2
print 'a2=', a2, 'b2=', b2, 'c2=', c2
print 'func=', func(x, a2, b2, c2)

xf = np.linspace(0, 70, 100)
yf = a*np.exp(-b*x) + c

plt.clf()
plt.plot(x, yf, 'r-', label="Fitted Curve")
plt.plot(x, func(x, *popt))
plt.plot(x, func(x, *popt2), 'b-', label='Fit w/o guess')

plt.plot(x, yl, 'go', label='Lacquered')

plt.legend()
plt.ylabel("Temperature (K)")
plt.xlabel("Time (min)")
plt.show()

rawDescription: Python exponential decay curve_fit gives me a linear fit


code:
class Level():

    def __init__(self, lesson):

        options = (1, "j"), (u"\u0430", "F"), (u"\u0435", "T")

        self.lesson = lesson
        self.word, self.letter = options[lesson-1]

    def print_text(self):

        text = u"This lesson's letter is {0}. Type the letter {1}."
        print text.format(self.word, self.letter)

l = Level(2)
l.print_text()

rawDescription: Pygame Class and __init__

description: Pygame Class and __init__


code:
num = int(raw_input("input number: "))
print "\033[A                             \033[A"    # ansi escape arrow up then overwrite the line

rawDescription: Delete the last input row in Python

description: Delete the last input row in Python


code:
import sys
import xml.dom.minidom as dom
import string
from xml.dom import minidom
from xml.dom.minidom import parse
import os
Read_Data = minidom.parse("germany50.xml")
nodelist = Read_Data.getElementsByTagName("node")

class Node:
    def __init__(self,uid,x,y):
        self.uid=uid
        self.x=x
        self.y=y
        self.linklist=[]
        self.demandlist=[]

class Link:
    def __init__(self,uid,source,target,capacity):
        self.uid=uid
        self.source=source
        self.target=target
        self.capacity=capacity

class Demand:
    def __init__(self,uid,source,destination,demandValue):
        self.uid=uid
        self.source=source
        self.destination=destination
        self.demandValue=demandValue

nodemap={}

for node in nodelist :
    if node.hasAttribute("id"):
        Nodeid = node.getAttribute("id")
    xCoordinates = node.getElementsByTagName('x') [0]
    yCoordinates = node.getElementsByTagName('y') [0]
    nodemap[Nodeid]=Node(Nodeid,xCoordinates,yCoordinates)
    print "%s : %s %s" %(node.getAttribute("id"), xCoordinates.childNodes[0].data, yCoordinates.childNodes[0].data)

linklist = Read_Data.getElementsByTagName("link")
for link in linklist :
    if link.hasAttribute("id"):
        Linkid = link.getAttribute("id")
    Source = link.getElementsByTagName('source') [0]
    Destination = link.getElementsByTagName('target') [0]
    Capacity = link.getElementsByTagName('capacity') [0]
    linkobj= Link(Linkid,Source,Destination,Capacity)
    if nodemap.has_key(Source):
        nodemap[Source].linklist.append(linkobj)
    if nodemap.has_key(Destination):
        nodemap[Destination].linklist.append(linkobj)

    print "%s - %s to %s: %s" %(link.getAttribute("id"), Source.childNodes[0].data, Destination.childNodes[0].data, Capacity.childNodes[0].data)
demandlist = Read_Data.getElementsByTagName("demand")
for demand in demandlist :
    if demand.hasAttribute("id"):
        Demandid = demand.getAttribute("id")
    Source = demand.getElementsByTagName('source') [0]
    Destination = demand.getElementsByTagName('target') [0]
    Demandval = demand.getElementsByTagName('demandValue') [0]
    demandobj=Demand(Demandid,Source,Destination,Demandval)
    if nodemap.has_key(Source):
        nodemap[Source].demandlist.append(demandonj)
    if nodemap.has_key(Destination):
        nodemap[Destination].demandlist.append(demandobj)

    print "%s needs %s" %(demand.getAttribute("id"), Demandval.childNodes[0].data)

print nodemap

rawDescription: Creating a network in Python using XML data


code:
from xml.etree import ElementTree

tree = ElementTree.parse('filename.xml')
for elem in tree.findall('lex'):
    print elem.attrib['marker']

rawDescription: extract values from xml

description: Extract values from xml


code:
import string
t = string.Template("Hello my name is $name")
print t.substitute(name='Guido')
t = "Hello my name is %(name)s"
print t % dict(name='Tim')
t = "Hello my name is {name}"
print t.format(name='Barry')

rawDescription: Replacing a substring of a string with Python


rawDescription: Sort list of lists of strings in Python

description: Sort list of lists of strings in Python


rawDescription: Python - xpath syntax for a form (lxml.html)


code:
finalList = []
firstParts = []
for line in List:
    item = line[0].split("-")
    print item[0]
    if item[0] not in firstParts:
            firstParts.append(item[0])
            finalList.append(line)

rawDescription: Find if a string is not part of a list of list in Python


code:
cs = tricontourf(tri,height)
for collection in cs.collections:
    for path in collection.get_paths():
        print path.to_polygons()

rawDescription: get numpy array of matplotlib tricontourf

description: Get numpy array of matplotlib tricontourf


code:
class A(object):
    def __init__(self, msg):
        self.msg = msg

    def __call__(self):
        print self.msg

    def __getattr__(self, name):
        return self

a = A('Hello World')
a()
a.b()
a.b.c()

rawDescription: Class with all possible methods defined/catched


code:
for letter in "hello":
    print letter

rawDescription: (Python) - Need some help involving the CSV writer


rawDescription: How to pad with zeros a tensor along some axis (Python)

description: Pad with zeros a tensor along some axis (Python)


code:
def click(*args):
    print "click"
click
click()

rawDescription: Function with arguments autostarts

description: Function with arguments autostarts


rawDescription: Python: replace only one occurrence in a string


code:
from threading import Thread

class GetTitleThread(Thread):

    def __init__(self, fileName):
        self.sTitle = None
        self.fileName = fileName
        super(GetTitleThread, self).__init__()

    def run(self):
        print "getTitle"
        audio = MP3(self.fileName)

        try:
            self.sTitle = str(audio["TIT2"])
        except KeyError:
            self.sTitle = os.path.basename(self.fileName)

        self.sTitle = replace_all(self.sTitle) #remove special chars

if __name__ == '__main__':
    t = GetTitleThread('SomeSong.mp3')
    t.start()
    t.join()
    print t.sTitle

rawDescription: python Returning data from a threaded def

description: Python Returning data from a threaded def


code:
# step 3: now POST the details of note

import urllib

token = response.read()
add_note_url = "http://www.google.co.uk/reader/api/0/item/edit"
data = {'snippet' : 'This is the note', 'T' : token}
encoded_data = urllib.urlencode(data)
req = urllib2.Request(add_note_url, encoded_data)
req.add_header('Authorization', 'GoogleLogin auth=%s' % credentials['Auth'])
response = urllib2.urlopen(req)

# this part is optional
if response.code == 200:
    print 'Gadzooks!'
else:
    print 'Curses and damnation'

rawDescription: How can I can I programatically post a note to Google Reader?

description: Can I programatically post a note to Google Reader


code:
name = "Bob" ## or maybe you could ignore that
while True:
    try:
        print "Whats your name?"
        name = raw_input("Name:")

    except KeyboardInterrupt:
        print name
        raw_input("PRESS ENTER TO RESUME")

rawDescription: Resume a python program after exception?

description: Resume a python program after exception


code:
#put this in your init
self.time = 0

#put this in your condition
if keymap[pyglet.window.key.RIGHT]:
    self.mario.time = time.time()
    self.mario.jump()

def jump(self):
    print time.time()-self.time
    self.y -= -10 +20**(time.time()-self.time+.2)

rawDescription: How to make a sprite jump in Pyglet?

description: Make a sprite jump in Pyglet


code:
def decorate(f):
    def wrap(*args, **kwargs):
        # Print a greeting every time decorated function is called
        print "Hi from wrap!"
        return f(*args, **kwargs)
    return wrap

class DecorateMeta(type):
    def __new__(cls, name, bases, dct):
        # Find which decorator to use through the decorator attribute
        try:
            decorator = dct["decorator"]
        except KeyError:
            raise TypeError("Must supply a decorator")

        # Loop over all attributes
        for key, val in dct.items():
            # If attribute is callable and is not the decorator being used
            if hasattr(val, "__call__") and val is not decorator:
                dct[key] = decorator(val)

        return type.__new__(cls, name, bases, dct)

class Test:
    __metaclass__ = DecorateMeta
    decorator = decorate

    def seasonal_greeting(self):
        print "Happy new year!"

Test().seasonal_greeting()

# Hi from wrap!
# Happy new year!

rawDescription: Memoizing all methods by default

description: Memoizing all methods by default


code:
from itertools import permutations, product

def main():
    teamA = [40, 50, 70]
    teamB = [75, 25, 30]

    # Compute two averages by processing every possible match:
    #   pa   Probability that Team A wins a match.
    #   ng   Expected N of games in a match.
    tot_pa, tot_ng, n = (0, 0, 0)
    for As, Bs in product(permutations(teamA), permutations(teamB)):
        pa, ng = prob_a_wins(As, Bs)
        tot_pa += pa
        tot_ng += ng
        n      += 1

    print tot_pa / n  # 0.61233
    print tot_ng / n  # 2.50580

def prob_a_wins(As, Bs):
    # Probabilities that Team A wins game 1, 2, 3, and the match.
    g1, g2, g3 = [ a / float(a + b) for a, b in zip(As, Bs) ]
    pa = (
        g1       * g2            +  # win g1 and g2
        g1       * (1 - g2) * g3 +  # win g1 and g3
        (1 - g1) * g2       * g3    # win g2 and g3
    )

    # Probabability of a two-game match, and expected N of games.
    two = (
        g1       * g2 +        # win  g1 and g2
        (1 - g1) * (1 - g2)    # lose g1 and g2
    )
    ng  = two * 2  +  (1 - two) * 3

    return (pa, ng)

main()

rawDescription: Working out probability of a team win

description: Working out probability of a team win


code:
special_divs = soup.find_all('div',{'class':'Special_Div_Name'})
for text in special_divs:
    download = text.find_all('a', href = re.compile('\.mp3$'))
    for text in download:
        hrefText = (text['href'])
        print hrefText

rawDescription: How to find all anchor tags inside a div using Beautifulsoup in Python

description: Find all anchor tags inside a div using Beautifulsoup in Python


code:
from multiprocessing import Process, Queue

def f1(q):
  while 1:
    q.put('Hello')

def f2(q):
  while 1:
    q.put('Goodbye')

def main():
  q = Queue()
  p1 = Process(target=f1, args=(q,))
  p1.start()

  p2 = Process(target=f2, args=(q,))
  p2.start()

  while True:
     try:
        print q.get()
     except:
        break

if __name__ == '__main__':
  main()

rawDescription: Python; unable to run multiple processes


code:
#!/usr/bin/python2

import sys

def colorize(stdoutColor, stderrColor):
  defaultColor = '\033[0;0m'

  def applyColorize(f):
    class colorWrapper(object):
      def __init__(self, wrapee, color):
        self.wrapee = wrapee
        self.color = color
      def __getattr__(self, attr):
        if attr == 'write' and self.wrapee.isatty():
          return lambda x: self.wrapee.write(self.color + x + defaultColor)
        else:
          return getattr(self.wrapee, attr)

    def wrapper(*args, **kwds):
      oldStdout = sys.stdout
      oldStderr = sys.stderr
      sys.stdout = colorWrapper(oldStdout, stdoutColor)
      sys.stderr = colorWrapper(oldStderr, stderrColor)
      try:
        f(*args, **kwds)
      finally:
        sys.stdout = oldStdout
        sys.stderr = oldStderr

    return wrapper

  return applyColorize

greenColor = '\033[01;32m'
redColor = '\033[01;31m'

def foo():
  print "I'm ordinary and boring!"
  print >> sys.stderr, 'Writing to stderr!'

@colorize(greenColor, redColor)
def colorFoo():
  print "I'm colorful and exciting!"
  print >> sys.stderr, 'Writing to stderr!'

if __name__ == '__main__':
  foo()
  colorFoo()
  foo()

rawDescription: (python) colour printing with decorator in a function


code:
import weakref
from bunch import Bunch

class MyObject(object):

    indexes = Bunch()  # Could just use dict()

    def __init__(self, **kwargs):
        super(MyObject, self).__init__()
        for k, v in kwargs.items():
            setattr(self, k, v)

    def __setattr__(self, name, value):
        try:
            index = MyObject.indexes[name]
        except KeyError:
            index = weakref.WeakValueDictionary()
            MyObject.indexes[name] = index
        try:
            old_val = getattr(self, name)
            del index[old_val]
        except (KeyError, AttributeError):
            pass
        object.__setattr__(self, name, value)
        index[value] = self

obj1 = MyObject(status=1, timestamp=123123)
obj2 = MyObject(status=2, timestamp=2343)

print MyObject.indexes.status[1]
print obj1.indexes.timestamp[2343]
obj1.status = 5
print obj2.indexes['status'][5]

rawDescription: pythonic way to index list of objects

description: Pythonic way to index list of objects


code:
import oauth2 as oauth
import oauth2.clients.imap as imaplib

# Set up your Consumer and Token as per usual. Just like any other
# three-legged OAuth request.
consumer = oauth.Consumer('your_consumer_key', 'your_consumer_secret')
token = oauth.Token('your_users_3_legged_token',
    'your_users_3_legged_token_secret')

# Setup the URL according to Google's XOAUTH implementation. Be sure
# to replace the email here with the appropriate email address that
# you wish to access.
url = "https://mail.google.com/mail/b/your_users_email@gmail.com/imap/"

conn = imaplib.IMAP4_SSL('imap.googlemail.com')
conn.debug = 4

# This is the only thing in the API for impaplib.IMAP4_SSL that has
# changed. You now authenticate with the URL, consumer, and token.
conn.authenticate(url, consumer, token)

# Once authenticated everything from the impalib.IMAP4_SSL class will
# work as per usual without any modification to your code.
conn.select('INBOX')
print conn.list()

rawDescription: use imaplib and oauth for connection with Gmail

description: Use imaplib and oauth for connection with Gmail


code:
from collections import Counter
import random

# the number of question ids I need returned to
# assign to the exam
needed = 3

# the "pool" of possible question ids the user has access to
possible = [1,2,3,4,5]

# examples of lists of question ids I might see that represent
# questions a user has already answered
answered1 = []
answered2 = [1,3]
answered3 = [5,4,3,2]
answered4 = [5,4,3,2,1,1,2]
answered5 = [5,4,3,2,1,1,2,3,4,5,1]
answered6 = [5,4,3,2,1]

def getdiff(answered):
    diff = set(possible) - set(answered)
    still_needed = needed - len(diff)
    if still_needed > 0:
        not_already_selected = list(set(possible) - diff)
        random.shuffle(not_already_selected)
        diff = list(diff) + not_already_selected[0:still_needed]
        random.shuffle(diff)
        return diff
    diff = list(diff)
    random.shuffle(diff)
    if still_needed == 0:
        return diff
    return diff[0:needed]

def workit(answered):
    """ based on frequency, reduce the list down to only
        those questions we want to consider "answered"
    """
    have_count = 0
    if len(possible) > len(answered):
        return getdiff(answered)
    counted = Counter(answered)
    max_count = max(counted.values())
    # the key here is to think of "answered" questions as
    # only those that have been seen with max frequency
    new_answered = []
    for value, count in counted.iteritems():
        if count == max_count:
            new_answered.append(value)
    return getdiff(new_answered)

print 1, workit(answered1)
print 2, workit(answered2)
print 3, workit(answered3)
print 4, workit(answered4)
print 5, workit(answered5)
print 6, workit(answered6)

"""
>>>
1 [2, 4, 3]
2 [2, 5, 4]
3 [5, 2, 1]
4 [5, 3, 4]
5 [2, 4, 3]
6 [2, 3, 5]
>>> ================================ RESTART ================================
>>>
1 [3, 1, 4]
2 [5, 2, 4]
3 [2, 4, 1]
4 [5, 4, 3]
5 [4, 5, 3]
6 [1, 5, 3]
>>> ================================ RESTART ================================
>>>
1 [1, 2, 3]
2 [4, 2, 5]
3 [4, 1, 5]
4 [5, 4, 3]
5 [2, 5, 4]
6 [2, 1, 4]
"""

rawDescription: Efficient algorithm for determining values not as frequent in a list


code:
clr.AddReference ("Microsoft.AnalysisServices.AdomdClient.dll")
clr.AddReference ("System.Data")

from Microsoft.AnalysisServices.AdomdClient import AdomdConnection , AdomdDataAdapter
from System.Data import DataSet

conn = AdomdConnection("Data Source=0.0.0.0;Catalog=MyCatalog;")
conn.Open()
cmd = conn.CreateCommand()
cmd.CommandText = "your mdx query" # in your case 'select from finance'
adp = AdomdDataAdapter(cmd)
datasetParam =  DataSet()
adp.Fill(datasetParam)
conn.Close();

# datasetParam hold your result as collection a\of tables
# each tables has rows
# and each row has columns
print datasetParam.Tables[0].Rows[0][0]

rawDescription: Execute query on SQL Server Analysis Services with IronPython


code:
import requests
from bs4 import BeautifulSoup

def get_viewstate():
    url = "http://tickets.cathay.com.sg/index.aspx"
    req = requests.get(url)
    data = req.text

    bs = BeautifulSoup(data)
    return bs.find("input", {"id": "__VIEWSTATE"}).attrs['value']

url = "http://tickets.cathay.com.sg/index.aspx"
data = {"__VIEWSTATE": get_viewstate()}
req = requests.post(url, data)

bs = BeautifulSoup(req.text)
print bs.findAll("td", {"class": "movieTitlePlatinum"}) #Just an example, you could also do bs.findAll("img") etc.

rawDescription: python-how to crawl past __VIEWSTATE

description: Python-how to crawl past __VIEWSTATE


code:
def is_prime(x):
    if x < 2:
        return False
    if x == 2:
        return True
    if x == 3:
        return True
    else:
        for n in range(2,x-1):
            print "Current value is %d." %n
            if x % n == 0:
                return False
        return True

rawDescription: Codecademy Python "is_prime" exercise in "Practice Makes Perfect"-Is it really iterating?


code:
b = {11,14,15}  #notice the {} braces or use `set([11,14,15])`
print sum(b.issubset(x) for x in a)

rawDescription: count number of sublist that contain 3 common elements in Python 3

description: Count number of sublist that contain 3 common elements in Python 3


code:
basedir = os.path.dirname(__file__)
for m in sys.modules:
    if m in locals() and not m.startswith('_'): # Only export regular names
        mod = locals()[m]
        if '__file__' in mod.__dict__  and mod.__file__.startswith(basedir):
            print m

rawDescription: Recursively populating __all__ in __init__.py


code:
from azure.storage import TableService

table_service = TableService(account_name='name', account_key='key')
alltables = table_service.query_tables()
for table in alltables:
    print table.name

rawDescription: Fetching Windows Azure Storage tables names through python API


code:
f = open('tmpfile','r')
x = f.read()
y = tuple(i for i in x.split(',') if 'ABC' in i)
print y

rawDescription: Finding a list or tuple within a .txt file containing a specified item


code:
print mytext
uri = re.findall(r'uri\"\:\"[\'"]?([^\'" >]+)', mytext) #gets the uri
smallpicurl = re.findall(r'smallPictureUrl\"\:\"[\'"]?([^\'" >]+)', mytext) # gets the smallPictureUrl
''.join(uri).rstrip()
''.join(smallpicurl).rstrip()

rawDescription: take urls from lines of a file in python

description: Take urls from lines of a file in python


code:
class SumCorr(QtGui.QMainWindow, Ui_SumCorr_ui):

    def __init__(self):
        SumCorr_ui.__init__(self)
        self.setupUi(self)

        self.save_energies_button.clicked.connect(self.saveEnergyScheme)

    def saveEnergyScheme(self):
        print 'energyList'

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    mySumCorr = SumCorr()
    mySumCorr.show()
    sys.exit(app.exec_())

rawDescription: PyQt4 app button not working

description: PyQt4 app button not working


code: print "Temp " + subprocess.check_output(command, shell=True)

rawDescription: Print subprocess.call result

description: Print subprocess.call result


code:
"""table_per_association.py

The HasAddresses mixin will provide a new "address_association" table for
each parent class.   The "address" table will be shared
for all parents.

This configuration has the advantage that all Address
rows are in one table, so that the definition of "Address"
can be maintained in one place.   The association table
contains the foreign key to Address so that Address
has no dependency on the system.

"""
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import create_engine, Integer, Column, \
                    String, ForeignKey, Table
from sqlalchemy.orm import Session, relationship
import itertools

class Base(object):
    """Base class which provides automated table name
    and surrogate primary key column.

    """
    @declared_attr
    def __tablename__(cls):
        return cls.__name__.lower()
    id = Column(Integer, primary_key=True)
Base = declarative_base(cls=Base)

class Address(Base):
    """The Address class.

    This represents all address records in a
    single table.

    """
    street = Column(String)
    city = Column(String)
    zip = Column(String)

    @property
    def all_owners(self):
        return list(
            itertools.chain(
            *[
                getattr(self, attr)
                for attr in [a for a in dir(self) if a.endswith("_parents")]
            ]
        ))

    def __repr__(self):
        return "%s(street=%r, city=%r, zip=%r)" % \
            (self.__class__.__name__, self.street,
            self.city, self.zip)

class HasAddresses(object):
    """HasAddresses mixin, creates a new address_association
    table for each parent.

    """
    @declared_attr
    def addresses(cls):
        address_association = Table(
            "%s_addresses" % cls.__tablename__,
            cls.metadata,
            Column("address_id", ForeignKey("address.id"),
                                primary_key=True),
            Column("%s_id" % cls.__tablename__,
                                ForeignKey("%s.id" % cls.__tablename__),
                                primary_key=True),
        )
        return relationship(Address, secondary=address_association,
                    backref="%s_parents" % cls.__name__.lower())

class Customer(HasAddresses, Base):
    name = Column(String)

class Supplier(HasAddresses, Base):
    company_name = Column(String)

engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)

session = Session(engine)

a1 = Address(
            street='123 anywhere street',
            city="New York",
            zip="10110")
a2 = Address(
            street='40 main street',
            city="San Francisco",
            zip="95732")

session.add_all([
    Customer(
        name='customer 1',
        addresses=[a1, a2]
    ),
    Supplier(
        company_name="Ace Hammers",
        addresses=[a1]
    ),
])

session.commit()

for customer in session.query(Customer):
    for address in customer.addresses:
        print address.all_owners

rawDescription: Generic associations in SQLAlchemy with multiple parents


code:
import csv

with open('real.csv', 'rb') as csvfile:
    for row in csv.reader(csvfile, delimiter=' '):
        reals = row

with open('binary.csv', 'rb') as csvfile:
    pwreader = csv.reader(csvfile, delimiter=' ')

    for row in pwreader:
        result = []
        for i,b in enumerate(row):
            if b == '1' :
                result.append(reals[i])

        print " ".join(result)

rawDescription: Python: How to do looping multiplication and discard those binary value 0?


code:
print "\x1b[H\x1b[2J",
print "hello clear world"

rawDescription: How is possible to create a python shell script like top unix command?


code:
opt = child.get('desc')

extent = child.get('extent')

if opt == 'es':
    opt2 = "ESP:"
elif opt == "la":
    opt2 = "LAT:"
elif opt == "en":
    opt2 = "ENG:"

# Check variable values
print "opt: ", opt
print "opt2: ", opt2

if opt in ["es","la","en","ar","fr"] and extent == "begin":
    print time, opt2+(" " + opt2).join([c.encode('latin-1') for c in child.tail.split(' ')])

rawDescription: Python AND OR statements

description: Python AND OR statements


code:
t1 = dendropy.Tree(stream=StringIO("(8,3)"),schema="newick")

# Explicitly create and add the taxon to the taxon set
taxon_1 = dendropy.Taxon(label="5")
t1.taxon_set.add_taxon(taxon_1)

# Create a new node and assign a taxon OBJECT to it (not a label)
n = dendropy.Node(taxon=taxon_1, label='5')

# Now this works
print t1.find_node_with_taxon_label("5")

rawDescription: Adding new nodes to a Tree by dendroPy

description: Adding new nodes to a Tree by dendroPy


code:
for dd in List1:
    if dd["a"] > 1.3 * dd["b"]:
        print dd["value"]

rawDescription: Compare values of keys in Python list with multiple dictionaries


code:
from selenium import webdriver

DEMO_PAGE = '''
    data:text/html,
    <form><input type="number" id="field_id"></form>
'''

browser = webdriver.Firefox()
browser.get(DEMO_PAGE)

input_number = browser.find_element_by_id('field_id')
input_number.send_keys('12')

input_number_value = input_number.get_attribute('value')
print "input_number_value = " + input_number_value

rawDescription: Selenium send_keys doesn't work if input type="number"


code:
# PART 1. The Lexer

symbols = None

def read(input):
    global symbols
    import re
    symbols = re.findall(r'\w+|[()]', input)

def getsym():
    global symbols
    return symbols[0] if symbols else None

def popsym():
    global symbols
    return symbols.pop(0)

# PART 2. The Parser
# Built upon the following grammar:
#
#     program = expr*
#     expr    = '(' func args ')'
#     func    = AND|OR|NOT
#     args    = arg*
#     arg     = string|expr
#     string  = [a..z]

def program():
    r = []
    while getsym():
        r.append(expr())
    return r

def expr():
    popsym() # (
    f = func()
    a = args()
    popsym() # )
    return {f: a}

def func():
    return popsym()

def args():
    r = []
    while getsym() != ')':
        r.append(arg())
    return r

def arg():
    if getsym() == '(':
        return expr()
    return string()

def string():
    return popsym()

# TEST = Lexer + Parser

def parse(input):
    read(input)
    return program()

print parse('(AND a b (OR c d)) (NOT foo) (AND (OR x y))')
# [{'AND': ['a', 'b', {'OR': ['c', 'd']}]}, {'NOT': ['foo']}, {'AND': [{'OR': ['x', 'y']}]}]

rawDescription: Recursive parsing for lisp like syntax

description: Recursive parsing for lisp like syntax


code:
for line in infile:
    columns = line.strip().split()
    for x in data[columns[0]]:
        for y in data[columns[1]]:
            print x, y

rawDescription: Retrieving and combining multiple dictionary entries


code:
from collections import Counter
from itertools import izip
import csv

with open(filename, 'rb') as f:
    reader = csv.reader(f, delimiter='\t')
    keys = next(reader)
    counts = Counter()
    for i, row in enumerate(reader):
        counts.update(k for k, v in izip(keys, row) if v)
    line_count = i + 1
    for k in keys:
        print k, 'density:', 1.0 * counts[k] / line_count

rawDescription: Python - Calculate field density in csv/tsv file


code:
from Xlib.display import Display
import Xlib
from Xlib import X
import Xlib.XK
import sys
import signal
import time
display = None
root = None

def handle_event(event):
    print "handle!"
    if (event.type == X.KeyRelease):
        send_key("x")

# from http://shallowsky.com/software/crikey/pykey-0.1
def send_key(emulated_key):
    shift_mask = 0 # or Xlib.X.ShiftMask
    window = display.get_input_focus()._data["focus"]
    keysym = Xlib.XK.string_to_keysym(emulated_key)
    keycode = display.keysym_to_keycode(keysym)
    event = Xlib.protocol.event.KeyPress(
        time = int(time.time()),
        root = root,
        window = window,
        same_screen = 0, child = Xlib.X.NONE,
        root_x = 0, root_y = 0, event_x = 0, event_y = 0,
        state = shift_mask,
        detail = keycode
        )
    window.send_event(event, propagate = True)
    event = Xlib.protocol.event.KeyRelease(
        time = int(time.time()),
        root = display.screen().root,
        window = window,
        same_screen = 0, child = Xlib.X.NONE,
        root_x = 0, root_y = 0, event_x = 0, event_y = 0,
        state = shift_mask,
        detail = keycode
        )
    window.send_event(event, propagate = True)

def main():
    # current display
    global display,root
    display = Display()
    root = display.screen().root

    # we tell the X server we want to catch keyPress event
    root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask)
    # just grab the "1"-key for now
    root.grab_key(10, 0, True,X.GrabModeSync, X.GrabModeSync)

    signal.signal(signal.SIGALRM, lambda a,b:sys.exit(1))
    signal.alarm(10)
    while 1:
        event = display.next_event()
        print "event"
        handle_event(event)
        display.allow_events(X.AsyncKeyboard, X.CurrentTime)

if __name__ == '__main__':
    main()

rawDescription: globally capture, ignore and send keyevents with python xlib, recognize fake input

description: Globally capture, ignore and send keyevents with python xlib, recognize fake input


code:
from urlparse import urljoin

base = "http://www.example.com/dir1/dir1/"
print urljoin(base, "/page.htm")
print urljoin(base, "page.htm")
print urljoin(base, "../page.htm")
print urljoin(base, '../../page.htm')

rawDescription: Convert partial URL from parsed page to complete URL


code:
import os, urllib, yaml

os_url = "http://planet.openstreetmap.org/replication/"
base_dir = "./"

try:
    with open('download.log', 'r') as f:
        last_set = int(f.readlines()[-1].strip())
except:
    last_set = int(raw_input("Enter latest set:"))

stream = urllib.urlopen(os_url+"changesets/state.yaml")
latest_set = yaml.load(stream)['sequence']

for seq in range(last_set+1,latest_set+1):
    changeset_dir = "changesets/000/%03d/"%(seq/1000)
    changeset_file = "%03d.osm.gz"%(seq%1000)
    remote_file = os_url + changeset_dir + changeset_file
    local_dir = base_dir + changeset_dir
    local_file = local_dir + changeset_file

    if not os.path.exists(local_dir):
        os.makedirs(local_dir)

    try:
        with open(local_file) as f: pass
        print "Already downloaded",seq
    except:
        urllib.urlretrieve(remote_file, local_file)
        print "Downloaded changeset",seq

print "Up-to-date"

with open('download.log', 'a') as f:
    f.write('%s\n'%latest_set)

rawDescription: Automate daily downloading

description: Automate daily downloading


code:
import textwrap
s = "This is a string with length of some arbitrary number greater than 20"
print textwrap.fill(s, 20)

rawDescription: Formatting a string by word at a specific character length


code:
import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
    print process.ProcessId, process.Name, process.Status

rawDescription: Python: How to monitor status of multiple Python processes running in Windows Scheduler


code:
ord(u"ć")
u"café"[2]
u"café"[3]
for c in u"café":
    print repr(c), ord(c)

rawDescription: Python and unicode code point extraction


code:
from datetime import date, time, datetime
naive_midnight = datetime.combine(date.today(), time())
tz = pytz.timezone('Asia/Kolkata')
# Attach local timezone to above naive datetime object
midnight_user_timezone = tz.localize(naive_midnight)
print midnigth_user_timezone
# Convert to UTC
midnight_to_utc = midnight_user_timezone.astimezone(pytz.UTC)
print midnigth_to_utc

rawDescription: how to make sure that the UTC date saved in the database is equivalent to midnight in the specified timezone in Django

description: Make sure that the UTC date saved in the database is equivalent to midnight in the specified timezone in Django


code:
myDictionary = {}
with open('input.txt','r') as f:
    for line in f:
        x = line.split(':')[1].rstrip().split(',')
        for i in xrange(len(x)):
            try:
                x[i] = int(x[i])
            except ValueError:
                x[i] = float(x[i])
        myDictionary[line.split(':')[0]] = x
print myDictionary

rawDescription: Creating a Dictionary from a while loop (Python)


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

set1 = np.zeros((100, 2))
set2 = np.zeros((320, 2))
set3 = np.zeros((100, 2))

# p1
set1[:, 0] = np.arange(-50, 50, 1)
set1[:, 1] = 50+np.random.binomial(5, 0.4, size=100)

# p2 and p3
set2[:, 0] = np.arange(-20, 50, 70.0/320)
set2[:, 1] = 30+np.random.binomial(8, 0.25, size=320)

# p0
sp = np.array([0.0, 0.0])

tstamp = time.time()

# building line direction vectors
s1 = set1                   # set 1 is already the direction vector as sp=[0,0]
s2 = set2[1:] - set2[0:-1]  # set 2 direction vector (p3-p2)

num_master = np.size(s1, axis=0)
num_measure = np.size(s2, axis=0)

# calculate intersection
div = np.transpose(np.repeat([s1[:, 0]], num_measure, axis=0)) * s2[:, 1] - \
      np.transpose(np.transpose(np.repeat([s2[:, 0]], num_master, axis=0)) * s1[:, 1])

s = np.transpose(np.repeat([s1[:, 1]], num_measure, axis=0)) * set2[:-1, 0] - \
    np.transpose(np.repeat([s1[:, 0]], num_measure, axis=0)) * set2[:-1, 1]
s = s / div

t = s2[:, 1] * set2[:-1, 0] - s2[:, 0] * set2[:-1, 1]
t = t / div

# get results by masking invalid results
mask = np.bitwise_and(np.bitwise_and(s>=0, s<=1),
                      np.bitwise_and(t>=0, t<=1))

# mask indices
ind1 = mask.sum(1)>0
t[np.invert(mask)] = 0
ind2 = np.argmax(t[ind1], axis=1)

# calculate result
projected_profile = s1[ind1] * np.transpose(np.repeat([t[ind1, ind2]], 2, axis=0))

print time.time()-tstamp

plt.plot(set1[:, 0], set1[:, 1], '*',
         set2[:, 0], set2[:, 1], '-',
         projected_profile[:, 0], projected_profile[:, 1], 'x',
         sp[0], sp[1], 'o')

plt.show()

rawDescription: Central projection on linesegments

description: Central projection on linesegments


code:
class Struct(Structure):
    _fields_ = [("a", c_int), ("b", c_int)]

    def __init__(self, a, b=2):
        super(Struct, self).__init__(a, b)

    def print_values(self):
        print self.a, self.b

rawDescription: Default values in a ctypes Structure

description: Default values in a ctypes Structure


code:
with open(r'C:\Hubble\Cimage_Project\Index40Count.txt', 'r+') as a:
    contents = a.read() # read the entire file
    if contents != textResult:
        a.seek( 0 ) # seek back to the beginning of the file
        a.truncate() # truncate in case the old value was longer than the new value
        a.write(textResult)
        #then do a bunch more code
        print "not passing"

rawDescription: Python if else statement not passing or not reading txt


code:
import StringIO
import networkx as nx
data = StringIO.StringIO("""1,2

2,3

1,3
""")

G = nx.read_edgelist(data, delimiter=',', nodetype=str)
for e in G.edges():
    print e
# ('1', '3')
# ('1', '2')
# ('3', '2')

rawDescription: Read csv edgelist into networkx

description: Read csv edgelist into networkx


code:
#!/usr/bin/python

"""Equation solving game."""

from random import randint
from random import choice

def gen_randoms(lower, higher):

    """Generates four random numbers between provided bounds."""

    integers = [randint(lower, higher) for x in range(4)]
    return integers

def gen_equation(integers):

    """Generates a random equation from four provided integers."""

    nums = [str(i) for i in integers]
    operators = ['*', '+', '-']
    equation = 'num op num op num op num'
    while 'op' in equation:
        equation = equation.replace('op', choice(operators), 1)
    while 'num' in equation:
        equation = equation.replace('num', choice(nums), 1)
    return equation

def evaluate(equation):

    """Evaluates an equation."""

    return eval(equation)

def main():

    """Main game function."""

    lower = int(raw_input("Enter a lower integer constraint: "))
    higher = int(raw_input("Enter a higher integer constraint: "))
    nums = gen_randoms(lower, higher)
    streak = 0

    while True:
        this_equation = gen_equation(nums)
        print this_equation

        user_answer = raw_input("What is the answer? ('Q' to quit) ")

        if user_answer.lower()[0] == 'q':
            break

        gen_answer = evaluate(this_equation)
        print 'The answer was: %d' % gen_answer

        if gen_answer == int(user_answer):
            streak += 1
            print 'Correct!'
            print 'Current streak: %d' % streak
        else:
            streak = 0
            print 'Incorrect!'

if __name__ == "__main__":
    main()

rawDescription: Python: Refactor code to remove global variable


code:
import numpy as np
import cv2
import math

THRESHOLD = 240

def subimage(image, center, theta, width, height):
    if 45 < theta <= 90:
        theta = theta - 90
        width, height = height, width

    theta *= math.pi / 180 # convert to rad
    v_x = (math.cos(theta), math.sin(theta))
    v_y = (-math.sin(theta), math.cos(theta))
    s_x = center[0] - v_x[0] * (width / 2) - v_y[0] * (height / 2)
    s_y = center[1] - v_x[1] * (width / 2) - v_y[1] * (height / 2)
    mapping = np.array([[v_x[0],v_y[0], s_x], [v_x[1],v_y[1], s_y]])
    return cv2.warpAffine(image, mapping, (width, height), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_REPLICATE)

def auto_crop(image_source):
    # First slightly crop edge - some images had a rogue 2 pixel black edge on one side
    init_crop = 5
    h, w = image_source.shape[:2]
    image_source = image_source[init_crop:init_crop+(h-init_crop*2), init_crop:init_crop+(w-init_crop*2)]

    # Add back a white border
    image_source = cv2.copyMakeBorder(image_source, 5,5,5,5, cv2.BORDER_CONSTANT, value=(255,255,255))

    image_gray = cv2.cvtColor(image_source, cv2.COLOR_BGR2GRAY)
    _, image_thresh = cv2.threshold(image_gray, THRESHOLD, 255, cv2.THRESH_BINARY)
    image_thresh2 = image_thresh.copy()
    image_thresh2 = cv2.Canny(image_thresh2, 100, 100, apertureSize=3)
    points = cv2.findNonZero(image_thresh2)

    centre, dimensions, theta = cv2.minAreaRect(points)
    rect = cv2.minAreaRect(points)

    width = int(dimensions[0])
    height = int(dimensions[1])

    box = cv2.boxPoints(rect)
    box = np.int0(box)

    temp = image_source.copy()
    cv2.drawContours(temp, [box], 0, (255,0,0), 2)

    M = cv2.moments(box)
    cx = int(M['m10']/M['m00'])
    cy = int(M['m01']/M['m00'])

    image_patch = subimage(image_source, (cx, cy), theta+90, height, width)

    # add back a small border
    image_patch = cv2.copyMakeBorder(image_patch, 1,1,1,1, cv2.BORDER_CONSTANT, value=(255,255,255))

    # Convert image to binary, edge is black. Do edge detection and convert edges to a list of points.
    # Then calculate a minimum set of points that can enclose the points.
    _, image_thresh = cv2.threshold(image_patch, THRESHOLD, 255, 1)
    image_thresh = cv2.Canny(image_thresh, 100, 100, 3)
    points = cv2.findNonZero(image_thresh)
    hull = cv2.convexHull(points)

    # Find min epsilon resulting in exactly 4 points, typically between 7 and 21
    # This is the smallest set of 4 points to enclose the image.
    for epsilon in range(3, 50):
        hull_simple = cv2.approxPolyDP(hull, epsilon, 1)

        if len(hull_simple) == 4:
            break

    hull = hull_simple

    # Find closest fitting image size and warp/crop to fit
    # (ie reduce scaling to a minimum)

    x,y,w,h = cv2.boundingRect(hull)
    target_corners = np.array([[0,0],[w,0],[w,h],[0,h]], np.float32)

    # Sort hull into tl,tr,br,bl order.
    # n.b. hull is already sorted in clockwise order, we just need to know where top left is.

    source_corners = hull.reshape(-1,2).astype('float32')
    min_dist = 100000
    index = 0

    for n in xrange(len(source_corners)):
        x,y = source_corners[n]
        dist = math.hypot(x,y)

        if dist < min_dist:
            index = n
            min_dist = dist

    # Rotate the array so tl is first
    source_corners = np.roll(source_corners , -(2*index))

    try:
        transform = cv2.getPerspectiveTransform(source_corners, target_corners)
        return cv2.warpPerspective(image_patch, transform, (w,h))

    except:
        print "Warp failure"
        return image_patch

cv2.namedWindow("Result")
image_src = cv2.imread("xray.png")
image_cropped = auto_crop(image_src)
cv2.imwrite("cropped xray.png", image_cropped)
cv2.imshow("Result", image_cropped)
cv2.waitKey(0)

rawDescription: Align x-ray images: find rotation, rotate and crop


code:
import wx

class MyForm(wx.Frame):

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

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

        randomId = wx.NewId()
        self.Bind(wx.EVT_MENU, self.onKeyCombo, id=randomId)
        accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('Q'), randomId )])
        self.SetAcceleratorTable(accel_tbl)

        text = wx.TextCtrl(panel)
        text.SetFocus()

    #----------------------------------------------------------------------
    def onKeyCombo(self, event):
        """"""
        print "You pressed CTRL+Q!"

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

rawDescription: Using accelerator table without menuitem


code:
typ = QVariant(6)
num = QVariant(0)
args_list = [typ, num]

self.dynamicCall('GetNumHWUnits(int, int&)', args_list)

print 'Num of HW units =', args_list[1].toInt()[0]

rawDescription: Passing integer by reference in python

description: Passing integer by reference in python


code:
instance = conn.get_all_instances(instance_ids=['instance_id'])
print instance[0].instances[0].start()

rawDescription: Starting a stopped EC2 instance with Boto


code:
import pygtk
pygtk.require('2.0')
import gtk
import wnck
import re
import sys
import time

screen = wnck.screen_get_default()
while gtk.events_pending():
    gtk.main_iteration()

titlePattern = re.compile('.*Geany.*')

windows = screen.get_windows()
for w in windows:
  if titlePattern.match(w.get_name()):
    print w.get_name()
    w.activate(int(time.time()))

rawDescription: python-wnck set focus to window

description: Python-wnck set focus to window


code:
db = MongoClient()
users = db.test.users
for u in users.find({}, {"_id": 1}).limit(10):
    print "!!!", u

rawDescription: How to get a list of _id names in PyMongo

description: Get a list of _id names in PyMongo


code:
d1 = {"foo": [1, 2, 3]}
d2 = d1.copy()
# Add a new element to d2 to show that the copy worked
d2["bar"] = []

# The two dicts are different.
print d1
print d2

# However, the list wasn't copied
# it's the same object that shows up in 2 different dicts
print d1["foo"] is d2["foo"]

# So that's what happens in your code: you're mutating the list.
d1["foo"].append(5)
print d2["foo"]

rawDescription: Python dictionary is somehow updating

description: Python dictionary is somehow updating


code:
def phrase(fruit):
    return "I like eating " + fruit + "s."

def say_fruit(fruit):
    print phrase(fruit)

rawDescription: Replacing variables in Python

description: Replacing variables in Python


code:
def send_request(self, xml_string):
    send_string = struct.pack('i', len(xml_string)) + xml_string
    size = len(send_string)
    sent = 0
    while sent < size:
        try:
            sent += self.sock.send(send_string[sent:])
        except socket.error:
            print >> sys.stderr, "The connection to the server was lost."
            break
    else:
        print "Request sent."

rawDescription: communicate with java server through python client

description: Communicate with java server through python client


code:
import functools

def Y(f):
    @functools.wraps(f)
    def Yf(*args):
        return inner(*args)
    inner = f(Yf)
    return Yf

@Y
def get_data(f):
    def inner_get_data(*args):
        # This is your real get data function
        # define it as normal
        # but just refer to it as 'f' inside itself
        print 'setting get_data.foo to', args
        f.foo = args
    return inner_get_data

get_data(1, 2, 3)

print get_data.foo

rawDescription: How can I refer to a function not by name in its definition in python?

description: Refer to a function not by name in its definition in python


code:
from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join()

rawDescription: Recommended language for multithreaded data work


code:
l = ['exiv2-devel', 'mingw-libs', 'tcltk-demos', 'fcgi', 'netcdf',
    'pdcurses-devel',     'msvcrt', 'gdal-grass', 'iconv', 'qgis-devel',
    'qgis1.1', 'php_mapscript']

if len(l) % 2 != 0:
    l.append(" ")

split = len(l)/2
l1 = l[0:split]
l2 = l[split:]
for key, value in zip(l1,l2):
    print '%-20s %s' % (key, value)         #python <2.6
    print "{0:<20s} {1}".format(key, value) #python 2.6+

rawDescription: How to print a list more nicely?

description: Print a list more nicely


code:
dictA = {"f1" : ["m"],
        "f2" : ["p","y","o","a","s","d","f","g"],
        "f3" : ["w","t"],
         "f5" : ["z","x"],
         "f6" : ["c","v"]}

result = []
limit_size = 3
values_list = []
for values in dictA.itervalues():
    values_list.append(len(values))

for i in range(0,max(values_list)):
    keys = list(dictA.keys())
    count = 0
    while count < len(keys):
        try:
            result.append(dictA[keys[count]][i])
        except IndexError:
            dictA.pop(keys[count])
        count = count + 1
print "final ",result[0:limit_size]

rawDescription: Python Form a List based from the values in dictionary


code:
import os
count = 0
for doc in os.listdir('C:\Users\B\Desktop\\newdocs'):
    if doc.endswith(".txt"):
        with open(doc, 'r') as f:
            single_line = ''.join([line for line in f])
            single_space = ' '.join(single_line.split())

        with open("new_doc{}.txt".format(count) , "w") as doc:
            doc.write(single_space)
        count += 1
    else:
        continue

rawDescription: Combine multiple lines of text documents into one


code:
class test():
    def __init__(self, var):
        self.var = var

    def value(self):
        print self.var

test_inst = test(var)
test_inst.value()

rawDescription: How to pass a variable from a function to a class python

description: Pass a variable from a function to a class python


code:
def fast_primes(max_n):
    numbers = range(3, max_n+1, 2)
    half = (max_n)//2
    initial = 4

    for step in xrange(3, max_n+1, 2):
        for i in xrange(initial, half, step):
            numbers[i-1] = 0
        initial += 2*(step+1)

        if initial > half:
            return [2] + filter(None, numbers)
start = 5
tmp = []
while len(tmp) <  10001:
    tmp=fast_primes(start)
    start <<=1

print tmp[10001]

rawDescription: Finding specific prime number

description: Finding specific prime number


code:
# Server code
import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer

def is_even(n):
    return n%2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(is_even, "is_even")
server.serve_forever()

# Client code
import xmlrpclib

proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
print "3 is even: %s" % str(proxy.is_even(3))
print "100 is even: %s" % str(proxy.is_even(100))

rawDescription: Getting some response from a python socket server


code:
import timeit
import datetime
def getList(size):
    s=[]
    for i in range(size):
        s.append(str(i))
    return s

def getSubList(size):
    s=[]
    for i in range(size/2):
        s.append(str(i))
    return s

def testSet(size):
    s=set(getList(size))
    sublist=set(getSubList(size))
    list(s&sublist)

def testIntersection(size):
    s=set(getList(size))
    sublist=set(getSubList(size))
    list(s.intersection(sublist))

def bruteForce(size):
    s=getList(size)
    sublist=getSubList(size)
    final=[]
    for elem in sublist:
        try:
            if s.index(elem)!=-1:
                final.append(elem)
        except ValueError:
            pass
currentsize=1000
for i in range(3):
    t1=datetime.datetime.now()
    testSet(1000)
    t2=datetime.datetime.now()
    testIntersection(currentsize)
    t3=datetime.datetime.now()
    bruteForce(currentsize)
    t4=datetime.datetime.now()
    print "Size="+str(currentsize)
    print "setoptime:"+str(t2-t1)
    print "setintersectiontime:"+str(t3-t2)
    print "bruteforcetime:"+str(t4-t3)
    currentsize=currentsize*10

rawDescription: Is there a faster way to check if a value is in a set in python?


code:
import os.path
with open('idfile') as fin:
     header = next(fin) #read the `#ID` comment header line.  Don't use it.
     for line in fin:
         fname = 'datafileID{0}.csv'.format(line.strip())
         if not os.path.exists(fname):
             print fname

rawDescription: Checking That All Files Are Present

description: Checking That All Files Are Present


code:
g = 0 # index for age
n = 0 # index for name
st_names = [0] * 3
st_age = [0] * 3

for n in range(0, 3): # loop used in our book for loading arrays
    st_names[n] = raw_input("Enter Student name")

    st_age[n] = int(raw_input("Enter student age ")) # Change g to n, because that's the variable increment in the loop

print st_names
print st_age

rawDescription: python 2.7.5, getting multiple user input for loading more than 1 array/list, problems with looping

description: Python 2.7.5, getting multiple user input for loading more than 1 array/list, problems with looping


code:
import calendar
cal = calendar.Calendar(0)
month = cal.monthdatescalendar(2010, 7)
lastweek = month[-1]
monday = lastweek[0]
print(monday)

2010-07-26

rawDescription: Python: get last Monday of July 2010

description: Python: get last Monday of July 2010


code:
print "welcome to your short story"

name = raw_input("Input your name: ")
# Convert the input to an integer.
age = int(raw_input("Input your age: "))

# This is the status variable being either adult or child
# before you were overriding age variable with adult or kid
status = ""

# Also, you have to handle the case where the age equals 21, before
# you were just checking if it is less or greater than 21
if age >= 21:
    status = "adult"
elif age < 21:
    status = "kid"

print "My name is ", name ," and I am a " , age ," year old " , status

rawDescription: Simple Python "if", "elif", "else"

description: Simple Python "if", "elif", "else"


code:
class D(object):
    def create(self):
        self.foo = 100
        self.bar = 'squirrels'
        baz = 'I have %d insane %s in my head.' % (self.foo, self.bar)
        return baz

class C(D):
    def create(self):
        super(C, self).create()
        print self.foo
        self.baz = 'I have %s cute %s in my yard.' % (self.foo, self.bar)

C().create()

rawDescription: Python class methods overloading

description: Python class methods overloading


code:
def assem(myFile):
    print "Hello ,World!"
    import myParser
    from myParser import Parser
    import code
    import symboleTable
    from symboleTable import SymboleTable

    newFile = "Prog.hack"
    output = open(newFile, 'w')
    input = open(myFile, 'r')
    prsr = Parser(input)

    while prsr.hasMoreCommands():
        str = "BLANK"
        if(parser.commandType() == Parser.C_COMMAND):
            str= "111" + code.comp(prsr.comp()
                ) + code.dest(prsr.dest()) + code.jump(prsr.jump()
                ) + "\n"

        output.write(str)
        prsr.advance()

if __name__ == "__main__":
    import sys
    assem(sys.argv[1])

rawDescription: Running python script from console

description: Running python script from console