code:
class ComplicatedThing(object):
    def __reduce__(self):
        return (ComplicatedThing, (self.progress_int,))

rawDescription: Python - How can I make this un-pickleable object pickleable?


code:
import random

abc = [chr(x) for x in range(97, 123)]
out = []

out.append(abc[0])
for item in abc[1:]:
    out += [''] * random.randrange(4, 8)
    out.append(item)

rawDescription: Append items to a list in python at spaced intervals


code:
try:
    from inspect import signature
except ImportError:
    from funcsigs import signature

sig = signature(yourfunction)
boundargs = sig.bind(*args, **kwargs)

rawDescription: Filling default missing/arguments from args/kwargs and function signature in Python 2.7


code:
buf = ''
while 1:
    buf += ser.read(1)
    if buf == data1:
        # do stuff
        buf = ''
    elif buf == data2:
        # do something else
        buf = ''

rawDescription: Waiting for incoming serial communication data Python


code:
import pygame
pygame.init()

screen = pygame.display.set_mode((400, 300))
loop = True

# No need to re-make these again each loop.
font1 = pygame.font.SysFont("calibri",40)
font2 = pygame.font.SysFont("calibri",20)

fps = 30
clock = pygame.time.Clock()

while loop:
    screen.fill((0, 0, 0), (0, 0, 400, 300))

    text = font1.render("TextA", True,(255,255,255))
    screen.blit(text,(0,0))

    text = font2.render("Begin", True,(255,255,255))
    screen.blit(text,(50,50))

    pygame.display.update()   # Call this only once per loop
    clock.tick(fps)     # forces the program to run at 30 fps.

rawDescription: Update display all at one time PyGame

description: Update display all at one time PyGame


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

fig = plt.figure()
ax1 = fig.add_subplot(111)
ax2 = ax1.twiny()

X = np.linspace(0,1,1000)
Y = np.cos(X*20)

ax1.plot(X,Y)
ax1.set_xlabel(r"Original x-axis: $X$")

new_tick_locations = np.array([.2, .5, .9])

def tick_function(X):
    V = 1/(1+X)
    return ["%.3f" % z for z in V]

ax2.set_xlim(ax1.get_xlim())
ax2.set_xticks(new_tick_locations)
ax2.set_xticklabels(tick_function(new_tick_locations))
ax2.set_xlabel(r"Modified x-axis: $1/(1+X)$")
plt.show()

rawDescription: How to add a second x-axis in matplotlib

description: Add a second x-axis in matplotlib


code:
POINTER = ctypes.POINTER
# Define the pkstream struct
class PKSTREAM(Structure):
   _fields_ = [('pInBuffer', POINTER(c_char)),
               ('nInSize', c_int),
               ('pOutBuffer', POINTER(c_char)),
               ('nOutSize', c_int),
               ('nLitSize', c_char),
               ('nDictSizeByte', c_char),
               ('pInPos', POINTER(c_char)),
               ('pOutPos', POINTER(c_char)),
               ('nBits', c_char),
               ('nBitBuffer', c_long),
               ('pDictPos', POINTER(c_char)),
               ('nDictSize', c_int),
               ('nCurDictSize', c_int),
               ('Dict', c_char * 0x1000)]

rawDescription: C struct => ctypes struct ... is this mapping correct?


code:
import time

def pigsl():
    print()
    print("You said the following:")
    time.sleep(1)
    sentenc = input("Please enter your sentence: ")
    print(sentenc)
    print()
    print("Your new sentence is:")
    senten = news(sentenc)
    time.sleep(1)
    print(senten)

def news(c):
    new=''                         #instead of new=c, just start with an empty string
    vowels = ('a', 'e', 'i', 'o', 'u')
    for x in c:                    #iterate through the sentence
        if x in vowels:            #if the character is a vowel
            new+=(x+'eg')          #append the vowel + 'eg' to the new empty string
        else:                      #otherwise
            new+=x                 #just append the consonant to the new string
    return new

pigsl()

rawDescription: python Need help placing 'eg' after every vowel in a sentence

description: Python Need help placing 'eg' after every vowel in a sentence


code: result = methodtocall(*[param1, ... ,paramN])

rawDescription: adapt the method with a dynamic number of parameters

description: Adapt the method with a dynamic number of parameters


code:
import matplotlib.pyplot as plt

f = plt.figure()
x = f.gca()
x.set_aspect("equal")
x.plot(range(10),range(10))
plt.show()

rawDescription: Matplotlib - matching axes unit length (resolution)


rawDescription: summing outer product of multiple vectors in einsum

description: Summing outer product of multiple vectors in einsum


code:
# setup.py

# ...

if os.getenv('NO_BUILD') == 'true':
    extensions = []
else:
    extensions = []

setup(
    # ...
    ext_modules=extensions,
)

rawDescription: Run 'python setup.py test' without running build_ext?


rawDescription: Python remove elements from two dimensional list


code:
def function(x, E, T, a, n, m):
    return x/E + (a/n+1)*T^(n+1)*x^m

def leastsq_function(params, *args):
    a = params[0]
    n = params[1]
    m = params[2]
    x = args[0]
    y = args[1]
    E = args[2]
    T = args[3]
    n1, n2 = args[2]

    yfit = np.empty(x.shape)
    yfit[:n1] = function(x[:n1], E[0], T[0], a, n, m)
    yfit[n1:n2] = function(x[n1:n2], E[1], T[1], a, n, m)
    yfit[n2:] = function(x[n2:], E[2], T[2], a, n, m)

    return y - yfit

params0 = [a0, n0, m0]
args = (x, y, (E0, E1, E2), (T0, T1, T2), (n1, n1+n2))
result = scipy.optimize.leastsq(leastsq_function, params0, args=args)

rawDescription: fit multiple parametric curves with scipy

description: Fit multiple parametric curves with scipy


code: groups_list = User.objects.filter(userprofile__status__in=group_list).values_list('email',    flat=True)

rawDescription: How to get the user email from auth.User when filterring a field in UserProfile?

description: Get the user email from auth.User when filterring a field in UserProfile


code:
from copy import deepcopy
list1.append(deepcopy(list2))

rawDescription: Append list to list, overwrite values

description: Append list to list, overwrite values


code:
import cv2; import numpy as np

class MyClass:
    def __init__(self,imagefile):
        self.image = cv2.imread(imagefile)

        #image details
        self.h,self.w = self.image.shape[:2]
        #self.bPoints, self.wPoints = np.array([[0,0]]),np.array([[0,0]])
        self.bPoints, self.wPoints = [],[]

        #CAUTION! Points are of the form (y,x)
        # use the np.where method instead of a double loop.
        # make sure self.th2 is a numpy array
        indx = np.where(self.th2==0)
        for i,j in zip(indx[0], indx[1]):
            self.bPoints.append((i,j))

        indx = np.where(self.th2==255)
        for i,j in zip(indx[0], indx[1]):
            self.wPoints.append((i,j))

        # Point filtering
        #for i in xrange(self.h):
        #    for j in xrange(self.w):
        #        if self.th2.item(i,j) == 0:
        #            #self.bPoints = np.append([[i,j]], self.bPoints, axis=0)
        #            self.bPoints.append((i,j))
        #        else:
        #            self.wPoints.append((i,j))
        #            #self.wPoints = np.append([[i,j]], self.wPoints, axis=0)

        #self.bPoints = self.bPoints[:len(self.bPoints) - 1]
        #self.wPoints = self.wPoints[:len(self.wPoints) - 1]
        self.bPoints, self.wPoints = np.array(self.bPoints), np.array(self.wPoints)

rawDescription: Manipulating a large binary image array with numpy and cv2


code:
import wx

class Main(wx.Frame):

    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Main', size=(700, 500))

        self.SetBackgroundColour('white')

        panel = wx.Panel(self, wx.ID_ANY, size=(50, 50))
        panel.SetBackgroundColour('blue')

        sizer = wx.BoxSizer()
        sizer.AddStretchSpacer(1)
        sizer.Add(panel, 0, wx.ALIGN_CENTER)
        sizer.AddStretchSpacer(1)

        self.SetSizer(sizer)

if __name__ == '__main__':
    app = wx.App()
    frame = Main(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

rawDescription: wxPython. How do I center a static size panel?

description: WxPython. How do I center a static size panel


code:
import matplotlib.pyplot as plt

ax1 = plt.subplot2grid(shape=(2,6), loc=(0,0), colspan=2)
ax2 = plt.subplot2grid((2,6), (0,2), colspan=2)
ax3 = plt.subplot2grid((2,6), (0,4), colspan=2)
ax4 = plt.subplot2grid((2,6), (1,1), colspan=2)
ax5 = plt.subplot2grid((2,6), (1,3), colspan=2)

rawDescription: Position 5 subplots in Matplotlib

description: Position 5 subplots in Matplotlib


code:
# SConscript, for example
sources = ["bar1_1.cpp", "bar1_2.cpp", ...]
objects = [env.Object(x) for x in sources]
Return(objects)

# SConstruct (top-level)
directory_1_objects = SConscript("directory_1/SConscript")
directory_2_objects = SConscript("directory_2/SConscript")
program = env.Program("magical_wonders", [directory_1_objects, directory_2_objects])

rawDescription: SCons setup for hierarchical source but single target


code:
counts = [0 for _ in range (10) ]

with open('city_all.txt', 'r') as f:
    for line in (x.strip () for x in f):
        if line.isdigit():
            allcount += 1
            try: counts[int(line)] += 1
            except IndexError: pass

rawDescription: optimizing my Benfold's law program

description: Optimizing my Benfold's law program


code:
class FilePage(models.Model):
    file = models.ForeignKey(File, on_delete=models.CASCADE, related_name='file_pages')
    file_page_number = models.IntegerField()
    patient = models.ManyToManyField(Patient, null=True, blank=True)
    document_type = models.ManyToManyField(DocumentType, null=True, blank=True)

rawDescription: Combining the required fields from two different models


code:
if y < PosYe + 20:     #we define the water level = PosYe+20
    Canevas.coords(plongeur,x,y)
    x,y=x,y+g*t*dt*m1

elif flag and PosYe + 20 <= y :
    beep()
    flag = False

rawDescription: Play winsound at a specific condition

description: Play winsound at a specific condition


code:
import requests
from os.path import getsize

#get size of previous downloaded chunk file
beg = getsize(PATH_TO_FILE)
#if we want we can get the size before download the file (without actually download it)
end = request.head(URL).headers['content-length']
#continue the download in the next byte from where it stopped
headers = {'Range': "bytes=%d-%s"%(beg+1,end)}
download = requests.get(URL, headers=headers)

rawDescription: how make python script for renewable downloads?

description: How make python script for renewable downloads


rawDescription: How to convert string like 1.424304064E9 to datetime in pandas dataframe?

description: Convert string like 1.424304064E9 to datetime in pandas dataframe


rawDescription: DISTINCT ON in django

description: DISTINCT ON in django


code:
with open(in_file) as f, open(out_file,"w") as g:
    for line in f:
        if line == "\n":
            g.write("0\n")
        else:
            g.write(str(line.count(" ")+1)+"\n")

rawDescription: Creating a program which counts words number in a row of a text file (Python)


code:
import multiprocessing
import time

def process(x):
    time.sleep(1)
    return x

if __name__ == '__main__':
    jobs = range(100)
    pool = multiprocessing.Pool(20)
    for result in pool.imap_unordered(process, jobs):
        print(result)
    pool.close()
    pool.join()

rawDescription: Multiprocessing slots in python

description: Multiprocessing slots in python


code: ares = (a.transpose(0,1,3,2) * v).transpose(0,1,3,2)

rawDescription: numpy element-wise multiplication of an array and a vector

description: Numpy element-wise multiplication of an array and a vector


code:
a = range(100)
b = range(50)

fst, snd = (a, b) if len(a) < len(b) else (b, a)
len(set(fst).intersection(snd))

rawDescription: A fast way to find the number of elements in list intersection (Python)


code:
a = np.random.random((20, 22))

def distance(array, xpos, ypos):
    # probably a gazillion methods to create the actual distances...
    # if you array is large and you are only interested to a certain size
    # you sould probably slice out a smaller one first of course.
    dists = np.sqrt(np.arange(-xpos, array.shape [0]-xpos, dtype=float)[:,None]**2
          + np.arange(-ypos, array.shape [1]-ypos, dtype=float)[None,:]**2)
    return dists

# Prepare which bins to use:
dists = distance(a, 10, 11).astype(int)

# Do a bincount with weights.
result = np.bincount(dists.flat, weights=a.flat)
# and add them up:
result = np.add.accumulate(result)

rawDescription: Matrix calculations on equidistant elements without for loops


code:
from subprocess import call
call(["F:\\FirefoxPortable\\firefox.exe", "-new-tab", url])

rawDescription: How to set the path to a browser executable with python webbrowser

description: Set the path to a browser executable with python webbrowser


code:
import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class TestLabels():
    def __init__(self, parent, num_labels):
        self.parent = parent
        self.string_var_list=[]
        tk.Button(self.parent, text="Exit", command=self.parent.quit,
                  bg="orange").grid(row=100)
        self.create_labels(num_labels)

        ## change one label
        str_var=self.string_var_list[10]
        str_var.set("***changed***")

    def create_labels(self, num_labels):
        """create labels, 5 to a row
        """
        r=0
        c=0
        colors=["lightblue", "yellow", "white", "pink", "lightgreen"]
        for ctr in range(num_labels):
            s_var=tk.StringVar()
            ## won't save the Label ID since you use StringVar to change it
            tk.Label(self.parent, textvariable=s_var,
                     bg=colors[c]).grid(row=r, column=c, sticky="nsew")
            s_var.set("Label #%d" % (ctr))
            self.string_var_list.append(s_var)
            c += 1
            if c > 4:
                c=0
                r += 1

if __name__ == "__main__":
    root = tk.Tk()
    MA=TestLabels(root, 14)
    root.mainloop()

rawDescription: create and set variables at same time in a loop tkinter

description: Create and set variables at same time in a loop tkinter


code:
def your_code():
    # ...
    warnings.warn("deprecated", DeprecationWarning)
    # ...

def your_test():
    with warnings.catch_warnings(record=True) as w:
        your_code()
        assert len(w) > 1

rawDescription: Check that a function raises a warning with nose tests


code:
a_lines = open('FileA', 'r').readlines()[1:]
b_lines = open('FileB', 'r').readlines()[1:]
file_result = open('result', 'w')

# Read content of FileA to a table (list of lists)
a_table = []
for l in a_lines:
    a_table.append([w.strip() for w in l.split(',')])

# Read content of FileB in a dictionary.
# The 'id' field as dictionary key for simple look-up.
b_dict = {}
for l in b_lines:
    words = l.split(',')
    b_dict[words[1].strip()] = words[0].strip()

# Do the actual work and save result.
for row in a_table:
    if row[0] in b_dict:
        file_result.write(b_dict[row[0]] + ',' + row[0] + ',')
        file_result.write(row[2] + '\n')

rawDescription: Python: Comparing 2 CSV's and pull data over if a certain cell matches


code:
to_crawl = [(start_page, [])]
for link, path in to_crawl:
    links = get_all_links(source(link), crawled)
    if goal in links:
        return path+[link]+[goal]
    crawled.append(link)
    to_crawl.extend((new_link, path + [new_link]) for new_link in links)

rawDescription: Finding shotest path between Wikipedia articles


code:
#! /usr/bin/python
import sys
from datetime import datetime
d_format = "%Y-%m-%d"

try:
    start = datetime.strptime(sys.argv[1], d_format)
    end = datetime.strptime(sys.argv[2], d_format)
except (TypeError, IndexError):
    sys.stderr.write("Example: grep-date-range.py 2011-03-25 2011-04-02 \n")

for line in sys.stdin:
    try:
        date = datetime.strptime(line.split()[0], d_format)
        # suit the <=, <, comparisons bellow to your needs:
        if start <= date < end:
            sys.stdout.write(line)
    except (ValueError, IndexError):
        pass

rawDescription: how to grep by date time range?

description: Grep by date time range


code:
import htql;
address = '88-21 64th st , Rego Park , New York 11374'
states=['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut',
    'Delaware', 'District Of Columbia', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana',
    'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan',
    'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
    'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma',
    'Oregon', 'PALAU', 'Pennsylvania', 'PUERTO RICO', 'Rhode Island', 'South Carolina', 'South Dakota',
    'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin',
    'Wyoming'];

a=htql.RegEx();
a.setNameSet('states', states);

state_zip1=a.reSearchStr(address, "&[s:states][,\s]+\d{5}", case=False)[0];
# state_zip1 = 'New York 11374'

state_zip2=a.reSearchList(address.split(), r"&[ws:states]<,>?<\d{5}>", case=False)[0];
# state_zip2 = ['New', 'York', '11374']

rawDescription: Information extraction with Python using huge list of entity names


code:
msg = '"\n{} {} removed. We therefore currently have {} definitions to test..."'
prep = 'definition was'
if count > 1:
    prep = 'definitions were'

print(msg.format(count, prep, len(test_list)))

rawDescription: String formatting for different options in python


code:
class Order(models.Model):
  customer = models.foreignKey(Customer)
  total = models.charField(max_length=10)
  has_shipped = models.booleanField()
  products = models.ManyToManyField(Product)

rawDescription: How do I store multiple values in a single attribute

description: Store multiple values in a single attribute


code:
shortlen = 150
shortset = set()
for i in xrange(len(seq)-shortlen+1):
    shortset.add(seq[i:i+shortlen])

for line in short:
    count1 += 1
    if line in shortset:
        count2 += 1

rawDescription: Efficient way to find if a short string is present in a longer string in python


code:
import numpy
import theano
import theano.tensor as tt

def compile(input_size, hidden_sizes, output_size, reg_lambda, learning_rate):
    ws, bs = [], []
    x = tt.matrix('x')
    x.tag.test_value = numpy.random.standard_normal(size=(2, input_size))\
        .astype(theano.config.floatX)
    previous_size = input_size
    h = x
    for hidden_size in hidden_sizes:
        w = theano.shared(
                numpy.random.standard_normal(size=(previous_size, hidden_size))
                          .astype(theano.config.floatX))
        b = theano.shared(numpy.zeros((hidden_size,), dtype=theano.config.floatX))
        h = tt.tanh(tt.dot(h, w) + b)
        ws.append(w)
        bs.append(b)
        previous_size = hidden_size
    w = theano.shared(numpy.random.standard_normal(size=(previous_size, output_size))
                      .astype(theano.config.floatX))
    b = theano.shared(numpy.zeros((output_size,), dtype=theano.config.floatX))
    y = tt.nnet.softmax(tt.dot(h, w) + b)
    ws.append(w)
    bs.append(b)
    z = tt.ivector('z')
    z.tag.test_value = numpy.random.randint(output_size, size=(2,))
    cost = tt.nnet.categorical_crossentropy(y, z).mean()
    for w, b in zip(ws, bs):
        cost += tt.sum(w ** 2) * reg_lambda
        cost += tt.sum(b ** 2) * reg_lambda
    updates = [(p, p - learning_rate * tt.grad(cost, p)) for p in ws + bs]
    return theano.function([x, z], outputs=[cost], updates=updates)

theano.config.compute_test_value = 'raise'
compile(10, [8, 6, 4, 8, 16], 32, 0.1, 0.01)

rawDescription: Dynamically creating symbolic expressions in theano


code:
import shlex
shlex.split('-o 1 --long "Some long string"')

rawDescription: Python, how to parse strings to look like sys.argv


code: print(etree.tostring(element, encoding="unicode", pretty_print=True, with_tail=False))

rawDescription: Why does this element in lxml include the tail?


code:
from urllib.request import urlopen
html = urlopen("http://www.stackoverflow.com/").read().decode('utf-8')
print(html)

rawDescription: How do I download a website using python 3?

description: Download a website using python 3


code:
import numpy as np

a = np.array((1,2,3,2,3,2,1,3))

occurences = np.where(a == a.max())

# occurences == array([2, 4, 7])

rawDescription: numpy.argmax: how to get the index corresponding to the *last* occurrence, in case of multiple occurrences of the maximum values

description: Numpy.argmax: how to get the index corresponding to the *last* occurrence, in case of multiple occurrences of the maximum values


code:
for button in stock_button_list:
    self.button1=gtk.Button(stock=getattr(gtk,button))

rawDescription: How to create gtk.STOCK_* buttons from a modified list of strings?

description: Create gtk.STOCK_* buttons from a modified list of strings


code:
import subprocess
subprocess.call(r"net time \\172.21.5.135")

rawDescription: Get current system time of a machine on the network


code: object.__set__(self, instance, value)

rawDescription: Can a Python descriptor's __set__ ever get called with instance = None?


code:
@property
def my_date_formatted(self):
    return self.my_date.strftime('%B %Y')

rawDescription: Django Applying a date formatting in a model


Python: '\\x41' to '\x41'


code: WORKDIR /var/sites/demo

rawDescription: How to RUN docker commands inside a folder

description: RUN docker commands inside a folder


code:
x1=np.linspace(0,np.pi)
y1=np.sin(x1)

y2=np.sin(x1)

rect1=[0.1,0.1,0.8,0.8]
ax1=plt.axes(rect,frameon=True)
ax1.yaxis.tick_left()
plt.plot(x1,y1)
plt.ylabel('axis 1')
plt.xlabel('x')

rect2=[0.1,1,0.2,0.2]
ax2=plt.axes(rect2,frameon=False)
ax2.yaxis.tick_right()
ax2.plot(x1,y2)

percent = 0.2
xp = percent*np.pi
yp = np.sin(xp)
ax2.plot(xp,yp, marker='o')

ax2.yaxis.set_label_position('right')
ax2.axes.get_xaxis().set_visible(False)
ax2.axes.get_yaxis().set_visible(False)

ax2.annotate('%d Percent' %(percent*100), xy=(0.5, 0.))

rect3=[0.3,1,0.2,0.2]
ax3=plt.axes(rect3,frameon=False)
ax3.yaxis.tick_right()
ax3.plot(x1,y2)

percent = 0.4
xp = percent*np.pi
yp = np.sin(xp)
ax3.plot(xp,yp, marker='o')

ax3.yaxis.set_label_position('right')
ax3.axes.get_xaxis().set_visible(False)
ax3.axes.get_yaxis().set_visible(False)

ax3.annotate('%d Percent' %(percent*100), xy=(0.5, 0.))

plt.show()

rawDescription: using a pdf image inside a subplot in matplotlib

description: Using a pdf image inside a subplot in matplotlib


rawDescription: How to get Beautiful Soup to get link from href and class?

description: Get Beautiful Soup to get link from href and class


code:
def inheritors(klass):
    subclasses = set()
    work = [klass]
    while work:
        parent = work.pop()
        for child in parent.__subclasses__():
            if child not in subclasses:
                subclasses.add(child)
                work.append(child)
    return subclasses

rawDescription: Python: find all classes which inherit from this one?


rawDescription: Python: How do you get an XML element's text content using xml.dom.minidom?


code:
# socket setup

(buffer, sockaddress) = mysocket.recvfrom(6)
if len(buffer)== 6:
    i1,i2,i3,i4, port = struct.unpack('!BBBBH', buffer)

rawDescription: Deserializing C data

description: Deserializing C data


code:
xml_out = doc.toxml()
return xml_out[22:]

rawDescription: Getting the XML without the <?xml ...?> from minidom


code:
# models:  make sure the naming convention matches what ManyToManyField would create
class Report_LocationGroups(models.Model):
    locationgroup = models.ForeignKey(LocationGroup)
    report = models.ForeignKey(Report)

# admin
class ReportInline(admin.TabularInline):
    model = models.Report_LocationGroups

class LocationGroupAdmin(admin.ModelAdmin):
    inlines = ReportInline,

rawDescription: How to add bi-directional manytomanyfields in django admin?

description: Add bi-directional manytomanyfields in django admin


code:
delim_value = Group(delimitedList(string_value | int_value))("list")
list_value = Or( (Suppress("[") + delim_value + Suppress("]"),
                  Suppress("(") + delim_value + Suppress(")")) )

rawDescription: How to match parentheses / brackets in pyparsing

description: Match parentheses / brackets in pyparsing


code:
import fileinput

parsed_info = []
for linenum, line in enumerate(fileinput.input()):
    if not line.startswith("#DEBUG"):
        continue # Skip line

    msg = line.partition("MSG")[1] # Get everything after MSG
    words = msg.split() # Split on words
    info = {}
    for w in words:
        k, _, v = w.partition(":") # Split each word on first :
        info[k] = v

    parsed_info.append(info)

    if linenum % 10000 == 0: # Or maybe  if len(parsed_info) > 500:
        # Insert everything in parsed_info to database
        ...
        parsed_info = [] # Clear

rawDescription: best way to parsing Large files by regex python

description: Parsing Large files by regex python


code:
import datetime
d = datetime.datetime.strptime("01-01-1904", "%m-%d-%Y")
d
d + datetime.timedelta(seconds=3453120824)
(d + datetime.timedelta(seconds=3453120824)).strftime("%a, %d %b %Y %H:%M:%S GMT")

rawDescription: Convert Mac Timestamps with python

description: Convert Mac Timestamps with python


code:
def podcastsetup(request):

    title = 'Podcast Setup'
    title_align_center = True
    form = PodcastSetupForm(request.POST or None)
    context = {
        "title": title,
        "form": form
    }

    if form.is_valid():

        instance = form.save(commit=False)

        company_name = form.cleaned_data.get("company_name")
        first_name = form.cleaned_data.get("first_name")
        last_name = form.cleaned_data.get("last_name")

        instance.company_name = company_name
        instance.first_name = first_name
        instance.last_name = last_name

        instance.save()

        return render(request, "forms.html", context)
    else:
        return render(request, "forms.html", context)

rawDescription: The view producer.pfviews.podcastsetup didn't return an HttpResponse object. It returned None instead


code:
with open('data.txt') as inf:
    for line in inf:
        num = line.strip()
        if num:
            fn = '%s.txt' %num
            with open(fn, 'w') as outf:
                outf.write('contains one line of text "%s"\n' %num)

rawDescription: iterate through a text file and write a new text file for each line python

description: Iterate through a text file and write a new text file for each line python


code:
import webbrowser

webbrowser.open("http://xkcd.com/353/")

rawDescription: Programatically opening URLs in web browser in Python


code: text = re.sub(r"([" + re.escape(string.punctuation) + r"])\1+", r"\1", text)

rawDescription: How can I compress repetitive characters to a single character using RE in Python?

description: Compress repetitive characters to a single character using RE in Python


code:
import crypt
encrypted = crypt.crypt(password, '$6$' + salt + '$')

rawDescription: Storing user and password in a database

description: Storing user and password in a database


code:
def timeout(func, args=(), kwargs={}, timeout_duration=1, default=None):
    '''This function will spwan a thread and run the given function using the args, kwargs and
    return the given default value if the timeout_duration is exceeded
    '''
    import threading
    class InterruptableThread(threading.Thread):
        def __init__(self):
            threading.Thread.__init__(self)
            self.result = default
        def run(self):
            try:
                self.result = func(*args, **kwargs)
            except:
                self.result = default
    it = InterruptableThread()
    it.start()
    it.join(timeout_duration)
    if it.isAlive():
        return it.result
    else:
        return it.result

rawDescription: python exit a blocking thread?

description: Python exit a blocking thread


code:
db.aetitles.hospital_id.requires = [IS_NOT_EMPTY(),
                                IS_IN_DB(db, db.hospitals.id, '%(title)s')]

rawDescription: How to set multiple validators to a field in web2py?

description: Set multiple validators to a field in web2py


code:
import pygame
from pygame.locals import *
#about: pygame boilerplate

class GameMain():
    # handles intialization of game and graphics, as well as game loop
    done = False

    def __init__(self, width=800, height=600):
        """Initialize PyGame window.

        variables:
            width, height = screen width, height
            screen = main video surface, to draw on

            fps_max = framerate limit to the max fps
            limit_fps = boolean toggles capping FPS, to share cpu, or let it run free.
            now = current time in Milliseconds. ( 1000ms = 1second)
        """
        pygame.init()

        # save w, h, and screen
        self.width, self.height = width, height
        self.screen = pygame.display.set_mode(( self.width, self.height ))
        pygame.display.set_caption( "pygame tutorial code" )

        self.sprite_bg = pygame.image.load("bg.jpg").convert()
        self.sprite_ball = pygame.image.load("ball.png").convert_alpha()

    def main_loop(self):
        """Game() main loop."""
        while not self.done:
            self.handle_events()
            self.update()
            self.draw()

    def draw(self):
        """draw screen"""
        self.screen.fill(Color('darkgrey'))

        # draw your stuff here. sprites, gui, etc....
        self.screen.blit(self.sprite_bg, (0,0))
        self.screen.blit(self.sprite_ball, (100,100))

        pygame.display.flip()

    def update(self):
        """physics/move guys."""
        pass

    def handle_events(self):
        """handle events: keyboard, mouse, etc."""
        events = pygame.event.get()
        kmods = pygame.key.get_mods()

        for event in events:
            if event.type == pygame.QUIT:
                self.done = True
            # event: keydown
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE: self.done = True

if __name__ == "__main__":
    game = GameMain()
    game.main_loop()

rawDescription: Syntax Error When Following Pygame/Python Tutorial on youtube


code:
import re
import urllib2
html = urllib2.urlopen("http://craftstud.io/builds").read()
re.search(r"Server \d+\.\d+\.\d+\.\d+", html).group()

rawDescription: Simple Dynamic Web Scraping - Without BeautifulSoup


rawDescription: Spark operations map,flatmap,filter,ReduceByKey, using python


code:
X = numpy.array([1, 2, 3])
def g(x): return numpy.ones_like(x)
g(X)

rawDescription: Constant lambda expressions, shape of return data


code:
import networkx as nx
import matplotlib.pyplot as plt

G = nx.path_graph(4)
nx.draw_networkx(G)
plt.grid('on')
plt.show()

rawDescription: Show a background grid (a.k.a. cells) when drawing a graph with networkx


rawDescription: Pascal funcs Insert/Delete in Python

description: Pascal funcs Insert/Delete in Python


code:
import win32com.client
import win32gui
import win32process

hwnd = win32gui.GetForegroundWindow()
_, pid = win32process.GetWindowThreadProcessId(hwnd)
shell = win32com.client.Dispatch("WScript.Shell")

shell.AppActivate('filename.docx')

rawDescription: Open documents in the browser

description: Open documents in the browser


code:
class Node:
    def __init__(self, value=None, left_child=None, right_child=None):
        self.value = value
        self.left_child = left_child
        self.right_child = right_child

class BinarySearchTree:
    def __init__(self):
        self.root = Node()

    def __str__(self):
        return 'bst'

    def insert(self, root, value):
        if root is None:
            root = Node()
        if root.value is None:
            root.value = value
        else:
            if value < root.value:
                self.insert(root.left_child, value)
            else:
                self.insert(root.right_child, value)

if __name__ == '__main__':
    bst = BinarySearchTree()
    data = []
    for i in range(100):
        data.append(randint(0,100))
        bst.insert(bst.root, data[i])
    print(bst.root.value)

rawDescription: what's wrong with my method insert of binary search tree?

description: What's wrong with my method insert of binary search tree


code: (5*x for x in xrange(2,4)).next()

rawDescription: Why is there no first(iterable) built-in function in Python?


code:
def truthtable(f, values=None):
    if values is None:
        prints(variables(f) + ["formula"])
        values = []
    # print values
    if len(values) == len(variables(f)):
        prints(values + [f(*values)])
    else:
        for b in [True, False]:
            truthtable(f, values + [b])

rawDescription: Making truthtables in python

description: Making truthtables in python


code:
import pandas as pd
import matplotlib.pyplot as plt
a = {'Test1': {1: 21867186, 4: 20145576, 10: 18018537},
     'Test2': {1: 23256313, 4: 21668216, 10: 19795367}}

d = pd.DataFrame(a).T
#print d

f = plt.figure()

plt.ticklabel_format(style = 'plain')

plt.title('Title here!', color='black')
d.plot(kind='bar', ax=f.gca())
plt.show()

rawDescription: How to avoid axis values with 1e7 in pandas and matplotlib

description: Avoid axis values with 1e7 in pandas and matplotlib


code:
def getsanityresults(xmlfile):
    testresult=[]
    dom = minidom.parse(xmlfile)
    data=dom.getElementsByTagName('Sanity_Results')
    textnode = data[0].childNodes[0]
    testresults=textnode.data
    for line in testresults.splitlines():
        line = line.strip('\r,\n')
        line = re.sub(r'(http://[^\s]+|//[^\s]+|\\\\[^\s]+)', r'\1', line)
        testresult.append(line)
    return '
'.join(testresult)

rawDescription: Outputting hyperlinks to outlook

description: Outputting hyperlinks to outlook


code:
import subprocess, os
my_env = os.environ.copy()
my_env["PATH"] = "/usr/sbin:/sbin:" + my_env["PATH"]
subprocess.Popen(my_command, env=my_env)

rawDescription: Python subprocess/Popen with a modified environment


code:
class params:
    a = 1
    b = 2
    c = 3
    d = 4

    def __call__(self):
        r = dict()
        for attr in dir(self):
            if not attr.startswith('_'):
                r[attr] = getattr(self, attr)
        return r

p = params()
function(**p())

rawDescription: Python Class elements as Function parameters


code:
class BaseEngine(object):

    # Core methods: engines have to provide their own implementation
    #               (except for from_string which is optional).

    def __init__(self, params):
        """
        Initializes the template engine.
        Receives the configuration settings as a dict.
        """
        params = params.copy()
        self.name = params.pop('NAME')
        self.dirs = list(params.pop('DIRS'))
        self.app_dirs = bool(params.pop('APP_DIRS'))
        if params:
            raise ImproperlyConfigured(
                "Unknown parameters: {}".format(", ".join(params)))

rawDescription: Why setting a dict shallow copy to itself?


code:
item_ids = db.my_categories(slug=request.args[0]).items_
items = db(db.my_items.id.belongs(item_ids)).select()

rawDescription: How can I select all items referenced by a specific list:reference field?

description: Select all items referenced by a specific list:reference field


code:
from django.utils.translation import ugettext as _

class Foo(models.Model):
    help_text = _(u'Documentation of Foo model to localize')

rawDescription: Displaying model's docstring in admin app


code:
(6, lambda number: (
    ungettext('%(value).1f million', '%(value).1f million', number),
    ungettext('%(value)s million', '%(value)s million', number),
)),

rawDescription: Python Number to Word e.g 10K, 10.65M

description: Python Number to Word e.g 10K, 10.65M


code:
from Tkinter import *

# a subclass of Canvas for dealing with resizing of windows
class ResizingCanvas(Canvas):
    def __init__(self,parent,**kwargs):
        Canvas.__init__(self,parent,**kwargs)
        self.bind("<Configure>", self.on_resize)
        self.height = self.winfo_reqheight()
        self.width = self.winfo_reqwidth()

    def on_resize(self,event):
        # determine the ratio of old width/height to new width/height
        wscale = float(event.width)/self.width
        hscale = float(event.height)/self.height
        self.width = event.width
        self.height = event.height
        # resize the canvas
        self.config(width=self.width, height=self.height)
        # rescale all the objects tagged with the "all" tag
        self.scale("all",0,0,wscale,hscale)

def main():
    root = Tk()
    myframe = Frame(root)
    myframe.pack(fill=BOTH, expand=YES)
    mycanvas = ResizingCanvas(myframe,width=850, height=400, bg="red", highlightthickness=0)
    mycanvas.pack(fill=BOTH, expand=YES)

    # add some widgets to the canvas
    mycanvas.create_line(0, 0, 200, 100)
    mycanvas.create_line(0, 100, 200, 0, fill="red", dash=(4, 4))
    mycanvas.create_rectangle(50, 25, 150, 75, fill="blue")

    # tag all of the drawn widgets
    mycanvas.addtag_all("all")
    root.mainloop()

if __name__ == "__main__":
    main()

rawDescription: How to get tkinter canvas to dynamically resize to window width?

description: Get tkinter canvas to dynamically resize to window width


code:
import calendar

month = (2014, 9) # October, 2014

sum(1 for week in calendar.monthcalendar(*month) if week[calendar.MONDAY])
Out[28]: 5

sum(1 for week in calendar.monthcalendar(*month) if week[calendar.FRIDAY])
Out[29]: 4

rawDescription: Get amount of weekday X in a month

description: Get amount of weekday X in a month


code:
class Room:
    def __init__(self, building, floor, office):
        self.building = building
        self.floor = floor
        self.office = office

class Entry:
    def __init__(self, lastname, firstname, room):
        self.lastname = lastname
        self.firstname = firstname
        self.room = room

entries = []
file=open('file.txt','r')
for line in file.readlines():
    lastname, remaining = line.split(', ')
    firstname, remaining = remaining.split(' ')
    building, floor, office = remaining.split('.')
    room = Room(building, floor, office)
    entry = Entry(lastname, firstname, room)
    entries.append(entry)
file.close()

rawDescription: Python classes and lists

description: Python classes and lists


code:
def create_gs_key(filename, rpc=None):
  """Create an encoded key for a Google Storage file.

  The created blob key will include short lived access token using the
  application's service account for authorization.

  This blob key should not be stored permanently as the access token will
  expire.

  Args:
    filename: The filename of the google storage object to create the key for.
    rpc: Optional UserRPC object.

  Returns:
    An encrypted blob key object that also contains a short term access token
      that represents the application's service account.
  """
  rpc = create_gs_key_async(filename, rpc)
  return rpc.get_result()

rawDescription: Are blob_key's for GCS entities safe to use later?


code:
lookup = set([x[0] for x in least_common])
new_array = []
for elem in array:
    if elem in lookup:
        new_array.append(elem + (elem * (mod[1]/100)))
    else:
        new_array.append(elem)

rawDescription: Modifying specific array elements in Python 3.1


code:
import numpy as np
import matplotlib.pyplot as plt
data = np.array([[1,2,3,4,5,6], [4,3,8,9,1,2]]).T
data
y = data[:,1].reshape(-1,2).sum(axis=1)
y
x = data[:,0].reshape(-1,2).mean(axis=1)
x
plt.bar(x, y)
plt.show()

rawDescription: matplotlib histogram with frequency and counts

description: Matplotlib histogram with frequency and counts


code:
import sys
import time

def flush_out(string):
    print(string)
    sys.stdout.flush()

#script does something
flush_out("Waiting for 10 seconds...")
time.sleep(10)
flush_out("Exiting")
sys.exit(0)

rawDescription: Gradle Exec task and process output

description: Gradle Exec task and process output


code:
from csv import DictWriter
import usaddress

tagged_lines = []
fields = set()
# Note 1: Use the 'with' statement instead of worrying about opening
# and closing your file manually
with open('address_sample.txt') as in_file:
    # Note 2: You don't need to mess with readline() and while loops;
    # just iterate over the file handle directly, it produces lines.
    for line in in_file:
        tagged = usaddress.tag(line)[0]
        tagged_lines.append(tagged)
        fields.update(tagged.keys()) # keep track of all field names we see

with open('address_sample.csv', 'w') as out_file:
    writer = DictWriter(out_file, fieldnames=fields)
    writer.writeheader()
    writer.writerows(tagged_lines)

rawDescription: Convert tuple containing an OrderedDict with tagged parts to table with columns named from tagged parts


code:
scale_w = new_width/old_width
scale_h = new_height/old_height
photoImg.zoom(scale_w, scale_h)

rawDescription: image resize under PhotoImage

description: Image resize under PhotoImage


code: return rand.random() > 0.5

rawDescription: Running a loop while a function is true in python


code:
urls = ['/issues/2013-01-15;2013-01-15/all=NoticeCode%3a2441/start=1',
'/issues /2013-01-15;2013-01-15/all=NoticeCode%3a2453/start=1',
'/issues/2013-01-15;2013-01-15/all=NoticeCode%3a2462/start=1',
'/issues/2012-02-10;2013-02-20/all=NoticeCode%3a2441/start=1']

base_url = 'http://www.london-gazette.co.uk'
for u in urls:
    starting_url = urlparse.urljoin(base_url, u)
    scrape_and_look_for_next_link(starting_url)

rawDescription: Scraping links from more than one URL

description: Scraping links from more than one URL


rawDescription: using first row as heading in data frame

description: Using first row as heading in data frame


code:
class keep_writer:
    def write(self, txt):
        self.txt = txt

@app.route('/large.csv')
def generate_large_csv():
    def generate():
        kw = keep_writer()
        wr = csv.writer(kw)  # add optional configuration for the csv.writer
        for row in iter_all_rows():
            wr.writerow(row)  # just write the row
            yield kw.txt      # and yield the line build by the csv.writer
    return Response(generate(), mimetype='text/csv')

rawDescription: How to return one generated line from csv?

description: Return one generated line from csv


code:
from collections import Counter

pairs = Counter()

with open(...) as fp:
   for line in fp:
      pairs.update(re.findall(r'\d+\s+\d+', line))

rawDescription: Python: counting occurrences of a set of strings in a file


code:
ordered = (qs.extra(select={"order_column": "CONVERT(column, INTEGER)"})
           .order_by("order_column"))

rawDescription: How to order a Django QuerySet string property numerically?

description: Order a Django QuerySet string property numerically


code:
prices
orders
prices.lookup(orders.Date, orders.ticker)

rawDescription: Vectorized look-up of values in Pandas dataframe


code:
class A(object): pass
class B(object): pass
hash(A)
hash(B)

rawDescription: Dictionary of types

description: Dictionary of types


code:
from mongoengine import *
import datetime

class Page(Document):
    title = StringField(max_length=200, required=True)
    date_modified = DateTimeField(default=datetime.datetime.now)

rawDescription: mongodb columns constraints in python like ruby mongoid

description: Mongodb columns constraints in python like ruby mongoid


code:
import dns.resolver

my_resolver.nameservers = ['8.8.8.8']
answer = my_resolver.query(
    qname = fqdn_port,
    source = '1.2.3.4',
)

rawDescription: dnsPython use IP on interface

description: DnsPython use IP on interface


code:
specific_cell = df.iloc[0,11]
df['SpecificCell'] = specific_cell

rawDescription: Find Specific Cell in Pandas Dataframe Then Fill new column with it


code:
client = MongoClient(host=MONGO_HOST,port=27017,max_pool_size=200)
client.drop_database("database_name")

rawDescription: How to delete pymongo.Database.Database object

description: Delete pymongo.Database.Database object


code:
import urllib

urllib.urlretrieve("http://www.digimouth.com/news/media/2011/09/google-logo.jpg", "local-filename.jpg")

rawDescription: How to save an image locally using Python whose URL address I already know?

description: Save an image locally using Python whose URL address I already know


code:
import gobject
gobject.threads_init()

rawDescription: using threads and pyGST in a wx python app

description: Using threads and pyGST in a wx python app


code:
from django.contrib import admin
from wiki.models import Page, Category, CategoryItem

for cls in (Page, Category, CategoryItem):
    admin.site.register(cls)

rawDescription: How to add multiple models from a Django app to adminpanel?

description: Add multiple models from a Django app to adminpanel


code:
import collections
d = collections.defaultdict(list)
d[0].append('potato')
d

rawDescription: Pythonic way to make sure list exists at dict key before appending


code:
config.add_route('users', '/users')
config.add_route('user', '/users/{id:\d+}')

API = ConvenienceAPI()

@view_defaults(route_name='users', renderer='json')
class UsersViews(object):

    api = API

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

    @view_config(request_method='GET')
    def get(self):
        users = self.api.retrieve_users()
        return users

    @view_config(request_method='POST')
    def post(self):
        username = self.request.POST.get('username')
        password = self.request.POST.get('password')
        firstname = self.request.POST.get('firstname')
        lastname = self.request.POST.get('lastname')
        email = self.request.POST.get('email')
        user = self.api.create_user(username, password, firstname, lastname, email)
        return user

@view_defaults(route_name='user', renderer='json')
class UserViews(object):

    api = API

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

    def get_user(self):
        user_id = int(self.request.matchdict['id'])
        user = self.api.retrieve_user(user_id)
        return user

    @view_config(request_method='GET')
    def get(self):
        user = self.get_user()
        if user is None:
            raise HTTPNotFound()
        return user

    @view_config(request_method='PUT')
    def put(self):
        user = self.get_user()
        if user is None:
            raise HTTPNotFound()
        new_username = self.request.POST.get('username')
        updated_user = self.api.update_user(user.username, new_username) # This is a strange way of updating the username, but your 'API' is not really relevant to this question.
        return HTTPAccepted() # Some REST services return the updated user.

    @view_config(request_method='DELETE')
    def delete(self):
        user = self.get_user()
        del_user = self.api.delete_user(user.username, user.firstname, user.lastname)
        if del_user: # If user was deleted.
            return HTTPAccepted() # Or something like this. As above, you might want to return the deleted user.
        else:
            return HTTPBadRequest()

rawDescription: RESTful design in Pyramid View Configuration


code:
import csv
from collections import defaultdict

data = defaultdict(list)

with open(inputfilename, 'rb') as infh:
    reader = csv.reader(infh)
    next(reader, None)  # skip the header

    for col1, col2 in reader:
        data[col2].append(int(col1))
        if len(data[col2]) > 1:
            data[col2] = range(min(data[col2]), max(data[col2]) + 1)

rawDescription: Convert a csv to a dictionary with multiple values?


code:
# I think it's axis 2, might have to play around there
mask = (im_gray != 255).all(axis=2)
im_out[mask] = im_gray[mask]

rawDescription: Shorter way of this NumPy list comprehension


code:
if request.POST.get('delete'):
    obj.delete()

rawDescription: How to add a Delete button to django.forms generated form?

description: Add a Delete button to django.forms generated form


code:
given_number = 10
l = list(range(1, given_number + 1))
odds = [i for i in l if i%2]
print(l, odds)

rawDescription: Read a python list and remove certain numbers from given list


code:
from numpy import mean
from scipy import cumsum
indexes = cumsum([0] + indexmeans)
[mean(listA[i:j]) for i,j in zip(indexes[:-1], indexes[1:])]

rawDescription: Python mean of subset of a list

description: Python mean of subset of a list


code:
from django.db.models import Q

normal_and_alive = Q(cat_type="normal") & ~Q(lives__state__in=["dead", "unknown"])
schroedinger_and_not_dead = Q(cat_type="schroedinger") & ~Q(lives__state="dead")

cats = Cat.objects.filter(normal_and_alive | schroedinger_and_not_dead)

rawDescription: How to filter a Django QuerySet's related fields' 'all' or 'none'

description: Filter a Django QuerySet's related fields' 'all' or 'none'


code:
from django.contrib.auth.models import User
from rest_framework import viewsets
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response
from myapp.serializers import UserSerializer, PasswordSerializer

class UserViewSet(viewsets.ModelViewSet):
    """
    A viewset that provides the standard actions
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer

    @action()
    def set_password(self, request, pk=None):
        user = self.get_object()
        serializer = PasswordSerializer(data=request.DATA)
        if serializer.is_valid():
            user.set_password(serializer.data['password'])
            user.save()
            return Response({'status': 'password set'})
        else:
            return Response(serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

rawDescription: Django rest framework ModelSerializer

description: Django rest framework ModelSerializer


code:
df2 = df.copy()
df2['Date'] = df2['Date'] - pd.Timedelta(1, 'd')
pd.concat([df, df2], keys=[0,1]).swaplevel(0,1).sort_index()

rawDescription: add calculated row after each row in dataframe

description: Add calculated row after each row in dataframe


code:
class FEOProcessor(object):
    CHECKS = [
        ('Standard JavaScript Inlining Optimization', ('EMBED_JAVASCRIPT',), 'check_js_inlining'),
        ('HTML5 Advanced Cache', ('JAVASCRIPT_HTML5_CACHE', 'CSS_HTML5_CACHE'), 'check_html5_advanced_cache'),
        ('Cookieless Resource Domain', ('RENAME_JAVASCRIPT', 'RENAME_CSS'), 'check_cookieless_resource_domain'),
        ('Minificatiopn of JS', ('MINIFY_JAVASCRIPT',), 'check_js_minifaction'),
        ('File Versioning', ('RENAME_JAVASCRIPT', 'RENAME_IMAGE', 'RENAME_CSS'), 'check_file_versioning'),
        ('Small Image Embedding', ('EMBED_IMAGE',), 'check_small_image_embedding'),
        ('Responsive Image Loading', ('RESPONSIVE_IMAGES',), 'check_responsive_image_loading'),
        ('Asynchronous JS and CSS Loading', ('ASYNC_JAVASCRIPT',), 'check_async_js_and_css_loading'),
        ('JS Pre-Execution', ('PRE_EXECUTE_JAVASCRIPT',), 'check_js_pre_execution'),
    ]

    def __init__(self):
        self.parser = Extractor()
        self.result = dict((k, 'Not Applied') for k,_,_ in self.CHECKS)

    for _, keys, name in CHECKS:
        locals()[name] = lambda self, result, _keys=keys: all(result.get(k, 0)>0 for k in _keys)

    def process_feo_debug_output(self, analysis_id, url):
        feed = self.parser.start_parser(analysis_id, url, True)
        result = self.get_feo_tags(feed)
        for name, _, func in self.CHECKS:
            self.result[name] = ('Not Applied','Applied')[getattr(self,func)(result)]
        return self.result

    def get_feo_tags(self, feed):
        result = {}
        tag_list = re.findall(r'(?:TextTransApplied):\s*((?:(?:[A-Z]+(?:_[A-Z\d]+)+)?\(\d+\)\s*(?:,\s*|;))*)', str(feed))
        for tag in tag_list:
            for element in tag.split(","):
                index = element.index('(')
                if element[:index].strip():
                    result[element[:index].strip()] = (element.split("(")[1].rstrip(");"))
        return result

rawDescription: How to get rid of all the if statements in here

description: Get rid of all the if statements in here


code: copy_list = org_list[:]

rawDescription: python: changes to my copy variable affect the original variable

description: Python: changes to my copy variable affect the original variable


code: re.search('[a-zA-Z]', the_string)

rawDescription: How can I check if a string contains ANY letters from the alphabet?

description: Check if a string contains ANY letters from the alphabet


code:
start_string = "aaafoobbb"
search_string = "foo"
index = start_string.index(search_string)
s[(index - 2) : (index + len(search_string) + 2)]

rawDescription: Extracting a substring from a string and also including 10 characters before and after


code: raise ValueError('invalid gender %r' % gender)

rawDescription: How to designate unreachable python code

description: Designate unreachable python code


rawDescription: Python function that knows which button engaged it (Tkinter)


code:
def foo(bar):
    return foo.my_map.get(bar, defaultType)()
foo.my_map = {"rab": barType, "oof": fooType}

rawDescription: In python, how to store 'constants' for functions only once?


code:
from pygooglechart import PieChart3D

def python_pie3D() :
  # initialize chart object, 250 x 250 pixels
  chart = PieChart3D(250, 250)

  # pass your data to the chart object
  chart.add_data([398, 294, 840, 462])

  # make labels for the slices
  chart.set_pie_labels("Lithuania Bulgaria Ukraine Romania".split())

  # render the image
  chart.download('revenue_east_europe.png')

rawDescription: 3D pie graph in matplotlib

description: 3D pie graph in matplotlib


code:
pt = (2, 2)
distance = 1
mask = (indices[0] - pt[0]) ** 2 + (indices[1] - pt[1]) ** 2 <= distance ** 2
result = my_array[mask]

rawDescription: How to filter a numpy array based on indices?

description: Filter a numpy array based on indices


code:
def send_complex_message():
    return requests.post("https://api.mailgun.net/v2/DOMAIN/messages",
              auth=("api", "key-SECRET"),
              files={
                  "attachment[0]": ("FileName1.ext", open(FILE_PATH_1, 'rb')),
                  "attachment[1]": ("FileName2.ext", open(FILE_PATH_2, 'rb'))
              },
              data={"from": "FROM_EMAIL",
                    "to": [TO_EMAIL],
                    "subject": SUBJECT,
                    "html": HTML_CONTENT
              })

rawDescription: How to send a mail with multiple attachments and custom file names using Mailgun (from Python)

description: Send a mail with multiple attachments and custom file names using Mailgun (from Python)


code:
#!/usr/bin/python
import urllib
import shutil

ids_to_transfer = ['MyFolder1', 'MyFolder2']

for id in ids_to_transfer:
    urllib.urlopen('http://admin:password_dev@localhost:8080/manage_exportObject?id=' + id)

    shutil.move('/tmp/instance/dev/var/' + id + '.zexp', '/tmp/instance/prod/import/' + id + '.zexp')

    urllib.urlopen('http://admin:password_prod@localhost:9090/manage_delObjects?ids=' + id)
    urllib.urlopen('http://admin:password_prod@localhost:9090/manage_importObject?file=' + id + '.zexp')

rawDescription: Exporting a zope folder with python

description: Exporting a zope folder with python


code:
from os import environ
is_android = 'ANDROID_STORAGE' in environ
is_android

rawDescription: How to distinguish "Android" from other "Linux" operating systems in Python?

description: Distinguish "Android" from other "Linux" operating systems in Python


code:
data['positive'] = data['values'] > 0
data
data['values'].plot(kind='barh', color=data.positive.map({True: 'r', False: 'k'}))

rawDescription: Matplotlib Bar Chart Choose Color if Value is Postive vs Value is Negative


code:
class RecordForm(forms.Form):
    name = forms.CharField(max_length=255)
    domain = forms.TypedChoiceField(choices=[])
    type = forms.TypedChoiceField(choices=...)
    content = forms.CharField()
    ttl = forms.CharField()
    comment = forms.CharField()

    def __init__(self, *args, **kwargs):
        super(RecordForm, self).__init__(*args, **kwargs)
        self.fields['type'].choices = [(c.id, c.name) for c in Domain.objects.all()]

rawDescription: django - form field will not update without server reset... instantiating a class again

description: Django - form field will not update without server reset... instantiating a class again


code:
title_count = Counter(titles).most_common()
for name,count in title_count:
    print('{} was found {} times'.format(name, count))

rawDescription: Extracting frequency from list

description: Extracting frequency from list


code:
def convertible(v):
    try:
        int(v)
        return True
    except (TypeError, ValueError):
        return False

newlist = [i for i in mylist if convertible(i)]

rawDescription: Python : Clean and efficient way to remove items that are not convertable to int from list


rawDescription: Get character position in alphabet

description: Get character position in alphabet


code:
from itertools import product

basis = ['ssaa','asas']
sample = ['x','y','z']

results = []
for base in basis:
    for comb in map(list, product(sample, repeat=base.count('s'))):
        results.append(''.join(comb.pop(0) if l == 's' else l for l in base))
print(results)

rawDescription: Replacing certain letters in a string by every other letter in a list


code:
# first create new column
df2['merge_col'] = df2['col_x']

# replace empty values
empty_rows = df2['merge_col'].isnull()
df2.loc[empty_rows, 'merge_col'] = df2.loc[empty_rows, 'col_y']

# merge with the new column
df3 = pd.merge(df1, df2, left_on='col1', right_on='merge_col', how='left')

rawDescription: Pandas join two tables but not sure with which column in df2 ('or' syntax?)


code:
[i.get('addr') for i in tree.findall(
    './host/address[@vendor="QEMU Virtual NIC"]/../address[@addrtype="ipv4"]')]

rawDescription: How to get the IP address from a nmap output in XML with an specific vendor using ElementTree in Python

description: Get the IP address from a nmap output in XML with an specific vendor using ElementTree in Python


code:
STATICFILES_DIRS = (
     '/home/arnav/project/static',
     '/home/arnav/Documents/covers',
)

rawDescription: Is it possible to use django to load a file from a system directory?


code:
mapper(User, users_table, include_properties=['user_id', 'user_name'])

mapper(Address, addresses_table, exclude_properties=['street', 'city', 'state', 'zip'])

rawDescription: Map only certain parts of the class to a database using SQLAlchemy?


code:
rowMask="4s"*611
def readUsableFields(filename,stdPath):
    usableFields=[]

    with open(stdPath+filename,"r") as f:
            count_line=0
            for line in f:
                    count_col=0
                    # len(line) = 611 * 4 +1
                    # as there is a trailing '\0'
                    if(len(line)!=2445):
                            continue
                    fields=struct.Struct(bytes(rowMask,"UTF-8")).unpack_from(bytes(line,"UTF-8"))
                    for field in fields:
                            if(field!=-999):
                                    usableFields.append([count_line,count_col])
                            count_col+=1
                    count_line+=1

            f.close()
    return usableFields

rawDescription: unpack_from() des not work with big files

description: Unpack_from() des not work with big files


code:
class User(Base):
    __tablename__ = 'users'

    def __init__(self, **kwargs):
        if 'money' not in kwargs:
             kwargs['money'] = self.__table__.c.money.default.arg
        super(User, self).__init__(**kwargs)

    id = Column(Integer, primary_key=True)
    money = Column(Integer, default=100)

rawDescription: How to apply Column defaults before a commit in sqlalchemy

description: Apply Column defaults before a commit in sqlalchemy


code:
import csv

f = open(r'/path/to/csv_f.txt')
g = open(r'/path/to/csv_g.txt')
csv_f = csv.reader(f)
csv_g = csv.reader(g, delimiter=' ')

dict = []

for row_g in csv_g:
    dict.append(row_g)
for row_f in csv_f:
    positions = []
    counts = []
    size = len(row_f)
    i=0
    while i<size:
        fword = row_f[i]
        for line in dict:
            gword = line[1]
            if fword == gword:
                positions.append(line[0])
                counts.append(1)
                break
        i+=1
    print(positions, counts)

rawDescription: Python - issues with return in function

description: Python - issues with return in function


code:
mylist, var1, var2 = ['john','phil','ted'], 'xxx', 'zzz'
'{0} bla bla bla {1} bla bla {2}'.format(var1, " ".join(mylist), var2)

rawDescription: Python Format String with List

description: Python Format String with List


code:
def tearDown(self):
    User.objects.all().delete()

rawDescription: How to test a function for different database states in Python and Django? (avoiding repetitions)

description: Test a function for different database states in Python and Django? (avoiding repetitions)


rawDescription: Python Pandas: How can I search for a string in a column?


code:
def makePickle(self, record):
    ei = record.exc_info
    if ei:
        dummy = self.format(record)
        record.exc_info = None
    d = dict(record.__dict__)
    d['msg'] = record.getMessage()
    d['args'] = None
    s = cPickle.dumps(d, 1)
    if ei:
        record.exc_info = ei

    # slen represents a "long integer", which is usually 32 bits large.
    slen = struct.pack(">L", len(s))

    # Here is where the 4 byte length is prepended to the message
    return slen + s

def emit(self, record):
    try:
        s = self.makePickle(record)
        # s is actually (length of the message) + (message)
        self.send(s)
    except (KeyboardInterrupt, SystemExit):
        raise
    except:
        self.handleError(record)

def send(self, s):
    """
    Send a pickled string to the socket.

    This function allows for partial sends which can happen when the
    network is busy.
    """
    if self.sock is None:
        self.createSocket()

    if self.sock:
        try:
            if hasattr(self.sock, "sendall"):
                self.sock.sendall(s)
            else:
                sentsofar = 0
                left = len(s)
                while left > 0:
                    sent = self.sock.send(s[sentsofar:])
                    sentsofar = sentsofar + sent
                    left = left - sent
        except socket.error:
            self.sock.close()
            self.sock = None  # so we can call createSocket next time

rawDescription: how do we know the first 4 bytes read on a tcp socket are the length of the message?

description: How do we know the first 4 bytes read on a tcp socket are the length of the message


code:
X_new = vectorizer.transform([sentence])
y_new = model.predict(X_new)

rawDescription: predicting closest cluster sklearn

description: Predicting closest cluster sklearn


code:
a = np.array([[np.array([1,2,3]), np.array([2])], [np.array([0,1,2,3,4]), np.array([0,4])]])
vmean = np.vectorize(np.mean)
vmean(a)

rawDescription: mean of array of array

description: Mean of array of array


code:
from six import string_types
isinstance(s, string_types)

rawDescription: How to check if variable is string with python 2 and 3 compatibility

description: Check if variable is string with python 2 and 3 compatibility


code:
while True:
    files = os.path.listdir('./sub_directory/')

    if files != []:
        do_something()

rawDescription: (Unix/Python) Activity Monitor % CPU

description: (Unix/Python) Activity Monitor % CPU


code:
from varnish import VarnishManager as varnish_manager
manager = varnish_manager(('localhost:6082',))
manager.run('stop', secret=current_app.config['VARNISH_SECRET'])
manager.run('start', secret=current_app.config['VARNISH_SECRET'])

rawDescription: Clear all Varnish cache from python

description: Clear all Varnish cache from python


code:
def update(val):
    frame = numpy.around(sframe.val)
    l.set_data(data[frame,:,:])

rawDescription: Visualization of 3D-numpy-array frame by frame


code:
from distutils.version import LooseVersion  # or StrictVersion
LooseVersion("1.0-SNAPSHOT") < LooseVersion("1.1-SNAPSHOT")
v = LooseVersion("1.0.1-SNAPSHOT")
v.version

rawDescription: python module for handling version of software

description: Python module for handling version of software


code:
import numpy as np
from scipy.sparse import csr_matrix

user_ids = []
subreddit_ids = []
subreddits = {}
i=0
with open("reddit_user_posting_behavior.csv", 'r') as f:
    for line in f:
        for sr in line.rstrip().split(",")[1:]:
            if sr not in subreddits:
                subreddits[sr] = len(subreddits)
            user_ids.append(i)
            subreddit_ids.append(subreddits[sr])
        i+=1

adj = csr_matrix(
    ( np.ones((len(userids),)), (np.array(subreddit_ids),np.array(user_ids)) ),
    shape=(len(subreddits), i) )

rawDescription: Efficiently constructing sparse biadjacency matrix in Numpy


code:
def fwriteKeyVals(data, f, indent=0):
    if isinstance(data, list):
        try:
            gotdata = isinstance(data[0], list)
        except IndexError:
            gotdata = False

        if gotdata:

            f.write( "\n" + "    " * indent + "[" )
            for i in range(len(data) ):
                if ( i == 0):
                    f.write( "[" )
                else:
                    f.write( "    " * indent + " [" )
                for j in range(len(data[0])):
                    f.write( "%3d" % data[i][j] )
                    f.write( "," ) if j != len(data[0])-1 else (f.write( "]," ) if i != len(data)-1 else f.write( "]" ))
                f.write( "\n" ) if i != len(data)-1 else f.write( "]" )
        else:
            try:
                gotdata = data[0]
            except IndexError:
                gotdata = 'False'

            if gotdata is not 'False':
                f.write( " [" )
                for i in range(len(data) ):
                    if isinstance(data[i], Number):
                        f.write( "%3d" % data[i] )
                    else:
                        dataListLf = data[i].split("\n")
                        dataToWrite = dataListLf[0]
                        for line in dataListLf[1:]:
                            dataToWrite += '\\n'+line

                        f.write( "\"" + dataToWrite + "\"")
                    f.write( "," ) if i != len(data)-1 else f.write( "]" )
            else:
                f.write( " [\"\"]" )

rawDescription: Pretty print a JSON.dump 2d array in Python


code:
class SlideShow(models.Model):
    name = models.CharField(blank=False, max_length=50)
    slug = models.SlugField(blank=False, max_length=50)
    type = models.CharField(choices=SLIDESHOW_TYPES, default=SLIDESHOW_TYPE_DEFAULT, max_length=20)

    height = models.IntegerField(blank=False)
    width = models.IntegerField(blank=False)

    auto_play = models.BooleanField(default=True)
    loop_play = models.BooleanField(default=True)
    random = models.BooleanField(default=True)
    display_time = models.DecimalField(blank=False, decimal_places=3, max_digits=5)
    transition_speed = models.DecimalField(blank=False, decimal_places=3, max_digits=5)

    music_file = models.FileField(blank=True,upload_to=gen_slideshowimage_upload_dir_func('music'))
    music_loop = models.BooleanField(default=True)

    def slide_count(self):
        return len(self.images_set.all())

    def __unicode__(self):
        return self.name

class Images(modes.Model):
    image = models.FileField(blank=False,upload_to=gen_slideshowimage_upload_dir_func('images'))
    caption = models.CharField(blank=True, max_length=25)
    slideshow = models.ForeignKey(SlideShow, on_delete=models.CASCADE)

rawDescription: How do I repeat certain fields as needed in Django admin?

description: Repeat certain fields as needed in Django admin


code:
from boto.s3.key import Key
k=Key(bucket)
k.key='cli-images/image-thumb.jpg'
k.copy('bucketname', 'cli-images/moved/image-thumb.jpg')
k.delete()

rawDescription: Using boto library on S3

description: Using boto library on S3


code:
from pymongo import MongoClient
client = MongoClient('localhost:27017')

def my_function(mydb):
    db = client.get_database(mydb)
    return db.collection.find().count()

print(my_function('my_database'))

rawDescription: How to connect mongoDB with python?

description: Connect mongoDB with python


code: ' '.join('abcdefg')

rawDescription: adding characters to a string in python 3

description: Adding characters to a string in python 3


code:
with open(filename, 'rb+') as filehandle:
    filehandle.seek(-1, os.SEEK_END)
    filehandle.truncate()

rawDescription: Python - Remove very last character in file


code:
import urllib2
url = 'http://www.example.com/index.html'
req = urllib2.Request(url)
response = urllib2.urlopen(req)
#now you can get the data by response.read()

rawDescription: to establish the connection with the some web page in python

description: To establish the connection with the some web page in python


code:
for groups in people:
    s = 0
    for i in range(min(len(groups), num_dos)):
        s += sum(dollars.get(x, 0) for x in groups[i])
    groups.append(s)

rawDescription: Dictionary lookup efficiency

description: Dictionary lookup efficiency


code:
f = open('foo.txt', 'w')
f.write('a')
f.close()
f = open('foo.txt ', 'r')

rawDescription: Loading file to string, no such file?

description: Loading file to string, no such file


code:
import Tkinter as tk

class Q_and_A(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master, borderwidth=1, relief="sunken")
        self.label = tk.Label(self)
        self.results_txtbx = tk.Text(self, width=20, height=4, wrap="none",
                                     borderwidth=0, highlightthickness=0)
        self.results_scrbr = tk.Scrollbar(self, orient="vertical",
                                          command=self.results_txtbx.yview)
        self.results_txtbx.configure(yscrollcommand=self.results_scrbr.set)

        self.label.grid(row=1, columnspan=2)
        self.results_scrbr.grid(row=0, column=1, sticky="ns")
        self.results_txtbx.grid(row=0, column=0, sticky="nsew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

root = tk.Tk()
root.wm_title("Question And Answer")

app = Q_and_A(root)
app.pack(side="top", fill="both", expand=True)
root.mainloop()

rawDescription: Tkinter - Text widget shrinks when Scrollbar is added


code:
from win32security import LookupAccountName, ConvertSidToStringSid
from _winreg import OpenKey, QueryValueEx, HKEY_LOCAL_MACHINE

def getUserDir(userName):
    ssid = ConvertSidToStringSid(LookupAccountName(None, userName)[0])
    key = OpenKey(HKEY_LOCAL_MACHINE, r'SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\\' + ssid)
    return QueryValueEx(key, 'ProfileImagePath')[0]

rawDescription: What is the Windows equivalent of pwd.getpwnam(username).pw_dir?


code:
def recListProcessing(seq, threshold=0, min_len=1):
    len_seq = len(seq)
    if len_seq < min_len:
        return

    min_value = min(seq)
    if min_value > threshold:
        yield (min_value, len_seq, len_seq - 1)

    start = 0
    while start < len_seq:
        try:
            end = seq.index(min_value, start)
        except ValueError:
            end = len_seq
        sub_seq = seq[start:end]
        for item in recListProcessing(sub_seq, threshold, min_len):
            yield (item[0], item[1], item[2] + start)
        start = end + 1

rawDescription: Returning longest consecutive sequence of ranging integers


code:
# at module level (not inside class or function)
finish = False
def signal_handler(signal, frame):
    global finish
    finish = True

signal.signal(signal.SIGINT, signal_handler)

# wherever you have your file downloading code (same module)
for file_ in files_to_download:
    if finish:
        break
    urllib.urlretrieve("".join(baseurl, file_), os.path.join(".", file_))

rawDescription: Delay the keyboard interrupt in Python for an important part of the program


code:
import csv

with open('data', 'r') as f:
    reader = csv.reader(f)

    for row in reader:
        if len(row) < 5:
            continue

        #process the data

rawDescription: python - skip top lines of CSV file

description: Python - skip top lines of CSV file


code:
for i,line in enumerate(input):
    line = clean2(line)
    if i in (1, 4):
        outfile.write(line)

rawDescription: Python read file and write lines according to their index number


code:
import theano
a=theano.tensor.matrix()
idxs=(a<0).nonzero()
new_a=theano.tensor.set_subtensor(a[idxs], 0)

rawDescription: What Is equivalent to a [a < 0] = 0 in Theano?


code:
send_thread = SendThread()  # Assuming this inherits from threading.Thread
send_thread.daemon = True  # This must be called before you call start()

rawDescription: Python: How to interrupt raw_input() in other thread


code:
class Foo():

    @property
    def bar(self):
        # slow calculation
        return somefloat

f = Foo()

f.bar  # returns somefloat but doesn't require the empty parentheses

rawDescription: Aliasing a class attribute to a function


code:
data = pd.read_csv('SUPERLARGEFILE.csv', index_col=0, encoding = "ISO-8859-1", low_memory=Fals, chunksize=10)

result = []
for chunk in data:  # get chunks of 10 rows each
   result.append(chunk.mean())
# do something with res e.g. res = DataFrame(res).to_csv("result.csv")

rawDescription: streamline processing of large files in pandas

description: Streamline processing of large files in pandas


code:
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
shellreturn = subprocess.check_output(["C:\Python34\python", root.wgetdir + "\html2text.py", keyworddir + "\\" + item], startupinfo=startupinfo) #this could be any subprocess.

rawDescription: Python Subprocess causing latency with shell


code:
NUM_OF_PROCESSES = multiprocessing.cpu_count()
TIME_OUT_IN_SECONDS = 60

for dir_name in directories:
    my_queue.put(dir_name)

# creates processes that equals to number of CPU
processes = [multiprocessing.Process(target=transfer_directory, args=(my_queue,)) for x in range(NUM_OF_PROCESSES)]

# starts processes
for p in processes:
    p.start()

# blocks the calling thread
for p in processes:
    p.join()

def transfer_directory(my_queue):
    """processes element of directory queue if queue is not empty"""
    while my_queue is not empty:
        dir_name = my_queue.get(timeout=TIME_OUT_IN_SECONDS)
        src_dir = os.path.join(top_level,dir_name)
        dst_dir = src_dir.replace(args.src_dir,args.target_dir)

rawDescription: Python - Multiprocessing - More jobs that cpus.


code:
# set wants to master
def wantmaster(haves, wants):
  global repo
  return { "refs/heads/master": repo.refs["HEAD"] }

client, src = dulwich.client.get_transport_and_path(origin_uri)

client.send_pack(src, wantmaster, repo.object_store.generate_pack_contents)

rawDescription: How to add something to the index, commit it, then push the master branch to a named remote with dulwich?

description: Add something to the index, commit it, then push the master branch to a named remote with dulwich


code: sorted(your_list, lambda x: int(x.split()[-1]))

rawDescription: Sorting by substring in string Python

description: Sorting by substring in string Python


code:
with open('results.csv', 'wb') as csvfile:
    fieldnames = ['Name', 'Address', '#Reviews']
    writer = csv.writer(csvfile)
    writer.writerow(fieldnames)
    writer.writerows(z) # z is a list of all rows to write

rawDescription: Python: Cleanest way to write tuples as rows of a DictWriter?


code:
for stuff in listofstrings:
    dictionary["key"] = dictionary.get("key",defaultvalueforkey) + stuff

rawDescription: Cocatenating strings in place in a dictionary


code:
def isAnagram(str1, str2):
    str1_list = list(str1)
    str1_list.sort()
    str2_list = list(str2)
    str2_list.sort()

    return (str1_list == str2_list)

rawDescription: Find anagrams for a list of words

description: Find anagrams for a list of words


code:
import requests
import os

def twitter_api():
    access_token = config.get('twitter_credentials', 'access_token')
    access_token_secret = config.get('twitter_credentials', 'access_token_secret')
    consumer_key = config.get('twitter_credentials', 'consumer_key')
    consumer_secret = config.get('twitter_credentials', 'consumer_secret')

    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = API(auth)
    return api

def tweet_image(url, message):
    api = twitter_api()
    filename = 'temp.jpg'
    request = requests.get(url, stream=True)
    if request.status_code == 200:
        with open(filename, 'wb') as image:
            for chunk in request:
                image.write(chunk)

        api.update_with_media(filename, status=message)
        os.remove(filename)
    else:
        print("Unable to download image")

url = "http://animalia-life.com/data_images/bird/bird1.jpg"
message = "Nice one"
tweet_image(url)

rawDescription: How to update twitter status with image using image url in tweepy?

description: Update twitter status with image using image url in tweepy


rawDescription: How to sort arrival and departure times in python

description: Sort arrival and departure times in python


rawDescription: Convert python byte string to numpy int?

description: Convert python byte string to numpy int


code:
import subprocess
proc = subprocess.Popen(['file.jar'], cwd = '/path/to/file')

rawDescription: How to execute a file that requires being in the same directory?

description: Execute a file that requires being in the same directory


code:
import struct
import datetime

def convert(timestamp):
    return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')

def binary_to_int(b):
    return struct.unpack('<i', b)[0] # read string as little endian integer

sampledata = [26, 231, 215, 85]
binarydata = struct.pack('<BBBB', *sampledata)
timestamp = binary_to_int(binarydata)
result = convert(timestamp)

rawDescription: How to convert little endian float binary to time format

description: Convert little endian float binary to time format


code:
binned_time = df.groupby(pd.cut(df.speed, bins=[0,20,40,60,80]))['time']
binned_time.sum()

rawDescription: Pandas timeseries, binning on one columnn and accumulate time spent in the bins


code:
class MyAwesomeThread(Thread):
    """
    Threading wrapper to handle counting and processing of tasks
    """
    def __init__(self, session, q):
        self.q = q
        self.count = 0
        self.session = session
        self.response = None
        Thread.__init__(self)

    def run(self):
        """TASK RUN BY THREADING"""
        while True:
            url, host = self.q.get()
            httpHeaders = {'Host' : host}
            self.response = session.get(url, headers=httpHeaders)
            # handle response here
            self.count+= 1
            self.q.task_done()
        return

q=Queue()
threads = []
for i in range(CONCURRENT):
    session = requests.session()
    t=MyAwesomeThread(session,q)
    t.daemon=True # allows us to send an interrupt
    threads.append(t)

## build urls and add them to the Queue
for url in buildurls():
    q.put_nowait((url,host))

## start the threads
for t in threads:
    t.start()

rawDescription: grequests pool with multiple request.session?

description: Grequests pool with multiple request.session


code:
class franchise(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255)

    def __str__(self):  # __unicode__ for py2
        return self.name

class ItemAdmin(admin.ModelAdmin):
    list_display = ('title', 'franchise', 'description')

rawDescription: Can Django ModelAdmin display foreign key fields in the "add" and "change" pages?


code:
import asyncio

def run_once(loop):
    loop.stop()
    loop.run_forever()

loop = asyncio.get_event_loop()

for i in range(100):
    print('Iteration', i)
    run_once(loop)

rawDescription: Is it possible to run only a single step of the asyncio event loop


code: self.text.get("1.0", "end-1c")

rawDescription: Better way to dump the contents of ScrolledText widget


code: myString += chr(24)

rawDescription: Appending invisible ASCII characters to string


code:
root = Node()
child = Node()
child.parent=root
root.children = [child]

rawDescription: When would an informative __repr__ be dangerous?


code:
num = line[1:2]
desc = line[5:25]
status = line[25:54]
remain = line[54:58]
lifetime = line[60:68]
lba = line[77:99]

rawDescription: Python - How to parse smartctl program output?


code:
import datetime
datetime.datetime.strptime(u"2015-02-17T15:06:23.869000", "%Y-%m-%dT%H:%M:%S.%f")

rawDescription: how to transfer unicode to datetime in python?

description: Transfer unicode to datetime in python


code:
from glob import glob
sql_files = glob('../sql/*/*')  # No files in ../sql/ root
doc_files = glob('../doc/*') + glob('../doc/*/*')
buildOptions = dict(include_files=list(zip(sql_files, sql_files))\
                                  + list(zip(doc_files, doc_files)))

rawDescription: How to exclude gitignore files from cx_Freeze distribution

description: Exclude gitignore files from cx_Freeze distribution


rawDescription: Python: How do I approximate a float when I convert to string?


code:
for line in os.popen("Fping x.x.x.x x.x.x.x -l"):
    ipList.append(line)
    print(line)

rawDescription: subprocess output to stdout and to PIPE

description: Subprocess output to stdout and to PIPE


code:
def rand_parts(seq, n, l):
    indices = xrange(len(seq) - (l - 1) * n)
    result = []
    offset = 0
    for i in sorted(random.sample(indices, n)):
        i += offset
        result.append(seq[i:i+l])
        offset += l - 1
    return result

rawDescription: n random, contiguous and non-overlapping subsequences each of length

description: N random, contiguous and non-overlapping subsequences each of length


code: return zip(dates, values)

rawDescription: Will these data ever become mismatched through processing?


code:
import os

def LongComputation(sample):
    # do some stuff that results in a value
    return value

def main():
    Info = {}
    for sample in os.listdir("."):
        Info[sample] = LongComputation(sample)

main()

rawDescription: Global vs local instances in Python

description: Global vs local instances in Python


rawDescription: Python variable can not be same name as function it is calling like it is possible in PHP?


code:
from mock import patch, Mock
from abmodule import A,  B #Where A and B are

@patch("abmodule.B")
def test_A(bmock):
    # Set B costructor to return a Mock object
    bmock.return_value = Mock()
    bmock.upload.return_value='Hi'
    a = A()
    a.send()
    #Check if upload was called!
    bmock.return_value.upload.assert_called_with()

rawDescription: Mock a class with tedious __init__

description: Mock a class with tedious __init__


code: item = db.get(db.Key.from_path('Notes', id))

rawDescription: select rows in appengine database based on key ids

description: Select rows in appengine database based on key ids


code:
y = F1(x[0])   # F1(x[0]) is calculated, then y is bound to it
z[j] = F2(y)
y = F1(x[1])   # y is still bound to F1(x[0]) while F1(x[1]) is computed
               # The memory for F1(X[0]) is finally freed when y is rebound
z[j] = F2(y)

rawDescription: Static memory in python: do loops create new instances of variables in memory?


rawDescription: Concatenating nested tuples

description: Concatenating nested tuples


code:
x=9**2

xmlTemplate="""
<features>
<name>{name}</name>
<age>{age}</age>
<dob>{dob}</dob>
</features>""".format(name='Michael', age=x, dob='20/10/1925')

rawDescription: How to insert the output of a code into an xml tag placeholder (python)

description: Insert the output of a code into an xml tag placeholder (python)


code:
for match in matches:
    first_names.append(match[0])
    last_names.append(match[1])

rawDescription: Python pattern matching with regex, findall and groups


rawDescription: killing a thread in python

description: Killing a thread in python


code: count = sum(kmerCounts.get(x, 0) for x in block.get_kmers())

rawDescription: Finding intersections of huge sets with huge dicts


code:
from datetime import datetime
mytime= datetime.now()
s= mytime.strftime("%Y-%b-%d %H:%M:%S.%f")
print(s)
'2014-Aug-23 08:51:32.911271'

rawDescription: Convert time to string with milliseconds


code:
from django.test import TestCase
from sys import modules

# The list of all objects against which the tests have to be performed
formids = [12,124,234]
# get_formulas returns a list of formulas I have to test independently, linked to a formid
formulas = {id: get_formulas(id) for id in formids}

current_module = sys.modules(__name__)

def test_formula_method(self, formula):
    # Does some assertions
    self.assertNotEqual(formula.id, 0)

for formid in formids:
    attrs = {'formid': formid}
    for f in formulas[formid]:
        # f=f so the 2nd arg to test_formula_method is staying local
        # and not overwritten by last one in loop
        attrs['test_formula_%s' % f.name] = lambda self, f=f: test_formula_method(self, f)

    klass_name = "TestForm%s" % formid
    klass = type(klass_name, (TestCase,), attrs)

    setattr(current_module, klass_name, klass)

rawDescription: Django tests - print all test failures for a series of n times the same test


code:
from Tkinter import *

class AutoScrollListBox_demo:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.listbox_log = Listbox(frame, height=4)
        self.scrollbar_log = Scrollbar(frame)

        self.scrollbar_log.pack(side=RIGHT, fill=Y)
        self.listbox_log.pack(side=LEFT,fill=Y)

        self.listbox_log.configure(yscrollcommand = self.scrollbar_log.set)
        self.scrollbar_log.configure(command = self.listbox_log.yview)

        b = Button(text="Add", command=self.onAdd)
        b.pack()

        #Just to show unique items in the list
        self.item_num = 0

    def onAdd(self):
        self.listbox_log.insert(END, "test %s" %(str(self.item_num)))       #Insert a new item at the end of the list

        self.listbox_log.select_clear(self.listbox_log.size() - 2)   #Clear the current selected item
        self.listbox_log.select_set(END)                             #Select the new item
        self.listbox_log.yview(END)                                  #Set the scrollbar to the end of the listbox

        self.item_num += 1

root = Tk()
all = AutoScrollListBox_demo(root)
root.title('AutoScroll ListBox Demo')
root.mainloop()

rawDescription: How to add Autoscroll on insert in Tkinter Listbox?

description: Add Autoscroll on insert in Tkinter Listbox


code:
import sqlite3

connection = sqlite3.connect("table.sqlite")
cursor = connection.cursor()

source_table = 'table1'
target_table = 'test_table1'

stmt = "SELECT * FROM %s" % source_table

cursor.execute(stmt)
data = cursor.fetchall()

fields = ','.join('?' for desc in cursor.description)
stmt = "insert into {} values ({})".format(target_table, fields)
cursor.executemany(stmt, data)
connection.commit()
connection.close()

rawDescription: python sqlite3, inserting row into table using data from another table

description: Python sqlite3, inserting row into table using data from another table


code:
items = []
for index in xrange(self.ui.QListWidget.count()):
     items.append(self.ui.QListWidget.item(index))
labels = [i.text() for i in items]

rawDescription: PyQt4: get list of all labels in QListWidget


code:
def main():
    def button_callback(button,a, byMouse,label):
        nonlocal date  # <--------------
        date = date - oneday
        ...

    oneday = datetime.timedelta(1)
    date = datetime.date.today()

rawDescription: Is there a way to modify datetime object in python?


rawDescription: Plotting 3 cols of pandas dataframe as heatmap


rawDescription: Inverting numpy array image which might be uint8, uint16,


code:
def parallelizer_task(processor,input,callback):
    output = processor(input)
    if callback:
        callback(output)

    # Attach result to current thread
    thread = threading.currentThread()
    thread.result = output

class ThreadsParallelizer(Parallelizer):

    def parallelize(self,processors,input=None,callback=None):
        threads = []
        for processor in processors:
            t = threading.Thread(...)
            threads.append(t)
            t.start()

        # wait for threads to finish
        for th in threads:
            th.join()

        # do something with results
        results = [th.result for th in threads]
        return results

rawDescription: python threading issue

description: Python threading issue


code:
n = pynotify.Notification('title', 'content', 'icon')
n.props.summary
n.props.body
n.props.icon_name

rawDescription: How can I read the title, text and icon name from a pynotify.Notification?

description: Read the title, text and icon name from a pynotify.Notification


code:
from flask import Flask, render_template, send_file
from docx import Document
from cStringIO import StringIO

@app.route('/')
def index():
    document = Document()
    document.add_heading("Sample Press Release", 0)
    f = StringIO()
    document.save(f)
    length = f.tell()
    f.seek(0)
    return send_file(f, as_attachment=True, attachment_filename='report.doc')

rawDescription: Generating word docs with Flask?

description: Generating word docs with Flask


code:
#!/usr/bin/env python3
import sys
from subprocess import Popen, PIPE, STDOUT
from dialog import Dialog # $ pip install pythondialog

d = Dialog(dialog="dialog")
d.set_background_title("Random title")
with Popen([sys.executable or "python", "-u", "-c", r"""import sys, time

for i in range(100):
    print(i)
    if i % 10 == 0:
        sys.stderr.write('.\n')
    time.sleep(.1)
"""], stdout=PIPE, stderr=STDOUT, bufsize=0) as p:
    d.progressbox(fd=p.stdout.fileno(), text="youtube-dl output:")

rawDescription: How can I make sure that my Dialog.progressbox() continuously gives output?

description: Make sure that my Dialog.progressbox() continuously gives output


code:
import networkx as nx
from matplotlib import pyplot as plt
import math

G = nx.Graph()

row = '643715.202,2499149.0506 643752.61523545,2499089.86084203 ' +\
    '643773.6038,2499056.6558 643773.73878609,2499056.44011079 ' +\
    '643793.20162482,2499025.34111554 643813.55943268,2498992.81212045 ' +\
    '643826.6563,2498971.8852'

a=row.split(" ")
# Saving the previous node to be able to calculate the distance
prev_point = None
# Save the positions in a dictionary to be able to draw
# the nodes at the correct positions
pos = {}
for i in a:
    cur_point = tuple([float(x) for x in i.split(',')])
    assert len(cur_point) == 2
    if prev_point is not None:
        # Calculate the distance between the nodes with the Pythagorean
        # theorem
        b = cur_point[1] - prev_point[1]
        c = cur_point[0] - prev_point[0]
        a = math.sqrt(b ** 2 + c ** 2)
        G.add_edge(cur_point, prev_point, weight=a)
    G.add_node(cur_point)
    pos[cur_point] = cur_point
    prev_point = cur_point
nx.draw(G, pos=pos)
plt.savefig('roads.png')

rawDescription: Have to point two dimensional graph(x,y) co ordinates networkx python


code:
rbfi = Rbf(*data.T)
mesh = np.meshgrid(*[i['grid2'] for i in self.cambParams], indexing='ij')
chi2 = rbfi(*mesh)

rawDescription: SciPy: n-dimensional interpolation of sparse data


code:
import sys

class WriteLog:

    def __init__(self, output):
        self.output = output
        if output == 'stdout':
            self.logfile = sys.stdout
        elif output == 'stderr':
            self.logfile = sys.stderr
        else:
            self.logfile = open(output, 'a')

    def write(self, text):
        self.logfile.write(text)

    def close(self):
        if self.output != 'stdout' and self.output != 'stderr':
            self.logfile.close()

    def __del__(self):
        self.close()

if __name__ == '__main__':
    a = WriteLog('stdout')
    a.write('This goes to stdout\n')
    b = WriteLog('stderr')
    b.write('This goes to stderr\n')
    c = WriteLog('/tmp/logfile')
    c.write('This goes to /tmp/logfile\n')

rawDescription: Writing Python output to either screen or filename


code:
cell_list = worksheet.range('A1:A7')
cell_values = [1,2,3,4,5,6,7]

for i, val in enumerate(cell_values):  #gives us a tuple of an index and value
    cell_list[i].value = val    #use the index on cell_list and the val from cell_values

worksheet.update_cells(cell_list)

rawDescription: Python/gspread - how can I update multiple cells with DIFFERENT VALUES at once?


code: cursor.execute(QryString, matter)

rawDescription: Python -SQL String Escape Wildcards

description: Python -SQL String Escape Wildcards


code:
from itertools import permutations
combs = permutations(range(20,30), 9)
from copy import deepcopy
combs2 = deepcopy(combs)
next(combs)
next(combs2)

rawDescription: Copying a generator without blowing up memory


code:
from django.contrib.auth import authenticate, login
user = authenticate(username = username, password = password)
if user is not None:
    login(request, user)

rawDescription: How can I programmatically authenticate a user in Django?

description: Programmatically authenticate a user in Django


code:
sys.path.insert(0, '/home/mscarpa/PhpstormProjects/quasar-node/quasar-node/quasar-node/src/lib')

import configuration
import server

rawDescription: Python calling a file from another directory


code:
G=nx.random_geometric_graph(5,0.5)
M=nx.to_scipy_sparse_matrix(G, nodelist=G.nodes(), dtype=float)
M.todense()
S = scipy.array(M.sum(axis=1)).flatten()
S[S != 0] = 1.0 / S[S != 0]
Q = scipy.sparse.spdiags(S.T, 0, *M.shape, format='csr')
(Q*M).todense()

rawDescription: Normalizing matrix row scipy matrix

description: Normalizing matrix row scipy matrix


code:
file = open("classj.txt")
classj = (file.readlines())
s = []
for line in sorted(classj):
    classj = (line.rstrip())
    classa = (classj.split("-"))
    score = int(classa[1])
    name = (classa[0])
    s.append( (name,score) )
s.sort(reverse=True, key=lambda x:x[1])
for x in s:
    print(x[0],"-",x[1])

rawDescription: Sorting imported text file?

description: Sorting imported text file


code:
import simplejson

class StreamManager(models.Manager):
    def all_serialized(self):
        qset = super(StreamManager, self).get_query_set()
        return simplejson.dumps(
               [{ 'id':item.id,
                  'name':item.name,
                  'network':item.network.name,
                  'score':item.score,
                  'custom_url':item.custom_url,}
                  for item in qset])

rawDescription: How do I customize the python side of my Django models?

description: Customize the python side of my Django models


code:
def update_lines(num, data1, data2, line1, line2):
    line1.set_data(data1[...,:num])
    line2.set_data(data2[...,:num])
    return line1,line2

data1 = np.random.rand(2, 25)
data2 = np.random.rand(2, 25)

fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax2 = fig1.add_subplot(122)
l1, = ax1.plot([], [], 'r-')
l2, = ax2.plot([], [], 'g-')
for ax in (ax1, ax2):
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_xlabel('x')
    ax.set_title('test')
line_ani = animation.FuncAnimation(fig1, update_lines, 25, fargs=(data1, data2, l1, l2),interval=50, blit=False)

rawDescription: matplotlib animation multiple datasets

description: Matplotlib animation multiple datasets


code:
#! /usr/bin/python
import numpy as np

with open('Acetobacterium_woodii_DSM_1030.mdistr', 'r') as f:
    # read file
    lines = f.read().splitlines()

values = []
weights = []

for line in lines:
    if line.startswith("-"):
        # print statistics and reset variables
        print ("Min: %d, Max: %d, Avg: %d" %(min(values),max(values),
                                             np.average(values,
                                                        weights=weights)))
        values = []
        weights = []
    elif line.startswith("#"):
        row = line.split()
        values.append(int(row[1]))
        weights.append(int(row[2]))

# print statistics before quitting
print ("Min: %d, Max: %d, Avg: %d" %(min(values),max(values),
                                     np.average(values,
                                                weights=weights)))

rawDescription: Weighted mean with Numpy in file txt

description: Weighted mean with Numpy in file txt


code:
df.loc[df.Diff < 0, 'Diff'] = 365 + df.Day - df.shift().loc[df.Diff < 0, 'Day']
df

rawDescription: Subtraction/Addition from seperate rows/columns


code:
import html5lib
from html5lib import treebuilders
parser = html5lib.HTMLParser(tree=treebuilders.getTreeBuilder("simpleTree"))
with open('test.html') as test:
    doc = parser.parse(test)
    for child in doc:
        if child.parent.name == "body":
            return child.toxml()

rawDescription: html5lib. How to get valid html without adding html, head and body tags?

description: Html5lib. How to get valid html without adding html, head and body tags


code:
nameArr = np.array(['josh','is','a','person'])
[np.where(nameArr==i) for i in ['a','is']]

rawDescription: Python/numpy: Get array locations of a list of values


code:
g_data = soup.find_all("div", {"class":"podaci"})
for i in g_data:
 print(type(some))
print(type(some.encode('utf-8', 'ignore')))

rawDescription: Python - Cant make it to encode string properly


code:
routes_in = (('/report', '/reporter/reporter/index'),)
routes_out = (('/reporter/reporter/index', '/report'),)

rawDescription: web2py routes.py not working as shown in book

description: Web2py routes.py not working as shown in book


code:
def fetchpnum(self,searchable):
        # print self.doc1.Paragraphs.Count
        for i in range(1, self.doc1.Paragraphs.Count):
            value = self.doc1.Paragraphs(i).Range.Text.lower()
            if value.startswith(searchable.lower()):
                return i
        return 0

rawDescription: ms word-2003 Search and Replace text

description: Ms word-2003 Search and Replace text