code:
with open('input.txt') as input:
    for line in input:
        name, data = line.split(' ', 1)

        with open('{0}.txt'.format(name), 'a') as f:
            f.write(data)

rawDescription: One input file to multiple output files

description: One input file to multiple output files


rawDescription: python list comprehension: creating 2d array

description: Python list comprehension: creating 2d array


code:
for fieldname in a.dtype.names:
    ind = a[fieldname] == ''
    a[fieldname][ind] = '54321'

rawDescription: numpy: Replacing values in a recarray

description: Numpy: Replacing values in a recarray


code:
import os
rootdir = 'input'
extensions = ('.mp4', '.avi', '.wmv')

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        ext = os.path.splitext(file)[-1].lower()
        if ext in extensions:
            print (os.path.join(subdir, file))

rawDescription: Python - Loop through files of certain extensions


code:
def handler(*args):
    sys.exit(1)

signal.signal(signal.SIGALRM, handler)
signal.alarm(60*10)

# Now start the wsgi application and wait
httpd = make_server('', 80, application)
httpd.serve_forever()

rawDescription: How to exit program but a function is still running?

description: Exit program but a function is still running


code:
def get_media_requests(self, item, info):
    if isinstance(item, ExampleMedia):
        request = Request("http://example.com/medialibrary/"+str(item['mediaID']))
        request.meta['item'] = item
        request.headers['fpBuster']=item['uniqueID']
        yield request

rawDescription: Scrapy - allow media pipeline to download already downloaded urls


code:
def getLoc(matrix, elem):
    row , col = 0 , 0
    for a in [j for i in matrix for j in i]:
        if a == elem:
            return row / len(matrix), col % len(matrix)
        col += 1
        row += 1

rawDescription: Python row, column, matrix trouble

description: Python row, column, matrix trouble


code:
import ScriptingBridge
f = ScriptingBridge.SBApplication.applicationWithBundleIdentifier_("com.apple.Finder")

rawDescription: Copying files with native file manager in python


code:
"{:^{}}".format("Title", width)
#^---------------^ first argument goes with first brace
#   ^--------------------^ second argument goes with second brace and sets the width

rawDescription: Formatting Strings in Python

description: Formatting Strings in Python


code:
import random

functions = [f1, f2, f3]
random.shuffle(functions)
for function in functions:
    function(mydict)

rawDescription: Apply a set of functions randomly on a python dictionary with random.shuffle()


code:
sc = SparkContext(appName="Json data preprocessor")
distData = sc.textFile(sys.argv[2]).coalesce(10)     #10 partition tasks
json_extractor = JsonExtractor(sys.argv[1])
cleanedData = distData.flatMap(json_extractor.extract_json)
cleanedData.saveAsTextFile(sys.argv[3])

rawDescription: Spark speed performance

description: Spark speed performance


code:
self.proxies = get_proxies() #list of proxies
proxy_address = random.choice(self.proxies)
request.meta['proxy'] = proxy_address
request.meta['download_slot'] = hash(proxy_address) % MAX_CONCURRENT_REQUESTS

rawDescription: Throttling requests with multiple proxies


code:
import csv

students = {}

with open(inputcsvfile, 'rb') as scoressource:
    reader = csv.reader(scoressource)
    for name, score in reader:
        if score < students.get(name, 'Z'):
            students[name] = score

with open(outputcsvfile, 'wb') as scoresdest:
    writer = csv.writer(scoresdest)
    for name, score in students.iteritems():
        writer.writerow([name, score])

rawDescription: Python: how to group rows by one column and pick one row by another column?


code:
from collections import OrderedDict, Counter
class OrderedCounter(Counter, OrderedDict):
    pass
oc = OrderedCounter()
oc[0] = 5
oc[2] = 27
oc
oc.update({2:1, 0:6, 7:1})
oc
oc.popitem()
oc.popitem()
oc.popitem()

rawDescription: Python: collections.Counter with deque-style ordering


code:
import Tkinter as tk
import time

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.lb = tk.Listbox(self)
        self.lb.pack(fill="both", expand=True)

        self.lb.insert("end", "item 1","the current time", "item 3")

        self.after(1000, self._update_listbox)

    def _update_listbox(self):
        self.lb.delete(1)
        self.lb.insert(1, time.asctime())

        self.after(1000, self._update_listbox)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: How can I change the text of Listbox item?

description: Change the text of Listbox item


code:
from itertools import chain

def custom_dictmerge(*args):
    result = {}
    keys = set(chain(*(d.keys() for d in args)))
    for k in keys:
        result[k] = tuple(d[k] for d in args if k in d)
        if len(result[k]) == 1:
            result[k] = result[k][0]
    return result

rawDescription: Adding dictionaries

description: Adding dictionaries


code:
import re
regex = re.compile('\.jpg$|\.gif$|\.png$', re.IGNORECASE)
url_list = ['http://www.example.com/example.png', 'http://www.example.com/example']
urls_without_images = filter(lambda url: not regex.search(url), url_list)

rawDescription: Removing image links from a list

description: Removing image links from a list


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)

plt.figure()
plt.ylim([-2,2])
plt.xlim([-0.5,0.5])

lambda_x = np.linspace(-0.5,0.5,1000)
u = np.linspace(-2,2,1000)

X,Y = np.meshgrid(lambda_x,u)
# Drawing f=0 lines for implicit function f(u;lambda) = lambda*u  + u**3 - u**5
f1 = X  + Y**2 - Y**4
f2 = Y

sigma = X + 3*Y**2 - 5*Y**4

unstable = sigma > 0
stable = sigma < 0

#for i in range(0,10):
    #f1[i] = f1[i][stable[i]]

plt.contour(np.ma.masked_where(unstable, X),np.ma.masked_where(unstable, Y), np.ma.masked_where(unstable, f1), levels = [0],colors = ('r'),linewidths = 4,extend='both')
plt.contour(np.ma.masked_where(stable, X),np.ma.masked_where(stable, Y), np.ma.masked_where(stable, f1), levels = [0],colors = ('b'),linewidths = 4,linestyles = 'dashed',extend='both')
plt.contour(np.ma.masked_where(unstable, X),np.ma.masked_where(unstable, Y), np.ma.masked_where(unstable, f2), levels = [0],colors = ('r'),linewidths = 4,extend='both')
plt.contour(np.ma.masked_where(stable, X),np.ma.masked_where(stable, Y), np.ma.masked_where(stable, f2), levels = [0],colors = ('b'),linewidths = 4,linestyles = 'dashed',extend='both')

plt.show()

rawDescription: Matplotlib challenge - changing line style in contour map


code:
from sys import exit

def start():
    print("You are in an old temple.")
    print("There is a door to your right and left or you can walk forwad.")
    print("Which one do you take?")

    choice = input("> ")

    if choice == "left":
        gold_room()
    elif choice == "right":
        trap_room()
    elif choice == "forward":
        monster_room()
    else:
        dead("you got caught by the ancient gods and you must be killed.")

def monster_room():
    print("you're in a room with a monster. what you gonna do?")

    choice = input("> ")
    if "left" in choice:
        print("you are going to the gold room")
        gold_room()
    elif "right" in choice:
        print("you are going to the trap room")
        trap_room()
    else:
        dead("couldn't understand what did you say so you are dead!")

def gold_room():
    print("you chose the left room. now you are in a room with a pot of gold!")
    print("you can take the pot.")
    print("or you can just rob the money in it.")
    print("or you go go to other rooms.")

    choice = input("> ")

    if choice == "take the pot":
        print("you are a millionaire from now on!!!")
    elif choice == "rob the money":
        dead("you will never rest in piece!")
    elif choice == "another room":
        monster_room()

def trap_room():
    print("you are now in a trap room.")
    print("there is a hidden trap in this room.")
    print("be careful!")
    print("you can go back to the  monster room")
    print("or you can find the trap")

    choice = input("> ")

    if "find" in choice:
        start()
    elif "back" in choice:
        gold_room()

def dead(why):
    print(why, "rekt!")
    exit(0)

start()

rawDescription: Building a mini text-based python game as homework of learn python the hard way


code:
...
# basename is actually set to "circle" ("arc" is commented out) in that example.
# this saves the figure to "circle_residu2.png" or "circle_residu1.png"
p.savefig('%s_residu%d.png' % (basename, 2 if residu2 else 1))

rawDescription: What are 'r_[]' and "basename = 'arc'" lines in Python code


rawDescription: Python List Division/Splitting

description: Python List Division/Splitting


code:
import timeit as t

def fib_recursive(n):
    if n==0:
        return 0
    elif n == 1:
        return 1
    else:
        return fib_recursive(n-1) + fib_recursive(n-2)

setup = 'from __main__ import fib_recursive'
t.timeit('fib_recursive(29)', setup=setup)

rawDescription: How do you use timeit to time how long it takes a function to run?

description: Use timeit to time how long it takes a function to run


code:
def setglobal(s, x):
    globals()[s] = x

rawDescription: Set a global variable with eval or exec in Python


code:
def flatten(l):
  if isinstance(l, list):
     return sum(map(flatten,l),[])
  else:
     return [l]

rawDescription: An ALGORITHM to create one list from "X" nested lists in Python


rawDescription: Checking Item List

description: Checking Item List


code:
def get_my_class(name):
    return type(name, (db.Expando,), {})

rawDescription: App Engine - Specify entity name at runtime


code:
def OnWaveletSelfAdded(event, wavelet):
  for id in wavelet.blips:
    blip = wavelet.blips[id]
    logging.debug(blip.text)

rawDescription: How do I get contents of a Google Wave given the wave id and wavelet id?

description: Get contents of a Google Wave given the wave id and wavelet id


rawDescription: Item assignment to Python dask array objects


code:
class MyTest(unittest.TestCase):
    def __init__(self, *args, **kwargs):
         super(MyTest, self).__init__(*args, **kwargs)
         settings.TESTING = True

rawDescription: Django: Define a form differently when in testing mode


code: a = vstack([ gen() for _ in xrange(nSamples) ])

rawDescription: Pythonic way of creating 2D numpy array

description: Pythonic way of creating 2D numpy array


code:
#Import libraries
import urllib2
from bs4 import BeautifulSoup
import datetime

#create soup
soup = BeautifulSoup(urllib2.urlopen('https://www.theice.com/marketdata/DelayedMarkets.shtml?productId=3418&hubId=4080').read())
table = soup.find('table', {"class":"data default borderless"})

#Find and record time
time_idx = -1
for idx, th in enumerate(table.find_all('th')):
    # Find the column index of Time
    if th.get_text() == 'Time':
        time_idx = idx
        break

timevar = []
for tr in table.find_all('tr'):
    # Extract the content of each column in a list
    td_contents = [td.get_text() for td in tr.find_all('td')]
    # If this row matches our requirement, take the Time column
    if 'Dec13' in td_contents:
        time_str = td_contents[time_idx]
        # This will capture Thu Dec 05 16:26:24 EST 2013 GMT, convert to datetime object
        time_obj = datetime.datetime.strptime(time_str,'%a %b %d %H:%M:%S EST %Y GMT')
        timevar.append(datetime.datetime.strftime(time_obj,'%x'))

#create output document
with open('CarbonPrice.txt','a') as f:
    f.write(timevar[0])

rawDescription: How do I change the date format of the html I'm scraping?

description: Change the date format of the html I'm scraping


code:
for i in range(N):
    for j in range(N):
        distance_matrix[i,j] = cosine_distance(tf_idf[i,:], tf_idf[j,:])

rawDescription: Cosine similarity between item descriptions using scikit-learn


code:
import Tkinter as tk
from random import randrange

class Example(tk.Frame):
    def __init__(self, master):
        tk.Frame.__init__(self, master)

        self.DrawArea = tk.Canvas(self, width=1000, height=600, background="black",
                                  borderwidth=0, highlightthickness=0)
        self.scale = tk.Canvas(self, width=30, height=600, background="black",
                               borderwidth=0, highlightthickness=0)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.DrawArea.xview)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.DrawArea.yview)
        self.DrawArea.configure(yscrollcommand=self.vsb.set, xscrollcommand=self.hsb.set)

        self.DrawArea.grid(row=0, column=0, sticky="nsew")
        self.scale.grid(row=0, column=1, sticky="nsew")
        self.vsb.grid(row=0, column=2, sticky="ns")
        self.hsb.grid(row=1, column=0, columnspan=2,sticky="ew")
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

        self.draw_scale()
        self.draw_points()

    def draw_scale(self):
        value = 5.0
        for y in range(25, 600, 25):
            self.scale.create_text((25, y), text=str(value), fill="white", anchor="ne")
            value -= 0.5

    def draw_points(self):
        import math
        for x in range(5,10000):
            y = randrange(600)
            color = "green" if (300 > y > 200) else "red"
            self.DrawArea.create_rectangle(x-2,y-2,x+2,y+2, fill=color)
        self.DrawArea.configure(scrollregion = self.DrawArea.bbox("all"))

root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()

rawDescription: Python tkinter scrollbar/graph scale slowing down scrolling


code:
for filename in glob.glob('xyz*'):
    os.remove(filename)

rawDescription: python delete file if name begins with 'xyz'

description: Python delete file if name begins with 'xyz'


code:
import os
import shutil

BASE = '.'
for root, dirs, files in os.walk(BASE):
    n = 0
    head = root
    while head and head != BASE:
        head, _ = os.path.split(head)
        n += 1
    if n == 3:
        for dir in dirs:
            shutil.rmtree(os.path.join(root, dir))
        del dirs[:]      # clear dirs so os.walk() doesn't look for subdirectories

rawDescription: Recursively delete folders below 3rd level in large structure of folders


code:
def health_calculator(age, apples, cigs_smoked):
    answer=(100-age)+(apples*2.5)-(cigs_smoked*2)
    print(answer)
fjonis_data=[15, 7, 0]
health_calculator(*fjonis_data)

rawDescription: Console can't print out my code

description: Console can't print out my code


code:
addrinfo = getaddrinfo('fe80::225:ff:fecd:5aa0%en0', 2001, AF_INET6, SOCK_STREAM)
addrinfo
(family, socktype, proto, canonname, sockaddr) = addrinfo[0]
s = socket(family, socktype, proto)
s.connect(sockaddr)

rawDescription: Why doesn't a %en0 suffix work to connect a link-local IPv6 TCP socket in Python?


rawDescription: multivariate linear regression inputs fitting

description: Multivariate linear regression inputs fitting


code:
from urllib.request import urlopen
import json
import csv

url_fl = 'http://company.server.org'

url_response = urlopen(url_fl).read().decode('utf-8')
obj = json.loads(url_response)
obj_parse = obj['aaData']

with open('test.csv', 'w', newline='') as fp:
    data = csv.writer(fp, delimiter=',')
    for row in obj_parse:
        data.writerows([row[1:]])

rawDescription: Reading JSON file in Python - doesn't look to be in standard format - how to parse it?


code:
import os

def walklevel(some_dir, level=1):
    some_dir = some_dir.rstrip(os.path.sep)
    assert os.path.isdir(some_dir)
    num_sep = some_dir.count(os.path.sep)
    for root, dirs, files in os.walk(some_dir):
        yield root, dirs, files
        num_sep_this = root.count(os.path.sep)
        if num_sep + level <= num_sep_this:
            del dirs[:]

rawDescription: os.walk without digging into directories below

description: Os.walk without digging into directories below


code: range(1,int(raw_input('Number: '))+1)

rawDescription: In python, how do I make a raw_input of a list from one number?


code:
a = Decimal(-0.00001)
a = a.quantize(Decimal('.01'),rounding=ROUND_HALF_UP)
if a == 0:
    a = abs(a)

rawDescription: How to convert -0.00 to 0.00 in Python 3

description: Convert -0.00 to 0.00 in Python 3


code:
import matplotlib.pyplot as plt
f, ax = plt.subplots(1)
xdata = [1, 4, 8]
ydata = [10, 20, 30]
ax.plot(xdata, ydata)
ax.set_ylim(ymin=0)
plt.show(f)

rawDescription: Change y range to start from 0 with matplotlib


code: np.mean(np.atleast_2d(myarray), axis=0)

rawDescription: Preserve vertical dimension with numpy.mean()


code:
import sys
import zmq
from multiprocessing import Process, Queue, Value

def server_task():
    context = zmq.Context.instance()

    server = context.socket(zmq.ROUTER)

    server.bind("tcp://127.0.0.1:7777")

    timeout_queue = Queue()
    port_list = [ 1 ]

    proc_list = [ ]

    while True:
        try:
            id = server.recv_multipart()[0]

            # Get an unused port from the list
            # Ports from clients that have timed out are recycled here

            while not timeout_queue.empty():
                port_list.append(timeout_queue.get())

            port = port_list.pop()

            if len(port_list) == 0:
                port_list.append(port + 1)

            # Spawn a new worker task, binding the port to a socket

            proc_running = Value("b", True)

            proc_list.append(proc_running)

            Process(target=worker_task, args=(proc_running, port, timeout_queue)).start()

            # Send the new port to the client

            server.send_multipart([id, str(7777 + port)])

        except KeyboardInterrupt:
            break

    # Safely allow our worker processes to terminate
    for proc_running in proc_list:
        proc_running.value = False

    context.term()

def worker_task(proc_running, port, timeout_queue):
    context = zmq.Context.instance()

    worker = context.socket(zmq.ROUTER)

    worker.setsockopt(zmq.RCVTIMEO, 5000)
    worker.bind("tcp://127.0.0.1:%d" % (7777 + port, ))

    while proc_running.value:
        try:
            id, data = worker.recv_multipart()

            worker.send_multipart([id, data])
        except zmq.Again:
            timeout_queue.put(port)

            context.term()

            break

    print("Client on port %d disconnected" % (7777 + port, ))

rawDescription: Zeromq (pyzmq) ROUTER procession of multiple clients' data and subsequent timeout handling


code:
from django.template import Template, Context
from django.conf import settings
settings.configure()
t = Template('My name is {{ my_name }}.')
c = Context({'my_name': 'Daryl Spitzer'})
t.render(c)

rawDescription: How do I use Django templates without the rest of Django?

description: Use Django templates without the rest of Django


code:
return Player.objects.filter(game_id=self.game_id, is_alive=True).order_by(
    Case(When(position__gt=self.position, then=Value(0)), default=Value(1)),
    'position'
)[:1].get()

rawDescription: Can this be reduced to a single query?

description: Can this be reduced to a single query


code: pattern = re.compile(r" ([a-zA-Z]+) \1 ")

rawDescription: Locating doubled words in a string

description: Locating doubled words in a string


code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import scipy.optimize as si
import scipy.optimize as so

fig = plt.figure()
ax = plt.axes(xlim=(-0.5, 3.5), ylim=(-10, 100))
line, = ax.plot([], [], 'o')

def F(x):
        return (x**3-x**2-9.)**2

#get the optimize progress
res_x = []
so.fmin(F, -9, callback=res_x.append)
res_x = np.array(res_x).ravel()
res_y = F(res_x)

def init():
    line.set_data([], [])
    return line,

# animation function.  This is called sequentially
def animate(i):
    line.set_data(res_x[i], res_y[i])
    return line,

ax.plot(np.linspace(0,10,100), F(np.linspace(0,10,100)), 'g')
#frames is the length of res_x
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=len(res_x), interval=200, blit=True)

rawDescription: how to animate matplotlib function optimization?

description: Animate matplotlib function optimization


code:
import re
s = "A1B345FS"
s2=re.findall('..',s)
s3=":".join(s2)

print(s3)

rawDescription: separate a string 2 by 2 using Python3 macadress format

description: Separate a string 2 by 2 using Python3 macadress format


code:
from nltk.corpus import stopwords
my_stop_words = stopwords.words('english')

rawDescription: Extract non-content English language words string - python


code:
import subprocess

# Files to verify
crlfile = r"path\to\crlfile"
mycacert = r"path\to\mycacert"
# Set up args
args = ["openssl", "crl", "-inform", "DER", "-in", crlfile, "-CAfile", mycacert, "-noout"]
# Run the thing
output = subprocess.check_output(args)
verified = True if output.upper() == "VERIFY OK" else False

rawDescription: Python OpenSSL - Verifying a CRL File

description: Python OpenSSL - Verifying a CRL File


code:
from django.contrib.humanize.templatetags.humanize import intcomma

class ProductMaxIPPortEldCountMonthlyTable(tables.Table):
    ip_count = tables.Column()

    def render_ip_count(self, value):
        return intcomma(value)

rawDescription: Humanizing django-tables2 output?

description: Humanizing django-tables2 output


rawDescription: Writing text file with python returns odd result?


rawDescription: Using a hash function to give a memorable personality to objects


code:
from flask import send_file

@app.route("/<file_name>")
def getFile(file_name):
    return send_file(file_name, as_attachment=True)

rawDescription: Save file into another computer - Python


code:
import os
import types
import importlib
strs = '/path/to/myfle.py'
path, _ = os.path.splitext(strs)               #now path is '/path/to/myfile'
file_name = path.split('/')[-1]                # returns myfile
mod = importlib.import_module(file_name, path)
funcs = [x for x in dir(mod) if isinstance(getattr(mod,x), types.FunctionType)]
funcs

rawDescription: Python - if given file name using inspect to get list of methods in file


code:
import Tkinter

def OnKeyPress(event):
    value = event.widget.get()
    string="value of %s is '%s'" % (event.widget._name, value)
    status.configure(text=string)

root = Tkinter.Tk()

entry1 = Tkinter.Entry(root, name="entry1")
entry2 = Tkinter.Entry(root, name="entry2")
entry3 = Tkinter.Entry(root, name="entry3")

# Three different bindtags. The first is just the default but I'm
# including it for illustrative purposes. The second reverses the
# order of the first two tags. The third introduces a new tag after
# the class tag.
entry1.bindtags(('.entry1', 'Entry', '.', 'all'))
entry2.bindtags(('Entry', '.entry2', '.', 'all'))
entry3.bindtags(('.entry3','Entry','post-class-bindings', '.', 'all'))

btlabel1 = Tkinter.Label(text="bindtags: %s" % " ".join(entry1.bindtags()))
btlabel2 = Tkinter.Label(text="bindtags: %s" % " ".join(entry2.bindtags()))
btlabel3 = Tkinter.Label(text="bindtags: %s" % " ".join(entry3.bindtags()))
status = Tkinter.Label(anchor="w")

entry1.grid(row=0,column=0)
btlabel1.grid(row=0,column=1, padx=10, sticky="w")
entry2.grid(row=1,column=0)
btlabel2.grid(row=1,column=1, padx=10, sticky="w")
entry3.grid(row=2,column=0)
btlabel3.grid(row=2,column=1, padx=10)
status.grid(row=3, columnspan=2, sticky="w")

# normally you bind to the widget; in the third case we're binding
# to the new bindtag we've created
entry1.bind("<KeyPress>", OnKeyPress)
entry2.bind("<KeyPress>", OnKeyPress)
entry3.bind_class("post-class-bindings", "<KeyPress>", OnKeyPress)

root.mainloop()

rawDescription: How to bind self events in Tkinter Text widget after it will binded by Text widget?

description: Bind self events in Tkinter Text widget after it will binded by Text widget


code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as colors
import numpy.ma as ma

#See http://stackoverflow.com/questions/18926031/how-to-extract-a-subset-of-a-colormap-as-a-new-colormap-in-matplotlib
def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
    new_cmap = colors.LinearSegmentedColormap.from_list(
        'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
        cmap(np.linspace(minval, maxval, n)))
    return new_cmap

#truncate the colourmap
n_colours = 4
new_cmap = truncate_colormap(cm.get_cmap('spectral_r'), 0, 0.4, n=n_colours)

#discretise the colourmap
bounds = np.linspace(0,n_colors,n_colours+1)
norm = colors.BoundaryNorm(bounds, new_cmap.N)

#build array one
array1 = np.random.rand(10,10)

#build array two
array2 = np.random.randint(0,5,100).reshape(10,10)

#mask the array
array2 = ma.masked_array(array2, array2==0)

#plot it
plt.pcolormesh(array1,cmap = plt.cm.binary)
plt.pcolormesh(array2,cmap = new_cmap, norm=norm)
cbar = plt.colorbar()
plt.show()

rawDescription: Discretised color map with transparency in Matplotlib


code:
from time import sleep
from random import random

import matplotlib.pyplot as plt

import dispatch

plot_update = dispatch.Signal(providing_args=["y"])

plt.ion()

class DynamicPlot(object):
    def __init__(self, fid_name):
        self.fig = plt.figure(fid_name)
        plt.clf()
        self.ax = self.fig.add_subplot(111)
        self.line1, = self.ax.plot([1], [0], 'ko')
        self.ax.axis((0, 2, 0, 1))
        self.fig.canvas.draw()
        plt.show()

        def _update_callback(sender, **kwargs):
            self.line1.set_ydata(kwargs['y'])
            self.fig.canvas.draw()

        plot_update.connect(_update_callback, weak=False)
        # plot_update.connect(tmp(self))

if __name__ == "__main__":

    d1 = DynamicPlot("Not it Works!")

    while(True):
        plot_update.send_robust(sender=None, y=[random()])
        sleep(0.1)

rawDescription: Encapsulating matplotlib plot update in a class, when using dispatcher package


code: es_client = get_es(urls=['localhost:9200'], http_auth="username:password")

rawDescription: Python elasticutils http authentication or api key?


code: return all(c in lettersGuessed for c in secretWord)

rawDescription: Looping Inside a list in python

description: Looping Inside a list in python


code: links = [x.split('-cat-')[0] for x in response.split('-p-')]

rawDescription: issues with xrange and list iteration

description: Issues with xrange and list iteration


code:
from PySide import QtGui, QtCore
import time
import sys

class MyApp(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MyApp, self).__init__(parent)

        # Setup
        self.centralWidget = QtGui.QWidget(self)
        self.setCentralWidget(self.centralWidget)
        self.setup_UI()

        # Execute process!
        self.set_process()
        self.timer = QtCore.QTimer()
        self.i = 0
        self.timer.timeout.connect(self.update)
        self.timer.start(1000)

    def setup_UI(self):
        ''' Attach widgets to window '''
        self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
        self.list_widget = QtGui.QListWidget()
        self.progress_bar = QtGui.QProgressBar()
        self.mainLayout.addWidget(self.list_widget)
        self.mainLayout.addWidget(self.progress_bar)

    def set_process(self):
        ''' Manipulate the ui '''
        self.progress_bar.setMaximum(0)
        self.progress_bar.setMaximum(10)

    def update(self):
        if self.i > 9:
            self.timer.stop()

        self.list_widget.addItem('Item ' + str(self.i))
        self.progress_bar.setValue(self.i)
        self.i += 1

def main():
    app = QtGui.QApplication(sys.argv)
    my_win = MyApp()
    my_win.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

rawDescription: How to show PySide/PyQt UI and auto-run one of its methods?

description: Show PySide/PyQt UI and auto-run one of its methods


rawDescription: Get name of variable with highest integer from list Python 3.3.4


code: cfiles = [os.path.abspath(input_arg)+"/"+files for files in os.listdir(input_arg) if files.endswith(".c")]

rawDescription: Append file name to list comprehension

description: Append file name to list comprehension


code:
df = pd.read_csv("test_data2.csv", index_col=[0,1], skipinitialspace=True)
df

rawDescription: Load CSV to Pandas MultiIndex DataFrame

description: Load CSV to Pandas MultiIndex DataFrame


code:
class worker(threading.Thread):

    def __init__(self, timer):
        self.timer = timer
        super(worker, self).__init__()

    def run(self):
        while True:
            print("job %s" % self.timer)
            time.sleep(10)

if __name__ == "__main__":

    mydict = {1:150, 2:250, 3:350}

    for values in mydict.keys():
        print(values)
        new_worker_thread = worker(values)
        new_worker_thread.start()

rawDescription: Time specific threading

description: Time specific threading


rawDescription: Combine lists row-wise (Python)

description: Combine lists row-wise (Python)


code:
import itertools
b = list(itertools.izip_longest(fillvalue='', *([itertools.chain(*a)] * 2)))

rawDescription: Python html table layout

description: Python html table layout


code:
#!/usr/local/bin/python3

print('Content-type: text/html\n')

def fib(n):
    a, b = 0, 1
    for i in range(n):
        a, b = b, a + b
    return a

print('''

        Index Number
        Decimal
        Hexadecimal
        Octadecimal
        float

''')

for i in range (0, 51):
    print('''

        {0}
        {1}
        {1:x}
        {1:o}
        {1:.2f}

'''.format(i, fib(i)))

print('
')

rawDescription: Python HTML Table

description: Python HTML Table


code:
b"abcde"
b"abcde".decode("utf-8")

rawDescription: Convert bytes to a Python string

description: Convert bytes to a Python string


code:
import argparse

parser = argparse.ArgumentParser(description="My program!", formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-s", type=argparse.FileType('r'), help="Filename to be passed")
args = vars(parser.parse_args())

open_file = args.s

rawDescription: how to pass file as argument in python script using argparse module?

description: Pass file as argument in python script using argparse module


code:
ids = set(list1)
result = [x for x in list2 if x[0] in ids]
result
ids - set(x[0] for x in result)

rawDescription: Python - Intersection of list with nested list


code: words_s1 = nltk.regexp_tokenize(s1, r'(?u)\d+(?:\.\d+)?|\w+')

rawDescription: nltk tokenize measurement units

description: Nltk tokenize measurement units


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

# simulate some artificial data
x = np.random.randn(1000,)
y = 5 * x ** 2 + np.random.randn(1000,)
data = pd.DataFrame(0.0, columns=['X', 'Y'], index=np.arange(1000))
data.X = x
data.Y = y

# now do your stuff
# ================================
# use the pandas 'cut' function
data['X_bins'] = pd.cut(data.X, 3)
# for each bin, calculate the mean of Y
result = data.groupby('X_bins')['Y'].mean()
# do the plot
result.plot()

rawDescription: matplotlib mean interval plot

description: Matplotlib mean interval plot


code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(1, 100, 1000)
y = x
plt.hexbin(x, y, mincnt = 1, gridsize = 16, vmax = 100, cmap='summer')

rawDescription: matplotlib basemap hexbin colorbar maximum value higher than maximum value in array

description: Matplotlib basemap hexbin colorbar maximum value higher than maximum value in array


code:
rows = myListOfRows
for col in xrange(len(rows[0])):
    checkCol(rows, col)

def checkCol(rows, c):
    for r in xrange(len(rows)-1):
        if rows[r][c] != -9999 or -9999 != rows[r+1][c]: # checking for elements that are not already -9999.00
            if abs(rows[r][c] - rows[r+1][c]) > 0.03:
                rows[max([r,r+1], key=lambda rr: rows[rr][c])][c] = -9999.00

rawDescription: iterating through an output comparing data in same position for continuity

description: Iterating through an output comparing data in same position for continuity


code:
a = 10
b = 4
c = 2
d = 3
if ((c+2<d)) or ((c*d)==(a-b)):
    if (True and not(True) or True):
        print ("X")
    else:
        print("Y")
    print("Z")

rawDescription: How would I go about interpreting this python code?


code:
import ast
ast.dump(ast.parse('print "hi"'))
ast.dump(ast.parse('print "hi",'))

rawDescription: Python ast.Print "nl" argument?

description: Python ast.Print "nl" argument


code:
import json
json.dumps(mylist)

rawDescription: Represent List as a String with Double-Quotes Instead of Single-Quotes for JSON


code:
import os

in_dir = './test'
remove = 'hello'

paths = [os.path.join(in_dir,file) for file in os.listdir(in_dir) if remove in file]

for file in paths:
    os.rename(file, file.replace(remove, ""))

rawDescription: Python filename change

description: Python filename change


code:
import subprocess
for f in files:
    subprocess.call(['wc', '-l', f])

rawDescription: Running "wc -l <filename>" within Python Code


code:
from xlwings import Application, Workbook, xlfunc
import numpy as np

@xlfunc
def interp(x, xp, yp):
    """Interpolate vector x on vector (xp,yp)"""
    y = np.interp(x, xp, yp)
    return y[:, np.newaxis]  # column orientation

@xlfunc
def random():
    """Returns a random number between 0 and 1"""
    app = Application(Workbook.caller())
    # We shall make this easier in a future release (getting the array dimensions)
    r = app.xl_app.Caller.Rows.Count
    c = app.xl_app.Caller.Columns.Count
    x = np.random.randn(r, c)
    return x

rawDescription: Accelerate UDF in xlwings

description: Accelerate UDF in xlwings


code:
from locust import HttpLocust, TaskSet, task
from pyquery import PyQuery

class UserBehaviour(TaskSet):

    def get_data(self, url, locator):
        data = []
        request = self.client.get(url)

        pq = PyQuery(request.content)
        link_elements = pq(locator)
        for link in link_elements:
            if key in link.attrib and "http" not in link.attrib[key]:
                data.append(link.attrib[key])

        return data

    @task
    def test_get_thing(self):
        data_ = self.get_data("/url/to/send/request", "#review-ul > li > div > a", "href")
        self.client.post("url", data = data_)

rawDescription: Posting dynamic values in Locust

description: Posting dynamic values in Locust


code:
from ansible.plugins.filter import ipaddr
dir(ipaddr)

rawDescription: Calling a filter plugin from an action plugin


code:
for i, j in enumerate(fint):
    worksheet.write(globdat.cycle, i, j)

rawDescription: copying data to excel using python (xlwt)

description: Copying data to excel using python (xlwt)


code: filename = natsorted(filename)

rawDescription: How to sort folder names with natsort in Python that I got with os.walk?

description: Sort folder names with natsort in Python that I got with os.walk


code:
import StringIO

FFMPEG_BIN = "ffmpeg.exe"
some_fs = gridfs.GridFS(db, "some.col")
vid_id = ObjectId("5339e3b5b322631b544b2338")

vid_file = some_fs.get(vid_id)

# Should be a proper file-like object
infile =  StringIO.StringIO(vid_file.read())

pipe = sp.Popen([FFMPEG_BIN,
    # "-v", "quiet",
    "-y",
    "-i", "-",
    "-vcodec", "copy", "-acodec", "copy",
    "-ss", "00:00:00", "-t", "00:00:10", "-sn",
    "test.mp4" ]
    ,stdin=infile, stdout=sp.PIPE
)
pipe.wait()

...

infile.close()

rawDescription: How to get a file object from mongodb using pymongo?

description: Get a file object from mongodb using pymongo


code: from math import sin, cos, pi

rawDescription: Certain trigonometry packages from numpy


rawDescription: Convert data from one format to another

description: Convert data from one format to another


code:
class IterPartRegistry(type):
    def __iter__(cls):
        return (c for c in cls._registry if isinstance(c, cls))

rawDescription: Make class iterable respecting inheritance


code: otherlist = [(x[0],y) for x in first_list for y in x[1]]

rawDescription: List operation for nested list

description: List operation for nested list


code:
f = open("grade_file.txt", "r")
line = f.readlines()

for i in (line):
    i = i.strip()
    print(i[-1:])

rawDescription: I want to get the last element and the 3rd to the last element of each line in a .txt file


code:
from billiard import Process
from model import CrawlerSettings
from scrapy.crawler import CrawlerRunner
from scrapy.utils.log import configure_logging
from spiders import spider
from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from utility import MysqlConnector

class ProcessJob():
    def processJob(self, job):
        #update job
        mysql = MysqlConnector.Mysql()
        db = mysql.getConnection();
        cur = db.cursor();
        job.status = 1
        update = "UPDATE job SET status=1 WHERE id=" + str(job.id)
        cur.execute(update)
        db.commit()
        db.close()

        #Start new crawler
        configure_logging()
        webspider = spider.MySpider;
        if job.ajax == 1:
            webspider.custom_settings = CrawlerSettings.ajax_settings;
        else:
            webspider.custom_settings = CrawlerSettings.normal_settings;
        crawler = UrlCrawlerScript(webspider, job)
        crawler.start()

class UrlCrawlerScript(Process):
    def __init__(self, spider, job):
        Process.__init__(self)
        self.crawler = CrawlerRunner()
        self.crawler.crawl(spider, job=job)

    def run(self):
        d = self.crawler.join()
        d.addBoth(lambda _: reactor.stop())
        reactor.run(0)

rawDescription: ReactorAlreadyRunning Scrapy

description: ReactorAlreadyRunning Scrapy


code:
class FullTime(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "FullTime(%r)" % self.name
staffList = ["Alice", "Bob", "Eve"]
staffClassDict = {employee: FullTime(employee) for employee in staffList}
staffClassDict

rawDescription: Dict Comprehension With Class Objects

description: Dict Comprehension With Class Objects


code:
def count(local_list):
    sum1 = 0
    for l in local_list:
            if not isinstance(l,list):
                    return 1
            else:
                    sum1+= count(l)

    return sum1

count([['a', 'b', 'c', 'a'], [['b', 'd', 'a', 'c'], ['a', 'a', 'a', 'b']], ['a', 'b', 'g', 'h'], [['x', 'z', 'c', 'c'], ['b', 'c', 'b', 'a']]])

rawDescription: How to Sum up number of list

description: Sum up number of list


code:
lst = ['a','b',' c', '', '', '', 'd','e','f','','k','j']

res_lst = []
res_str = ''
for i, item in enumerate(lst):
    res_str += item # Add current item to res_str

    # If next 3 items result is an empty string
    # and res_str has value
    if not ''.join(lst[i+1:i+3]) and res_str:
        res_lst.append(res_str)
        res_str = ''

print(res_lst)

rawDescription: python3 group items in list seperated by at least three empty items

description: Python3 group items in list seperated by at least three empty items


code: max(max(v) if isinstance(v, collections.Iterable) else v for v in d.itervalues())

rawDescription: Finding maximum value in a dictionary containing mixed items in Python


code:
x = '\\frac{\\alpha}{x}'
x
print(x)

rawDescription: latex() in sympy. How to get \frac and not \\frac

description: Latex() in sympy. How to get \frac and not \\frac


code:
class LikeSerializer(serializers.ModelSerializer):
    user = UserSerializer(required=False)
    likes = serializers.SerializerMethodField('no_likes_get')

    def no_likes_get(self, like_obj):
        return Like.objects.filter(movie_id = like_obj.movie_id).count()

    class Meta:
        model = Like
        fields = ('id', 'user', 'movie_id', 'likes')

rawDescription: How can I get results from multiple models with Django REST Framework

description: Get results from multiple models with Django REST Framework


code:
class FileInfo(dict):
    """store file metadata"""
    def __init__(self, filename=None):
        super( FileInfo, self ).__init__()
        self["name"] = filename

rawDescription: Inheritance and Overriding __init__ in python


code:
starts = df["Start Time"].copy()
starts.sort()
dt = starts.diff()
heat = (dt > pd.Timedelta(seconds=60)).cumsum()
heat = heat.sort_index()

rawDescription: Dividing Pandas DataFrame rows into similar time-based groups


code:
if not os.path.isdir("DirectoryName"):
    os.makedirs("DirectoryName")

if not os.path.isfile('FileName.xlsx'):
    wb = openpyxl.Workbook()
    dest_filename = 'FileName.xlsx'
    wb.save(os.path.join('DirectoryName', dest_filename), as_template=False)

rawDescription: Create an excel file in a different directory


rawDescription: Avoiding Tkinter OptionMenu button resizing


code:
import os
import shutil

source = '/tmp/src/'
destinations = []

def copy_files_multiple_dest(source,destinations):
  sfiles = os.listdir(source) # list of all files in source
  for f in sfiles:
    for dest in destinations:
      shutil.copy(os.path.join(source,f), dest)

rawDescription: Python - What is the fastest way i can copy files from a source folder to multiple destinations


code:
import os
pathiter = (os.path.join(root, filename)
    for root, _, filenames in os.walk(folder)
    for filename in filenames
)
for path in pathiter:
    newname =  path.replace('ES.txt', '_ES_manual.txt')
    if newname != path:
        os.rename(path,newname)

rawDescription: Replacing Filename characters with python


code:
from fabric.api import run, env, cd, settings, hide, show
env.host_string='xxx.xxx.com'
env.user='user'
env.password='password'
run('ls -lart')

rawDescription: How to call .ksh file as part of Unix command through ssh in Python

description: Call .ksh file as part of Unix command through ssh in Python


code:
if __name__ == '__main__':
        from contextlib import closing

        cP1 = Counter()
        cP2 = Counter()

        # I hope you have an actual list of configs here, otherwise map will
        # will call `countIt` with the single characters of the string 'myConfig'
        cfg = "myConfig"

        # `contextlib.closing` makes sure the pool is closed after we're done.
        # In python3, Pool is itself a contextmanager and you don't need to
        # surround it with `closing` in order to be able to use it in the `with`
        # construct.
        # This approach, however, is compatible with both python2 and python3.
        with closing(multiprocessing.Pool(4)) as p:
            # Just counting, no need to order the results.
            # This might actually be a bit faster.
            for c1, c2 in p.imap_unordered(countIt, cfg):
                cP1 += c1
                cP2 += c2

rawDescription: return counter object in multiprocessing / map function

description: Return counter object in multiprocessing / map function


code:
chloe.pencolor("blue4")

# draw the spokes
for i in range(24):
    chloe.forward(20)
    chloe.backward(20)
    chloe.left(15)

# raise pen
chloe.up()

# head down
chloe.setheading(270)

# go forward 20
chloe.forward(20)

# reset heading
chloe.setheading(0)

# pen down
chloe.down()

# draw the circle
chloe.circle(20)

rawDescription: Drawing a indian flag chakra in python turtlee


rawDescription: In python how to I make [x , y] into individual variables?


code:
def asteriskify( string ):
   if len( string ) == 0: return ''   # corner case pointed out by smci
   return string[ 0 ] + string[ 1: ].replace( string[ 0 ], '*' )

rawDescription: Take a string and return all occurrences of its first char with a '*'


code:
from time import strftime

name_file = "{0}_{1}.txt".format(prefix_file,
                                 strftime("%Y%m%d%H%M"))

rawDescription: How get a datetime string to suffix my filenames?


code:
from test_app.models import AMe, MP
ame = AMe.objects.create()
for _ in xrange(6):
    MP.objects.create(MyAMeID=ame)
ame.mp_set.all()

rawDescription: Getting django record by foreign key

description: Getting django record by foreign key


code:
import re
files = (file for file in tar.getnames() if file.startswith('rootdir/'))

rawDescription: Python: Untar a single folder from a tarball


code:
for report in reports:
    for x in range(0, 30):
        browser.find_element_by_partial_link_text('Program Management').send_keys(Keys.ARROW_DOWN * x, Keys.ENTER)
        browser.find_element_by_id("ctl00_PlaceHolderMain_ReportViewer1_HtmlOutputReportResults2_CSVButton_Content").click()

rawDescription: Python Selenium webdriver loop with addition


code:
x = 3
def f( x=x ):
    return x

x = 7
f()  # 3

rawDescription: Binding an objects value within a function (closure)


rawDescription: How do I log text and a variable in the same line in Python?

description: Log text and a variable in the same line in Python


rawDescription: workarounds in python for appending list elements by value

description: Workarounds in python for appending list elements by value


code:
import requests
import json

data = {"consumer_key": "..."}
headers = {"content-type": "application/json"}
response = requests.post("http://getpocket.com", data=json.dumps(data), headers=headers)
response.json

rawDescription: How can I perform the json request in python

description: Perform the json request in python


code:
from constraint import Problem

prob = Problem()
prob.addVariables(["a1", "a2"], range(10,41,2))
prob.addVariable("b1", [0, 2])
prob.addVariable("b2", range(5, 31, 5))
prob.addConstraint(lambda a1, a2: a2 <= a1, ["a1", "a2"])
prob.addConstraint(lambda b1, b2: b1 != 0 or b2 == 5, ["b1", "b2"])

for params in prob.getSolutionIter():
    run_sim(**params)

rawDescription: Python cartesian product and conditions?

description: Python cartesian product and conditions


code:
def starting_with(arr, start_index):
     # use xrange instead of range in python 2
     for i in range(start_index, len(arr)):
        yield arr[i]
     for i in range(start_index):
        yield arr[i]

for value in starting_with(my_list, 3):
    ...

rawDescription: Iterate Through a List Given a Starting Point


code:
x = np.arange(93,98)
y = np.arange(7,12)
position = np.transpose([np.tile(x, len(x)), np.repeat(y, len(y))])
position

rawDescription: scipy.optimize.curve_fit error- result from function not a proper array of floats

description: Scipy.optimize.curve_fit error- result from function not a proper array of floats


code:
class Dated(models.Model):
  ...
  created = models.DateTimeField(default=datetime.now)

  class Meta:
    ordering = ('created',)

  def __cmp__(self, other):
    try:
      return cmp(self.created, other.created)
    except AttributeError:
      return cmp(self.created, other)

rawDescription: How do you order lists in the same way QuerySets are ordered in Django?

description: Order lists in the same way QuerySets are ordered in Django


code:
import csv
import random

with open('datafile.csv', 'r') as data, \
        open('test.csv', 'w') as test, \
        open('train.csv', 'w') as train:
    test_writer = csv.writer(test)
    train_writer = csv.writer(train)
    reader = csv.reader(data)
    header = next(reader)
    test_writer.writerow(header)
    train_writer.writerow(header)
    for row in reader:
        if random.random() > 0.85:
            test_writer.writerow(row)
        else:
            train_writer.writerow(row)

rawDescription: Python: How to retain headers from one file and append it to another


code:
total = 0
maxValue = None
while True:
    value = input("Enter the next number: ")
    if value != "end":
        num = float(value)
        maxValue = num if maxValue and num > maxValue else num
        total += num
    else:
        print("The sum of all values except for the maximum value is: ",total-maxValue )
        # return outside a function is SyntaxError
        break

rawDescription: Using += in a while loop but need to exclude highest number


rawDescription: What's the fastest way to compare datetime in pandas?


rawDescription: Python ensure object appears at end of list


code:
k = int(raw_input('From What row items should be appeared again at the end?'))
res = [l for l in product(*(li+[li[k]])) if l[k]<l[len(li)] ]

rawDescription: Special type of combination using itertools


code: array_3 = [x for x in array_1 if x not in array_2]

rawDescription: Python Array Substraction

description: Python Array Substraction


code:
def month_year_iter( start_month, start_year, end_month, end_year ):
    ym_start= 12*start_year + start_month - 1
    ym_end= 12*end_year + end_month - 1
    for ym in range( ym_start, ym_end ):
        y, m = divmod( ym, 12 )
        yield y, m+1

rawDescription: How to create a month iterator

description: Create a month iterator


code:
fname = input("Please enter the file name for your list of names: ")

with open(fname, "r") as infile, open("student-firstnames.txt", "w") as outfile:
    for line in infile:
        firstname = line.strip().split(', ')[-1]
        print(firstname, file=outfile)

rawDescription: How do I split a list of first and last names apart?

description: Split a list of first and last names apart


code:
import re
data = """(u'I', u'PRON')(u'am', u'VERB')(u'nobody', u'NOUN')(u':', u'.')(u'A', u'DET')(u'red', u'ADJ')(u'sinking', u'NOUN')(u'autumn', u'NOUN')(u'sun', u'NOUN')(u'Took',u'NOUN')(u'my', u'PRON')(u'name', u'NOUN')(u'away', u'ADV')(u'.', u'.')(u'Keep', u'VERB')(u'straight', u'VERB')(u'down', u'PRT')(u'this',u'DET')(u'block', u'NOUN')'s = r"(u'I', u'PRON')(u'am', u'VERB')(u'nobody', u'NOUN')(u':', u'.')(u'A', u'DET')(u'red', u'ADJ')(u'sinking', u'NOUN')(u'autumn', u'NOUN')(u'sun', u'NOUN')(u'Took', u'NOUN')(u'my', u'PRON')(u'name', u'NOUN')(u'away', u'ADV')(u'.', u'.')(u'Keep', u'VERB')(u'straight',u'VERB')(u'down', u'PRT')(u'this', u'DET')(u'block', u'NOUN')"""
#Use regex to get the split the data as required
rx = re.compile(r"\(u'(.*?)'\,\su'(.*?)'\)")
#Find all the matches
matches = rx.findall(s)
tuples = []
for match in matches:
    tuples.append(match)

#Get the nouns from the list of tuples
nouns = [ x for x in tuples if "NOUN" in x]

rawDescription: How to iterate through tuples in a file

description: Iterate through tuples in a file


code:
from dateutil.parser import parse
parse("Fri, 2 Jan 2015 11:44:17 -0800")

rawDescription: Getting a tzinfo object from a timestamp


code:
agg = {
    '_id' : unique_id_str,
    'total' : int(total),
    'mode' : int(mymode)
}

rawDescription: including a NumberInt in a dict for pymongo

description: Including a NumberInt in a dict for pymongo


code:
import cv2
import numpy as np

# create video capture
cap = cv2.VideoCapture(0)

while(1):

    # read the frames
    _,frame = cap.read()

    # smooth it
    frame = cv2.blur(frame,(3,3))

    # convert to hsv and find range of colors
    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
    thresh = cv2.inRange(hsv,np.array((0, 80, 80)), np.array((20, 255, 255)))
    thresh2 = thresh.copy()

    # find contours in the threshold image
    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

    # finding contour with maximum area and store it as best_cnt
    max_area = 0
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > max_area:
            max_area = area
            best_cnt = cnt

    # finding centroids of best_cnt and draw a circle there
    M = cv2.moments(best_cnt)
    cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00'])
    cv2.circle(frame,(cx,cy),5,255,-1)

    # Show it, if key pressed is 'Esc', exit the loop
    cv2.imshow('frame',frame)
    cv2.imshow('thresh',thresh2)
    if cv2.waitKey(33)== 27:
        break

# Clean up everything before leaving
cv2.destroyAllWindows()
cap.release()

rawDescription: OpenCV Python single (rather than multiple) blob tracking?


code:
class Rectangle(db.Model):
    width   = db.IntegerProperty()
    height  = db.IntegerProperty()

    @db.ComputedProperty
    def area(self):
      return self.width * self.height

rawDescription: How to overwrite the put() method on a python app engine model?

description: Overwrite the put() method on a python app engine model


code: squared = [x**2 for x in lst]

rawDescription: Using python to return a list of squared integers


code:
from django.db.models import F
...
MyModel.objects.filter(id=...).update(hit_count=F('hit_count')+1)

rawDescription: Increment Page Hit Count in Django

description: Increment Page Hit Count in Django


code:
SuperEggs= Spam(id='SuperEggs', eggs="2 egg whites")
SuperEggs.put()

rawDescription: Specifying a key for datastore entities

description: Specifying a key for datastore entities


code:
X_list = []
Y_list = []
with open(file) as catalog:
    for line in catalog:
        column = line.split()
        if not line.startswith('#'): #skipping column labels
            x = float(column[3])
            y = float(column[4])
            X_list.append(x)
            Y_list.append(y)
#then print the lists if you wish
print(X_list)
print(Y_list)

rawDescription: Python - turning text column into array, where I can access each number in the array


code:
class Songs(dict):

    def load(self, song_id):
        with open(song_id, 'r') as f:
            for line in f:
                sid, name, genre = line.rstrip().split('::')[:3]
                self[sid] = [name, genre]

mysongs = Songs()
mysongs.load('barnes_and_barnes__fish_heads')
mysongs['barnes_and_barnes__fish_heads'] = ['roly poly', 'strange']  # set
del mysongs['barnes_and_barnes__fish_heads']                         # delete

rawDescription: Python class - Set & Delete methods?

description: Python class - Set & Delete methods


code:
def put(self, request, *args, **kwargs):
    request.PUT = QueryDict(request.body)
    form = self.form_class(request.PUT)
    if form.is_valid():
        logging.warning("This form is VALID")
    else:
        logging.warning("This form is INVALID")

rawDescription: dojo/request PUT method dosn't work

description: Dojo/request PUT method dosn't work


code:
from collections import defaultdict

d = defaultdict(dict)

phonemes = ["T", "UH", "T"]
graphemes = ["t", "oo", "t"]
# Create offset list for dict key
graphemes_offset = ['^'] + graphemes[:len(graphemes)-1]

for ph, g, g_o in zip(phonemes, graphemes, graphemes_offset):
  d[(ph,g_o)][g] = d[(ph, g_o)].setdefault(g,0) + 1

rawDescription: trigram frequency in dictionaries

description: Trigram frequency in dictionaries


code:
import sys
import os

for f in sys.argv[1:]:
    if os.path.exists(f):
        for line in open(f).readlines():
            process(line)

rawDescription: What is the equivalent of Perl's (<>) in Python? fileinput doesn't work as expected


code: kwargs['count'] = 100000

rawDescription: How do I evade the limit of 100 entries in python splunk query

description: Evade the limit of 100 entries in python splunk query


code:
dir(form.base_product)
[#ommitted some attributes#,
'__weakref__', '_formfield', '_obj', '_run_validation_chain',
'_translations', 'data', 'default', 'description',
'do_not_call_in_templates', 'errors', 'filters', 'flags', 'form',
'form_class', 'gettext', 'id', 'label', 'meta', 'name', 'ngettext',
'object_data', 'populate_obj', 'post_validate', 'pre_validate',
'process', 'process_data', 'process_errors', 'process_formdata',
'raw_data', 'render_kw', 'separator', 'short_name', 'type',
'validate', 'validators', 'widget' ]

rawDescription: WTForm FormField populate values

description: WTForm FormField populate values


code:
prop = getattr(foo.__class__,"the_property_name")
obj_key = prop.get_value_for_datastore(foo)

rawDescription: Get a datastore object by getattr without knowing its class


code:
import sys

#Functions
def a(filename): pass
def b(filename): pass
def c(filename): pass

#Function chooser
func_arg = {"-a": a, "-b": b, "-c": c}

#Do it
if __name__ == "__main__":
    func_arg[sys.argv[1]](sys.argv[2])

rawDescription: Python - specify which function in file to use on command line


code:
class Child(Parent):
  def __init__(self, *args, **kwargs):
    super(Child, self).__init__(*args, **kwargs)

rawDescription: why I can not use **kwargs

description: Why I can not use **kwargs


rawDescription: Create a matrix with every possible column


code:
string = "t e s t  t e s t"
' '.join(word.replace(' ', '') for word in string.split('  '))

rawDescription: How to replace strings that are similar

description: Replace strings that are similar


code: data.loc[data["Brand"].isin(group_clients), "FreeSec"] = True

rawDescription: how to set values to rows of boolean filtered dataframe column

description: Set values to rows of boolean filtered dataframe column


rawDescription: Removing duplicates in each row of a numpy array


code:
lines = [line.lower() for line in lines]
with open('listTogether.txt', 'w') as out:
    out.writelines(sorted(lines))

rawDescription: convert all lines in file to lowercase then write to new file

description: Convert all lines in file to lowercase then write to new file


code:
from tkinter import *
root=Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM,fill=X)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)

root.mainloop()

rawDescription: python and tkinter: using scrollbars on a canvas

description: Python and tkinter: using scrollbars on a canvas


code:
import win32api

try:
    info = win32api.GetFileVersionInfo('rpmsrv.exe', "\\")
    ms = info['FileVersionMS']
    ls = info['FileVersionLS']
    __version__ = "%d.%d.%d.%d" % (win32api.HIWORD(ms), win32api.LOWORD (ms),
                                 win32api.HIWORD (ls), win32api.LOWORD (ls))
except:
    __version__ = '5.1.1.000' # some appropriate default here.

rawDescription: Find Version of Binary File

description: Find Version of Binary File


code:
class DateField(Field):
    ...
    def pre_save(self, model_instance, add):
        if self.auto_now or (self.auto_now_add and add):
            value = datetime.date.today()
            setattr(model_instance, self.attname, value)
            return value

rawDescription: Which timezone does Django use in DateField's auto_now_add?


code:
class ReplyAdmin(admin.ModelAdmin):
    list_display = ["__str__", "user", "question", "creationDate", "get_pages",]

    def get_pages(self, obj):
        return "\n".join([page.title for page in obj.question.page.all()])

    class Meta:
        model = Reply
admin.site.register(Reply, ReplyAdmin)

rawDescription: add fields in admin.py from a ManyToMany

description: Add fields in admin.py from a ManyToMany


code:
import logging
from Queue import Queue #queue in python 3
from threading import Event #hmm... this needed?

insert_logger = logging.getLogger('async_insert')
insert_logger.setLevel(logging.INFO)

def handle_err(err):
  insert_logger.warning('Failed to insert due to %s', err)

#Designed to work in a high write environment. Chained callbacks for best performance and fast fail/stop when error
#encountered. Next insert should re-up the writing. Potential loss of failed write. Some guarantee on order of write
#preservation.
class CappedQueueInserter(object):
  def __init__(self, session, max_count=0):
    self.__queue = Queue(max_count)
    self.__session = session
    self.__started = Event()

  @property
  def started(self):
    return self.__started.is_set()

  def insert(self, bound_statement):
    if not self.started:
      self._begin(bound_statement)
    else:
      self._enqueue(bound_statement)

  def _begin(self, bound_statement):
    def callback():
      try:
        bound = self.__queue.get(True) #block until an item is added to the queue
        future = self.__session.execute_async(bound)
        future.add_callbacks(callback, handle_err)
      except:
        self.__started.clear()

    self.__started.set()
    future = self.__session.execute_async(bound_statement)
    future.add_callbacks(callback, handle_err)

  def _enqueue(self, bound_statement):
    self.__queue.put(bound_statement, True)

#Separate insert statement binding from the insertion loop
class InsertEnqueue(object):
  def __init__(self, prepared_query, insert, consistency_level=None):
    self.__statement = prepared_query
    self.__level = consistency_level
    self.__sink = insert

  def insert(self, *args):
    bound = self.bind(*args)
    self.__sink.insert(bound)

  @property
  def consistency_level(self):
    return self.__level or self.__statement.consistency_level

  @consistency_level.setter
  def adjust_level(self, value):
    if value:
      self.__level = value

  def bind(self, *args):
    bound = self.__statement.bind(*args)
    bound.consistency_level = self.consistency_level

    return bound

rawDescription: Python: Asynchronous Cassandra Inserts

description: Python: Asynchronous Cassandra Inserts


code:
conn = None

def driverFunc():
    global conn
    try:
        conn = psycopg2.connect(database=db, user=user, password=password, host=host)
        tables = ['table1', 'table2']
        countDict = {}
        for thisTable in tables:
            db_query(thisTable, countDict)
    finally:
        if not conn == None:
            conn.close()

def db_query(tableName, countDict):
    # Beware of SQL injection with the following line:
    query = "select count(*) from " + tableName + " where error_string != '';"
    cur = None

    try:
        cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
        cur.execute(query)
        countDict[tableName] = int(cur.fetchone())
    finally:
        if not cur == None:
            cur.close()

rawDescription: Return a mapped dictionary based on multiple queries


code:
with open('filename.extension', 'r') as file:
    for line in file:
        print(line)
        time.sleep(1)

rawDescription: Shutil.copyfileobj Load line after line using time.sleep()


code:
S1 = "thirteen things that don't make sense"
S2 = "13 Things That Don't Make Sense"
eq_map = {'one': '1', 'two': '2', 'thirteen': '13'}

def convert(s):
    s = s.lower()
    return [eq_map.get(w, w) for w in s.split()]

assert convert(S1) == convert(S2)

rawDescription: Comparing strings of numbers with strings of words of numbers


code:
import os
from pathlib import PureWindowsPath
a, b = map(PureWindowsPath, [r'C:\root\dir', r'C:\root\dir1'])
PureWindowsPath(*os.path.commonprefix([a.parts, b.parts]))

rawDescription: Python os.path.commonprefix - is there a path oriented function?


code:
# say, reader is the reader object
# do next(reader) to skip the headings
from itertools import groupby
def by_month(row):
    date = row[0]
    month = int(date.split("/", 1)[0])
    return month

m_g = groupby(reader, by_month)

for month, group in m_g:
    group = list(group)
    # use a month to month_name map
    with open('{}.csv'.format(month), 'w') as fw:
        w = csv.writer(fw)
        w.writerows(group)

rawDescription: Filter CSV by month and create new csv with that months data


code: conn.execute("SELECT foo FROM table where bar = '%s'" % var)

rawDescription: How to access content of declared Variable in python in my POSTGRES sql query

description: Access content of declared Variable in python in my POSTGRES sql query


code:
def addslashes(value):
    """
    Adds slashes before quotes. Useful for escaping strings in CSV, for
    example. Less useful for escaping JavaScript; use the ``escapejs``
    filter instead.
    """
    return value.replace('\\', '\\\\').replace('"', '\\"').replace("'", "\\'")
addslashes.is_safe = True
addslashes = stringfilter(addslashes)

rawDescription: How do I write a Django template custom tag that adds a slash before a single quote?

description: Write a Django template custom tag that adds a slash before a single quote


code:
def allInRange(bst, left, right):
    if bst is EmptyValue:
        return
    if left <= bst.root <= right:
        print(bst.root)
    allInRange(bst.left, left, right)
    allInRange(bst.right, left, right)

rawDescription: Collect values in a certain range from a binary tree?


code:
class Position(models.Model):
    label = models.CharField(max_length=100)

    def __unicode__(self):
        return self.label

class UserProfile(models.Model):
    [...]
    positions = models.ManyToManyField(Position)

rawDescription: Basic django model question on ManyToMany


code:
class GuildMiddleware(object):
    def process_request(self, request):
        request.G = figure_out_what_G_is()
        return None

rawDescription: Is it possible to check whether a context variable is already set in a view within a custom context processor definition?


code:
import io

with io.open(r'c:\path\to\output.txt', 'w', encoding='utf-8-sig') as file:
    file.write(u"abc\n")
    file.write(u"\u2744\n")

rawDescription: In Python: saving Unicode letters with newlines into a .txt so that it works fine while opening both using Excel and a text editor


code:
from django.db.models import Q

se_qs = Se.objects\
    .filter(pqse__pq=pq, ws__is_important=True)\
    .exclude(Q(pqse__pqsew__personpqsew__is_valid=False) |
             Q(pqse__pqsew__personpqsew__isnull=False))

rawDescription: Django advanced query with "or"

description: Django advanced query with "or"


code: from protocol import *

rawDescription: What's the difference between xmpp.protocol.Message and xmpp.Message?


code:
from PIL import ImageGrab
im = ImageGrab.grabclipboard()
im.save('somefile.png','PNG')

rawDescription: How do I read a jpg or png from the windows clipboard in python and vice versa?

description: Read a jpg or png from the windows clipboard in python and vice versa


rawDescription: Pandas Dataframe or Panel to 3d numpy array


rawDescription: How to create a series of numbers using Pandas in Python

description: Create a series of numbers using Pandas in Python


code:
BROKER_URL = 'amqp://guest:guest@localhost//'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'

rawDescription: Save the Celery task in DB- Django

description: Save the Celery task in DB- Django


code:
class User(models.Model):
    id = models.IntegerField()
    created_on = models.DateTimeField(auto_now_add=True)
    updated_on = models.DateTimeField(auto_now=True)
    name = models.CharField(default='blah blah')

rawDescription: Automatically changing Django model field on update for one field and freezing one-time write-only for another?


code:
def notebook_creation_hook_callback( notebook, page, x, y ):
    new_notebook = gtk.Notebook
    new_notebook.set_group_id(notebook.get_group_id()) #Id must be the same as original notebook
    window = gtk.Window( gtk.WINDOW_TOPLEVEL )
    window.add( new_notebook)
    window.show_all()
    window.move( x,y )
    return new_notebook #Return the new notebook on success

description: Gtk.notebook_set_window_creation_hook function return


rawDescription: How to scroll to a selected item in a ScrolledListBox in python?

description: Scroll to a selected item in a ScrolledListBox in python


code:
return qs.annotate(nnum_active_users=django.db.models.Count(django.db.models.Case(django.db.models.When(
                                                                                user__is_active=True, then=1, ))
                                                                      )
                           )

rawDescription: Django: How to annotate query set with count number of active users and non active users


code:
import pylab
import numpy as np
e = np.arange(0.0, 1.0, 0.15).reshape(-1, 1)
nu = np.linspace(0, 2*np.pi, 50000)
x =  ((1-e)/(1+e))**0.5 * np.tan(nu/2.)
x2 = e*(1-e**2)**0.5 * np.sin(nu)/(1 + e*np.cos(nu))
using_arctan = True
using_OP_arctan2 = False

if using_arctan:
    M2evals = 2*np.arctan(x) - x2
    M2evals[ M2evals<0 ] += 2*np.pi
elif using_OP_arctan2:
    M2evals = 2 * np.arctan2(1,1/x) - x2

fig2 = pylab.figure()
ax2 = fig2.add_subplot(111)
for M2e, _e in zip(M2evals, e.ravel()):
    ax2.plot(nu.ravel(), M2e, label = str(_e))
pylab.legend(loc='upper left')
pylab.show()

rawDescription: using arctan / arctan2 to plot a from 0 to 2π

description: Using arctan / arctan2 to plot a from 0 to 2π


code:
import pyinotify

# Instanciate a new WatchManager (will be used to store watches).
wm = pyinotify.WatchManager()
# Associate this WatchManager with a Notifier (will be used to report and
# process events).
notifier = pyinotify.Notifier(wm)
# Add a new watch on /tmp for ALL_EVENTS.
wm.add_watch('/tmp', pyinotify.ALL_EVENTS)
# Loop forever and handle events.
notifier.loop()

rawDescription: Python - How to know if a file is completed when copying from an outside process


code:
class OneSquare():
    def __init__(self, can, start_x, start_y, size):
        self.can=can
        self.id = self.can.create_rectangle((start_x, start_y,
                  start_x+size, start_y+size), fill="red")
        self.can.tag_bind(self.id, "<ButtonPress-1>", self.set_color)

        self.color_change=True

    def set_color(self, event=None):
        self.color_change = not self.color_change
        color="red"
        if not self.color_change:
            color="green"
        self.can.itemconfigure(self.id, fill=color)

root = Tk()
canvas = Canvas(root)
canvas.grid(column=1, row=1, sticky=(N, S, E, W))

OS1=OneSquare(canvas, 1, 1, 60)
OS2=OneSquare(canvas, 90, 1, 30)
OS3=OneSquare(canvas, 1, 90, 90)

Button(root, text="Exit", bg="orange",
       command=root.quit).grid(row=2)

root.mainloop()

rawDescription: Changing rectangle color on click in Python using Tk


code:
import Leap

class FingerCounter (Leap.Listener):
    def on_frame (self, controller):
        f = controller.frame()
        print(len(f.fingers.extended()))

listener = FingerCounter()

try:
    controller = Leap.Controller()
    controller.add_listener(listener)
    raw_input()
except KeyboardInterrupt:
    pass
finally:
    controller.remove_listener(listener)

rawDescription: "extended" function python in Leap Motion SDK doesn't work


code:
textedit.setLineWrapMode(QtGui.QPlainTextEdit.NoWrap)
# above code in between here
textedit.setLineWrapMode(QtGui.QPlainTextEdit.WidgetWidth)

rawDescription: Position cursor at specifc line

description: Position cursor at specifc line


code:
from nltk import word_tokenize, pos_tag, ne_chunk
sent = "This is a Foo Bar sentence."
nltk.pos_tag(word_tokenize(sent))
tagged_sent = nltk.pos_tag(word_tokenize(sent))
ne_chunk(tagged_sent)

rawDescription: What does NN VBD IN DT NNS RB means in NLTK?


code:
class mydeque(object):

  def __init__(self):
    self.left = []
    self.right = []

  def pushleft(self, v):
    self.left.append(v)

  def pushright(self, v):
    self.right.append(v)

  def popleft(self):
    if not self.left:
      self.__fill_left()
    return self.left.pop()

  def popright(self):
    if not self.right:
      self.__fill_right()
    return self.right.pop()

  def __len__(self):
    return len(self.left) + len(self.right)

  def __getitem__(self, i):
    if i >= len(self.left):
      return self.right[i-len(self.left)]
    else:
      return self.left[-(i+1)]

  def __fill_right(self):
    x = len(self.left)//2
    self.right.extend(self.left[0:x])
    self.right.reverse()
    del self.left[0:x]

  def __fill_left(self):
    x = len(self.right)//2
    self.left.extend(self.right[0:x])
    self.left.reverse()
    del self.right[0:x]

rawDescription: O(1) indexable deque of integers in Python


code:
import requests

r = requests.get("http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js")

rawDescription: Parsing through the HTML and scripts on a webpage using python?


code:
def registering(self, username, email, password, passwordCheck, phone):
    with open('static/test.jpg') as test:
        imgStringIO = StringIO(test.read())

    return self.app.post('/registering',
        content_type='multipart/form-data',
        data=dict(
            {'file': (imgStringIO, 'test.jpg')},
            username=username,
            email=email,
            password=password,
            passwordCheck=passwordCheck,
            phone=phone
        ), follow_redirects=True
    )

def test_03_registering(self):
    rv = self.registering('TestUser', 'test@test.com', 'passwordTest', 'passwordTest', '900102030')
    assert 'Registered Successfully' in rv.data

rawDescription: Can't unit-test image upload with python


code:
import random
import threading

def main():
    lock = CustomRLock()
    if random.randrange(2):
        lock.acquire()
    print('Lock was', end='')
    if not lock.acquired:
        print(' not', end='')
    print(' acquired.')

class CustomRLock(threading._PyRLock):

    @property
    def acquired(self):
        return bool(self._count)

if __name__ == '__main__':
    main()

rawDescription: how to check if RLock in python 3 is acquired by any thread

description: Check if RLock in python 3 is acquired by any thread


rawDescription: Subsetting a string based on particular values


code:
from xml.dom import minidom

doc = minidom.Document()

ele = doc.createElement("element")
doc.appendChild(ele)
ele.attributes['xml:id']= "something"

print (ele.toxml())

rawDescription: How to create an xml attribute with special characters using Python?

description: Create an xml attribute with special characters using Python


code:
if not (args.process or args.upload):
    parser.error('No action requested, add -process or -upload')

rawDescription: Python argparse: Make at least one argument required


code:
def compare(directory1, directory2):
    for file1 in os.listdir(directory1):
        for file2 in os.listdir(directory2):
            if "some" in file1 and "some" in file2:
                print("found")

rawDescription: Comparing names of two files in different directories


code:
class TestBlobModel(ndb.Model):
    img = ndb.BlobProperty()

class Test(webapp2.RequestHandler):
    def get(self):
        image_id = self.request.get('id')
        if image_id:
            m = TestBlobModel.get_by_id(long(image_id))
            self.response.headers['content-type'] = 'image/png'
            self.response.out.write(m.img)
        else:
            self.response.out.write("""
                <form enctype="multipart/form-data" action="/test" method="POST">
                <input type="file" name="image" />
                <input type="submit" />
                </form>""")

    def post(self):
        img_data = self.request.get('image')
        m = TestBlobModel()
        m.img = img_data
        m.put()

        html = 'View your image' % m.key.id()
        self.response.out.write(html)

rawDescription: Error uploading image to Google App Engine


code:
def melat(lat):
    return  degrees( log ( tan ( radians( lat / 2 + 45 ) ) ))

def latit(mlt):
    return ( degrees(atan ( exp ( radians(mlt) ) )) - 45 ) * 2

def XY2LatLong(x, y):
    EQUATOR_MINUTE_LENGTH = 1851.8518519
    GeoPoint = namedtuple('GeoPoint', ('lat', 'lon'))
    Point = namedtuple('Point', ('x', 'y'))
    base = GeoPoint(55.98619572, 11.62113936)
    factor = cos( radians(base.lat) ) * EQUATOR_MINUTE_LENGTH * 60.
    return GeoPoint(
        latit(melat(base.lat) + y / factor),
        base.lon + x / factor)

rawDescription: Get lat lon for a pos when only dx dy is known from an other point


code: df.loc[:, ['salary', 'age']].fillna(-1, inplace=True)

rawDescription: pandas replace null values for a subset of columns

description: Pandas replace null values for a subset of columns


code:
allowed_domains = ["go-on.fi"]
start_urls = ["http://www.go-on.fi/tyopaikat"]

def parse(self, response):
    pages = response.xpath('//ul[@class="pagination"][last()-1]/a/text()').extract()
    page = 1
    start = 0
    while page <= pages:
        url = "http://www.go-on.fi/tyopaikat?start="+str(start)
        start += 20
        page += 1
        yield Request(url, callback=self.parse_page)

def parse_page(self,response):
    hxs = HtmlXPathSelector(response)
    items = []
    titles = hxs.select("//tr")

    for row in titles:
        item = JobData()
        item['header'] = row.select("./td[1]/a/text()").extract()
        item['link'] = row.select("./td[1]/a/@href").extract()
        items.append(item)

rawDescription: start_urls in Scrapy

description: Start_urls in Scrapy


code: r = cursor.execute("select to_char( thetime, 'DD-MON-RR HH24.MI.SSXFF' ) from my_table")

rawDescription: python - does cx_Oracle allow you to force all columns to be cx_Oracle.STRING?

description: Python - does cx_Oracle allow you to force all columns to be cx_Oracle.STRING


code:
from random import randint
from PySide.QtGui import QWidget, QPushButton, QVBoxLayout, QApplication
from facesview import FacesView

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.list = FacesView(self)
        self.button = QPushButton('Test', self)
        self.button.clicked.connect(self.handleButton)
        layout = QVBoxLayout(self)
        layout.addWidget(self.list)
        layout.addWidget(self.button)

    def handleButton(self):
        labels = []
        self.list.model().setRowCount(0)
        for row in range(10):
            labels.append(randint(0, 3))
            text = 'Item(%d) - Cluster(%d)' % (row, labels[-1])
            self.list.addItem(text, 'icon.png')
        self.list.updateFaceClusters(labels)

if __name__ == '__main__':

    import sys
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())

rawDescription: How to sort items in Qt QListview using Qt.UserRole

description: Sort items in Qt QListview using Qt.UserRole


code:
__acl__ = [
    (Deny, 'group:B', 'something'),   # If user in B, deny
    (Allow, 'group:A', 'something'),  # Else (=if user not in B), if user in A, allow
    (Deny, Everyone, 'something'),    # Else, Deny
]

rawDescription: Pyramid: ACE order in ACL

description: Pyramid: ACE order in ACL


code:
def splited_lines_generator(lines, n):
    for i in xrange(0, len(lines), n):
            yield lines[i: i + n]

for index, lines in enumerate(splited_lines_generator(['a', 'b', 'c', 'd', 'e'], 2)):
    with open('new/' + str(index) + '.txt', 'w') as f:
            f.write('\n'.join(lines))

rawDescription: How do I write in a new text file every x lines? [python]

description: Write in a new text file every x lines? [python]


code:
import unittest
import sys
class TestSample(unittest.TestCase):
    def test(self):

        # To fix the Windows forking system it's necessary to point __main__ to
        # the module we want to execute in the forked process
        old_main =                          sys.modules["__main__"]
        old_main_file =                     sys.modules["__main__"].__file__
        sys.modules["__main__"] =           sys.modules["app"]
        sys.modules["__main__"].__file__ =  sys.modules["app"].__file__

        # do your testing here

        sys.modules["__main__"] =           old_main
        sys.modules["__main__"].__file__ =  old_main_file

rawDescription: How to unit-test code that uses python-multiprocessing

description: Unit-test code that uses python-multiprocessing


code:
def two_vertical_lines (height, width):
    for x in range (0, height):
        if width > 2:
            new_width = width - 2
            mult2 = " " * new_width
            print ("*", mult2, "*", sep='')  # <-- change
        else:
             print ("**", "\n", end='')
    return

rawDescription: Spacing in python string

description: Spacing in python string


code:
if not hasattr(submodule, 'STD_INPUT_HANDLE'):
    import _submodule
    _missing = (
        'STD_INPUT_HANDLE', 'STD_OUTPUT_HANDLE', 'STD_ERROR_HANDLE', 'SW_HIDE',
        'STARTF_USESTDHANDLES', 'STARTF_USESHOWWINDOW')
    for _name in _missing:
        setattr(submodule, _name, getattr(_submodule, _name))
    del _submodule, _name, _missing

rawDescription: Unable to use subprocess constants

description: Unable to use subprocess constants


code:
import csv
import json
import oauth2 as oauth
import urllib

# I don't if any of these are actually needed?
import sys
import requests
import time

CONSUMER_KEY    = ''
CONSUMER_SECRET = ''
ACCESS_KEY      = ''
ACCESS_SECRET   = ''

class TwitterSearch:
    def __init__(self,
        ckey    = CONSUMER_KEY,
        csecret = CONSUMER_SECRET,
        akey    = ACCESS_KEY,
        asecret = ACCESS_SECRET,
        query   = 'https://api.twitter.com/1.1/search/tweets.{mode}?{query}'
    ):
        consumer     = oauth.Consumer(key=ckey, secret=csecret)
        access_token = oauth.Token(key=akey, secret=asecret)
        self.client  = oauth.Client(consumer, access_token)
        self.query   = query

    def search(self, q, mode='json', **queryargs):
        queryargs['q'] = q
        query = urllib.urlencode(queryargs)
        return self.client.request(self.query.format(query=query, mode=mode))

def write_csv(fname, rows, header=None, append=False, **kwargs):
    filemode = 'ab' if append else 'wb'
    with open(fname, filemode) as outf:
        out_csv = csv.writer(outf, **kwargs)
        if header:
            out_csv.writerow(header)
        out_csv.writerows(rows)

def main():
    ts = TwitterSearch()
    response, data = ts.search('@gmail.com', result_type='recent')
    js = json.loads(data)

    # This _should_ work, based on sample data from
    # https://dev.twitter.com/docs/api/1.1/get/search/tweets
    messages = ([msg['created_at'], msg['txt'], msg['user']['id']] for msg in js.get('statuses', []))

    write_csv('twitter_gmail.csv', messages, append=True)

if __name__ == "__main__":
    main()

rawDescription: Twitter Python Json to CSV

description: Twitter Python Json to CSV


code:
import inspect
inspect.isclass(inspect)
inspect.isclass(inspect.ArgInfo)

rawDescription: How to identify whether a variable is a class or an object

description: Identify whether a variable is a class or an object


rawDescription: Delete rows based on multiple conditions; including other column conditionals


code:
objDict = {obj[0]: Place(*obj) for obj in objdict['places']}
globals().update(objDict)

rawDescription: How can I instantiate a variable?

description: Instantiate a variable


code:
import unittest

class Tester(unittest.TestCase):
    def test_true(self):
        self.assertTrue(True)

if __name__ == "__main__":
    unittest.main(module='foobar')

rawDescription: unittest.py doesn't play well with trace.py - why?

description: Unittest.py doesn't play well with trace.py - why


code:
for needle in needles:
    if needle in haystack:
        found.append(needle)

rawDescription: Search haystack for several equal length needles (Python)


code:
import tkFileDialog

old_file = tkFileDialog.askopenfilename(filetypes=[('.csvfiles', '.csv')],
                                        title='Old file selected first')

rawDescription: Python tkinter change 'open' in the title bar


code:
with open('path/to/input') as infile, open('path/to/output', 'w') as outfile:
    for line in infile:
        if not line.count('\t'):
            gene = line.strip()
            continue
        outfile.write(gene + '\t')
        outfile.write(line)

rawDescription: Change string order in a file

description: Change string order in a file


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

if __name__=='__main__':

    rows = 144
    columns = 175

    camera_image = np.loadtxt(r'path/to/filename')
    if(not camera_image.size == rows * columns):
        raise Exception('Size mismatch!')

    my_image = np.reshape(camera_image, (rows, columns)) / 255.0

    figure = plt.figure()
    axes = figure.add_subplot(1, 1, 1)

    axes.imshow(my_image)

    plt.show()

rawDescription: Python: raw image data to grayscale

description: Python: raw image data to grayscale


code:
from datetime import timedelta

prev_date= None
for line in InFile:
    datetimeobject = datetime.strptime(line,'%Y%m%d')
    if datetimeobject-timedelta(days=1) != prev_date:
        outfile.write('things')
    prev_date= datetimeobject

rawDescription: How to compare next datetime in a list with the current one

description: Compare next datetime in a list with the current one


code:
from sympy import *
a = 2*Symbol('x')
isinstance(a, Mul)
a = 2 + Symbol('x')
isinstance(a, Add)

rawDescription: how to compare sympy datatypes

description: Compare sympy datatypes


code: turtle.goto( turtle.pos() + (15,-15) )

rawDescription: Python, move turtle to relative coordinates


code:
f = open("a.txt", "r")
f.readline(size)

rawDescription: Limiting amount read using readline

description: Limiting amount read using readline


rawDescription: Select element within a list/tuple

description: Select element within a list/tuple


code:
import wx

########################################################################
class TestPanel(wx.Panel):

    #----------------------------------------------------------------------
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)

        e = wx.FontEnumerator()
        e.EnumerateFacenames()
        elist= e.GetFacenames()

        elist.sort()

        s1 = wx.StaticText(self, -1, "Face names:")

        self.lb1 = wx.ListBox(self, -1, wx.DefaultPosition, (200, 250),
                              elist, wx.LB_SINGLE)

        self.Bind(wx.EVT_LISTBOX, self.OnSelect, id=self.lb1.GetId())

        self.txt = wx.StaticText(self, -1, "Sample text...", (285, 50))

        row = wx.BoxSizer(wx.HORIZONTAL)
        row.Add(s1, 0, wx.ALL, 5)
        row.Add(self.lb1, 0, wx.ALL, 5)
        row.Add(self.txt, 0, wx.ALL|wx.ADJUST_MINSIZE, 5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(row, 0, wx.ALL, 30)
        self.SetSizer(sizer)
        self.Layout()

        self.lb1.SetSelection(0)
        self.OnSelect(None)
        wx.FutureCall(300, self.SetTextSize)

    #----------------------------------------------------------------------
    def SetTextSize(self):
        self.txt.SetSize(self.txt.GetBestSize())

    #----------------------------------------------------------------------
    def OnSelect(self, evt):
        face = self.lb1.GetStringSelection()
        font = wx.Font(28, wx.DEFAULT, wx.NORMAL, wx.NORMAL, False, face)
        self.txt.SetLabel(face)
        self.txt.SetFont(font)
        if wx.Platform == "__WXMAC__": self.Refresh()

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None,
                          title="wx.FontEnumerator Tutorial",
                          size=(800,600))
        panel = TestPanel(self)

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: wxPython -- how to use an installed font on Ubuntu

description: WxPython -- how to use an installed font on Ubuntu


code:
#!/usr/bin/python

import gobject;
gobject.threads_init()
import gst;

if __name__ == "__main__":
    # First create our pipeline
    pipe = gst.Pipeline("mypipe")

    # Create a software mixer with "Adder"
    adder = gst.element_factory_make("adder","audiomixer")
    pipe.add(adder)

    # Gather a request sink pad on the mixer
    sinkpad1=adder.get_request_pad("sink%d")

    # Create the first buzzer..
    buzzer1 = gst.element_factory_make("audiotestsrc","buzzer1")
    buzzer1.set_property("freq",1000)
    pipe.add(buzzer1)
    # .. and connect it's source pad to the previously gathered request pad
    buzzersrc1=buzzer1.get_pad("src")
    buzzersrc1.link(sinkpad1)

    # Add some output
    output = gst.element_factory_make("autoaudiosink", "audio_out")
    pipe.add(output)
    adder.link(output)

    # Start the playback
    pipe.set_state(gst.STATE_PLAYING)

    raw_input("1kHz test sound. Press <ENTER> to continue.")

    # Get an another request sink pad on the mixer
    sinkpad2=adder.get_request_pad("sink%d")

    # Create an another buzzer and connect it the same way
    buzzer2 = gst.element_factory_make("audiotestsrc","buzzer2")
    buzzer2.set_property("freq",500)
    pipe.add(buzzer2)

    buzzersrc2=buzzer2.get_pad("src")
    buzzersrc2.link(sinkpad2)

    # Start the second buzzer (other ways streaming stops because of starvation)
    buzzer2.set_state(gst.STATE_PLAYING)

    raw_input("1kHz + 500Hz test sound playing simoultenously. Press <ENTER> to continue.")

    # Before removing a source, we must use pad blocking to prevent state changes
    buzzersrc1.set_blocked(True)
    # Stop the first buzzer
    buzzer1.set_state(gst.STATE_NULL)
    # Unlink from the mixer
    buzzersrc1.unlink(sinkpad2)
    # Release the mixers first sink pad
    adder.release_request_pad(sinkpad1)
    # Because here none of the Adder's sink pads block, streaming continues

    raw_input("Only 500Hz test sound. Press <ENTER> to stop.")

rawDescription: Adding and removing audio sources to/from GStreamer pipeline on-the-go


code:
import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey

fontsize= 20
def circle(filename):
    color = 'w'
    pos = [2, 1]
    fig = plt.figure(figsize=(8, 9))
    ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], frameon=False)
    sankey = Sankey(ax=ax, gap=0.5, scale=1.0/pos[0])

    sankey.add(patchlabel='\nFirst', facecolor='darkslateblue',
               flows=[pos[1], -pos[1]],
               labels=[None, None],
               pathlengths=[0.5, 0.25],
               orientations=[-1, -1], connect=(1, 0))
    sankey.add(patchlabel='\n\n\n\n\n\nSecond', facecolor='blueviolet',
               flows=[pos[1], -pos[1]],
               labels=[None, None],
               pathlengths=[0.5, 0.25],
               orientations=[0, -1], prior = 0, connect=(1, 0))
    sankey.add( facecolor='cornflowerblue',
                flows=[pos[1], -pos[1]],
                labels=[None, None],
                pathlengths=[0.22, 0.75],
                orientations=[0, -1], prior = 1, connect=(1, 0))

    add_horizontal_text(ax, color)
    diagrams = sankey.finish()
    for diagram in diagrams:
        diagram.text.set_fontweight('bold')
        diagram.text.set_fontsize(fontsize)
        diagram.text.set_color(color)

    diagrams[1].text.set_fontsize(fontsize-2)
    plt.savefig(filename)

    return

def add_horizontal_text(ax, color):
    x = -0.5
    y = -1.68
    offset = .1
    ax.text(x, y, 'Third', fontsize=fontsize, weight='bold', ha='center', color=color)
    return

circle('CircularArrow.png')

rawDescription: Circular Arrow Flow Chart in Python

description: Circular Arrow Flow Chart in Python


code:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy.special import binom

def Bernstein(n, k):
    """Bernstein polynomial.

    """
    coeff = binom(n, k)

    def _bpoly(x):
        return coeff * x ** k * (1 - x) ** (n - k)

    return _bpoly

def Bezier(points, num=200):
    """Build Bézier curve from points.

    """
    N = len(points)
    t = np.linspace(0, 1, num=num)
    curve = np.zeros((num, 2))
    for ii in range(N):
        curve += np.outer(Bernstein(N - 1, ii)(t), points[ii])
    return curve
xp = np.array([2,3,4,5])
yp = np.array([2,1,4,0])
x, y = Bezier(list(zip(xp, yp))).T

plt.plot(x,y)
plt.plot(xp,yp,"ro")
plt.plot(xp,yp,"b--")

plt.show()

rawDescription: Splines with Python (using control knots and endpoints)


code:
import wx

########################################################################
class TestFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Test")

        panel = wx.Panel(self)
        self.radios = wx.RadioBox(panel, label="Choices",
                                  choices = ["None", "Accept", "Reject"])

        button = wx.Button(panel, label="Run")
        button.Bind(wx.EVT_BUTTON, self.onBtn)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.radios, 0, wx.ALL, 5)
        sizer.Add(button, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onBtn(self, event):
        """"""
        btn = event.GetEventObject()
        btn.SetLabel("Running")
        radio_value = self.radios.GetStringSelection()
        if radio_value == "None":
            self.showMsg("Error", "Please Choose 'Accept' or 'Reject'!")
        elif radio_value == "Accept":
            self.showMsg("Message", "Thank you for accepting!")
        else:
            self.showMsg("Message", "We're sorry, but you cannot continue the install")

    #----------------------------------------------------------------------
    def showMsg(self, title, msg):
        """"""
        dlg = wx.MessageDialog(None, msg, title, wx.OK | wx.ICON_QUESTION)
        dlg.ShowModal()
        dlg.Destroy()

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

rawDescription: Call functions from within a wxPython event handler


code:
a = np.array([[1,0,0,1,0,0,0,1],[1,1,1,1,0,0,1,1]])
import scipy.stats
most, mostcc = scipy.stats.mode(a, axis=1)
most
mostcc

rawDescription: How to find most occurred element in each row of a multidimentional array?

description: Find most occurred element in each row of a multidimentional array


rawDescription: Django objects queryset filter by matching datetime


code:
def mindistance(data1,data2,variables,L):
    start_time = time.time()
    pred_values=[]
    test1=[]
    for index2, row2 in data2[variables].iterrows():
        test=list( data1[variables].apply( distance, args=(row2,L,), axis=1 ) )
        index=test.index(min(test))
        #print index
        b=round(data1['e'].iloc[index],2)
        pred_values.append(b)

    #print pred_values
    #print len(pred_values)
    return "Time taken: " + str(round(time.time() - start_time,2)) + " seconds"

rawDescription: Function taking a long time to run in Python - Efficiency


code:
#!/usr/bin/env python3
from PIL import Image

def quantizetopalette(silf, palette, dither=False):
    """Convert an RGB or L mode image to use a given P image's palette."""

    silf.load()

    # use palette from reference image
    palette.load()
    if palette.mode != "P":
        raise ValueError("bad mode for palette image")
    if silf.mode != "RGB" and silf.mode != "L":
        raise ValueError(
            "only RGB or L mode images can be quantized to a palette"
            )
    im = silf.im.convert("P", 1 if dither else 0, palette.im)
    # the 0 above means turn OFF dithering
    return silf._makeself(im)

palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
palimage = Image.new('P', (16, 16))
palimage.putpalette(palettedata * 64)
oldimage = Image.open("School_scrollable1.png")
newimage = quantizetopalette(oldimage, palimage, dither=False)
newimage.show()

rawDescription: Convert image to specific palette using PIL without dithering


rawDescription: How to merge pandas calculated series into pandas dataframe

description: Merge pandas calculated series into pandas dataframe


code:
if len(abc) > x and abc[x] == 'fish':
    dostuff
else:
    dootherstuff

rawDescription: How to make nested IF statements neater

description: Make nested IF statements neater


code:
import math
while True:
    num = int(input("Give me a number: "))  # Get the number
    if(1<=num<=10):
        break;
list_num = [x for x in range(1, num + 1)]  # A list with every second number
print([math.factorial(z) for z in list_num[0::2]])  # The factorial of every element in that list.

rawDescription: Python save descending number from input in a list


code:
from distutils import util
util.get_platform()
import sys
'%s.%s' % sys.version_info[:2]

rawDescription: How to get the arch string that distutils uses for builds?

description: Get the arch string that distutils uses for builds


rawDescription: How to change target directory of an external command in a Python script

description: Change target directory of an external command in a Python script


code:
def launch(self):
    frame, lineno = self.stack[self.curindex]
    filename = self.canonic(frame.f_code.co_filename)
    if exists(filename):
        command = 'subl -b "%s:%d"' % (filename, lineno)
        os.system(command)

def preloop(self):
    launch(self)

def precmd(self, line):
    launch(self)
    return line

rawDescription: Interactively watch code in python debugger pdb


code:
print('Hello world!')
input()

rawDescription: Python Hello World keeps closing itself

description: Python Hello World keeps closing itself


code:
from django.core.files.uploadedfile import SimpleUploadedFile

my_model.file_field = SimpleUploadedFile('best_file_eva.txt', 'these are the file contents!')

rawDescription: Django test FileField using test fixtures


code:
from PyQt4 import QtGui, QtCore
import threading
import time

class MyThread(threading.Thread):
    def __init__(self, *args):
        threading.Thread.__init__(self, *args)

        self.job_done = threading.Event()
        self.qt_object = QtCore.QObject()

    def end_job(self):
        self.job_done.set()

    def run(self):
        while not self.job_done.is_set():
            time.sleep(1)

        QtCore.QObject.emit(self.qt_object, QtCore.SIGNAL("job_done"))

th = MyThread()
th.start()

app = QtGui.QApplication([])

w = QtGui.QWidget()
btn = QtGui.QPushButton('click me to exit', w)
QtGui.QVBoxLayout(w)
w.layout().addWidget(btn)

def btn_clicked():
    th.end_job()

QtCore.QObject.connect(btn, QtCore.SIGNAL("clicked()"), btn_clicked)

QtCore.QObject.connect(th.qt_object, QtCore.SIGNAL("job_done"), w.close)

w.show()

app.exec_()

rawDescription: emitting signal when python thread finishes to close the Qt Gu that intiated the thread

description: Emitting signal when python thread finishes to close the Qt Gu that intiated the thread