code:
def do_whatever():
    def sub(text):
        captured.append(text)
        return "new"

    captured = []
    result = re.sub(r".*", sub, "test test")
    print captured

    captured = []
    result = re.sub(r".*", sub, "foo bar")
    print captured

rawDescription: getting groups with python re.sub

description: Getting groups with python re.sub


code:
for i in range(1, 256):
    name = "key{}".format(i)
    print globals()[name]

rawDescription: Use variable in variable name

description: Use variable in variable name


code:
new_trigrams = []
c = 2
while c < len(token) - 2:
    new_trigrams.append((token[c], token[c+1], token[c+2]))
    c += 2
print new_trigrams

rawDescription: Python NLTK: Bigrams trigrams fourgrams

description: Python NLTK: Bigrams trigrams fourgrams


code:
import input_data
import datetime
import numpy as np
import tensorflow as tf
import cv2
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from random import randint

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

x = tf.placeholder("float", [None, 784])

W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x,W) + b)
y_ = tf.placeholder("float", [None,10])

cross_entropy = -tf.reduce_sum(y_*tf.log(y))

train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

#Train our model
iter = 1000
for i in range(iter):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

#Evaluationg our model:
correct_prediction=tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
print "Accuracy: ", sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})

#1: Using our model to classify a random MNIST image from the original test set:
num = randint(0, mnist.test.images.shape[0])
img = mnist.test.images[num]

classification = sess.run(tf.argmax(y, 1), feed_dict={x: [img]})
'''
#Uncomment this part if you want to plot the classified image.
plt.imshow(img.reshape(28, 28), cmap=plt.cm.binary)
plt.show()
'''
print 'Neural Network predicted', classification[0]
print 'Real label is:', np.argmax(mnist.test.labels[num])

#2: Using our model to classify MNIST digit from a custom image:

# create an an array where we can store 1 picture
images = np.zeros((1,784))
# and the correct values
correct_vals = np.zeros((1,10))

# read the image
gray = cv2.imread("my_digit.png", 0 ) #0=cv2.CV_LOAD_IMAGE_GRAYSCALE #must be .png!

# rescale it
gray = cv2.resize(255-gray, (28, 28))

# save the processed images
cv2.imwrite("my_grayscale_digit.png", gray)
"""
all images in the training set have an range from 0-1
and not from 0-255 so we divide our flatten images
(a one dimensional vector with our 784 pixels)
to use the same 0-1 based range
"""
flatten = gray.flatten() / 255.0
"""
we need to store the flatten image and generate
the correct_vals array
correct_val for a digit (9) would be
[0,0,0,0,0,0,0,0,0,1]
"""
images[0] = flatten

my_classification = sess.run(tf.argmax(y, 1), feed_dict={x: [images[0]]})

"""
we want to run the prediction and the accuracy function
using our generated arrays (images and correct_vals)
"""
print 'Neural Network predicted', my_classification[0], "for your digit"

rawDescription: How to test tensorflow cifar10 cnn tutorial model

description: Test tensorflow cifar10 cnn tutorial model


code:
class Projet(object):

    def pathDirectory(self):
        print "- - in pathDirectory - -"
        self.pathDir= str(QFileDialog.getExistingDirectory(ui.pathTab1, 'Select Path','', QFileDialog.ShowDirsOnly))

    def goFunc(self):
        print "- - In goFunc - -"
        print self.pathDir

if __name__ == "__main__":
    p = Projet()
    p.pathDirectory()
    p.goFunc()

rawDescription: Sharing values between methods

description: Sharing values between methods


code:
p = Post(title='Some title')
print p.title  # prints 'Some title'

rawDescription: Difference between Class Members and Instance Members in Django and "Ordinary" Python?


code:
def fizzbuzz(num_range):
    for each in num_range:
        if each % 3 == 0 and each % 5 == 0:
            print "fizzbuzz"
        elif each % 3 == 0:
            print "fizz"
        elif each % 5 == 0:
            print "buzz"
        else:
            print each

print fizzbuzz(range (1,101))

rawDescription: Python, Fizz Buzz inserting the range as a parameter of the function


code:
import sys
import chilkat

compress = chilkat.CkCompression()

#  Any string argument automatically begins a 30-day trial.
success = compress.UnlockComponent("30-day trial")
if (success != True):
    print "Compression component unlock failed"
    sys.exit()

compress.put_Algorithm("ppmd")

#  Decompress back to the original:
success = compress.DecompressFile("t.zipx", "t")
if (success != True):
    print compress.lastErrorText()
    sys.exit()

print "Success!"

rawDescription: Accessing .zipx with Python

description: Accessing .zipx with Python


code:
def extract_function_from_string(content):
    exec(content)
    start = 4
    end = content.find("(")
    function_name = content[start:end]
    return eval(function_name)
my_function = extract_function_from_string("def my_function(x):\n    return x**2")
print my_function(5)

rawDescription: How to extract a function given by a content string in Python

description: Extract a function given by a content string in Python


code:
longSubjectNames = [] #example: "Academy for Classical Acting"
abbreviations = [] #example: "ACA"

with open ("/Users/it/Desktop/Classbook/classAbrevs.txt", "r") as myfile:
    lines = myfile.readlines()
    for line in lines:
        name, abrev = line.replace("\n","").split(",")
        longSubjectNames.append(name)
        abbreviations.append(abrev)

print longSubjectNames
print abbreviations

rawDescription: Split .txt file and Append to Two Separate Lists Python


code:
row_format ="{:>15}" * (len(teams_list) + 1)
print row_format.format("", *teams_list)
for team, row in zip(teams_list, data):
    print row_format.format(team, *row)

rawDescription: Python: Printing Lists as Tabular Data

description: Python: Printing Lists as Tabular Data


code:
l = [True, [[True, False], [False, [False, True]]]]

def foo(x):
    if isinstance(x, list):
        return foo(x[0]) or foo(x[1])
    else:
        return x

print foo(l)

rawDescription: recurse over a list

description: Recurse over a list


code:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect("9.9.9.9", 22, "username", "password")
channel = client.invoke_shell()
channel.settimeout(0.0)

while True:
    r, w, e = select.select([channel], [], [])
    try:
        console_data = ""
        while channel.recv_ready():
            console_data += channel.recv(1024)
        if len(console_data) == 0:
           print "\n*** EOF\n"
           break

        # Search console_data for console prompt
        # If found, start a serial console
        if re.search("->", console_data):
            channel.send("start -script SP/Console")
        elif re.search("y/n", console_data):
            channel.send("y\n")
        elif re.search("SOME STRING ON CONSOLE", console_data):
            print "Action completed"
            break
    except socket.timeout:
        pass
channel.close()
client.close()

rawDescription: Monitor console output via ssh in python


code:
import logging
import matplotlib
from matplotlib.widgets import Lasso
from matplotlib.colors import colorConverter
from matplotlib.collections import RegularPolyCollection
from matplotlib import path
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import rand

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

class Datum(object):
      colorin = colorConverter.to_rgba('red')
      colorShift = colorConverter.to_rgba('cyan')
      colorCtrl = colorConverter.to_rgba('pink')
      colorout = colorConverter.to_rgba('blue')

      def __init__(self, x, y, include=False):
         self.x = x
         self.y = y
         if include:
            self.color = self.colorin
         else:
            self.color = self.colorout

class LassoManager(object):
      def __init__(self, ax, data):
         self.axes = ax
         self.canvas = ax.figure.canvas
         self.data = data

         self.Nxy = len(data)

         facecolors = [d.color for d in data]
         self.xys = [(d.x, d.y) for d in data]
         fig = ax.figure
         self.collection = RegularPolyCollection(fig.dpi, 6, sizes=(100,),facecolors=facecolors, offsets = self.xys, transOffset = ax.transData)

         ax.add_collection(self.collection)

         self.cid = self.canvas.mpl_connect('button_press_event', self.onpress)
         self.keyPress = self.canvas.mpl_connect('key_press_event', self.onKeyPress)
         self.keyRelease = self.canvas.mpl_connect('key_release_event', self.onKeyRelease)
         self.pick=self.canvas.mpl_connect('pick_event', self.onpick)
         self.lasso = None
         self.shiftKey = False
         self.ctrlKey = False
         self.pickEvent = False

      def callback(self, verts):
          logging.debug('in LassoManager.callback(). Shift: %s, Ctrl: %s' % (self.shiftKey, self.ctrlKey))
          facecolors = self.collection.get_facecolors()
          p = path.Path(verts)
          ind = p.contains_points(self.xys)
          for i in range(len(self.xys)):
              if ind[i]:
                 if self.shiftKey:
                    facecolors[i] = Datum.colorShift
                 elif self.ctrlKey:
                    facecolors[i] = Datum.colorCtrl
                 else:
                    facecolors[i] = Datum.colorin
                    print self.xys[i]
              else:
                   facecolors[i] = Datum.colorout

          self.canvas.draw_idle()
          self.canvas.widgetlock.release(self.lasso)
          del self.lasso

      def onpress(self, event):
            logging.debug('in LassoManager.onpress(). Event received: %s' % event)
            if self.pickEvent:
                self.pickEvent = False
                return
            if self.canvas.widgetlock.locked(): return
            if event.inaxes is None: return
            self.lasso = Lasso(event.inaxes, (event.xdata, event.ydata), self.callback)
            # acquire a lock on the widget drawing
            self.canvas.widgetlock(self.lasso)

      def onKeyPress(self, event):
          logging.debug('in LassoManager.onKeyPress(). Event received: %s (key: %s)' % (event, event.key))
          if event.key == 'alt':
             self.ctrlKey = True
          if event.key == 'shift':
             self.shiftKey = True

      def onKeyRelease(self, event):
          logging.debug('in LassoManager.onKeyRelease(). Event received: %s (key: %s)' % (event, event.key))
          if event.key == 'alt':
             self.ctrlKey = False
          if event.key == 'shift':
             self.shiftKey = False

      def onpick(self, event):
          logging.debug('in LassoManager.onpick(). Event received: %s' % event)
          self.pickEvent = True
          if event.mouseevent.button == 3:
             index = event.ind
             print 'onpick scatter: ', index, np.take(x, index), np.take(y, index)

if __name__ == '__main__':
    x,y =rand(2,100)
    data = [Datum(*xy) for xy in zip(x,y)]
    fig = plt.figure()
    ax = plt.axes()

    ax.scatter(x,y,picker=True)

    lman = LassoManager(ax, data)
    plt.show()

rawDescription: Combining matplotlib's mouse button events with pick events


code:
import re
blah = "word word: monty py: thon"
answer = re.sub(r'\w+:\s?','',blah)
print answer

rawDescription: How can I remove all words that end in ":" from a string in Python?

description: Remove all words that end in ":" from a string in Python


code:
words = ['whyyyyyy', 'heyyyy', 'alrighttttt', 'cool', 'mmmmonday']
import urllib2
word_list = set(i.lower() for i in urllib2.urlopen('https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt').read().split('\n'))

found_words = []
for word in (i.lower() for i in words):

    #Check word doesn't exist already
    if word in word_list:
        found_words.append(word)
        continue

    last_char = None
    i = 0
    current_word = word
    while i < len(current_word):

        #Check if it's a duplicate character
        if current_word[i] == last_char:
            current_word = current_word[:i] + current_word[i + 1:]

        #Reset word if no more duplicate characters
        else:
            current_word = word
            i += 1
            last_char = current_word[i]

        #Word has been found
        if current_word in word_list:
            found_words.append(current_word)
            break

print found_words
#['why', 'hey', 'alright', 'cool', 'monday']

rawDescription: String preprocessing

description: String preprocessing


code:
prev = None
for line in sorted(open('file')):
  line = line.strip()
  if prev is not None and not line.startswith(prev):
    print prev
  prev = line
if prev is not None:
  print prev

rawDescription: Removing duplicated lines from a txt file


code:
import re
from collections import Counter
import time

# names file downloaded from http://www.tucows.com/preview/520007
# the set contains over 5400 names
f = open('./names.txt', 'r')
names = [ name.rstrip() for name in f.read().split(',') ]
name_set = set(names) # set of unique names
names_dict = Counter(names) # Counter ~= dict of names with counts

# Expect: 246   lalala   name="Jack";surname="Smith"
pattern = re.compile(r'.*\sname="([^"]*)"')

def select_rows_set():
    f = open('./data.txt', 'r')
    out_f = open('./data_out_set.txt', 'a')
    for record in f.readlines():
        name = pattern.match(record).groups()[0]
        if name in name_set:
            out_f.write(record)
    out_f.close()
    f.close()

def select_rows_dict():
    f = open('./data.txt', 'r')
    out_f = open('./data_out_dict.txt', 'a')
    for record in f.readlines():
        name = pattern.match(record).groups()[0]
        if name in names_dict:
            out_f.write(record)
    out_f.close()
    f.close()

if __name__ == '__main__':
    # One round to time the use of name_set
    t0 = time.time()
    select_rows_set()
    t1 = time.time()
    time_for_set = t1-t0
    print 'Total set: ', time_for_set

    # One round to time the use of names_dict
    t0 = time.time()
    select_rows_dict()
    t1 = time.time()
    time_for_dict = t1-t0
    print 'Total dict: ', time_for_dict

rawDescription: Filter large file using python, using contents of another


code:
items = ['ratelimiter:foobar:201401011157',
 'ratelimiter:foobar:201401011158',
 'ratelimiter:foobar:201401011159',
 'ratelimiter:foobar:201401011200']

print items[0][:[len(set(x)) == 1 for x in zip(*items)].index(False)] + '*'
# ratelimiter:foobar:201401011*

rawDescription: Converting a list of strings to a single pattern


code:
import os

for f in os.listdir('.'):
    print "%s: %s" % (f, getLength(f))

rawDescription: Get total length of videos in a particular directory in python


code:
growths = {'goblin': 200, 'humans': 300, 'ogre': 400}
print growths[race]

rawDescription: how to print the value of the string, not the string it self

description: Print the value of the string, not the string it self


code:
class MiniMath:
    def add(self, a, b):
        return a + b

class MoreMath(MiniMath):
    def sub(self, a, b):
        return a - b

mm = MoreMath()
print mm.add(1, 2) # 3
print mm.sub(1, 2) # -1

rawDescription: Can the python image module be extended?

description: Can the python image module be extended


code:
from ete2 import Tree
t = Tree("(((b,c), a), (d,e));")

print "Method 1: caching node content"
internal_node_content = t.get_cached_content(store_attr="name")
for node, content in internal_node_content.iteritems():
    if not node.is_leaf():
        print node, "\nConcat:", ''.join(content)

print "Method 2: post order iteration"
node2concat = {}
for node in t.traverse("postorder"):
    if not node.is_leaf():
        concat = ''.join([node2concat[ch] for ch in node.children])
        node2concat[node] = concat
        print node, "\nConcat:", concat
    else:
        node2concat[node] = node.name

#Method 1: caching node content
#
#         /-b
#      /-|
#   /-|   \-c
#  |  |
#--|   \-a
#  |
#  |   /-d
#   \-|
#      \-e
#Concat: acbed
#
#   /-b
#--|
#   \-c
#Concat: cb
#
#      /-b
#   /-|
#--|   \-c
#  |
#   \-a
#Concat: acb
#
#   /-d
#--|
#   \-e
#Concat: ed
#Method 2: post order iteration
#
#   /-b
#--|
#   \-c
#Concat: bc
#
#      /-b
#   /-|
#--|   \-c
#  |
#   \-a
#Concat: bca
#
#   /-d
#--|
#   \-e
#Concat: de
#
#         /-b
#      /-|
#   /-|   \-c
#  |  |
#--|   \-a
#  |
#  |   /-d
#   \-|
#      \-e
#Concat: bcade

rawDescription: python ete2 binary tree get the sum of descendants values

description: Python ete2 binary tree get the sum of descendants values


code:
def print_table(table):
    col_width = [max(len(x) for x in col) for col in zip(*table)]
    for line in table:
        print "| " + " | ".join("{:{}}".format(x, col_width[i])
                                for i, x in enumerate(line)) + " |"

table = [(str(x), str(f(x))) for x in mylist]
print_table(table)

rawDescription: Python format tabular output

description: Python format tabular output


code:
for tag in soup.findAll('a', href = re.compile('^/l[0-9]+/.*$')):
    print tag['href']

rawDescription: Problems with regex matching

description: Problems with regex matching


code:
import random
import math
def BoxMullerFormula(a,b):
    c = math.sqrt(-2 * math.log(a)) * math.cos(2 * math.pi * b)
    d = math.sqrt(-2 * math.log(a)) * math.sin(2 * math.pi * b)
    return (c,d)
def BoxM():
     while 1:
        Rand01 = random.random()
        Rand02 = random.random()
        (a,b) = BoxMullerFormula(Rand01,Rand02)
        yield a
        yield b

BoxM01 = BoxM()
for i in xrange(10):
    print BoxM01.next()

rawDescription: Initializing a static variable in Python


code: print ''.join('\\'+hex(ord(char))[1:] for char in 'foo')

rawDescription: Best way to convert "foo" into "\x66\x6f\x6f"

description: Convert "foo" into "\x66\x6f\x6f"


code:
from suds.client import Client
client=Client('http://eample.wsdl')
res = client.service.AbsoluteMove(profile_token, destination, speed)
print res

rawDescription: Passing arguments into SUDS client statement


code:
from subprocess import Popen, PIPE

p = Popen(['python', 'myscript.py', 'olink'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
print output

rawDescription: How to use a python function with the principal python script

description: Use a python function with the principal python script


code: print len(os.listdir(location))

rawDescription: Python: how to run a command while using a defined variable?


code:
#!/usr/bin/python

''' A file that will read the text in the php file and add each array as
a dictionary item to a dictionary and saves it as a dictionary.
'''
import pickle
import pdb

file_name = 'lgvalues-abscissa.php'
#file_name = 'lgvalues-weights.php'
text = 'legendre_roots['
#text = 'quadrature_weights['

def is_number(s):
  try:
    float(s)
    return True
  except ValueError:
    return False

mydict = dict()
lst = []
ft = open(file_name,'rt')
file_lines = ft.readlines()
for i, l in enumerate(file_lines):
  if l.find(text) != -1:
    key = l.split()[0]
    key = [key[l.find('[')+1:l.find(']')],]
    continue
  if is_number(l.strip()[:16]):
    lst.append(float(l.strip()[:16]))
    if l.strip()[-2:] == ');':
      if int(key[0]) != len(lst):
        print 'key %s does not have the right amount of items.'\
            %(key[0])
      tempdict = {}
      tempdict = tempdict.fromkeys(key,lst)
      mydict.update(tempdict)

      lst = []

file_name = file_name[:-4]+'.dat'
fb = open(file_name,'wb')
pickle.dump(mydict,fb)
print 'Dictionary file saved to %s' % (file_name)
fb.close()

rawDescription: Convert a php array to a python dictionary


code:
import Xlib
import Xlib.display

def main():
    display = Xlib.display.Display(':0')
    root = display.screen().root
    root.change_attributes(event_mask=
        Xlib.X.ButtonPressMask | Xlib.X.ButtonReleaseMask)

    while True:
        event = root.display.next_event()
        print event

if __name__ == "__main__":
    main()

rawDescription: Python Xlib catch/send mouseclick

description: Python Xlib catch/send mouseclick


code:
import subprocess as sp

cmd = ["adb","push","file","/mnt/sdcard/file"]
mysp = sp.popen(cmd, env={'ADB_TRACE':'adb'}, stdout=sp.PIPE, stderr=sp.PIPE)
stdout,stderr = mysp.communicate()

if mysp.returncode != 0:
    print stderr
else:
    print stdout

rawDescription: How can I get the info in cmd when I set ADB_TRACE=adb

description: Get the info in cmd when I set ADB_TRACE=adb


code:
import P4

p4 = P4.P4()
try:
    p4.connect()
    info = p4.run("info")
    for key in info[0]:
        print key, "=", info[0][key]
    print
    print

    if 1:
        p4.tagged = 0

        #This makes the output untagged.
        result = p4.run("describe","5707")
        print result

except P4.P4Exception, ex:
    for w in p4.warnings:
        print w

finally:
    p4.disconnect()

rawDescription: need equivalent python command for p4 describe

description: Need equivalent python command for p4 describe


code:
from itertools import izip,tee

cur,nxt = tee(izip(X,Y,Z))

next(nxt,None) #advance nxt iterator

for (x1,y1,z1),(x2,y2,z2) in izip(cur,nxt):
    print x1 + x2 + (y1-y2)/(z1-z2)

rawDescription: How to refer to next element in a loop?

description: Refer to next element in a loop


code: print sum(myList)/len(myList)

rawDescription: Average of all values in a list - is there a more 'Pythonic' way to do this?


code:
import numpy as np
import h5py

f = h5py.File('simdata_020_01.mat','r')
variables = f.items()

for var in variables:
    name = var[0]
    data = var[1]
    print "Name ", name  # Name
    if type(data) is h5py.Dataset:
        # If DataSet pull the associated Data
        # If not a dataset, you may need to access the element sub-items
        value = data.value
        print "Value", value  # NumPy Array / Value

rawDescription: Reading ALL variables in a .mat file with python h5py


code:
from gi.repository import Gtk
from gi.repository import Gdk
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import WebKit
import threading
import time

# Use threads
GLib.threads_init()

class App(object):
    def __init__(self):
        window = Gtk.Window()
        webView = WebKit.WebView()
        window.add(webView)
        window.show_all()

        #webView.load_uri('http://www.google.com') # Here it works on main thread

        self.window = window
        self.webView = webView

    def run(self):
        Gtk.main()

    def show_html(self):
        print 'show html'

        time.sleep(1)
        print 'after sleep'

        # Update widget in main thread
        GLib.idle_add(self.webView.load_uri, 'http://www.google.com') # Here it doesn't work

app = App()

thread = threading.Thread(target=app.show_html)
thread.start()

app.run()
Gtk.main()

rawDescription: Webkit threads with PyGObject on Gtk3

description: Webkit threads with PyGObject on Gtk3


code:
import operator

def stagger(l, w):
    if len(l)>=w:
        return [tuple(l[0:w])]+stagger(l[1:], w)
    return []

def pleat(f, l, w=2):
    return map(lambda p: f(*p), stagger(l, w))

print pleat(operator.add, range(10))
print pleat(lambda x, y, z: x*y/z, range(3, 13), 3)
print pleat(lambda x: "~%s~"%(x), range(10), 1)
print pleat(lambda a, b, x, y: a+b==x+y, [3, 2, 4, 1, 5, 0, 9, 9, 0], 4)

rawDescription: What do we call this (new?) higher-order function?


code:
def my_print(text, begin, end):
    "Print 'text' in UPPER between 'begin' and 'end' in lower"
    try:
      print begin.lower() + text.upper() + end.lower()
    except (AttributeError, TypeError):
      raise AssertionError('Input variables should be strings')

rawDescription: Proper way to assert type of variable in Python


code:
val = raw_input("Type your three digit number please: ")

print 'The first digit is {}'.format(val[0])
print 'The second digit is {}'.format(val[1])
print 'The third digit is {}'.format(val[2])

rawDescription: Writing a program that inputs a three digit number to output the digits on a single line


code:
from subprocess import Popen,PIPE

# Example with output only
p = Popen(["echo", "This is a test"], stdout=PIPE)
out, err = p.communicate()
print out.rstrip()

# Example with input and output
p = Popen("./TestProgram", stdin=PIPE, stdout=PIPE)
out, err = p.communicate("This is the input\n")
print out.rstrip()

rawDescription: Communication between a Python parent and C child processes in Linux


code:
import multiprocessing
import threading
from time import sleep

def run(i, conn):
    try:
        f(i)
    except CustomException as e:
        conn.send(e)

def f(i):
   print i, ' called'
   sleep(i)
   if i == 11:
       raise CustomException
   print i, ' done'

class CustomException(Exception):
    pass

class ProcessManager(object):
    def __init__(self):
        self.processes = []
        self._threads = []
        self._lock = threading.Lock()

    def terminate_all(self):
        with self._lock:
            for p in self.processes:
                if p.is_alive():
                    print "Terminating %s" % p
                    p.terminate()

    def launch_proc(self, func, args=(), kwargs= {}):
        t = threading.Thread(target=self._proc_thread_runner,
                             args=(func, args, kwargs))
        self._threads.append(t)
        t.start()

    def _proc_thread_runner(self, func, args, kwargs):
        parent_conn, child_conn = multiprocessing.Pipe()
        args = args + tuple([child_conn])
        p = multiprocessing.Process(target=func, args=args, kwargs=kwargs)
        self.processes.append(p)
        p.start()
        while p.exitcode is None:
            p.join()
        if parent_conn.poll():
            obj = parent_conn.recv()
            if isinstance(obj, CustomException):
                self.terminate_all()

    def wait(self):
        for t in self._threads:
            t.join()

if __name__ == '__main__':
    proc_manager = ProcessManager()
    for i in range(0, 20):
        proc_manager.launch_proc(run, (i,))
    proc_manager.wait()

rawDescription: Python: terminate all alive processes based on an exception type


code:
import shelve
d = shelve.open('mydb')
d['key1'] = 12345
d['key2'] = value2
print d['key1']
d.close()

rawDescription: What is the least resource intense data structure to distribute with a Python Application


code:
import urllib2
print urllib2.urlopen("https://www.bitstamp.net/api/transactions/").read()

rawDescription: how to implement request GET in Python

description: Implement request GET in Python


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

def f2( a, *args ):
    print "args:", args
    return a + np.sum( args, axis=0 )

x = np.ones(3)
print f2( x, x*2, x*3 )

def quadf( *args ):
    print "quadf args:", args
    return 1

quad( quadf, 0, 1, (2,3) )

rawDescription: How can I vectorize a function in numpy with multiple arguments?

description: Vectorize a function in numpy with multiple arguments


code:
my_plist = plistlib.readPlist()
settings = my_plist["settings"]
for d in settings:
    if "scope" in d:
        if "comment" not in d["scope"]:
            print "A scope with no comment!"
    else:
        print "A dict with no scope!"

rawDescription: Finding if a plist contains a key with a specific string


code:
data = dict((cookie.name, cookie.value) for cookie in cookjar)
print data["PHPSESSID"]

rawDescription: How can I get cookies using urllib2

description: Get cookies using urllib2


code:
for index in range(len(name)):
    print name[index]

rawDescription: Print all the data obatained from FIND_ELEMENTS_BY_ID() - Python


rawDescription: How to loop thru a json strings and convert them to datetime objects?

description: Loop thru a json strings and convert them to datetime objects


code:
def medianoflist(l):
    x = (l[int((len(l)-1)/2)] + l[int(len(l)/2)])/2
    print x

rawDescription: How to get the median of a list?

description: Get the median of a list


code:
x = input ('guess a number between 0 to 100 \n ')
q = 33
if x > q :
    print ('your number is to high')
if x == q :
    print 'you have guessed right'
if x < q :
   print 'your number is to low'

while x != 33 :
    p = input ('try again \n')
    if p == 33 :
        print 'you finally got it'
        break

rawDescription: Try to exit while looping in python

description: Try to exit while looping in python


code:
import _winreg

key = _winreg.OpenKey(
    _winreg.HKEY_LOCAL_MACHINE,
    r"HARDWARE\DESCRIPTION\System\CentralProcessor\0")

value, type = _winreg.QueryValueEx(key, "~MHz")
print 'CPU speed is:', value

rawDescription: How to detect CPU speed and H.D.D rpm in objective-c or python

description: Detect CPU speed and H.D.D rpm in objective-c or python


rawDescription: How to find a max value

description: Find a max value


code:
#! /usr/bin/env python

from scapy.all import *

def pkt2pcap(p):
        sec = int(p.time)
        usec = int(round((p.time-sec)*1000000))
        s = str(p)
        caplen = len(s)
        return struct.pack("IIII", sec, usec, caplen, caplen) + s

# adapted from Scapy's sendpfast
def mysendpfast(x, pps=None, mbps=None, realtime=None, loop=0, file_cache=False, iface=None):
    """Send packets at layer 2 using tcpreplay for performance
    pps:  packets per second
    mpbs: MBits per second
    realtime: use packet's timestamp, bending time with realtime value
    loop: number of times to process the packet list
    file_cache: cache packets in RAM instead of reading from disk at each iteration
    iface: output interface """
    if iface is None:
        iface = conf.iface
    argv = [conf.prog.tcpreplay, "--intf1=%s" % iface ]
    if pps is not None:
        argv.append("--pps=%i" % pps)
    elif mbps is not None:
        argv.append("--mbps=%i" % mbps)
    elif realtime is not None:
        argv.append("--multiplier=%i" % realtime)
    else:
        argv.append("--topspeed")
    if loop:
        argv.append("--loop=%i" % loop)
        if file_cache:
            argv.append("--enable-file-cache")
    argv.append("-")
    try:
        f = subprocess.Popen(argv, stdin=subprocess.PIPE)
        # PCAP header
        p = x.next()
        f.stdin.write(struct.pack("IHHIIII", 0xa1b2c3d4L,
                                  2, 4, 0, 0, MTU,
                                  conf.l2types[p.__class__]))
        # Let's send
        f.stdin.write(pkt2pcap(p))
        for p in x:
            f.stdin.write(pkt2pcap(p))
        f.stdin.close()
        f.communicate()
    except KeyboardInterrupt:
        log_interactive.info("Interrupted by user")
    except Exception,e:
        log_interactive.error("while trying to exec [%s]: %s" % (argv[0],e))

rawDescription: Scapy and tcpreplay: bypass temporary file for performance


code:
import html2text
import sys

print html2text.html2text(sys.stdin.read())

rawDescription: How to STDIN and STDOUT with PHP and Python to use html2text and get a markdown formated text?


rawDescription: How to remove square brackts and single quotes from a list using python?

description: Remove square brackts and single quotes from a list using python


code:
def makeModule(**kwargs):
    print repr(kwargs)

    module_filename = 'generated_module.py'
    with open(module_filename, 'w') as module_file:
        module_file.write('''\
from math import *

def func(voltage, current):
    def f(voltage):
        return exp({a1} * voltage)

    return {a0}*voltage+{b}*current+f(voltage)
'''.format(**kwargs))

    module_name = module_filename.replace('.py', '')
    module = __import__(module_name)
    return module.func

def main():
    func = makeModule(a0=-1.23, b=0.772, a1=-6.32)
    print 'Result:', func(2, 3)

if __name__ == '__main__':
    main()

rawDescription: Dynamically creating nested functions from a yaml script


code:
import random

def find_path(graph, start, end, path, max_):
        # Append to the path
        path.append(start)
        # If the end has been reached, or the length about to exceed, return
        if start == end or len(path) == max_:
            return path

        # Randomly select the next neighbor and traverse it
        find_path(graph, random.choice(graph[start]), end, path, max_)

graph = {1: [9],
    2: [10],
    3: [6, 8],
    4: [1, 6],
    5: [1],
    6: [7],
    7: [1, 3],
    8: [2],
    9: [4],
    10: [3, 5]}

start = random.randint(1,10)
end = random.randint(1,10)

path = []
find_path(graph, start, end, path, 20)
print start, end, path

rawDescription: Traversing a directed graph randomly

description: Traversing a directed graph randomly


code:
import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

x = [dt.datetime(2009, 05, 01), dt.datetime(2010, 06, 01),
     dt.datetime(2011, 04, 01), dt.datetime(2012, 06, 01)]
y = [1, 3, 2, 5]

fig, ax = plt.subplots()
ax.plot_date(x, y, linestyle='--')

ax.annotate('Test', (mdates.date2num(x[1]), y[1]), xytext=(15, 15),
            textcoords='offset points', arrowprops=dict(arrowstyle='-|>'))

fig.autofmt_xdate()
plt.show()

rawDescription: Annotate Time Series plot in Matplotlib

description: Annotate Time Series plot in Matplotlib


code:
visits_country = (['123', 'United States'], ['456', 'United States'],
                  ['1', 'Canada'], ['24', 'Canada'], ['12', 'Mexico'])

dictionary = {}

for count, country in visits_country:
    dictionary[country] = dictionary.get(country, 0) + int(count)

print dictionary

rawDescription: finding all iterations of an element in a tuple and returning its pair

description: Finding all iterations of an element in a tuple and returning its pair


code:
import random
teams = ['One team', 'Another team', 'A third team']
team1, team2 = random.sample(teams, 2)
print team1
print team2

rawDescription: selecting numbers from a list in python

description: Selecting numbers from a list in python


code:
import numpy as np

def main():
    # Generate some random data
    x = np.cumsum(np.random.random(1000) - 0.5)
    condition = np.abs(x) < 1

    # Print the start and stop indicies of each region where the absolute
    # values of x are below 1, and the min and max of each of these regions
    for start, stop in contiguous_regions(condition):
        segment = x[start:stop]
        print start, stop
        print segment.min(), segment.max()

def contiguous_regions(condition):
    """Finds contiguous True regions of the boolean array "condition". Returns
    a 2D array where the first column is the start index of the region and the
    second column is the end index."""

    # Find the indicies of changes in "condition"
    d = np.diff(condition)
    idx, = d.nonzero()

    # We need to start things after the change in "condition". Therefore,
    # we'll shift the index by 1 to the right.
    idx += 1

    if condition[0]:
        # If the start of condition is True prepend a 0
        idx = np.r_[0, idx]

    if condition[-1]:
        # If the end of condition is True, append the length of the array
        idx = np.r_[idx, condition.size] # Edit

    # Reshape the result into two columns
    idx.shape = (-1,2)
    return idx

main()

rawDescription: Find large number of consecutive values fulfilling condition in a numpy array


code:
from Tkinter import *

class Window():

    def __init__(self, master):

        self.master = master
        self.v1 = IntVar()

        Label(master, text="""Which Method?""",justify = LEFT, padx = 20).pack()
        Radiobutton(master, text="Positive",padx = 20, variable=self.v1, value=1).pack(anchor=W)
        Radiobutton(master, text="Negative", padx = 20, variable=self.v1, value=2).pack(anchor=W)
        Radiobutton(master, text="Both", padx = 20, variable=self.v1, value=3).pack(anchor=W)

root = Tk()
w = Window(root)
w.master.mainloop()

print "The radiobutton outside of the class: %d" %w.v1.get()

rawDescription: Python Tkinter Radiobutton variable output


code:
import csv
import StringIO
line='name=zhg,code=#123,"text=hello,boy"'
string_file = StringIO.StringIO(line)
for row in csv.reader(string_file):
 print row

rawDescription: split comma-separated key-value pairs with commas

description: Split comma-separated key-value pairs with commas


code:
def regOutput():
    for index, row in enumerate(export):
        print row['SKU']
        print row['Qty']
        print ' '
        print ' '
        print 'A'
        print 'P'
        print row['productPriceSingle']

        if index % 5 == 4:
            print ' '
            print "P%u" % ((index+1) // 5)
            print 'M'
            print '%u CL' % (index+1)
        else:
            print 'CL'

        print row['Order Store ID']

        if index % 5 == 4:
            print ' '
            print 'A'

rawDescription: Python: Generating a Text from CSV with every 5th row Having a Different Function


code:
def on_mouse(event, x, y, flags, params):
    if event == cv.CV_EVENT_LBUTTONDOWN:
        print 'Start Mouse Position: '+str(x)+', '+str(y)
        sbox = [x, y]
        boxes.append(sbox)
    elif event == cv.CV_EVENT_LBUTTONUP:
        print 'End Mouse Position: '+str(x)+', '+str(y)
        ebox = [x, y]
        boxes.append(ebox)

count = 0
while(1):
    count += 1
    _,img = cap.read()
    img = cv2.blur(img, (3,3))

    cv2.namedWindow('real image')
    cv.SetMouseCallback('real image', on_mouse, 0)
    cv2.imshow('real image', img)

    if count < 50:
        if cv2.waitKey(33) == 27:
            cv2.destroyAllWindows()
            break
    elif count >= 50:
        if cv2.waitKey(0) == 27:
            cv2.destroyAllWindows()
            break
        count = 0

rawDescription: Python & CV2: How do i draw a line on an image with mouse then return line coordinates?


code:
myStr = 'AABBCCDDEEFF112233'
print ':'.join(myStr[i:i+2] for i in range(0, len(myStr), 2))

rawDescription: Python: Insert string (colon) in between HEX


code:
list1 = [("fruits","apple",1),("fruits","banana",1),("legumes","bean",3),("meats","deadbeef",1),("meats","pork",2)]
list2 = [("fruits","apple",2),("fruits","mango",4),("legumes","pea",3),("plants","rose",1)]
group1 = set([i[0] for i in list1])
group2 = set([i[0] for i in list2])
newItems = group2 - group1
print "New group:"+str(newItems)   #you can write logic for >>> with item "rose", count 2

dict1 = {}
for i in list1:
    dict1[i[1]] = i[2]
dict2 = {}
for i in list2:
    dict2[i[1]] = i[2]

totalkeys = set(dict1.keys() + dict2.keys())
for item in totalkeys:
    if dict1.has_key(item):
        if dict2.has_key(item):
            if dict1[item] ==dict2[item]:
                pass
            else:
                #your logic for change val
                pass
        else:
            pass #your logic for new item in list 2
    else:
        pass #your logic for deleted item

rawDescription: Deeply comparing two lists of tuples (Python)


code:
import os, glob
data_path='.'
os.chdir(data_path)
for fname in glob.glob('slope_*.xlsx'):
    print fname

rawDescription: search particular pattern file using os.walk or any other module

description: Search particular pattern file using os.walk or any other module


code:
from igraph import Graph
d = {'a': ['b', 'c', 'd'], 'b': ['c', 'd'], 'c': ['d']}
g = Graph.TupleList([(k, v) for k, vs in d.iteritems() for v in vs])
print g

rawDescription: Populating a graph from a dictionary using igraph


code:
import difflib
given = '\xd0\x9d\xd0\xb0 \xd1\x80\xd0\xb0\xd0\xb1\xd0\xbe\xd1\x82\xd0\xd0\xb5 \xd0\xbf\xd0\xbb\xd0\xb0\xd1\x82\xd1\x8f\xd1\x82 \xd0\xb1\xd0\xb0\xd0\xb1\xd0\xbb\xd0\xbe, \xd0\xbd\xd0\xd0\xbe \xd1\x80\xd0\xb0\xd0\xb1\xd0\xbe\xd1\x82\xd0\xb0\xd1\x82\xd1\x8c \xd0\xbd\xd0\xb0\xd0\xb4\xd0\xbe \xd0\xbd\xd0\xb0 \xd0\xbd\xd0\xb5\xd0\xb9'
expected = 'На работе платят бабло, но работать надо на ней' # requires UTF-8 terminal
for opcode in difflib.SequenceMatcher(a=expected, b=given).get_opcodes():
    print "%6s a[%d:%d] b[%d:%d]" % opcode
    if opcode[0] == 'insert': print 'Inserted:', repr(given[opcode[3]:opcode[4]])

expected[14:17]
given[14:18]
expected[44:47]
given[45:49]
given = '\xd0\xaf \xd0\xbd\xd0\xb5 \xd0\xbf\xd1\x80\xd0\xbe\xd1\x82\xd0\xb8\xd0\xb2 \xd0\xbf\xd0\xb5\xd1\x80\xd0\xb2\xd0\xbe\xd0\xb3\xd0\xbe \xd0, \xd0\xbd\xd0\xbe \xd0\xb1\xd0\xb5\xd0\xb7 \xd0\xb2\xd1\x82\xd0\xbe\xd1\x80\xd0\xbe\xd0\xb3\xd0\xbe \xd0\xbc\xd0\xbd\xd0\xb5 \xd0\xb2\xd0\xb5\xd1\x81\xd0\xb5\xd0\xbb\xd0\xb5\xd0\xb9'
expected = 'Я не против первого, но без второго мне веселей' # requires UTF-8 terminal
for opcode in difflib.SequenceMatcher(a=expected, b=given).get_opcodes():
    print "%6s a[%d:%d] b[%d:%d]" % opcode
    if opcode[0] == 'insert': print 'Inserted:', repr(given[opcode[3]:opcode[4]])

expected[34:38]
given[34:40]

rawDescription: UnicodeDecodeError when dumping to json input from console


code:
def get_data():
    df = sqlio.read_sql(qry, conn)
    value = df['count'][0]
    print value
    return value

@app.route('/', methods=['GET'])
def index():
    value = get_data()
    print "index", value
    return render_template('index.html', session_value=value)

@app.route('/get_data', methods=['GET'])
def get_data_route():
    value = get_data()
    # ... display your data somehow (HTML, JSON, etc.) ...

rawDescription: update values with function call to Flask

description: Update values with function call to Flask


code:
import pandas as pd
df1 = pd.DataFrame(profile_list)
df2 = pd.DataFrame(marks_list)
result = pd.merge(df1, df2, on=['name'])
print result
print result.values.tolist()
result = result.set_index(['name'])
name = raw_input("Enter student name: ")
sub = raw_input("Enter subject name: ")
print result[sub][name]

rawDescription: finding common elements in 2 list of dicts in python

description: Finding common elements in 2 list of dicts in python


code:
framerate = 24

def timecode_to_frames(timecode):
    return sum(f * int(t) for f,t in zip((3600*framerate, 60*framerate, framerate, 1), timecode.split(':')))

print timecode_to_frames('15:41:08:02') - timecode_to_frames('15:41:07:00')
# returns 26

def frames_to_timecode(frames):
    return '{0:02d}:{1:02d}:{2:02d}:{3:02d}'.format(frames / (3600*framerate),
                                                    frames / (60*framerate) % 60,
                                                    frames / framerate % 60,
                                                    frames % framerate)

print frames_to_timecode(26)
# returns "00:00:01:02"

rawDescription: How to do timecode calculation?

description: Do timecode calculation


code:
import urllib
import urllib2
url = "http://www.coolwebsite.com"
post = urllib.urlencode({"users":"bob", "bobsgreat":"yes", "postcount":"14"})

request = urllib2.Request(url, post)
socket = urllib2.urlopen(request)

hdrs = socket.headers
source = socket.read()
socket.close()

print "---- Headers -----"
print hdrs
print "---- Source HTML -----"
print source
print "---- END -----"

value = 0
for line in source.splitlines():
    if not line.strip():  continue
    if line.startswith("value="):
        try:
            value = line.split("=")
        except IndexError:
            pass
    if value > 0:
        break

open("some.html", "w").write("value is: %d" % value)

rawDescription: Using a language such as Ruby or Python to interact with API's


code:
class ParseCreateFile:

    begin = re.compile(r"CREATE TABLE[ ']*([^ ']+)[ ']*[(][^/;]+(PRIMARY KEY.*)[^/;]+;", re.IGNORECASE)

    def __init__(self,fileName):
        self.fileName = fileName
        self.readFromFile()

    def readFromFile(self):
        try:
            file = open(self.fileName,'r')
            fileData = file.read()
            file.close()
            self.parse(fileData)
        except IOError:
            print 'Unable to read file %s' % self.fileName

    def parse(self,rawData):
        self.list = []
        found = self.begin.findall(rawData)
        for i in found:
            print i[0], i[1]

if __name__ == '__main__':
    print "only the main classes"
    file = ParseCreateFile('file2.sql')

rawDescription: How to print tablename and primary key from file.sql in python

description: Print tablename and primary key from file.sql in python


code:
from Bio.SeqIO.QualityIO import FastqGeneralIterator
import glob
import os

def process(in_filepath, out_filepath):
    maxN = 0
    countall = 0
    countincl = 0
    with open(in_filepath, "rU") as fin:
        with open(out_filepath, "w") as fout:
            for (title, sequence, quality) in FastqGeneralIterator(fin):
                countN = sequence.count("N", 0, len(sequence))
                countall += 1
                if countN == maxN:
                    fout.write("@%s\n%s\n+\n%s\n" % (title, sequence, quality))
                    countincl += 1
    print os.path.split(in_filepath)[1], countall, countincl

folder = "/path/to/folder"  # folder to process
for in_filepath in glob.glob(os.path.join(folder, "*.fastq")):
    root, ext = os.path.splitext(in_filepath)
    if not root.endswith("_filt"):  # avoid processing existing output files
        out_filepath = root + "_filt" + ext
        process(in_filepath, out_filepath)

rawDescription: How to run a code for multiple fastq files?

description: Run a code for multiple fastq files


code:
mystr = 'a\nb'
print mystr
print repr(mystr)
print repr(mystr)[1:-1] # This will get rid of the apostrophes added by repr

rawDescription: Is it possible to print linebreaks in a string in Python


code:
binary_name = "httpd-2.2.31.tar.gz"
print binary_name.split("-")[1][:3].replace('.','')

rawDescription: Split file name on python

description: Split file name on python


code:
def remove(word):
    removeword = input("Please enter the word you would like to remove")
    remove_english_word(removeword)
    remove_spanish_word(removeword)

def remove_english_word(removeword):
    try:
        index = english_list.index(removeword)
        spanishword = spanish_list[index]
        english_list.remove(removeword)
        spanish_list.remove(spanishword)
        del (english_to_spanish[removeword])
        del (spanish_to_english[spanishword])
        print("The word '%s' and '%s' has been removed from the English and Spanish list, enter 'show' to see." %(removeword, spanishword))
    except ValueError, e:
        pass # In this case the word doesn't exist in english_list

def remove_spanish_word(removeword):
    try:
        index = spanish_list.index(removeword)
        englishword = english_list[index]
        spanish_list.remove(removeword)
        english_list.remove(englishword)
        del (spanish_to_english[removeword])
        del (english_to_spanish[englishword])

        print("The word '%s' and '%s' has bee removed from the Spanish and English list, enter 'show' to see." %(removeword, englishword))
    except ValueError, e:
        pass # In this case the word doesn't exist in spanish_list

rawDescription: Python - deleteing words from a list

description: Python - deleteing words from a list


code:
import json
# ... you read here from the file
data = '''{
  "Code" : "Success",
  "LastUpdated" : "2012-10-19T08:52:10Z"
}'''
result = json.loads(data)
print result['Code']

rawDescription: How to handle JSON with Python?

description: Handle JSON with Python


code:
from itertools import combinations, permutations

""" Process list to generate Unique strings matching given criteria """
def uniqLst(aList, finalList):

    """ Iterate through outer loop to compare each string until End of Loop """

    # Terminate if length is 0
    if(len(aList) == 0):
        return

    # Initialize local values
    found = False
    item1 = aList[0];

    # Go through list and compare with first item
    for item2 in aList[1:len(aList)]:

        """ Check if second string is reverse / palindrome or same """
        if item1[::-1] == item2 or item1 == item2:
            found = True
            print "Removing", item2 #Debug Data
            aList.remove(item2) # Remove second item matching criteria

    # If no item matches, add first item to final list
    if found != True:
        temp = aList.pop(0)
        finalList.append(temp)

        # Recursively call this function with updated aList
    uniqLst(aList, finalList)

    return

""" Main Function """
if __name__== "__main__":

    tmpLst1 = ["".join(x) for x in permutations('ABC', 3)]
    tmpLst2 = ["".join(x) for x in combinations('ABC', 3)]

    checkStrLst = tmpLst1 + tmpLst2

    finalList = []
    uniqLst(checkStrLst, finalList)
    print "========================"
    print "finalList", finalList

rawDescription: Python: Logical error in recursion under double for loop


code:
import pythoncom as pc, pyHook as ph
import time

def KeyboardHook(event):
    print chr(event.Ascii)
    return True

hm = ph.HookManager()
hm.KeyDown = KeyboardHook
hm.HookKeyboard()

while time.clock() < 5:
    pc.PumpWaitingMessages()

rawDescription: I want to stop pythoncom working

description: I want to stop pythoncom working


code:
with open('so.txt') as f:
    for line in f:
        time = float(line.split(', ')[1])
        print time

rawDescription: Python 2.7.6 - Reading a certain part of an array


code:
from collections import defaultdict

unique_clu = defaultdict(set)

with open(fileIn) as infh:
    for line in infh:
        clu, gen, spec, rest = line.split(None, 3)
        unique_clu[clu].add(spec)

for key in sorted(unique_clu):
    count = len(unique_clu[key])
    if count:
        print '{}:{}'.format(key, count)

rawDescription: Create dictionary and see if key always has same value


code:
regex = re.compile(r'>Settlement Date:</td>[^>]*>([^<]*)')
match = regex.search(data)
print match.group(1)

rawDescription: How to best extract the following content in html string in python?

description: Best extract the following content in html string in python


code: for x, y in itertools.izip(l, l[1:]): print x, y

rawDescription: Pythonic iteration over sliding window pairs in list?


code:
mystr = "Hello\nGoodbye"
print mystr
print repr(mystr)
# Remove apostrophes
print repr(mystr)[1:-1]

rawDescription: How can I literally print out an escape sequence in python?

description: Literally print out an escape sequence in python


code:
class Example(QtGui.QMainWindow):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):
        testAction = QtGui.QAction(QtGui.QIcon('test.png'), '&Exit', self)
        testAction.triggered.connect(self.runExample)
        menubar = self.menuBar()
        testMenu = menubar.addMenu('&Test')
        testMenu.addAction(testAction)

    def runExample(self):
        print "Running example."

rawDescription: User-defined slots for menu pyqt4

description: User-defined slots for menu pyqt4


code:
a = arange(12).reshape(3,4)

class fnc_class:
    def __init__(self, _array):
        # store the shape:
        self.shape = _array.shape
        self._array = _array
        # initialize the coordinates:
        self.coordinates = [0] * len(self.shape)

    def filter(self, buffer):
        result = self._array[tuple(self.coordinates)] == buffer.max()
        print self.coordinates
        # calculate the next coordinates:
        axes = range(len(self.shape))
        axes.reverse()
        for jj in axes:
            if self.coordinates[jj] < self.shape[jj] - 1:
                self.coordinates[jj] += 1
                break
            else:
                self.coordinates[jj] = 0
        return result

fnc = fnc_class(a)
generic_filter(a, fnc.filter, footprint = [[1, 0], [0, 1]])

rawDescription: Getting current element in scipy.ndimage.filters.generic_filter


code:
def gen():
    yield 1
    yield this_wont_be_evaluated

print any(gen())

rawDescription: Is there a builtin function version of `and` and/or `or` in Python?


code:
import cv2
import cv2.cv as cv

#Using cv2:
stream = cv2.VideoCapture(filename)
print stream.get(cv.CV_CAP_PROP_FRAME_COUNT)

#using cv:
stream = cv.CaptureFromFile(filename)
print cv.GetCaptureProperty(stream, cv.CV_CAP_PROP_FRAME_COUNT)

rawDescription: no access to GetCaptureProperty or any similar function in python opencv

description: No access to GetCaptureProperty or any similar function in python opencv


code:
from datetime import datetime, timedelta

def which_interval(date0, delta, date1, n_intervals):
    date0 = datetime.strptime(date0, '%Y-%m-%d')
    delta = timedelta(days = delta)
    date1 = datetime.strptime(date1, '%Y-%m-%d')
    delta1 = date1 - date0
    quadrile = int(((float(delta1.days) / delta.days) * n_intervals))
    return quadrile

# Example: figure out which quarter August 1st is in
interval = which_interval(
    '2016-01-01',
    366,
    '2016-08-01',
    4)
print '2016-08-01 is in interval %d, Q%d'%(interval, interval+1)

rawDescription: find the interval of a given date in python

description: Find the interval of a given date in python


code:
class C(object):
  def __new__(cls, val):
    if val == 5:
      return 'five'
    else:
      return super(C, cls).__new__(cls)

c1 = C(3)
print c1
c2 = C(5)
print c2

rawDescription: Python - question about factory functions


rawDescription: How to access a value from this JSON object returned from the google books API python?

description: Access a value from this JSON object returned from the google books API python


code:
import itertools

a = [1, 2, 3]

for element in itertools.cycle(a):
    print element

# -> 1 2 3 1 2 3 1 2 3 1 2 3 ...

rawDescription: How can I infinitely loop an iterator in Python, via a generator or other?

description: Infinitely loop an iterator in Python, via a generator or other


code:
a, b = 1, 1
total = 0
while a <= 4000000:
    if a % 2 == 0:
        total += a
    a, b = b, a+b  # the real formula for Fibonacci sequence
print total

rawDescription: Sum of even fibonacci numbers below 4 million - Python


code:
name = input("What did you put as the calculation name?: ")
saved_calcs = open("saved_calcs.txt", "r")
lines = saved_calcs.read()
split_lines = lines.split("\n")
index = split_lines.index(str(name))+1
for line in split_lines[index:index+8]:
    print line
saved_calcs.close()

rawDescription: Python Search a .txt file and when a match is found print a number of lines after the matching word


code:
import codecs

with codecs.open(my_file, "r", "cp1252") as f:
    for line in f:
        if u"µ" in line:
            print "found"

rawDescription: Search for Windows-1252 characters using Python


code:
a="CATCGTCCT"
b="CACCGACCG"
mismatch_seq=[]
for x in range (0, len(a)):
    if a[x] !=  b[x]:
        mismatch_seq.append(x)
print mismatch_seq

rawDescription: how to use loops to compare sequences?

description: Use loops to compare sequences


code:
class competitor(object):
    def __init__(self, name, dob, number, events):
        self.name = name
        self.dob = dob
        self.number = number ##this needs to check db for existing
        self.events = events

    def listEvents(self):
        for event in self.events:
            event.show()

class event(object):
    def __init__(self, name, distance, date, time):
        self.name = name
        self.distance = distance #meters
        self.date = date # [dd,mm,yyyy]
        self.time = time # [d,h,m,s]

    def printDate(self):
        date = str(self.date[0]) + "/" + str(self.date[1]) + "/" + str(self.date[2])
        return date

    def printTime(self):
        if (self.time[0] > 0):
            time = str(self.time[0]) + "." + str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
        else:
            time = str(self.time[1]) + ":" + str(self.time[2]) + "." + str(self.time[3])
        return time

    def getKmPace(self):
        time_s = self.time[0]*3600*24 + self.time[1]*3600 + self.time[2]*60 + self.time[3]
        time_m = time_s/60.0
        pace = time_m/(self.distance/1000.0)
        return pace

    def show(self):
        print "Event: ", self.name, " Date: ", self.printDate()
        print "Distance: ",self.distance/1000.0,"KM, Time: ", self.printTime()
        print "Pace per 1 KM: ", self.getKmPace(), " minutes."

kdl = event("20KM",20000,[26,4,2014],[0,1,27,36])
kdl_bad = event("20KM",20000,[26,4,2013],[0,2,35,37])

print 'First event:'
kdl.show()

print 'Richard has two events:'
richard = competitor("Richard", 1993, 1, [kdl,kdl_bad])
richard.listEvents()

rawDescription: How do I inherit a class in python?

description: Inherit a class in python


code:
import re

pat = re.compile(r'<doc id="([^"]*)" name="([^"]*)">(.*?)</doc>', re.DOTALL)

with open('input.txt') as f: xml = f.read()

matches = pat.findall(xml)

print matches

rawDescription: Parsing multiple files in an XML-like format


code:
import socket
from receiver import receiver
import logging

TCP_IP = ''
TCP_PORT = 1180
BUFFER_SIZE = 1024

print 'Script started'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
print 'Trying to bind'
server.bind((TCP_IP, TCP_PORT))
print 'bound to', (TCP_IP, TCP_PORT)
server.listen(1)
print 'listening for connection'

while True:
    try:
        conn, addr = server.accept()
        print 'accepted incoming connection'
        print 'Connection address:', addr
        for cur in receiver():
            data = cur.tostring()
            #print "sending data:", cur
            print len(data)
            conn.sendall(data)  # echo
    except Exception:
        logging.exception("processing request")

rawDescription: Errno 98: Address already in use - Python Socket


code:
class odict(dict):
    def __init__(self, *args, **kw):
        super(odict,self).__init__(*args, **kw)
        self.itemlist = super(odict,self).keys()
    def __setitem__(self, key, value):
         # TODO: what should happen to the order if
         #       the key is already in the dict
        self.itemlist.append(key)
        super(odict,self).__setitem__(key, value)
    def __iter__(self):
        return iter(self.itemlist)
    def keys(self):
        return self.itemlist
    def values(self):
        return [self[key] for key in self]
    def itervalues(self):
        return (self[key] for key in self)

od = odict(a=1,b=2)
print od
od['d']=4
od['c']=3
print od   # look at the `__str__` and `__repr__` methods
print od.keys()
print od.values()

rawDescription: Python:Extend the 'dict' class

description: Python:Extend the 'dict' class


code:
import subprocess

process = subprocess.Popen('script_name')
print process.communicate()

rawDescription: Execute /bin script from other python script


code:
with open(file_name) as your_file:
    start = your_file.readline().count(',') # initial count
    print all(i.count(',') == start for i in your_file)

rawDescription: reading file to check for same number of delimiters

description: Reading file to check for same number of delimiters


code:
index = 0

def isWordInArray(listA, listB):
    global index
    if index < len(listA):
        if listA[index] in listB:
            item_to_remove = listA[index]
            listA.remove(item_to_remove)
            listB.remove(item_to_remove)
            print "Removed {}".format(item_to_remove)
        else:
            index += 1

        isWordInArray(listA, listB)

rawDescription: How to make recursive formula for algorithm

description: Make recursive formula for algorithm


code:
import re
from lxml import etree
import requests

URL = "http://timesofindia.indiatimes.com/entertainment/movie-reviews/hindi/Madras-Cafe-movie-review/movie-review/21975443.cms"

response = requests.get(URL)

parser = etree.HTMLParser()
root = etree.fromstring(response.text, parser=parser)
rating_text = root.find('.//div[@id="sshow"]/table/tr/td[2]/div[1]/script[1]').text  # prints fbcriticRating="4";
print re.search("\d+", rating_text).group(0)  # prints 4

rawDescription: Filter info, from link, in python?

description: Filter info, from link, in python


code:
formatter = HtmlFormatter(style=MyStyle)
formatter.noclasses = True
print highlight(content,PythonLexer(),formatter)

rawDescription: How can I customize the output from pygments?

description: Customize the output from pygments


code:
import urllib
import urllib2
import re

def Connect2Web():
  aResp = urllib2.urlopen("https://uniservices1.uobgroup.com/secure/online_rates/gold_and_silver_prices.jsp");
  web_pg = aResp.read();

  pattern = "SILVER PASSBOOK ACCOUNT	" + "(.*)	" * 4
  m = re.search(pattern, web_pg)
  if m:
    print "SILVER PASSBOOK ACCOUNT:"
    print "\tCurrency:", m.group(1)
    print "\tUnit:", m.group(2)
    print "\tBank Sells:", m.group(3)
    print "\tBank Buys:", m.group(4)
  else:
    print "Nothing found"

rawDescription: reading some content from a web page read in python

description: Reading some content from a web page read in python


code:
from Tkinter import *
import tkFileDialog

root = Tk()

top = Toplevel()
top.wm_attributes('-topmost', 1)
top.withdraw()
top.protocol('WM_DELETE_WINDOW', top.withdraw)

def do_dialog():
    oldFoc = top.focus_get()
    print tkFileDialog.askdirectory()
    if oldFoc: oldFoc.focus_set()

b0 = Button(top, text='choose dir', command=do_dialog)
b0.pack(padx=100, pady=100)

def popup():
    top.deiconify()
    b0.focus_set()

b1 = Button(root, text='popup', command=popup)
b1.pack(padx=100, pady=100)
root.mainloop()

rawDescription: askdirectory() changes focus to different window

description: Askdirectory() changes focus to different window


code:
from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.Firefox()
driver.get("http://epl.squawka.com/everton-vs-aston-villa/18-10-2014/english-barclays-  premier-league/matches")

#find the elements which bring up the shots svg image
inputElement = driver.find_element_by_id("mc-stat-shot")
inputElement.click()
inputElement = driver.find_element_by_id("team2-select")
inputElement.click()

pageSource = driver.page_source
soup = BeautifulSoup(pageSource)
for circle in soup.find_all('circle'):
    if circle['r'] == '6.5':
        x = circle['cx']
        y = circle['cy']
        print x, y
    else:
        continue

driver.quit()

rawDescription: Scrapy: obtaining data from squawka

description: Scrapy: obtaining data from squawka


code:
t = Team.objects.get(pk=168)
for x in t.teamplayer_set.all():
  if x.captain:
    print "%s (Captain)" % (x.player.name)
  else:
    print x.player.name

rawDescription: In Django, how do you retrieve data from extra fields on many-to-many relationships without an explicit query for it?


code:
import json
str1 = '{"event": {"event_type": "signature_request_sent","event_time": "1407227830","event_hash": "567b6a342e29ceb8ad3a9641ccd27968f04f2c744c7dd3af8a1922f4eaa5d337","event_metadata": {"related_signature_id": null,"reported_for_account_id": null,"reported_for_app_id": "290e6bb260fed5613b68066084fc77ff","event_message": null}},"account_guid": null,"client_id": "290e6bb260fed5613b68066084fc77ff","signature_request": {"title": "My First embedded signature request with a template","original_title": "My First embedded signature request with a template","subject": "My First embedded signature request with a template","message": "Awesome, right?","test_mode": true,"is_complete": false,"has_error": false,"custom_fields": [],"response_data": [],"signing_url": null,"signing_redirect_url": null,"final_copy_uri": "","signatures": [{"signature_id": "9c5d829af9e0566134dd28bd064a2fe8","has_pin": false,"signer_email_address": "napopasas@gmail.com","order": null,"status_code": "awaiting_signature","signed_at": null,"last_viewed_at": null,"last_reminded_at": null}],"cc_email_addresses": []}}'
js1 = json.loads(str1)
print type(js1)

rawDescription: Python: how to convert json unicode dictionry into python rdictionary


rawDescription: select one random number from each row of a 2D list in python3.4

description: Select one random number from each row of a 2D list in python3.4


code:
while stars < 50:
   print "That's no good, pick again"
   critic, aud, stars = getScore()

rawDescription: Trouble with while loop in python function


code:
#!/usr/bin/env python

import pcap
import struct

def parse_packet(data):
    """
    Parse Ethernet/IP/TCP packet.
    """
    # See the Ethernet, IP, and TCP standards for details.

    data = data[14:] # Strip Ethernet header

    header_length = 4 * (ord(data[0]) & 0x0f) # in bytes
    data = data[header_length:]  # Strip IP header

    dest_port = struct.unpack('!H', data[2:4])[0]
    if not dest_port == 80: # This is an outgoing package
        return

    header_length = 4 * ((ord(data[12]) & 0xf0) >> 4) # in bytes
    data = data[header_length:] # Strip TCP header

    return data

def parse_get(data):
    """
    Parse a HTTP GET request, returning the request URI.
    """
    if data is None or not data.startswith('GET'):
        return

    fields = data.split('\n')
    uri = fields[0].split()[1]

    for field in fields[1:]:
        if field.lower().startswith('host:'):
            return field[5:].strip() + uri

def packet_handler(length, data, timestamp):
    uri = parse_get(parse_packet(data))
    if not uri is None:
        print uri

# Set up pcap sniffer
INTERFACE = 'wlan0'
FILTER = 'tcp port 80'
p = pcap.pcapObject()
p.open_live(INTERFACE, 1600, 0, 100)
p.setfilter(FILTER, 0, 0)

try:
    while True:
        p.dispatch(1, packet_handler)
except KeyboardInterrupt:
    pass

rawDescription: Obtaining loaded images from a given URL via Python


code:
import re

line = 'imm48-1gb-sfp/imm48-1gb-sfp'
match = re.match(r'(.*?)/', line)
if match:
    card_type = match.group(1)
    print card_type

rawDescription: Python Regex Matching

description: Python Regex Matching


code:
a=['a','b','c','d','e','f','g']
en=enumerate(a)
en
for index, elem in en:
  print index, elem

for index, elem in en:
  print index, elem

rawDescription: Why do list and tuple change the output of enumerate?


code:
def initializePlugin(obj):
    try:
        import plugin_node_1
        plugin_node_1.initializePlugin(obj)

        import plugin_node_2
        plugin_node_2.initializePlugin(obj)

    except:
        print 'Failed to register plug-in'

    ms_menu.createMenu()
    ms_menu.buildMenu()

def uninitializePlugin(obj):
    try:
        import plugin_node_1
        plugin_node_1.uninitializePlugin(obj)

        import plugin_node_2
        plugin_node_2.uninitializePlugin(obj)

    except:
        print 'Failed to deregister plug-in'

    ms_menu.deleteMenu()

rawDescription: hiding maya python plugin files

description: Hiding maya python plugin files


code:
import requests

# Prepare the session that will store the cookies.
s = requests.Session()
# Get the cookies
s.get("http://www.hidrografico.pt/boias-ondografo.php")

table_url = "http://www.hidrografico.pt/components/com_products/scripts/server/data_getestactable.php"
# Prepare the parameters
payload = { "estid": "4",
        "param": "1"
        }
r = s.post(table_url, data=payload)
print r.text

rawDescription: Unable to access AJAX url with requests, BeautifulSoup


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class A(Base):
    __tablename__ = "a"

    id = Column(Integer, primary_key=True)
    data = Column(String(20))
    boolean = Column(Boolean)

# works
e = create_engine("sqlite://", echo=True)

# works
#e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)

# works
#e = create_engine("mysql://scott:tiger@localhost/test", echo=True)

Base.metadata.create_all(e)

s = Session(e)
s.add_all([
    A(data='a1', boolean=True),
    A(data='a2', boolean=False),
    A(data='a3', boolean=True),
])

# works
print s.query(A).filter(A.data > 'a1', A.boolean).all()

# works
print s.query(A).filter(A.boolean).all()

# if before 0.8, need to use and_() or table-bound column
# print s.query(A).filter(and_(A.data > 'a1', A.boolean)).all()
# print s.query(A).filter(A.__table__.c.boolean).all()

rawDescription: Boolean type column as a ClauseElement in SQLAlchemy


code:
import csv

suffixes = ['ity', 'esome', 'ece']

with open('input.csv') as words:
    suffix_count = 0
    reader = csv.reader(words, delimiter='\t')
    for word, _ in reader:
        if any(word.endswith(suffix) for suffix in suffixes):
            suffix_count += 1

print "Found {} suffix(es)".format(suffix_count)

rawDescription: How to count the suffixes in words contained in a csv file?

description: Count the suffixes in words contained in a csv file


code:
from collections import defaultdict

data = [('turtle', 'sock'), ('frog', 'hat'), ('turtle', 'shirt'), ('frog', 'boot')]

d = defaultdict(list)
for animal, clothing in data:
    d[animal].append(clothing)

print d['turtle']

rawDescription: Python array sorting

description: Python array sorting


code:
class User(object):
    def __init__(self, id=None, ip=None, os=None, input_dict=None):
        if input_dict:
            self.from_dict(input_dict)
        else:
            self.id = id
            self.ip = ip
            self.os = os

    def from_dict(self, input_dict):
        self.id = input_dict.get('id')
        self.ip = input_dict.get('ip')
        self.os = input_dict.get('os')

    def to_dict(self):
        return {'id': self.id, 'ip': self.ip, 'os': self.os}

    def __repr__(self):
        return "id: {0}, ip: {1}, os: {2}".format(self.id, self.ip, self.os)

user1 = User(1, "127.0.0.1", "windows")
dict1 = user1.to_dict()
dict1['id'] = 2
user2 = User(input_dict=dict1)

print user1
# id: 1, ip: 127.0.0.1, os: windows

print user2
# id: 2, ip: 127.0.0.1, os: windows

rawDescription: Writing an object's attributes to a sqlite3 database


code:
import re
import xlwt

f = open("C:\Results\VAMOS_RxQual_Build_Update_Fri_04-11.08-16.txt", "r")
searchlines = f.readlines()
searchstrings = ['TSC Set 2 Indicator']
timestampline = None
timestamp = None
f.close()
a = 0
tot = 0

my_xls=xlwt.Workbook(encoding="utf-8") # begin your whole mighty xls thing
my_sheet=my_xls.add_sheet("Results")   # add a sheet to it

row_num=0 # let it be current row number
my_sheet.write(row_num,0,"match.group()") # here go column headers,
my_sheet.write(row_num,1,"value[5]")      # change it to your needs
row_num+=1 # let's change to next row

while a<len(searchstrings):
    for i, line in enumerate(searchlines):
        for word in searchstrings:
            if word in line:
                timestampline = searchlines[i-33]
                for l in searchlines[i:i+1]: #print timestampline,l,
                #print
                    for i in line:
                        str = timestampline
                        match = re.search(r'\d{2}:\d{2}:\d{2}.\d{3}', str)
                        if match:
                            value = line.split()
                            print '\t',match.group(),'\t',value[5],
                            # here goes cell writing:
                            my_sheet.write(row_num,0,match.group())
                            my_sheet.write(row_num,1,value[5])
                            row_num+=1
                            # and that's it...
                            print
                            print
                            tot = tot+1
                            break

    print 'total count for', '"',searchstrings[a],'"', 'is', tot
    tot = 0
    a = a+1
    # don't forget to save your file!
    my_xls.save("results.xls")

rawDescription: Python, appending printed output to excel file


code:
from datetime import datetime
print datetime.strptime("21-Feb-12", "%d-%b-%y")
#datetime.datetime(2012, 2, 21, 0, 0)

rawDescription: Python datetime: 21-Feb-12 to %d-%m-%Y

description: Python datetime: 21-Feb-12 to %d-%m-%Y


code:
import numpy as np
from traits.api import Array, HasTraits, NO_COMPARE

class C(HasTraits):
    x = Array(comparison_mode=NO_COMPARE)

    def _x_changed(self):
        print 'Doing something'

c = C()
c.x = np.linspace(0, 1, 10)

# This does not trigger:
c.x[3] = 100
# This does:
c.x = c.x

rawDescription: Trigger an event in Python traits package when an Array element is changed


code:
def make_closure():
  x = 123
  l = locals()
  return lambda(varname): eval(varname, None, l)

closure = make_closure()
print closure("x") # 123

rawDescription: Auto-bind scope variables in a closure

description: Auto-bind scope variables in a closure


code:
def tell_me(self):
    print self.name
    print 'all I contain is'
    for item in self:
        print item

rawDescription: python: inherited from builtin type list, how to access elements within new class?

description: Python: inherited from builtin type list, how to access elements within new class


code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

nphi,nz=12,20
r=1 # radius of cylinder
phi = np.linspace(0,360, nphi)/180.0*np.pi
z= np.linspace(0,1.0,nz)
print z

facecolors=['r','g','b','y']
cols=[]
verts2 = []
for i  in range(len(phi)-1):
    cp0= r*np.cos(phi[i])
    cp1= r*np.cos(phi[i+1])
    sp0= r*np.sin(phi[i])
    sp1= r*np.sin(phi[i+1])

    for j in range(len(z)-1):
        z0=z[j]
        z1=z[j+1]
        verts=[]
        verts.append((cp0, sp0, z0))
        verts.append((cp1, sp1, z0))
        verts.append((cp1, sp1, z1))
        verts.append((cp0, sp0, z1))
        verts2.append(verts)
        value=np.random.rand()
        print value
        col=plt.cm.bwr(value)
        print col
        cols.append(col)

poly3= Poly3DCollection(verts2, facecolor=cols  )

poly3.set_alpha(0.8)
ax.add_collection3d(poly3)
ax.set_xlabel('X')
ax.set_xlim3d(-1, 1)
ax.set_ylabel('Y')
ax.set_ylim3d(-1, 1)
ax.set_zlabel('Z')
ax.set_zlim3d(0, 1)
plt.show()

rawDescription: Converting a 2D figure into a cylinder in Python


code:
import timeit

trials = 1000000

setup = """
foo = {'key0': 'val0', '_key1': 'val1', '_key2': 'val2', 'key3': 'val3', 'key4': 'val4'}
"""

run = """
for key in foo:
    if key.startswith('_'):
        continue
    bar = key
"""
t = timeit.Timer(run, setup)
print 'Using ''continue'': %f' % min(t.repeat(3, trials))

run = """
for key in foo:
    if not key.startswith('_'):
        bar = key
"""
t = timeit.Timer(run, setup)
print 'Using ''if not'': %f' % min(t.repeat(3, trials))

rawDescription: Is there a performance difference of breaking out using continue and using not in a loop in Python?


code:
import subprocess
import re

while True:
    # prevents lots of python error output
    try:
        s = raw_input('> ')
    except:
        break

    # check if you should exit
    if s.strip().lower() == 'exit':
        break

    # try to run command
    try:
        cmd = subprocess.Popen(re.split(r'\s+', s), stdout=subprocess.PIPE)
        cmd_out = cmd.stdout.read()

        # Process output
        print cmd_out

    except OSError:
        print 'Invalid command'

rawDescription: Basic Terminal emulation in python

description: Basic Terminal emulation in python


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

sort_idx = np.argsort(x)
y=y[sort_idx]
x=x[sort_idx]
minm=np.array([],dtype=int)
maxm=np.array([],dtype=int)
length = y.size
i=0

while i < length-1:
    if i < length - 1:
        while i < length-1 and y[i+1] >= y[i]:
            i+=1

        if i != 0 and i < length-1:
            maxm = np.append(maxm,i)

        i+=1

    if i < length - 1:
        while i < length-1 and y[i+1] <= y[i]:
            i+=1

        if i < length-1:
            minm = np.append(minm,i)
        i+=1

print minm,maxm

rawDescription: How to find all maximas if x and y values of the function are given as np.array

description: Find all maximas if x and y values of the function are given as np.array


code:
for i in soup.find('div',{'id':'content'}).findAll('a'):
    print i['href']

rawDescription: Extracting unordered list for a particular <div>: BeautifulSoup


code:
class recurdict(dict):
    '''
    Recursive Dictionary
    '''
    def __init__(self, idict=None, **kwargs):
        dict.__init__(self)
        if idict is not None:
            self.update(idict)
        if len(kwargs):
            self.update(kwargs)

    def __contains__(self, key):
        return (key in self.keys())

    def __getitem__(self, key):
        if self.__contains__(key):
            return self.___getitem__(self, key)
        else:
            raise KeyError(key)

    @staticmethod
    def ___getitem__(idict, key):
        if dict.__contains__(idict, key):
            return dict.__getitem__(idict, key)
        else:
            result = None
            for k, v in idict.iteritems():
                if hasattr(v, "keys"):
                    result = recurdict.___getitem__(v, key)
                    if(result):
                        return result
                    else:
                        continue

    def flatten(self):
        return self._flatten(self)

    @staticmethod
    def _flatten(idict, key_chain = []):
        found_keys = []
        for k, v in idict.iteritems():
            if hasattr(v, "keys"):
                found_keys.extend(recurdict._flatten(v, key_chain + [k]))
            else:
                found_keys.append((tuple(key_chain + [k]), v))
        return found_keys

    def has_key(self, key):
        return self.__contains__(key)

    def keys(self):
        return self._keys(self)

    @staticmethod
    def _keys(idict):
        found_keys = dict.keys(idict)
        for k, v in idict.iteritems():
            if hasattr(v, "keys"):
                found_keys.extend(recurdict._keys(v))
        return found_keys

    def update(self, other=None, **kwargs):
        if other is None:
            pass
        elif hasattr(other, "iteritems"):
            for k, v in other.iteritems():
                self.__setitem__(k,v)
        elif hasattr(other, "keys"):
            for k in other.keys():
                self.__setitem__(k,other.__getitem__(k))
        else:
            for k, v in other:
                self.__setitem__(k,v)
        if kwargs:
            self.update(kwargs)

example_dict = recurdict\
({
    "key1" : "value1",
    "key2" : "value2",
    "key3" :
    {
        "key3a": "value3a"
    },
    "key4" :
    {
        "key4a":
        {
            "key4aa": "value4aa",
            "key4ab": "value4ab",
            "key4ac":
            {
                "key4ac1": [0,1,2],
                "key4ac2": (3,4,5),
                "key4ac3":
                [
                    {
                        "sub_key_x": 0,
                        "sub_key_y": 1,
                    },
                    6
                ]
            }
        },
        "key4b": "value4b"
    }
})

print example_dict.keys()
print "key1" in example_dict
print "key4ac1" in example_dict
print example_dict["key4ac1"]
for (k, v) in example_dict.flatten():
    print (k, v)

rawDescription: Create Parent Dictionary by key

description: Create Parent Dictionary by key


code:
import urllib, urllib2
from bs4 import BeautifulSoup, Comment
url='your url here'
content = urllib2.urlopen(url).read()
soup = BeautifulSoup(content, "html.parser")
print soup.find_all('#1 in Beauty')

rawDescription: find specific text in beautifulsoup

description: Find specific text in beautifulsoup


code:
for word in wordStr:
    print repr(word)
    word = word.strip()
    [etc]

rawDescription: Palindrome function not working in Python


rawDescription: How to sum items in a for loop in python?

description: Sum items in a for loop in python


code:
class C(object):
    def __init__(self,c):
         self.content = c
    def __nonzero__(self):
        return bool(self.content)

c1 = C(None)
c2 = C([])
c3 = C([1])
print 1 if c1 else 0 #result: 0
print 1 if c2 else 0 #result: 0
print 1 if c3 else 0 #result: 1

rawDescription: Override class methods to check for emptyness


code:
import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import random

data = np.random.random((50, 50))
df = pd.DataFrame(data)
row_colors = ["b" if random.random() > 0.2 else "r"  for i in range (0,50)]
cmap = sns.diverging_palette(133, 10, n=7, as_cmap=True)
result = sns.clustermap(df, row_colors=row_colors, col_cluster = False, cmap=cmap, linewidths = 0)
print dir(result)  # here is where you see that the ClusterGrid has several axes objects hiding in it
ax = result.ax_heatmap  # this is the important part
ax.plot([5, 30], [5, 5], 'k-', lw = 10)
plt.show()

rawDescription: Plot on top of seaborn clustermap

description: Plot on top of seaborn clustermap


code:
@unittest.skip("demonstrating skipping")
def testFoo():
    """Foo test case"""
    print 'this is foo test case'

rawDescription: How to disable individual tests temporarily using unittest module in python?

description: Disable individual tests temporarily using unittest module in python


code:
class CIPv6_Test(CGeneral_Test):
    def setUp(self):
        super(CIPv6_Test, self).setUp()
        print "this is the per-test sub setup method"
        do_sub_setup()

rawDescription: Using inheritance setUp() and tearDown() methods in nose tests


code:
import boto
s3 = boto.connect_s3()
bucket = s3.lookup('mybucketname')
total_bytes = 0
for key in bucket:
       total_bytes += key.size
print total_bytes

rawDescription: AWS S3 through boto - checking disk space


code:
f = open('words.txt')

for line in f.readlines():
    secret = line.rstrip('\n')
    if (secret[-1:] == "\n"):
        print "Error, new line character at the end of the string. This will not match!"
    elif (len(secret) >= 32):
        print "Error, string too long. Must be less than 32 characters."
    else:
    # create a cipher object using the secret
        cipher = AES.new(secret + (BLOCK_SIZE - len(secret) % BLOCK_SIZE) * PADDING)

    # decode the encoded string
        decoded = DecodeAES(cipher, encrypted)

        if (decoded.startswith('FLAG:')):
            print "\n"
            print "Success: "+secret+"\n"
            print decoded+"\n"
            break
        else:
            print 'Wrong password'
f.close()

rawDescription: How to read txt file in python line by line and set each line to a variable

description: Read txt file in python line by line and set each line to a variable


code:
import cgi
import feedparser

d = feedparser.parse('http://rss.slashdot.org/Slashdot/slashdot')

# iterate through entries. If the type is not text/html, HTML clean it
for entry in d.entries:
    if entry.summary_detail.type != 'text/html':
        print cgi.escape(entry.summary)
else:
    print entry.summary

rawDescription: Force feedparser to sanitize on all content types


code:
True = False
True
del True
True

rawDescription: How do I undo True = False in python interactive mode?

description: Undo True = False in python interactive mode


code:
sales_list = sales1.split('\n')

# Below code searches all columns for a 10 digit number and outputs the
# results to a new csv file.

with open("c:\\test1111.csv", "r") as old, \
     open("c:\\results1111.csv", 'wb') as new:
    i = 0 # counter to add the proper sales figure
    for line in old:
        #Regex to match exactly 10 digits
        match = re.search('(?<!\d)\d{10}(?!\d)', line)
        if match:
            match1 = match.group()
            print match1
            new.writelines(str(sales_list[i])+ ',' + (match1) + '\n')
        else:
            nomatch = "No match"
            print nomatch
            new.writelines(str(sales_list[i])+ ',' + (nomatch) + '\n')
        i += 1

rawDescription: Inserting data into two columns of csv

description: Inserting data into two columns of csv


code: print ' '.join(re.match(regex, example_line).groups())

rawDescription: Python regular expressions: return matches in a line rather than list?


rawDescription: Preserve space when stripping HTML with Beautiful Soup


code:
class Restaurant:
    def __init__(self, name, location, phone, parent = None):
        self.staff = []
        self.tables = []
        self.menu = []
        self.name = name
        self.location = location
        self.phone = phone

        self.rest_dict = {"Staff": self.staff, "Tables": self.tables, "Menu": self.menu}

    def addStaff(self, staff_object):
        self.staff.append(staff_object)

    def addTables(self, table_object):
        self.tables.append(table_object)

    def addMenu(self, menu_object):
        self.menu.append(menu_object)
r = Restaurant("foo","bar",911) # instantiate
r.addStaff("Foo")
r.addStaff("Bar") # append using method
r.staff.append("Foo1") # you can also  access attributes directly
r.addMenu("Lunch")
r.addMenu("Dinner")
r.menu.append("Lunch")
r.addTables(1)
r.addTables(2)
r.tables.append(3)
print r.rest_dict

{'Menu': ['Lunch', 'Dinner', 'Lunch'], 'Tables': [1, 2, 3], 'Staff': ['Foo', 'Bar', 'Foo1']}

rawDescription: loop over list of selected object properties in python to create a tree view

description: Loop over list of selected object properties in python to create a tree view


code:
if k == 0x63 or k == 0x43:
    print 'capturing!'
    cv.SaveImage("test.jpg",frame)

rawDescription: Save frame from webcam to disk with opencv python bindings


code:
import subprocess
from threading import Thread
import time
import sys
import logging
import tempfile
import os

import game_playing_module

class Pusher(Thread):
    def __init__(self, source, dest, proc, name):
        Thread.__init__(self)
        self.source = source
        self.dest = dest
        self.name = name
        self.proc = proc

    def run(self):
        while (self.proc.poll()==None) and\
              (not self.source.closed) and (not self.source.closed):
            line = self.source.readline()
            logging.info('%s: %s' % (self.name, line[:-1]))
            self.dest.write(line)
            self.dest.flush()

def get_reader_writer():
    fd_read, fd_write = os.pipe()
    return os.fdopen(fd_read, 'r'), os.fdopen(fd_write, 'w')

def connect(exe):
    logging.basicConfig(level=logging.DEBUG,\
                        format='%(message)s',\
                        filename=LOG_FILE_NAME,
                        filemode='w')

    program_to_grader_reader, program_to_grader_writer =\
                              get_reader_writer()

    grader_to_program_reader, grader_to_program_writer =\
                              get_reader_writer()

    p1 = subprocess.Popen(exe, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

    old_stdin = sys.stdin
    old_stdout = sys.stdout

    sys.stdin = program_to_grader_reader
    sys.stdout = grader_to_program_writer

    push1 = Pusher(p1.stdout, program_to_grader_writer, p1, '1')
    push2 = Pusher(grader_to_program_reader, p1.stdin, p1, '2')

    push1.start()
    push2.start()

    game_playing_module.play()

    sys.stdin = old_stdin
    sys.stdout = old_stdout

    fil = file(LOG_FILE, 'r')
    data = fil.read()
    fil.close()
    return data

if __name__=='__main__':
    if len(sys.argv) != 2:
        print 'Usage: connect.py exe'
        print sys.argv
        exit()
    print sys.argv
    print connect(sys.argv[1])

rawDescription: python os.mkfifo() for Windows

description: Python os.mkfifo() for Windows


code:
h = ses.add_torrent(params)
s = h.status()
while (not h.is_seed()):
    print s.total_wanted   # prints total size wanted after meta data is obtained, before that 0 is printed.

rawDescription: How do you get the total size of a torrent in libtorrent?

description: Get the total size of a torrent in libtorrent


code:
def scrambler(string):
    sen = ''
    for c in string:
        if c.lower() == 'k':
            sen += 'm'
        elif c.lower() == 'o':
            sen += 'q'
        elif c.lower() == 'e':
            sen += 'g'
        else:
            sen += c
    return sen

print scrambler('koe')

rawDescription: Condition not being met

description: Condition not being met


code:
import numpy as np

class VolumeViewer(object):
    def __init__(self, raw_dicom_stack, nframes, ds):

        self.raw_dicom_stack = raw_dicom_stack
        self.nframes = nframes
        self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        self.frame = 0
        self.frames_delete = 0
        self.ds = ds
        self.reset_stack = raw_dicom_stack

        # Plot the first slice of the image
        self.fig, self.ax = plt.subplots()
        self.im = self.ax.imshow(np.array(raw_dicom_stack[0]), cmap = cm.gray)

    def update(self, value):
        self.frame = int(self.nframes * value)
        # Update the image data
        dat = np.array(self.raw_dicom_stack[self.frame])
        self.im.set_data(dat)
        self.im.set_clim([dat.min(), dat.max()])
        # Redraw the plot
        self.fig.canvas.draw()

    def add(self,event):
        if self.query_add == True:
            print "Image series has already summed"
        else:
            for x in range(self.nframes):
                self.summed_image += self.raw_dicom_stack[x]
            del self.raw_dicom_stack[:]
            self.raw_dicom_stack.append(self.summed_image[:,:])
            self.nframes = len(self.raw_dicom_stack)

            # Update the image data
            dat = np.array(self.raw_dicom_stack[0])
            self.im.set_data(dat)
            self.slider.reset()
            self.im.set_clim([dat.min(), dat.max()])
            # Redraw the plot
            self.fig.canvas.draw()

            self.query_add = True
            #rezero summed_image
            #summed_image = np.zeros((self.summed_image.shape[0],self.summed_image.shape[1]))

    def show(self):
        plt.show()

    def reset(self, event):

        #self.summed_image = np.zeros((self.raw_dicom_stack[0].shape[0],self.raw_dicom_stack[0].shape[1]))
        #self.query_register = False
        #self.query_add = False
        self.__init__(self.reset_stack, self.nframes, self.ds)

    def quit(self,event):
        quit()

if __name__ == '__main__':
    viewer = VolumeViewer(np.zeros((2,10,10)), 4, None)
    viewer.reset()

rawDescription: Reinitializing python matplotlib module - reset button


code:
def compPlayHand(hand, wordList, n):
    #  Keep track of the total score
    totalScore = 0
    # As long as there are still usable letters left in the hand:
    while compChooseWord(hand,wordList,n) is not None:

        # Display the hand

        print "Current Hand: ",
        displayHand(hand)

        word = compChooseWord(hand,wordList,n)  # comp chooses word
        hand = updateHand(hand,word)
        # Tell the user how many points the word earned, and the updated total score, in one line followed by a blank line
        getWordScore(word,n)
        totalScore += getWordScore(word,n)
        # Update the hand
        c = calculateHandlen(hand)

        print   '"'+str(word)+'"' + " earned " + str(getWordScore(word,n)) +' points.' " Total:  " + str(totalScore) + " points."     # Otherwise (the word is valid):
        print

        if compChooseWord(hand,wordList,n) is None:  # End the game (break out of the loop)

            print  "Current Hand: ",
            displayHand(hand)

            print "Total score: " + str(totalScore) + " points."

rawDescription: getting rid of 'NONE' before a print statement in python

description: Getting rid of 'NONE' before a print statement in python


code:
test_string = "test.case#tester"
look_for = [".", "#", "(", ")"]

for index, single_char in enumerate(test_string):
  if single_char in look_for:
    print index
    break

rawDescription: How do I find the first dot, hash, or parens in a String?

description: Find the first dot, hash, or parens in a String


code:
def my_func():
   x = int(raw_input('Enter an integer:   '))

   ## start guessing with 0
   ans = 0

   while ans*ans*ans < abs(x):
       ans = ans + 1
       print 'current guess =', ans

   print 'last guess = ', ans
   print 'ans*ans*ans = ', ans*ans*ans

   ##if its a perfect cube

   if ans*ans*ans == abs(x):
   ## perfect, but negative
       if x<0:
             ans = -ans
       print 'Cube root of ' + str(x)+ ' is ' + str(ans)

   ## If its not a cube at all
   else:
       print x, 'is not a perfect cube'

   ## Now to start a new calculation
   again = raw_input('Find another perfect cube? (Y/N)')

   if again == "N":
       quit
   if again == "Y":
       my_func()

if __name__ == '__main__':
    my_func()

rawDescription: How to optionally repeat a program in python

description: Optionally repeat a program in python


code:
dicts = [{'uid':'12345', 'markets':['UK', 'US']},
         {'uid':'55644', 'markets':['IN', 'SG', 'IE']}]

def flatten(lists):
    res = []
    for d in lists:
        for market in d['markets']:
            res.append({'uid': d['uid'], 'markets': [market]})

    return res

print flatten(dicts)

# [{'markets': ['UK'], 'uid': '12345'}, {'markets': ['US'], 'uid': '12345'},
#  {'markets': ['IN'], 'uid': '55644'}, {'markets': ['SG'], 'uid': '55644'},
#  {'markets': ['IE'], 'uid': '55644'}]

rawDescription: Duplicate the dictionary for each in key value list


code:
import Queue, thread, subprocess

results= Queue.Queue()
def process_waiter(popen, description, que):
    try: popen.wait()
    finally: que.put( (description, popen.returncode) )
process_count= 0

proc1= subprocess.Popen( ['python', 'mytest.py'] )
thread.start_new_thread(process_waiter,
    (proc1, "1 finished", results))
process_count+= 1

proc2= subprocess.Popen( ['python', 'mytest.py'] )
thread.start_new_thread(process_waiter,
    (proc2, "2 finished", results))
process_count+= 1

# etc

while process_count > 0:
    description, rc= results.get()
    print "job", description, "ended with rc =", rc
    process_count-= 1

rawDescription: Python on Windows - how to wait for multiple child processes?


rawDescription: Python, combinations, permutations without repeat


code:
import random
from timeit import timeit
from string import ascii_letters
generate_text = lambda len: ''.join([random.choice(ascii_letters) for _ in xrange(len)])
for i in range(8):
    length = 10 + 10 ** i
    testword = generate_text(length)
    timing = timeit('hash(t)', 'from __main__ import testword as t')
    print 'Length: {}, timing: {}'.format(length, timing)

rawDescription: efficiency of long (str) keys in python dictionary

description: Efficiency of long (str) keys in python dictionary


rawDescription: Iterate through a dictionary and return results in a table


code:
def F1 (A):
   return A+1

def F2 (B):
   C=B+1
   print C

# Then use it as parameter
F2(F1(1))

rawDescription: How can a function use variables generated by other (functions) as parameters for itself in python?


code:
from lxml import html

html_snippet = '
This class has some text and a few links in it.
'

xpath_query = "//p[@class='something']"

tree = html.fromstring(html_snippet)
query_results = tree.xpath(xpath_query)
for item in query_results:
    print "'{0}'".format(item.text_content())

rawDescription: How to select text without the HTML markup

description: Select text without the HTML markup


code:
import mechanize

browser = mechanize.Browser()
browser.set_handle_robots(False)
browser.open("https://www.linkedin.com/")
browser.select_form(name="login")

browser["session_key"] = "your_email"
browser["session_password"] = "your_password"
response = browser.submit()

print response.read()

rawDescription: LinkedIn automatic login using Python

description: LinkedIn automatic login using Python


code:
for f in files[:]:    # Note the [:] after "files"
    tLog = os.path.getmtime(f)
    print "checking ", f, datetime.datetime.fromtimestamp(tLog)
    if difftime > tLog:
        print "difftime is bigger than tLog", "removing ", f
        files.remove(f)

rawDescription: python filter files by modified time

description: Python filter files by modified time


code:
def GetSum(n, arr):
    if len(arr) == 0 and n != 0:
        return False
    return (n == 0 or
      GetSum(n, arr[1:]) or
      GetSum(n-arr[0], arr[1:]))

def ArrayAddition(arr):
    arrs = sorted(arr)
    return GetSum(arrs[-1], arrs[:-1])

print ArrayAddition([2,95,96,97,98,99,100])

rawDescription: looping through loops in python?

description: Looping through loops in python


code:
def funcdefn_visitor(self, node, parent, userdata):

    # You might want to change the test here
    if node.location.file.endswith("/stdio.h"):
        print "Skipping 'stdio.h'"
        # Continue with next sibling
        return 1

    if node.kind == clang.cindex.CursorKind.FUNCTION_DECL: #gives function definitions
        self.func_defn.append(clang.cindex.Cursor_displayname(node))
        self.func_defn_line_no.append(node.location.line)
        self.func_defn_col_no.append(node.location.column)

    print 'Found %s [line=%s, col=%s]' % (
        clang.cindex.Cursor_displayname(node),
        node.location.line,
        node.location.column)

    # Continue visiting recursively
    return 2

index = clang.cindex.Index.create()

tu = index.parse(self.filename)
#-- link cursor visitor to call back to give function definitions
clang.cindex.Cursor_visit(
    tu.cursor,
    clang.cindex.Cursor_visit_callback(self.funcdefn_visitor),
    None)

rawDescription: In libclang how to exclude functions from stdio.h


rawDescription: Python Parsing Network

description: Python Parsing Network