code:
from django.utils.text import slugify

def return_slug(firstname, lastname):

    # get a slug of the firstname and last name.
    # it will normalize the string and add dashes for spaces
    # i.e. 'HaRrY POTTer' -> 'harry-potter'
    u_username = slugify(unicode('%s %s' % (firstname, lastname)))

    # split the username by the dashes, capitalize each part and re-combine
    # 'harry-potter' -> 'Harry-Potter'
    u_username = '-'.join([x.capitalize() for x in u_username.split('-')])

    # count the number of users that start with the username
    count = User.objects.filter(username__startswith=u_username).count()
    if count == 0:
        return u_username
    else:
        return '%s-%d' % (u_username, count)

rawDescription: Slugify string in Django

description: Slugify string in Django


code:
class PreviousURLMiddleware(object):
    def process_response(request, response):
        if response.status_code == 200:
            request.session['previous_url'] = request.get_full_url()
        return response

rawDescription: Does Django have a built in way of getting the last app url the current user visited?


code:
combination_num = range(k+1, n+1)
combination_den = range(1, n-k+1)
combination_log = np.log(combination_num).sum() - np.log(combination_den).sum()
p_k_log = k * np.log(p)
neg_p_K_log = (n - k) * np.log(1 - p)
p_log = combination_log + p_k_log + neg_p_K_log
probability = np.exp(p_log)

rawDescription: Computing a binomial probability for huge numbers


code:
cma1 = FormSet.GetFieldValue(CurrentForm, "ANL_CMA1_NUM_CLOSED_SALES")
cma2 = FormSet.GetFieldValue(CurrentForm, "ANL_CMA2_NUM_CLOSED_SALES")
delta = float(cma2 - cma1) / cma1
FormSet.SetFieldValue(CurrentForm, "ANL_CHG_NUM_CLOSED_SALES", delta*100.0)

rawDescription: calculate percent difference python

description: Calculate percent difference python


code:
import cv2
import numpy as np
import os

# the list of images (tables)
images = ['table1.png', 'table2.png', 'table3.png', 'table4.png', 'table5.png']

# the list of templates (used for template matching)
templates = ['train1.png']

def remove_duplicates(lines):
    # remove duplicate lines (lines within 10 pixels of eachother)
    for x1, y1, x2, y2 in lines:
        for index, (x3, y3, x4, y4) in enumerate(lines):
            if y1 == y2 and y3 == y4:
                diff = abs(y1-y3)
            elif x1 == x2 and x3 == x4:
                diff = abs(x1-x3)
            else:
                diff = 0
            if diff < 10 and diff is not 0:
                del lines[index]
    return lines

def sort_line_list(lines):
    # sort lines into horizontal and vertical
    vertical = []
    horizontal = []
    for line in lines:
        if line[0] == line[2]:
            vertical.append(line)
        elif line[1] == line[3]:
            horizontal.append(line)
    vertical.sort()
    horizontal.sort(key=lambda x: x[1])
    return horizontal, vertical

def hough_transform_p(image, template, tableCnt):
    # open and process images
    img = cv2.imread('imgs/'+image)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)

    # probabilistic hough transform
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 200, minLineLength=20, maxLineGap=999)[0].tolist()

    # remove duplicates
    lines = remove_duplicates(lines)

    # draw image
    for x1, y1, x2, y2 in lines:
        cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 1)

    # sort lines into vertical & horizontal lists
    horizontal, vertical = sort_line_list(lines)

    # go through each horizontal line (aka row)
    rows = []
    for i, h in enumerate(horizontal):
        if i < len(horizontal)-1:
            row = []
            for j, v in enumerate(vertical):
                if i < len(horizontal)-1 and j < len(vertical)-1:
                    # every cell before last cell
                    # get width & height
                    width = horizontal[i+1][1] - h[1]
                    height = vertical[j+1][0] - v[0]

                else:
                    # last cell, width = cell start to end of image
                    # get width & height
                    width = tW
                    height = tH
                tW = width
                tH = height

                # get roi (region of interest) to find an x
                roi = img[h[1]:h[1]+width, v[0]:v[0]+height]

                # save image (for testing)
                dir = 'imgs/table%s' % (tableCnt+1)
                if not os.path.exists(dir):
                     os.makedirs(dir)
                fn = '%s/roi_r%s-c%s.png' % (dir, i, j)
                cv2.imwrite(fn, roi)

                # if roi contains an x, add x to array, else add _
                roi_gry = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
                ret, thresh = cv2.threshold(roi_gry, 127, 255, 0)
                contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

                if len(contours) > 1:
                    # there is an x for 2 or more contours
                    row.append('x')
                else:
                    # there is no x when len(contours) is <= 1
                    row.append('_')
            row.pop()
            rows.append(row)

    # save image (for testing)
    fn = os.path.splitext(image)[0] + '-hough_p.png'
    cv2.imwrite('imgs/'+fn, img)

def process():
    for i, img in enumerate(images):
        # perform probabilistic hough transform on each image
        hough_transform_p(img, templates[0], i)

if __name__ == '__main__':
    process()

rawDescription: Processing an image of a table to get data from it


code:
from pylab import *

# Generate random test data in your range
N = 200
epsilon = 10**(-9.0)
X = epsilon*(50*random(N) + 1)
Y = random(N)

# X2 now has the "units" of nanometers by scaling X
X2 = (1/epsilon) * X

subplot(121)
scatter(X,Y)
xlim(epsilon,50*epsilon)
xlabel("meters")

subplot(122)
scatter(X2,Y)
xlim(1, 50)
xlabel("nanometers")

show()

rawDescription: Changing plot scale by a factor in matplotlib


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

x = np.arange(10)
y = np.random.randn(10)

plt.plot(x,y)
plt.xticks(np.arange(min(x), max(x)+1, 3))
plt.show()

rawDescription: Matplotlib pyplot - tick control and showing date


code:
from distutils.command.install import install
from distutils.core import setup

def run_file(path):
    with open(path, 'r') as f:
        exec(f.read())

class myinstall(install): # subclass distutils's install command
    def finalize_options(self): # called after option parsing
        # call base class function
        install.finalize_options(self)
        # super won't work because distutils under Python 2 uses old-style classes
        # ignore a module
        self.distribution.py_modules.remove('mymodule')
    def run(self): # called to run a command
        # run tests first
        run_file('path/to/test.py')
        # ^ remember to make sure the module is in sys.path
        # run the real commands
        install.run(self)

setup(
    name='abc',
    py_modules=['mymodule'],
    cmdclass={'install': myinstall}
    # ^ override the install command
)

rawDescription: How can I check for which command is run in setup.py?

description: Check for which command is run in setup.py


code:
try:
   val = int(userInput)
except ValueError:
   print("That's not an int!")

rawDescription: Python: how to check if input is a number?


rawDescription: How to get ROE%20 from %s%20?

description: Get ROE%20 from %s%20


code: p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)

rawDescription: Subprocess.Popen spits output on screen even with stdout=subprocess.PIPE)


code:
def twoStrings(string1,string2):
        return zip(string1,string2)

rawDescription: Zip with tuples and list

description: Zip with tuples and list


code:
import numpy as np

# Create large arrays x and y.
# Note they are 1e4 not 1e6 b/c of memory issues creating random numpy matrices (CookieOfFortune)
# However, the same principles apply to larger arrays
x = np.random.randn(10000, 10000)
y = np.random.randn(10000, 10000)

# Create memory maps for x and y arrays
xmap = np.memmap('xfile.dat', dtype='float32', mode='w+', shape=x.shape)
ymap = np.memmap('yfile.dat', dtype='float32', mode='w+', shape=y.shape)

# Fill memory maps with data
xmap[:] = x[:]
ymap[:] = y[:]

# Create memory map for out of core dot product result
prodmap = np.memmap('prodfile.dat', dtype='float32', mode='w+', shape=x.shape)

# Due out of core dot product and write data
prodmap[:] = np.memmap.dot(xmap, ymap)

# Create memory map for out of core addition result
addmap = np.memmap('addfile.dat', dtype='float32', mode='w+', shape=x.shape)

# Due out of core addition and write data
addmap[:] = xmap + ymap

# Create memory map for out of core scaling result
scalemap = np.memmap('scalefile.dat', dtype='float32', mode='w+', shape=x.shape)

# Define scaling constant
scale = 1.3

# Do out of core  scaling and write data
scalemap[:] = scale * xmap

rawDescription: matrix operations with gigantic matrices in python

description: Matrix operations with gigantic matrices in python


rawDescription: Nested Lists in Python - Shifting entries alongside dimensions efficiently


code:
def __lt__(self, other):
    if not isinstance(other, X):
        return NotImplemented
    return self._a < other._a

rawDescription: How to handle mixed types when implementing comparison operators?

description: Handle mixed types when implementing comparison operators


code:
import re
s = "This is Testing! The email is testing@my.com"
re.sub(r'\w+', lambda m: m.group()[::-1], s)

rawDescription: Reverse a string in Python but dont reverse alphanumerics


code:
import numpy as np

# This is a simple example. In your case, A is loaded with np.loadtxt
A = np.array([[1, 2.0, 3.0], [3, 1.0, 2.0], [2.0, 4.0, 3.0]])
B = A.copy()

# Copy the max indices of rows of A into first column of B
B[:,0] = np.argmax(A[:,1:], 1)

# Save the results using np.savetxt with fmt, dynamically generating the
# format string based on the number of columns in B (setting the first
# column to integer and the rest to float)
np.savetxt('/path/to/output.txt', B, fmt='%d' + ' %f' * (B.shape[1]-1))

rawDescription: Store and find higher value index in a numpy array


code:
import datetime as dt
date_list = [dt.date(2011, 5, 31), dt.date(2011, 6, 15)]
print('[{}]'.format(', '.join(map(str,date_list))))

rawDescription: overriding default format when printing a list of datetime objects

description: Overriding default format when printing a list of datetime objects


code:
if __debug__:
    import os
    import sys
    os.execv(sys.executable, ['python', '-O'] + sys.argv)

rawDescription: Activating Python's optimization mode via script-level command-line argument


code:
def nth_matcher(n, replacement):
    def alternate(n):
        i=0
        while True:
            i += 1
            yield i%n == 0
    gen = alternate(n)
    def match(m):
        replace = gen.next()
        if replace:
            return replacement
        else:
            return m.group(0)
    return match

re.sub("([0-9])", nth_matcher(3, "X"), "1234567890")

rawDescription: How to replace the Nth appearance of a needle in a haystack? (Python)

description: Replace the Nth appearance of a needle in a haystack? (Python)


code:
cherrypy.config.update({
'environment': 'production',
'log.screen': False,
'log.access_file': '/home/adamek/webapps/cocoa_foody/logs/access.log',
'log.error_file': '/home/adamek/webapps/cocoa_foody/logs/error.log',
'server.socket_host': '127.0.0.1',
'server.socket_port': 15982,
'tools.caching.on': False,
'tools.encode.encoding': "utf-8",
'tools.json_in.on': True,
'tools.json_in.force': False,
})

rawDescription: CherryPy JSON vs Form POST

description: CherryPy JSON vs Form POST


code:
import time
from concurrent.futures import ThreadPoolExecutor
from tornado import gen, web
from tornado.concurrent import run_on_executor
from tornado.ioloop import IOLoop
import argparse
from common.config import APIConfig
import sys
import os
import motor

parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config-file", dest='config_file',
                    help="Config file location")
args = parser.parse_args()
CONF = APIConfig().parse(args.config_file)

client = motor.MotorClient(CONF.mongo_url)
db = client[CONF.mongo_dbname]

class Handler(web.RequestHandler):

    executor = ThreadPoolExecutor(10)

    def initialize(self):
        """ Prepares the database for the entire class """
        self.db = self.settings["db"]

    @gen.coroutine
    def get(self):
        self.blocking_task("Ping", "Void-R")

    @run_on_executor
    def blocking_task(self, name, status):
        IOLoop.instance().add_callback(callback=lambda: self.some_update(name, status))

    @gen.coroutine
    def some_update(self, name, status):
        mongo_dict = yield self.db.test_cases.find_one({"name": name})
        self.db.test_cases.update({ "name": name }, { "$set": { "status" : status } } )

if __name__ == "__main__":
    app = web.Application([
        (r"/", Handler),
    ],
        db=db,
        debug=CONF.api_debug_on,
    )
    app.listen(8888)
    IOLoop.current().start()

rawDescription: Tornado error: cannot switch to a different thread


rawDescription: Access Multiselect Form Field in Flask

description: Access Multiselect Form Field in Flask


code:
sys.stdin = open('path_to_input_file')
raw_input('Prompt: ')

rawDescription: Docker and single python file

description: Docker and single python file


code:
def procrustes(X, Y, scaling=True, reflection='best'):
    """
    A port of MATLAB's `procrustes` function to Numpy.

    Procrustes analysis determines a linear transformation (translation,
    reflection, orthogonal rotation and scaling) of the points in Y to best
    conform them to the points in matrix X, using the sum of squared errors
    as the goodness of fit criterion.

        d, Z, [tform] = procrustes(X, Y)

    Inputs:
    ------------
    X, Y
        matrices of target and input coordinates. they must have equal
        numbers of  points (rows), but Y may have fewer dimensions
        (columns) than X.

    scaling
        if False, the scaling component of the transformation is forced
        to 1

    reflection
        if 'best' (default), the transformation solution may or may not
        include a reflection component, depending on which fits the data
        best. setting reflection to True or False forces a solution with
        reflection or no reflection respectively.

    Outputs
    ------------
    d
        the residual sum of squared errors, normalized according to a
        measure of the scale of X, ((X - X.mean(0))**2).sum()

    Z
        the matrix of transformed Y-values

    tform
        a dict specifying the rotation, translation and scaling that
        maps X --> Y

    """

    n,m = X.shape
    ny,my = Y.shape

    muX = X.mean(0)
    muY = Y.mean(0)

    X0 = X - muX
    Y0 = Y - muY

    ssX = (X0**2.).sum()
    ssY = (Y0**2.).sum()

    # centred Frobenius norm
    normX = np.sqrt(ssX)
    normY = np.sqrt(ssY)

    # scale to equal (unit) norm
    X0 /= normX
    Y0 /= normY

    if my < m:
        Y0 = np.concatenate((Y0, np.zeros(n, m-my)),0)

    # optimum rotation matrix of Y
    A = np.dot(X0.T, Y0)
    U,s,Vt = np.linalg.svd(A,full_matrices=False)
    V = Vt.T
    T = np.dot(V, U.T)

    if reflection is not 'best':

        # does the current solution use a reflection?
        have_reflection = np.linalg.det(T) < 0

        # if that's not what was specified, force another reflection
        if reflection != have_reflection:
            V[:,-1] *= -1
            s[-1] *= -1
            T = np.dot(V, U.T)

    traceTA = s.sum()

    if scaling:

        # optimum scaling of Y
        b = traceTA * normX / normY

        # standarised distance between X and b*Y*T + c
        d = 1 - traceTA**2

        # transformed coords
        Z = normX*traceTA*np.dot(Y0, T) + muX

    else:
        b = 1
        d = 1 + ssY/ssX - 2 * traceTA * normY / normX
        Z = normY*np.dot(Y0, T) + muX

    # transformation matrix
    if my < m:
        T = T[:my,:]
    c = muX - b*np.dot(muY, T)

    #transformation values
    tform = {'rotation':T, 'scale':b, 'translation':c}

    return d, Z, tform

rawDescription: Procrustes Analysis with NumPy?

description: Procrustes Analysis with NumPy


code: queue.put((-priority, item))

rawDescription: Priority queue with higher priority first in Python


code:
a = soup.find(lambda tag: tag.name == 'a' and tag.has_attr('href') and 'Some text' in tag.text)
print(a['href'])
# -> 'https://alink.com'

rawDescription: find_all does not find text in mixed content

description: Find_all does not find text in mixed content


code:
from pyunpack import Archive
Archive('a.zip').extractall('/path/to')

rawDescription: How can unrar a file with python

description: How can unrar a file with python


code:
class DecoratorFactory(object):
    def __init__(self, value):
        self._value = value

    def decorator(self, function):
        def wrapper(*args, **kwargs):
            print(self._value)
            return function(*args, **kwargs)
        return wrapper

factory = DecoratorFactory("shared between all decorators")

@factory.decorator
def dummy1():
    print("dummy1")

@factory.decorator
def dummy2():
    print("dummy2")

# prints:
# shared between all decorators
# dummy1
dummy1()

# prints:
# shared between all decorators
# dummy2
dummy2()

rawDescription: Python initializing a memoizing decorator with settings


code:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import numpy as np
import scipy.cluster.vq as scv

def colormap2arr(arr,cmap):
    # http://stackoverflow.com/questions/3720840/how-to-reverse-color-map-image-to-scalar-values/3722674#3722674
    gradient=cmap(np.linspace(0.0,1.0,100))

    # Reshape arr to something like (240*240, 4), all the 4-tuples in a long list...
    arr2=arr.reshape((arr.shape[0]*arr.shape[1],arr.shape[2]))

    # Use vector quantization to shift the values in arr2 to the nearest point in
    # the code book (gradient).
    code,dist=scv.vq(arr2,gradient)

    # code is an array of length arr2 (240*240), holding the code book index for
    # each observation. (arr2 are the "observations".)
    # Scale the values so they are from 0 to 1.
    values=code.astype('float')/gradient.shape[0]

    # Reshape values back to (240,240)
    values=values.reshape(arr.shape[0],arr.shape[1])
    values=values[::-1]
    return values

arr=plt.imread('mri_demo.png')
values=colormap2arr(arr,cm.jet)
# Proof that it works:
plt.imshow(values,interpolation='bilinear', cmap=cm.jet,
           origin='lower', extent=[-3,3,-3,3])
plt.show()

rawDescription: how to reverse color map image to scalar values

description: Reverse color map image to scalar values


code:
from urllib import urlencode
data = [("foo", 10), ("bar", None), ("baz", "some_string")]
urlencode(data)
urlencode([x for x in data if x[1] is not None]) # no None's

rawDescription: How to process query parameters in form of a list of tuples?

description: Process query parameters in form of a list of tuples


code:
from tkinter import (Frame, Label, Entry, StringVar, Toplevel, Tk, Button)

class main(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.minsize(width=250, height=250)
        self.master.title("main window")
        self.grid()
        self.children_dict = dict()

        newWindowButton = Button(self,
                                 text="open new window",
                                 command=self.newWindowOpen)
        newWindowButton.grid(row=0, column=0)

    def newWindowOpen(self):
        childWindow = Toplevel()
        childWindow.wm_title("child window")

        childWindow.itemLabel = Label(childWindow, text="Test Value")
        childWindow.itemVar = StringVar()
        childWindow.itemEntry = Entry(childWindow,
                                      textvariable=childWindow.itemVar)

        self.children_dict[childWindow] = childWindow.itemVar
        childWindow.itemLabel.grid(row=0, column=0)
        childWindow.itemEntry.grid(row=0, column=1)

        childWindow.submitButton = Button(childWindow,
                                          text="submit",
                                          command=lambda: self.submitTest(childWindow))

        childWindow.submitButton.grid(row=1, column=0)

    def submitTest(self, childWindow):
        value = self.children_dict[childWindow].get()
        print(value)

root = Tk()
main_menu = main()
main_menu.mainloop()

rawDescription: Python tkinter - get value of toplevel field


code:
from pylab import *
from scipy import *
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import scipy.constants as const

skr = const.pi*const.epsilon_0*4

class DraggablePoint:
    global Polje
    def __init__(self, p, q0, ind):
        self.q = q0
        self.point = p
        self.c_kruznice = p.center
        self.press = None
        self.indeks = ind

    def connect(self):
        self.cidpress = self.point.figure.canvas.mpl_connect('button_press_event', self.button_press_event)
        self.cidrelease = self.point.figure.canvas.mpl_connect('button_release_event',self.button_release_event)
        self.cidmotion = self.point.figure.canvas.mpl_connect('motion_notify_event', self.motion_notify_event)

    def disconnect(self):
        self.point.figure.canvas.mpl_disconnect(self.cidpress)
        self.point.figure.canvas.mpl_disconnect(self.cidrelease)
        self.point.figure.canvas.mpl_disconnect(self.cidmotion)

    def button_press_event(self,event):
        if event.inaxes != self.point.axes:
            return
        contains = self.point.contains(event)[0]
        if not contains: return
        self.press = self.point.center, event.xdata, event.ydata

    def button_release_event(self,event):
        self.press = None
        self.point.figure.canvas.draw()
        # This is the part which will erase field
        # and draw for us new one
        self.c_kruznice = self.point.center[0], self.point.center[1]
        if self.indeks == 0:
            racun = polje(drs, X, Y)
            ax.cla()
            circles = []
            circles.append(ax.add_patch(circle1))
            circles.append(ax.add_patch(circle))
            circles.append(ax.add_patch(circle2))
            circles.append(ax.add_patch(circle3))
            ax.quiver(X,Y,racun[0],racun[1], color='r',  alpha=0.5)
            ax.quiver(X,Y,racun[0],racun[1], edgecolor='k', facecolor='None', linewidth =.5)
            plt.draw()

    def motion_notify_event(self, event):
        if self.press is None: return
        if event.inaxes != self.point.axes: returnO
        self.point.center, xpress, ypress = self.press
        dx = event.xdata - xpress
        dy = event.ydata - ypress
        self.point.center = (self.point.center[0]+dx, self.point.center[1]+dy)
        self.point.figure.canvas.draw()

if __name__ == '__main__':
    f_s=0.3
    def R(x,y):
        r=np.sqrt(x**2+y**2)+f_s
        return r

    def polje(tela, X, Y):
        Ex = 0
        Ey = 0
        for i in range(len(tela)):
            r = R(tela[i].c_kruznice[0] - X, tela[i].c_kruznice[1] - Y )
            ex = X - tela[i].c_kruznice[0]
            ey = Y - tela[i].c_kruznice[1]
            Ex += (tela[i].q/skr)*(1/(r+f_s)**3)*ex
            Ey += (tela[i].q/skr)*(1/(r+f_s)**3)*ey
        return Ex, Ey

    fig = plt.figure(figsize=(8,8))
    ax = fig.add_axes([0.05,0.05,0.92,0.92])
    ax.set_xlim(-6,6)
    ax.set_ylim(-6,6)
    scale = 0.2
    X,Y=np.mgrid[-5:5:scale, -5:5:scale]

    circles = []
    q=3*const.e
    s=abs(q)
    sx=sqrt(s/np.pi)*1e8*5
    circle2 = patches.Circle((2,3), 0.3, fc='r', alpha=0.5, picker=True)
    circle1 = patches.Circle((2,-3), 0.3, fc='r', alpha=0.5, picker=True)
    circle = patches.Circle((-2,3), 0.3, fc='b', alpha=0.5, picker=True)
    circle3 = patches.Circle((-2,-3), 0.3, fc = 'b', alpha = 0.5, picker = True )
    circles.append(ax.add_patch(circle1))
    circles.append(ax.add_patch(circle))
    circles.append(ax.add_patch(circle2))
    circles.append(ax.add_patch(circle3))
    drs = []

    i = 0
    q = [-1, 1, -1, 1]
    for c in circles:
        dr = DraggablePoint(c, q[i], i)
        dr.connect()
        drs.append(dr)
        i += 1

    racun = polje(drs, X, Y)
    ax.quiver(X,Y,racun[0],racun[1], color='r',  alpha=0.5)
    ax.quiver(X,Y,racun[0],racun[1], edgecolor='k', facecolor='None', linewidth =.5)
    plt.show()

rawDescription: How to update quiver plot after moving object on it?

description: Update quiver plot after moving object on it


code:
from bs4 import BeautifulSoup

html_doc ='
Age: 23 (10/21/1992)

Birthplace: Barranquilla, Colombia
'

bsObj = BeautifulSoup(html_doc, 'html.parser')

#
Age: 23 (10/21/1992)

birthday = bsObj.find( "div", {"class":"age"} )
birthday.span.clear()
print(birthday.get_text()) # 23 (10/21/1992)

#
Birthplace: Barranquilla, Colombia

birthplace = bsObj.find( "div", {"class":"hometown"} )
birthplace.span.clear()
print(birthplace.get_text()) # Barranquilla, Colombia

rawDescription: How to get text following a span with BeautifulSoup and Python?

description: Get text following a span with BeautifulSoup and Python


code:
def __call__(self, f):
    def wrapped_f(*args, **kwargs):
        if not permitted:
            raise Exception("not authenticated to do that")
        f(*args, **kwargs)
    return wrapped_f

rawDescription: Python Decorator with Arguments only called once


code: filelist = [line.rstrip('\n') for line in file]

rawDescription: remove line break from each element in python

description: Remove line break from each element in python


code:
yourdict = dict()

with open(file) as f:
   filedata = f.read().splitlines()
   for line in filedata:
       linedata = line.split()
       if linedata[0] not in yourdict:
           yourdict[linedata[0]] = []
       yourdict[linedata[0]].append(int(linedata[3]))
       yourdict[linedata[0]].append(int(linedata[4]))

for key in yourdict:
    yourdict[key] = [min(yourdict[key]), max(yourdict[key])]

rawDescription: Specific rows from CSV as dictionary and logic when keys are the same - Python


code:
setattr(
        self,
        user_method,
        lambda call=call: self.master.query(call)
        )

rawDescription: Python dynamic attribute value bind to method


code: super(Foo, self).__hash__()

rawDescription: Python - Using the default __hash__ method in __hash__ method definition


code:
import os
from subprocess import check_call

check_call([scriptB], cwd=os.path.dirname(scriptB))

rawDescription: Invoke python script from another python script and set execution directory of the executed script


code:
def admin_required(handler_method):
  def check_admin(self, *args):
    if not users.is_current_user_admin():
      self.redirect(users.create_login_url(self.request.uri))
      return
    else:
      handler_method(self, *args)
  return check_admin

rawDescription: does google-app-engine has "required_admin" method

description: Does google-app-engine has "required_admin" method


code:
def parse(self, response):
    url = response.xpath('//a[contains(@href,".interestingfileextension")]/@href').extract_first()
    return scrapy.Request(url, callback=self.parse_file)

def parse_file(self, response):
    # response here is the contents of the file
    print(response.body)

rawDescription: How do you open a file stream for reading using Scrapy?

description: Open a file stream for reading using Scrapy


rawDescription: Regular Expression for alpha/numeric characters, spaces and dashes


code:
with h5py.File('the_filename', 'r') as f:
    my_array = f['array_name'][()]

rawDescription: how to export HDF5 file to NumPy using H5PY?

description: Export HDF5 file to NumPy using H5PY


code:
from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.bind(("", 1234))
print(s.getsockname()) # prints ("0.0.0.0", 1234)
sq = socket(AF_INET, SOCK_DGRAM)
sq.connect(("10.0.0.3", 1234))
print(sq.getsockname()[0])
sq.close()
s.sendto("hello", ("10.0.0.3", 1234)) # sends from 10.0.0.2

rawDescription: Finding source IP-address when binding to 0.0.0.0


rawDescription: How can I lookup a named url in a view?

description: Lookup a named url in a view


code:
def createTable():
    queryCurs.execute(''' CREATE TABLE maths
    (id INTEGER PRIMARY KEY, userName TEXT, score INTERGER)''')

def addCust(userName,):
    queryCurs.execute("""INSERT INTO maths (userName, score)
    VALUES(?,?)""",(userName, score))

def main():
    createTable()

    createDb = sqlite3.connect("hhh.db")
    queryCurs = createDb.cursor()

    addCust("\n, They scored TEXT, score ITERGER, out of 10 TEXT")
    createDb.commit()

    queryCurs.execute("SELECT * FROM maths")

    for i in queryCurs:
      print ("\n")
      for j in i:
          print (j)

    queryCurs.close()

rawDescription: SQlite3 isn't displaying at end of code PYTHON


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

im2 = cv2.imread('norm1_zps89266edb.jpg')
im = im2.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
ret3,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

#we ony want the external contours
contours,hierarchy = cv2.findContours(thresh.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
#extract the countours with area > 50
squares = [cnt for cnt in contours if cv2.contourArea(cnt) > 50]

#mask array with the same shape as img (but only 1 channel)
mask = np.zeros((im.shape[0], im.shape[1]))
#draw the contours filled with 255 values.
cv2.drawContours(mask,squares,-1,255,-1)

newImage = np.where(mask==255, thresh, 255)

plt.imshow(newImage)
plt.show()

cv2.imwrite("cropped.jpg", newImage)

rawDescription: Letters blurry / fuzzy after crop function


code:
import pdfkit

def program2pdf(filename):

    programconfig = ProgramConfig.get_solo()
    programconfig.downloaded += 1
    programconfig.save()

    config = pdfkit.configuration(wkhtmltopdf=settings.PDF_WKHTMLTOPDF)

    content = render_to_string(
        'program/item2pdf.html', {
            'events': Event.objects.all(),
            'abs_path': settings.PDF_ADDRESS,
            'programconfig': programconfig
        }
    )

    pdf = pdfkit.PDFKit(content, "string", configuration=config).to_pdf()

    response = HttpResponse(pdf)
    response['Content-Type'] = 'application/pdf'
    response['Content-disposition'] = 'attachment;filename='
    response['Content-disposition'] += programconfig.get_filename

    return response

rawDescription: PDFkit write directly to the HTTP response


code:
import gtk

window = gtk.Window ()
align  = gtk.Alignment (0.5, 0.5)
button = gtk.Button ('a very long, possibly multiline text')

label = button.child
label.props.wrap = True
label.props.width_chars = 20

window.set_default_size (500, 500)
window.connect ('destroy', lambda *ignored: gtk.main_quit ())

window.add (align)
align.add (button)
window.show_all ()

gtk.main ()

rawDescription: How to Create a Multiline Button

description: Create a Multiline Button


code:
listo = [('a','1'),('b','0'),('c','2'),('d','0')]
minValue = min(listo, key=lambda x: x[1])[1]
minValueList = [x for x in listo if x[1] == minValue]

rawDescription: Can I return a list of ALL min tuples, using Python's Min function?

description: Return a list of ALL min tuples, using Python's Min function


code:
def perms(word):
    stack = list(word)
    results = [stack.pop()]
    while len(stack) != 0:
        c = stack.pop()
        new_results = []
        for w in results:
            for i in range(len(w)+1):
                new_results.append(w[:i] + c + w[i:])
        results = new_results
    return results

rawDescription: Iterative solution for :- Finding String permutations


code:
class plotPanel(wx.Panel):
    def __init__(self, parent):
        ...
        self.colorbar = None

    def anim(self, a):
        ...
        self.subplot.set_title("Frame %i" % a)
        if self.colorbar is None:
            self.colorbar = self.figure.colorbar(obj)
        else:
            self.colorbar.update_normal(obj)
        self.colorbar.update_normal(obj)
        return obj

rawDescription: matplotlib animation in wxpython panel

description: Matplotlib animation in wxpython panel


code:
def check_token(self, user, token):
    """
    Check that a password reset token is correct for a given user.
    """
    # Parse the token
    try:
        ts_b36, hash = token.split("-")
    except ValueError:
        return False

    try:
        ts = base36_to_int(ts_b36)
    except ValueError:
        return False

    # Check that the timestamp/uid has not been tampered with
    if not constant_time_compare(self._make_token_with_timestamp(user, ts), token):
        return False

    # Check the timestamp is within limit
    if (self._num_days(self._today()) - ts) > settings.PASSWORD_RESET_TIMEOUT_DAYS:
        return False

    return True

rawDescription: Password reset token in Python Google App Engine


code:
CHUNK_SIZE = 23000
ports_list_save = []
for host in results['hosts']:
    for protocol in results['hosts'][host]['protocols']:
        for port in results['hosts'][host]['protocols'][protocol]:
            current_port = history.Port(number=int(port),
                                        protocol=protocol,
                                        state=results['hosts'][host]['protocols'][protocol][port]['state'],
                                        service='',
                                        version='',
                                        address=history.Ip.objects.get(scan=self.scan, address=host))
            ports_list_save.append(current_port)
            if len(ports_list_save) > CHUNK_SIZE:
                history.Port.objects.bulk_create(ports_list_save)
                ports_list_save = []
if ports_list_save:
    history.Port.objects.bulk_create(ports_list_save)

rawDescription: Django memory leak

description: Django memory leak


code:
import StringIO
import unittest

# The TestCase subclass to test
class MyBaseTestCase(unittest.TestCase):
    def assertSpec(self, thing):
        assert thing == 123

# The testcase for MyBaseTestCase
class TestMyTest(unittest.TestCase):
    def test_assetSpec(self):
        """Ensure assertSpec works
        """

        class _TestSpec(MyBaseTestCase):
            def test_failure_case(self):
                 self.assertSpec(121)
            def test_success_case(self):
                 self.assertSpec(123)

        # Load tests from _TestSpec
        loader = unittest.TestLoader()
        suite = loader.loadTestsFromTestCase(_TestSpec)

        # Create runner, and run _TestSpec
        io = StringIO.StringIO()
        runner = unittest.TextTestRunner(stream = io)
        results = runner.run(suite)

        # Should be one failed test, and one passed test
        self.assertEquals(results.testsRun, 2)
        self.assertEquals(len(results.failures), 1)

if __name__ == "__main__":
    unittest.main()

rawDescription: How to unittest unittest TestCases

description: Unittest unittest TestCases


code:
from flask import redirect, url_for, request
...
# duck = request.form.get('search_term')
return redirect(url_for('search', search_term=duck, page=0, per_page=20))
...

rawDescription: Flask -- pass arguments to outgoing url.


code:
import sys
class MyCherryPyApplication(object):

  def default(self):
    sys.exit()
  default.exposed = True

cherrypy.quickstart(MyCherryPyApplication())

rawDescription: How to shutdown cherrypy from within?

description: Shutdown cherrypy from within


code:
d = {'Jim': [4, 2, 6], 'Fred': [5, 3, 8], 'Neil': [5, 1, 7]}
{i:sorted(j, reverse=True) for i,j in d.items()}
{i:max(j) for i,j in d.items()}

rawDescription: Sort the lists in a dictionary with lists as values


code:
class Animal(models.Model):
    def __unicode__(self):
        if hasattr(self, 'dog'):
            return self.dog.__unicode__()
        elif hasattr(self, 'cat'):
            return self.cat.__unicode__()
        else:
            return 'Animal'

rawDescription: Django Admin Interface Does Not Use Subclass's __unicode__()


code: jenni.write(['PRIVMSG','target_nick'],"Write your message here")

rawDescription: phenny/jenni IRC bot programming

description: Phenny/jenni IRC bot programming


code:
key.content_type = 'text/html'
key.set_contents_from_filename(content)

rawDescription: Automatically set boto content type

description: Automatically set boto content type


code:
#!/usr/bin/python

import csv
import json

csvfile = [
    '"name","age","grade"',
    '"ann",42,1.3',
    '"hans",23,1.7'
]
row_types = {'name': str, 'grade': float, 'age': int}

reader = csv.DictReader(csvfile)

jsonfile = open('test.json', 'w')
for row in reader:
    print('reader produces strings only:')
    print(row)
    print('convert to known types')
    row_converted = {k: row_types[k](v) for k, v in row.items()}
    print(row_converted)
    json.dump(row_converted, jsonfile)
    jsonfile.write('\n')

rawDescription: keep variable type in json.dump

description: Keep variable type in json.dump


code:
def f(x):
    ref_dict = {
        'a': 1,
        'b': 2,
    }

    return_tone = ref_dict[x]
    return return_tone

rawDescription: Writing a dictionary to replace switch statements in functions


code:
colourImgArray = []
sized = 'sometitle'
colourImgArray.extend(sized)
colourImgArray
colourImgArray = []
colourImgArray.append(sized)
colourImgArray

rawDescription: Python .extend splitting Characters unnecessarily


code:
notes = driver.find_elements_by_xpath("//*[contains(text(),'%')]")
notes = [note.text for note in notes]

rawDescription: Turn Array of Selenium Webdriver Elements into their values?


code:
canvas = Canvas(root,width=1200,height=800)
canvas.label = Label(canvas)

rawDescription: tkinter access child from parent

description: Tkinter access child from parent


code:
finder = BigramCollocationFinder.from_words(fw_2)
# only bigrams that appear 3+ times
finder.apply_freq_filter(2)
scored = finder.score_ngrams(bgm.likelihood_ratio)
fw_2 = [i for i in scored[0:20] if i[1] > 15]

rawDescription: Why doesn't my list comprehension code sort things immediately?


code:
data = pd.read_csv('output_list.txt', sep=" ", header = None)
data.columns = ["a", "b", "c", "etc."]

rawDescription: Load data from txt with pandas

description: Load data from txt with pandas


code:
client = pymongo.MongoClient()

# some code here

client.close()

rawDescription: How to close a mongodb python connection?

description: Close a mongodb python connection


code:
from urllib import urlopen
urlopen("http://****.com/win_new_lines.htm").read()
urlopen("http://****.com/unix_new_lines.htm").read()

rawDescription: How can I download and read a URL with universal newlines?

description: Download and read a URL with universal newlines


code:
from datetime import datetime, timedelta
dt = datetime(1970 ,1 ,1)
t = timedelta(minutes=30)
tuple((str(i), (dt + (i * t)).strftime("%X")) for i in range(48))

rawDescription: How Can python create tuple of tuple with time elements?


rawDescription: Python: Run a function of each n-th element of the lists contained in a list


code:
import tkinter as tk
from time import time

update = 1000
def L1(label):
    def hashrate():
        msg = "Your current GH/S = "
        speed = msg , time()
        label.config(text=speed)
        label.after(update, hashrate)
    hashrate()
root = tk.Tk()
root.title("Eyes On Miner GUI V0.2")
label = tk.Label(root)
label.pack()
L1(label)

rawDescription: Python Tkinter Label Refresh Woes

description: Python Tkinter Label Refresh Woes


code:
# Use fetchall():
((points,),) = cursor.fetchall()  # points = 56L

# Or, if you use fetchone():
(points,) = cursor.fetchone()     # points = 56L

rawDescription: cursor.fetchall() returns extra characters using MySQldb and python

description: Cursor.fetchall() returns extra characters using MySQldb and python


code:
s = sched.scheduler(time.time, time.sleep)
map(s.cancel, s.queue)

rawDescription: Python sched alternative to cancel all events


code:
from PIL import ImageGrab, Image
import win32gui

hwnd = 2622054 # My python intepreter window
thumbnailsize = 128, 128

# Set the current window to your window
win32gui.SetForegroundWindow(hwnd)
# Get the size of the window rect.
bbox = win32gui.GetWindowRect(hwnd)
# Grab the image using PIL and thumbnail.
img = ImageGrab.grab(bbox)
img.thumbnail(thumbnailsize, Image.ANTIALIAS)
# Save.
img.save('c:/test/test.png')

rawDescription: How can capture a window photo (thumbnail)?


code:
import collections

exons = collections.defaultdict(list)
firstvalues = {}
with open('test_coding.txt') as f:
    for line in f:
        chrom, start, end, isoform = line.split()
        if isoform not in firstvalues:
            firstvalues[isoform] = int(start)
        exons[isoform].append((int(start) - firstvalues[isoform], int(end) - firstvalues[isoform]))

rawDescription: correcting values in a dictionary

description: Correcting values in a dictionary


code:
from pymouse import PyMouseEvent

def fibo():
    a = 0
    yield a
    b = 1
    yield b
    while True:
        a, b = b, a+b
        yield b

class Clickonacci(PyMouseEvent):
    def __init__(self):
        PyMouseEvent.__init__(self)
        self.fibo = fibo()

    def click(self, x, y, button, press):
        '''Print Fibonacci numbers when the left click is pressed.'''
        if button == 1:
            if press:
                print(self.fibo.next())
        else:  # Exit if any other mouse button used
            self.stop()

C = Clickonacci()
C.run()

rawDescription: How to detect mouse click in python 3 on linux?

description: Detect mouse click in python 3 on linux


code:
def base_3_to_seq(base3):
   for seq in itertools.product(range(3), repeat=3):
       if base_3(seq) == base3:
           return seq
   return "Impossible"

rawDescription: Python Sequence elements comparison

description: Python Sequence elements comparison


code:
file_path = r"d:\folder\file.csv"
cursor.execute("LOAD data local INFILE %s INTO TABLE test ...);", file_path)

rawDescription: Python: double Backslash formatting

description: Python: double Backslash formatting


code: bc = 'on' if c.page=='blog' else 'off'

rawDescription: Shorter, more pythonic way of writing an if statement


code:
data = ['a,x', 'b,y', 'c,z']
f = open('data.csv', 'wb')
w = csv.writer(f, delimiter = ',')
w.writerows([x.split(',') for x in data])
f.close()

rawDescription: Write list of comma separate strings to csv file in Python


code:
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from tastypie.http import HttpUnauthorized, HttpForbidden
from django.conf.urls import url
from tastypie.utils import trailing_slash

class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        fields = ['first_name', 'last_name', 'email']
        allowed_methods = ['get', 'post']
        resource_name = 'user'

    def override_urls(self):
        return [
            url(r"^(?P<resource_name>%s)/login%s$" %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('login'), name="api_login"),
            url(r'^(?P<resource_name>%s)/logout%s$' %
                (self._meta.resource_name, trailing_slash()),
                self.wrap_view('logout'), name='api_logout'),
        ]

    def login(self, request, **kwargs):
        self.method_check(request, allowed=['post'])

        data = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))

        username = data.get('username', '')
        password = data.get('password', '')

        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                login(request, user)
                return self.create_response(request, {
                    'success': True
                })
            else:
                return self.create_response(request, {
                    'success': False,
                    'reason': 'disabled',
                    }, HttpForbidden )
        else:
            return self.create_response(request, {
                'success': False,
                'reason': 'incorrect',
                }, HttpUnauthorized )

    def logout(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        if request.user and request.user.is_authenticated():
            logout(request)
            return self.create_response(request, { 'success': True })
        else:
            return self.create_response(request, { 'success': False }, HttpUnauthorized)

rawDescription: How can I login to django using tastypie

description: Login to django using tastypie


code:
def find_adjacent_coords(db, lat, lon, step=0.041667):
    """Find coords that are in a +/- step range of lat, lon."""
    #XXX disregard values near +/- 90 latitude, +/- 180 longitude
    coords_range = lat-step, lat+step, lon-step, lon+step
    return db.execute("""select lat, lon from coords where
lat > ? and lat < ? and
lon > ? and lon < ?""", coords_range).fetchall()

rawDescription: How to determine 4 nearest adjacent points in grid database around given latitude and longitude using python

description: Determine 4 nearest adjacent points in grid database around given latitude and longitude using python


code:
class LangueAdmin(BaseAdmin):
    fields = BaseAdmin.fields + ('name',)

rawDescription: Django admin: is there a way to know fields of the model?


code:
def exitDevice(ip,m=None,sendExit=True):
   if m is None: m = getDefaultValueForM()
   if sendExit: m.send ( 'exit' )

rawDescription: Default parameter in python with objects


code:
def largest(sequence):
    """
    This is based on Bentley's Programming Pearls chapter 8.
    My modification: if the sequence is all negatives
    then max is the largest element
    """
    max_so_far = max_up_to_here = 0
    largest_element = sequence[0]
    all_negatives = True

    for element in sequence:
        max_up_to_here= max(max_up_to_here + element, 0)
        max_so_far = max(max_so_far, max_up_to_here)
        largest_element = max(largest_element, element)
        if element >= 0:
            all_negatives = False

    if all_negatives:
        return largest_element
    return max_so_far

rawDescription: Best way to find the most maximum sum of a range in array

description: Find the most maximum sum of a range in array


rawDescription: Pandas convert_object(convert_numeric=True) not producing np.nan for full series of non-numeric values


code:
import contextlib
from selenium import webdriver

class closing(contextlib.closing):
    def __exit__(self, *exc_info):
        self.thing.quit()

with closing(webdriver.Firefox()) as browser:
    browser.get('http://stackoverflow.com')

rawDescription: Is it possible to have contextlib.closing() to call an arbitrary cleanup method instead of .close()


rawDescription: Splitting a list into a pairs in Python

description: Splitting a list into a pairs in Python


code:
sqla_metadata = sqlalchemy.schema.MetaData()

type_mapping = {'int': Integer, 'text': String} # etc.
cols = []
for (col_name, col_type) in your_parsed_metadata.fields:
    cols.append(Column(col_name, type_mapping[col_type]))

cols.append(Column('datafile_id', Integer, ForeignKey("datafile.datafile_id"), nullable=False),)
new_table = Table(your_parsed_metadata.tablename, sqla_metadata, *cols)
sqla_metadata.create_all(engine)

rawDescription: SQLAlchemy model definition at execution


code:
newButton = QRadioButton(...)
newText = newButton.text()

index = 0
for button in get_buttons():
    if button.text() >= newText:
      break
    index += 1

layout.insertWidget(index, newButton)

rawDescription: PyQt Sort List of Radio Buttons

description: PyQt Sort List of Radio Buttons


code:
def on_click_decorator(key):
    def real_decorator(func):
        setattr(func, "_callback_to", key)
        return func
    return real_decorator

class Meta(type):
    def __new__(metacls, name, bases, dct):
        callback_stack = {}
        for name, value in dct.items():
            if hasattr(value, "_callback_to"):
                callback_stack[value._callback_to] = key
        dct["callback_stack"] = callback_stack
        # we've done all needed manipulation - no
        # need for the class to be from a special type - so we
        # don't call type.__new__
        return type(name, bases, dct)

rawDescription: Is that possible to make this kind of decorator in python (2.4)?


code:
import csv
import itertools

with open("Source 1.csv", "rb") as source1, \
     open("Source 2.csv", "rb") as source2, \
     open("merged_output.csv", "wb") as merged_output:

    source1_reader = csv.reader(source1, delimiter=',')
    source2_reader = csv.reader(source2, delimiter=',')
    merged_output_writer = csv.writer(merged_output, delimiter=',')

    for row1, row2 in itertools.izip(source1_reader, source2_reader):
        merged_output_writer.writerow(
            tuple(itertools.chain.from_iterable(itertools.izip(row1, row2))))

rawDescription: Merge two spreadsheets using python - Source of columns in new sheets alternates between source files


rawDescription: CRC32 checksum in Python with hex input

description: CRC32 checksum in Python with hex input


code:
for line in myfile:
  row = csv.reader([line]).next()

rawDescription: How  to know the byte position of a row of a CSV file in python?


code:
class Foo:
    def __init__(self, text):
        self.text = text
    def __str__(self):
        return self.text
    def __add__(self, other):
        return str(self) + other
    def __radd__(self, other):
        return other + str(self)

rawDescription: How to concatenate `Object` with a string?

description: Concatenate `Object` with a string


code:
import pandas as pd

# Some sample data to plot.
list_data = [30, 40, 50, 40, 20, 10, 5]

# Create a Pandas dataframe from the data.
df = pd.DataFrame(list_data)

# Create a Pandas Excel writer using XlsxWriter as the engine.
excel_file = 'testfile.xlsx'
sheet_name = 'Sheet1'

writer = pd.ExcelWriter(excel_file, engine='xlsxwriter')
df.to_excel(writer, sheet_name=sheet_name)

# Access the XlsxWriter workbook and worksheet objects from the dataframe.
# This is equivalent to the following using XlsxWriter on its own:
#
#    workbook = xlsxwriter.Workbook('filename.xlsx')
#    worksheet = workbook.add_worksheet()
#
workbook = writer.book
worksheet = writer.sheets[sheet_name]

# Apply a conditional format to the cell range.
worksheet.conditional_format('B2:B8', {'type': '3_color_scale'})

# Close the Pandas Excel writer and output the Excel file.
writer.save()

rawDescription: Easiest way to create a color gradient on excel using python/pandas?


code:
### module1.py:
def main():
    # does something
    ...

if __name__ == '__main__':
    main()

### module2.py:
from module1 import main as main_from_module_one

if __name__ == '__main__':
    main_from_module_one()  # calling function main defined in module1

rawDescription: Calling if __name__ == '__main__': in one module from a function in another module


rawDescription: Matplotlib ColorbarBase: delete color separators


code:
a = np.array([1, 2, 3])
t = tuple(a)

rawDescription: How do I construct a tuple in Cython?

description: Construct a tuple in Cython


code:
setA = set(listA)
setB = set(listB)
new_list = list(setB - setA)

rawDescription: what is better way to get different from two list

description: What is better way to get different from two list


code:
import types

class PropertyFromCompute(property):

    def __init__(self, func):
        self.func = None
        self.func_name = func.__name__
        self.internal_prop_name = self.func_name.replace('compute', '')

    def fget(self, obj):
        try:
            return obj.__getattribute__(self.internal_prop_name)
        except AttributeError:
            obj.__setattr__(self.internal_prop_name, self.func())
            return obj.__getattribute__(self.internal_prop_name)

    def __get__(self, obj, objtype=None):
        if obj is None:
            return self
        if self.func is None:
            try:
                self.func =  obj.__getattribute__(self.func_name)
            except AttributeError:
                raise AttributeError("unreadable attribute")
        return self.fget(obj)

class WithPropertyfromCompute(type):

    def __new__(cls, clsname, bases, dct):
        add_prop = {}
        for name, obj in dct.items():
            if isinstance(obj, types.FunctionType) and name.startswith('compute_'):
                add_prop.update({name.replace('compute_',''): PropertyFromCompute(obj)})
        dct.update(add_prop)
        return super().__new__(cls, clsname, bases, dct)

class myClass(object, metaclass=WithPropertyfromCompute):

    def compute_foo(self):
        raise NotImplementedError('Do not instantiate the base class, ever !')

class myChildClass(myClass):

    def compute_foo(self):
        return 4

base = myClass()
try:
    print(base.foo)
except NotImplementedError as e:
    print(e)
print(myClass.foo)
child = myChildClass()
print(child.foo)

rawDescription: Python decorator with arguments

description: Python decorator with arguments


rawDescription: Retrieve block of text from a file, using key-words as start-point in Python 2.7?


code:
for i,n in sorted(counter.items()):
    binrange='{}-{}'.format(i*10, (i + 1) * 10 - 1)
    print('{:<12}{:<4}'.format(binrange, n))

rawDescription: String format alignment

description: String format alignment


code:
import itertools
a=[1,2,3]
b=[4,5,6]
itertools.product(a,b)
list(itertools.product(a,b))

rawDescription: Pairwise crossproduct in Python

description: Pairwise crossproduct in Python


code:
class dequePlus(collections.deque):
    def __init__(self, iterable):
        collections.deque.__init__(self, iterable, 100)

rawDescription: how to set maxlen when subclassing a deque

description: Set maxlen when subclassing a deque


rawDescription: How can I round an integer to the nearest 20th?

description: Round an integer to the nearest 20th


code:
import signal, sys, time, pstats, cProfile

call = 0

def sigint_handler(signal, frame):
  global call
  if call < 2:
    stats = pstats.Stats(profiler)
    stats.strip_dirs().sort_stats('cumulative').print_stats()
    profiler.enable()
    call += 1
  else:
    sys.exit()

def wait():
  time.sleep(1)

def main_io_loop():
  signal.signal(signal.SIGINT, sigint_handler)
  while 1:
    wait()

profiler = cProfile.Profile()
profiler.runctx("main_io_loop()", globals(), locals())

rawDescription: Succesive calls to cProfile/pstats no updating properly


code:
def premultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255:
                r = r * a // 255
                g = g * a // 255
                b = b * a // 255
                pixels[x, y] = (r, g, b, a)

def unmultiply(im):
    pixels = im.load()
    for y in range(im.size[1]):
        for x in range(im.size[0]):
            r, g, b, a = pixels[x, y]
            if a != 255 and a != 0:
                r = 255 if r >= a else 255 * r // a
                g = 255 if g >= a else 255 * g // a
                b = 255 if b >= a else 255 * b // a
                pixels[x, y] = (r, g, b, a)

rawDescription: Scale images with PIL preserving transparency and color?


rawDescription: What's the corresponding multielement operator version of "numpy.logical_or"?


code:
from PIL import Image
import PIL.ImageOps

image = Image.open('your_image.png')

inverted_image = PIL.ImageOps.invert(image)

inverted_image.save('new_name.png')

rawDescription: How to invert colors of image with PIL (Python-Imaging)?

description: Invert colors of image with PIL (Python-Imaging)


code:
from lxml import etree

xmltree = etree.fromstring(your_xml)

for footnote in xmltree.iterfind("//footnotes/footnote"):
   # do something
   pass

rawDescription: How can I get the only element of a certain type out of a list more cleanly than what I am doing?

description: Get the only element of a certain type out of a list more cleanly than what I am doing


code:
import numpy as N

br = ['14 0.000000 -- (long term 0.000000)\n']*50000
aa = N.zeros((len(br), 2))

for i,line in enumerate(br):
    al, strs = aa[i], line.split(None, 2)[:2]
    al[0], al[1] = float(strs[0]), float(strs[1])

rawDescription: Converting a list of strings in a numpy array in a faster way


code:
from tkinter import *
from tkinter import ttk

root_window = Tk()

estyle = ttk.Style()
estyle.element_create("plain.field", "from", "clam")
estyle.layout("EntryStyle.TEntry",
                   [('Entry.plain.field', {'children': [(
                       'Entry.background', {'children': [(
                           'Entry.padding', {'children': [(
                               'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})],
                      'border':'2', 'sticky': 'nswe'})])

estyle.configure("EntryStyle.TEntry",
    fieldbackground="light blue")           # Set color here

entry = ttk.Entry(root_window, style="EntryStyle.TEntry")
entry.pack(padx=10, pady=10)

root_window.mainloop()

rawDescription: Python Tkinter custom themes

description: Python Tkinter custom themes


code:
import dbus
bus = dbus.Bus()

service_name = "org.gnome.Rhythmbox"
sobj_name = "/org/gnome/Rhythmbox/Shell"
siface_name = "org.gnome.Rhythmbox.Shell"

def set_rating(uri, rating):
    searchobj = bus.get_object(service_name, sobj_name)
    shell = dbus.Interface(searchobj, siface_name)
    shell.setSongProperty(uri, "rating", float(rating))

rawDescription: Rhythmbox: how do I access the 'rating' field of a track through Python script?


code:
datelist =  ['2014-05-06', '2014-05-07', '2014-05-08', '2014-05-09', '2014-05-10',    '2014-05-11', '2014-05-12', '2014-05-13']

import matplotlib
from matplotlib import pyplot
from matplotlib import dates
import datetime

converted_dates = map(datetime.datetime.strptime, datelist, len(datelist)*['%Y-%m-%d'])
x_axis = (converted_dates)
formatter = dates.DateFormatter('%Y-%m-%d')

y_axis = range(0,8)
pyplot.plot( x_axis, y_axis, '-' )
ax = pyplot.gcf().axes[0]
ax.xaxis.set_major_formatter(formatter)
pyplot.gcf().autofmt_xdate(rotation=25)
pyplot.show()

rawDescription: matplotlib string to dates

description: Matplotlib string to dates


code:
no_dupes = set()

for row in reader:
    type, year = row[0], row[1]
    values_list = row[2:]

    for index, value in enumerate(values_list, start=1):
        line = (type, year, index, value)
        no_dupes.add(line)

for t in no_dupes:
    writer.writerow(t)

rawDescription: Filter-Out Duplicate Table Entries

description: Filter-Out Duplicate Table Entries


code:
result = eval("""[klass for klass in ''.__class__.__base__.__subclasses__()
            if klass.__name__ == "BuiltinImporter"][0].load_module("builtins")""",
    {"__builtins__":None},{})
assert result is __builtins__

rawDescription: How `eval()` is not 'dangerous' in this example


code:
import inspect
def name_and_args():
    caller = inspect.stack()[1][0]
    args, _, _, values = inspect.getargvalues(caller)
    return [(i, values[i]) for i in args]

def f(a, b, c):
    print(name_and_args())
f(1, 2, 3)

rawDescription: Python: how to print function's arguments in the CORRECT ORDER


code:
sentence = 'hello I am cool'
list(sentence)

rawDescription: How do I split this string in python 2.7 keeping spaces?

description: Split this string in python 2.7 keeping spaces


code: cursor.execute("update Gps_table set longitude=?,latitude=? where gps_id=1", s1, s2)

rawDescription: Unable to update Multiple cloums in a sql database table using pyodbc


code: min(iterable, key=func)

rawDescription: Returning the argument from a list comprehension that's result satisfies <something>


code:
import datetime
diff = datetime.datetime.utcnow() - datetime.datetime(1900, 1, 1, 0, 0, 0)
timestamp = diff.days*24*60*60+diff.seconds
timestamp
# returns
# 3531049334

rawDescription: NTP timestamps in Python

description: NTP timestamps in Python


code:
def update_some_row():
    some = db.some(request.vars["id"]) or redirect(URL('error'))
    column = {request.vars["columnName"].lower():request.vars["value"]}
    some.update_record(**column)
    return request.vars["value"]

rawDescription: jQuery DataTables autorefresh

description: JQuery DataTables autorefresh


code:
query_string = """MATCH (n)-[r]-(m)
                RETURN n, n.node_id, n.name, n.type, n.description, m.node_id, m.name, m.type, m.description"""
query = neo4j.CypherQuery(graph_db, query_string)
results = query.execute().data

rawDescription: Fastest way to get a large number of nodes from Neo4j using py2neo


code:
xmldoc = minidom.parse('your.xml')
matchingNodes = [node for node in xmldoc.getElementsByTagName("id") if node.nodeValue == '345754']

rawDescription: Python: Using minidom to search for nodes with a certain text


code:
class Foo(object):
    def blah(self):
        print("Blah!")

    def __getitem__(self, attr):
        return getattr(self, attr)

f = Foo()

exec('blah()', globals(), f)

rawDescription: Make instance methods global in module?

description: Make instance methods global in module


code:
#urls.py

urlpatterns = patterns('',

    url(r'^downloads/(?P<parent_id>\d+)/$', ParentView.as_view(), name='parents_list'),
    url(r'^downloads/(?P<parent_id>\d+)/(?P<child_id>\d+)/$', ChildView.as_view(),
                                    name='childs_list'),
)

rawDescription: How to display tree structure with hyperlinks in django views

description: Display tree structure with hyperlinks in django views


code:
a
i = np.argsort(a['1st'], kind='mergesort')
a[i]

rawDescription: Numpy shuffle rows then sort by one column only


code:
def find(node):
    if node.parent != node:
        node.parent = find(node.parent)
    return node.parent

rawDescription: Disjoint-Set forests in Python alternate implementation


code:
for k in d.keys():
    if k.startswith('Prefix'):
        d.pop(k)

rawDescription: Efficient way of removing keys of certain prefix from python dictionary


code:
def fit_transform(self, X, y=None):
    """Fit LSI model to X and perform dimensionality reduction on X.
    Parameters
    ----------
    X : {array-like, sparse matrix}, shape (n_samples, n_features)
        Training data.
    Returns
    -------
    X_new : array, shape (n_samples, n_components)
        Reduced version of X. This will always be a dense array.
    """

rawDescription: Python LSA with Sklearn

description: Python LSA with Sklearn


code:
import time

# Get input
date = input("Enter a date: ")
# Convert to timestamp
date_timestamp = time.mktime(time.strptime(date, "%d/%m/%Y"))

# Check if in future
if(date_timestamp > time.time()):
    print("It's in the future,")
else:
    print("It's not in the future,")

rawDescription: How do i convert a date string from user input to date.today() python

description: Convert a date string from user input to date.today() python


code:
def foo(i):
    if i > 5:
            y = 2
            return True, y
    return False,0
for i in range(0,10):
    x,y = foo(i)
    if x:
            ret_value = y
    else:
            ret_value = 0
    print(ret_value)
type(foo(6))        #Example to show type of value returned.

rawDescription: Returning a Boolean and a variable at same time in a function ? Python


code:
def transpose(iterable_of_three_tuples):
    teed = itertools.tee(iterable_of_three_tuples, 3)
    return ( e[0] for e in teed[0] ),  ( e[1] for e in teed[1] ),  ( e[2] for e in teed[2] )

rawDescription: Lazily transpose a list in Python

description: Lazily transpose a list in Python


rawDescription: Handling Pandas dataframe columns with mixed date formats


code:
#!/usr/bin/python2
import time

startup_time = time.ctime()

def application(environ, start_response):
    current_time = time.ctime()
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return [startup_time, '\n', current_time]

if __name__ == '__main__':
    from wsgiref import simple_server
    srv = simple_server.make_server('localhost', 8080, application)
    srv.serve_forever()

rawDescription: WSGI - Why does it cache the output for : os.popen("date").read() in multi threads and picks one randomly?


code:
from celery import Celery
celery = Celery()
celery.config_from_object('celeryconfig')
celery.send_task('tasks.add', (2,2))

rawDescription: Celery - How to send task from remote machine?


code:
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy import create_engine, Integer, Column, \
                    String, and_
from sqlalchemy.orm import Session, relationship, foreign, remote, backref
from sqlalchemy import event

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

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

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

    This represents all address records in a
    single table.

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

    discriminator = Column(String)
    """Refers to the type of parent."""

    parent_id = Column(Integer)
    """Refers to the primary key of the parent.

    This could refer to any table.
    """

    @property
    def parent(self):
        """Provides in-Python access to the "parent" by choosing
        the appropriate relationship.

        """
        return getattr(self, "parent_%s" % self.discriminator)

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

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

    """

@event.listens_for(HasAddresses, "mapper_configured", propagate=True)
def setup_listener(mapper, class_):
    name = class_.__name__
    discriminator = name.lower()
    class_.addresses = relationship(Address,
                        primaryjoin=and_(
                                        class_.id == foreign(remote(Address.parent_id)),
                                        Address.discriminator == discriminator
                                    ),
                        backref=backref(
                                "parent_%s" % discriminator,
                                primaryjoin=remote(class_.id) == foreign(Address.parent_id)
                                )
                        )
    @event.listens_for(class_.addresses, "append")
    def append_address(target, value, initiator):
        value.discriminator = discriminator

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

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

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

session = Session(engine)

session.add_all([
    Customer(
        name='customer 1',
        addresses=[
            Address(
                    street='123 anywhere street',
                    city="New York",
                    zip="10110"),
            Address(
                    street='40 main street',
                    city="San Francisco",
                    zip="95732")
        ]
    ),
    Supplier(
        company_name="Ace Hammers",
        addresses=[
            Address(
                    street='2569 west elm',
                    city="Detroit",
                    zip="56785")
        ]
    ),
])

session.commit()

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

rawDescription: sqlalchemy generic foreign key (like in django ORM)

description: Sqlalchemy generic foreign key (like in django ORM)


code:
def rolling_window(arr, window):
    """Very basic multi dimensional rolling window. window should be the shape of
    of the desired subarrays. Window is either a scalar or a tuple of same size
    as `arr.shape`.
    """
    shape = np.array(arr.shape*2)
    strides = np.array(arr.strides*2)
    window = np.asarray(window)
    shape[arr.ndim:] = window # new dimensions size
    shape[:arr.ndim] -= window - 1
    if np.any(shape < 1):
        raise ValueError('window size is too large')
    return np.lib.stride_tricks.as_strided(arr, shape=shape, strides=strides)

# Now:
slices = rolling_window(arr, 2)
# Slices will be 4-d not 3-d as you wanted. You can reshape
# but it may need to copy (not if you have done no slicing, etc. with the array):
slices = slices.reshape(-1,slices.shape[2:])

rawDescription: Slicing a numpy image array into blocks

description: Slicing a numpy image array into blocks


code:
def open_popup(self, cr, uid, ids, context=None):
    mod_obj = self.pool.get('ir.model.data')
    if move.parent_production_id:
        res = mod_obj.get_object_reference(cr, uid, 'module_name', 'id_specified_for_the_view')
        return {
            'name': 'Provide your popup window name',
            'view_type': 'form',
            'view_mode': 'form',
            'view_id': [res and res[1] or False],
            'res_model': 'your.popup.model.name',
            'context': "{}",
            'type': 'ir.actions.act_window',
            'nodestroy': True,
            'target': 'new',
            'res_id': record_id  or False,##please replace record_id and provide the id of the record to be opened
        }

rawDescription: Method return value to call another form in OpenERP


code:
@classmethod
def  get_userdefined_formfields(cls):
    """Takes all the model fields on a concrete model,
    and subtracts out the ones in the parent (abstract) model."""
    allfields = cls._meta.fields + cls._meta.many_to_many
    #this assumes we only inherit from one abstract model
    #could be modified if that were not the case
    parentfields = AbstractModel._meta.fields + AbstractModel._meta.many_to_many
    #filter out parent fields
    childfields = [f for f in allfields if f not in parentfields]
    #filter out auto fields and fields without help text, return names
    return childfields

rawDescription: Get only the non-inherited fields for a Django mixin


code:
def addone(num1):
    num1=int(num1)+1
def subtractone(num1):
    num1=int(num1)-1
def combine():
    addone(1)
    subtractone(1)

rawDescription: Binding one button to two events with Tkinter


code:
from gi.repository import Gtk, Gio
from gi.repository import WebKit

HEIGHT = 500
WIDTH = 800

class MainWindow(Gtk.Window):

 def __init__(self):

    Gtk.Window.__init__(self, title="Resolution")
    self.set_border_width(0)
    self.set_default_size(WIDTH, HEIGHT)

    hb = Gtk.HeaderBar()
    hb.props.show_close_button = True
    hb.props.title = "Resolution"
    self.set_titlebar(hb)

    button = Gtk.Button()
    icon = Gio.ThemedIcon(name="emblem-system-symbolic")
    image = Gtk.Image.new_from_gicon(icon, 1)
    button.add(image)
    button.connect("clicked", self.sidebarShowHide)
    button.set_focus_on_click(False)
    hb.pack_start(button)

    sidebarbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
    toplevelbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)

    self.add(toplevelbox)

    self.sidebar = Gtk.Box()
    toplevelbox.pack_start(self.sidebar, False, False, 0)
    self.sidebar.add(sidebarbox)

    self.searchentry = Gtk.SearchEntry()
    self.searchentry.connect("search-changed", self.search_changed)
    sidebarbox.pack_start(self.searchentry, False, False, 0)

    label = Gtk.Label("Contents Selector")
    sidebarbox.pack_start(label, True, True, 0)

    scroller = Gtk.ScrolledWindow()
    content = WebKit.WebView()
    scroller.add(content)
    toplevelbox.pack_start(scroller, True, True, 0)

    content.open("/home/oliver/resolution/placeholder.html")

def sidebarShowHide(self, button):
    if self.sidebar.get_visible():
        self.sidebar.hide ()
    else:
        self.sidebar.show ()

def search_changed(self, searchentry):
    pass

win = MainWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

rawDescription: Gtk3 and Python - Box doesn't resize to fill window


code:
def get_children(parent):
    result = collection.find({'tags': 'exception', 'parentException': parent})
    result = result.sort('viewPriority')
    return [{'value': ex['value'], 'label': ex['label']} for ex in result]

def get_group_info(parent):
    return {'groupName': parent, 'children': get_children(parent)}

result = collection.find({'tags': 'exception'})
result = result.sort('viewPriority').distinct('parentException')

exceptions = [get_group_info(parent) for parent in result]

rawDescription: Methods to indent when using multiple list comprehension


code:
from collections import OrderedDict

result = OrderedDict()

for item in x:
    result[item[0]] = result.get(item[0], 0) + item[1]

result
[('a', 2), ('b', 1), ('c', 1)]

rawDescription: python built in reducebykey

description: Python built in reducebykey


code:
from datetime import datetime, timedelta
datetime.utcnow() + timedelta(minutes=5)

rawDescription: How can I convert a Python datetime object to UTC?

description: Convert a Python datetime object to UTC


rawDescription: finding item in two lists efficiently Python

description: Finding item in two lists efficiently Python


code:
import datetime
ax.plot_date([d.astype(datetime.datetime) for d in data['date']], data['count'])

rawDescription: Problems with datetime plot in matplotlib


code:
for testcases in root.iter('testcase'):
    testcase_name = QtGui.QApplication.translate(
        "qadashboard", str(testcases.attrib), None)
    item = QtGui.QListWidgetItem(testcase_name, self.listWidgetTestCases)
    item.setFlags(item.flags() | QtCore.Qt.ItemIsUserCheckable)
    item.setCheckState(QtCore.Qt.Unchecked)

rawDescription: How can I create a list in Qt with checkboxes

description: Create a list in Qt with checkboxes


code:
LastPost = aliased(Post, name='last')
last_id = (
    session.query(LastPost.id)
    .filter(LastPost.author_id == Author.id)
    .order_by(LastPost.publish_date.desc())
    .order_by(LastPost.id.desc())
    .limit(1)
    .correlate(Author)
    .as_scalar()
)

query = (
    DBSession.query(Author, Post)
    .outerjoin(Post, Post.id == last_id)
)

for author, last_post in query:
    print(author, last_post)

rawDescription: Use sqlalchemy to select only one row from related table


code:
import shlex
import subprocess
proc = subprocess.Popen(shlex.split('sudo insteon on 23'))
proc.communicate()

rawDescription: how to run my own external command in python script

description: Run my own external command in python script


code:
s = Series(["a","b","c","a"], dtype="category")
s

rawDescription: Closest equivalent of a factor variable in Python Pandas


code:
list1 = [1, 2, 3, 4, 5, 6]
list2 = []

# You couldn't just make 'list1_copy = list1',
# because this would just copy (share) the reference.
# (i.e. when you change list1_copy, list1 will also change)

# this will make a (new) copy of list1
# so you can happily iterate over it ( without anything getting lost :)
list1_copy = list1[:]

for item in list1_copy:
    list2.append(item)
    list1.remove(item)

rawDescription: iterating through a list removing items, some items are not removed

description: Iterating through a list removing items, some items are not removed


code:
import seaborn as sns
iris = sns.load_dataset("iris")
x_vars = ['sepal_length', 'sepal_width', 'petal_length']
y_vars = ['petal_width']
pp = sns.pairplot(data=iris, x_vars=x_vars, y_vars=y_vars)
_ = plt.setp(pp.axes[0,1].get_yticklabels(), visible=True) #changing the 2nd plot

rawDescription: Show y_ticklabels in a seaborn pairplot with shared axes


code:
class MyView(View):
    @decorate
    def dispatch(self, *args, **kwargs):
        return super(MyView, self).dispatch(*args, **kwargs)

rawDescription: Django class based DetailView/ListView ajax decorator?


code:
from django.db.models import Q

Event.objects.filter(Q(creator=me) | Q(participants=me)).distinct()

rawDescription: Django query with many to many and through table


code:
from itertools import groupby

def back_from_dummies(df):
    result_series = {}

    # Find dummy columns and build pairs (category, category_value)
    dummmy_tuples = [(col.split("_")[0],col) for col in df.columns if "_" in col]

    # Find non-dummy columns that do not have a _
    non_dummy_cols = [col for col in df.columns if "_" not in col]

    # For each category column group use idxmax to find the value.
    for dummy, cols in groupby(dummmy_tuples, lambda item: item[0]):

        #Select columns for each category
        dummy_df = df[[col[1] for col in cols]]

        # Find max value among columns
        max_columns = dummy_df.idxmax(axis=1)

        # Remove category_ prefix
        result_series[dummy] = max_columns.apply(lambda item: item.split("_")[1])

    # Copy non-dummy columns over.
    for col in non_dummy_cols:
        result_series[col] = df[col]

    # Return dataframe of the resulting series
    return pd.DataFrame(result_series)

(back_from_dummies(df_dummies) == df).all()

rawDescription: The most elegant way to get back from pandas.df_dummies


rawDescription: Comparing two lists and number of occurences


code:
data = [1,2,3,4]
tweets = ['a','b','c','d']
list(chain.from_iterable(zip(data,tweets)))

rawDescription: Python-Order a list so that X follows Y and Y follows X


code:
import datetime
now = datetime.datetime.now()
now
onehour = datetime.timedelta(hours=1)
oneminute = datetime.timedelta(minutes=1)
now + onehour
now + oneminute
now.strftime("%b %d %Y %H:%M:%S")
(now - onehour).strftime("%b %d %Y %H:%M:%S")

rawDescription: python time.time() move forward/backward hours/mins/secs

description: Python time.time() move forward/backward hours/mins/secs


code:
import xlwt
from xlrd import open_workbook
from xlutils.copy import copy
from xlwt import easyxf

old_file = open_workbook('c:\\oldfile.xls',formatting_info=True)
old_sheet = old_file.sheet_by_index(0)

new_file = copy(old_file)
new_sheet = new_file.get_sheet(0)

style = easyxf('font: bold true;'
               'borders: left no_line, right no_line, top no_line, bottom no_line;'
               'font: color red')

row_data = []

for row_index in range(old_sheet.nrows):
    rows = old_sheet.row_values(row_index)
    row_data.append(rows)

for row_index, row in enumerate(row_data):
    for col_index, cell_value in enumerate(row):

        # indicate the columns want to be formatted
        if col_index == 0 or col_index == 1:
            new_sheet.write(row_index, col_index, cell_value, style)
        else:
            new_sheet.write(row_index, col_index, cell_value)

new_file.save('c:\\newfile.xls')

rawDescription: Changing formats of contents in columns in an existing Excel workbook


code:
setup(
    cmdclass = {'build_ext': build_ext},
    ext_modules = [Extension("myext", ["myext.pyx", "myextlib.pyx"])])

rawDescription: How do I use Cython for external module compilation, from within my setup.py file?

description: Use Cython for external module compilation, from within my setup.py file


code:
set1= book.objects.filter(name='Python')
set2= book.objects.filter(author_name='Mona')
non_common = set1.exclude(id__in=[o.id for o in set2])

rawDescription: Django-How to combine two object set & remove the common objects in these two sets


code:
import pycurl
c = pycurl.Curl()
c.setopt(c.URL, 'http://my.server/a.jpg')
with open('o.jpg', 'w') as f:
    c.setopt(c.WRITEFUNCTION, f.write)
    c.perform()

rawDescription: Saving output to file using pycurl?

description: Saving output to file using pycurl


code:
from Tkinter import *
root = Tk()
w = Label(root, text=u"Hello, \u03bc-world!")
w.pack()
root.mainloop()

rawDescription: Greek letters in a GUI - PYTHON

description: Greek letters in a GUI - PYTHON


code: dreampie /path/to/python2.x

rawDescription: Is it possible to change the Python interpreter version DreamPie is using?


code:
def point_to_index(point):
    try:
        return points_dict.get(point, points.index(point))
    except:
        if len(points) >= max_cache:
            del points_dict[points[len(points)-max_cache]]
        points.append(point)
        points_dict[points] = len(points)-1
        return len(points)-1

rawDescription: What is the most efficient way to add an element to a list only if isn't there yet?


code: service_states = s.read().splitlines()

rawDescription: Output of command to List using Python

description: Output of command to List using Python


code: B = [int(i+1 in A) for i in range(max(A))]

rawDescription: Converting an array to 0s and 1s

description: Converting an array to 0s and 1s


code:
import math
v = 0.115
math.floor(v*100)/100

rawDescription: How to round down

description: Round down


rawDescription: How can I tell if a file is a descendant of a given directory?

description: Tell if a file is a descendant of a given directory


code:
class CheckableTabWidget(QtGui.QTabWidget):

    checkBoxList = []

    def addTab(self, widget, title):
        QtGui.QTabWidget.addTab(self, widget, title)
        checkBox = QtGui.QCheckBox()
        self.checkBoxList.append(checkBox)
        self.tabBar().setTabButton(self.tabBar().count()-1, QtGui.QTabBar.LeftSide, checkBox)
        self.connect(checkBox, QtCore.SIGNAL('stateChanged(int)'), lambda checkState: self.__emitStateChanged(checkBox, checkState))

    def isChecked(self, index):
        return self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).checkState() != QtCore.Qt.Unchecked

    def setCheckState(self, index, checkState):
        self.tabBar().tabButton(index, QtGui.QTabBar.LeftSide).setCheckState(checkState)

    def __emitStateChanged(self, checkBox, checkState):
        index = self.checkBoxList.index(checkBox)
        self.emit(QtCore.SIGNAL('stateChanged(int, int)'), index, checkState)

rawDescription: QTabWidget with CheckBox in title

description: QTabWidget with CheckBox in title


code:
import re  # regular expressions, not needed (alternatives: the `split` method) but convenient

re_pattern = r'[^:]*:(\d+)\D+(\d+)\D+(\d+)\D+(\d+)'
with open(inputfile) as fin:
    for line in fin:
        start, _, offset_start, offset_end = re.search(re_pattern, line).groups()
        print('chr1 {} {}'.format(int(start) + int(offset_start) - 1,int(start) + int(offset_end) - 1))

rawDescription: How to extract and merge the coordinates using Python?

description: Extract and merge the coordinates using Python


code:
def thing():

    store = {}

    while True:
        name = input('Perp: ')
        if not name:
            for k, v in store.items():
                print('Perp' + k + ' has a ' + v)
            break
        else:
            store[name] = input('item: ')

rawDescription: How to eliminate prompt when adding new value to dictionary?

description: Eliminate prompt when adding new value to dictionary


code:
from gi.repository import Gtk, GLib
import threading
import urllib.request

class Window(Gtk.Window):
    def __init__(self):
        super().__init__()
        self.connect('delete-event', Gtk.main_quit)
        self.progress = Gtk.ProgressBar()
        self.add(self.progress)
        self.show_all()

        # Start download
        self.thread = threading.Thread(target=self.download)
        self.thread.start()
        GLib.timeout_add_seconds(1, self.download_pulse)

    def download(self):
        urllib.request.urlretrieve('<link>')

    def download_pulse(self):
        if self.thread.is_alive():
            self.progress.pulse()
            return True
        return False

win = Window()
Gtk.main()

rawDescription: Handling GTK Objects on Threads in Python


code:
def magic(func):
    def wrapper(*args):
        return func(' - '.join(map(str, args)))
    return wrapper

rawDescription: How can I use a decorator to modify the type of *args?

description: Use a decorator to modify the type of *args


code: return (t1start <= t2start <= t1end) or (t2start <= t1start <= t2end)

rawDescription: python date interval intersection

description: Python date interval intersection


code:
from django.db import transaction

with transaction.atomic():
    for i, row in df.iterrows():
        mv = MeasureValue.objects.get(org=row.org, month=month)

        if (row.percentile is None) or np.isnan(row.percentile):
            # if it's already None, why set it to None?
            row.percentile = None

        mv.percentile = row.percentile
        mv.save()

rawDescription: Update multiple objects at once in Django?


code:
employees = Employee.objects.all()
for day in range(19, 26):
    employees = employees.filter(shift__date_requested=datetime.date(2015, 7, day))

rawDescription: Can I filter users to require a matching entry on every date in a given range?

description: Filter users to require a matching entry on every date in a given range


code:
lines = open('infile.txt').readlines()
# split each line based on tabs and spaces
lines = [ x.split() for x in lines ]
# remove empty fields (assuming you want to)
lines = [ x for x in lines if len(x) > 0 ]
# transform your lists (lines) into coma separated strings
lines = [ ','.join(x) for x in lines ]
# separate lines with \n for writelines
lines = [ x + '\n' for x in lines ]
open('outfile.txt', 'w').writelines(lines)

rawDescription: Creating a comma delimited file from a very messy text file


code:
while(True):
    x = raw_input("Enter something:")
    if x == "":
        break

rawDescription: How do I ask a user continuously input something?

description: Ask a user continuously input something


code: theta[0] = theta[0] - alpha*1/m*sum([((theta[0]+theta[1]*xi) - yi)**2 for (xi,yi) in zip(x,y)])

rawDescription: Linear Regression\Gradient Descent python implementation


code: annotations.ix[annotations.groupby(['bookid'], sort=False)['weight'].idxmax()][['bookid', 'conceptid', 'weight']]

rawDescription: How to get the max value of a multiple column group-by pandas?

description: Get the max value of a multiple column group-by pandas


code:
# sitecustomize.py
import sys

class AttributeIsReadonly(ValueError):
    pass

class MakeModuleAttributesReadonly(object):
    def __init__(self, module, readonly_attributes):
        self.module=module
        self.readonly_attributes=readonly_attributes

    def __setattr__(self, item, value):
        if item in ['module', 'readonly_attributes']:
            return super(MakeModuleAttributesReadonly, self).__setattr__(item, value)
        if item in self.readonly_attributes:
            raise AttributeIsReadonly('Access on attribute %r is readonly' % item)
        return setattr(self.module, item, value)

    def __getattr__(self, item):
        return getattr(self.module, item)

sys.modules['sys']=MakeModuleAttributesReadonly(sys, ['path'])

#import sys
#sys.path=sys.path # this will raise the above AttributeIsReadonly

rawDescription: Where does sys.path get replaced?

description: Where does sys.path get replaced


code: sorted(list(T))

rawDescription: How to access an "anonymous" variable?

description: Access an "anonymous" variable


code:
import random

x = int(raw_input("Number(1-6): ")) # note I made x an int

while True:
    y = random.randint(1, 6)
    if x != y: break

rawDescription: Python random number excluding one variable


code:
def unflatten_multidict(multidict):
    d = {}
    for k in multidict.keys():
        m = re.match(r'^([^\[\]]+)((?:\[[^\[\]]+\])+)', k)
        if m:
            attr = m.group(1)
            lastd = d.setdefault(attr, {})
            subkeys = re.findall(r'\[([^\[\]]+)\]', m.group(2))
            for i, subkey in enumerate(subkeys):
                if i < len(subkeys) - 1:
                    lastd = lastd.setdefault(subkey, {})
                else:
                    lastd[subkey] = multidict.get(k)
        else:
            d[k] = multidict.get(k)

    return d

rawDescription: Converting request params from DataTables with Pyramid


code:
import tkinter
t = tkinter.Tk()
input()

rawDescription: What magic prevents Tkinter programs from blocking in interactive shell?


code: plt.subplots_adjust(right=.8) # or even smaller number to shift the right edge to the left

rawDescription: Adjust right margin width to fit margin text


code:
from pysnmp.entity.rfc3413.oneliner import cmdgen

def getCounter(ip, community, mibName, counterName, indexNum=None):
    if indexNum is not None:
        mibVariable = cmdgen.MibVariable(mibName, counterName, int(indexNum))
    else:
        mibVariable = cmdgen.MibVariable(mibName, counterName)

    cmdGen = cmdgen.CommandGenerator()

    errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.getCmd(
        cmdgen.CommunityData(community),
        cmdgen.UdpTransportTarget((ip, 161)),
        mibVariable.addMibSource("/path/to/mibs")
    )

    if not errorIndication and not errorStatus:
        return varBindTable

#from pysnmp import debug
#debug.setLogger(debug.Debug('msgproc'))

print(getCounter('demo.snmplabs.com',
                 'recorded/linux-full-walk',
                 'HOST-RESOURCES-MIB',
                 'hrSWRunPerfMem',
                  970))

rawDescription: pysnmp poll specific process index within HOST-RESOURCES-MIB

description: Pysnmp poll specific process index within HOST-RESOURCES-MIB


code:
import re

re_field = re.compile('@(param|type|rtype|return)')

def fix_docstring(app, what, name, obj, options, lines):
    for i in xrange(len(lines)):
        lines[i] = re_field.sub(r':\1', lines[i])

def setup(app):
    app.connect('autodoc-process-docstring', fix_docstring)

rawDescription: Automated way to switch from epydoc's docstring formatting to sphinx docstring formatting?


code: G = nx.MultiGraph()

rawDescription: How to draw a graph with duplicate edges in networkx in python

description: Draw a graph with duplicate edges in networkx in python


code:
from collections import defaultdict

lists = [['apple', 'window', 'pear', 2, 1.55, 'banana'],
['apple', 'orange', 'kiwi', 3, 1.80, 'banana'],
['apple', 'envelope', 'star_fruit', 2, 1.55, 'banana'],
['apple', 'orange', 'pear', 2, 0.80, 'coffee_cup'],
['apple', 'orange', 'pear', 2, 3.80, 'coffee_cup']]

dic = defaultdict(int)
fts = []
for lst in lists:
    first_third = lst[0], lst[2]
    dic[first_third] += 1
    if dic[first_third] == 2: fts.append(first_third)
    lst.append(dic[first_third])

for lst in lists:
    if (lst[0], lst[2]) not in fts:
        lst[-1] -= 1

print(lists)

rawDescription: find and update duplicates in a list of lists

description: Find and update duplicates in a list of lists


code:
def refresh(self):
    if (self.nbfig > 0): #meaning the c++ app already plotted a figure
        plt.ioff()
        plt.close()

rawDescription: plotting a new graph after closing a first one in matplotlib

description: Plotting a new graph after closing a first one in matplotlib


code:
import tkinter as tk

def display_before_value(event):
        """Display the value of the text widget before the class bindings run"""
        value = text.get("1.0", tk.END)
        print("The (before) value is:", value)
        root.after(1, display_after_value)
        return

def display_after_value():
        """Display the value of the text widget after the class bindings run"""
        value = text.get("1.0", tk.END)
        print("The (after) value is:", value)
        return

# Add the widgets
root = tk.Tk()
text = tk.Text(root)

# Add the bindings
text.bind("<Tab>", display_before_value)

# Show the text widget
text.grid()

# Run
root.mainloop()

rawDescription: How to Capture the Value of a Text Widget after the Tab Key is Pressed in Tkinter?


code:
from pyspark.mllib.linalg import DenseVector
from pyspark.mllib.regression import LabeledPoint

feature_matrix_vectors = sc.parallelize([
    DenseVector([1.0, 31.0, 5.0, 1935.0, 24.0]),
    DenseVector([1.0, 1231.0, 15.0, 2008.0, None])
])

(feature_matrix_vectors
    .map(lambda v: LabeledPoint(1.0 if None in v else 0.0, v))
    .collect())

rawDescription: Iterating through a Spark RDD

description: Iterating through a Spark RDD


code:
strbits = np.fromfile(infile, dtype=np.int8, count=73)
a_string = ''.join([chr(item) for item in strbits])

rawDescription: reading char data from binary file with numpy

description: Reading char data from binary file with numpy


code:
from django.db import models
from django.utils import timezone

def default_time():
    return timezone.now() + timezone.timedelta(+14)

class MyModel(models.Model):

    my_datetime = models.DateTimeField(default=default_time)

rawDescription: Django migration with DateTimeField and timedelta default


rawDescription: VirtualEnv creates envs. with all system wide packages instead of clean env


code:
class TelefoneIpForm(forms.ModelForm):
    class Meta:
        model = TelefoneIp

    def save(self, commit=True):
        instance = super(TelefoneIpForm, self).save(commit=commit)

        equipamento = instance.equipamento
        equipamento.cliente = instance.cliente_atual
        equipamento.save()

class TelefoneIPAdmin(admin.ModelAdmin):
    list_display = ('mac', 'modelo', 'fornecedor', )
    list_filter = ['cliente_atual__nome']
    search_fields = ['mac']
    inlines = [TelefoneIPInline]
    form = TelefoneIpForm

rawDescription: Editing a Django model after saving it

description: Editing a Django model after saving it


code:
fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot( range(1,10), camean - 25, 'ro-' )
ax.plot( range(1,10), oemean , 'go-' )
ax.plot( range(1,10), hlmean , 'bo-' )
ax.set_yticks(range(5, 60, 5))
ax.set_yticklabels(["5","10","15","20","25","30","...","65","70","75"])
ax.legend(('ClassificationAccuracy','One-Error','HammingLoss'),loc='upper right')
pylab.show()

rawDescription: Pyplot, plot 2 dataset into one figure, skip part of the y-axis


rawDescription: Iterative grow a shuffled list in python


code:
def strip_common_path_prefix(seq):
    # blah blah
    return prefix, type(seq)(result)

rawDescription: Maintaining sequence type in set or list comprehension in python


code:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import style

# use ggplot style sheet
style.use('ggplot')

plt.plot(np.random.randn(10))

rawDescription: How do you make ggplot plots of numpy arrays?

description: Make ggplot plots of numpy arrays


code:
from Tkinter import *

# dummy list so that the code does not relay on actually drives and files
rdrive = ['drive1','drive2','drive3']

sel_files = {'drive1': ['file1','file2'],
                  'drive2': ['file3','file4'],
                  'drive3': ['file6','file5']}

class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.initUI()

    def initUI(self):
        self.parent.title("Listbox")
        self.pack(fill=BOTH, expand=1)

        # Drive Select List Box
        # global rdrive
        # rdrive = drive_ctypes.find_rmdrv()

        # use dummy rdrive instead of physical drives. Otherwise,
        # cant reproduce the problem.

        self.lb = Listbox(self, height=10, selectmode=MULTIPLE)
        for i in rdrive:
            self.lb.insert(END, i)

        self.lb.bind("<<ListboxSelect>>", self.onSelect)

        self.lb.grid(row =3, column =2)

        ## File Select List Box
        self.flb = Listbox(self, height=10, selectmode=MULTIPLE)

        self.flb.grid(row =3, column =4)

    def onSelect(self, event):
        # most changes are here. GUI programming is event driven, so you need
        # to get the list of files for selected drive (i.e. when selection even occurs).
        # Also here you respond the the even, so that the right list is populated.

        # get widget (i.e. right listbox) and currently selected item(s)
        widget = event.widget
        selection=widget.curselection()

        files_avalibe = []

        # if something was selected, than get drives for which it was selected
        # and retrieve files for each drive
        if selection:

            for drive_i in selection:
                selected_drive = rdrive[drive_i]
                files_avalibe += sel_files[selected_drive]

            print(files_avalibe)

        # once we have files from the selected drive, list them
        # in the right list box
        self.update_file_list(files_avalibe)

    def update_file_list(self, file_list):
          # updates right listbox
          self.flb.delete(0, END)
          for i in file_list:
            self.flb.insert(END, i)

    def findfiles(self,val):
        sender = val.widget

root = Tk()
f = Example(root)
root.mainloop()

rawDescription: Python Tkinter: Passing List Arguments to Populate another List


rawDescription: PyQt5 connect Enter key to Widget

description: PyQt5 connect Enter key to Widget


code:
def pack_all(lst):
    fmt = ''.join('i' if isinstance(x, int) else 'd' for x in lst)
    return struct.pack(fmt, *lst)

rawDescription: float, int and str to byte array - python

description: Float, int and str to byte array - python


code:
gcc_format = GCCFORMAT(GCCN)
results = gcc_format.pList

rawDescription: Parse an element in a list and store the result in a dictionary


code:
setup(
    name='django-glinks',
    version='0.1.4',
    author='Shawn Simon',
    author_email='shawn.simon.developer@gmail.com',
    packages=['glinks', "glinks.templatetags"],
    url='http://pypi.python.org/pypi/django-glinks/',
    license='LICENSE.txt',
    description='Interal add manager for django sites.',
    long_description=open('README.txt').read(),
    install_requires=[
        "Django >= 1.6.0",
    ],
    classifiers=[
        'Environment :: Web Environment',
        'Framework :: Django',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License', # example license
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2.7',
        'Topic :: Internet :: WWW/HTTP',
        'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
    ],
)

rawDescription: Pip django app installation

description: Pip django app installation


code:
import sys

if sys.version_info < (2 , 6):
    REQUIRES = ['gevent', 'ssl', 'configobj', 'simplejson', 'mechanize'],
else:
    REQUIRES = ['gevent', 'configobj', 'simplejson', 'mechanize'],

setup(
# [...]
    install_requires=REQUIRES,
# [...]
)

rawDescription: install_requires in setup.py depending on installed Python version

description: Install_requires in setup.py depending on installed Python version