code:
#myscript.py
from scrapy.item import Item, Field
from scrapy import Spider

class MyItem(Item):
    title = Field()
    link = Field()

class MySpider(Spider):

     start_urls = ['http://www.example.com']
     name = 'samplespider'

     def parse(self, response):
          item = MyItem()
          item['title'] = response.xpath('//h1/text()').extract()
          item['link'] = response.url
          yield item

rawDescription: A web crawler in a self-contained python file


code:
class FooBarProtocol(protocol.DatagramProtocol):
    def startProtocol(self):
        self.transport.getHost().host   # => 0.0.0.0
        self.transport.connect(self.dstHost, self.dstPort)
        self.transport.getHost().host   # => 192.168.1.102

rawDescription: Twisted: source IP address for outbound connections


code:
def letter_count(filename):
    count = 0
    letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    with open(filename) as f:
        for line in f:
            for char in line:
                if char in letters:
                    count += 1
    return count

rawDescription: Function that takes a string representing a filename as an argument. Function returns number of letters in the txt file


code:
import maya.OpenMaya as OpenMaya

def barycentricInterp(vecA, vecB, vecC, vecP):
    '''
    Calculates barycentricInterpolation of a point in a triangle.

    :param vecA - OpenMaya.MVector of a vertex point.
    :param vecB - OpenMaya.MVector of a vertex point.
    :param vecC - OpenMaya.MVector of a vertex point.
    :param vecP - OpenMaya.MVector of a point to be interpolated.

    Returns list of 3 floats representing weight values per each point.
    '''
    v0 = vecB - vecA
    v1 = vecC - vecA
    v2 = vecP - vecA

    d00 = v0 * v0
    d01 = v0 * v1
    d11 = v1 * v1
    d20 = v2 * v0
    d21 = v2 * v1
    denom = d00 * d11 - d01 * d01
    v = (d11 * d20 - d01 * d21) / denom
    w = (d00 * d21 - d01 * d20) / denom
    u = 1.0 - v - w

    return [u, v, w]

rawDescription: Joint Weight Interpolation Maya

description: Joint Weight Interpolation Maya


code: subprocess.Popen('find / -name "hello.txt" -exec echo {} \;', shell=True)

rawDescription: bash query through Python subprocess, bash variables wont expand

description: Bash query through Python subprocess, bash variables wont expand


code:
def run(self):
    if self.command is not None:
        self.process = Popen( self.command.split(), shell=False, stdout=PIPE, stderr=PIPE)

def stop(self):
    if self.process is not None:
        self.process.terminate()

rawDescription: stopping a python thread waiting for popen?

description: Stopping a python thread waiting for popen


code:
import pandas as pd
df = pd.read_csv('test.cvs', parse_dates=['d1', 'd2', 'd3'])
df.ix[2, 'd1'] = None
df.ix[1, 'd2'] = None
df.ix[4, 'd3'] = None
df
df.min(axis=1)

rawDescription: Finding the min date in a Pandas DF row and create new Column


code:
from Products.contentmigration.walker import CustomQueryWalker

def getImagesToPhotosMigrationWalker(self, query):
    return CustomQueryWalker(self, ImagesToPhotosMigrator, query)

def migrateImages(self):
    walker = getImagesToPhotosMigrationWalker(self, {'path' : '/youfolder'})
    walker.go()
    return walker.getOutput()

rawDescription: Content Migration for a Specific Folder

description: Content Migration for a Specific Folder


code:
import csv

lstName = ['a', 'b', 'c', 'd', 'e', 'etc']

with open('file.csv', 'r+') as f:
    reader = csv.reader(f, delimiter='\t')
    out_lines = [row + [lstName[i]] for i, row in enumerate(reader)]
    f.seek(0)    # set file position to the beginning of the file
    csv.writer(f, delimiter='\t').writerows(out_lines)

rawDescription: Add data to rows in csv file python

description: Add data to rows in csv file python


code:
pal= dict(M='blue',F='red')
g = sns.FacetGrid(ngrpdf, col='CCS_Category_ICD9',hue='Gender', palette=pal,hue_order=['M','F'], col_wrap=4)
g = (g.map(plt.scatter,"Admit_Month","count").add_legend())

rawDescription: Getting facet grid plot out of Pandas dataframe


code:
import numpy as np
import pylab as plt

X = np.linspace(0,5,100)
Y1 = X + 2*np.random.random(X.shape)
Y2 = X**2 + np.random.random(X.shape)

plt.scatter(X,Y1,color='k')
plt.scatter(X,Y2,color='g')
plt.show()

rawDescription: Python matplotlib superimpose scatter plots


code:
select(
    [
        bar.c.foo,
        func.count().over().label('full_count'),
    ],
    ...
)

rawDescription: Using the OVER window function in SQLAlchemy


code:
import os, os.path
# If you're on Python 2.5 or newer, use 'with'
# needs 'from __future__ import with_statement' on 2.5
fh=open('MyHtmlFile.html','w')
fh.write('\r\n<title>Lots of tables</title>\r\n\r\n')
# this will recursively descend the tree
for dirpath, dirname, filenames in os.walk(rootFolder):
    for filename in filenames:
        # again, use 'with' on Python 2.5 or newer
        infile = open(os.path.join(dirpath, filename))
        # this will format the lines and join them, then format them into the table
        # If you're on Python 2.6 or newer you could use 'str.format' instead
        fh.write('
\r\n%s\r\n
' %
                     '\r\n'.join('
%s
	' % line for line in infile))
        infile.close()
fh.write('\r\n')
fh.close()

rawDescription: pythonic way to access data in file structures

description: Pythonic way to access data in file structures


code:
import csv
import fnmatch
import os

# load the csv with entries
with open('file_with_entries.csv','r') as f:
    readr = csv.reader(f)
    data = []
    for row in readr:
        data.extend(row)

# find csv files
rootdir = os.getcwd() # could be anywhere
matches = []
for root, dirs, files in os.walk(rootdir):
    for filename in fnmatch.filter(files, '*.csv'):
        matches.append(os.path.join(root, filename))

# find occurences of entry in each file
for eachcsv in matches:
    with open(eachcsv, 'r') as f:
        text = f.read()
        for entry in data:
            if entry in text:
                print("found %s in %s" % (entry,eachcsv))

rawDescription: Searching in files and folder is extremely slow in Python


code:
num = int(input("Type numerator: "))
den = int(input("Type denominator: "))
whole_part = num // den
fract_part = num % den
print("Result: {} and {}/{}".format(whole_part, fract_part, den))

rawDescription: How to convert improper fraction to mixed number?

description: Convert improper fraction to mixed number


code:
lstobj = yourdict['A']
lstobj[1][1] = 'Sue'
# explicit set to notify `yourdict`
yourdict['A'] = lstobj

rawDescription: Overriding __setitem__ in Python dict subclass doesn't work for setting values of items inside


code:
dates = {
    'date1': yesterday,
    'date2': today,
    'date3': tomorrow,
}

times = {
    'time1': morning,
    'time2': afternoon,
    'time3': evening,
}

rawDescription: How do I have user's response to raw_input( ) access a string of the same name?

description: Have user's response to raw_input( ) access a string of the same name


code: target = table.sql('select * where sales < 1000.00')

rawDescription: Null and Sorting

description: Null and Sorting


code:
find = "123/456/x".split('/')
find = find.pop() # remove 'x' from the string
find = "/".join(find)
query_str = "SELECT id, path FROM portfolio WHERE path LIKE '%s'" % (find, )

rawDescription: How do I increment a column variable in postgres where inputs might change the beginning of the variable?

description: Increment a column variable in postgres where inputs might change the beginning of the variable


code:
from sqlalchemy import event
from sqlalchemy import DDL
event.listen(
    Article.__table__,
    "after_create",
    DDL("ALTER TABLE %(table)s AUTO_INCREMENT = 1001;")
)

rawDescription: Setting SQLAlchemy autoincrement start value


code: new_dict = {my_mod(key): my_dict[key] for key in my_dict}

rawDescription: Modifying Python dictionary keys

description: Modifying Python dictionary keys


code: pos = text.index(INSERT)

rawDescription: Is it possible to get numeric index (position) of marks in tkinter text?


code:
def OnCC(self, evt):
    data = wx.ColourData()
    data.SetChooseFull(True)

    # set the first custom color (index 0)
    data.SetCustomColour(0, (255, 170, 128))
    # set indexes 1-N here if you like.

    # set the default color in the chooser
    data.SetColour(wx.Colour(128, 255, 170))

    # construct the chooser
    dlg = wx.ColourDialog(self, data)

    if dlg.ShowModal() == wx.ID_OK:
        # set the panel background color
        color = dlg.GetColourData().Colour
        self.panel.SetBackgroundColour(color)
    dlg.Destroy()

rawDescription: Why doesn't this work (wxpython/color dialog)


code:
class Node:
    """Referential Structure used to create new nodes"""
    def __init__(self, data):
        """Constructor."""
        self.data = data
        self.left = None
        self.right = None

rawDescription: Traversing the elements of BST

description: Traversing the elements of BST


code:
from Tkinter import *
window = Tk()

def printVal():
    canvas = Canvas(window, width = 100, height = 100)
    canvas.grid(row = 0, column = 0, columnspan = 2)
    string = entryval.get()
    canvas.create_text(50,50, text = string)

entryval = Entry(window)
entryval.grid(row = 1, column = 0)
button = Button(window, text = "Print", command = printVal)
button.grid(row = 1, column = 1)

window.mainloop()

rawDescription: How to print the value entered in entry widget into canvas

description: Print the value entered in entry widget into canvas


code:
def get_letter_grade(score):
    # define your own rules here

    if score<50:
        return "F"
    if score <60:
        return "D"
    if score<70:
        return "C"
    if score<80:
        return "B"
    return "A"

with open(args.source, 'r') as source_file, open(args.output, 'w') as output_file:
    writer = csv.writer(output_file, lineterminator='\n')
    reader = csv.reader(soure_file, delimiter=',')
    headers = next(reader)
    headers.append('Letter Grade')

    for row in reader:
        row = [i.strip().replace("'", '') for i in row]
        grade = int(row[1])
        letter_grade = get_letter_grade(grade)
        writer.writerow(row + [letter_grade])

rawDescription: Write to existing column based on values in another column


code:
from tkinter import *
from tkinter import ttk

class Gui(Tk):

    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        initframe = ttk.Frame(self, padding="3 3 12 12")
        initframe.grid(column=0, row=0, sticky=(N, W, E, S))
        initframe.columnconfigure(0, weight=1)
        initframe.rowconfigure(0, weight=1)
        self.resizable(False, False)

        ttk.Button(initframe, text="Check Info", command=self.check_info).grid(column=2, row=2, sticky=(W, E))
        ttk.Button(initframe, text="Run Tests" ).grid(column=4, row=2, sticky=(W, E))

        for child in initframe.winfo_children():
            child.grid_configure(padx=20, pady=20)

        self.update()
        self.geometry(self.geometry())

        self.bind('<Return>', self.check_info_enter)

    def goback(self):
        self.info_frame.destroy()

    def check_info(self):
        self.info_frame = ttk.Frame(self, padding="3 3 12 12")
        self.info_frame.grid(column=0, row=0, sticky=(N, W, E, S))
        self.info_frame.columnconfigure(0, weight=1)
        self.info_frame.rowconfigure(0, weight=1)
        self.resizable(False, False)

        ttk.Button(self.info_frame, text="Return Home", command=self.goback).grid(column=2, row=2, sticky=(W, E))

    def check_info_enter(self, event):
        self.update()

if __name__ == "__main__":
    app = Gui(None)
    app.title('Initial')
    app.mainloop()

rawDescription: Returning to initial frame after creating new frame using tkinter


code:
sNgrams = '|'.join(('\s+'.join(re.escape(gram)
                   for gram in nGram.split()))
                   for nGram in reversed(sorted(aNgrams, key=len)))
sNgrams
reNgrams = re.compile(r"(?<!\w)(?=(" + sNgrams + r")(?!\w))",
                        flags=re.UNICODE|re.IGNORECASE)
reNgrams.findall(doc)

rawDescription: Matching multiple overlapping n-grams with a single regular expression


code:
class ItemDetailInline(admin.TabularInline):
    model = ItemDetail

class ItemAdmin(admin.ModelAdmin):
    inlines = [
        ItemDetailInline,
    ]

rawDescription: Django admin application for master/detail page


code:
id_int = re.findall('r\d+',newstr)
#                    ^
# should be:
id_int = re.findall(r'\d+',newstr)

rawDescription: re.findall working in console but not in script?

description: Re.findall working in console but not in script


code:
def wait_for_internet_connection():
    while True:
        try:
            response = urllib2.urlopen('http://74.125.113.99',timeout=1)
            return
        except urllib2.URLError:
            pass

def main():
    ...

wait_for_internet_connection()
main()

rawDescription: python wait until connection active

description: Python wait until connection active


rawDescription: Remove trailing zeroes python but with small restriction


code:
from myapp.models import MyModel
import urllib2

with open("sourceListOfURLs.txt", "r" ) as source:
    for aLine in source:
        for this, the, the_other in someGenerator( aLine ):
            object= MyModel.objects.create( field1=this, field2=that, field3=the_other )
            object.save()

def someGenerator( url ):
    # open the URL with urllib2
    # parse the data with BeautifulSoup
    yield this, that, the_other

rawDescription: How to populate sqlite3 in django?

description: Populate sqlite3 in django


code:
import matplotlib.pyplot as plt
from scipy.interpolate import UnivariateSpline
import numpy as np

x = range(30)
y = [1, 1, 1, 2, 1, 1, 1, 2, 4, 5, 8, 12, 13, 14, 12, 11, 9, 6, 5, 4, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1 ]

s = UnivariateSpline(x, y, s=5)
xs = np.linspace(0, 29, 100)
ys = s(xs)

plt.plot(x, y, 'o')
plt.plot(xs, ys)
plt.show()

rawDescription: Is there a way to plot a curve of best fit without function? Python


code:
data = [('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})], [('uid=xxxxxx,ou=People,dc=xxxxxxx,dc=eu', {'departmentcode': ['xxxxx']})]
departmentcode = []
for elem in data:
    departmentcode.append(elem[0][1]['departmentcode'])

rawDescription: Get custom attribute from python ldap query


code:
from itertools import izip
def rotateItems(dictionary):
  if dictionary:
    keys = dictionary.iterkeys()
    values = dictionary.itervalues()
    firstkey = next(keys)
    dictionary = dict(izip(keys, values))
    dictionary[firstkey] = next(values)
  return dictionary
firstdict
rotateItems(firstdict)

rawDescription: Rotate dictionary keys in python

description: Rotate dictionary keys in python


code:
from django.db.backends.signals import connection_created
from django.dispatch import receiver

@receiver(connection_created)
def extend_sqlite(connection=None, **kwargs):
    connection.connection.create_function("least", 2, min)
    connection.connection.create_function("greatest", 2, max)

rawDescription: How to extend SQLite with Python functions in Django?

description: Extend SQLite with Python functions in Django


code:
...
b = s.post('http://my.server.com/netdot/NetdotLogin', data={'destination' : '/netdot/', 'credential_0' : 'admin', 'credential_1' : 'password'})
...

rawDescription: How to have Python3 requests library send actual Content-Length with post request instead of 0?

description: Have Python3 requests library send actual Content-Length with post request instead of 0


code:
class UniqueDict(dict):
    def __setitem__(self, key, value):
        if key not in self:
            dict.__setitem__(self, key, value)
        else:
            raise KeyError("Key already exists")

rawDescription: How can I force a dictionary in python to only have unique keys?

description: Force a dictionary in python to only have unique keys


code:
ax.grid(False)
for a in (ax.w_xaxis, ax.w_yaxis):
    for t in a.get_ticklines()+a.get_ticklabels():
        t.set_visible(False)
    a.line.set_visible(False)
    a.pane.set_visible(False)

rawDescription: Invisible Axes Trisurf Plot Python

description: Invisible Axes Trisurf Plot Python


code:
data = {'PLAYER': 'Oreo', 'STRENGTH': '11.75'}
data['STRENGTH'] = float(data['STRENGTH'])
data
data['STRENGTH'] += 1
data

rawDescription: How to append or change data within a file

description: Append or change data within a file


rawDescription: How to print particular JSON value in Python?

description: Print particular JSON value in Python


code: run('echo "{0} ALL=(ALL) ALL" >> /etc/sudoers'.format(env.user))

rawDescription: Fabric Sudo No Password Solution

description: Fabric Sudo No Password Solution


code:
def dict_interdiff(d1, d2):
    # symmetric difference, keys in either d1 or d2 but not both.
    sym_diff = d1.viewkeys() ^ d2
    # intersection, keys that are common to both d1 and d2.
    intersect = d1.viewkeys() & d2
    # apply f on values of the keys that common to both dicts.
    a = {k: f(d1[k], d2[k]) for k in intersect}
    b = {k: d1[k] for k in sym_diff & d1.viewkeys()}
    # add key/value pairings from d2 using keys that appear in sym_diff
    b.update({k: d2[k] for k in sym_diff & d2.viewkeys()})
    return a,b

rawDescription: Understanding intersect and difference of dictionaries in python


code:
import numpy as np
import Image
from scipy import ndimage
import matplotlib.pyplot as plt

roi = 2.5
peak_threshold = 120

im = Image.open('Q766c.png');
image = im.copy()

size = 2 * roi + 1
image_max = ndimage.maximum_filter(image, size=size, mode='constant')
mask = (image == image_max)
image *= mask

# Remove the image borders
image[:roi] = 0
image[-roi:] = 0
image[:, :roi] = 0
image[:, -roi:] = 0

# Find peaks
image_t = (image > peak_threshold) * 1

# get coordinates of peaks
f = np.transpose(image_t.nonzero())

# Show
img = plt.imshow(np.asarray(im))
plt.plot(f[:, 1], f[:, 0], 'o', markeredgewidth=0.45, markeredgecolor='b', markerfacecolor='None')

plt.axis('off')
plt.savefig('local_max.png', format='png', bbox_inches='tight')
plt.show()

rawDescription: Image Analysis: Finding proteins in an image


code:
class CalculateCost(webapp.RequestHandler):
    def get(self):
        shipping_method = self.request.get('shipping_method')
        # calculate your cost here
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('%d' % cost)

rawDescription: Partial page updates

description: Partial page updates


code: newname = re.sub('  +', ' ', oldname)

rawDescription: Creating a string replacement loop in Python


code:
from django import forms
from django.contrib import admin
from flightdeck.models import Equipment

class EquipmentAdminForm(forms.ModelForm):
    reason = forms.CharField()

    class Meta:
        model = Equipment

rawDescription: Django ModelAdmin extra input field not from Model


code:
from __future__ import division
from random import shuffle

def make_graph(depth, graph, start, end, turns):
    # add points to graph
    graph.add(start)
    graph.add(end)

    if depth > 0:
        # unpack input values
        fromtime, fromvalue = start
        totime, tovalue = end

        # calcualte differences between points
        diffs = []
        last_time, last_val = fromtime, fromvalue
        for t, v in turns:
            new_time = fromtime + (totime - fromtime) * t
            new_val = fromvalue + (tovalue - fromvalue) * v
            diffs.append((new_time - last_time, new_val - last_val))
            last_time, last_val = new_time, new_val

        # add 'brownian motion' by reordering the segments
        shuffle(diffs)

        # calculate actual intermediate points and recurse
        last = start
        for segment in diffs:
            p = last[0] + segment[0], last[1] + segment[1]
            make_graph(depth - 1, graph, last, p, turns)
            last = p
        make_graph(depth - 1, graph, last, end, turns)

from matplotlib import pyplot
depth = 8
graph = set()
make_graph(depth, graph, (0, 0), (1, 1), [(1/9, 2/3), (5/9, 1/3)])
pyplot.plot(*zip(*sorted(graph)))
pyplot.show()

rawDescription: How to generate a fractal graph of a market in python

description: Generate a fractal graph of a market in python


code: save_as = True

rawDescription: In the Django admin interface, is there a way to duplicate an item?


code:
vals = [{'theoretical_qty': 10.0, 'product_id': 7, 'sequence': u'10', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 25, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}, {'theoretical_qty': 15.0, 'product_id': 8, 'sequence': u'3', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 25, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}, {'theoretical_qty': 21.0, 'product_id': 9, 'sequence': u'7', 'location_id': 12, 'prod_lot_id': False, 'inventory_id': 27, 'package_id': False, 'product_qty': 0, 'product_uom_id': 1, 'partner_id': False}]

vals.sort(key=lambda d: int(d.get('sequence', 0)))

from pprint import pprint
pprint(vals)

rawDescription: Sorting Python List of Dictionaries

description: Sorting Python List of Dictionaries


code:
@app.route('/wait', methods=['POST'])
def wait():
    response = twiml.Response()
    response.say("You are %s in the queue." % request.form['QueuePosition'])
    response.play("http://com.twilio.music.classical.s3.amazonaws.com/BusyStrings.mp3")
    account_sid = "*****"
    auth_token = "*****"
    client = TwilioRestClient(account_sid, auth_token)
    queue = client.queues.get(request.form['QueueSid']) #Get the queue based on SID
    friendlyName = queue.friendly_name; #Obtain the queue's Friendly Name
    client.sms.messages.create(to="+15555555555", from_="+15555555554", body="A caller is in the call queue - %(num)s in queue %(queue)s" % {"num": request.form['From'], "queue" : friendlyName}) #SMS with caller ID and queue's friendly name
    return str(response)

rawDescription: Get the name of the queue a caller is in, Twilio


code:
from scapy.all import rdpcap, wrpcap

pkts = rdpcap('h264_file.pcap')
stripped_pkts = [pkt.payload for pkt in pkts] # strip the RadioTap header; extract just its payload
wrpcap('stripped_h264_file.pcap', stripped_pkts)

rawDescription: Strip radiotap headers from a pcap file

description: Strip radiotap headers from a pcap file


code:
plist = []
for i in range(10):
    p = multiprocessing.Process(target=func)
    p.start()
    plist.append(p)
    sleep(5)
for p in plist:
    p.join()
do_something()

rawDescription: how to fetch process from python process pool

description: Fetch process from python process pool


code:
first, colon, rest = "commence:2009-11-19T19:55:00".partition(':')
print (first, colon, rest)

rawDescription: Regex to Split 1st Colon

description: Regex to Split 1st Colon


code:
cols = df.columns
num_cols = df._get_numeric_data().columns
num_cols
list(set(cols) - set(num_cols))

rawDescription: Check which columns in DataFrame are Categorical


code:
#plot pdf
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
ax1.plot(x, norm.pdf(x, mu, sigma), lw=3, c='r', label='PDF')

#RNG
y = [-3, -2, -1, 0, -1, -2, -3]
if all(y) <= 1. / (sigma * sqrt(2 * pi)) * exp(-(mu ** 2 / 2 * sigma ** 2)):
    u = sigma * np.random.randn(100) + mu
    ax2.hist(u, histtype='bar', alpha=0.5)
plt.show()

rawDescription: Histogram on Gaussian probability density function graph in Python


rawDescription: Matching characters in two Python strings


code:
from midiutil.MidiFile import MIDIFile

# create your MIDI object
mf = MIDIFile(1)     # only 1 track
track = 0   # the only track

time = 0    # start at the beginning
mf.addTrackName(track, time, "Sample Track")
mf.addTempo(track, time, 120)

# add some notes
channel = 0
volume = 100

pitch = 60           # C4 (middle C)
time = 0             # start on beat 0
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

pitch = 64           # E4
time = 2             # start on beat 2
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

pitch = 67           # G4
time = 4             # start on beat 4
duration = 1         # 1 beat long
mf.addNote(track, channel, pitch, time, duration, volume)

# write it to disk
with open("output.mid", 'wb') as outf:
    mf.writeFile(outf)

rawDescription: How can I write a MIDI file with Python?

description: Write a MIDI file with Python


code:
import pytz
import datetime
from win32com.propsys import propsys, pscon

properties = propsys.SHGetPropertyStoreFromParsingName(filepath)
dt = properties.GetValue(pscon.PKEY_Media_DateEncoded).GetValue()

if not isinstance(dt, datetime.datetime):
    # In Python 2, PyWin32 returns a custom time type instead of
    # using a datetime subclass. It has a Format method for strftime
    # style formatting, but let's just convert it to datetime:
    dt = datetime.datetime.fromtimestamp(int(dt))
    dt = dt.replace(tzinfo=pytz.timezone('UTC'))

dt_tokyo = dt.astimezone(pytz.timezone('Asia/Tokyo'))

rawDescription: Python - how to read Windows "Media Created" date (not file creation date)


code:
import os
os.getcwd()
os.chdir('..')
os.getcwd()

rawDescription: Get parent directory of a specific file

description: Get parent directory of a specific file


code:
import sys
from PySide import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        action = QtGui.QAction(self)
        action.setShortcut('Ctrl+t')
        action.triggered.connect(self.on_triggered)
        self.addAction(action)

    def on_triggered(self):
        print('triggered')

app = QtGui.QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())

rawDescription: Hide an action without disabling it

description: Hide an action without disabling it


code:
import pygame,pygbutton
from pygame.locals import *
pygame.init()

#Create the "Pre Button Press Screen"
width = 1024
height = 768
screen = pygame.display.set_mode([width,height])
pygame.display.set_caption('OLD SCREEN NAME')
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, [0,0])
pygame.display.flip()
button1 = pygbutton.PygButton((50, 50, 60, 30), '1')
button2 = pygbutton.PygButton((120, 50, 60, 30), '2')
button3 = pygbutton.PygButton((190, 50, 60, 30), '3')
buttonhasbeenpressed = False

def screenPostButtonPress():
    width = 1024
    height = 768
    screen = pygame.display.set_mode([width,height])
    pygame.display.set_caption('NEW SCREEN NAME!!!!!!!')
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((20, 20, 40))
    screen.blit(background, [0,0])
    pygame.display.flip()
    #buttons not on screen after a button has been pressed

def waitingForButtonClick():
    allButtons = [button1,button2,button3]
    buttonevent1 = button1.handleEvent(event)
    buttonevent2 = button2.handleEvent(event)
    buttonevent3 = button3.handleEvent(event)

    for b in allButtons:
        b.draw(screen)

    if 'click' in buttonevent1 or 'click' in buttonevent2 or 'click' in buttonevent3:
        return False
    return True

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    #Wait for a button to be pressed, once one has, "clear" the screen by creating a new screen
    if buttonhasbeenpressed == False and waitingForButtonClick() == False:
       buttonhasbeenpressed = True
       screenPostButtonPress()
    pygame.display.update()

rawDescription: How to remove a button created by pygbutton

description: Remove a button created by pygbutton


rawDescription: Merge output when using "or" operator in xpath


code:
from decimal import *
getcontext().prec = 6
Decimal(1) / Decimal(7)
Decimal('0.142857')
getcontext().prec = 28
Decimal(1) / Decimal(7)
Decimal('0.1428571428571428571428571429')

rawDescription: Formatting output for matrices in Python


code:
import tkinter as tk

root = tk.Tk()
text = tk.Text(root)
text.pack()

def tab(arg):
    print("tab pressed")
    text.insert(tk.INSERT, " " * 4)
    return 'break'

text.bind("<Tab>", tab)
root.mainloop()

rawDescription: Tkinter Text: Setting tab spaces not working


code: data[0] = ~ data[0] & 0xFF

rawDescription: Python3 Byte Level Bit Operations

description: Python3 Byte Level Bit Operations


code:
class Thing(db.Model):
    __tablename__ = 'thing'
    id = db.Column(db.Integer, primary_key=True)
    actions = db.relationship('Action', backref=db.backref('thing'))

class Action(db.Model):
    __tablename__ = 'action'
    id = db.Column(db.Integer, primary_key=True)
    thing_id = db.Column(db.Integer, db.ForeignKey('thing.id'))
    discriminator = db.Column('type', db.String(50))
    __mapper_args__ = {'polymorphic_on': discriminator}

class BuildAction(Action):
    __mapper_args__ = {'polymorphic_identity': 'build_action'}
    time_required = db.Column(db.Integer)

rawDescription: One-to-many relationship to multiple models


code:
@skip("skip this test")
def testSomething(self):
    pass # TODO

def testBar(self):
    self.skipTest('We need a test here, really')

def testFoo(self):
    raise SkipTest('TODO: Write a test here too')

rawDescription: How should I indicate that a test hasn't been written yet in Python?


code:
import itertools
x = itertools.permutations(range(3))
next(x)
next(x)
next(x)

rawDescription: Python how to get permutations one by one


code: im2 = im.convert('RGB').convert('P', palette=Image.ADAPTIVE)

rawDescription: png24 to png8 conversion

description: Png24 to png8 conversion


code:
import numpy as np
df1['randomNumCol'] = np.random.choice(range(1, 6), df1.shape[0])

rawDescription: Python create new column in pandas df with random numbers from range


code:
if answer.lower() == "f":
    i = i + 1

elif answer.lower() == "p":
    i = i - 1

else:
    break

rawDescription: How to browse through array in Python?

description: Browse through array in Python


code:
import itertools
from pprint import pprint
formfielddata = [
    {'value': '10.16.0.4', 'param': 'target_ips'},
    {'value': '10.16.0.6', 'param': 'target_ips_6'},
    {'param': 'target_ips_7', 'value': '10.16.0.7'},
    {'value': '10.16.0.8', 'param': 'target_ips_8'},
    {'param': 'target_ips_9', 'value': '10.16.0.9'},
    {'value': '10.16.0.10', 'param': 'target_ips_10'},
    {'value': '10.16.0.11', 'param': 'target_ips_11'},
]

preferred_order = ['param', 'value']
apiargs = {}

pprint("%s" % ([dict(zip(*[iter(
    (v for k, v in sorted(f.items(), key=lambda x:preferred_order.index(x[0])))
)]*2)) for f in formfielddata]))
[apiargs.update(dict(zip(*[iter(
    (v for k, v in sorted(f.items(), key=lambda x:preferred_order.index(x[0])))
)]*2))) for f in formfielddata]
pprint("%s" % (apiargs))

rawDescription: Python (3 if it makes a difference) dictionaries and specific list result order with generator


code:
import subprocess

bash_com = 'curl -k -H "Authorization: Bearer xxxxxxxxxxxxxxxx" -H "hawkular-tenant: test" -X GET https://www.example.com/test | python -m json.tool'
subprocess.Popen(bash_com)
output = subprocess.check_output(['bash','-c', bash_com])

rawDescription: Execute curl command within Python

description: Execute curl command within Python


code:
ax = df.plot(kind='bar')
ax.set_xlabel('fred')
ax.set_ylabel('barney')
ax.set_title('wilma')

rawDescription: In matplotlib.pyplot, how to create label in x and y coordinate?


code:
url(r'((?:(?:[a-zA-Z]+)/(?:\d+)/)*)','kwargs.views.aview')

def aview(request,args):
    args = dict(zip(*[iter(args.split('/'))]*2))
    return HttpResponse(args)

rawDescription: How can I get **kwargs from url by urls.py setting?

description: Get **kwargs from url by urls.py setting


code:
@login_required
def updateProfile(request, user_id):

    if request.method == 'POST':
        form = UserProfileForm(request.POST)

        if form.is_valid():
            obj = form.save(commit=False)  # Get just object, but don't save yet
            obj.user = request.user        # set user (or user_id)
            obj.save()                     # Save object
            return index(request)

    else:
        profile, created = UserProfile.objects.get_or_create(user_id = self.user_id) # don't know if this will actually work.
        profile_form = UserProfileForm(profile)
        context = {
            'user' : request.user,
            'form' : profile_form
        }
        return render(request, 'myapp/profile.html', context)

rawDescription: How to update a form attribute from view code in django

description: Update a form attribute from view code in django


code: dict(x[1:] for x in reversed(myListOfTuples))

rawDescription: Elegant way to create a dictionary of pairs, from a list of tuples?


code:
from scipy.sparse import hstack
hstack((X, X2))

rawDescription: Concatenate sparse matrices in Python using SciPy/Numpy


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

fig = plt.figure()

position = np.arange(6) + .5

plt.tick_params(axis = 'x', colors = '#072b57')
plt.tick_params(axis = 'y', colors = '#072b57')

speeds = [1, 2, 3, 4, 1, 2]
heights = [0, 0, 0, 0, 0, 0]
rects = plt.bar(position, heights, align = 'center', color = '#b8ff5c')
plt.xticks(position, ('A', 'B', 'C', 'D', 'E', 'F'))

plt.xlabel('X Axis', color = '#072b57')
plt.ylabel('Y Axis', color = '#072b57')
plt.title('My Chart', color = '#072b57')

plt.ylim((0,100))
plt.xlim((0,6))

plt.grid(True)

rs = [r for r in rects]

def init():
    return rs

def animate(i):
    global rs, heights
    if all(map(lambda x: x==100, heights)):
        heights = [0, 0, 0, 0, 0, 0]
    else:
        heights = [min(h+s,100) for h,s in zip(heights,speeds)]
    for h,r in zip(heights,rs):
        r.set_height(h)
    return rs

anim = animation.FuncAnimation(fig, animate, init_func=init,frames=200, interval=20, blit=True)

plt.show()

rawDescription: Growing matplotlib bar charts

description: Growing matplotlib bar charts


code:
l.add_value('loc', {'lng': l.get_output_value('lng'),
                    'lat': l.get_output_value('lat')})

rawDescription: Scrapy appending Key: value pairs to another Key


code:
from subprocess import Popen, PIPE

cmd = ['powershell.exe', '-ExecutionPolicy', 'Bypass', '-File', 'C:\\Users\\cobalt\\Documents\\test\\test.ps1']

proc = Popen(cmd, stdout=PIPE, stderr=PIPE)
while True:
    line = proc.stdout.readline()
    if line != b'':
        print(line.strip())
    else:
        break

rawDescription: Python interaction with PowerShell

description: Python interaction with PowerShell


rawDescription: Basic Django query?

description: Basic Django query


code: re.search(r'\$\.', inputstring)

rawDescription: Search a regex containing special characters


code:
start_ticks=pygame.time.get_ticks() #starter tick
while mainloop: # mainloop
    seconds=(pygame.time.get_ticks()-start_ticks)/1000 #calculate how many seconds
    if seconds>10: # if more than 10 seconds close the game
        break
    print (seconds) #print how many seconds

rawDescription: Countdown timer in Pygame

description: Countdown timer in Pygame


code:
import operator

o = {item1: operator.or_}

if o[item1](i>4, i<0):
    ...

rawDescription: Use a string (representing a logical operator) in a Python expression


code:
l = [1, 2]

print(l.append(3))

print(l)

rawDescription: Python - Traverse list of dictionaries - Error as NoneType


code:
arr = np.array([[0,9,9,0,0,9,9,1],[9,0,1,9,0,0,0,1]])
target = [1,9] # wanted values
index = np.where(np.in1d(arr.ravel(), target).reshape(arr.shape))

no_zeros = arr[index]

rawDescription: Applying a mask for speeding up various array calculations


code:
with requests.Session() as s:
    r = s.get(URL1)
    r = s.post(URL2, data="username and password data payload")

rawDescription: Using requests module, how to handle 'set-cookie' in request response?


code:
import random
numbers = range(a,b) + range(c,d)
r = random.choice(numbers)

rawDescription: Can Python generate a random number that excludes a set of numbers, without using recursion?


code:
import tkinter

window = tkinter.Tk()
myCanvas = tkinter.Canvas(window, width = 400, height = 400)
myCanvas.pack()

tag_mapping = {}                                      # <----
tag_mapping['tag1'] = { "id": 1, "name": "test" }     # <----
textItemContents = "asdf"
textItem = myCanvas.create_text(1, 1, tags='tag1', text = textItemContents)

searchTags = myCanvas.gettags(textItem)

print(searchTags) # => ('tag1',)
print(searchTags[0]) # => tag1
print(tag_mapping[searchTags[0]]) # => {'name': 'test', 'id': 1}
print(isinstance(tag_mapping[searchTags[0]], dict)) # => True

rawDescription: Canvas object Tags: not the type I originally gave it


rawDescription: Converting between multiline and single-line (with escaped linebreaks) string representations in Python?


rawDescription: Solving for x values of polynomial with known y in scipy / numpy


code:
re.sub('\|+guiHelperMenu$', '', s)
re.sub('\|+guiHelperMenu$', '', t)

rawDescription: in python regex How to use * in lookahead assertion?

description: In python regex How to use * in lookahead assertion


code:
def distance_squared(a, b):
    diff = complex(*a) - complex(*b)
    return diff.real ** 2 + diff.imag ** 2

from itertools import combinations
from collections import defaultdict

neighbours = defaultdict(list)
eps_squared = eps ** 2

point_neighbours = ((point, neighbours[tuple(point)]) for point in points)

for (p, p_neighbours), (q, _) in combinations(point_neighbours , r=2):
    if distance_squared(p, q) <= eps_squared:
        p_neighbours.append(q)

rawDescription: Speeding up block of code in python

description: Speeding up block of code in python


code:
class MyModelForm(forms.ModelForm):
    created_on = forms.DateField(widget=forms.DateInput(attrs={'readonly': 'readonly'}))

    def __init__(self, *args, **kwargs):
        super(MyModelForm, self).__init__(*args, **kwargs)

        if self.instance:
            if not self.initial.get('created_on'):
                self.initial['created_on'] = self.instance.created_on

    class Meta:
        model = MyModel
        fields = ('field1', 'field2', 'etc', 'created_on')

rawDescription: Include editable=false fields in ModelFormSet


code:
positions = {}
with open('fileA.txt') as fA:
    pos = 0
    for lineA in fA:
        uid = lineA.split(' ')[0] #gets SNP_A-1780270
        positions[uid] = pos
        pos += len(lineA)
with open('fileB.txt') as fB, open('fileA.txt') as fA, open('fileC.txt', 'w') as out:
    for lineB in fB:
        pos = positions[lineB.strip()]
        fA.seek(pos)
        lineA = fA.readline()
        out.write('%s\n', lineA)

rawDescription: compare two files with long lists to get common elements and adjoining information

description: Compare two files with long lists to get common elements and adjoining information


code:
s1 = "1) check if it is a timestamp in UTC format (e.g. if it is of the form '2014-05-10T12:30:00')."
s2 = "3) If it is not of timestamp, simply return the string."
re.compile('\d\d\d\d-\d\d-\d\d\(T\)\d\d:\d\d:\d\d')
s = re.sub(r'(.*\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d.*)',r'\1 \2',s1)
print(s)
s = re.sub(r'(.*\d\d\d\d-\d\d-\d\d)T(\d\d:\d\d:\d\d.*)',r'\1 \2',s2)
print(s)

rawDescription: Check if a string contains date or timestamp in python


rawDescription: Using Pandas to read an undelimited file


code:
def not_loggedin_required(function):
    def wrap(request, *args, **kwargs):
        if request.user.is_authenticated():
            return redirect(account) # redirect to profile page
        else:
            return function(request, *args, **kwargs)
    return wrap

@not_loggedin_required
def login(request):
    ...

rawDescription: Django function to require that user not be logged in


code: my_list = "Paul Michael Jessica Megan".split(" ")

rawDescription: Perl qw-operator in Python?

description: Perl qw-operator in Python


code:
import threading
import pycurl

def testf():
    c = pycurl.Curl()
    c.setopt(pycurl.CONNECTTIMEOUT, 3)
    c.setopt(pycurl.TIMEOUT, 3)
    c.setopt(pycurl.NOSIGNAL, 1)
    c.setopt(pycurl.URL, 'http://localhost/cgi-bin/foo.py')
    c.setopt(pycurl.HTTPGET, 1)
    c.perform()

for i in range(100):
    t = threading.Thread(target=testf)
    t.start()

rawDescription: pycurl/curl not following the CURLOPT_TIMEOUT option

description: Pycurl/curl not following the CURLOPT_TIMEOUT option


code:
def has_dupes(x):
    z = list()
    for val in x.values():
        if val in z:
            return True
        z.append(val)
    return False

rawDescription: How to get multiple values from a ditcionary

description: Get multiple values from a ditcionary


code:
import hashlib

d1 = 'data-1'
d2 = 'data-2'
d3 = 'data-3'

h1 = hashlib.md5(d1)
# instead of hashlib.md5(d1).update(d2), or hashlib.md5(d1 + d2)
h12 = h1.copy()
h12.update(d2)
# instead of hashlib.md5(d1).update(d3), or hashlib.md5(d1 + d3)
h13 = h1.copy()
h13.update(d3)

h2 = hashlib.md5(d2)
# instead of hashlib.md5(d2).update(d1), or hashlib.md5(d2 + d1)
h21 = h2.copy()
h21.update(d1)

# ...

rawDescription: How to update hashlib.md5 hasher using existing hasher in python?

description: Update hashlib.md5 hasher using existing hasher in python


code:
import collections

class LowercasingSet(collections.MutableSet):
  def __init__(self, initvalue=()):
    self._theset = set()
    for x in initvalue: self.add(x)
  def add(self, item):
    self._theset.add(item.lower())
  def discard(self, item):
    self._theset.discard(item.lower())
  def __iter__(self):
    return iter(self._theset)
  def __len__(self):
    return len(self._theset)
  def __contains__(self, item):
    try:
      return item.lower() in self._theset
    except AttributeError:
      return False

rawDescription: Overriding set methods in Python

description: Overriding set methods in Python


code:
import re

regex = r'<(?!/?QUOTE\b)[^>]+>'

tests = [
    'a plain old string',
    'a string with <SOME> <XML TAGS="stuff">',
    'a string with <QUOTE>, </QUOTE>, and <QUOTE with="data">',
    'a string that has <QUOTEA> tags </QUOTEB>',
]

for i in tests:
    result = re.sub(regex, '', i)
    print('{}\n{}\n'.format(i, result))

rawDescription: Replacing all but specific elements using regex in Python


code:
class MyService(remote.Service):

    def initialize_request_state(self, state):
        self.headers = state.headers

    @remote.method(MyRequest, MyResponse)
    def my_request(self, request):
        logging.debug(self.headers)

rawDescription: Get request headers using GAE Protocol RPC service method


code:
if request.method == 'POST':
    form = FormHelp(request.POST)

    if form.is_valid():
        help_def.org = form.cleaned_data.get("org")
        help_def.help_type = form.cleaned_data.get("help_type")
        help_def.help_type = form.cleaned_data.get("help_content")
        help_def.save()

rawDescription: Django form not inserting form values

description: Django form not inserting form values


code:
from Bio import SeqIO
from collections import defaultdict
records=list(SeqIO.parse("data/dna.txt", "fasta"))
compilation = defaultdict(list)
for record in records:
    compilation[record.seq].append(record.id)

rawDescription: How to find identical sequences in Python

description: Find identical sequences in Python


code:
import tkinter as tk

def center_window(win):
    w = win.winfo_screenwidth()
    h = win.winfo_screenheight()
    x = w/2 - win.winfo_width()/2
    y = h/2 - win.winfo_height()/2
    win.geometry("+%d+%d" % (x, y))

root = tk.Tk()
top1 = tk.Toplevel(root)
top1.title('TopLevel Window 1')
top1.minsize(300, 300)

b = tk.Label(top1, text='Here is a Label')

top1.after(1, lambda: center_window(top1))

l = tk.Button(top1, text='Click Me')

b.pack()
l.pack()

root.mainloop()

rawDescription: Why doesn't this centre the window?

description: Why doesn't this centre the window


code: writer.writerow(("%.6f" % obj["time"], ))

rawDescription: how to set accuracy when using csv writer in python

description: Set accuracy when using csv writer in python


code:
dims = []
while isinstance(matrix, list) and matrix is not None:
    dims.append(len(matrix))
    matrix = matrix[0]
number_of_dimensions = len(dims)

rawDescription: How can I determine the dimensionality of a list?

description: Determine the dimensionality of a list


code:
class MyClass:
    def __init__(self, x):
        self.x = x

    def __hash__(self):
        return hash(self.x)

    def __eq__(self, rhs):
        return rhs.x == self.x

d = {MyClass(1): 1}
assert d[MyClass(1)] == 1

rawDescription: How does Python 3 convert instances of custom types into dict keys?


rawDescription: How to remove all rows in a numpy.ndarray that contain non-numeric values

description: Remove all rows in a numpy.ndarray that contain non-numeric values


code:
import maya.OpenMaya as OpenMaya

def projectPoint(worldPnt, camPnt, depth):
    '''
    @param worldPnt - MPoint of point to project. (WorldSpace)
    @param camPnt - MPoint of camera position. (WorldSpace)
    @param depth - Float value of distance.
    Returns list of 3 floats.
    '''
    #Get vector from camera to point and normalize it.
    mVec_pointVec = worldPnt - camPnt
    mVec_pointVec.normalize()

    #Multiply it by the depth and the camera offset to it.
    mVec_pointVec *= depth
    mVec_pointVec += OpenMaya.MVector(camPnt.x, camPnt.y, camPnt.z)

    return [mVec_pointVec.x, mVec_pointVec.y, mVec_pointVec.z]

rawDescription: Project a 2D Point to 3D using a depth value. Maya Python API


rawDescription: In Python, is there a way to sort a list made of lists and tuples, consistently?


code:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_aspect('equal')

u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)

x = 1 * np.outer(np.cos(u), np.sin(v))
y = 1 * np.outer(np.sin(u), np.sin(v))
z = 1 * np.outer(np.ones(np.size(u)), np.cos(v))
#for i in range(2):
#    ax.plot_surface(x+random.randint(-5,5), y+random.randint(-5,5), z+random.randint(-5,5),  rstride=4, cstride=4, color='b', linewidth=0, alpha=0.5)
elev = 10.0
rot = 80.0 / 180 * np.pi
ax.plot_surface(x, y, z,  rstride=4, cstride=4, color='b', linewidth=0, alpha=0.5)
#calculate vectors for "vertical" circle
a = np.array([-np.sin(elev / 180 * np.pi), 0, np.cos(elev / 180 * np.pi)])
b = np.array([0, 1, 0])
b = b * np.cos(rot) + np.cross(a, b) * np.sin(rot) + a * np.dot(a, b) * (1 - np.cos(rot))
ax.plot(np.sin(u),np.cos(u),0,color='k', linestyle = 'dashed')
horiz_front = np.linspace(0, np.pi, 100)
ax.plot(np.sin(horiz_front),np.cos(horiz_front),0,color='k')
vert_front = np.linspace(np.pi / 2, 3 * np.pi / 2, 100)
ax.plot(a[0] * np.sin(u) + b[0] * np.cos(u), b[1] * np.cos(u), a[2] * np.sin(u) + b[2] * np.cos(u),color='k', linestyle = 'dashed')
ax.plot(a[0] * np.sin(vert_front) + b[0] * np.cos(vert_front), b[1] * np.cos(vert_front), a[2] * np.sin(vert_front) + b[2] * np.cos(vert_front),color='k')

ax.view_init(elev = elev, azim = 0)

plt.show()

rawDescription: python matplotlib: drawing 3D sphere with circumferences

description: Python matplotlib: drawing 3D sphere with circumferences


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib import sankey
import subprocess

N = 10
time = np.linspace(0, np.pi * 2, N)
flow_in = 4 * np.ones_like(time)
flow_out_a = 2 + np.sin(time)
flow_out_b = flow_in - flow_out_a

fig, ax = plt.subplots()
sk = sankey.Sankey(ax=ax, scale=1 / max(flow_in), format=u'%0.3g')
diags_text = ax.text(0.05, 0.9, '', transform=ax.transAxes)

def init():
    a = flow_in[0]
    b = -flow_out_a[0]
    c = -flow_out_b[0]
    sk.add(flows=[a, b, c],
           orientations=[0, 0, -1],
           alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])
    diags = sk.finish()
    return []

def anim(idx):
    for diag in sk.diagrams:
        # remove called instead of set_visible(False) to keep
        # `len(ax.get_children())` constant
        diag.patch.remove()
        diag.text.remove()
        for txt in diag.texts:
            txt.remove()
    sk.diagrams = []

    a = flow_in[idx]
    b = -flow_out_a[idx]
    c = -flow_out_b[idx]

    sk.add(flows=[a, b, c],
           orientations=[0, 0, -1],
           alpha=0.2, lw=2.0, trunklength=1, pathlengths=[1, 1, 1])
    diags = sk.finish()
    diags_text.set_text('len(sk.diagrams) = {l} | len(ax.get_children()) = {gc}'
                        .format(l=len(sk.diagrams), gc=len(ax.get_children())))
    ax.set_xlim(-2, 3)
    ax.set_ylim(-3, 2)
    fig.savefig('frame_{:03d}.png'.format(idx))
    return []

ax.set_xlim(-2, 3)
ax.set_ylim(-3, 2)

anim = animation.FuncAnimation(fig, anim, init_func=init, repeat=False,
                               frames=N, interval=20, blit=True)

# anim.save('mymovie.mp4', writer='mencoder')
plt.show()
subprocess.call(['convert', '-delay', '10', '-loop', '0', '*.png', 'out.gif'])

rawDescription: Animate a matplotlib sankey diagram

description: Animate a matplotlib sankey diagram


code:
import os  # <----
import shutil

src = r"C:\test"
dest = r"C:\copied_test"
dest = os.path.join(dest, os.path.basename(src))  # <---
shutil.copytree(src, dest)

rawDescription: Keep directory name when copying in Python 3.5


code:
ct = 0
while 1:
    msg = self.infile.read(1500)
    if len(msg) == 0:
        break
    else:
        ct += 1

rawDescription: Python file reading after file is all read


rawDescription: Python(3), Loop over tuple and use string format to display


code:
a=[1,2,3,4]
print(a.reverse())
a
a=[1,2,3,4]
print(reversed(a))
list(reversed(a))
a

rawDescription: Python a = a.reverse makes the list empty?


rawDescription: update the value in set Python?

description: Update the value in set Python


code: [e for e in emails if not any(pat in e for pat in pattern_list)]

rawDescription: Python list comprehension nested loop

description: Python list comprehension nested loop


code: pd.DataFrame([x.split('\t') for x in test])

rawDescription: Pandas dataframe from generator where each line is a tab-separated row


code: [any(i in j for i in a) for j in b]

rawDescription: python find if each element of a string list contains any of another string list

description: Python find if each element of a string list contains any of another string list


code:
L = 3
m = (np.arange(2**L)[:,None] >> np.arange(L)[::-1]) & 1
m
np.hstack([m.sum(1,keepdims=True), m])

rawDescription: Binary matrix entries

description: Binary matrix entries


code:
import sys
from EasyDialogs import ProgressBar
from urllib import urlretrieve

def download(url, filename):
    bar = ProgressBar(title='Downloading...', label=url)

    def report(block_count, block_size, total_size):
        if block_count == 0:
            bar.set(0, total_size)
        bar.inc(block_size)

    urlretrieve(url, filename, reporthook=report)

if __name__ == '__main__':
    url = sys.argv[1]
    filename = sys.argv[2]
    download(url, filename)

rawDescription: File Downloader with GUI progress display?


code:
import re

strings =[
    '50 hour',
    '5 hrs',
    '3 hours',
    '5hrs',
    '1hour',
]

for original in strings:
    replaced = re.sub(r'(\d+)\s*(?:hours?\b|hrs?\b)',
                      lambda m: str(int(m.group(1)) * 60),
                      original)
    print('{} => {}'.format(original, replaced))

rawDescription: Python regex hours to minutes

description: Python regex hours to minutes


code: sample_list = [select_one_name, select_two_name, etc]

rawDescription: Python: Create List of Object References


code:
keys = ['a','b','c']
value = [0, 0]
{key: list(value) for key in keys}

rawDescription: dict.fromkeys all point to same list

description: Dict.fromkeys all point to same list


code:
class base: pass
classNames = ["class1", "class2", "class3", "class4"]
classes = [type(name, (base,), {}) for name in classNames]

rawDescription: python define a group of classes

description: Python define a group of classes


code:
class MyClassAdmin(admin.ModelAdmin):

    list_display = ('my_own_method')

    def my_own_method(self, obj):
        if [condition]:
            return True
        else:
            return False
    my_own_method.boolean = True

rawDescription: My own method used in list_display and value as boolean icon


code:
from repoze.what.plugins.pylonshq import ControllerProtector

allow_only = All(not_anonymous(msg=l_(u'You must be logged on')),
                     my_custom_predicate(msg=l_(u'something wrong')))

@ControllerProtector(allow_only)
class MyController(BaseController):

    def index(self, **kw):
        return dict()

rawDescription: How to transfer url parameters to repoze custom predicate checkers

description: Transfer url parameters to repoze custom predicate checkers


code:
import jinja2

my_loader = jinja2.ChoiceLoader([
    app.jinja_loader,
    jinja2.FileSystemLoader(['templates/somwehere',
                             'templates/another_place',
                             'apps/templates']),
])
app.jinja_loader = my_loader

rawDescription: Optimal layout for Flask blueprint templates?


code:
utol = 100.
ltol = -100.
yy = 1/(xx-2)
yy[yy>utol] = np.inf
yy[yy<ltol] = -np.inf

ax.plot(xx, yy, zorder=100, linewidth=3, color='red')

rawDescription: how to handle an asymptote/discontinuity with Matplotlib

description: Handle an asymptote/discontinuity with Matplotlib


rawDescription: Write a raw binary file with NumPy array data


code:
df.Age.replace(np.NaN, 'Is Null value', inplace=True)

# Or, depending on your needs:
df['Age'] = df.Age.replace(np.NaN, 'Is Null value')

# Or without `replace` :
df['Age'] = df.Age.apply(lambda x: x if not pd.isnull(x) else 'Is Null value')

rawDescription: replacing null values in a Pandas Dataframe using applymap

description: Replacing null values in a Pandas Dataframe using applymap


code:
listA = ['1','2','3','4','5','6','6','8','9','5','3','7']
split = [listA[i:(i+3)] for i in range(0, len(listA) - 1, 3)]
split
del split[0][-1]
split
listA = sum(split, []) # <- flatten split list back to 1 level
listA
split = [listA[i:(i+3)] for i in range(0, len(listA) - 1, 3)]
split

rawDescription: How could i refresh a list once an item has been removed from a list within a list in python


code:
c_float_p = ctypes.POINTER(ctypes.c_float)
data = numpy.array([[0.1, 0.1], [0.2, 0.2], [0.3, 0.3]])
data = data.astype(numpy.float32)
data_p = data.ctypes.data_as(c_float_p)

rawDescription: How to use NumPy array with ctypes?

description: Use NumPy array with ctypes


code:
import csv
writer = csv.writer(open(filename))

rawDescription: Replace Character in Python Before Writing to File


code:
request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=20.0, request_timeout=20.0)
response = yield tornado.gen.Task(http_client.fetch, request)

rawDescription: Set timeout to an http request in Tornado


code:
FOO = 42
BAR = 777
HOSTNAME=socket.gethostname()
try:
    _imp = __import__('Constants_'+HOSTNAME)
    _locals = locals()
    for _name in dir(_imp):
        if not _name.startswith('_'):
            _locals[_name] = getattr(_imp, _name)
    del _imp, _locals, _name
except ImportError:
    pass

rawDescription: overriding a set of namespaced constants

description: Overriding a set of namespaced constants


code:
import re

def getPage():
    url = "http://url:port/min_day.js"
    req = urllib2.Request(url)
    response = urllib2.urlopen(req)
    return response.read()

def repl(match):
    return '20%s%s%s,%s,%s,%s,-1,-1,%s,%s'%(match.group('year'),
                                            match.group('month'),
                                            match.group('day'),
                                            match.group('time'),
                                            match.group('v3'),
                                            match.group('v1'),
                                            match.group('v5'),
                                            match.group('v4'))

pattern = re.compile(r'm\[mi\+\+\]="(?P<day>\d{2})\.(?P<month>\d{2})\.(?P<year>\d{2}) (?P<time>[\d:]{8})\|(?P<v1>\d+);(?P<v2>\d+);(?P<v3>\d+);(?P<v4>\d+);(?P<v5>\d+).+')

data = [re.sub(pattern, repl, line).split(',') for line in getPage().split('\n')]

# If you want to sort your data
data = sorted(data, key=lambda x:x[0], reverse=True)

# If you want to write your data back to a formatted string
new_string = ';'.join(','.join(x) for x in data)

# If you want to write it back to file
with open('new/file.txt', 'w') as f:
    f.write(new_string)

rawDescription: Manipulate string data

description: Manipulate string data


rawDescription: Grouping data on unique column keys and joining (pivot table) in Pandas Python


code:
import xlrd
wb = xlrd.open_workbook('myworkbook.xls')
sh = wb.sheet_by_index(0)
lst = [sh.row(n) for n in xrange(sh.nrows)]
searchdict = dict(B=1, E=2, C=3)
lst.sort(key=lambda row: searchdict.get(row[index][0], 4))

rawDescription: xlrd / xlutils reordering spreadsheet rows

description: Xlrd / xlutils reordering spreadsheet rows


rawDescription: How do you determine sqlalchemy driver from the session?

description: Determine sqlalchemy driver from the session


code:
import os

#get all plt files
traj_files = []
for root, dirs, files in os.walk('Data'):
    for filename in files:
        if filename.endswith('.plt'):
            traj_files.append(os.path.join(root, filename))

for traj_file in traj_files:

    #the new column we want to write
    new_col = traj_file.split('/')[1]
    #check if filename looks OK
    if len(new_col) != 3 or not new_col.isnumeric() or not '/Trajectory/' in traj_file:
        continue

    #read old file and write new column
    with open(traj_file + '.new', 'w') as new_traj:
        with open(traj_file, 'r') as old_traj:
            for line in old_traj.readlines():
                new_traj.write(new_col + ' ' + line)

rawDescription: Folder name as one of the column names

description: Folder name as one of the column names


code:
import lxml.html
html = lxml.html.fromstring("
/mytext,
")
lxml.html.tostring(html)

rawDescription: Python convert html ascii encoded text to utf8


code:
class StageForm(forms.Form):
    targets = forms.ModelMultipleChoiceField(queryset=Target.objects.all(), widget=forms.CheckboxSelectMultiple())

rawDescription: Django serving stale form validation choices


code:
from selenium import webdriver
import time

# timeout = 10
# driver.set_page_load_timeout(timeout)

start = time.time()
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=ANY'])
driver.set_window_size(1024, 768)
driver.get('https://f8790d1e-aknuds1.node.tutum.io/')
driver.save_screenshot('screen.png')
driver.quit()

end = time.time()
print(end - start)

rawDescription: Why can't PhantomJS open my Web page?

description: Why can't PhantomJS open my Web page


code:
bad_code = "system('rm -rf /users')"
eval(bad_code)

rawDescription: Making a system/program vulnerable to exploits


code:
import random
candidateWords = [...]

def wordlist(words):
    for index, item in enumerate(words):
        print(index, ") ", item, sep='')

available_words = random.sample(candidateWords, 8)
one = random.choice( available_words )

print("Welcome to the Guess-The-Word Game.\nThe Password is one of these words:")
wordlist(available_words)
print(one)

rawDescription: How can I use random.sample & random.choice

description: Use random.sample & random.choice


code:
def func( *files ):
    for f in files:
         doSomethingWithFile( f )

func( *['file1','file2','file3'] ) #Is treated like func('file1','file2','file3')
func( 'file1' )

rawDescription: How can I tell if a python variable is a string or a list?

description: Tell if a python variable is a string or a list


code:
Code
from PIL import Image, ImageDraw

def draw_ellipse(image, bounds, width=1, outline='white', antialias=4):
    """Improved ellipse drawing function, based on PIL.ImageDraw."""

    # Use a single channel image (mode='L') as mask.
    # The size of the mask can be increased relative to the imput image
    # to get smoother looking results.
    mask = Image.new(
        size=[int(dim * antialias) for dim in image.size],
        mode='L', color='black')
    draw = ImageDraw.Draw(mask)

    # draw outer shape in white (color) and inner shape in black (transparent)
    for offset, fill in (width/-2.0, 'white'), (width/2.0, 'black'):
        left, top = [(value + offset) * antialias for value in bounds[:2]]
        right, bottom = [(value - offset) * antialias for value in bounds[2:]]
        draw.ellipse([left, top, right, bottom], fill=fill)

    # downsample the mask using PIL.Image.LANCZOS
    # (a high-quality downsampling filter).
    mask = mask.resize(image.size, Image.LANCZOS)
    # paste outline color to input image through the mask
    image.paste(outline, mask=mask)

# green background image
image = Image.new(mode='RGB', size=(700, 300), color='green')

ellipse_box = [50, 50, 300, 250]

# draw a thick white ellipse and a thin black ellipse
draw_ellipse(image, ellipse_box, width=20)

# draw a thin black line, using higher antialias to preserve finer detail
draw_ellipse(image, ellipse_box, outline='black', width=.5, antialias=8)

# Lets try without antialiasing
ellipse_box[0] += 350
ellipse_box[2] += 350

draw_ellipse(image, ellipse_box, width=20, antialias=1)
draw_ellipse(image, ellipse_box, outline='black', width=1, antialias=1)

image.show()

rawDescription: Draw Ellipse in Python PIL with line thickness


code:
all_size_list = dict((str(x['sizeOptionId']), str(x['sizeOptionName'])) for x in two_dimension_sizelist)

for product in products_list:
    if "sizeDimension1Id" in product.keys():
        size = product['sizeDimension1Id']
        if size in all_size_list.keys():
            product.update({
                'size1Name': all_size_list[size],
                'size1Id': size
            })

        # This can exist only if size1 exists
        if "sizeDimension2Id" in product.keys():
            size = product['sizeDimension2Id']
            if size in all_size_list.keys():
                product.update({
                    'size2Name': all_size_list[size],
                    'size2Id': size
                })

rawDescription: Create dictionary with different fields from different dictionaries


code:
import glob, os
with open('axlspd.txt', "w") as axlspd:
    for f in f in glob.glob('P:\MyDep\Myunit\Mysection\Stuff\Reports\YR*\O*\*\*\*\*.pdf'):
        fpath, fname = os.path.split(f)
        rname, extname = os.path.splitext(fname)
        dirtup = (f, rname)
        axlspd.write(', '.join(dirtup)+'\n')

rawDescription: Beginner Python: Writing GLOB Results to txt


code:
x = numpy.array([1, 2, 1])
numpy.repeat(x, 4)

rawDescription: In Python/NumPy, how to pad an array by replicating the elements?


code:
def order_after_insert(fields, id):
    fields.update(id=id)
    db(db.order.id == id).update(order_id=db.order.order_id.compute(fields))
db.order._after_insert.append(order_after_insert)

rawDescription: How to compute a databasefield with the field-id

description: Compute a databasefield with the field-id


code:
import importlib
themodule = importlib.import_module('django-google-storage')

rawDescription: Referencing a python package installed with pip


code:
@validates('field_one', 'field_two')
def validates_fields(self, key, value):
    if key == 'field_two':
        assert self.field_one != value
    return value

rawDescription: Flask validates decorator multiple fields simultaneously


code:
df1
df2
df1.merge(df2, how='outer')

rawDescription: Merging Multiple Pandas DataFrames - Some with Shared Unique IDs, Some with Shared Columns


code:
def helper(dataZero, dataSnap): # assumes arguments are sorted ascending!
    ds_iter = iter(dataSnap)
    curr_snap = next(ds_iter)
    for lo, hi in dataZero:
        while curr_snap[0] < lo:
            curr_snap = next(ds_iter)
        while lo <= curr_snap[0] <= hi:
            yield curr_snap
            curr_snap = next(ds_iter)

result = list(helper(dataZero, dataSnap))

rawDescription: Searching large structured numpy arrays quickly


code:
def module_config(mod):
    '''Loads the config residing next to the module.'''
    import configparser, os.path
    cp = configparser.ConfigParser()
    cp.read_file(open(os.path.splitext(mod.__file__)[0] + '.conf'))
    return cp

# load config for some module
import some_module
module_config(some_module)

# load config for current module
module_config(sys.modules(__name__))

rawDescription: How to tell Configparser to open the config file, that has the same name, as the module, opening it?

description: Tell Configparser to open the config file, that has the same name, as the module, opening it


code:
def checkString(a, b):
    return a.lower() in b.lower()

rawDescription: how to check whether a string A is contained into a longer string B for any combination of upper/lower character

description: Check whether a string A is contained into a longer string B for any combination of upper/lower character


code:
s = scipy.sparse.coo_matrix([1,2,3])
s
s.getformat()
s = scipy.sparse.csr_matrix([1,2,3])
s
s.getformat()

rawDescription: How to determine SciPy matrix "type" of a variable M

description: Determine SciPy matrix "type" of a variable M


code:
df.set_index(['year', 'names']).xs(2010)
df.set_index(['year', 'names']).xs([2010, 'a'])

rawDescription: How to make a hierarchical index from data in the columns?

description: Make a hierarchical index from data in the columns


code:
a1 = np.ma.array([1, 2, 3], mask = [0,1,1])
a2 = np.ma.array([7, 8, 9], mask = [1,1,0])
arr = np.ma.array(np.dstack((a1, a2)), mask=np.dstack((a1.mask, a2.mask)))
np.prod(arr[0], axis=1)

rawDescription: Aggregating numpy masked arrays

description: Aggregating numpy masked arrays


code:
import requests

class MyRequests:
    def __init__(self, username, passwd):
        self.username = username
        self.passwd = passwd

    def get(self, *args, **kwargs):
        # do your thing
        resp = requests.get(...)
        # do more processing

rawDescription: How to subclass requests in python through inheritance

description: Subclass requests in python through inheritance


code: ',\\\n'.join(map(lambda c: r'`%(col)s` = IF(VALUES(`%(col)s`) IS NULL, `%(col)s`, VALUES(`%(col)s`))' % {'col': c}, cols))

rawDescription: python sql statement optimalization in python

description: Python sql statement optimalization in python


code:
from threading import Thread

t1 = Thread(target = firstFunction)
t2 = Thread(target = secondFunction)

t1.start()
t2.start()

rawDescription: Simple way to run two while loops at the same time using threading?


code:
import numpy as np
import csv
array = []
reading = csv.reader(open('file.csv', 'rb'))
for row in reading:
    array.append(row)
make_sequence(10,array)

rawDescription: arrays are not functioning depending on their format

description: Arrays are not functioning depending on their format


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

colors_brewer = brewer2mpl.get_map('Paired', 'Qualitative', 12).mpl_colors
colors_jet = plt.cm.jet(np.linspace(0,1,12))

# random data
data = np.random.rand(100,12)

# plot it
fig, ax = plt.subplots(1,2)
ax[0].hist(data, bins=10, stacked=True, color=colors_brewer)
ax[1].hist(data, bins=10, stacked=True, color=colors_jet)
plt.show()

rawDescription: Pyplot historgram with more than 7 datasets


code:
flag = False

for line in infile:
    if flag is True:
        if line.startswith('>'):
            flag = False
        else:
            outfile.write(line)
    if line.startswith('>FGENESH:[mRNA]'):
        flag = True
        outfile.write(line)

rawDescription: python extracting sequences from gene prediction output

description: Python extracting sequences from gene prediction output


code:
from Tkinter import *

cat_win = Tk()
v = IntVar()
Radiobutton(cat_win, text="Truck", variable=v, value=1).pack(anchor=W)
Radiobutton(cat_win, text="Equipment", variable=v, value=2).pack(anchor=W)
Radiobutton(cat_win, text="Trailer", variable=v, value=3).pack(anchor=W)
Button(cat_win, text='Next', command=quit).pack()  # <-----

mainloop()

rawDescription: Python program hangs when using radiobuttons and buttons together


code:
selects = filter(lambda x: x != "None",
              [image_style, image_background, image_activity,
               image_merchandise, image_type]
          )

if selects:
    for choice in selects:
        items.filter("image_tags =", choice)
else:
    self.response.out.write(""" select tags to sort images""")

rawDescription: How to filter all selected options?

description: Filter all selected options


code:
import csv
with open(fileinpath, 'rb') as fin, open(fileoutpath, 'wb') as fout:
    freader = csv.reader(fin, delimiter = '\t')
    fwriter = csv.writer(fout, delimiter = '\t')
    idx = 1
    for line in freader:
        line[4], line[0] = line[0], line[4] #switches position between first and last column
        del line[3] #delete fourth column
        line.insert(0, idx)
        fwriter.writerow(line)
        idx += 1

rawDescription: Deleting/rearranging/adding in very large tsv files Python


code:
import site;
site.getsitepackages()

rawDescription: Psycopg2 shows different version number when inside dist-packages and outside it


code:
class ExclusiveMinValueValidator(BaseValidator):
    compare = lambda self, a, b: a <= b
    message = _(u'Ensure this value is greater than %(limit_value)s.')
    code = 'min_value'

rawDescription: Exclusive Limits on Form Fields

description: Exclusive Limits on Form Fields


code:
class MyClass:
    def __init__(self):
        self.my_list = []

    def add_to_list(self, var):
        self.my_list.append(var)

rawDescription: python object persisting list accidentally between requests

description: Python object persisting list accidentally between requests


code:
# Seconds before cached records expire
cache_expire = 500

# Create hash of parameters, to use as a lookup for job.id (cache)
hash = hashlib.sha1()
for param in search:
    hash.update(str(search[param]))
url_hash = 'url:{0}'.format(hash.hexdigest())

# Check if we have a cached result, need old job_key
job_key = r.hget(url_hash, 'job_key')
if job_key:
    job_hash = 'rq:job:{0}'.format(job_key)
    ttl = r.ttl(job_hash)
    if ttl:
        # Add 30 more seconds of buffer room
        # to ensure job.result doesn't get deleted pre-maturely
        r.expire(job_hash, ttl+30)
        return jsonify(search_id=job_key)
    else:
        # Job result has already been deleted, clear lookup hash
        r.delete(url_hash)

# Create new job
job = q.enqueue_call(func=worker.search, args=(search,), result_ttl=cache_expire)
# Create job.id lookup using hash as key (for cache)
if r.hsetnx(url_hash, 'job_key', job.id):
    r.expire(url_hash, cache_expire)

return jsonify(search_id=job.id)

rawDescription: Check if previous result already exists with Python-RQ


code:
import glob

files = [open(f) for f in glob.glob('./*.data')] #Make list of open files
fout = open("res.csv", 'w')

for row in range(21):
    for f in files:
        fout.write( f.readline().strip() ) # strip removes trailing newline
        fout.write(',')
    fout.write('\n')

fout.close()

rawDescription: Python- Import Multiple Files to a single .csv file


code:
import sys
from collections import OrderedDict
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtCore import Qt

class TupleKeyedOrderedDict(OrderedDict):
    def __init__(self, *args, **kwargs):
        super().__init__(sorted(kwargs.items()))

    def __getitem__(self, key):
        if isinstance(key, tuple):
            item = self
            for k in key:
                if item != ():
                    item = item[k]
            return item
        else:
            return super().__getitem__(key)

    def __setitem__(self, key, value):
        if isinstance(key, tuple):
            item = self
            previous_item = None
            for k in key:
                if item != ():
                    previous_item = item
                    item = item[k]
            previous_item[key[-1]] = value
        else:
            return super().__setitem__(key, value)

class SettingsModel(QtCore.QAbstractItemModel):
    def __init__(self, data, parent=None):
        super().__init__(parent)
        self.root = data
        self.my_index = {}   # Needed to stop garbage collection

    def index(self, row, column, parent):
        if not self.hasIndex(row, column, parent):
            return QtCore.QModelIndex()
        if parent.isValid():
            index_pointer = parent.internalPointer()
            parent_dict = self.root[index_pointer]
        else:
            parent_dict = self.root
            index_pointer = ()
        row_key = list(parent_dict.keys())[row]
        child_pointer = (*index_pointer, row_key)
        try:
            child_pointer = self.my_index[child_pointer]
        except KeyError:
            self.my_index[child_pointer] = child_pointer
        index = self.createIndex(row, column, child_pointer)
        return index

    def get_row(self, key):
        if key:
            parent = key[:-1]
            if not parent:
                return 0
            return list(self.root[parent].keys()).index(key[-1])
        else:
            return 0

    def parent(self, index):
        if not index.isValid():
            return QtCore.QModelIndex()
        child_key_list = index.internalPointer()
        if child_key_list:
            parent_key_list = child_key_list[:-1]
            try:
                parent_key_list = self.my_index[parent_key_list]
            except KeyError:
                self.my_index[parent_key_list] = parent_key_list
            return self.createIndex(self.get_row(parent_key_list), 0,
                                    parent_key_list)
        else:
            return QtCore.QModelIndex()

    def rowCount(self, parent):
        if parent.column() > 0:
            return 0    # only keys have children, not values
        if parent.isValid():
            indexPtr = parent.internalPointer()
            parentValue = self.root[indexPtr]
            if isinstance(parentValue, OrderedDict):
                return len(self.root[indexPtr])
            else:
                return 0
        else:
            return len(self.root)

    def columnCount(self, parent):
        return 2  # Key & value

    def data(self, index, role):
        if not index.isValid():
            return None
        if role in (QtCore.Qt.DisplayRole, QtCore.Qt.EditRole):
            indexPtr = index.internalPointer()
            if index.column() == 1:    # Column 1, send the value
                return self.root[indexPtr]
            else:                   # Column 0, send the key
                if indexPtr:
                    return indexPtr[-1]
                else:
                    return None
        else:  # Not display or Edit
            return None

    def setData(self, index, value, role):
        pointer = self.my_index[index.internalPointer()]
        self.root[pointer] = value
        self.dataChanged.emit(index, index)
        return True

    def flags(self, index):
        if not index.isValid():
            return 0
        return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsEditable

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    data = TupleKeyedOrderedDict(**{'1': OrderedDict({'sub': 'b'}), '2': OrderedDict({'subsub': '3'})})

    model = SettingsModel(data)
    tree_view = QtWidgets.QTreeView()
    tree_view.setModel(model)
    tree_view.show()
    sys.exit(app.exec_())

rawDescription: Editing Second Column in QTreeView with custom model Doesn't show editor


code:
class MyClick(object):

    def __init__(self):

        # initialize mode
        self._mode = 1

    def pick(self, event):
        """do some calculation depending on the last key pressed"""

        if self._mode == 1:
            # do something...
            pass

        elif self._mode == 2:
            # do something different...
            pass

        elif self._mode == 3:
            # do something else entirely...
            pass

    def press(self, event):
        """record last key pressed"""

        if event.key == '1':
            self._mode = 1

        elif event.key == '2':
            self._mode = 2

        elif event.key == '3':
            self._mode = 3

rawDescription: Using key_press_event to select a 'mode' for different types of pick_event


code:
import operator

class Person(object):
    def __init__(self, name, ssn):
        self.name = name
        self._ssn = ssn
    ssn = property(operator.attrgetter("_ssn"))

rawDescription: How to restrict setting an attribute outside of constructor?

description: Restrict setting an attribute outside of constructor


code: from main.__init__ import classname

rawDescription: Import module from different folder: special case


rawDescription: Python NumPy Convert Columns to Rows

description: Python NumPy Convert Columns to Rows


rawDescription: Splitting a string with repeated characters into a list using regex


code:
import os
import Tkinter

def close():
    gui.destroy()

gui = Tkinter.Tk()
gui.geometry("300x200")
messagebutton1 = Tkinter.Button(gui,text='Process Completed', command=close)
messagebutton1.place(x=80,y=80)

gui.mainloop()

rawDescription: Close the message button

description: Close the message button


code:
from fabric.api import *
import socket
socket.setdefaulttimeout(5)

# Setup fabric details
env.user         = 'fabric-user'
env.key_filename = '/root/.ssh/fabric_rsa'

def execute_remote(command):
    return run(command)

@parallel
def execute_remote_parallel(command):
    return run(command)

def run_fabric(cmd,hosts,in_parallel):
    """ Check the parameters and call the corresponding fabric block"""
    if in_parallel:
        return execute(execute_remote_parallel,command=cmd,hosts=hosts)
    else:
        return execute(execute_remote,command=cmd,hosts=hosts)

class FabricFunctions:

    def stop_service(self,hosts,in_parallel,servicename):
        cmd = "sudo /sbin/service %s stop" % servicename
        run_fabric(cmd, hosts, in_parallel)

    def start_service(self,hosts,in_parallel,servicename):
        cmd = "sudo /sbin/service %s start" % servicename
        run_fabric(cmd, hosts, in_parallel)

#
#   Example - Untested!
#
f = FabricFunctions()
f.start_service("ec2-instance-ip",False,"httpd")

rawDescription: Python deployment using Fabric

description: Python deployment using Fabric


code:
parser._get_all_options()[1:]
[x.get_opt_string() for x in parser._get_all_options()[1:]]
[x.dest for x in parser._get_all_options()[1:]]

rawDescription: How do I get optparse list all possible arguments and options?

description: Get optparse list all possible arguments and options


code:
from django import template
register = template.Library()

@register.simple_tag(takes_context=True)
def stop_repeat(context, event):
    """
    Finds various types of links embedded in feed text and creates links out of them.
    """
    if event.date:
        if (event.get_date_time_location(), event.id) in context:
            return ''
        else:
            context[(event.get_date_time_location(), event.id)] = (event.get_date_time_location(), event.id)
            return event.get_date_time_location()

rawDescription: displaying unique objects only in django templates

description: Displaying unique objects only in django templates


code:
float_list = []
for item in instance_properties:
    try:
        float_list.append(float(item))
    except ValueError:
        pass

rawDescription: Extract only numbers from data in Jython


code:
class Bed(db.Model):
    name = db.StringProperty()

def _gazKey(x, y):
    return db.Key.from_path('Bed', x, y, 1)

class Eyut(db.Model):
    texx=db.TextProperty()
    date = db.DateTimeProperty(auto_now_add=False)
    glink=db.StringProperty()

class MainHandler(webapp2.RequestHandler):
    def get(self):
        eks=Eyut(parent=_gazKey('yaz','yaze'),key_name='yaze')
        eks.put()
        y=Eyut.get_by_key_name('yaze',parent=db.Key.from_path('Bed','yaz','yaze',1))
        y.texx="any text"
        y.put()
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.write(y.texx + '\n')

rawDescription: I cant add text to textProperty

description: I cant add text to textProperty


rawDescription: Unescaping Characters in a String with Python


code:
import matplotlib.pyplot as plt

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

ax1.set_xlabel('xlabel 1')
ax2.set_xlabel('xlabel 2')

ax2.xaxis.set_label_coords(0.5,0.95)

ax1.set_title('my title')

plt.show()

rawDescription: place top x-axis label on the inside of the graph

description: Place top x-axis label on the inside of the graph


code:
('INNERGRID', (0,0), (0,-1), colors.grey),  # gridlines for first column
('BOX', (0,0), (-1,-1), 0.25, colors.grey), # outer border
('BOX', (0,0), (0,-1), 0.25, colors.grey),  # inner border
("LINEBELOW", (0, 'splitlast'), (-1, 'splitlast'), 0, colors.grey), # draw bottom line on table split

rawDescription: ReportLab table with a column spanning all rows split across PDF page?


code:
from PIL import Image

sparse = [
    [1, 2, [255, 255, 0]],
    [1, 5, [255, 100, 0]],
    [2, 5, [0, 255, 110]],
    ]

im = Image.new("RGB", (20, 20), (255, 255, 255))
for item in sparse:
    x, y, color = item
    im.putpixel((x, y), tuple(color))

im.save("schlomer.png")
im.show()

rawDescription: create PNG image from sparse data

description: Create PNG image from sparse data


code:
def hideBG():
    global state
    if state == "Hidden":
        background_label.pack()
        state = "Showing"

    elif state == "Showing":
        background_label.pack_forget()
        state = "Hidden"

window = tkinter.Tk()

background_image=tkinter.PhotoImage(file="BG.png")
background_label = tkinter.Label(window, image=background_image)

hideBttn = tkinter.Button(window, text="Hide Background", command=hideBG)
state = "Showing"

hideBttn.pack()
background_label.pack()

window.mainloop()

rawDescription: Display and Hide PNG image in Tkinter Python3


code:
from __future__ import with_statement
from fabric.api import *
from contextlib import contextmanager as _contextmanager

env.activate = '. ./.env/bin/activate'

@_contextmanager
def virtualenv():
    with prefix(env.activate):
        yield

def deploy():
    with virtualenv():
        local('echo hello world!')

rawDescription: fabric returning 127 with "source" command

description: Fabric returning 127 with "source" command


code:
def Convert(s):
    p = s.split(",")
    return (int(p[0]), int(p[1]))

rawDescription: How to convert '1,2' into a tuple (1,2) in python?

description: Convert '1,2' into a tuple (1,2) in python


rawDescription: How to tell if a InstrumentedAttribute is a relation in sqlalchemy?

description: Tell if a InstrumentedAttribute is a relation in sqlalchemy


code:
@subscriber('pyramid.events.NewRequest')
def newrequest(event):
    request = event.request
    if request.remote_addr.startswith('66.'):
        raise SomeException('my custom message')

@view_config(context=SomeException, renderer='string')
def exc_view(exc, request):
    return exc.message

rawDescription: How do I globally redirect an ip address in Pyramid?

description: Globally redirect an ip address in Pyramid


code:
dictfun = dict()
for txt in ("a", "b", "c"):
    dictfun[txt] = lambda txt=txt: "function " + txt

rawDescription: Close variable in lambda function

description: Close variable in lambda function


code:
one = np.ascontiguousarray(a['one'])
one.ctypes.data_as(ctypes.POINTER(ctypes.c_double))

rawDescription: ctypes pointer to numpy array with custom dtype

description: Ctypes pointer to numpy array with custom dtype


code:
with open('foo.pyc', 'rb') as f:
    data = f.read()
code = marshal.loads(data[8:])
code

rawDescription: how to call a python bytecode file by boost.python or by python/c api

description: Call a python bytecode file by boost.python or by python/c api


code:
import shelve
import os

def highscore(name, score):
    """
    Function to return the high score from our
    persistent storage of score records for a given
    person.
    """
    # Get our working directory
    working_dir = os.getcwd()
    # Create our shelf object for a player
    highscore_fn = os.path.join(working_dir, 'highscore.shelve')
    # Open our shelf
    with shelve.open(highscore_fn, writeback=True) as shelf:
        # Check if player exists in shelf
        if name in shelf:
            # Check if score is greater than existing score
            if score > shelf[name]:
                # Assign our new high score
                high_score = score
                # Assign the value of player to our shelf
                shelf[name] = score
            else:
                # Existing high score stands for player
                high_score = shelf[name]
        else:
            # Assign the player to the shelf
            shelf[name] = score
            # Assign the high score to the player score
            high_score = score

    return high_score

rawDescription: How can I isolate a Python dictionary from a list for comparison?

description: Isolate a Python dictionary from a list for comparison


rawDescription: Getting the mean of multiple axis of a numpy array


code:
class Foo():
     ...

vars=('tx', 'ty', 'tz')  # plus plenty more
for v in vars:
    setattr(Foo, v, 0)

rawDescription: how to create class variable dynamically in python

description: Create class variable dynamically in python


code:
## Return the new created instance
# @param name Name of the instance to create in a String format
# @param image OpenStack image to deploy on the virtual machine
# @param flavor OpenStack flavor to use for the virtual machine
# @param keypair Name of the keypair to copy on the instance
# @param sec_groups List of security groups to link to the instance
def create_instance(self,name,image,flavor,keypair=None,sec_groups=None):
  instance = self.client.servers.create(
    name=name,
    image=image,
    flavor=flavor,
    key_name=keypair,
    security_groups=sec_groups,
    userdata="#!/bin/bash \n echo 'AMAZING TEST' > /root/test"
  )
  return instance

rawDescription: Specify post-installation script when creating an instance in OpenStack [python-novaclient]


code:
def get_matching_country(order_num):
    i = 1
    matching_req = Country.objects.none()
    while true:
        req = Country.objects.filter(country_code=order_num[:i])
        if res.exists():
            matching_req = req
            i += 1
        else:
            break

    return matching_req

rawDescription: get best match for django query

description: Get best match for django query


code:
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
           "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"}

cj = http.cookiejar.CookieJar()
req = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
req.addheaders = list(headers.items())

# The data should be URL-encoded and then encoded using UTF-8 for best compatilibity
data = urllib.parse.urlencode({"Username": username, "Password": password}).encode("UTF-8")
res = req.open("http://example.com/login.php", data)

rawDescription: Python 3.4 HTTP POST request with cookies


rawDescription: Print list to csv file in Python3

description: Print list to csv file in Python3


rawDescription: matplotlib only plot one figure at a time

description: Matplotlib only plot one figure at a time


code:
class Thing:
     def __init__(self,**kwargs):
         for k,v in kwargs.items():
             setattr(self,k,v)
     def __getitem__(self,item):
         return self.__dict__.get(item,None)
     def __repr__(self):
         return "<Thing: %s>"%self.__dict__

Thing(**json.loads('{"name":"Bogdan","age":"21","sex":"male"}'))

rawDescription: Create Objects from Json dynamically in python


code:
import pandas as pd

df = pd.read_csv('keyword.csv', header = None)

df.columns = ['col1','col2','col3','col4']

df['avgCol'] = (df['col2'] + df['col4'])/2

rawDescription: Find mean of value row wise in a list

description: Find mean of value row wise in a list


code:
def page(request, page_lang_name='eng', page_title='home'):
    page_lang_obj = SiteLanguage.objects.get(title=page_lang_name)
    context = {
        'page_lang': page_lang_obj,
        'page_content': getattr(page_lang_obj, page_title)
    }

rawDescription: django view variable matching to a queryset

description: Django view variable matching to a queryset


code:
soup = BeautifulSoup(open('MUFC.html'))

for script in soup.find_all('script'):
    script.extract()

paragraphs = soup.find_all('p')
for paragraph in paragraphs:
    print(paragraph.get_text(strip=True))

rawDescription: Remove script tags inside p tags using beautifulsoup


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

x = y = np.arange(5)

fig, ax = plt. subplots()

ln, = ax.plot(x, y, lw=10, solid_capstyle='round')
ln2, = ax.plot(x, 4-y, lw=10)
ln2.set_solid_capstyle('round')
ax.margins(.2)

rawDescription: How to draw rounded line ends using matplotlib

description: Draw rounded line ends using matplotlib


code: [word for word in the_text.split() if word in set_list]

rawDescription: Using a list with words to find that words in a text


rawDescription: how do I make a fork or non-blocking system call in python

description: Make a fork or non-blocking system call in python


code:
import random
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

# generate some random data (approximately over 5 years)
data = [float(random.randint(1271517521, 1429197513)) for _ in range(1000)]

# convert the epoch format to matplotlib date format
mpl_data = mdates.epoch2num(data)

# plot it
fig, ax = plt.subplots(1,1)
ax.hist(mpl_data, bins=50, color='lightblue')
ax.xaxis.set_major_locator(mdates.YearLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
plt.show()

rawDescription: Histogram in matplotlib, time on x-Axis

description: Histogram in matplotlib, time on x-Axis


code:
# current directory containing cgi-bin directory with scripts in
# subclass CGIHTTPRequestHandler and override cgi_directories to change this
#
os.chdir('/path/to/htdocs')

BaseHTTPServer.HTTPServer(('',80), CGIHTTPServer.CGIHTTPRequestHandler).serve_forever()

rawDescription: Liteweight CGI Server to use on local machine to serve KML to Google Earth via Python or similar?


code:
@rumps.clicked("About")
def about(sender):
    rumps.alert("This is a cool app!")

@rumps.clicked("Preferences")
def about(sender):
    rumps.alert("This is a cool app's settings!")

app = rumps.App("My Toolbar App", title='World, Hello')
app.menu = [
    rumps.MenuItem('About', icon='pony.jpg', dimensions=(18, 18)),
    'Preferences',
]
app.run()

rawDescription: Using Rumps in Python and OS X Without Creating a Class


rawDescription: How to express 4-digit number while using Xpath in Scrapy

description: Express 4-digit number while using Xpath in Scrapy


code: key,rest = my_list[0],my_list[1:]

rawDescription: Is there a way to assign the "rest" of the items in a list to the final variable in multiple assignment in python?


code:
import os
import sys
bindir = os.path.abspath(os.path.dirname(sys.argv[0]))

rawDescription: What is the Python equivalent of Perl's FindBin?


code:
from Bio.SubsMat import MatrixInfo

def score_match(pair, matrix):
    if pair not in matrix:
        return matrix[(tuple(reversed(pair)))]
    else:
        return matrix[pair]

def score_pairwise(seq1, seq2, matrix, gap_s, gap_e):
    score = 0
    gap = False
    for i in range(len(seq1)):
        pair = (seq1[i], seq2[i])
        if not gap:
            if '-' in pair:
                gap = True
                score += gap_s
            else:
                score += score_match(pair, matrix)
        else:
            if '-' not in pair:
                gap = False
                score += score_match(pair, matrix)
            else:
                score += gap_e
    return score

seq1 = 'PAVKDLGAEG-ASDKGT--SHVVY----------TI-QLASTFE'
seq2 = 'PAVEDLGATG-ANDKGT--LYNIYARNTEGHPRSTV-QLGSTFE'

blosum = MatrixInfo.blosum62

score_pairwise(seq1, seq2, blosum, -5, -1)

rawDescription: Is there a function that can calculate a score for aligned sequences given the alignment parameters?


code:
import json
#read string input
dataone= raw_input()
m=json.loads(dataone)
print (m["attribute name in double quotes "])

rawDescription: How to get JSON document as user input in python

description: Get JSON document as user input in python


code:
data = ['{\n  "data": {\n    "promoter": {\n      "instagram_id": 123,\n      "instagram_name": "user",\n      "instagram_token": "\\"123\\"",\n      "location": "sydney",\n      "profile_pic": "profile/pic.jpg",\n      "user_id": 1,\n      "username": "my_user_name"\n    },\n    "success": true\n  }\n}']
import json
json_obj = json.loads(data[0])
json_obj['data']['promoter']['instagram_id']

rawDescription: List to JSON Object

description: List to JSON Object


code:
finalMap = {}
for d in fruitColourMapping:
    finalMap.update(d)

rawDescription: "Flattening" a list of dictionaries

description: "Flattening" a list of dictionaries


code:
import re

re.findall(r'\\bibitem\{(.*?)\}', latex_text)     # ['item1', 'item2']

rawDescription: Extract argument from general function code in python


code:
from copy import deepcopy
dictionary_new = deepcopy(dictionary_old)

rawDescription: Python, creating a new variable from a dictionary? not as straightforward as it seems?


code:
def _get_payoff(self, actual, predicted):
    pred_factor = numpy.abs(0.5 - predicted)
    payoff_selector = 2*numpy.isclose(actual, 1) + (predicted < 0.5)
    payoff = numpy.choose(payoff_selector,
                          [
                              self.fp_payoff,
                              self.tn_payoff,
                              self.tp_payoff,
                              self.fn_payoff,
                          ])
    return numpy.sum(payoff * pred_factor)

def get_total_payoff(self):
    return self._get_payoff(self.target, predictions)

rawDescription: How can I optimize this NumPy code?

description: Optimize this NumPy code


code:
class ItemDataForm(ModelForm):
    def __init__(self,product,*args,**kwargs):
        super(ItemDataForm, self).__init__(*args, **kwargs)
        del self.fields['created']
        fields = 'int1', 'int2', 'float1', 'float2', 'string1', 'string2'
        for field in fields:
            val = getattr(product, field + '_name')
            fval = field + '_val'
            if val:
                self.fields[fval].label = val
                self.fields[fval].value = getattr(product, field + '_default')
            else:
                del self.fields[fval]

rawDescription: More Pythonic way to repeat some actions for a number of attributes


code:
collections.Counter(x[0] for x in itertools.groupby(my_list)).get(1, 0) > 1
collections.Counter(x[0] for x in itertools.groupby(my_list)).get(2, 0) > 1

rawDescription: How to tell if items occur sequentially in a list in python

description: Tell if items occur sequentially in a list in python


code:
import numpy as np
import matplotlib.pyplot as plt
x = np.random.gamma(4, 0.5, 1000)
result = plt.hist(x, bins=20, color='c')
plt.axvline(x.mean(), color='b', linestyle='dashed', linewidth=2)

rawDescription: Drawing average line in histogram (matplotlib)


code:
def numfmt(x, pos): # your custom formatter function: divide by 100.0
    s = '{}'.format(x / 100.0)
    return s

import matplotlib.ticker as tkr     # has classes for tick-locating and -formatting
yfmt = tkr.FuncFormatter(numfmt)    # create your custom formatter function

# your existing code can be inserted here

pylab.gca().yaxis.set_major_formatter(yfmt)

# final step
pylab.savefig("Output.pdf", dpi=400, bbox_inches='tight', pad_inches=1)

rawDescription: How to divide ytics to a certain number in matplotlib?

description: Divide ytics to a certain number in matplotlib


code:
class TableA(Base):
    __table__ = table_a
    __mapper_args__ = {
            'primary_key':[table_a.pk,],
            }

    # new_col = column_property(func.regexp_substr(original, r'[^-]*'))  # or
    new_col = column_property(func.regexp_substr(table_a.c.original, r'[^-]*'))

rawDescription: add a virtual column to a reflected table

description: Add a virtual column to a reflected table


code:
for batch in per_batch(log_entries):
    sp = transaction.savepoint()
    try:
        process_batch(batch)
    except BatchFailedException:
        sp.rollback()
        transaction.commit()
        raise

rawDescription: ZODB: To where transaction.savepoint writes data?


code:
result = numpy.zeros([height,width, 3], dtype=numpy.uint8)
pyopencl.enqueue_copy(queue, result, result_g).wait()

surface = pygame.display.set_mode((width, height), pygame.DOUBLEBUF)
rgb2 = numpy.transpose(rgb, (1,0,2))
pygame.pixelcopy.array_to_surface(surface, rgb2)
pygame.display.flip()

rawDescription: Using python, how can I display a matrix of RGB values in a window?


code:
class students():
    def __init__(self, fname='', lname='', grades=None):
        self.firstName = fname
        self.lastName = lname
        self.gradeBook = [] if grades is None else grades

rawDescription: Problems with a Python List

description: Problems with a Python List


code:
dd <- data.frame(x=rnorm(15), y= rnorm(15))

boxplot(dd, boxwex = 0.3)
# note that you need to transpose `dd`
matpoints(y= t(dd), x= c(1.17,1.83),pch=19, col='black')
matlines(y= t(dd), x= c(1.2,1.8), lty=1, col = 'black')

rawDescription: How to draw a plot joining points from two measurement times?

description: Draw a plot joining points from two measurement times


code:
import Tkinter as tk

root=tk.Tk()
text=tk.Text(width = 40, height=4, font=("Helvetica",32))
text.pack()

text.insert(1.0, "Hello, world")
text.config(state="disabled")

root.mainloop()

rawDescription: help in python with font size and block Text Class in Tkinter

description: Help in python with font size and block Text Class in Tkinter


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

# some random data to plot
x = np.random.rand(10,10)*80e3 - 40e3
plt.imshow(x, aspect='auto', vmin=-40e3, vmax=40e3)

# create the colorbar
cb = plt.colorbar(orientation='horizontal', ticks=[-40e3,0,40e3])

# get the xtick labels
tl = cb.ax.get_xticklabels()

# set the alignment for the first and the last
tl[0].set_horizontalalignment('left')
tl[-1].set_horizontalalignment('right')

plt.show()

rawDescription: alignment of color bar labels in matplotlib

description: Alignment of color bar labels in matplotlib