code:
#!/usr/bin/python3
from fpdf import FPDF
pdf = FPDF()
# important: compression is not yet supported in py3k version
pdf.compress = False
pdf.add_page()
pdf.add_font('Ubuntu', 'B', 'Ubuntu-B.ttf', uni=True)
pdf.set_font('Ubuntu', 'B', 14)
text= "ľšččšžťčšžťšýťčý"
pdf.cell(8,5,text)
pdf.write(8, text)
pdf.ln(8)
pdf.output("unicode.pdf", 'F')

rawDescription: Python3 PDF generator library

description: Python3 PDF generator library


code: wall.sort(key=lambda x: x.date)

rawDescription: Order 2 different querysets by date

description: Order 2 different querysets by date


code:
def string_in_list(s, entities):
    idx = 0
    while idx < len(entities):
        if s in entities[idx]:
            yield entities[idx]
        idx += 1
        if idx >= len(entities):
            # restart from the beginning
            idx = 0
huge_list = ['you', 'say', 'hello']
m = string_in_list('y', huge_list)
next(m)  # you
next(m)  # say
next(m)  # you, again

rawDescription: Find + Find next in Python

description: Find + Find next in Python


code:
X = range(Y.size)
Xlabels = [0,0.1,0.3,0.5]
plt.xticks(X, Xlabels)
plt.plot(X, Y)

rawDescription: Matplotlib - Resizing x-axis

description: Matplotlib - Resizing x-axis


code:
graph.vs["size"] = rescale(graph.degree(), out_range=(0, 10))
plot(g)

rawDescription: How to use igraph python's metamagic class?

description: Use igraph python's metamagic class


code:
from itertools import product
yourlist = [[0], [1, 6], [2, 3], [2, 3], [], [4]]
for combo in product(*filter(None, yourlist)):
    print(combo)

rawDescription: getting all possible cases in Python3

description: Getting all possible cases in Python3


rawDescription: What are "named tuples" in Python?

description: What are "named tuples" in Python


code:
with open('fileD.txt') as f:
    for line in f:
        last_two_nums = line.split()[1][-5:].split('.') # gives for ex: ['17', '13']
        # do what you've got to do here

rawDescription: extracting IP values

description: Extracting IP values


rawDescription: How can I get first n bits of floating point number as integer in python

description: Get first n bits of floating point number as integer in python


code:
#/usr/bin/python

from random import sample, choice
from cPickle import load

dataset_filename = 'dataset.pkl'

class DataSearch(object):
  def __init__(self,filename):
    self.data = load(open(filename,'rb'))
    self.col_sets = [ dict() for x in self.data[0] ]
    self.process_data()
  def process_data(self):
    for row in self.data:
      for i,v in enumerate(row):
        self.col_sets[i].setdefault(v,set()).add(row)
  def search(self,*args):
    # args are integers, sequences of integers, or None in related column positions.
    results = []
    for i,v in enumerate(args):
      if v is None:
        continue
      elif isinstance(v,int):
        results.append(self.col_sets[i].get(v,set()))
      else: # sequence
        r = [ self.col_sets[i].get(x,set()) for x in v ]
        r = reduce(set.union,r[1:],r[0])
        results.append(r)
    #
    results.sort(key=len)
    results = reduce(set.intersection,results[1:],results[0])
    return results
  def sample_search(self,*args):
    search = []
    for i,v in enumerate(args):
      if v is None:
        search.append(None)
      else:
        search.append(sample(self.col_sets[i].keys(),v))
    return search

d = DataSearch(dataset_filename)

rawDescription: What's the fastest way to filter a table of integers in Python?


code:
(slice(None, None, None),
 slice(None, 2, None),
 slice(None, None, None),
 slice(None, 540, None))

rawDescription: NumPy slice notation in a dictionary

description: NumPy slice notation in a dictionary


code:
collected = {}
for record in records:
    descr = record.description.split("_")[1]
    try:
        collected[descr].append(record)
    except KeyError:
        collected[descr] = [record ,]

rawDescription: Extract sequences from a FASTA file to multiple files, file based on header_IDs in a separate file


code: ['text', 'dummy_short-tags', 'full length long tag']

rawDescription: Optional closing symbol and several words capturing with regex


code:
word = 'fooBARbazaar'
counts = {}
for character in word.upper():
    # count the character.
    counts[character] = counts.get(character, 0) + 1

counts
max(counts, key=counts.__getitem__)

rawDescription: How to apply a for loop to a string in Python

description: Apply a for loop to a string in Python


code:
class TEST:
    def __init__(self):
        self._x = 6
    def x(self):
        return(self._x)

rawDescription: Understanding Python classes

description: Understanding Python classes


code: colors = cmap(np.linspace(0, 1, len(names)))

rawDescription: Elegant way to match a string to a random color matplotlib


code:
id = 1
for x in range(1, 4):
    studentID = "Bart: {}".format(id)
    print(studentID)
    id += 1

rawDescription: Python Increment Int variable in a String


code:
import pandas
import numpy

a = numpy.random.rand(10, 10)>0.5
b = pandas.DataFrame(a, columns=list('ABCDEFGHIJ'))
selectedcolumns = ['A', 'B', 'C']
b['Anded'] = b[selectedcolumns].all(1)

rawDescription: Efficient boolean operation of a pandas series object on a pandas dataframe object


rawDescription: How do I escape closing '/' in HTML tags in JSON with Python?

description: Escape closing '/' in HTML tags in JSON with Python


code:
import numpy as np
import cv2

def hitmiss(src, kernel):
    im = src / 255
    k1 = (kernel == 1).astype('uint8')
    k2 = (kernel == -1).astype('uint8')
    e1 = cv2.erode(im, k1, borderType=cv2.BORDER_CONSTANT)
    e2 = cv2.erode(1-im, k2, borderType=cv2.BORDER_CONSTANT)
    return e1 & e2

if __name__ == "__main__":
    im = cv2.imread('blobs.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)
    _, im_binary = cv2.threshold(im, 50, 255, cv2.THRESH_BINARY)

    kernel = np.array([[-1,-1, 1],
                       [-1, 1, 1],
                       [-1,-1, 1]])

    im_mask = np.zeros(im_binary.shape, np.uint8)

    im_mask |= hitmiss(im_binary, kernel)
    im_mask |= hitmiss(im_binary, np.fliplr(kernel))
    im_mask |= hitmiss(im_binary, kernel.T)
    im_mask |= hitmiss(im_binary, np.flipud(kernel.T))

    im_dst = im_binary & ((1 - im_mask) * 255)
    cv2.imwrite('dst.png', im_dst)

rawDescription: How to remove single pixels on the borders of a blob?

description: Remove single pixels on the borders of a blob


code:
container = soup.find("div", class_="container")
for link in container.select("a[href*=price"):
    print(link["href"])

rawDescription: Navigation with BeautifulSoup

description: Navigation with BeautifulSoup


rawDescription: Django Query including a table where no relation can be followed


code:
df = pd.DataFrame(np.random.randn(3), ind, ['a'])  # ind = df.index
df2 = pd.DataFrame(np.random.randn(3), ind2, ['b'])  # ind2 = df2.index
df.reindex(ind & ind2)

rawDescription: DataFrame algebra in Pandas

description: DataFrame algebra in Pandas


code:
import numpy as np

y_true = np.array([[0,1,0],
                   [0,1,1],
                   [1,0,1],
                   [0,0,1]])

y_pred = np.array([[0,1,1],
                   [0,1,1],
                   [0,1,0],
                   [0,0,0]])

def hamming_score(y_true, y_pred, normalize=True, sample_weight=None):
    '''
    Compute the Hamming score (a.k.a. label-based accuracy) for the multi-label case
    http://stackoverflow.com/q/32239577/395857
    '''
    acc_list = []
    for i in range(y_true.shape[0]):
        set_true = set( np.where(y_true[i])[0] )
        set_pred = set( np.where(y_pred[i])[0] )
        #print('\nset_true: {0}'.format(set_true))
        #print('set_pred: {0}'.format(set_pred))
        tmp_a = None
        if len(set_true) == 0 and len(set_pred) == 0:
            tmp_a = 1
        else:
            tmp_a = len(set_true.intersection(set_pred))/\
                    float( len(set_true.union(set_pred)) )
        #print('tmp_a: {0}'.format(tmp_a))
        acc_list.append(tmp_a)
    return np.mean(acc_list)

if __name__ == "__main__":
    print('Hamming score: {0}'.format(hamming_score(y_true, y_pred))) # 0.375 (= (0.5+1+0+0)/4)

    # For comparison sake:
    import sklearn.metrics

    # Subset accuracy
    # 0.25 (= 0+1+0+0 / 4) --> 1 if the prediction for one sample fully matches the gold. 0 otherwise.
    print('Subset accuracy: {0}'.format(sklearn.metrics.accuracy_score(y_true, y_pred, normalize=True, sample_weight=None)))

    # Hamming loss (smaller is better)
    # $$ \text{HammingLoss}(x_i, y_i) = \frac{1}{|D|} \sum_{i=1}^{|D|} \frac{xor(x_i, y_i)}{|L|}, $$
    # where
    #  - \\(|D|\\) is the number of samples
    #  - \\(|L|\\) is the number of labels
    #  - \\(y_i\\) is the ground truth
    #  - \\(x_i\\)  is the prediction.
    # 0.416666666667 (= (1+0+3+1) / (3*4) )
    print('Hamming loss: {0}'.format(sklearn.metrics.hamming_loss(y_true, y_pred)))

rawDescription: Getting the accuracy for multi-label prediction in scikit-learn


code:
from random import shuffle
guess(1, 0)
guess(1, 1)

rawDescription: List index out of range even though I'm asking for [0]?


code:
import sqlite3
connection = sqlite3.connect('genes.db')
cursor = connection.cursor()

# create a table for inserting the data
create = ('CREATE TABLE genes (A text, B text, C text, D text, E text)')
cursor.execute(create)

difffile = """\
one two three four five six seven eight nine ten
a b c d e f g h i j
k l m n o p q r s t\
"""

# rows list for storing the data to insert
rows = []

# loop over lines in difffile
for line in difffile.split("\n"):

    # split the current line by whitespace
    fields = line.split()

    # skip over lines with less than 10 columns
    if len(fields) < 10:
        continue

    # columns we want
    cols = (1, 6, 7, 8, 10)

    # create a row tuple with the data we want
    row = tuple([fields[col - 1] for col in cols])

    # add the current row to our rows list
    rows.append(row)

# insert row list into the database table "genes"
cursor.executemany("INSERT INTO genes VALUES(?, ?, ?, ?, ?)", rows)

# test the insert by dumping the database table genes:
results = cursor.execute("SELECT * FROM genes")
for r in results.fetchall():
    print(r)

rawDescription: Creating an sqlite database with a diff file


code: months = lambda a, b: abs((a.year - b.year) * 12 + a.month - b.month)

rawDescription: Subtract dates in Django?

description: Subtract dates in Django


code:
from sortedcontainers import sortedlistwithkey

array = sortedlistwithkey.SortedListWithKey(key=lambda x: -x[-1])
n = 3
for row in rows:
    array.add(row)
    if len(array) > n:
        array.pop()
print(array.as_list())

rawDescription: How to store lists over criteria in Python?

description: Store lists over criteria in Python


code:
phyLineN= []
phyLineN.append([])
phyLineN[0]= []
phyLineN[0].append([ "....", ".", "....",".", "...." ])
phyLineN.append([])
phyLineN[1]= []
phyLineN[1].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[2]= []
phyLineN[2].append([ ".   ", ".", "    ",".", "    " ])
phyLineN[2].append([ ".   ", " ", "    "," ", "    " ])
phyLineN.append([])
phyLineN[3]= []
phyLineN[3].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[4]= []
phyLineN[4].append([ ".   ", ".", "    ",".", "    " ])
phyLineN[4].append([ ".   ", " ", "    "," ", "    " ])
phyLineN.append([])
phyLineN[5]= []
phyLineN[5].append([ ".   ", " ", ". . "," ", "    " ])
phyLineN.append([])
phyLineN[6]= []
phyLineN[6].append([ "....", ".", "....",".", "...." ])

phyLineY= []
phyLineY.append([])
phyLineY[0]= []
phyLineY[0].append([ "    ", " ", "    "," ", "    " ])
phyLineY.append([])
phyLineY[1]= []
phyLineY[1].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[2]= []
phyLineY[2].append([ "    ", "H", "    ","H", "    " ])
phyLineY[2].append([ "    ", "H", "    ","H", "    " ])
phyLineY.append([])
phyLineY[3]= []
phyLineY[3].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[4]= []
phyLineY[4].append([ "    ", "H", "    ","H", "    " ])
phyLineY[4].append([ "    ", "H", "    ","H", "    " ])
phyLineY.append([])
phyLineY[5]= []
phyLineY[5].append([ "    ", " ", "===="," ", "    " ])
phyLineY.append([])
phyLineY[6]= []
phyLineY[6].append([ "    ", " ", "    "," ", "    " ])

def printNumberNY(num,structN,structY):

    phyRowH= [ len(structN[0]), len(structN[1]), len(structN[2]), len(structN[3]), len(structN[4]), len(structN[5]), len(structN[6]) ]

    # Validate structure and compute phyColW
    # This could be moved to an object constructor so is computed only once
    first= 1
    for line in structN :
        for phyLine in line :
            if first :
                phyColW= [ len(phyLine[0]), len(phyLine[1]), len(phyLine[2]), len(phyLine[3]), len(phyLine[4]) ]
                first= 0
            else:
                for i, _ in enumerate(phyLine) :
                    if len(phyLine[i]) != phyColW[i] : raise "Inconsistent physical column width"

    # Real rendering of the (full) number in 7-segment form
    num= str(num)
    for linenum, linetype in enumerate(typeforline) :
        for phyLine in range(phyRowH[linenum]) :
            line= ""
            for d in num :
                for col, qq in enumerate(phyColW) :
                    if digits[int(d)].lines[linenum][col] != " " :
                        line+= structY[linenum][phyLine][col]
                    else:
                        line+= structN[linenum][phyLine][col]
            print( line )

printNumberNY(3475649560458,phyLineN,phyLineY)

rawDescription: Calculator 7 Segment Display w/ Width

description: Calculator 7 Segment Display w/ Width


code:
data = [[np.random.normal(i, 1, 30) for i in range(2)],
        [np.random.normal(i, 1.5, 30) for i in range(3)],
        [np.random.normal(i, 2, 30) for i in range(4)]]

grouped_boxplots(data)
plt.show()

rawDescription: Pyplot boxplots centered around xticks

description: Pyplot boxplots centered around xticks


code: linregress(df['col_X'], df['col_Y'])

rawDescription: How to apply linregress in Pandas bygroup

description: Apply linregress in Pandas bygroup


code: zip(a, [str(i) for i in b])

rawDescription: How to multiply python string and integer arrays

description: Multiply python string and integer arrays


code:
a = Position(1,2)
b = Position(1,2)
c = Position(2,3)
a == b
a == c
b == c
a != a
a != b
a != c

rawDescription: Python 3.2.2 custom object comparation fails


code:
class MemoryManager(object):
    def __init__(self):
        self.cache = {}
        self.buffer = False

    def __enter__(self):
        self.buffer = True

    def __store(self):
        # store data
        pass

    def __setitem__(self, key, value):
        self.cache[key] = value
        if not self.buffer:
            self.__store()

    def __exit__(self, exc_type, exc_value, traceback):
        self.__store()
        self.buffer = False

rawDescription: Python disable while iterating

description: Python disable while iterating


code:
client = ec2.meta.client

response = client.reboot_instances(InstanceIds=[...])

rawDescription: How can I access a low-level client from a Boto 3 resource instance?

description: Access a low-level client from a Boto 3 resource instance


code: 'Hello!\nthis is a test string\nyay.'

rawDescription: List of lists chunked into sublists

description: List of lists chunked into sublists


code:
class UserForm(Form):
    username = StringField('Username', validators=[DataRequired()])
    password = PasswordField('Password', validators=[DataRequired()])
    job = SelectField(
        'Job',
        validators=[DataRequired()]
    )

    def __init__(self, *args, **kwargs):
        super(UserForm, self).__init__(*args, **kwargs)
        self.job.choices = [(a.id, a.name) for a in Job.query.order_by(Job.name)]

rawDescription: Dynamic select field using WTForms not updating


rawDescription: Writing DataFrame column to a file

description: Writing DataFrame column to a file


rawDescription: Zipping specific elements of a list in Python


code:
import lxml.etree as ET
root = ET.fromstring(s)
for target in root.xpath('.//Department/orgID[text()="126"]'):
    d = {
        dept.find('name').text: int(dept.find('orgID').text)
        for dept in target.xpath('ancestor-or-self::Department')
    }
    print(d)

rawDescription: get all parents of xml node using python

description: Get all parents of xml node using python


code:
class Values():
    def __init__(self, *attrs):
        '''each element in attrs is a tuple(attrname, value)'''
        if attrs is not None:
            for a in attrs:
                n, v = a
                setattr(self, n, v)

    def add_one(self, attr):
        a = getattr(self, attr, None)
        if a:
            if isinstance(a, tuple):
                setattr(self, attr, tuple(e + 1 for e in a))
            else:
                setattr(self, attr, a + 1)

v = Values(('x', 1), ('y', 2), ('z', 3), ('t', (7, 8, 9)))
print('Initial values:', v.x, v.y, v.z, v.t)
v.add_one('x')
print('X modified:', v.x, v.y, v.z, v.t)
v.add_one('t')
print('T modified:', v.x, v.y, v.z, v.t)
v.add_one('a')
print('Tried to modify non-existent attribute "a"', v.x, v.y, v.z, v.t)

rawDescription: Python: preferred way to handle large number of attributes that might not exists?


rawDescription: Filtering through multiple xpath elements on web page


rawDescription: An easily refreshable Queue for Python Threading


code:
from bs4 import BeautifulSoup
import requests

base_url = '?'.join(['http://www.fashionguide.com.tw/Beauty/08/MsgL.asp',
                     'LinkTo=TopicL&TopicNum={topic}&Absolute=1'])
topics = [13976, 13996, ]

for t in topics:
    url = base_url.format(topic=t)
    page_content = requests.get(url).content  # returns bytes
    soup = BeautifulSoup(page_content, 'lxml')
    title1 = soup.find("span", attrs={"class": "style1", "itemprop": "brand"})
    title2 = soup.find("span", attrs={"class": "style1", "itemprop": "name"})
    print(title1.text + title2.text)

rawDescription: How to scrape Traditional Chinese text with beautifulsoup?

description: Scrape Traditional Chinese text with beautifulsoup


code: [[foo(i,j) for j in range(i+1,COUNT)] for i in range(0,COUNT)]

rawDescription: test each object in a list against every other object in the same list exactly once using python

description: Test each object in a list against every other object in the same list exactly once using python


rawDescription: Acquiring the Minimum array out of Multiple Arrays by order in Python


code:
for foo in ('bar', 'bazbaz'):
    for character in foo:
        print(character, foo)

rawDescription: Confused about how lists or os.listdir() work in Python 3


code:
for j in range(2*i, prime_max, i):
    number_list[j] = False

rawDescription: Array initialized with numpy is much slower in a loop with if statement


rawDescription: How do I avoid looping through an image pixel by pixel with opencv & numpy

description: Avoid looping through an image pixel by pixel with opencv & numpy


code:
text = span.get_text(strip=True)
if text == 'ABSTRACT':
    my_file['Abstract'] = div
if text == 'Keywords':
    my_file['Keywords'] = div
if text == 'REFERENCES':
    my_file['References'] = div

rawDescription: how to get a parent of an <span> tag containing a specific text

description: Get a parent of an <span> tag containing a specific text


code:
import numpy as np
import scipy.cluster.hierarchy as hac
import matplotlib.pyplot as plt

a = np.array([[0.1,   2.5],
              [1.5,   .4 ],
              [0.3,   1  ],
              [1  ,   .8 ],
              [0.5,   0  ],
              [0  ,   0.5],
              [0.5,   0.5],
              [2.7,   2  ],
              [2.2,   3.1],
              [3  ,   2  ],
              [3.2,   1.3]])

fig, axes23 = plt.subplots(2, 3)

for method, axes in zip(['single', 'complete'], axes23):
    z = hac.linkage(a, method=method)

    # Plotting
    axes[0].plot(range(1, len(z)+1), z[::-1, 2])
    knee = np.diff(z[::-1, 2], 2)
    axes[0].plot(range(2, len(z)), knee)

    num_clust1 = knee.argmax() + 2
    knee[knee.argmax()] = 0
    num_clust2 = knee.argmax() + 2

    axes[0].text(num_clust1, z[::-1, 2][num_clust1-1], 'possible\n<- knee point')

    part1 = hac.fcluster(z, num_clust1, 'maxclust')
    part2 = hac.fcluster(z, num_clust2, 'maxclust')

    clr = ['#2200CC' ,'#D9007E' ,'#FF6600' ,'#FFCC00' ,'#ACE600' ,'#0099CC' ,
    '#8900CC' ,'#FF0000' ,'#FF9900' ,'#FFFF00' ,'#00CC01' ,'#0055CC']

    for part, ax in zip([part1, part2], axes[1:]):
        for cluster in set(part):
            ax.scatter(a[part == cluster, 0], a[part == cluster, 1],
                       color=clr[cluster])

    m = '\n(method: {})'.format(method)
    plt.setp(axes[0], title='Screeplot{}'.format(m), xlabel='partition',
             ylabel='{}\ncluster distance'.format(m))
    plt.setp(axes[1], title='{} Clusters'.format(num_clust1))
    plt.setp(axes[2], title='{} Clusters'.format(num_clust2))

plt.tight_layout()
plt.show()

rawDescription: Tutorial for scipy.cluster.hierarchy

description: Tutorial for scipy.cluster.hierarchy


rawDescription: Conditional regular expression to split on commas


rawDescription: remove all elements in list that do not end in a number in python

description: Remove all elements in list that do not end in a number in python


rawDescription: Python/Pandas subtracting numbers in a column


code:
import sys

sys.stdout = open("test-output.txt", "wt")

# Import the rest

rawDescription: Capturing log information from bunch of python functions in single file


code:
class MyClass(object):
    """
    Description for class

    """

    def __init__(self, par1, par2):
        self.var1 = par1 #: initial value: par1
        self.var2 = par2 #: initial value: par2

    def method(self):
        pass

rawDescription: how to show instance attributes in sphinx doc?

description: Show instance attributes in sphinx doc


rawDescription: How to convert a sympy polynomial into a list?

description: Convert a sympy polynomial into a list


rawDescription: How to use distinct with pipeline in mongodb using python

description: Use distinct with pipeline in mongodb using python


code:
import pandas as pd

# split into lines creating an iterator so we don't have to slice.
spl = iter(d["visualLogs"][0].rstrip().split("\r"))

# split first line to get the  column names.
columns = next(spl).split(",")

# split remaining lines into individual rows, removing empty row.
rows = [v for v in (sub_str.split(",") for sub_str in spl) if len(v) > 1]

df = pd.DataFrame(data=rows, columns=columns)

rawDescription: how to extract python data frame from JSON string

description: Extract python data frame from JSON string


code:
import  pandas as pd

df = pd.DataFrame([sub.split(",") for sub in l])
print(df)

rawDescription: Python convert comma separated list to pandas dataframe


code:
names = f.__code__.co_names
# ('call_some_function_A', 'call_some_function_B', 'call_some_function_Z')

rawDescription: Executing a function in reverse in Python


code:
#!/usr/bin/env python2

from sys import argv
from os import devnull
from subprocess import Popen

with open(devnull, "w") as fnull:
    Popen(argv[1:], stdout = fnull, stderr = fnull, close_fds=True)

rawDescription: Unable to drop privileges for a new process with QProcess in PyQt4


code:
name1 = "John"
name2 = "Mary"
"hello, #{name1}.  Where is #{name2}?"

rawDescription: What is Ruby equivalent of Python's `s= "hello, %s. Where is %s?" % ("John","Mary")`


code:
# Import relevant modules
import numpy as np
import cv2

# Define points
pts = np.array([[542, 107], [562, 102], [582, 110], [598, 142], [600, 192], [601, 225], [592, 261], [572, 263], [551, 245], [526, 220], [520, 188], [518, 152], [525, 127], [524, 107]], dtype=np.int32)

### Define image here
img = 255*np.ones((300, 700, 3), dtype=np.uint8)

# Initialize mask
mask = np.zeros((img.shape[0], img.shape[1]))

# Create mask that defines the polygon of points
cv2.fillConvexPoly(mask, pts, 1)
mask = mask.astype(np.bool)

# Create output image (untranslated)
out = np.zeros_like(img)
out[mask] = img[mask]

# Find centroid of polygon
(meanx, meany) = pts.mean(axis=0)

# Find centre of image
(cenx, ceny) = (img.shape[1]/2, img.shape[0]/2)

# Make integer coordinates for each of the above
(meanx, meany, cenx, ceny) = np.floor([meanx, meany, cenx, ceny]).astype(np.int32)

# Calculate final offset to translate source pixels to centre of image
(offsetx, offsety) = (-meanx + cenx, -meany + ceny)

# Define remapping coordinates
(mx, my) = np.meshgrid(np.arange(img.shape[1]), np.arange(img.shape[0]))
ox = (mx - offsetx).astype(np.float32)
oy = (my - offsety).astype(np.float32)

# Translate the image to centre
out_translate = cv2.remap(out, ox, oy, cv2.INTER_LINEAR)

# Determine top left and bottom right of translated image
topleft = pts.min(axis=0) + [offsetx, offsety]
bottomright = pts.max(axis=0) + [offsetx, offsety]

# Draw rectangle
cv2.rectangle(out_translate, tuple(topleft), tuple(bottomright), color=(255,0,0))

# Show image, wait for user input, then save the image
cv2.imshow('Output Image', out_translate)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('output.png', out_translate)

rawDescription: Extracting polygon given coordinates from an image using OpenCV


rawDescription: Python - How do I make a dictionary inside of a text file?


code:
idx_1s, = np.where(x == -1)  # find the positions of the -1's
idx1s, = np.where(x == 1)  # find the positions of the 1's

rawDescription: Python/Numpy - Fill gaps between non-consecutive points?


code:
def count_number(file_dict, num):
    count = 0
    for set_name, number_set in file_dict.iteritems():
        count += number_set.get(num, 0)

    return count

rawDescription: Comparing two lists items in python

description: Comparing two lists items in python


code:
def average():
    inputnum = 0
    numlist = []
    while inputnum <=100:
        inputnum = input("Please input a number: ")
        inputnum = float(inputnum)
        if inputnum <= 100:
            numlist.append(inputnum)
    average = sum(numlist)/len(numlist)
    print(average)

average() # finally call the function

rawDescription: Average function in Python

description: Average function in Python


code: del cursor

rawDescription: How to close cursor in MongoKit

description: Close cursor in MongoKit


code:
MANIFEST = '__openerp__.py'

# ...

terp_file = mod_path and opj(mod_path, MANIFEST) or False

# ...

f = tools.file_open(terp_file)
try:
    info.update(eval(f.read()))
finally:
    f.close()

rawDescription: Import dict without name from a module

description: Import dict without name from a module


code:
urls = ['http://example.com/foo',
        'http://example.com/bar']
results = [list() for _ in urls]
results_lock = threading.Lock()

def download(url, i):
    with urllib.request.urlopen(url) as f:
        result = f.read()
    with results_lock:
        results[i] = url

pool = ThreadPool(max_workers=8)
pool.start()
for i, url in enumerate(urls):
    pool.submit(functools.partial(download, url, i))
pool.stop()

result = b''.join(results)

with open('output_file', 'wb') as f:
    f.write(result)

rawDescription: how to download multiple file simultaneously and join them in python?

description: Download multiple file simultaneously and join them in python


code:
from decimal import *
from re import *

rawDescription: Python Module Package with no methods

description: Python Module Package with no methods


code: zip(map(abs, entry[1][0]), map(abs, entry[1][1]))

rawDescription: Exporting a modified list to CSV

description: Exporting a modified list to CSV


code:
def Display(self):
    self.imageDisplay = PhotoImage(file='C:\image.gif')

    wrange = xrange(0, self.imageDisplay.width())
    hrange = xrange(0, self.imageDisplay.height())
    get = self.imageDisplay.get
    put = self.imageDisplay.put

    def convert_pixel(raw):
        return ('#%02x%02x%02x' %
            tuple(255 - int(component) for component in raw.split(' ')))

    for y in hrange:
        put('{' + ' '.join(convert_pixel(get(x, y)) for x in wrange) + '}', (0, y))

    c = Canvas(self, width=700, height=700); c.pack()
    c.grid(padx=0,pady=0, column=0, row=0)
    c.create_image(0,0, image = self.imageDisplay, anchor = NW)

rawDescription: Optimization of color inversion - Python Tkinter library


code:
import scipy
import scipy.cluster.hierarchy as sch
X = scipy.randn(100, 2)     # 100 2-dimensional observations
d = sch.distance.pdist(X)   # vector of (100 choose 2) pairwise distances
L = sch.linkage(d, method='complete')
ind = sch.fcluster(L, 0.5*d.max(), 'distance')

rawDescription: How to get flat clustering corresponding to color clusters in the dendrogram created by scipy

description: Get flat clustering corresponding to color clusters in the dendrogram created by scipy


code:
d = {'file1.txt': ['a', 'b', 'c'],
     'file2.txt': ['d', 'e', 'f'],
     'file3.txt': ['g', 'h', 'i']}

for filename, lst in d.iteritems():
    if lst:
        with open(os.path.join(dirpath, filename), 'w') as f:
            for elem in lst:
                f.write(elem)

rawDescription: dealing with multiple lists

description: Dealing with multiple lists


code:
def mainWork():
    # do stuff here
    with lock:
        if 'di_unread_inbox' in inbox_page:
            inboxmessagelabel.set("some label")

    # do other stuff

rawDescription: lock all threads but one in python

description: Lock all threads but one in python


code: #!/usr/bin/env python

rawDescription: Trying use a .pth file to add a path in documents folder on Mac


code:
result = {}
if search.is_valid():
    success = True
    subject = search.cleaned_data['search']
    sourceCode = urllib2.urlopen("http://finance.yahoo.com/q/ks?s="+subject).read()
    pbr = sourceCode.split('Price/Book (mrq):	')[1].split('	')[0]
    result['pbr'] = pbr
    result['search'] = search

rawDescription: Displaying view variables in template with Django?


code: ws = wb.add_worksheet(data['ATR'])

rawDescription: Switching between worksheets

description: Switching between worksheets


code:
input = ['canter creant cretan nectar', 'opt top pot',
         'subessential suitableness']
sorted(input, key=lambda l: len(l.split(None, 1)[0]), reverse=True)

rawDescription: List ordering in Python. Very simple

description: List ordering in Python. Very simple


code: x, key, y = line.split()

rawDescription: Python - Extracting Text From a File

description: Python - Extracting Text From a File


code:
def convert(l):
    return tuple(convert(x) for x in l) if type(l) is list else l

rawDescription: convert lists to tuples converting also the lists inside the list

description: Convert lists to tuples converting also the lists inside the list


code:
import csv
with open("source","rb") as source:
    rdr= csv.reader( source )
    with open("result","wb") as result:
        wtr= csv.writer( result )
        for r in rdr:
            wtr.writerow( (r[0], r[1], r[3], r[4]) )

rawDescription: Deleting columns in a CSV with python

description: Deleting columns in a CSV with python


code:
FEED_EXPORTERS = {
    'csv': 'importable.path.to.CsvOptionRespectingItemExporter',
}

rawDescription: Modifiying CSV export in scrapy

description: Modifiying CSV export in scrapy


code: df['col'] = 'str' + df['col'].astype(str)

rawDescription: Append string to the start of each value in a said column of a pandas dataframe (elegantly)


code:
import glob

associatedFiles = {}
for fileName in glob.glob('E:\\New_Fields\\*.*'):
    fileID = fileName.split('\\')[-1].split('_')[0]
    if fileID not in associatedFiles:
        associatedFiles[fileID] = [fileName]
    else:
        associatedFiles[fileID].append(fileName)

rawDescription: Open a second file in a loop

description: Open a second file in a loop


code:
import pyexiv2
import json

metadata = pyexiv2.ImageMetadata(filename)
metadata.read()
userdata={'Category':'Human',
          'Physical': {
              'skin_type':'smooth',
              'complexion':'fair'
              },
          'Location': {
              'city': 'london'
              }
          }
metadata['Exif.Photo.UserComment']=json.dumps(userdata)
metadata.write()

rawDescription: Writing complex custom metadata on images through python


code:
def keys_activate():
    turtle.onkey(setChar1, '1')
    turtle.onkey(setChar2, '2')
    turtle.onkey(setChar3, '3')
    turtle.listen()

def keys_deactivate():
    turtle.onkey(None, '1')
    turtle.onkey(None, '2')
    turtle.onkey(None, '3')

rawDescription: Python - Turtle Stop Listening

description: Python - Turtle Stop Listening


code: x = list(range(10+1))  # if you want a real list

rawDescription: Python Sequence of Numbers

description: Python Sequence of Numbers


rawDescription: Easy way to store multiple data in Python 3


code:
class My(object):
    def __init__(self):
        self.device = self

rawDescription: How to use __setattr__ correctly, avoiding infinite recursion

description: Use __setattr__ correctly, avoiding infinite recursion


code:
percentages = np.array([25., 50., 75.])
values = np.floor(255*percentages/100)
arr += values   # the first will be added to the first channel, etc.

rawDescription: Image from array and image using point

description: Image from array and image using point


rawDescription: Special cosine between scipy sparse matrices


rawDescription: Built-in method to Generate Random Strings of Fixed Length From Given Characters


code:
class GameOver(Exception):
    pass

class Quit(Exception):
    pass

def main():
    if game_over:
        raise GameOver
    if quit:
        raise Quit
    else:
        return value

while True:
    try:
        main()
    except GameOver:
        game_over()
    except Quit:
        break

rawDescription: High-level function returning low-level function in python?


code: [operator.lt ,operator.le , operator.eq]

rawDescription: How to transform a arithmetic expression to a Python arithmetic function?

description: Transform a arithmetic expression to a Python arithmetic function


code:
foo="Won 1, 2, 3, 4, Lost 5, 6, 7, 8"
[x for x in foo if x.isdigit()]

rawDescription: Regex to get digits in a certain format

description: Regex to get digits in a certain format


code:
import itertools

myCounter = itertools.count(5)
for item in myList:
    print("la la la.", next(myCounter), "foo foo foo", next(myCounter))

rawDescription: How would a pythonista code the equivalent of the ++ increment operator in Python 3?


code:
terms_elements = terms_and_conditions_soup.select(".popBodyText")[0]
terms = terms_elements.get_text('\n', strip=True)

rawDescription: Scrape text after every <br> - BeautifulSoap


code:
def myFun(*args):
    return [int(''.join(map(str, l))) for l in zip(*args)]

rawDescription: Arbitrary number of assignments (lists (of numbers)) converted into single list


code:
def sort_drm(drm_folder, all_songs=[]):
    os.mkdir('/drm_collection')
    known_types = []
    for root, dirs, files in os.walk(drm_folder):
    	for name in files:
    		full_name = os.path.join(root, name)
    		all_songs.append(full_name)
    for item in all_songs:
    	if item[-3:] not in known_types:
    		known_types.append(item[-3:])
    for item in known_types:
    	os.mkdir('/drm_collection/'+item)
    for item in all_songs:
    	shutil.copy2(item, '/drm_collection/'+item[-3:])

rawDescription: Sorting music

description: Sorting music


rawDescription: Retrieve clicked list item in Django

description: Retrieve clicked list item in Django


rawDescription: create csv file on each run

description: Create csv file on each run


code:
def score(request):
   if request.is_ajax():
     if request.method == 'GET':
        user = UserProfile.objects.get(id=request.GET.get('userid'))
        user.score = request.GET.get('total')
        user.save()
        return HttpResponse("%s" % user.score )

rawDescription: Simple ajax request to save variable to django backend from template


code:
amount = Decimal(input('Enter amount: '))
currency1 = read_currency('Enter source currency: ')
currency2 = read_currency('Enter target currency: ')
rate = conversion_rate(currency1, currency2)
print('{} {} is equal to {} {}'
      .format(currency1, amount, currency2, amount * rate))

rawDescription: Make sure input is limited to variables

description: Make sure input is limited to variables


rawDescription: not all duplicates being removed

description: Not all duplicates being removed


code:
@app.route("/foo")
def foo():
    return flask.render_template("bar.html", text=footext.split('\n'))

rawDescription: disabling autoescape in flask

description: Disabling autoescape in flask


code: print(*L)

rawDescription: Print a list of space-separated elements in Python 3


rawDescription: python pandas time series select day of year

description: Python pandas time series select day of year


code:
user1 = User.query.filter_by(id=1).first()
user1.friends
user2 = User.query.filter_by(id=2).first()
user1.friends.append(user2)
user1.friends
user1.friends[0].added_by

rawDescription: Adding data to related table with SQLAlchemy


code: [" ".join(n.split(", ")[::-1]) for n in namelist]

rawDescription: Switch Lastname, Firstname to Firstname Lastname inside List


code: df['c'] = df.loc[df['b']=='x','a'].apply(lambda v: v if v < len(df) else np.nan)

rawDescription: pandas get last value of column x when column y is equal to z

description: Pandas get last value of column x when column y is equal to z


code:
@register.filter
def is_in_group(user, group_name):
    return user.groups.filter(name=group_name).exists()

rawDescription: Django idiomatic group test in templates


code: sorted(a, key=lambda word: [alphabet.index(c) for c in word])

rawDescription: Sorting string values according to a custom alphabet in Python


code:
small_names = filter(lambda n: len(n)<=4, names)
#equivalent to: small_names = [n for n in names if len(n) <=4]

print(small_names) # ['jake', 'Brad', 'Tony']

rawDescription: Python: how to create a list from elements that don't meet a certain condition


code:
class PostIcon(models.Model):
    post = models.ForeignKey(Post)
    icon = models.ForeignKey(Icon)

rawDescription: Way to allow for duplicate many-to-many entries in Python/Django


code:
from django.db import models

class Message(models.Model):
    type = models.IntegerField(default=1)
    message_text = models.CharField(max_length=500)

    @classmethod
    def random_by_type(cls, type_number):
        return cls.objects.filter(type=type_number).order_by('?').first()

rawDescription: Python - most efficient way to select a random message according to a type


code:
slist = ["A", "C", "E"]
df[df["A"].isin(slist)]

rawDescription: Not able to use 'if x in list' using pandas


rawDescription: Printing X-type pattern in Python 2.7

description: Printing X-type pattern in Python 2.7


code:
from matplotlib.colors import LogNorm

fig, ax = plt.subplots()
mesh = ax.pcolormesh(xbins, ybins, values, cmap='gist_earth', norm=LogNorm())

ax.axis('tight')
ax.set(title='Log Normalized Plot of Values')
fig.colorbar(mesh)

plt.show()

rawDescription: python, matplotlib: specgram data array values does not match specgram plot

description: Python, matplotlib: specgram data array values does not match specgram plot


code: reduced_list = list(s - set(last5))

rawDescription: Generate random numbers without using the last n values in Python


code:
import json
objid = json.loads(datastring)["objectId"]

rawDescription: search string in text file and capture following n characters

description: Search string in text file and capture following n characters


code: topicResult = " ".join([t['TopicArn'] for t in topicsList if t['TopicArn'].endswith(topic)])

rawDescription: Variable that returns complete string inside list


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''

''')
elem = soup.find(class_='special_tag')
elem.string = 'inner'
print(elem)

rawDescription: Replacing the inner HTML with BeautifulSoup?


code:
idx = pd.IndexSlice
index1 = idx[1, :, :, 0:1]
index2 = idx[2, :, :, 1:2]
pd.concat([df.loc[index1], df.loc[index2]])

rawDescription: how to slice MultiIndex DataFrame with dependent levels of MultiIndex

description: Slice MultiIndex DataFrame with dependent levels of MultiIndex


code:
import lxml.etree as ET
content = '''\
<record>
    <nd>First</nd>
    <nd>Second</nd>
    <nd ref="203936110"></nd>
    <nd>Third</nd>
    <nd>Fourth</nd>
</record>'''
root = ET.fromstring(content)

for elt in root.xpath('''
    //nd[@ref="203936110"]/following-sibling::nd[1]
    |
    //nd[@ref="203936110"]/preceding-sibling::nd[1]'''):

    print(elt.text)

rawDescription: XPath - Selecting preceding and following sibling of a specific node in a single query


rawDescription: Building a directed multigraph (Python)

description: Building a directed multigraph (Python)


rawDescription: How to store a one-to-one relationship table for fast lookups in Python?

description: Store a one-to-one relationship table for fast lookups in Python


code:
source
import concurrent.futures, time

poolx = concurrent.futures.ThreadPoolExecutor(max_workers=1)
poolx.submit(time.sleep, 3)
poolx.submit(time.sleep, 0)   # very fast

print('pending:', poolx._work_queue.qsize(), 'jobs')
print('threads:', len(poolx._threads))
print()

# TODO: make thread safe; work on copy of queue?
print('Estimated Pending Work Queue:')
for num,item in enumerate(poolx._work_queue.queue):
    print('{}\t{}\t{}\t{}'.format(
        num+1, item.fn, item.args, item.kwargs,
        ))

poolx.shutdown(wait=False)

rawDescription: Checking up on a `concurrent.futures.ThreadPoolExecutor`


code:
class JobForm(forms.ModelForm):
    end_time = MyDateTimeField()

rawDescription: allow HH:MM kind of input in a django DateTimeField instead of long format

description: Allow HH:MM kind of input in a django DateTimeField instead of long format


code:
@attr(speed='slow')
class MyTestCase:
    def test_long_integration(self):
        pass
    def test_end_to_end_something(self):
        pass

rawDescription: Django tests that should not run automatically


code:
image_dict = collections.defaultdict(list)

for image in images:
    # Will return float
    diff = (capture_time - starting_time).total_seconds()

    image_dict[int(diff)].append(image)

rawDescription: Bin photos by time taken

description: Bin photos by time taken


code:
def capacity(self):
    return self.trucks * self.multiplier

rawDescription: django, show fields on admin site that are not in model

description: Django, show fields on admin site that are not in model


code:
from collections import defaultdict
dict_stuff = defaultdict(list)
for key, value in stuff:
    dict_stuff[key].append(value)

rawDescription: What's the cleanest (most Pythonic) way of creating a dictionary with list values from a sequence?


code:
D2 = np.sqrt(((A[:, None, :] - B[None, :, :]) ** 2).sum(-1))
weights2 = D2.sum(1)
np.allclose(weights, weights2)  # True

rawDescription: Numpy - Speed up iteration comparison?

description: Numpy - Speed up iteration comparison


code:
from abc import ABCMeta, abstractmethod

class DictionaryVisitor(object):
    __metaclass__ = ABCMeta

    @abstractmethod
    def visit(self, node, parents, result):
        """ This inspects the current node and
            accumulates any data into result """
        pass  # Implement this in the subclasses

    def accept(self, node, parents, result):
        """ This tests if the node should be traversed. This is an efficiency
            improvement to prevent traversing lots of nodes that you have no
            interest in """
        return True

    def traverse(self, node, parents, result):
        """ This traverses the dictionary, visiting each node in turn """
        if not self.accept(node, parents, result):
            return

        self.visit(node, parents, result)
        if 'childs' in node:
            for child in node['childs']:
                self.traverse(child, parents + [node], result)

    def start(self, dict_list):  # bad method name
        """ This just handles the parents and result argument of traverse """
        # Assuming that result is always a list is not normally appropriate
        result = []
        for node in dict_list:
            self.traverse(node, [], result)
        return result

rawDescription: create different list of dicts from master list of dict

description: Create different list of dicts from master list of dict


rawDescription: Multiply Series by distributing across MultiIndex levels


code:
graph = facebook.GraphAPI(oauth_access_token)
tags = json.dumps([{'x':50, 'y':50, tag_uid:12345}, {'x':10, 'y':60, tag_text:'a turtle'}])
graph.put_photo(open('img.jpg'), 'Look at this cool photo!', album_id_or_None, tags=tags)

rawDescription: Accessing Facebook API Data with Python

description: Accessing Facebook API Data with Python


code: map(lambda x: x.split(';') ,a[0].strip().split('\n'))

rawDescription: Convert one dimension list to two diminsions with specific delimiters


code:
import BeautifulSoup
REMOVE_ATTRIBUTES = [
    'lang','language','onmouseover','onmouseout','script','style','font',
    'dir','face','size','color','style','class','width','height','hspace',
    'border','valign','align','background','bgcolor','text','link','vlink',
    'alink','cellpadding','cellspacing']

doc = '''<title>Page title</title>
This is paragraph one.
This is paragraph two.'''
soup = BeautifulSoup.BeautifulSoup(doc)
for tag in soup.recursiveChildGenerator():
    try:
        tag.attrs = [(key,value) for key,value in tag.attrs
                     if key not in REMOVE_ATTRIBUTES]
    except AttributeError:
        # 'NavigableString' object has no attribute 'attrs'
        pass
print(soup.prettify())

rawDescription: BeautifulSoup: Strip specified attributes, but preserve the tag and its contents


rawDescription: Python: Extract JSON keys

description: Python: Extract JSON keys


code:
t1, t2 = leg.get_texts()
t1.get_fontproperties() is t2.get_fontproperties()

rawDescription: How to change fontsize of individual legend entries in pyplot?

description: Change fontsize of individual legend entries in pyplot


code:
class _Example(object):
    def __init__(self, doSomething, doSomethingElse):
        self.doSomething     = doSomething
        self.doSomethingElse = doSomethingElse

ExampleStore = {}

def register(type1, type2, instance):
    ExampleStore[(type1, type2)] = instance

def doSomething(a, b):
    types = type(a), type(b)
    return ExampleStore[types].doSomething(a, b)

def doSomethingElse(a, b):
    types = type(a), type(b)
    return ExampleStore[types].doSomethingElse(a, b)

def Example(type1, type2, doSomething, doSomethingElse):
    register(type1, type2, _Example(doSomething, doSomethingElse))

__all__ = [
    'doSomethingElse',
    'doSomethingElse',
    'Example'
]

rawDescription: From Haskell to functional Python

description: From Haskell to functional Python


code: scheme, netloc, path = parsed_url[:3]

rawDescription: Expand only some tuple elements to variable list


code:
x = ((df>16)&mask).values.nonzero()
zip(x[0],x[1])

rawDescription: Python pandas: get elements (index,col) below diagonal in dataframe


code:
g = open('output.txt', 'w')
for num in numlist:
    g.write("%f\n" % num)
g.close()

rawDescription: Taking each item in a list and writing them on a text file


code:
result_list = []
for j in some_list:
    result_list.append(method(j))
result = tuple(any(grouped) for grouped in zip(*result_list))

rawDescription: Most pythonic way to 'or' tuples?

description: Most pythonic way to 'or' tuples


code: running_time = (current_time - launch_time).total_seconds()/60

rawDescription: Python doesn't compare date?

description: Python doesn't compare date


code:
movies_added_last(seconds=60)
movies_added_last(minutes=20)
# etc.

rawDescription: Passing Arguments to Inner Function

description: Passing Arguments to Inner Function


code:
response = client.create_role(
    Path='string',
    RoleName='string',
    AssumeRolePolicyDocument='string'
)

rawDescription: How do you create roles and policies with Boto 3?

description: Create roles and policies with Boto 3


code:
__tablename__ = 'commercial'
id = Column(None, ForeignKey('building.id'), primary_key=True)

rawDescription: SQLAlchemy Inheritance

description: SQLAlchemy Inheritance


code:
import pylab as p
import networkx as nx

G = nx.Graph()
G.add_edge("A","B")
G.add_edge("A","H")
G.add_edge("H","C")
G.add_edge("B","C")
G.add_edge("B","D")

nx.draw(G)
p.show()

rawDescription: Graph traversal with Networkx (Python)

description: Graph traversal with Networkx (Python)


code:
np.allclose(pi[0, 0, 0], np.linalg.pinv(b[0, 0, 0]))
np.allclose(pi[1, 2, 3], np.linalg.pinv(b[1, 2, 3]))

rawDescription: Python, simultaneous pseudo-inversion of many 3x3, singular, symmetric, matrices


code:
prop = prop.setter(setter)
prop.fget is getter
prop.fset is setter
prop.fdel is None

rawDescription: How does the @property decorator work?

description: How does the @property decorator work


code:
char* PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *size)
char* PyUnicode_AsUTF8(PyObject *unicode)

rawDescription: Python 3.3 C-API and UTF-8 Strings

description: Python 3.3 C-API and UTF-8 Strings


code:
def denormalise(df, *colnames):
    df1 = df.copy() #  optional, but means we're not changing df globally
    cols = [(colname, df1.pop(colname).apply(pd.Series).stack()) for colname in colnames]
    for colname, c in cols:
        c.index = c.index.droplevel(-1)
        c.name = colname
        df1 = df1.join(c)
    return df1
    # optionally .reindex_axis(df.columns, axis=1)  # reorder columns
    #        and .reset_index(drop=True)            # 0,1,...n index

rawDescription: Denormalise the data using Pandas

description: Denormalise the data using Pandas


code:
# Form n(i) x n(j) array and then append with "columnar" M(i,j) array
nn_arr = n[np.array(list(itertools.product(range(5), repeat=2)))]

nn_M_arr = np.concatenate((nn_arr,M.reshape(-1,1)),axis=1)

# Get linear indices version
dims = nn_M_arr.max(0)+1
lidx = np.ravel_multi_index(nn_M_arr.T,dims)

# Get unique indices and the counts
_, idx, counts = np.unique(lidx,return_index=True,return_counts=True)

# Get corresponding unique triplets using unique indices and zip with counts
out = zip(map(tuple,nn_M_arr[idx]),counts)

rawDescription: Count occurences of elements of a matrix fast


code:
sm = difflib.SequenceMatcher(None, 'hello', 'hello world')
diff = show_diff(sm)
diff

rawDescription: How to create a manual diff between two Json objects which can be sent to Reviewboard using python?

description: Create a manual diff between two Json objects which can be sent to Reviewboard using python


code: finallist = [item.strip() for item in newlist]

rawDescription: lists and sublists

description: Lists and sublists


code:
positions, scores = Counter(), Counter()
for sequence, position, strand, score in reader:
    positions[sequence] += position
    scores[sequence] += scores

for sequence in positions:
    writer.writerow(sequence, positions[sequence], scores[sequence])

rawDescription: Parsing Data From Long to Wide Format in Python


code:
for character in inputphrase:
    if character in vowels:
        i = i + 1
print(i)

rawDescription: Python While loops with tuples

description: Python While loops with tuples


code:
import numpy as np
import scipy as sp
import pylab as pl

def Catalan(n):
    if n==0:
        return (1)
    elif n==1:
        return (1)
    else:
        return (((4*n-2)*Catalan(n-1))/(n+1))
result = [Catalan(n) for n in range(18)]
ax = pl.subplot()
ax.bar(range(len(result)), result)
pl.show()

rawDescription: Graphing values generated from loop

description: Graphing values generated from loop


rawDescription: What is Go's equivalent to Python's crypt.crypt?


code:
splitted = dfs['BINDATA'].apply(lambda x: pd.Series(list(x)))
splitted.columns = ['B'+str(x) for x in splitted.columns]
dfs.join(splitted)

rawDescription: How to split a dataframe column into multiple columns

description: Split a dataframe column into multiple columns


code:
async def display_dt():

    while not stop_future.done():
        print('dt-1', datetime.datetime.now())
        # sleep 5 seconds or stop_future done
        await asyncio.wait([asyncio.sleep(5), stop_future], return_when=FIRST_COMPLETED) # <----
        print('dt-2', datetime.datetime.now())

    task = asyncio.Task.current_task()
    task.cancel()
    print(stop_future.result())

rawDescription: Why asyncio.wait does not wait for the FIRST_COMPLETED


code:
from functools import partial

for name in iter(partial(raw_input, 'Name:'), ''):
    do_something_with(name)

rawDescription: How to do variable assignment inside a while(expression) loop in Python?

description: Do variable assignment inside a while(expression) loop in Python


code:
params, cov = optimize.leastsq(residuals, x0 = np.ones(len(UV), dtype = float),
                               args=(B, C, H))

rawDescription: Constraining the least squares fitting in python


code:
ax.set_zorder(ax2.get_zorder()+1) # put ax in front of ax2
ax.patch.set_visible(False) # hide the 'canvas'
ax2.patch.set_visible(True) # show the 'canvas'

rawDescription: Multi-Axis Graph with Line on top. Matplotlib


code:
arr = list(range(10))
b = arr
print(id(arr), id(b))
arr = [u + 1 for u in arr]
print(id(arr), id(b))
print(arr, b)

c = arr
print(id(arr), id(c))
arr[:] = [u + 1 for u in arr]
print(id(arr), id(c))
print(arr, c)

rawDescription: Edit a list while iterating over it (the pythonic way!)


code:
import numpy
import matplotlib.pyplot as plt

def CatmullRomSpline(P0, P1, P2, P3, nPoints=100):
  """
  P0, P1, P2, and P3 should be (x,y) point pairs that define the
  Catmull-Rom spline.
  nPoints is the number of points to include in this curve segment.
  """
  # Convert the points to numpy so that we can do array multiplication
  P0, P1, P2, P3 = map(numpy.array, [P0, P1, P2, P3])

  # Calculate t0 to t4
  alpha = 0.5
  def tj(ti, Pi, Pj):
    xi, yi = Pi
    xj, yj = Pj
    return ( ( (xj-xi)**2 + (yj-yi)**2 )**0.5 )**alpha + ti

  t0 = 0
  t1 = tj(t0, P0, P1)
  t2 = tj(t1, P1, P2)
  t3 = tj(t2, P2, P3)

  # Only calculate points between P1 and P2
  t = numpy.linspace(t1,t2,nPoints)

  # Reshape so that we can multiply by the points P0 to P3
  # and get a point for each value of t.
  t = t.reshape(len(t),1)

  A1 = (t1-t)/(t1-t0)*P0 + (t-t0)/(t1-t0)*P1
  A2 = (t2-t)/(t2-t1)*P1 + (t-t1)/(t2-t1)*P2
  A3 = (t3-t)/(t3-t2)*P2 + (t-t2)/(t3-t2)*P3

  B1 = (t2-t)/(t2-t0)*A1 + (t-t0)/(t2-t0)*A2
  B2 = (t3-t)/(t3-t1)*A2 + (t-t1)/(t3-t1)*A3

  C  = (t2-t)/(t2-t1)*B1 + (t-t1)/(t2-t1)*B2
  return C

def CatmullRomChain(P):
  """
  Calculate Catmull Rom for a chain of points and return the combined curve.
  """
  sz = len(P)

  # The curve C will contain an array of (x,y) points.
  C = []
  for i in range(sz-3):
    c = CatmullRomSpline(P[i], P[i+1], P[i+2], P[i+3])
    C.extend(c)

  return C

rawDescription: Emulating Excel's "scatter with smooth curve" spline function in Matplotlib for 3 points


rawDescription: Default save path for Python IDLE?

description: Default save path for Python IDLE


code: elements = [ObjectValue(attribute_name='name', style={'fontName': 'wqy'})]

rawDescription: Unicode characters are boxes in Geraldo/ReportLab generated PDF


code:
import functools

def send_emails(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
        this_task_id = self.request.id
        email = kwargs.pop('email', False)  # get email and remove it from kwargs
        try:
            ret = func(self, *args, **kwargs)
        except NotifyException as ex:
            if email:
                send_failure_mail(email, this_task_id)
            return ex.value
        except Exception:
            if email:
                send_failure_mail(email, this_task_id)
            # It would be better to raise again to allow celery knows the task has failed
            raise
        else:
            if email:
                send_success_mail(mail, this_task_id)
            return ret
    return wrapper

rawDescription: Send mail on Celery task success/failure reliably


code:
l = ['banana', 'apple', 'pear']
', '.join(l)

rawDescription: Avoiding typing redundancy when generating strings with elements separated by a token


rawDescription: Count number of elements in each row in 2D list


code: import matplotlib.pyplot as plt

rawDescription: pyplot equivalent for pl.cm.Spectral in matplotlib

description: Pyplot equivalent for pl.cm.Spectral in matplotlib


code:
def next_chunk(self, fmt):
    sz = struct.calcsize(fmt)
    chunk = struct.unpack_from(fmt, self.recv_buf, self.recv_buf_i)
    self.recv_buf_i += sz
    return chunk

rawDescription: Faster sockets in Python

description: Faster sockets in Python


code:
sorted(dic)
new_dic = {}
for k in sorted(dic):
    new_dic[dic[k]] = k

new_dic

rawDescription: Swapping contents of the dictionary

description: Swapping contents of the dictionary


rawDescription: Looping through a list from a specific key to the end of the list


code:
x = u'\x99\x8c\x85\x8d'
x.encode('latin-1')

rawDescription: Fixing faulty unicode strings

description: Fixing faulty unicode strings


code:
m = M.shape[0]
x0 = np.ones(m)/np.sqrt(m)

rawDescription: I'm having difficulty understanding the syntax of scipy.optimize


code:
from django.db import connection
def my_custom_sql(self):
    cursor = connection.cursor()
    cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
    cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
    row = cursor.fetchone()
return row

rawDescription: Is it possible to use raw SQL with python's django web framework?


rawDescription: Regex to capture the c langage comment use python


code:
for i in range(1,5):
    contentA = tree.xpath("//table[@class='yfnc_tabledata1']/tr[1]/td/table/tr[2]/td[{i}]".format(i=i))[0].text_content().strip()
    print(contentA)

rawDescription: LXML - parse td content within tr tag

description: LXML - parse td content within tr tag


rawDescription: Parse directory path to a variable in python


code:
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

def packOrders(request):
    # your code
    return HttpResponseRedirect(reverse('orders'))

rawDescription: django return same page on clicking button

description: Django return same page on clicking button


code:
htmls = '''

    Name:

    Alex

... more text

'''

htmls.contents
soup = BeautifulSoup(htmls)
h1 = soup.find("h1", {"class": "student"})

h1

rawDescription: BeautifulSoup doesn't return from h1 properly


code:
def hello():
    print("hello world!!!")
    return mock.DEFAULT

rawDescription: Is there anyway to print a statement when Python mock function called?


code:
@route('/')
@route('/hello/<name>')
def greet(name='Stranger'):
    return template('Hello {{name}}, how are you?', name=name)

rawDescription: RESTful Python for Java (Jersey) developer


code:
IS_IN_SET(('sunday','monday','tuesday','wednesday','thursday','friday','saturday'),
          multiple=True)

rawDescription: How to define list of enumerated values in Web2Py

description: Define list of enumerated values in Web2Py


code:
def OnVetoItems(self, event):
    if event.m_itemIndex == 0:
        event.Veto()
        return

rawDescription: WxPython ListCtrl index 0 item non editable


code:
{'2': {'BillingNumber': '2',
       'City': 'Reading',
       'CustomerName': 'CHARLIE RYAN',
       'IsActive': 'Yes',
       'IsCreditHold': 'No',
       'IsPayScan': 'Yes',
       'State': 'PA'},
 '3': {'BillingNumber': '3',
       'City': '',
       'CustomerName': 'INSURANCE BILLS',
       'IsActive': '',
       'IsCreditHold': '',
       'IsPayScan': '',
       'State': ''},
 '4': {'BillingNumber': '4',
       'City': '',
       'CustomerName': 'AAA',
       'IsActive': '',
       'IsCreditHold': '',
       'IsPayScan': '',
       'State': ''}}

rawDescription: Compare 2 .csv files with Python then output results


rawDescription: PYTHON - creating multiple lists in a loop


code:
import wx

# custom StaticBox class
class MyStaticBox(wx.StaticBox):
  def __init__(self, *args, **kwargs):
    super(MyStaticBox, self).__init__(*args, **kwargs)
    self.Bind(wx.EVT_PAINT, self.OnPaint)

  def OnPaint(self, event):
    # do what you want here
    width, height = self.GetSize()
    dc = wx.PaintDC(self)
    dc.Clear()
    dc.SetPen(wx.Pen('#4285F4'))
    dc.DrawLine(0, 0, width, height)
    dc.DrawLine(width, 0, 0, height)
    # after you finished, call the StaticBox OnPaint
    super(MyStaticBox, self).OnPaint(event)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.frame = parent

        MyStaticBox(self, -1, 'Example', (5, 30), size=(290, 185))

        #self.line = wx.StaticLine(self,-1, (25, 150), (100,1))
        #self.line.SetForegroundColour(("blue"))

class MainFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Window",size=(305,430))
        panel = MainPanel(self)
        self.CenterOnParent()

if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

rawDescription: wx.Python: How can I draw some lines into wx.staticbox

description: Wx.Python: How can I draw some lines into wx.staticbox


code:
import re
def to_words(text):
    'Break text into a list of words without punctuation'
    return re.findall(r"[a-zA-Z']+", text)

def match(a, b):
    # Make b the longer list.
    if len(a) > len(b):
        a, b = b, a
    # Map each word of b to a list of indices it occupies.
    b2j = {}
    for j, word in enumerate(b):
        b2j.setdefault(word, []).append(j)
    j2len = {}
    nothing = []
    unique = set() # set of all results
    def local_max_at_j(j):
        # maximum match ends with b[j], with length j2len[j]
        length = j2len[j]
        unique.add(" ".join(b[j-length+1: j+1]))
    # during an iteration of the loop, j2len[j] = length of longest
    # match ending with b[j] and the previous word in a
    for word in a:
        # look at all instances of word in b
        j2lenget = j2len.get
        newj2len = {}
        for j in b2j.get(word, nothing):
            newj2len[j] = j2lenget(j-1, 0) + 1
        # which indices have not been extended?  those are
        # (local) maximums
        for j in j2len:
            if j+1 not in newj2len:
                local_max_at_j(j)
        j2len = newj2len
    # and we may also have local maximums ending at the last word
    for j in j2len:
        local_max_at_j(j)
    return unique

rawDescription: Find maximum length of all n-word-length substrings shared by two strings


rawDescription: Python Tkinter - Toplevel MoveInAnimation


code:
high_corr_out = np.zeros(A.shape[1])
high_corr_out

rawDescription: Efficient pairwise correlation for two matrices of features


code:
with open('userdata.log','r',encoding = 'utf-8') as my_file:
    for label in SHEETS:
        for line in my_file:
            if label in line:
                print (line)
        my_file.seek(0)

rawDescription: Fixing Nested For Loops

description: Fixing Nested For Loops


code:
t = []
def flatten(aList):
    for i in aList:
        if type(i) !=list:
             t.append(i)
        else:
             flatten(i)
    return t
flatten([1, 1])
flatten([1, 1])
t  # your global, still holding all those values:

rawDescription: Flatten an irregular list of lists in Python recursively


code:
from binascii import hexlify

key = hexlify(os.urandom(lenght))

rawDescription: Whats the simplest and safest method to generate a API KEY and SECRET in Python


code: df = DataFrame(randn(90,2),columns=list('AB'),index=date_range('20130101 9:01:01',freq='s',periods=90))

rawDescription: Create overlapping groups with pandas timegrouper


code: health = min(health, 100)

rawDescription: Stuck on a simple if statement Python 3

description: Stuck on a simple if statement Python 3


code: numpy.fromfunction(lambda x, y: x + y, (5, 5))

rawDescription: Are there alternative way to manage value assignment of n-dim array/matrix/list in Python?


code: z = np.array([[[23] * ncols]*nrows]).reshape(nrows, ncols)

rawDescription: Reshaping Arrays in Numpy with variables


code: a = "This is a string literal"  #the string on the right side is a string literal, "a" is a string variable.

rawDescription: String literal with triple quotes in function definitions


rawDescription: Writing lists of data to a text file column by column


code:
DATA
PoolOfExtLines
DATA[((DATA == PoolOfExtLines[:,None,:]).all(2)).any(0),-1] = -1
DATA

rawDescription: Numpy array: Changing the values of the last column when lines of a 2d-array are equal to lines of another 2d-array


code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import readline  # not really required, but allows you to
                 # navigate with your arrow keys
import code

def predefined_function():
    return "whoop!"

vars = globals().copy()
vars.update(locals())
shell = code.InteractiveConsole(vars)
shell.interact()

rawDescription: How to write a python script that can get and execute python commands?

description: Write a python script that can get and execute python commands


code:
lines_you_want = []

with open ("test.txt","r") as myResults:
    lines = myResults.readlines()
    indexes_of_lines_you_want = [] # We create a list for the indexes of the lines you want to extract

    for i in range(len(lines)):
        if '*******' in lines[i]: # We check if the current line is a line full of stars
            indexes_of_lines_you_want.extend([i+2, i+3]) # We add the indexes current_index+2 and current_index+3 to our list

    for i in indexes_of_lines_you_want:
        lines_you_want.append(lines[i])

rawDescription: Python filtering text

description: Python filtering text


code:
import xlwt
b = xlwt.Workbook()
s = b.add_sheet('x')
style = xlwt.easyxf("", "#,###.00")
s.write(0, 0, 1234567.89, style)
b.save("locale_fmt_demo.xls")

rawDescription: How to have localized style when writing cell with xlwt

description: Have localized style when writing cell with xlwt


code:
import inspect

def methods(c):
    return (m for m in (getattr(c, d) for d in dir(c))
            if inspect.ismethoddescriptor(m) or inspect.ismethod(m))

rawDescription: For each function in class within python


code: result = r"fred\xbf"

rawDescription: How do I tell Python not to interpret backslashes in strings?

description: Tell Python not to interpret backslashes in strings


code:
x = []

for d in xrange(0,66):
    x.append(data[:,d])

rawDescription: Creating new array in for loop (Python)

description: Creating new array in for loop (Python)


code: readfile = open('file_being_read.dat')

rawDescription: Dividing a string by type of data inside each line of file in python


code:
import cookielib
import urllib
import urllib2

# Store the cookies and create an opener that will hold them
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))

# Add our headers
opener.addheaders = [('User-agent', 'RedditTesting')]

# Install our opener (note that this changes the global opener to the one
# we just made, but you can also just call opener.open() if you want)
urllib2.install_opener(opener)

# The action/ target from the form
authentication_url = 'https://ssl.reddit.com/post/login'

# Input parameters we are going to send
payload = {
  'op': 'login-main',
  'user': '<username>',
  'passwd': '<password>'
  }

# Use urllib to encode the payload
data = urllib.urlencode(payload)

# Build our Request object (supplying 'data' makes it a POST)
req = urllib2.Request(authentication_url, data)

# Make the request and read the response
resp = urllib2.urlopen(req)
contents = resp.read()

rawDescription: Login to website using urllib2 - Python 2.7


code:
def xor(list1, list2):
    outputlist = []
    list3 = list1 + list2
    for i in range(0, len(list3)):
        if ((list3[i] not in list1) or (list3[i] not in list2)) and (list3[i] not in outputlist):
             outputlist[len(outputlist):] = [list3[i]]
    return outputlist

rawDescription: Comparing two lists and only printing the differences? (XORing two lists)


code:
ChannelCodeType = Enum('TELEPHONE', 'TELEFAX', 'EMAIL', 'WEBSITE',
                                               type_name='channelCode')

class Contact(ComplexModel):
    "contact person and communication channel"
    contactName = primitive.Unicode(min_len=1, max_len=70, nillable=False)
    channel = primitive.Unicode(max_occurs='unbounded')
    channelCode = XmlAttribute(ChannelCodeType, attribute_of='channel')

rawDescription: XmlAttribute for an unbounded primitive type


code:
class ReturnList:
    def __init__(self):
        self.list = { 1: "Item1", 2: "Item2" }

redirect.returnKeys(ReturnList())

rawDescription: Assign array to key dictionary in python


code: word.lower() == word

rawDescription: Has a better way to check word?

description: Has a better way to check word


code:
Solution
group_hours = (df.hour <= df.hour.shift()).cumsum()

def insert_missing_hours(df):
    return df.set_index('hour').reindex([0, 6, 12, 18]).reset_index()

df.groupby(group_hours).apply(insert_missing_hours).reset_index(drop=1)

rawDescription: Modular arithmetic in python to iterate a pandas dataframe


code: l1 + filter(lambda x: x not in l1, l2)

rawDescription: Subtract and add two lists without changing their order in Python


code:
def change_2(v):
    v[2] = 6
    return v

x = [1,2,3]
z = change_2(x[:])

rawDescription: how to pass a list as value and not as reference?

description: Pass a list as value and not as reference


code: new_list = sorted(l, cmp=cmp_values)

rawDescription: python string compare issue in a list

description: Python string compare issue in a list


code:
value = info.findNext("b")
if not value:
    value = "Oompa Loompa"

rawDescription: Compressing `x if x else y` statement in Python


code:
import numpy as np

# Let's say we have a file with a 512 byte header, the
# first 16 bytes of which are the width and height
# stored as big-endian 64-bit integers.  The rest of the
# "main" data array is stored as little-endian 32-bit floats

with open('data.dat', 'r') as f:
    width, height = np.fromfile(f, dtype='>i8', count=2)
    # Seek to the end of the header and ignore the rest of it
    f.seek(512)
    data = np.fromfile(f, dtype=np.float32)

# Presumably we'd want to reshape the data into a 2D array:
data = data.reshape((height, width))

rawDescription: Read binary file which has different datatypes


code:
def make_proxy(f_hash):
    def f_proxy(*args, **kwargs):
        global gfunctions
        f = lfunctions[f_hash]
        f(*args, **kwargs)

    return f_proxy

rawDescription: Can I restore a function whose closure contains cycles in Python?

description: Restore a function whose closure contains cycles in Python


code:
def page_query(q):
    while True:
        r = False
        for elem in q.limit(1000):
            r = True
            yield elem
        if not r:
            break

for chunk in page_query(query):
    # Do something with the chunk if needed
    session.delete(chunk)
session.commit()

rawDescription: How to do a paged query with empty collections in SQLAlchemy?

description: Do a paged query with empty collections in SQLAlchemy


code:
with open(file) as f, open(outfile, 'w') as w:
    for line in f:
        if 'Blue' not in line:
            w.write(line)

rawDescription: Python extract certain rows

description: Python extract certain rows


code:
# Store country_codes and continent_codes column data for further usage
oldval = np.array(df['country_codes'])
newval = np.array(df['continent_codes'])

# Left and right indices for each match from oldval in arr
left_idx = np.searchsorted(oldval,arr,'left')
right_idx = np.searchsorted(oldval,arr,'right')

# Mask of elements to be changed
mask = left_idx!=right_idx

# Using the mask put selective elements from continent_codes column into arr
arr[mask] = newval[left_idx[mask]]

rawDescription: Replace values in numpy 2D array based on pandas dataframe


code:
def myview(request):
    form = MyForm()
    c = {'form': form}
    return render(request, 'mytemplate.html', c)

rawDescription: How exactly should I implement conditional logic in Django forms?


code: callable(fn)

rawDescription: python 3: how to check if an object is a function?

description: Python 3: how to check if an object is a function


code:
import numpy as np
data = np.loadtxt("raw.txt", comments=">",
                  converters = {0: lambda s: [x for x in s]}, dtype=str)

print (data=="A").sum(axis=0)
print (data=="T").sum(axis=0)
print (data=="C").sum(axis=0)
print (data=="G").sum(axis=0)

rawDescription: how to populate a matrix in python

description: Populate a matrix in python


code: parser.add_argument('delay', type=int, nargs='?', default=21)

rawDescription: Make django custom management command argument "Not Required"


code: List # 4 is largest.

rawDescription: Isolating lists based on value in python3


code:
def static_init(cls_to_decorate):
    class Wrapper(cls_to_decorate):
        _static_init_called = False

        def __new__(cls, *args, **kwargs):
            if not Wrapper._static_init_called:
                raise Exception('{}.static_init() should be called.'.format(cls_to_decorate.__name__))
            return super().__new__(cls, *args, **kwargs)

        @classmethod
        def static_init(cls, *args, **kwargs):
            Wrapper._static_init_called = True
            return super().static_init(*args, **kwargs)
    return Wrapper

rawDescription: Best way to make class variables initialization mandatory

description: Make class variables initialization mandatory


code:
chars = list()

for i in range(0, len(code), 5):
    piece = "".join(code[i:i+5])
    chars.append(self._morse_char_map.get(piece, piece[0]))

return "".join(chars)

rawDescription: How can I analyze or improve my niece's simple compression algorithm that is based on Morse code?

description: Analyze or improve my niece's simple compression algorithm that is based on Morse code


rawDescription: to get max number after concatenation in list

description: To get max number after concatenation in list


code:
len(Images.objects.none()) == 0
len(Images.objects.all()) == 5

rawDescription: Is there a Django function to count objects even on None objects?


code: your_array[..., None]

rawDescription: forcing genfromtxt output to no-vector

description: Forcing genfromtxt output to no-vector


code: matches = [x['title'] for x in soup.find_all('input')]

rawDescription: How to make a RegEx fit in Python

description: Make a RegEx fit in Python


code:
cells = set()
enzymes = set()
biochemicals = set()

with open(filename) as fh:
    out.write(fh.readline()) #ticker
    for line in fh:
        cols = line.split(',')
        row_id = cols[3]
        row_category = cols[17]

        if row_category == ' Cell ':
            cells.add(row_id)
        elif row_category == ' Biochemical ':
            biochemicals.add(row_id)
        elif row_category == ' Enzyme ':
            enzymes.add(row_id)

rawDescription: Improve efficiency in Python matching

description: Improve efficiency in Python matching


rawDescription: Python regex to avoid matching word followed by multiple conditions


code:
def iter_rows(ws):
    for row in ws.iter_rows():
        yield [cell.value for cell in row]

rawDescription: View row values in openpyxl

description: View row values in openpyxl


code:
from pyspark.sql.types import *

schema = StructType([
    StructField("name", StringType(), True),
    StructField("gender", StringType(), True),
    StructField("count", LongType(), True)
])

df = (sqlContext.read.format("com.databricks.spark.csv")
   .schema(schema)
   .load(out))

rawDescription: Exporting text files to PostgreSQL using Spark - Automation


code:
a = restore_state()
if a < 3:
    score = 5
    with open('tom.txt','a') as fo:
        fo.write('Tom: ')
        fo.write(str(score))
        fo.write("\n")
        a = a + 1
        save_state(a)

rawDescription: Python counter between running code

description: Python counter between running code


code:
def findRootIndexes (s):
    nested = 0
    for i, c in enumerate(s):
        if c == '[':
            nested += 1
        elif c == ']':
            nested -= 1
        elif c == '.' and nested == 0:
            yield i

rawDescription: Returning the index of a string that is not within brackets


code:
from decimal import Decimal, ROUND_DOWN

x = 2.47893698236923
print(Decimal(str(x)).quantize(Decimal('.01'), rounding=ROUND_DOWN))
print(Decimal(str(x)).quantize(Decimal('.001'), rounding=ROUND_DOWN))

rawDescription: Python 3.5.0 decimal

description: Python 3.5.0 decimal


code: squared_sum = X.multiply(X).sum(1)

rawDescription: element-wise operations on a sparse matrix

description: Element-wise operations on a sparse matrix


rawDescription: How to find all neighbors of a given point in a delaunay triangulation using scipy.spatial.Delaunay?

description: Find all neighbors of a given point in a delaunay triangulation using scipy.spatial.Delaunay


code:
for txt, val in languages:
    Radiobutton(root,
                text=txt,
                padx = 20,
                variable=v,
                command=lambda: ShowChoice(root),
                value=val).pack(anchor=W)

rawDescription: Python - Tkinter RadioButton If Statement


code:
def set_value(self, member, value):
    members = {
        "a" : self.a,
        "b" : self.b
    }
    members[member] = value

rawDescription: Passing an Python Class Attribute Given a String


code:
@classmethod
def create_new_users(cls): # several users!
    print("how many users do you want to create")
    num = int(input())
    for _ in range(num): # simpler iteration
        print("enter the user's name")
        name = input("") # in 3.x, this is always a string, so it cannot be None...
        # if name == "" or "None,none": # That won't work as you think.
        if name == '' or name.lower() == 'none': # but why disallow the string 'None'?
            # raise SyntaxError("name cannot be None or empty")
            raise RuntimeError("name cannot be None or empty") # or ValueError or alike
            # break not needed. raise jumps out without it as well.
        user = cls(name, "", "", "", "") # name is an input, not an output.
        cls.users.append(name)

rawDescription: Applying the @staticmethod, python3

description: Applying the @staticmethod, python3


rawDescription: how to append to a dictionary values from another dictionary if keys match

description: Append to a dictionary values from another dictionary if keys match


code:
def process(q):
    for item, count in q:
        item.count = count
        yield count

rawDescription: SQLAlchemy equivalent to Django's annotate() method


code:
print("Please enter a number")
Numbered_Being_Entered_Input = input()
Numbered_Being_Entered = int(Numbered_Being_Entered_Input)
Accumulated_Number = 0

while True:
    if Numbered_Being_Entered > 0:
        print("The number you have entered is ", Numbered_Being_Entered)
        Accumulated_Number = Numbered_Being_Entered + Accumulated_Number
        print("The accumulated sum of all the numbers you have entered is ", Accumulated_Number)
        Numbered_Being_Entered_Input = input()
        Numbered_Being_Entered = int(Numbered_Being_Entered_Input)
    elif Numbered_Being_Entered <= 0:
        print("you have chosen the number 0 or a negative number")
        print("Please enter a number higher than 0")
        print("The accumulated sum of all the numbers you have entered before this error is ", Accumulated_Number)
        Numbered_Being_Entered_Input = input()
        Numbered_Being_Entered = int(Numbered_Being_Entered_Input)

rawDescription: how do i do a nested while statement after an while in python?

description: Do a nested while statement after an while in python


code:
('CCCCCCCCCC', 'ATCAAGACA', 'TGGTTTTTTT')
('GGGGGGGGGG', 'ATATACA', 'GATGCATAGC')

('CCCCCCCCCC', 'ATCAAGACA', 'TGGTTTTTTT')
('TTTTTTTTTT', 'ATTTATCA', 'GATTACAGAT')
('GGGGGGGGGG', 'ATATACA', 'GATGCATAGC')

rawDescription: python len function question

description: Python len function question


code: p.apply(f, 'items')

rawDescription: How to combine the data from many data frames into a single data frame with an array as the data values

description: Combine the data from many data frames into a single data frame with an array as the data values


rawDescription: Cumulative addition/multiplication in NumPy


code:
""" Module that imports the json module and monkey-patches it so
JSONEncoder.default() automatically pickles any Python objects
encountered that aren't standard JSON data types.
"""
from json import JSONEncoder
import pickle

def _default(self, obj):
    return {'_python_object': pickle.dumps(obj)}

JSONEncoder.default = _default  # Replace with the above.

rawDescription: Making object JSON serializable with regular encoder


rawDescription: Pandas Python: How to create lapse since today column?


code:
def sumTest2(xs):
    if len(xs) == 0:
        return 0

    total = xs[0] if xs[0] % 2 == 0 else 0

    return total + sumTest2(xs[1:])

rawDescription: sum even integer in a list of integer (without loop)

description: Sum even integer in a list of integer (without loop)


code:
from __future__ import print_function
import ctypes
import time
from multiprocessing import Process, Array, Value

values = [(s*4).encode('ascii') for s in 'abc']

def f1(a, v):
    for i, s in enumerate(values):
        a[i] = s

    v.value += 1

    print("f1 : ", a[:], v.value)

def f2(a,v):
    v.value += 1
    print("f2 : ", a[:], v.value)

def main():
    val = Value(ctypes.c_int, 0)
    arr = Array(ctypes.c_char_p, 3)

    print("Before :", arr[:], val.value)

    p = Process(target=f1, args=(arr, val))
    p2 = Process(target=f2, args=(arr, val))

    p.start()
    p2.start()

    p.join()
    p2.join()

    print("After : ", arr[:], val.value)

if __name__ == '__main__':
    main()

rawDescription: Shared Array not shared correctly in python multiprocessing


code:
import random

def gen_2d(iterations, Chr_size, num_mut):
    randbps = set()
    while len(randbps) < iterations:
        listed = set()
        while len(listed) < num_mut:
            listed.add(random.choice(range(Chr_size)))
        randbps.add(tuple(sorted(listed)))
    return np.array(list(randbps))

rawDescription: generate a 2D array of numpy.random.choice without replacement

description: Generate a 2D array of numpy.random.choice without replacement


rawDescription: Select a specific column in a DF and a random column (python3, pandas)


code:
class ResultForm2(BaseForm):
    selects = FieldList(SelectField(u'Select', coerce=int,
                                    validators=[validators.optional()],
                                    choices=[(1, 'A'), (2, 'B'), (3, 'C'),
                                             (4, 'D'), (5, 'E'), (6, 'FX'),
                                             (7, 'na')]), min_entries=2)

rawDescription: WTForms , form with more than one SelectField? Does SelectFieldArray exists?


rawDescription: Python: strip a wildcard word

description: Python: strip a wildcard word


code:
freqs = pd.get_dummies(ser)
freqs = freqs.where(freqs != 0, np.nan)
freqs

rawDescription: How to set the values of a dataframe given a series of indices and corresponding column names?

description: Set the values of a dataframe given a series of indices and corresponding column names


code: {{request.session.code}} # display code in the template

rawDescription: How to clear a form but still display one of its fields?

description: Clear a form but still display one of its fields


code:
{'bc968f9fa2db71455f50e0c13ce50e871fS7f0e': {u'AppServer': u'WebLogic 10',
                                             u'SSL_Port': u'28443',
                                             u'configureBPMUIStaticContent': u'true',
                                             u'context': u'workspace',
                                             u'instance_home': u'/essdev1/app/oracle/Middleware/user_projects/epmsystem1',
                                             u'isCompact': u'false',
                                             u'maintVersion': u'11.1.2.2.0.66',
                                             u'port': u'28080',
                                             u'serverName': u'FoundationServices0',
                                             u'validationContext': u'workspace/status',
                                             u'version': u'11.1.2.0'}}

rawDescription: How to create structured array out of unstructured HTML using python

description: Create structured array out of unstructured HTML using python


code: emails = {x[1]:x[0] for x in list_of_tuples}

rawDescription: How to efficiently select entries by date in python?

description: Efficiently select entries by date in python


code: by_closeness = sorted(data.keys(), key = lambda x: closeness(x, target), reverse = True)

rawDescription: Finding a string in a list and returning a secondary value with python


code:
l = [0,0,2.5,-5.0]
from collections import Counter
Counter(l)

rawDescription: counting the number of samples in a numpy array

description: Counting the number of samples in a numpy array


code:
def __init__(self, x=None):
    if x is None:
        x = []
    self._x = x

rawDescription: Python __init__ finding variable from nowhere


code:
A = np.arange(15).reshape((5, -1))

def cumsum2_reverse(arr):
    out = np.empty_like(arr)
    d, e = arr.shape
    for i in xrange(d):
        for j in xrange(e):
            b = 0
            for k in xrange(i + 1, d):
                for l in xrange(j + 1, e):
                    b += arr[k, l]
            out[i, j] = b
    return out

rawDescription: Inverse cumsum for numpy

description: Inverse cumsum for numpy


rawDescription: Getting XML attribute value with lxml module


code:
class Ui_Login(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)

rawDescription: Trying to close and open a new window using PyQT(Closed)


code:
# root/django/trunk/django/contrib/auth/models.py
# snip
def get_hexdigest(algorithm, salt, raw_password):
    """
    Returns a string of the hexdigest of the given plaintext password and salt
    using the given algorithm ('md5', 'sha1' or 'crypt').
    """
    raw_password, salt = smart_str(raw_password), smart_str(salt)
    if algorithm == 'crypt':
        try:
            import crypt
        except ImportError:
            raise ValueError('"crypt" password algorithm not supported in this environment')
        return crypt.crypt(raw_password, salt)

    if algorithm == 'md5':
        return md5_constructor(salt + raw_password).hexdigest()
    elif algorithm == 'sha1':
        return sha_constructor(salt + raw_password).hexdigest()
    raise ValueError("Got unknown password algorithm type in password.")

rawDescription: What is the format in which Django passwords are stored in the database?


code:
from bs4 import BeautifulSoup
from selenium import webdriver

url = "http://example.com/"

browser = webdriver.Firefox()
browser.get(url)
page = BeautifulSoup(browser.page_source, "lxml")

# Let's find some tables and then print all their rows
for table in page("table"):
    for row in table("tr"):
        print(row)

rawDescription: Python: scraping results of webpage of which results are generated server-side


code: myDict = collections.Counter(row[6] for row in csvreader)

rawDescription: Read inputfile as csv in python

description: Read inputfile as csv in python


rawDescription: how to write regular expression replaces zero with empty string and make sure that there is not leading digits

description: Write regular expression replaces zero with empty string and make sure that there is not leading digits


code: counter = 0

rawDescription: Using Python to keep track of deaths (Twitch bot)


code:
import ast
import csv

def convert_type(s):
    try:
        return ast.literal_eval(s)
    except (ValueError, SyntaxError):
        return s

def convert_csv_row(lst):
    return [convert_type(x) for x in lst]

data = csv.reader(open('pppp.csv','r', newline=''))
converted = [convert_csv_row(row) for row in data]
print(converted)

rawDescription: Import csv as list of list in python 3.3


rawDescription: %timeit equivalent in code

description: %timeit equivalent in code


code:
def foo(*args, **kwargs):
    if 'foo' in args:
        do_this()
    elif 'bar' in args:
        do_that()
    else:
        raise ValueError()

rawDescription: Optional but conditional argument python


code: gridTL = map(lambda m: gl(gridList, m), section)

rawDescription: Turning a list of coordinates into a set of list/grid references


code: match = CITY_REG.match(line)

rawDescription: Regular Expressions in data parsing Python


code:
def partial(func, *part_args):
    def wrapper(*extra_args):
        args = list(part_args)
        args.extend(extra_args)
        return func(*args)

    return wrapper

rawDescription: How does the functools partial work in Python?


rawDescription: How to convert utf-8 string to big5 with python?

description: Convert utf-8 string to big5 with python


code:
def get_int(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:   # not an int!
            pass   # try again

residents = get_int("How many people live at your house? ")

rawDescription: Variables based on input

description: Variables based on input


code:
l = zip(name, idc, startD, ...)
l.sort(key=...)

rawDescription: sorting data that is read in from file

description: Sorting data that is read in from file


rawDescription: Explain to me how .join() works in Python


code:
def to_dict(self):
     retval = {'identification': self.identifier,
               'information': self.information,
              }
     if self.children:
         retval['nodes'] = {e.position: e.to_dict for e in self.children}
     return retval

rawDescription: optional dictionary items in dict creation

description: Optional dictionary items in dict creation


rawDescription: percentage difference between two images in python using correlation coefficiant

description: Percentage difference between two images in python using correlation coefficiant


rawDescription: In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters?


code:
class BlogPostList(ListAPIView):

    def get_queryset(self):
        user = self.request.user
        return BlogPost.objects.annotate_is_liked_by_user(user)

rawDescription: Django (django-rest-framework) looking for a best practice to find out wether request.user exists in blog.likes


code:
import numpy as np

similarity = np.random.random((32, 32, 100, 100))
n = 1000
seq1, seq2, qual1, qual2 = [np.random.randint(0, s, n) for s in similarity.shape]

def slow():
    score = 0
    for i in xrange(len(seq1)):
        score += similarity[seq1[i], seq2[i], qual1[i], qual2[i]]
    return score

def fast():
    return similarity[seq1, seq2, qual1, qual2].sum()

rawDescription: numpy indexing with multiple arrays

description: Numpy indexing with multiple arrays


code:
class Holder:
    pass

my_holder = Holder()

my_holder.stuff = 'stuff'

rawDescription: What is a simple, yet proper, way to add a container to a pre-exisiting object in python?


code:
class MyTable(tables.Table):
 tr_class=tables.Column(visible=False, empty_values=())
 def render_tr_class(self, value):
   if value.chosen == True:
     return 'highlight'

rawDescription: django-tables2 specify different properties for different rows

description: Django-tables2 specify different properties for different rows


rawDescription: Python code for counting number of zero crossings in an array


code:
class Cached(object):
    """Simple example of immutable object reuse."""

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

    def __new__(cls, i, _cache={}):
        try:
            return _cache[i]
        except KeyError:
            # you must call __new__ on the base class
            x = super(Cached, cls).__new__(cls)
            x.__init__(i)
            _cache[i] = x
            return x

rawDescription: Reuse existing objects for immutable objects?


code:
arr_x = arr[:, :, None, None]
arr_y = arr

rawDescription: Iterate over all pairwise combinations of numpy array columns


code:
[{'children': [{'children': [], 'name': 'foo::fone'},
               {'children': [{'children': [], 'name': 'foo::ftwo::ffone'}],
                'name': 'foo::ftwo'}],
  'name': 'foo'},
 {'children': [], 'name': 'bar'}]

rawDescription: Recursive sorting function for list in Python


code:
from flask import Markup

message = Markup("
Voila! Platform is ready to used
")
flash(message)
return render_template('output.html')

rawDescription: How to display html content through flask messages?

description: Display html content through flask messages


code:
infile = open('hey.txt','r+')
content = infile.readlines() #reads line by line and out puts a list of each line
content[1] = 'This is a new line\n' #replaces content of the 2nd line (index 1)
infile.write(content)
infile.close()

rawDescription: I need to open and rewrite a line in a file in Python


rawDescription: Can a python descriptor be used to instantiate an attribute in the __init__ of another class?


code:
...
def process(self):
    self.parent().parent().statusBar().showMessage("Processing")
...

rawDescription: PySide: How to change the status bar text from another class?


code:
def sum_unique(label, weight):
    order = np.lexsort(label.T)
    label = label[order]
    weight = weight[order]
    unique = np.ones(len(label), 'bool')
    unique[:-1] = (label[1:] != label[:-1]).any(-1)
    totals = weight.cumsum()
    totals = totals[unique]
    totals[1:] = totals[1:] - totals[:-1]
    return label[unique], totals

rawDescription: Map arrays with duplicate indexes?

description: Map arrays with duplicate indexes


rawDescription: aggregate and sum daily data to month in python

description: Aggregate and sum daily data to month in python


code:
action = run_once(my_function)
while 1:
    if predicate:
        action()

rawDescription: Efficient way of having a function only execute once in a loop


rawDescription: Eliminate numbers in string in Python

description: Eliminate numbers in string in Python


code:
'(850,·900,\t12·'
'(frequencies·850,\t4·'
'phone·but\t2·'

#\t denotes tabulation, · denotes spaces

rawDescription: extracting data from multiple text files using python

description: Extracting data from multiple text files using python


rawDescription: Using parametrize for keyword arguments in pytest


code:
from operator import add, sub

def check_answer(number1, number2, answer, operator):
    operations = {
        "+": add,
        "-": sub
    }
    if operator not in operations:
        raise ValueError("Operator '%s' not supported" % operator)

    return operations[operator](number1, number2) == answer

rawDescription: Python "none" printing

description: Python "none" printing


rawDescription: NumPy: counting sizes of row-wise intersections between two arrays


code:
active = False
stored = []
for line in data:
    if " innings " in line:
        tmp = []
        active = True    # now we want to do things
        continue         # but not in this loop
    if line.startswith("Extras   "):
        stored.append(tmp)
        active = False
        continue
    if active:
        tmp.append(line)

rawDescription: Python3 Extracting lines between first instance of two markers


code: import tkinter as TK

rawDescription: Tkinter importing without *?

description: Tkinter importing without *


code:
from collections import defaultdict
from itertools import count

def read_file(file_path):
    result = defaultdict(dict)
    item = count()
    with open(file_path) as f:
        for line in f:
            if not line:
                continue
            parts = line.split()
            result[next(item)][parts[0]] = dict(p.split('=') for p in parts[1:])
    return dict(result)

rawDescription: Split dictionary key and list of values from dict


code: x = old_object

rawDescription: Python Assignment or Variable binding?

description: Python Assignment or Variable binding


code:
import numpy as np
from scipy import sparse
data = """0 234 345
0 236
0 345 365 465
0 12 35 379"""

column_list = []
for line in data.split("\n"):
    values = [int(x) for x in line.strip().split()[1:]]
    column_list.append(values)
lengths = [len(row) for row in column_list]
cols = np.concatenate(column_list)
rows = np.repeat(np.arange(len(column_list)), lengths)
m = sparse.coo_matrix((np.ones_like(rows), (rows, cols)))

rawDescription: How to manually create a sparse matrix in Python

description: Manually create a sparse matrix in Python


code:
text = "My name is Alice and his name is Bob"
words = text.split(' ')
indices = [i+2 for i, word in enumerate(words) if word == 'name']
names = [words[i] for i in indices if i < len(words)]

rawDescription: Python find which order element is in in a list


code: not any(my_hash.values())

rawDescription: Find if a hash in Python has only values of None


code:
def bought_range(row):
    return range(row.f_bought, row.l_bought)

df['bought_range'] = df.apply(bought_range, axis=1)

rawDescription: Getting a list of the range of 2 pandas columns


rawDescription: How to implement a rolling_pct_change(period) in pandas?

description: Implement a rolling_pct_change(period) in pandas


code:
VaR = driver.get_element_by_css_selector("a.PointsTabL.PointsSel .VaR")
print(VaR.text)

rawDescription: Python Slenium get two elements from same part of HTML Code


code: Range: bytes=0-999

rawDescription: How to `pause`, and `resume` download work?

description: `pause`, and `resume` download work


rawDescription: numpy genfromtxt issues in Python3

description: Numpy genfromtxt issues in Python3


code: unstack(df, 'year') # practice required by the code above

rawDescription: Passing an argument as a attribute in a function?


code:
def new_vec(*args, **kwargs):

    kpt_list, rpt_list, h_r, r_ndegen = setup(*args, **kwargs)

    phase = np.exp(1j * np.dot(rpt_list, kpt_list.T)) / r_ndegen[:, None]
    kpt_data = h_r.reshape(-1, phase.shape[0]).dot(phase)

    return kpt_data.reshape(h_r.shape[:2] + (-1,))

rawDescription: performance loss after vectorization in numpy

description: Performance loss after vectorization in numpy


code:
my_handler_entries = [e for e in atexit._exithandlers if e[0] == my_handler_func]
for e in my_handler_entries:
    atexit._exithandlers.remove(e)

rawDescription: Is registered atexit handler inherited by spawned child processes?


code:
for i, j in zip(*np.where(significant)):
     ax.plot(i, j, 'bo')

rawDescription: Plotting points on a contourf plot

description: Plotting points on a contourf plot


rawDescription: confused by Python set initialization

description: Confused by Python set initialization