code: print " ".join(make_hint(word, 0.3) for word in answer.split())

rawDescription: string replacement of fixed positions in python

description: String replacement of fixed positions in python


code:
import re
RE_kv = re.compile("\[%(.*)%\].*?\n?\s*{\s*(.*)")
matches = re.findall(RE_kv, test, re.M)
for k, v in matches:
    print k, v

rawDescription: Parsing template schema with Python and Regular Expressions


code:
score = sum(d[c] for c in word)
print score
21

rawDescription: Assigning values to letters and summing a sequence (not based on alphabetic order)


code:
for name, est in estimators.items():
    print est
    fig = plt.figure(fignum, figsize=(4, 3))
    plt.clf()
    ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)

    plt.cla()
    est.fit(X)
    labels = est.labels_
    print labels

rawDescription: At what part does the iris data receive a labeled cluster?


code:
var = open("test.jel")
var = var.read()
print var.replace('\n', '\\n\n')

rawDescription: Sensing new line?

description: Sensing new line


code:
from itertools import count, takewhile

def powers_of_two():
    for i in count():
        yield 2 ** i

for i in takewhile(lambda x: x < 20, powers_of_two()):
    print i

rawDescription: applying for loop such that counters are multiplied rather than being added in python

description: Applying for loop such that counters are multiplied rather than being added in python


code:
br.select_form(nr=0)
for field_name in br.form.controls:
    print field_name

rawDescription: Entering into form with Mechanize

description: Entering into form with Mechanize


code:
import numpy as np
from timeit import Timer

if __name__ == '__main__':

    setupstr="""
import numpy as np
N = 10000
"""

    method1="""
a = np.empty((N,),int)
a[::2] = 1
a[1::2] = -1
"""

    method2="""
a = np.tile([1,-1],N)
"""

    method3="""
a = np.array([1,-1]*N)
"""

    method4="""
a = np.array(list(itertools.islice(itertools.cycle((1,-1)), N)))
"""
    nl = 1000
    t1 = Timer(method1, setupstr).timeit(nl)
    t2 = Timer(method2, setupstr).timeit(nl)
    t3 = Timer(method3, setupstr).timeit(nl)
    t4 = Timer(method4, setupstr).timeit(nl)

    print 'method1', t1
    print 'method2', t2
    print 'method3', t3
    print 'method4', t4

rawDescription: How can I get an array of alternating values in python?

description: Get an array of alternating values in python


code:
from Tkinter import *

def print_value(entry):
    print entry.get()

root = Tk()

height = 5
width = 1
for i in range(height): #Rows
    for j in range(width): #Columns
        entry = Entry(root, text="", width=100)
        entry.grid(row=i, column=j)
        b = Button(root, text="print value", command= lambda entry=entry: print_value(entry), width=10)
        b.grid(row=i, column=j+1)

mainloop()

rawDescription: Getting the row of a tkinter button in grid on click


code:
import asyncio

@asyncio.coroutine
def a(n):
    print("A: {}".format(n))
    if n > 1000: loop.stop(); return n
    else: asyncio.async(b(n+1))

@asyncio.coroutine
def b(n):
    print("B: {}".format(n))
    asyncio.async(a(n+1))

loop = asyncio.get_event_loop()
asyncio.async(a(0))
loop.run_forever()

rawDescription: Mutually recursive coroutines with asyncio


code:
my_system = [Atom('H', 0, 1, 2),
             Atom('N', 3, 4, 5),
             Atom('C', 6, 7, 8),
             Atom('H', 9, 10, 11),]

group = Selection(my_system)
print group
print group.by_symbol('H')
print group.by_zrange(1, 2)
print
group = Selection(my_system)
print group.by_symbol('H').by_zrange(1, 2)

rawDescription: Selecting objects

description: Selecting objects


code:
from jinja2 import Template
jdata = {'Name': 'Jan', 'Hobbies': ['Python', 'collecting principles', 'DATEX II']}
templstr = """
My name is {{ Name }} and my Hobbies are:

{% for hobby in Hobbies %}
- {{ hobby }}
{% endfor %}
"""
templ = Template(templstr)
print templ.render(jdata)

rawDescription: Python 2.7 reading template and returning new file with substitutions


code:
# Matrix multiply
C = numpy.array([[1,2],[3,4]])
print C.dot(C)

# Pairwise operation
print C*C

rawDescription: Python Doing Math with Lists, Converting Lists to Matrices


code:
long_string = """First
Second
Third"""
print long_string

rawDescription: Python equivalent for write in file like in shell


code:
import pyximport
pyximport.install()
import fastdist
import numpy as np

A = np.random.randn(100, 200)

D1 = np.sqrt(np.square(A[np.newaxis,:,:]-A[:,np.newaxis,:]).sum(2))
D2 = fastdist.dist(A)

print np.allclose(D1, D2)
# True

rawDescription: Speeding up distance matrix computation with Numpy and Cython


rawDescription: transforming data frame in ipython a little like transpose

description: Transforming data frame in ipython a little like transpose


code:
class Student(object):

    def __init__(self, name):
        self.name = name
        self.scores = []
        self.highestscore = 0

    def __repr__(self):
        return '<' + self.name + ', ' \
              + 'high=' + str(self.highestscore) + ', ' \
              + 'avg=' + str(self.getAverage()) + '>'

    def addScore(self, score):
        self.scores.append(score)
        self.highestscore = max(self.highestscore, score)

    def getAverage(self):
        l = len(self.scores)
        if l == 0:
            return 0
        else:
            return sum(self.scores) / l

import csv

results = open("C:\Projetos\Data\Python\Scores1.csv", "r+")
csv1 = csv.reader(results, delimiter=",")

data = {}
highscore = 0

for eachline in csv1:
    #print(eachline)
    name = eachline[0]

    student = data.get(name, None)
    if student == None:
        student = Student(name)
        data[name] = student

    score = int(eachline[1])
    student.addScore(score)

students = [data[key] for key in data]

sorted_alphabetically = sorted(students, key=lambda x:x.name)
sorted_by_highest_score = sorted(students, key=lambda x:x.highestscore, reverse=True)
sorted_by_avg_score = sorted(students, key=lambda x:x.getAverage(), reverse=True)

print 'sorted_alphabetically =', sorted_alphabetically
print 'sorted_by_highest_score =', sorted_by_highest_score
print 'sorted_by_avg_score =', sorted_by_avg_score

rawDescription: Reading a CSV file, calculating averages and printing said averages


code:
url="http://v.youku.com/v_show/id_XOTU2Nzc3NDYw.html"
page = urllib2.urlopen(url)
page.headers.get('content-type')
txt = page.read().decode('utf8')
print txt

rawDescription: Encoding issue when scraping chinese text into csv with python


code:
import json
x = {'planet' : {'has': {'plants': 'yes', 'animals': 'yes', 'cryptonite': 'no'}, 'name': 'Earth'}}

print json.dumps(x, indent=2)

rawDescription: Howto Format dict string outputs nicely

description: Howto Format dict string outputs nicely


code:
def get_pivots():
    data = pd.DataFrame.from_csv('tmpData.txt')
    data['swings'] = np.nan

    pivot = data.irow(0).open
    last_pivot_id = 0
    up_down = 0

    diff = .3

    for i in range(0, len(data)):
        row = data.irow(i)

        # We don't have a trend yet
        if up_down == 0:
            if row.low < pivot - diff:
                data.ix[i, 'swings'] = row.low - pivot
                pivot, last_pivot_id = row.low, i
                up_down = -1
            elif row.high > pivot + diff:
                data.ix[i, 'swings'] = row.high - pivot
                pivot, last_pivot_id = row.high, i
                up_down = 1

        # Current trend is up
        elif up_down == 1:
            # If got higher than last pivot, update the swing
            if row.high > pivot:
                # Remove the last pivot, as it wasn't a real one
                data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.high - data.ix[last_pivot_id, 'high'])
                data.ix[last_pivot_id, 'swings'] = np.nan
                pivot, last_pivot_id = row.high, i
            elif row.low < pivot - diff:
                data.ix[i, 'swings'] = row.low - pivot
                pivot, last_pivot_id = row.low, i
                # Change the trend indicator
                up_down = -1

        # Current trend is down
        elif up_down == -1:
             # If got lower than last pivot, update the swing
            if row.low < pivot:
                # Remove the last pivot, as it wasn't a real one
                data.ix[i, 'swings'] = data.ix[last_pivot_id, 'swings'] + (row.low - data.ix[last_pivot_id, 'low'])
                data.ix[last_pivot_id, 'swings'] = np.nan
                pivot, last_pivot_id = row.low, i
            elif row.high > pivot - diff:
                data.ix[i, 'swings'] = row.high - pivot
                pivot, last_pivot_id = row.high, i
                # Change the trend indicator
                up_down = 1

    print data

rawDescription: Identifying price swings/trends in pandas dataframe with stock quotes


code:
#Gets the number of test cases from the user
num_ops = int(raw_input("Enter number of test cases: "))

#Initilze the list that the test cases will be stored in
test_cases = list()

#Append the test cases to the test_cases list
for x in range(num_ops):
    test_cases.append(int(raw_input("Enter test case")))

#Preform the operation on each of the test cases
for n in test_cases:
    results = [str(n)]
    while n != 1: # this is the operation
        if n % 2 == 0:
            n = n//2
        else:
            n = n*3+1
        results.append(str(n))
    print ' '.join(results)

rawDescription: How to print all the result of each loop one time in python

description: Print all the result of each loop one time in python


code:
results = []
while len(results) < 200:
    results = driver.find_elements_by_css_selector("div.flightbox")

    print len(results)

    # scroll
    driver.execute_script("arguments[0].scrollIntoView();", results[0])
    driver.execute_script("window.document.body.scrollTop = 0;")
    driver.execute_script("window.document.body.scrollTop = document.body.scrollHeight;")
    driver.execute_script("arguments[0].scrollIntoView();", results[-1])

rawDescription: PhantomJS acts differently than Firefox webdriver


code:
with open(r'D:\example.txt') as f:
    for line in f:
        print line.rstrip('\n')

rawDescription: How to display or show notepad file using python

description: Display or show notepad file using python


code:
header   = ['LowerLevel','Term','J','UpperLevel','Term','J']
prefixes = ['lower', 'upper']

def prefixed(header):
    prefix = ''

    for h in header:
        for p in prefixes:
            if h.lower().startswith(p):
                prefix, h = h[:len(p)], h[len(p):]
        yield prefix + h

print list(prefixed(header))

rawDescription: in-place modification of strings within a list?

description: In-place modification of strings within a list


code:
from ndb import model, query

class X(model.Expando):
  @classmethod
  def query_for(cls, name, value):
    return cls.query(query.FilterNode(name, '=', value))

print X.query_for('blah', 42).fetch()

rawDescription: How to create a query for matching keys?

description: Create a query for matching keys


code:
def side_by_side(*objs, **kwds):
    from pandas.core.common import adjoin
    space = kwds.get('space', 4)
    reprs = [repr(obj).split('\n') for obj in objs]
    print adjoin(space, *reprs)

rawDescription: How to shift a pandas MultiIndex Series?

description: Shift a pandas MultiIndex Series


code:
import urllib
s = b'https\u00253A\u00255C\u00252F\u00255C\u00252Ffbcdn-video'
print urllib.unquote_plus(s.decode('unicode_escape'))

rawDescription: convert unicode of form "\uxxxxxx" to string or text

description: Convert unicode of form "\uxxxxxx" to string or text


code:
from watcher import watcher
import numpy as np
import urllib2
class X(object):
    def __init__(self, foo):
        self.foo = foo

class Y(object):
    def __init__(self, x):
        self.xoo = x

    def boom(self):
        self.xoo.foo = "xoo foo!"
def main():
    x = X(50)
    watcher(x, 'foo', log_file='log.txt', include =['C:/Users/j/PycharmProjects/hello'])
    x.foo = 500
    x.goo = 300
    y = Y(x)
    y.boom()
    arr = np.arange(0,100,0.1)
    arr = arr**2
    for i in xrange(3):
        print 'a'
        x.foo = i

    for i in xrange(1):
        i = i+1

main()

rawDescription: Watch for a variable change in python

description: Watch for a variable change in python


code:
tested_nodes=set()
def solve_board(dfs_obj, node):
    if goal(node):  # only 1 peg!
        print node
        return node

    for new_node in succ(node):
        if tuple(new_node) not in tested_nodes:
            tested_nodes.add(tuple(new_node))
            print new_node
            result = solve_board(new_node)
            if result:  # True if it's a goal, None otherwise
                return result

rawDescription: Depth-First search in Python

description: Depth-First search in Python


code:
import itertools

numbers = sorted(map(int, raw_input().split(",")))
for k, g in itertools.groupby(numbers, lambda x: x // 10):
    print k, list(g)

rawDescription: count numbers in the list are between 1 and 10 in python

description: Count numbers in the list are between 1 and 10 in python


code:
import csv

# make a dictionary to store the data
data = {}

# first, read it in
with open("curves.csv", "rb") as fp:

    # make a csv reader object
    reader = csv.reader(fp)

    # skip initial line
    next(reader)

    for row in reader:
        # for each triplet, store it in the dictionary
        for i in range(len(row)//3):
            CL, D, PD = row[3*i:3*i+3]
            data[D, CL] = PD

# see what we've got
print data

with open("newcurves.csv", "wb") as fp:
    # get the labels in order
    row_labels = sorted(set(k[0] for k in data), key=int)
    col_labels = sorted(set(k[1] for k in data))

    writer = csv.writer(fp)
    # write header
    writer.writerow([''] + col_labels)

    # write data rows
    for row_label in row_labels:
        # start with the label
        row = [row_label]

        # then extend a list of the data in order, using the empty string '' if
        # there's no such value
        row.extend([data.get((row_label, col_label), '') for col_label in col_labels])

        # dump it out
        writer.writerow(row)

rawDescription: Use python (or R) to cleanup tables missing data


code:
@sched.cron_schedule(second='*/5')
def some_decorated_task():
    date_now = datetime.datetime.now().date()
    for item in DBsession.query(user_groups_n_hosts).filter(user_groups_n_hosts.end_time < str(date_now)):
        print item.id, item.server_users.native_linux_user

rawDescription: Flask-SQLAlchemy using global DB instance


code:
import re
import urllib
from bs4 import BeautifulSoup

tmp = re.compile("1-(..)")
url ='http://www.brothersoft.com/windows/photo_image/font_tools/2.html'
pageUrl = urllib.urlopen(url)
soup = BeautifulSoup(pageUrl)
pages = soup.find("div", {"class":"freemenu coLeft Menubox"})
page = pages.text
print int(re.search(r'of ([\d]+) ', page).group(1)) / 20 + 1

rawDescription: How to iterate link inside unknown total page number?

description: Iterate link inside unknown total page number


code:
def splitAndFind(text, phrase):
    text2 = text.replace(phrase, "###").split("###")
    if len(text2) > 1:
        return ((text2[0].split()[-5:], text2[1].split()[:5]))
print splitAndFind(zen, 'Dutch')
#(['obvious', 'at', 'first', 'unless', "you're"],
# ['.', 'Now', 'is', 'better', 'than'])

rawDescription: Python - Optimal code to find preceding and following five words from a given point in a line


code:
def make_pi():
    q, r, t, k, m, x = 1, 0, 1, 1, 3, 3
    for j in range(1000):
        if 4 * q + r - t < m * t:
            yield m
            q, r, t, k, m, x = 10*q, 10*(r-m*t), t, k, (10*(3*q+r))//t - 10*m, x
        else:
            q, r, t, k, m, x = q*k, (2*q+r)*x, t*x, k+1, (q*(7*k+2)+r*x)//(t*x), x+2

digits = make_pi()
pi_list = []
my_array = []

for i in make_pi():
    my_array.append(str(i))

my_array = my_array[:1] + ['.'] + my_array[1:]
big_string = "".join(my_array)
print "here is a big string:\n %s" % big_string

rawDescription: 1000 digits of pi in python

description: 1000 digits of pi in python


code:
import numpy
dataset2D = [ [6, 4], [0, 0, 0, 1], [1, 0, 2, 0], [2, 2, 0, 1], [1, 1, 1, 0], [0, 0, 1, 1], [1, 0, 2, 1] ]
dataset2D_size = dataset2D[0]
dataset2D = numpy.array(dataset2D)
partition2D = [ ['A', '1', '2', '4'], ['B', '3', '5'], ['C', '6'] ]

for partition in partition2D:
    label = partition[0]

    row_indices = [int(i) for i in partition[1:]]

    # Take the specified rows
    rows = dataset2D[row_indices]

    # Iterate the columns (this is the power of Python!)
    for column in zip(*rows):
        # Now, column will contain one column of data from specified row indices
        print column, # Apply your formula here
    print

rawDescription: Iterate over two nested 2D lists where list2 has list1's row numbers


rawDescription: Equivalent of R function 'ave' in Python Pandas


code:
from collections import defaultdict
D = defaultdict(list)

for line in open('filename', 'r'):
    k = line.split()[-1]
    D[k].append(line)

print 'VALUE    NO TIMES REPEATED'
print '-----    -----------------'
for value, lines in D.items():
    print '%-6s           %d'  % (value, len(lines))

rawDescription: count for number of repetition of values in a list and generate outfile

description: Count for number of repetition of values in a list and generate outfile


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup(html_source)
print list(soup.stripped_strings)

rawDescription: how to print already interpreted html data in python

description: Print already interpreted html data in python


code:
# Find all 'a' elements inside 'tr' table rows with css selector
for a in mySearchTree.cssselect('tr a'):
    print 'found "%s" link to href "%s"' % (a.text, a.get('href'))

rawDescription: Need python lxml syntax help for parsing html


code:
for name in player_names:
    print name

rawDescription: Selecting elements from Python array

description: Selecting elements from Python array


code:
for c in itertools.product([0, 1], repeat=4):
    print numpy.product(numpy.choose(c, [off, on]))

rawDescription: Python combinations of 2 lists

description: Python combinations of 2 lists


code:
import re

string = """
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP qlen 1000
    link/ether 00:e0:c8:10:00:00 brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.1/24 brd 192.168.100.255 scope global eth0
    inet 192.168.200.16/32 scope global eth0
    inet 192.168.200.17/32 scope global eth0
    inet 192.168.200.18/32 scope global eth0
3: gre0: <NOARP> mtu 1476 qdisc noop state DOWN
    link/gre 0.0.0.0 brd 0.0.0.0
4: 3g-wan1: <POINTOPOINT,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN qlen 3
    link/ppp
"""
print re.findall(r'\d+: (.*?):', string)

rawDescription: I want to be able to isolate a non-specific string from a list using python


code:
import argparse

class StoreOption(argparse.Action):
  def __init__(self, **kwargs):
    kwargs['action'] = kwargs.pop('subaction')
    container = kwargs.pop('container')

    action_class = container._pop_action_class(kwargs)
    if not callable(action_class):
      raise ValueError('unknown action "%s"' % (action_class,))

    kwargs['dest'] = 'thing'
    self._action = action_class(**kwargs)
    print "_action:", self._action

    super(StoreOption, self).__init__(
        option_strings= self._action.option_strings,
        dest= self._action.dest,
        nargs= self._action.nargs,
        const= self._action.const,
        default= self._action.default,
        type= self._action.type,
        choices= self._action.choices,
        required= self._action.required,
        help= self._action.help,
        metavar= self._action.metavar)

  def __call__(self, parser, namespace, values, option_string=None):
    print "got here:", option_string, namespace
    setattr(namespace, 'key', option_string)
    self._action(parser, namespace, values, option_string)

rawDescription: python argparse store --foo=bar as args.key='foo', args.value='bar'

description: Python argparse store --foo=bar as args.key='foo', args.value='bar'


code:
import itertools

alligned1 = 'I am going to go to some show'
alligned2 = 'I am not going to go the show'

results = []
word_pairs = zip(alligned1.split(), alligned2.split())
for k, v in itertools.groupby(word_pairs, key = lambda pair: pair[0] == pair[1]):
    if k:
        words = [pair[0] for pair in v]
        results.append(" ".join(words))

print results

rawDescription: Python matching words with same index in string


code:
df = pd.DataFrame( {'location' : ['A', 'A', 'A', 'B', 'B', 'B'],
                'dist_to_A' : [0, 0, 0, 50, 50, 50],
                'dist_to_B' : [50, 50, 50, 0, 0, 0],
                'location_var': [10, 10, 10, 14, 14, 14],
                'ind_var': [3, 8, 10, 1, 3, 4]})

df['ind'] = df.index

#The `location` and `location_var` corresponds to the choices,
#record them as dictionaries and drop them
#(Just realized you had a cleaner way, copied from yous).

ind_to_loc = dict(df['location'])
loc_dict = dict(df.groupby('location').agg(lambda x : int(np.mean(x)))['location_var'])
df.drop(['location_var', 'location'], axis = 1, inplace = True)
# now reshape
df_long = pd.wide_to_long(df, ['dist_to_'], i = 'ind', j = 'location')

# use the dictionaries to get variables `choice` and `location_var` back.

df_long['choice'] = df_long.index.map(lambda x: ind_to_loc[x[0]])
df_long['location_var'] = df_long.index.map(lambda x : loc_dict[x[1]])
print df_long.sort()

rawDescription: Complicated (for me) reshaping from wide to long in Pandas


code:
def my_func2(M, index, f=lambda x: x+1):
    copy = numpy.copy(M)
    copy[index] = f(copy[index])
    return copy

# prints the same result as before
print my_func2(M, (slice(None), 1))

rawDescription: What's the numpy/pythonic way to non-destructively replace a column x of a matrix with f(x)?


code:
class Stack:
    def __init__(self):
        self.data = []
    def push(self,n):
        self.data.append(n)
    def pop(self):
        return self.data.pop()
    def __iter__(self):
        return self
    def next(self):
        if len(self.data)>0:
            return self.pop()
        else:
            raise StopIteration

filo = Stack()
filo.push(1)
filo.push(2)
filo.push(3)

for i in filo:
    print i

rawDescription: Python Stack Iteration, best practices

description: Python Stack Iteration, best practices


code:
for i, item in enumerate(model_name.objects.all()):
    if item == some_item:
         break
else:
    # iterated through the whole queryset without finding the item
    i = None

if i is not None:
    print i
else:
    print "Not found"

rawDescription: From queryset to list, is it expensive operation?


code:
d = scipy.spatial.distance.cdist(A,B)

for q in range(len(B)):
    y = d[:,q]
    print y

rawDescription: finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python

description: Finding the distance between a set of points using scipy.spatial.distance.cdist(X, Y) in python


code:
import subprocess

proc = subprocess.Popen(['bash', 'gash.sh'], stdout=subprocess.PIPE)

output = proc.communicate()[0]

print "output:", output

rawDescription: How to set parent process' shell env from child process

description: Set parent process' shell env from child process


code:
def check_file(open_file):
    lines = 0
    open_file.seek(0)
    for line in open_file:
        if lines < 5:
            print line
            lines += 1
        else:
            break

rawDescription: How to used NamedTemporaryFile (and when is it closed?)

description: Used NamedTemporaryFile (and when is it closed?)


code:
for i in range(1, len(my_list)+1):
    print i

rawDescription: Python truncating numbers in integer array


code:
print mycustomer.orders # in case of bi-directional
print order.customer

rawDescription: Use a foreign key mapping to get data from the other table using Python and SQLAlchemy


code:
def my_callback():
    print 'Hello'
import functools
server = ThreadedTCPServer((HOST, PORT), functools.partial(ThreadedTCPRequestHandler, my_callback))

rawDescription: Proper way to add functionality to handle() in a python TCP Server?


code:
for line in open('Gin.txt'):    #Will be corrected later..
    if Q.lower() in line.lower():
        print line
        break
else:
    print "Found nothing. Did you spell it correct?"

rawDescription: Python Getting mixed results.

description: Python Getting mixed results.


code:
lst = [['\nAabhas\n'], ['\n\nThe sense, Feelings (1)\n\n'], ['\n5\n'], ['\nBoy\n'], ['\n\n']]

corrected_list = []

for i in lst:
    intm = []
    for j in i:
        intm.append(j.strip())
    corrected_list.append(intm)
print corrected_list

rawDescription: How to remove all \n's and make it as csv file of 5 columns?

description: Remove all \n's and make it as csv file of 5 columns


code:
import urllib
from bs4 import BeautifulSoup

url = 'http://www.brothersoft.com/windows/categories.html'
pageHtml = urllib.urlopen(url).read()
soup = BeautifulSoup(pageHtml)
sAll = [div.find('a') for div in soup.findAll('div', attrs={'class':'brRight'})]
for i in sAll:
    suburl = "http://www.brothersoft.com"+i['href']    #which is a url like 'midi_tools'

    content = urllib.urlopen(suburl).read()
    anosoup = BeautifulSoup(content)
    ablock = anosoup.find('table',{'id':'courseTab'})
    for atr in ablock.findAll('tr',{'class':'border_bot '}):
        print atr.find('dt').a.string      #name
        print "http://www.brothersoft.com" + atr.find('a',{'class':'tabDownload'})['href']   #link

rawDescription: How to get next page link in python beautifulsoup?

description: Get next page link in python beautifulsoup


code:
for row in soup.find_all('div',attrs={"class" : "reviewText"}):
    print row.text

rawDescription: Python: BeautifulSoup extract string between div tag by its class


code:
for s in ('(A|B|C)_STRING',
          '(|A_)STRING',
          'STRING_(A|B)_STRING_(C|D)'):
    print s, "->"
    for t in expander(s):
        print "   ", t

rawDescription: Python regex string expansion

description: Python regex string expansion


code:
import itertools
a = ['X', 'Y', '2']
output = []
for c in itertools.product(' -', repeat=len(a)-1):
   output.append( ('%s'.join(a) % c).replace(' ', '') )
print 'output=', output

rawDescription: Combine elements of a list with all possible separators


code:
class CustomDoubleDict(dict):
    def __getitem__(self, key):
        if key in self:
            return dict.__getitem__(self, key)
        else:
            closest_key = min(self.keys(), key=lambda c: (c[0] - key[0]) ** 2 + (c[1] - key[1]) ** 2)
            return dict.__getitem__(self, closest_key)

b = CustomDoubleDict()
b[90, 1] = 100
b[90, 55] = 101
b[90, 127] = 102
b[70, 1] = 40
b[70, 45] = 41
b[70, 107] = 42

print b[73, 40]  # prints 41
print b[70, 45]  # prints 41

rawDescription: Find integer nearest-neighbour in a dict


code:
from sys import float_info

cont = True
while(cont):
    initChoice = 0
    print " "
    print "What would you like to do?"
    print "1) Divide two numbers"
    print "2) Find the GCD of two numbers"
    print "3) Find the LCM of two numbers"
    print "0) Exit"
    initChoice = input("")
    if(initChoice == 1):
        #This hangs up when certain numbers are entered
        #It only seems to hang up when only 8 or 9 are involved
        #For example (8,9) (9,8) (88,89) (89,88) (98,99) (99,98)
        #It gets hung up on num1 = num1-num2
        num1 = raw_input("Please enter the first number: ")
        num2 = raw_input("Please enter the second number: ")
        num1 = float(num1)
        num2 = float(num2)
        numer = num1
        denom = num2
        ans = 0.0
        iter1 = 0.0
        iter2 = 0.0

        precision=float_info.min

        while(num1 > 0+precision):
            if(num1 >= num2):
                num1 = num1-num2
                iter1 += 1
            else:
                num2 = num2*(0.1)
                ans += iter1*(10**(-iter2))
                iter1 = 0
                iter2 += 1
        ans += iter1*(10**(-iter2))
        print numer,"divided by",denom,"is",ans

    elif(initChoice == 0):
        break

rawDescription: Python Program gets hung up on certain numbers


code:
import sys
import traceback

class TracePrints(object):
  def __init__(self):
    self.stdout = sys.stdout
  def write(self, s):
    self.stdout.write("Writing %r\n" % s)
    traceback.print_stack(file=self.stdout)

sys.stdout = TracePrints()

def a():
  print "I am here"

def b():
  a()

b()

rawDescription: Finding a print statement in Python

description: Finding a print statement in Python


code:
from subprocess import Popen, PIPE
import platform

if platform.system() == 'Windows':
    close_fds=False
else:
    close_fds=True

p = Popen('python long.py', stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=close_fds, shell=True)
print "Content-Type: text/plain\n"
print "invoked (%d)" % (p.pid)

rawDescription: How to spawn Python2.5 process through Apache and return immediately under Windows

description: Spawn Python2.5 process through Apache and return immediately under Windows


code:
for pair in combinations('abc', [1,2,3]):
  print pair

rawDescription: What would be a better implementation of all combinations in lexicographic order of a jagged list?


code:
from bs4 import BeautifulSoup
import requests

url = "http://web.mta.info/nyct/service/threelin.htm"
response = requests.get(url)

soup = BeautifulSoup(response.content)
table = soup.find('table', summary='Table of 3 Subway Line Stops')
stops = [tr('td')[2].text.strip().replace('\n', '').split(' /')
         for tr in table('tr', height=25)]
print stops

rawDescription: Scrape MTA Subway data?

description: Scrape MTA Subway data


code: print str(int(asplit[1])/60+int(asplit[0])) + '.' + str(int(asplit[1]) % 60)

rawDescription: Convert float into time (hour.minute) in python


code:
try:
  reader = csv.reader(open('bar.in', 'rb'), delimiter='|')
  result = dict((int(x[0]), eval(x[1])) for x in reader)
  print "testcsv.csv found, load it from file."
except:
  print "testcsv.csv not found. loop all over."

rawDescription: Read/Write a dictionary with values as integer lists in a csv file in python 2.6.x


code: print word

rawDescription: How to find a group of consecutive leters and check cases with python re

description: Find a group of consecutive leters and check cases with python re


code:
links = [
  {'uid': 1, 'lid': 6, 'path': 'a1.txt', 'shareid': 1},
  {'uid': 1, 'lid': 7, 'path': 'a2.txt', 'shareid': 2},
  {'uid': 1, 'lid': 8, 'path': 'a1.txt', 'shareid': 1}
]

op = dict()
for a in links:
  op.setdefault(a['path'], {}).update(shareid=a['shareid'])
  op[a['path']].setdefault('lid', []).append(a['lid'])
print op

rawDescription: Python list of dictionaries issue

description: Python list of dictionaries issue


code:
with open('second.py', 'w') as f: f.write('print "hello world"')

try:
    execfile('second.py')  # -> hello world
except Exception as e:
    # could also be "pass", "time.sleep()", etc
    print 'exception {} occurred'.format(e)

print 'continuing on...'

rawDescription: trying to run a python script from within another python script

description: Trying to run a python script from within another python script


code:
from bs4 import BeautifulSoup

html = """

ISP:	Brighthouse Networks

Services:	None Detected

City:	Miami

    Region:	Florida

Country:	United States

"""

soup = BeautifulSoup(html)
table = soup.findAll('table', {"style":"padding-top:10px;"})[0]

trs = table('tr')
for tr in trs:
    print tr.th.text,
    print tr.td.text

#and this for the 'Region'
print table("th")[3].text,
print table("td")[3].text

rawDescription: Python BeautifulSoup Error In Parsing HTML


code:
def find(chrdata, loc):
    # find - analogous to string find?
    fn = getattr(chrdata, 'find',None)
    if fn is None:
        #raise AttributeError(chrdata,'does not have find method')
        def fn(loc):
            i = int(loc)
            for gene in chrdata:
                if gene.find(i):
                    return gene  # gene.gene
            return None
    return fn(loc)

print 3, find(data['A'],3)

rawDescription: cython: reducing the size of a class, reduce memory use, improve speed

description: Cython: reducing the size of a class, reduce memory use, improve speed


code: print any.__doc__

rawDescription: Finding common elements from two lists of lists


code:
#!/usr/bin/env python

''' Appending data to a pickle file

    See http://stackoverflow.com/q/31365239/4014959

    Written by PM 2Ring 2015.07.12
'''

import cPickle as pickle
from random import choice, randint
from string import ascii_lowercase

#Create a random lowercase string
def rand_str(strlen=5):
    return ''.join([choice(ascii_lowercase) for _ in xrange(strlen)])

#Name of pickle file
fname = 'pickletest.pkl'

#Attempt to read the dicts from the pickle file
all_data = {}
count = 0
try:
    with open(fname, 'rb') as fh:
        while True:
            d = pickle.load(fh)
            count += 1
            print 'Record %2d: %s' % (count, d)
            all_data.update(d)
except EOFError:
    pass
except IOError as e:
    print '%s; the file will be created.' % e

print '\nCurrent data\n%s' % all_data

#Create a new record
d = dict((rand_str(), randint(10, 99)) for _ in xrange(5))
print '\nNew data\n%s' % d

#Append the record to the pickle file, using protocol 2
with open(fname, 'ab') as fh:
    pickle.dump(d, fh, 2)

rawDescription: How to update and save a python script (variables only, no logic) based on user input?

description: Update and save a python script (variables only, no logic) based on user input


code:
import timeit
x = "re.search('a+b', s)"
print timeit.timeit(x, "import re;s='a'*10000", number=10)
print timeit.timeit(x, "import re;s='b'*10000", number=10)

rawDescription: python regex speed

description: Python regex speed


rawDescription: Solving 5 Linear Equations in Python

description: Solving 5 Linear Equations in Python


rawDescription: value attribute for lxml.html

description: Value attribute for lxml.html


code:
from itertools import groupby

flat = ["Shoes", "pants", "shirt", "tie", "jacket", "hat"]

result=[]
for k, g in groupby(sorted(flat, key=len), key=len):
    result.append(list(g))

print result

rawDescription: Nest a flat list based on an arbitrary criterion


code:
def f(icss):
  kws = set(icss.split(','))
  N = 1
  while N in tab:
    stuff, others = tab[N]
    found = kws & set(others)
    if found:
      # maybe error if len(found) > 1 ?
      stuff = others[found.pop()]
    N, word_to_add = stuff
    if word_to_add is not None:
      print word_to_add

rawDescription: Is a graph library (eg NetworkX) the right solution for my Python problem?


code:
def add(a, b, print_it):
    c = a+b
    if print_it: print "Your answer is: ", str(c)    ## just print it
    else: return c    ## Put the value into "answer" below

answer = add(10, 15)
print answer
## Will print 25

add(10, 20, true)
## Will print """Your answer is 30"""

rawDescription: How to tell if a function will be returning a value to a variable?

description: Tell if a function will be returning a value to a variable


code:
import optparse

parser = optparse.OptionParser()
parser.add_option("-a", action="store", type="string", dest="a")

(opt, arg) = parser.parse_args()
print "Opt:", opt
print "Arg:", arg

rawDescription: How do I use optparse to just split the command-line arguments into options and positional args?

description: Use optparse to just split the command-line arguments into options and positional args


code:
if a[:n] == b[:n]:
    print 'strncmp success!'

rawDescription: strncmp in python

description: Strncmp in python


code:
import multiprocessing

def subf(x):
    print "in subf"

def f(x):
    print "in f"
    p = multiprocessing.Pool(2)
    p.map(subf, range(2))

if __name__ == "__main__":
    processes = []
    for i in range(2):
        proc = multiprocessing.Process(target=f, args=(i,))
        proc.start()
        processes.append(proc)

rawDescription: parallelize recursion python

description: Parallelize recursion python


code:
from re import findall

mystr = """
List: Name-Here
List: Name-Here, Here, Here'Here, Here-Here'Here, Here Here Here
List: Here, Here Here, Here Here Here Here
"""

items = findall("List:(.+)", mystr)
for item in items:
    print len(item.split(","))

rawDescription: Searching with regex for a number of names in a list?


code:
from bs4 import BeautifulSoup

text = ' TEXT I WANT  \
 DO NOT WANT  \
 I WANT THIS TOO  \
 DO NOT WANT  \
 DO NOT WANT '

soup = BeautifulSoup(text)
anchor_tags = soup.find_all('a')
for a in anchor_tags:
    for span in a.find_all('span', attrs={'itemprop': 'foo'}):
        print span.text

rawDescription: Pinpointing a ResultSet with find_all

description: Pinpointing a ResultSet with find_all


code: print s.decode('quoted-printable')

rawDescription: Replacing = with '\x' and then decoding in python


code:
parser = argparse.ArgumentParser()
parser.add_argument('-a',default=[None],action='store',type=int,nargs='*')
print parser.parse_args(''.split())
print parser.parse_args('-a'.split())
print parser.parse_args('-a 1 2 3'.split())

rawDescription: Python argparse: combine multiple-value argument with default and const


code:
import ast, inspect

# this is the test scenario
def function1():
    f1_var1 = 42
    def function2():
        f2_var1 = 42
        f2_var2 = 42
        def function3():
            f3_var1 = 42

# derive source code for top-level function
src = inspect.getsource(function1)

# derive abstract syntax tree rooted at top-level function
node = ast.parse(src)

# next, ast's walk method takes all the difficulty out of tree-traversal for us
for x in ast.walk(node):
    # functions have names whereas variables have ids,
    # nested-classes may all use different terminology
    # you'll have to look at the various node-types to
    # get this part exactly right
    name_or_id = getattr(x,'name', getattr(x,'id',None))
    if name_or_id:
        print name_or_id

rawDescription: python - list all inner functions of a function?

description: Python - list all inner functions of a function


rawDescription: Extracting float numbers from file using python


code:
words = ["hey","there","stack","overflow"]
print magic_function(words)

rawDescription: Given a list of words, make a subset of phrases with them


code:
lst = ["b", "c", 3] # Take care not to use "list" as a variable name by the way.
print lst[0]        # I used lst in my example

rawDescription: Confused about list variable assigment

description: Confused about list variable assigment


code:
print "The binomials are: (x+{})(x+{})".format(firstbinomial, secondbinomial)

# prints "The binomials are: (x+-3)(x+5)"

rawDescription: How to make the plus sign show for a variable in python

description: Make the plus sign show for a variable in python


code:
phrases = open("phrase.txt").readLines()
words = open("text.txt").read()

for phrase in phrases:
    all_words_found = True
    phrase_words = phrase.lower().split(" ")
    for word in phrase_words:
        if word not in words:
            all_words_found = False
            break

    if all_words_found:
        print phrase

rawDescription: Find phrases from one text file in another text file with python


code:
import sys

def do_something(val):
    # do something
    print val
    # return something
    return val

if __name__ == '__main__':
    try:
        arg = sys.argv[1]
    except IndexError:
        arg = None

    return_val = do_something(arg)

rawDescription: running a python script with values from views.py in Django

description: Running a python script with values from views.py in Django


code:
from django.template import Template, Context
t = Template("""
{% for staff in staff_members %}
     {% cycle '
' '' '' as div %}
     {{ staff }}
     {% cycle '' '' '
' as div %}
{% endfor %}
{% if not staff_members|length|divisibleby:3 %}

{% endif %}
""")
print t.render(Context({'staff_members': [1,2,3,4,5,6,7,8]}))

rawDescription: Django forloop index operations

description: Django forloop index operations


code:
from Tkinter import *
import threading
import Queue # This is thread safe
import time

class Std_redirector():
    def __init__(self, widget):
        self.widget = widget

    def write(self,string):
        self.widget.write(string)

class ThreadSafeText(Text):
    def __init__(self, master, **options):
        Text.__init__(self, master, **options)
        self.queue = Queue.Queue()
        self.update_me()

    def write(self, line):
        self.queue.put(line)

    def update_me(self):
        while not self.queue.empty():
            line = self.queue.get_nowait()
            self.insert(END, line)
            self.see(END)
            self.update_idletasks()
        self.after(10, self.update_me)

def func():
    i = 0
    while True:
        i += 1
        print i
        time.sleep(0.01)

root = Tk()
text = ThreadSafeText(root)
text.pack()
sys.stdout = Std_redirector(text)

thread1 = threading.Thread(target=func)
thread1.start()

root.mainloop()

rawDescription: Issue with Redirecting stdout to Tkinter text widget with threads


code:
l=['abc','abcdef','def','defdef','polopolo']
print [j for i, j in enumerate(l) if all(j not in k for k in l[i + 1:])]
# ['abcdef', 'defdef', 'polopolo']

rawDescription: python delete substrings from list of strings

description: Python delete substrings from list of strings


rawDescription: Python List Help (Find Biggest number)

description: Python List Help (Find Biggest number)


rawDescription: Regular expression to replace brackets that embrace whole entry


code:
def sel(self, action):
    print action.text()

rawDescription: getting radio button selection in PySide using Python 3.4

description: Getting radio button selection in PySide using Python 3.4


code:
import httplib
conn = httplib.HTTPConnection("ubuntu.icafebusiness.com")
conn.request("HEAD", "/images/ubuntugui2.jpg")
res = conn.getresponse()
print res.getheaders()

rawDescription: Django: check if an image exists at some particular url


code:
#!/usr/bin/env python
import os, sys
from distutils import sysconfig
from pkg_resources import Requirement
from setuptools.package_index import PackageIndex

index = PackageIndex()
root = sysconfig.get_python_lib()
path = os.path.join(root, 'easy-install.pth')
if not os.path.exists(path):
    sys.exit(1)
for line in open(path, 'rb'):
    if line.startswith('import sys'):
        continue
    path = os.path.join(root, line.strip(), 'EGG-INFO', 'PKG-INFO')
    if not os.path.exists(path):
        continue
    lines = [r.split(':', 1) for r in open(path, 'rb').readlines() if ':' in r]
    info = dict((k.strip(), v.strip()) for k, v in lines)
    print '%s %s updates..' % (info['Name'], info['Version'])
    spec = Requirement.parse(info['Name'] + '>' + info['Version'])
    index.find_packages(spec)
    versions = set([
        (d.parsed_version, d.version) for d in index[spec.key] if d in spec
        ])
    if versions:
        for _, version in sorted(versions):
            print '\t', version
    else:
        print '\tnone'

rawDescription: Python Easy_Install Update Report

description: Python Easy_Install Update Report


code:
def print_info(people):
    col_widths = [max(len(p[0]) for p in people) + 1, 10, 20]
    rows = [['Name', 'Job', 'Mood']] + [[p[0], p[1], p[2]] for p in people]
    rows = (map(str.ljust, row, col_widths) for row in rows)
    print '\n'.join(''.join(row) for row in rows)

def pad_string_right(string, length):
    if(len(string) > length):
        raise ValueError
    char_difference = length - len(string)
    return string + (' ' * char_difference)

print_info([("Barry", "plumber", "happy"),
            ("Brian", "butcher", "bored"),
            ("Betty", "singer", "hungry")])

rawDescription: Python: use str.ljust with map

description: Python: use str.ljust with map


code:
for root, dirnames, filenames in os.walk('\\\\192.168.1.18\D$'):
        for filename in filenames:
            match=os.path.join(root, filename)
            datafile = file(match)
            for line in datafile:
               print line

rawDescription: Read all system's drive data in network using python


code:
from selenium import webdriver
import re

driver = webdriver.PhantomJS()
driver.set_window_size(1120, 550) #For bug
driver.get("http://localhost:8000")

pattern = r"""
    \s*         #Match whitespace, 0 or more times, followed by...
    (\d+)       #a digit, one or more times, captured, followed by
    \s*         #whitespace, 0 or more times, followed by...
    [|]         #vertical bar, followed by...
    \s*         #whitespace, 0 or more times, followed by...
    \d+         #a digit, one or more times
"""
regex = re.compile(pattern, re.X)

trs = driver.find_elements_by_xpath('//table[@id="ambassadors-for-assignment"]/tbody/tr')
target_columns = [3, 4]

for target_column in target_columns:
    for tr in trs:
        target_column_xpath = './td[{}]'.format(target_column)  #VARY COLUMN HERE ***
        td = tr.find_element_by_xpath(target_column_xpath)
        match_obj = re.match(regex, td.text)

        if match_obj and match_obj.group(1) == '0':
            button_xpath = './/button[contains(concat(" ", normalize-space(@class), " "), " btn-success ")]'
            success_button = tr.find_element_by_xpath(button_xpath)
            #success_button.click()

            print "column {}:".format(target_column)
            print match_obj.group(0)
            print success_button.get_attribute('class')
            print

rawDescription: how to dynamically read a specific cell value in a table using selenium and python

description: Dynamically read a specific cell value in a table using selenium and python


rawDescription: Python Slicing List

description: Python Slicing List


code:
i=3
def foo(i=i):
    print i
i=4
foo()  #prints 3, you're happy now.

rawDescription: Python TkInter - Assigning tags to indeterminate number of images


code:
for x in range(10):
    sx = s.replace('x', str(x))
    for y in range(10):
        sxy = sx.replace('y', str(y))
        for z in range(10):
            sxyz = sxy.replace('z', str(z))
            print int(sxyz)

rawDescription: Python - Number with a variable part

description: Python - Number with a variable part


code:
for i in range(b, -1, -1):
    print i, a[i]

rawDescription: For loops over a list

description: For loops over a list


code:
def velocity(time, value, lf):
    plt.xlabel('$\mathrm{Iterations\/or\/Time}$')
    plt.title(r'$\mathrm{Residual\/history}$')
    plt.grid(True)
    plt.yscale('log')

    if 'Ux_0' in lf:
        velocity_color = 'r'
        velocity_label = 'x-vel'
    elif 'Uy_0' in lf:
        velocity_color = 'b'
        velocity_label = 'y-vel'
    elif 'Uz_0' in lf:
        velocity_color = 'g'
        velocity_label = 'z-vel'

    print "Entered"
    plt.plot(time, value, color = velocity_color, label = velocity_label)

    return time, value, lf

def save_velocity():
    plt.legend()
    plt.savefig(os.path.join(plotsDir, 'velocity'), bbox_inches='tight', dpi=100)
    plt.close()

for path, dirs, files in os.walk(logsDir, topdown=False):
    for lf in files:
        if any((filename in lf) for filename in ('Ux_0', 'Uy_0', 'Uz_0')):
            logFile = os.path.join(path, lf)
            data = np.loadtxt(logFile)
            time, value = data[:,0], data[:,1]
            (time, value, lf) = velocity(time, value, lf) # Not sure why you return any value at all here, do you use these values later in some way?

save_velocity()

rawDescription: If statement issues in Python

description: If statement issues in Python


code:
for row in Classobject.collectInfoFromXLS():
    print row

rawDescription: return Set of lists from out of function: python

description: Return Set of lists from out of function: python


code:
print len(Source)
#33
print len(Amount)
#32
#remove first element
Source = Source[1:]

df=pd.DataFrame({'Source':Source,'Amount':Amount}, columns=['Source','Amount'])

rawDescription: Python 3.x - Create dataframe and specify column names


code:
import requests, json
r = requests.get("https://www.python.org/")
rh = json.dumps(r.headers.__dict__['_store'])
print rh

rawDescription: Get a header with Python and convert in JSON (requests - urllib2 - json)


code:
from Bio.PDB import NeighborSearch, PDBParser, Selection

structure = PDBParser().get_structure('X', "1xxx.pdb")

chain = structure[0]['A']  # Supply chain name for "center residues"
center_residues = [chain[resi] for resi in [100, 140, 170, 53]]
center_atoms = Selection.unfold_entities(center_residues, 'A')

atom_list = [atom for atom in structure.get_atoms() if atom.name == 'CA']
ns = NeighborSearch(atom_list)

# Set comprehension (Python 2.7+, use `set()` on a generator/list for < 2.7)
nearby_residues = {res for center_atom in center_atoms
                   for res in ns.search(center_atom.coord, 10, 'R')}

# Print just the residue number (WARNING: does not account for icodes)
print sorted(res.id[1] for res in nearby_residues)

rawDescription: Get all neighbors of a set of Residues

description: Get all neighbors of a set of Residues


code:
from lxml import html
raw = '''

    foo
    bar

'''
root = html.fromstring(raw)
texts = root.xpath('//text()[normalize-space()]')
print [t for t in texts]
# output : ['foo', '\n\tbar\n']

[html.tostring(e.getparent()) for e in texts]
# output : ['foo\n\tbar\n', 'foo\n\tbar\n']
# see that calling getparent() on 'bar' returns the  not the

rawDescription: How to find text's Parent Node?

description: Find text's Parent Node


code:
url = "https://example.com/{0}".format(id)
print url

rawDescription: Passing a variable in url?

description: Passing a variable in url


code:
import sys
import time
from  multiprocessing import Process, Queue

def worker(q):
    for task_nbr in range(1000000):
        message = q.get()
    sys.exit(1)

def main():
    send_q = Queue()
    Process(target=worker, args=(send_q,)).start()
    msg = {
            'something': "More",
            "another": "thing",
            "what?": range(200),
            "ok": ['asdf', 'asdf', 'asdf']
            }
    for num in range(1000000):
        send_q.put(msg)

if __name__ == "__main__":
    start_time = time.time()
    main()
    end_time = time.time()
    duration = end_time - start_time
    msg_per_sec = 1000000 / duration

    print "Duration: %s" % duration
    print "Messages Per Second: %s" % msg_per_sec

rawDescription: how can I speed up simultaneous read and write of multiprocessing queues?

description: Speed up simultaneous read and write of multiprocessing queues


code:
# Full progress bar during long loop:
import sys

scan_wavelengths = range(0,1000000)  # the variable being varied
nWLs = len(scan_wavelengths)  # how many steps in the loop

# Progress Bar setup:
ProgMax = 20    # number of dots in progress bar
if nWLs<ProgMax:   ProgMax = nWLs   # if less than 20 points in scan, shorten bar
print "|" +    ProgMax*"-"    + "|     MyFunction() progress"
sys.stdout.write('|'); sys.stdout.flush();  # print start of progress bar
nProg = 0   # fraction of progress

# The main loop:
for step,wavelength   in   enumerate(scan_wavelengths):
    ''' `step` goes from 0-->N during loop'''

    #<Your complicated stuff in the loop>

    # update progress bar:
    if ( step >= nProg*nWLs/ProgMax ):
        '''Print dot at some fraction of the loop.'''
        sys.stdout.write('*'); sys.stdout.flush();
        nProg = nProg+1
    if ( step >= nWLs-1 ):
        '''If done, write the end of the progress bar'''
        sys.stdout.write('|     done  \n'); sys.stdout.flush();

rawDescription: How can I view the progress of a program run in spyder IDE?

description: View the progress of a program run in spyder IDE


code:
import random

def rand_print(s, char_rand_max, str_rand_max):
    for i in range(random.randint(1, str_rand_max)):
        print ''.join([c*random.randint(1, char_rand_max) for c in s]),
    print

rawDescription: Printing a random amount of characters a random amount of times in one line


code:
from string import ascii_lowercase
from itertools import product

for length in range(minimum_length, maximum_length + 1):
    for combo in product(ascii_lowercase, repeat=length):
        print ''.join(combo)

rawDescription: Python: Every possible letter combination


code:
seq = sc.parallelize(zip(xrange(16), "ATCGATGCATGCATGC"))

(seq
 .flatMap(lambda (pos, x): ((pos - i, (pos, x)) for i in range(3)))
 .groupByKey()
 .mapValues(lambda x: ''.join(v for (pos, v)  in sorted(x)))
 .filter(lambda (pos, codon): len(codon) == 3)
 .map(lambda (pos, codon): (pos % 3, pos, codon))
 .collect())

description: Conditionally Combining/Reducing key-pairs


code:
df = pd.DataFrame({'a': [1, 3, 5, 7, 9]})
a = [1, 2, 3]
for x in a:
    print '{x} is in df'.format(x=x), any(df.a == x)

rawDescription: How to convert unicode string into list using pandas python?

description: Convert unicode string into list using pandas python


code:
from lxml import html
raw = '''

    Recommended length of visit:
    <1 hour

    Fee:
    No

'''

root = html.fromstring(raw)
print html.tostring(root)

rawDescription: Scrapy xpath get text of an element that starts with <


code:
line ='(j V t) ^ (p V q) ^ (~p V t),j=T,t=F,p=F,q=T'
KB = re.search(r'^([^,]*),(.*)$', line).group(1)
Truth = re.search(r'^([^,]*),(.*)$', line).group(2)
print KB
print Truth

rawDescription: return all the text before the first comma

description: Return all the text before the first comma


code:
result = []
list = ['ba', 'cb', 'dc', 'ed', 'gf', 'jh']
start = 2
step = 5
end = start + step*len(list)
for i in range(start, end, step):
    result.append(list[i%len(list)])
print result

rawDescription: how to loop through list multiple times in Python

description: Loop through list multiple times in Python


code:
for el in seurch2('stardict',wordlist):
    print el

rawDescription: Finding out exact match in python

description: Finding out exact match in python


code: print hashlib.__doc__

rawDescription: Make python __doc__ property display correctly in a Windows CMD window?


code:
print 'Unique contigs are:', count_config.keys()
print 'Unique protens are:', count_protein.keys()

rawDescription: Comparing elements in line

description: Comparing elements in line


code:
set_1 = [["yes", 1], ["maybe", 1], ["never", 0], ["nopes", 0], ["si", 1]]
set_2 = ["of course", "yes", "always", "never", "no way", "no"]

def predict_label(item):
    return 2 # just to check which items got predicted

dset_1 = dict(set_1)

labeled_set_2 = [[item, dset_1.get(item, predict_label(item))] for item in set_2]
print labeled_set_2

rawDescription: How to find and order duplicates in data sets

description: Find and order duplicates in data sets


code:
blobs = binarized.findBlobs()
for blob in blobs:
   blob.draw()
   print blob
   binarized.show()
   raw_input()

rawDescription: How do I locate the rabbit?

description: Locate the rabbit


code:
for i in range(100):
    print 'doing something'
    time.sleep(1)
    if HostFunctions.AbortScript:
        cleanup()

rawDescription: Terminating an IronPython script

description: Terminating an IronPython script


code:
def rebase(word):
    possible_replacements = {'A': 'CTG', 'C': 'ATG'} # etc
    for base in word:
        print base
        try:
           yield random.choice(possible_replacements[base])
        except KeyError:
          # If you didn't change this base, yield the original one
          yield base

new_word = word[:5] + ''.join(rebase(word[5:]))

rawDescription: Using python replace function in for loop


code:
from pyparsing import *
import json

identifier = Word(alphas + nums + "_")
expression = identifier("lhs") + Suppress("=") + identifier("rhs")
struct_vals = delimitedList(Group(expression | identifier))
structure = Group(identifier + nestedExpr(opener="{", closer="}", content=struct_vals("vals")))
grammar = delimitedList(structure)

my_string = "something{name1, name2, opt1=2, opt2=text}, something_else{name3, opt1=58}"
parse_result = grammar.parseString(my_string)
result_list = parse_result.asList()

def list_to_dict(l):
    d = {}
    for struct in l:
        d[struct[0]] = {}
        for ident in struct[1]:
            if len(ident) == 2:
                d[struct[0]][ident[0]] = ident[1]
            elif len(ident) == 1:
                d[struct[0]][ident[0]] = None
    return d

print json.dumps(list_to_dict(result_list), indent=2)

rawDescription: Parsing a lightweight language in Python


code:
from test import NotifyList
top = NotifyList([0]*3, None, None) # Now we have [0 0 0]
print top
top[0] = NotifyList([0]*3, top, 0) # Now we have [ [0 0 0]  0  0 ]
print top
top[0][1] = NotifyList([0]*3, top[0], 1) # Now we have [ [[0 0 0] 0 0]  0  0 ]
top[0][1][2] = "this is a string" # Now we have [ [[0 0 "this is a string] 0 0]  0  0 ]
print top[0][1][2]
print top[0][1]

rawDescription: How to detect what element of a nested list has changed? (python)

description: Detect what element of a nested list has changed? (python)


code:
for table in response.xpath('//table[@class="table-stats"]'):
    date = table.xpath('./preceding-sibling::h2[1]/text()').extract()[0]
    print date
    for match in table.xpath('.//tr[@class="preview" and @id]'):
        home_team = match.xpath('.//span[@class="team-home teams"]/a/text()').extract()[0]
        away_team = match.xpath('.//span[@class="team-away teams"]/a/text()').extract()[0]
        print home_team, away_team

rawDescription: How to scrape contents from multiple tables in a webpage

description: Scrape contents from multiple tables in a webpage


code:
class A(object):
    def __init__(self):
        self.x = 0

class B(A):
    pass

ai = A()
bi = B()

print id(ai.x) == id(bi.x)
ai.x=3
print id(ai.x) == id(bi.x)

rawDescription: Is it possible to create a completely new instance of the members of a parent class?


code:
import math
import operator
from functools import partial
# Always do imports first then global vars
x=0
y=0

def move(steps,op_func,z):
    if z == "x":
        global x
        x = op_func(x, int(steps))
    else:
        global y
        y = op_func(y, int(steps))

def main():

    # We only need to define this once
    directions = {"UP":partial(move, op_func=operator.add, z="x"),
                  "DOWN":partial(move, op_func=operator.sub, z="x"),
                  "RIGHT":partial(move, op_func=operator.add, z="y"),
                  "LEFT":partial(move, op_func=operator.sub, z="y")}

    user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")
    while user_direct != " ":
        steps=user_direct[-1]
        direction=user_direct[:-1].replace(" ","")
        move_func = directions[direction]
        move_func(steps)

        user_direct=raw_input("Enter the way that you want the plane to move and how many steps you want it to move\n")

        global x
        global y
        distance=math.sqrt(x**2+y**2)
        print distance
main()

rawDescription: global varriable doesn't change

description: Global varriable doesn't change


rawDescription: Sorting a heterogeneous list of objects in Python


code:
from databases import Databases

db = Databases()

for qr in db.DB1.query(db.DB1.User):
    print qr.userid, qr.username

rawDescription: SQLAlchemy and Multiple Databases

description: SQLAlchemy and Multiple Databases


rawDescription: Python - How to transform a list of tuple to a tuple


code:
myList =[ [(None, None), (' mated', None), (' rooster', None), ('', None)] ,
              [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ]

# If there are multiple none values (if the tuple isn't always just two values)
print [ [ [ x for x in z if x == None] for z in el ] for el in myList ]
# [[[None, None], [None], [None], [None]], [[None], [None], [None], [None], [None]]]

# If it's always the last value in the tuple
print [[x[-1] for x in el] for el in myList]
# [[None, None, None, None], [None, None, None, None, None]]

rawDescription: How to access tuple elements in a nested list

description: Access tuple elements in a nested list


code:
for i, j, k in zip(a, a[1:], a[2:]):
 print i, j, k

rawDescription: Is there an easy way to get next and prev values in a for-loop?


code:
attrs = "Id,First,Last,Department,Hourly_Wage".split(',')
data = [ (1, 'Greg','Foo','HR',100),
          (2, 'Judy','Bar','EE',51),
          (3, 'Jake','Bar','HR',75),
          (4, 'Lila','Bax','HR',49),
          (5, 'Norm','Fob','EE',49) ]

from littletable import Table, DataObject

instructors = Table()
instructors.insert_many(
    DataObject(**dict(zip(attrs,d)))
        for d in data)

# can add index before or after items are added to table
instructors.create_index("Id", unique=True)
instructors.create_index("Department")

# unique keys are enforced
try:
    instructors.insert(DataObject(Id=4, First="Bob", Last="Fob"))
except KeyError as e:
    print e

# keys checked for uniqueness when creating unique index
try:
    instructors.create_index("Last", unique=True)
except KeyError as e:
    print e

# Uniquely indexed access gives a single record
print "%(First)s %(Last)s" % instructors.by.Id[3]

# Non-uniquely indexed access gives a new Table
print '\n'.join("%(Department)s %(First)s %(Last)s" % inst
                    for inst in instructors.by.Department["HR"])

# Table can still be accessed like a Python list
print "%(First)s %(Last)s" % instructors[-1]
print '\n'.join("%(Department)s %(First)s %(Last)s" % inst
                    for inst in instructors)

# use pivot for multi-dimensional grouping
instructors.addfield("wage_bracket", lambda d:d.Hourly_Wage/10*10)
instructors.create_index("wage_bracket")
instructors.pivot("Department wage_bracket").dump()
instructors.pivot("Department wage_bracket").dump_counts()

import sys
instructors.csv_export(sys.stdout)

rawDescription: A DSL in Python for tabular analysis and processing?


code:
s = '''GROUP = L1_METADATA_FILE
  GROUP = METADATA_FILE_INFO
    ORIGIN = "Image courtesy of the U.S. Geological Survey"
    REQUEST_ID = "0101305309253_00043"
    LANDSAT_SCENE_ID = "LE71460402010069SGS00"
    FILE_DATE = 2013-06-02T11:19:59Z
    STATION_ID = "SGS"
    PROCESSING_SOFTWARE_VERSION = "LPGS_12.2.1"
    DATA_CATEGORY = "NOMINAL"
  END_GROUP = METADATA_FILE_INFO
  GROUP = PRODUCT_METADATA
    DATA_TYPE = "L1T"
    ELEVATION_SOURCE = "GLS2000"
    OUTPUT_FORMAT = "GEOTIFF"
    EPHEMERIS_TYPE = "DEFINITIVE"
    SPACECRAFT_ID = "LANDSAT_7"
    SENSOR_ID = "ETM"
    SENSOR_MODE = "BUMPER"
    WRS_PATH = 146
    WRS_ROW = 040
    DATE_ACQUIRED = 2010-03-10'''

output = {} #Dict
for line in s.split("\n"): #Iterates through every line in the string
    l = line.split("=") #Seperate by "=" and put into a list
    output[l[0].strip()] = l[1].strip() #First word is key, second word is value

print output #Output is a dictonary containing all key-value pairs in your metadata seperated by "="

print output["SENSOR_ID"] #Outputs "ETM"

rawDescription: convert contents of metadata file into variables list

description: Convert contents of metadata file into variables list


code:
import sys

# Create 2 arrays which will contain independently the subjects and the marks:
# 1 - Initialize your array containing your 'subjects'
subjects = []
# 2 - Initialize your array containing your 'marks'
marks = []

# Opening file for reading. The path of the file is given in argument
open_file = open(sys.argv[1], 'r')

for line in open_file:
    # 'line' is a line in your file

    # We make sure that 'line' is non-empty
    if line:
        # for each line in your file, we read something like this:
        # subject---marks
        # We need to split the different information, which are separated by '---'
        information = line.split('---')

        # 'information' is an array containing 2 values:
        # 'information[0]' contains the subject
        # 'information[1]' contains the mark

        # We store each separate information in our arrays 'subjects' and 'marks':
        subjects.append(information[0])
        marks.append(information[1])

# When finishing reading the file, we close it
open_file.close()

# Now that all the data inside the file has been read, and store in our arrays
#    we have to use it to create / print it in an HTML table

# Print opening HTML tags -------------------------
print "<htm>
"

# Print the content of the table, line by line ----
for i in range(0, len(subjects)):
    print "
"+subjects[i]+"	"+marks[i]+"
"

# Print closing HTML tags -------------------------
print "
"

rawDescription: how to create a table using python

description: Create a table using python


code:
output = []

for i,j in itertools.combinations([a,b,c],2):
    all_diffs=alldiffs(i,j)
    total=count_total(i,j)
    zero=count_zero(all_diffs)
    total=np.array(total)
    union=map(sub,total,zero)
    zero=np.array(zero).tolist()
    union=np.array(union).tolist()
    union=[list(x) for x in union]
    sim=[[float(aaa) / bbb for (aaa, bbb) in itertools.izip(aa, bb)] \
        for (aa, bb) in itertools.izip(zero, union)]
    sim_comb=sum(sim,[])
    sum_of_sim=sum(sim_comb)
    number_sum=len(sim_comb)
    ave=sum_of_sim/number_sum
    one_ave=1-ave
    output += [one_ave]

print output

rawDescription: Python: How to store for loop result in a list?


code:
lst = [
  [['pat'],['cut'],['rat']],
  [['sat','pat'],['cut','pat']],
  [['rat','cut','pat'],['put','cut','bat'],['mat','gut','lit']]
]

print map(lambda ll: [len(set(l)) for l in zip(*ll)], lst)

rawDescription: Python: List Comparison to find unique element count of lists


rawDescription: How to use python to format strings with delimiter into columns

description: Use python to format strings with delimiter into columns


code:
def ircRecv():
    line = ''
    while 1: # same as while True:
        character = irc.recv(1)
        if character == '\n':
            break # exit the loop
        elif character != '\r':
            line += character
    print line
    return line

rawDescription: Python IRC Bot won't join channels on some servers, and I don't know how to make it respond to user input


code:
from nltk.corpus import names
from nltk.classify import apply_features, NaiveBayesClassifier

def gender_features(word):
    return {'last_letter': word[-1]}

train_female = [(word,'female') for word in names.words('female.txt')[:250]]
train_male = [(word,'male') for word in names.words('male.txt')[:250]]
train_data = train_female + train_male
train_set = apply_features(gender_features, train_data)

# Do like wise for the test set.
'''
test_female = [(word,'female') for word in names.words('female.txt')[250:]]
test_male = [(word,'male') for word in names.words('male.txt')[250:]]
test_data = test_female + test_male
test_set = apply_features(gender_features, test_data)
'''

classifier = NaiveBayesClassifier.train(train_set)
print classifier.classify(gender_features('Neo'))

rawDescription: TypeError: 'WordListCorpusReader' object has no attribute '__getitem__' while using nltk.classify.apply_features


code:
def wfsc_combined(self):
    print "%s\t%s\t%s" % (self.name[0], self.subnet[0], self.gateway[0])
    for ip in range(100,110):
        print "192.168.1.%d" % ip

rawDescription: Python - List and Loop in one def

description: Python - List and Loop in one def


code: print "foo bar {:>02} baz".format(birthday[month])

rawDescription: Python: How could this be written in a less verbose form?


code:
named_cursor = conn.cursor(name='cursor_x')
for row in named_cursor:
    print row

rawDescription: How to use postgres cursor within Python code

description: Use postgres cursor within Python code


code:
import lxml.etree

xml = '''<?xml version='1.0' encoding='ASCII'?>
<root>

    something
    <c>something different</c>

</root>'''

root = lxml.etree.fromstring(xml)
a = root.find('.//a')
parent = lxml.etree.Element('new_parent')
for child in a:
    parent.append(child)
a.append(parent)

print lxml.etree.tostring(root, xml_declaration=True)

rawDescription: Change parent in xml by Python (lxml)

description: Change parent in xml by Python (lxml)


code:
def a(name, age, id_words, phone):
     name = name
     age=age
     id=id_words[0]
     words=id_words[1]
     phone=phone
     print name+age+id+words+phone

a("piyush", "23", f("12","123"), "123")
piyush23123hello123

rawDescription: Pass function outputs as arguments to another function


code:
import numpy as np

def contiguous_regions(condition):
    """Finds contiguous True regions of the 1D 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"
    idx = np.flatnonzero(np.diff(condition)) + 1

    # Prepend or append the start or end indicies to "idx"
    # if there's a block of "True"'s at the start or end...
    if condition[0]:
        idx = np.append(0, idx)
    if condition[-1]:
        idx = np.append(idx, len(condition))

    return idx.reshape(-1, 2)

# Generate an example dataset...
t = np.linspace(0, 4*np.pi, 20)
x = np.abs(np.sin(t)) + 0.1
x[np.sin(t) < 0.5] = 0

print x

# Get the contiguous regions where x is not 0
for start, stop in contiguous_regions(x != 0):
    print x[start:stop]

rawDescription: Numpy Detection of region borders

description: Numpy Detection of region borders


code:
row_available = [k for k, v in seats[rowChoice].iteritems() if v == 'available']
avail_count = len(row_available)
if avail_count:
    print 'there {is_are} {count} seat{plural} available in row {rowChoice}, seat{plural} {seats}'.format(
        is_are='are' if avail_count > 1 else 'is', count=avail_count,
        plural='s' if avail_count > 1 else '', rowChoice=rowChoice,
        seats=row_available[0] if avail_count == 1 else ' and '.join([', '.join(row_available[:-1]), row_available[-1]]))

rawDescription: counting the number of key value pairs in a dictionary

description: Counting the number of key value pairs in a dictionary


code:
data={}
ss=((0,19),(20,41),(42,80))
with open('/tmp/p.txt','r') as f:
    for n,line in enumerate(f):
        fields={}
        for i,j in ss:
            field=line[i:j]
            t=field.split(':')
            fields[t[0].strip()]=t[1].strip()
        data[n]=fields

print data

rawDescription: Parsing string sections

description: Parsing string sections


code:
def Buildings(function):
    if function == '1':
        print 'blablabla'
    elif function == '2':
        print 'blablabla'
Buildings('1')

rawDescription: Object-Oriented Programming in Python

description: Object-Oriented Programming in Python


code:
import os, csv

# store file attributes for easy modifications
# format is 'filename': (hashcolumn, filepathNum)
files = {
        'A.csv': (7, 5),
        'B.csv': (15, 5),
        'C.csv': (1, 0),
        }

class NoFilesFound(Exception):
    "No .csv files were found to clean up"

def chkifexists(somefiles):
    # load all three at once, but only yield them if filename
    # is found
    filesfound = False
    for fname, (hashcolumn, filepathNum) in somefiles.items():
        if os.path.isfile(fname):
            filesfound = True
            yield fname, hashcolumn, filepathNum
    if not filesfound:
        raise NoFilesFound

def removedupes(infile, outfile, hashcolumn, filepathNum):
    # this is now a single-run function
    r1 = file(infile, 'rb')
    r2 = csv.reader(r1)
    w1 = file(outfile, 'wb')
    w2 = csv.writer(w1)
    hashes = set()
    for row in r2:
        if row[hashcolumn] =="":
            w2.writerow(row)
            hashes.add(row[hashcolumn])
        if row[hashcolumn] not in hashes:
            w2.writerow(row)
            hashes.add(row[hashcolumn])
    w1.close()
    r1.close()

def bakcount(origfile1, origfile2):
    '''This function creates a .bak file of the original and does a row count
    to determine the number of rows removed'''
    os.rename(origfile1, origfile1+".bak")
    count1 = len(open(origfile1+".bak").readlines())
    #print count1

    os.rename(origfile2, origfile1)
    count2 = len(open(origfile1).readlines())
    #print count2

    print str(count1 - count2) + " duplicate rows removed from " \
        + str(origfile1) +"!"

def CleanAndPrettify():
    print "Removing duplicate rows from input files..."
    try:
        for fname, hashcolumn, filepathNum in chkifexists(files):
            removedupes(
                   fname,
                   os.path.splitext(fname)[0] + "2.csv",
                   hashcolumn,
                   filepathNum,
                   )
            bakcount (fname, os.path.splitext(fname)[0] + "2.csv")
    except NoFilesFound:
        print "no files to clean up"

CleanAndPrettify()

rawDescription: Python: Check if file from list exists, execute function only if it exists


code:
class SomeDialect(csv.excel):
    delimiter = ','
    quotechar = '"'
    escapechar = "\\"
    doublequote = False
    lineterminator = '\n'
    quoting = csv.QUOTE_MINIMAL

f = cStringIO.StringIO()
writer = csv.writer(f, dialect=SomeDialect)
writer.writerow(the_list)
print f.getvalue()

rawDescription: How can I join a list into a string (caveat)?

description: Join a list into a string (caveat)


code:
"""A particular XML parser.  Formats change, so sometimes this changes, too."""

import xml.etree.ElementTree as xml

class SSXML_Source( object ):
    ns0= "urn:schemas-microsoft-com:office:spreadsheet"
    ns1= "urn:schemas-microsoft-com:office:excel"
    def __init__( self, aFileName, *sheets ):
        """Initialize a XML source.
        XXX - Create better sheet filtering here, in the constructor.
        @param aFileName: the file name.
        """
        super( SSXML_Source, self ).__init__( aFileName )
        self.log= logging.getLogger( "source.PCIX_XLS" )
        self.dom= etree.parse( aFileName ).getroot()
    def sheets( self ):
        for wb in self.dom.getiterator("{%s}Workbook" % ( self.ns0, ) ):
            for ws in wb.getiterator( "{%s}Worksheet" % ( self.ns0, ) ):
                yield ws
    def rows( self ):
        for s in self.sheets():
            print s.attrib["{%s}Name" % ( self.ns0, ) ]
            for t in s.getiterator( "{%s}Table" % ( self.ns0, ) ):
                for r in t.getiterator( "{%s}Row" % ( self.ns0, ) ):
                    # The XML may not be really useful.
                    # In some cases, you may have to convert to something useful
                    yield r

rawDescription: Which language is easiest and fastest to work with XML content?


code:
from lxml import etree

tree = etree.parse('test.xml')

result = {}
for element in tree.xpath('/ecmaarray/*'):
    name = element.attrib["name"]
    text = element.text
    childs = element.getchildren()

    if not childs:
        result[name] = text
    else:
        child_dict = {}
        child_list = []
        for child in childs:
            child_name = child.attrib.get('name')
            child_text = child.text
            if child_name:
                child_dict[child_name] = child_text
            else:
                child_list.append(child_text)

        if child_dict:
            result[name] = child_dict
        else:
            result[name] = child_list

print result

rawDescription: Extract attributes and certain tag values from xml using python script


code:
import yaml,sys

class StateMachine(object):
    pass

class State(object):
    pass

class Connection(object):
    pass

if __name__ == '__main__':
    o = yaml.load("""
    !!python/object:myyaml.StateMachine {
         states: [
           !!python/object:myyaml.State { name: p1 },
           !!python/object:myyaml.State { name: p2 },
           !!python/object:myyaml.State { name: p3 },],
         connections:
          [       !!python/object:myyaml.Connection { 'pim' : [p1,p2]}]}
    """)
    print o.states[0].name
    print o.states[1].name
    print o.connections[0].pim
    sys.exit(0)

rawDescription: Python, yaml nested objects

description: Python, yaml nested objects


rawDescription: getting desired value from dictionary based on another value

description: Getting desired value from dictionary based on another value


code:
seq = 1
a_dict = {}
moves = []
while seq < 4:
 a_dict['key' + str(seq)] = 'value' + str(seq)
 moves.append(copy.deepcopy(a_dict))
 seq = seq + 1

print moves

rawDescription: Saving a deep copy of an object, then modifying it and saving another copy?


code:
import networkx as nx

# Construct graph from example image, all edges pointing away from source
G = nx.DiGraph()
G.add_path([0,1,2,3,4])
G.add_path([1,5])
G.add_path([3,6])
G.add_path([2,7,8])

# Find first with 2 neighbors
neighbors = G.successors(0)
for n in neighbors:
    nneighbors = set(G.successors(n))
    if len(nneighbors) == 2:
        print "Found", n
        break
    neighbors.extend(nneighbors)

rawDescription: Identify first node after source node having two neighbors in NetworkX DiGraph


code:
import itertools
for ticker1, ticker2 in itertools.permutations(tickers, 2):
        print "t1=%s, t2=%s" % (ticker1, ticker2)

rawDescription: python function taking arguments from list

description: Python function taking arguments from list


code:
x = 10
print "'x' is ", x
def fun1(x):
    print "'x' is local to fun1 and is", x
    x = x**2
    print "'x' is local to fun1 and is now", x

def fun2():
    global x
    print "'x' is global and is", x
    x = x ** 2
    print "'x' is global and is now", x
print "'x' is still", x
fun1(x)
print "'x' is not local to fun anymore and returns to it's original value: ", x
fun2()
print "'x' is not local to fun2 but since fun2 uses global 'x' value is now:", x

rawDescription: Python why doesn't variable change after function?


code:
def replace_newline_with_br(s, soup):
    lines = s.split('\n')
    div = soup.new_tag('div')
    div.append(lines[0])
    for l in lines[1:]:
        div.append(soup.new_tag('br'))
        div.append(l)
    soup.append(div)

mytext = "My text with a few \n newlines \n"
mytext2 = "Some other text \n with a few more \n newlines \n here"

soup = BeautifulSoup("", )
replace_newline_with_br(mytext, soup)
replace_newline_with_br(mytext2, soup)
print soup.prettify()

rawDescription: Replace "\n" in element string with <br> tag in Beautifulsoup


rawDescription: TypeError: A Future or coroutine is required


code:
str = ['foo', 'bar']
type(str)
print repr(str)
del str
type(str)
print repr(str)

rawDescription: TypeError list object is not callable when trying to convert an object to a string


code:
class Foo(object):

    def __del__(self):
        print "deling", self

if __name__ == '__main__':
    import gc
    gc.disable() # no gc
    f = Foo()
    print "before"
    del f # f gets deleted right away
    print "after"

rawDescription: How to force deletion of a python object?

description: Force deletion of a python object


code:
for ele in line:
    print ele

rawDescription: Getting Poorly Formatted Output (Python)


rawDescription: Create a Reg Exp to search for __word__?

description: Create a Reg Exp to search for __word__


code:
import matplotlib.pyplot as plt
import matplotlib.colors as colors

norm = colors.Normalize(vmin=0,vmax=4)
cmap = plt.cm.viridis

mycolor = cmap(norm(3.5))
print mycolor
# (0.67848900000000001, 0.86374200000000001, 0.189503, 1.0)

rawDescription: Convert given value of contourf color to rgb code


code:
def example_generator():
    for i in [1,2,3,4,5]:
        yield i

gen = GeneratorWrap(example_generator())
print gen.next()  # prints 1
print gen.next()  # prints 2

rawDescription: is there an alternative way of calling next on python generators?

description: Is there an alternative way of calling next on python generators


code: print m[1][3]

rawDescription: Merging lists in a row wise manner to generate a 2D array


code:
from BeautifulSoup import BeautifulSoup, Tag, NavigableString
import re

html = """

The quick brown fox jumped over the lazy Dog

The dog, who was, in reality, not so lazy, gave chase to the fox.

See image for reference:

"""
soup = BeautifulSoup(html)

#search term, url reference
keywords = [("dog","http://en.wikipedia.org/wiki/Dog"),
            ("fox","http://en.wikipedia.org/wiki/Fox")]

def insertLinks(string_value,string_href):
    for t in soup.findAll(text=re.compile(string_value, re.IGNORECASE)):
            if t.parent.name !='a':
                    a = Tag('a', name='a')
                    a['href'] = string_href
                    a.insert(0, NavigableString(string_value))
                    string_list = re.compile(string_value, re.IGNORECASE).split(t)
                    replacement_text = soup.new_string(string_list[0])
                    t.replace_with(replacement_text)
                    replacement_text.insert_after(a)
                    a.insert_after(soup.new_string(string_list[1]))

for word in keywords:
    insertLinks(word[0],word[1])

print soup

rawDescription: Python String Replace: Keywords into URLs


code:
from collections import defaultdict
result = defaultdict(int)
for p,f in zip(positions,forces):
    result[p] += f

positions, forces = zip(*result.items())
print positions, forces

rawDescription: Sum elements in array with NumPy

description: Sum elements in array with NumPy


code:
numstr= raw_input("please enter the height:")

rows = int(numstr) # --> convert user input to an integer
def triangle(rows):
    PrintingList = list()
    for rownum in range (1, rows + 1): # use colon after control structure to denote the beginning of block of code
        PrintingList.append([]) # append a row
        for iteration in range (rownum):
            newValue = raw_input("Please enter the next number:")
            PrintingList[rownum - 1].append(int(newValue))
            print()

    for item in PrintingList:
      print item
triangle(rows)

rawDescription: Creating a triangle of numbers from user-input


code:
import os
import sys
import glob

SYS_USB_DEVICES = '/sys/bus/usb/devices'
SYS_BLOCK_DEVICES = '/sys/class/block'

try:
    serial = sys.argv[1]
except IndexError:
    print >> sys.stderr, "Usage: findflash.py SERIAL"
    sys.exit(1)

# PASS 1 Find USB node with corresponding to serial

for usbid in os.listdir(SYS_USB_DEVICES):
    usbserpath = os.path.join(SYS_USB_DEVICES, usbid, 'serial')
    if not os.path.exists(usbserpath):
        continue
    with open(usbserpath) as f:
        usb_serial = f.read().strip()

    if serial == usb_serial:
        # Found it!
        break
else:
    print >> sys.stderr, "Cannot find usb device with serial {0}".format(serial)
    sys.exit(1)

# Find SCSI ids corresponding to this device
# I didn't check SYSFS documentation, but tested it on openSUSE 13.1
# The form of path is:
#   <SUBDEVICE>/host<SCSI_HOST_ID>/target<SCSI_TARGET_ID>/<CTRL><CHANNEL>:<TGT>:<LUN>
# We need only basename

devs = glob.glob(os.path.join(SYS_USB_DEVICES, usbid,
                            '*/host*/target*/*:*:*:*'))

devs = map(os.path.basename, devs)

# PASS 2 - find mountpoints for devices with SCSI ids we discover earlier

# Parse mountpoint formatted as "/dev/... /path/to/mntpt ..."
def parse_mntpt(line):
    dev, mntpt, _ = line.split(None, 2)
    dev = os.path.basename(dev)
    return dev, mntpt

mntpts = {}
with open('/proc/mounts') as f:
    mntpts = dict(map(parse_mntpt, f.readlines()))

# List of ('scsi id', 'dev name', 'mnt pt (if exists)')
devlist = []

def create_dev(scsiid, devname):
    global mntpts
    devlist.append((scsiid, devname, mntpts.get(devname)))

for devname in os.listdir(SYS_BLOCK_DEVICES):
    devpath = os.path.join(SYS_BLOCK_DEVICES, devname)
    devlink = os.path.join(devpath, 'device')

    # Node is "virtual", i.e. partition, ignore it
    if not os.path.islink(devlink):
        continue

    scsiid = os.path.basename(os.readlink(devlink))
    if scsiid not in devs:
        continue

    create_dev(scsiid, devname)

    # Find partition names
    parts = glob.glob(os.path.join(devpath, '*/partition'))

    for partpath in parts:
        partname = os.path.basename(os.path.dirname(partpath))
        create_dev(scsiid, partname)

# END - print results
fmtstr = '{0:8} {1:5} {2}'
print fmtstr.format('SCSI ID', 'DEV', 'MOUNT POINT')
for scsiid, devname, mntpt in devlist:
    print fmtstr.format(scsiid, devname, mntpt)

rawDescription: Is it possible to get the mountpoint of a pendrive knowing its serialnumber using python?


code:
def rotate_left(x, n, maxbit=32):
    # assumes 32 bit
    x = x & (2 ** maxbit - 1)
    return ((x << n) | (x >> (maxbit - n)))

def key_hash(data):
    # hash should be a 32-bit unsigned integer
    hashed = 0
    for char in data:
        hashed = (hashed + rotate_left(hashed, 19) + ord(char))
    return hashed

def khash(data):
    h = 0
    for c in data:
        assert 0 <= h <= 0xFFFFFFFF
        h = (h + (h << 19) + (h >> 13) + ord(c)) & 0xFFFFFFFF
    assert 0 <= h <= 0xFFFFFFFF
    return h

guff = "twas brillig and the slithy toves did whatever"
print "yours: %08X" % key_hash(guff)
print "mine : %08X" % khash(guff)

rawDescription: CARP hash in Python

description: CARP hash in Python


code:
heapq.heapify(items)
size = 0
while items and size < 45000:
  item = heapq.heappop(items)
  size += item[1]
  print item

rawDescription: How can I partially sort a Python list?

description: Partially sort a Python list


code:
with open('my_file.csv') as f_obj:
    reader1, reader2 = itertools.tee(csv.reader(f_obj))
    next(reader2) # skip first line in reader2
    for f_row , s_row in zip(reader1, reader2):
          print f_row, s_row

rawDescription: how to iterate through consecutive lines of a csv file

description: Iterate through consecutive lines of a csv file


code:
parser_start.add_argument("file", help="firstline\nnext line\nlast line")
print parser.parse_args(['stop', '--help'])

rawDescription: Python argparse: How to insert newline the help text in subparser?


code:
l = ['This', 'is', 'a', 'list', 'BOMBAST']
print [x[0].upper() + x[1:] for x in l]
['This', 'Is', 'A', 'List', 'BOMBAST']

rawDescription: Capitalize the first letter in each string in a list


code:
import argparse

parent_parser = argparse.ArgumentParser(add_help=False)
parent_parser.add_argument('-H', '--host', default='192.168.122.1')
parent_parser.add_argument('-P', '--port', default='12345')

parser = argparse.ArgumentParser(add_help=False)
subparsers = parser.add_subparsers()

# subcommand a
parser_a = subparsers.add_parser('a', parents = [parent_parser])
parser_a.add_argument('-D', '--daemon', action='store_true')

parser_a.add_argument('-L', '--log', default='/tmp/test.log')

# subcommand b
parser_b = subparsers.add_parser('b', parents = [parent_parser])
parser_b.add_argument('-D', '--daemon', action='store_true')

# subcommand c
parser_c = subparsers.add_parser('c', parents = [parent_parser])
args = parser.parse_args()

print args

rawDescription: How to add common arguments to argparse subcommands?

description: Add common arguments to argparse subcommands


code:
for sentence in sentences:
    if ('federal', 'JJ') in sentence:
        print sentence

rawDescription: List with tuples in python

description: List with tuples in python


code:
import os
import sys

while True:
    value = raw_input('Enter data: ')

    if value == 'quit':
        break

    print value

    # wait for enter key
    sys.stdin.read(1)
    os.system('cls')

rawDescription: "Refreshing" Command Line in Windows

description: "Refreshing" Command Line in Windows


code:
class AdderWithBroadcast(object):

    consumers = []

    def __init__(self, x_prod, y_prod):
        self.x_prod = x_prod
        self.y_prod = y_prod

    def __iter__(self):
        return self

    def next(self):
        x = next(self.x_prod)
        y = next(self.y_prod)

        for consumer in self.consumers:
            consumer.send(x+y)

        return x+y

    def consumer():
        while True:
            a = (yield)
            print a, ' in consumer'

k = iter(range(10))
adder = AdderWithBroadcast(k, k)
cons = consumer()
cons.send(None)
adder.consumers.append(cons)
for i in adder:
    #  I won't include the actual result here, you can try in no your own
    print i

rawDescription: How to unify sender- and receiver-based coroutines?

description: Unify sender- and receiver-based coroutines


code:
for seq in generate_sequences(('A', 2), ('B', 1), ('C', 1)):
    print seq

# Outputs
#
# CBAA
# BCAA
# BACA
# BAAC
# CABA
# ACBA
# ABCA
# ABAC
# CAAB
# ACAB
# AACB
# AABC

rawDescription: Finding all sequences of A, B such that have a specified number of each element


code:
syms = ['a','b','c','d','e']
weights = [30, 25, 20, 15, 10]
t = encode(syms, weights)
print t

rawDescription: Mapping a list to a Huffman Tree whilst preserving relative order


code:
s = 'Hello world'
t = buffer(s, 6, 5)
t
print t

rawDescription: What is Python buffer type for?

description: What is Python buffer type for


code:
class Test(unittest.TestCase):
     value = result_of_some_expensive_function()
     def test(self):
         print self.value

rawDescription: How to set up a resource shared by several unit tests?

description: Set up a resource shared by several unit tests


code:
in_array = np.asarray([[7, 40], [777, 440]])
for index in np.ndindex(in_array.shape[1:]):
    print "---"
    print index
    print in_array[:, index] # gives 2D array

rawDescription: numpy.ndindex and array slices

description: Numpy.ndindex and array slices


code:
import sys
var_from_as = sys.argv[1] # for 1rst parameter cause argv[0] is file name
print 'this gets returned to AppleScript' # this gets set to outputvar

rawDescription: Getting Variable from Applescript and using in Python


code:
message = [
    {'Message_content': {
            'Argument': [{ 'Arg_name': 'acl_name' }, { 'Arg_name': 'acl_type' }],
            'Message': { 'Message_content': { 'Argument': [{ 'Arg_name': 'cat_name' }, { 'Arg_name': 'cat_enum' }] } }
    }},
    { 'Message_content': { 'Argument': [{ 'Arg_name': 'ac_name' }, { 'Arg_name': 'ac_type' }] } }
]

elements_to_handle = message
def handle_element(elem, output):
    try:
        arg = elem["Message_content"]["Argument"]
        output.append(arg)
    except KeyError:
        pass
    try:
        msg = elem["Message_content"]["Message"]
        elements_to_handle.append(msg)
    except KeyError:
        pass

out = []
while len(elements_to_handle):
    handle_element(elements_to_handle.pop(), out)

print out

rawDescription: Convert list of nested dictionary to a flat list of dictionary


code:
s = str(Decimal('2.5') * 10)
print s.rstrip('0').rstrip('.') if '.' in s else s
s = str(Decimal('2.5678') * 1000)
print s.rstrip('0').rstrip('.') if '.' in s else s

rawDescription: drop trailing zeros from decimal

description: Drop trailing zeros from decimal


code:
import os
import re
import fnmatch

rule = re.compile(fnmatch.translate('*.txt'), re.IGNORECASE)
print sorted([fname for fname in os.listdir('.') if rule.match(fname)])

rawDescription: Python reading text files for calculation


code:
class A(object):
    def __init__(self, x):
        print "Called A"
        self.a=x

class B(A):
    def __init__(self, x):
        print "Called B"
        super(B,self).__init__(x)
        self.b=x

class C(A):
    def __init__(self, x):
        print "Called C"
        super(C,self).__init__(x)
        self.c=x

class D(B,C):
    def __init__(self, x):
        print "Called D"
        super(D,self).__init__(x)
        self.d=x

foo = D(10)

rawDescription: Call all __init__ but once in Python

description: Call all __init__ but once in Python


code:
for c in reversed(range(10)):
    print c

rawDescription: Counting backward from an integer using Python range built-in function, related to parse trees


code:
a = []
b = []
with open('/Users/settingj/Desktop/NOxMultiplier.csv') as f:
    reader = csv.reader(f, delimiter='\t')
    for row in reader:
        a.append(row[0])
        b.append(row[1])
print a
print b

rawDescription: Parsing with Python

description: Parsing with Python


code:
import string, unicodedata
allowed = string.letters + string.digits
def fold(s):
  s = unicodedata.normalize("NFKD", unicode(s).lower()).encode("ascii", "ignore")
  s = "".join(c for c in s if c in allowed)
  return s

for example in ['abc LLC','xyz, LLC', 'abc , LLC','xyz LLC']:
  print "%r -> %r" % (example, fold(example))

rawDescription: Python string comparison similarity

description: Python string comparison similarity


code:
#!/usr/bin/python

import time

f = open("arduino.txt")
while line in f:
    pass
f.close();
#line is the last line now

items = line.strip().split()

print time.strftime("%Y-%m-%d %H:%M:%S"), items[2]
#Write to the new file here.

rawDescription: Python specific item from last line

description: Python specific item from last line


code:
VALID_KEYS = ["Central Spectrum", "Full Frame"]
results = {}
for key_1 in data.keys():
    # key_1 is '20101021', '20101004'...

    for key_2 in data[key_1].keys():
        # key_2 is '4x4', '4x2'...
        if key_2 not in results:
            results[key_2] = dict.fromkeys(VALID_KEYS, 0)
        for key_3 in data[key_1][key_2].keys():
            # key_3 is 'Central Spectrum', 'Full Frame', 'Custom'...
            if key_3 in VALID_KEYS:
                results[key_2][key_3] += data[key_1][key_2][key_3]
print results

rawDescription: summing nested dictionary entries

description: Summing nested dictionary entries


code:
for i in root.xpath('.//Variable'):
    i.attrib['value']='CHANGE'

print ET.tostring(root,pretty_print=True)

rawDescription: Write data in xml based on attribute key


code:
class IconButton(ButtonBehavior, Image):

    def __init__(self, angle=0, **kwargs):
        super(IconButton, self).__init__(**kwargs)

        self.rotate = Rotate(angle = angle)

        self.canvas.before.add(PushMatrix())
        self.canvas.before.add(self.rotate)
        self.canvas.after.add(PopMatrix())

        self.bind(pos=self.update_canvas)
        self.bind(size=self.update_canvas)

    def update_canvas(self, *args):
        self.rotate.origin = self.center

    def on_press(event):
        event.source = event.img_dn
        print '{} pressed'.format(event.id)

    def on_release(event):
        event.source = event.img_up
        print '{} released'.format(event.id)

rawDescription: Kivy: Rotate clickable image

description: Kivy: Rotate clickable image


code:
impl = getattr(c.service, method)
response = impl('90210')
print response

rawDescription: Unable utilize a methodname(from webservice)present in the form of a string.I tried utilizing getattr,but then not unable to extend it for suds


rawDescription: Generate a Dictionary using two lists

description: Generate a Dictionary using two lists


code:
#!/usr/bin/python
import socket, sys

# Where to connect
host = '127.0.0.1'
port = 1234

if len(sys.argv) < 2:
  print "Must give filename"
  sys.exit(1)

filename = sys.argv[1]
print "Sending %s to Tika on port %d" % (filename, port)

# Connect to Tika
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host,port))

# Open the file to send
f = open(filename, 'rb')

# Stream the file to Tika
while True:
  chunk = f.read(65536)
  if not chunk:
    # EOF
    break
  s.sendall(chunk)

# Tell Tika we have sent everything
s.shutdown(socket.SHUT_WR)

# Get the response
while True:
  chunk = s.recv(65536)
  if not chunk:
    # EOF
    break
  print chunk

rawDescription: python send file to tika running as a service

description: Python send file to tika running as a service


code:
s = u'\u653e'
print type(s), len(s), s
encoded = s.encode('unicode_escape')
print type(encoded), len(encoded), encoded

rawDescription: escape a string which contains non-ascii

description: Escape a string which contains non-ascii


code:
def example():
    # Note: You can use sys.argv here
    print "Hi! I'm a command written in python."

rawDescription: How can a Python module single file be installed using pip and PyPI?


code:
Students = ["student1", "student2", "student3", "student4", "student5"]
Scores = [12,13,8,15,16,19,18,14,3,15,6,3,9,11,13,5,2,18,3,7]

c = 0
Averages = []

for i in range(5):
    s = 0
    for j in range(4):
        s = s + Scores[c]
        c = c+1
    Averages.append(float (s)/4)

print Averages

rawDescription: How to use only a specific amount of elements in a for loop

description: Use only a specific amount of elements in a for loop


code:
import stockquote
print stockquote.get_quote('goog')

rawDescription: How to create a stock quote fetching app in python

description: Create a stock quote fetching app in python


code:
lst = [1, 2, 3, 4]
lst_iter = iter(lst)
for i in lst_iter:
    print i
    if i == 2:
        print 'skipping ahead to', next(lst_iter)

rawDescription: How do I check for next to next line while reading a file in python and strip the newline character at its end?

description: Check for next to next line while reading a file in python and strip the newline character at its end


rawDescription: Nearest Neighbour Algorithm

description: Nearest Neighbour Algorithm


code:
import itertools
import operator

probabilities = {'W':0.837, 'L':0.163}

for product in itertools.product(['W','L'], repeat=3): #3=number of games
    p = reduce(operator.mul,
               [probabilities[p] for p in product])
    print product, ":", p

rawDescription: Itertools to create a list and work out probability


code:
graph={ 0:[1,3,4], 1:[0,2,4], 2:[1,6], 3:[0,4,6], 4:[0,1,3,5], 5:[4], 6:[2,3] }
def bfs(graph, start):
    path = []
    queue = [start]
    while queue:
        vertex = queue.pop(0)
        if vertex not in path:
            path.append(vertex)
            queue.extend(graph[vertex])
    return path

print bfs(graph, 0)

rawDescription: BFS algorithm in Python

description: BFS algorithm in Python


code:
s = 'hello'
t1 = s
t2 = s[:]
print s, t1, t2
s = 'good bye'
print s, t1, t2

rawDescription: Python why would you use [:] over =

description: Python why would you use [:] over =


code:
for key, value in order_dict.iteritems() :
    print key, value

rawDescription: Append key value pair to a dictionary

description: Append key value pair to a dictionary