code:
# models.py
class FkModel(models.Model):
    name = models.CharField(max_length=255, unique=True)
    tag = models.ForeignKey(Tag, blank=True, null=True, related_name='fk')
    ...

class M2mModel(models.Model):
    name = models.CharField(max_length=255, unique=True)
    tag = models.ForeignKey(Tag, blank=True, null=True, related_name='m2m')
    ...

# admin.py
class MyModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        form.save() # as I learned, without saving at this point, obj.field_m2m.all() will be empty
        tags = []
        if obj.field_m2m.all():
            tags += [m2m.tag for m2m in obj.field_m2m.all()]
        if obj.field_fk:
            tags += [obj.field_fk.tag]
        form.cleaned_data['tags'] = tags
        super(MyModelAdmin, self).save_model(request, obj, form, change)

rawDescription: How to use RelatedManager add() method in save()

description: Use RelatedManager add() method in save()


code:
def outerfunc(calltheinner=True):
    def innerfunc():
        do_something()
    if calltheinner:
        return innerfunc()
    else:
        return innerfunc

rawDescription: Can I unit test an inner function in python?

description: Unit test an inner function in python


code:
def deactivate(self):
    """If there are no stocks, deactivate the product. Used in last step of checkout.
    """

    inactive = False

    if self.is_variant():
        prod = self.parent
        inactive = all(var.get_stock_amount() == 0 for var in prod.variants.filter(active=True))
        if inactive:
            prod.active = 0
        prod.save()
    else:
        if self.get_stock_amount() == 0:
            self.active = 0

        self.save()

rawDescription: working on lists in python

description: Working on lists in python


code:
# option-1:
subqueryload_all(
    'orders.details', # @note: this will load both *orders* and their *details*
    )

# option-2:
subqueryload_all(
    Purchase.orders,       # @note: this will load orders
    PurchaseOrder.details, # @note: this will load orders' details
    )

rawDescription: How to load nested relationships in SQLAlchemy?

description: Load nested relationships in SQLAlchemy


code:
from Bio import SeqIO

with open("test1_out.txt","w") as f:
        for seq_record in SeqIO.parse("test1.fasta", "fasta"):
            for i in range(len(seq_record.seq) - 4) :
               f.write(str(seq_record.id) + "\n")
               f.write(str(seq_record.seq[i:i+5]) + "\n")  #first 5 base positions

rawDescription: Python: How to print out sequences with length n from sliding window in FASTA file?


code:
import numpy as np
from scipy.special import jv, hankel1, jvp, h1vp
import matplotlib.pyplot as plt

nr = 2
m = 20

# create a 2000 x 2000 sample complex plane between -2-2i .. +2+2i
x = np.linspace(-2, 2, 2000)
y = np.linspace(-2, 2, 2000)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y
z = 1-C**2

# draw a contour image of the imaginary part (red) and real part (blue)
csr = plt.contour(x, y, z.real, 5, colors='b')
plt.clabel(csr)
csi = plt.contour(x, y, z.imag, 5, colors='r')
plt.clabel(csi)
plt.axis('equal')
plt.savefig('contours.png')

# draw an image of the absolute value of the function, black representing zeros
plt.figure()
plt.imshow(abs(z), extent=[-2,2,-2,2], cmap=plt.cm.gray)
plt.axis('equal')
plt.savefig('absval.png')

rawDescription: Storing roots of a complex function in an array in SciPy


code:
values = [varA[name]['value'] for name in 'ZYXWV']
varC = struct.pack('>'+str(len(values))+'h', *values)

rawDescription: Python 2.7.6 Optimizing code for packing big endian bytes into a string


code:
class Product(models.Model):
    # ...
    availability = models.IntegerField(help_text="(updated on save)")

    # Use Seth's save()
    def save(self, *args, **kwargs):
        self.availability = self.inventory - self.commited
        super(Product, self).save(*args, **kwargs)

rawDescription: Integer Field Math in Django

description: Integer Field Math in Django


code:
import networkx as nx
G = nx.DiGraph()
G.add_edge('a','b')
G.add_edge('b','a')
G.add_edge('c','d')
[(u,v,d) for (u,v,d) in G.edges_iter(data=True) if G.has_edge(v,u)]

rawDescription: Getting all edge pairs of directed graph. networkx


code:
x = 'ASDFBOB DESUDESUBOBADSF'
y = x.replace('BOB', '')
y

rawDescription: Remove specified of string, much like "remove" for arrays?


code:
import gtk

def set_active_name(combobox, col, name):
    liststore = combobox.get_model()
    for i in xrange(len(liststore)):
        row = liststore[i]
        if row[col] == name:
            combobox.set_active(i)

window = gtk.Window()
window.connect("destroy", gtk.main_quit)

liststore = gtk.ListStore(int, str)
liststore.append([0, 'Jack'])
liststore.append([1, 'Emily'])
liststore.append([2, 'Paul'])

combobox = gtk.ComboBox()
cell = gtk.CellRendererText()
combobox.pack_start(cell)
combobox.add_attribute(cell, 'text', 1)
combobox.set_model(liststore)

set_active_name(combobox, 1, 'Paul')

window.add(combobox)
window.show_all()

gtk.main()

rawDescription: set up the entry of a combobox (gtk)

description: Set up the entry of a combobox (gtk)


code:
tuples.sort(key=lambda x: (-x[1], x[0]))
tuples

rawDescription: Custom sort of a list of tuples

description: Custom sort of a list of tuples


code:
f = File.new_for_commandline_arg('...')
info = f.query_info('standard::icon')
print(info.get_icon())

rawDescription: GIO: get_icon() always returns None

description: GIO: get_icon() always returns None


code:
import csv

data = [['title1','title2','title3'],['date1','info1','category1'],['date2','info2','category3']]

with open('stuff.csv', 'wb') as csvfile:
  writer = csv.writer(csvfile)
  for line in data:
    writer.writerow(line)

rawDescription: Output a *.csv file created from a list of lists


code:
olsresult.params
dict(olsresult.params)

rawDescription: Pulling variable names when using pandas and statsmodels


code:
def get_form_initial(self, step):
    initial = {}
    if step == 1:
        client_pk = self.kwargs.get('client_pk')
        initial.update({
            'client': Client.objects.get(pk=client_pk)
        })
    return self.initial_dict.get(step, initial)

rawDescription: Passing initial data from a database to Django Form Wizard template


code:
import daemon
from daemon.pidfile import PIDLockFile

context = daemon.DaemonContext(
    pidfile= PIDLockFile('/var/run/spam.pid'),
    )

with context:
    main()

rawDescription: Python Daemon: checking to have one daemon run at all times


code:
string = raw_input("String: ")
print(list(string))

rawDescription: Splitting a string?

description: Splitting a string


code: firstname = first[0:2]

rawDescription: How can I use python to make direct letter-to-number substitution?

description: Use python to make direct letter-to-number substitution


rawDescription: How can I mass query fields with mongoengine?

description: Mass query fields with mongoengine


code:
from ast import literal_eval
literal_eval("[1,2,3]")

rawDescription: how can I convert "[1,2,3]" string to list [1,2,3]?

description: Convert "[1,2,3]" string to list [1,2,3]


code:
from ast import literal_eval
literal_eval('[1, 2], [2, 7], [4, 3]')
list(literal_eval('[1, 2], [2, 7], [4, 3]'))

rawDescription: Treating string as a list object in python


code:
word = input("Enter a word: ")

output = []

for letter in word:
    if letter in "aeiou" and not letter in output:
        output.append(letter)

print(output)

rawDescription: python 3.5.0 ::: Compare values inside lists with each others

description: Python 3.5.0 ::: Compare values inside lists with each others


code:
class Proxy(object):
    def __init__(self, wrapped):
        self._wrapped = wrapped

    def foo(self):
        return self._wrapped.foo() + 42

    def __getattr__(self, name):
        return getattr(self._wrapped, name)

rawDescription: Why are there two magic methods to access object attributes in Python?


code:
l1 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 1, 0,
                            transform=fig.transFigure, figure=fig)

l2 = mpl.patches.FancyArrow(blcorPosn, blcorPosn, 0, 1,
                            transform=fig.transFigure, figure=fig)

rawDescription: Draw arrows on matplotlib figure directly


code:
# Initialize files and PyGame mixer:
import pygame
pygame.init()
pygame.mixer.pre_init(44100, 16, 2, 4096) # Frequency, size, channels and buffersize
shield_on = pygame.mixer.Sound("shield_on.wav")

class game(Widget):
    ...
    def update_loop(self):
        ...
        if "game_file_says_shield_is_on":
            shield_on.play()

rawDescription: Kivy and in-game sounds: Game update loop waits for sound to finish before continuing [FPS issues using SoundLoader in Kivy]


code:
import urlparse
urlparse.urlparse('http://python.org')
urlparse.urlparse('http://python.org').netloc

rawDescription: Python working with hostnames, hostname to IP


rawDescription: Timestamps with different timezones in the same pandas DatetimeIndex object?


code:
import fileinput
for line in fileinput.input():
  process(line)

rawDescription: Python read from command line arguments or stdin


code:
account_ids = [item.pk for item in group.accounts]
accounts_not_in_group = UserAccount.objects(_id__nin=account_ids)

rawDescription: ReferenceFields with MongoEngine

description: ReferenceFields with MongoEngine


code:
#!/usr/bin/env python
import signal
from contextlib import closing
from multiprocessing import Pool

class Alarm(Exception):
    pass

def alarm_handler(*args):
    raise Alarm("timeout")

def mp_get_product_data(id, timeout=10, nretries=3):
    signal.signal(signal.SIGALRM, alarm_handler) #XXX could move it to initializer
    for i in range(nretries):
        signal.alarm(timeout)
        try:
            return id, get_product_data(id), None
        except Alarm as e:
            timeout *= 2 # retry with increased timeout
        except Exception as e:
            break
        finally:
            signal.alarm(0) # disable alarm, no need to restore handler
    return id, None, str(e)

if __name__=="__main__":
   with closing(Pool(num_procs)) as pool:
       for id, result, error in pool.imap_unordered(mp_get_product_data, products):
           if error is not None: # report and/or reschedule
              print("error: {} for {}".format(error, id))
   pool.join()

rawDescription: Restart a process if running longer than x amount of minutes


code:
class StoreAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        if obj.copy_holidays_from:
            form.cleaned_data['holidays'] = obj.copy_holidays_from.holidays.all()

        super(StoreAdmin, self).save_model(request, obj, form, change)

rawDescription: ManyToMany field not saved when using Django admin


code:
import django.db.models.options as options

options.DEFAULT_NAMES = options.DEFAULT_NAMES + ('schema',)

rawDescription: Adding attributes into Django Model's Meta class


code:
template = "{}({})"
functionName = "function"  # e.g. input from user
arg = "arg"  # e.g. input from user
formatted = template.format(functionName, arg)
assert formatted == "function(arg)"
kodi.executebuiltin(formatted)

rawDescription: Placing a Python variable inline

description: Placing a Python variable inline


code: self.img = [gtk.Image() for _ in xrange(20)]

rawDescription: python - best way to write a list of images

description: Python - best way to write a list of images


code:
import matplotlib.pyplot as plt
with open("m.txt") as m:
    for index, line in enumerate(m):
        m_float = map(float,line.strip())
        plt.plot(index, m_float,'bo')

    plt.ylabel('FLOC - % of line')
    plt.xlabel('Sample Number')
    plt.axis([-10,10,0,5])
    plt.show()

rawDescription: Plotting a string of numbers from text file


code:
def n_max(arr, n):
    indices = arr.ravel().argsort()[-n:]
    indices = (numpy.unravel_index(i, arr.shape) for i in indices)
    return [(arr[i], i) for i in indices]

rawDescription: python+numpy: efficient way to take the min/max n values and indices from a matrix

description: Python+numpy: efficient way to take the min/max n values and indices from a matrix


code:
for i, entry in enumerate(entries):
    bucket = i // 3
    # insert into choosen bucket number

rawDescription: Looking for a Hashing function given static number of buckets


code:
import os
import inspect

def get_app_name():
    return os.path.dirname(
               os.path.abspath(
                   inspect.stack()[-1][1])).split(os.path.sep)[-1]

rawDescription: Extract Django Project Label in Imported File


code:
x = np.linspace(-10,10,100);
[X,Y] = np.meshgrid(x,x)
g = np.exp(-(np.square(X) + np.square(Y))/2)
im_handle = plt.imshow(g)
scat = plt.scatter(50,50,c='r', marker='+')
# image, with scatter point overlayed
scat.remove()
plt.draw()
# underlying image, no more scatter point(s) now shown

# For completeness, can also remove the other way around:
plt.clf()
im_handle = plt.imshow(g)
scat = plt.scatter(50,50,c='r', marker='+')
# image with both components
im_handle.remove()
plt.draw()
# now just the scatter points remain.

rawDescription: Clear overlay scatter on matplotlib image


code:
def process_lab_name(request, lab_name):
    request.current_lab = lab_name

def lab(view_func):
    def _decorator(request, lab_name, *args, **kwargs):
        # process the lab_name argument
        process_lab_name(request, lab_name)
        # when calling the view function, exclude the lab_name argument
        response = view_func(request, *args, **kwargs)
        return response
    return wraps(view_func)(_decorator)

@lab
def some_view(request):
    return render(...)

rawDescription: Django URL with dynamic prefix

description: Django URL with dynamic prefix


code:
r = requests.get(url, stream=True)
data = r.raw.read(decode_content=True)

rawDescription: Python Requests .iter_content() generator to binary data?


code:
vars_dict = {}
vars_dict["my_variable"] = 'Some Value'
vars_dict["my_variable2"] = 'Some Value'

rawDescription: Python: Is it a good idea to dynamically create variables?


code:
class A:
    def __init__():
     self.a.b.c = 10

    @property
    def aliased(self):
        return self.a.b.c

    @aliased.setter
    def aliased(self, value):
         self.a.b.c = value

    def anotherMethod():
      self.aliased *= 10 # Updates value of self.a.b.c

rawDescription: How do I create an alias for a variable in Python?

description: Create an alias for a variable in Python


code:
#!/usr/bin/env python
import sys
import threading

try:
    from PySide import QtGui
    from PySide import QtCore
    from PySide.QtCore import Signal
except:
    from PyQt4.QtCore import pyqtSignal as Signal
    from PyQt4 import QtGui
    from PyQt4 import QtCore

class MyUI(QtGui.QWidget):
# Add Signal
    text_edit_evt = Signal(str)

    def __init__(self):
        super(MyUI, self).__init__()
        self.initUI()

# Connect the signal
        self.text_edit_evt.connect(self.textEdit.append)

    def initUI(self):

        someValues = [1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200]

        self.setGeometry(100, 300, 640, 450) #window's geometry

        lblValueChoice = QtGui.QLabel(self)
        lblValueChoice.setGeometry(10, 10, (self.width() - 20), 27)
        lblValueChoice.setText("Select your value:")

        cmbValueChoice = QtGui.QComboBox(self)
        cmbValueChoice.setObjectName("valueChoice")
        cmbValueChoice.setGeometry(10, (lblValueChoice.y() + lblValueChoice.height() + 5), (self.width() - 20), 27)

        for item in someValues: cmbSerialPorts.addItem(item)
        cmbSerialPorts.setCurrentIndex(len(someValues)-1)

        pbStartReading = QtGui.QPushButton("Start doing the magic!", self)
        pbStartReading.setGeometry(10, (cmbValueChoice.y() + cmbValueChoice.height() + 10), (self.width() - 20), 27)
        pbStartReading.setCheckable(True)
        pbStartReading.clicked[bool].connect(lambda: self.startReading(bool, str(cmbValueChoice.currentText())))

# Use self.textEdit so it can be referenced from other functions
        self.textEdit = QtGui.QTextEdit(self)
        self.textEdit.setObjectName("textEdit")
        self.textEdit.setGeometry(10, (pbStartReading.y() + pbStartReading.height() + 10), (self.width() - 20), (self.height() - (pbStartReading.y() + pbStartReading.height() + 10) - 10) )

        self.textEdit.append(add_env())

        self.setWindowTitle(u'MyMiserableUIFailure')

        self.show()

    def startReading(self, bool, myvalue):

        threading.Thread(target=self.readingLoop, args=(bool, myvalue, )).start()

    def readingLoop(self, bool, myvalue):

        while bool:
            # the error happens in the line below, when I
            # try to reference the textEdit control
            # -------------------------------------------
# Emit the Signal we created
            self.text_edit_evt.emit("this actually works!\n")

def main():
    app = QtGui.QApplication(sys.argv)

    theui = MyUI()

    sys.exit(app.exec_())

def add_env():
    newLine = QtCore.QString("\n")
    env_info = "The system is:" + newLine
    env_info += newLine + "System/OS name:            " + str(platform.system())
    env_info += newLine + "System release:          " + str(platform.release())
    env_info += newLine + "System release version:  " + str(platform.version())
    env_info += newLine + "Machine type:              " + str(platform.machine())
    env_info += newLine + "Platform:                  " + str(platform.platform(aliased=0, terse=1))
    env_info += newLine + "Processor:                 " + str(platform.processor())
    env_info += newLine + "Network name:              " + str(platform.node())
    env_info += newLine + "Python ver(maj,min,patch): " + str(platform.python_version_tuple())
    env_info += newLine + "Python build:              " + str(platform.python_build())
    env_info += newLine + "Python implementation:     " + str(platform.python_implementation())
    env_info += newLine
    env_info += newLine + "***************************"
    return env_info

if __name__ == '__main__':
    main()

rawDescription: How to reference PyQt UI elements from around the code?

description: Reference PyQt UI elements from around the code


code:
#!/usr/bin/python3
from gi.repository import Gtk, Gio

# ...

class updateWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Updating...")

        self.label = Gtk.Label()
        self.label.set_text("Idling...")
        self.add(self.label)

    def update(self):
        self.label.set_text("Updating... Please wait.")
        subprocess = Gio.Subprocess.new(["/usr/bin/pkexec", "/usr/bin/apt-get", "update"], 0)
        subprocess.wait_check_async(None, self._on_update_finished)

    def _on_update_finished(self, subprocess, result):
        subprocess.wait_check_finish(result)
        self.label.set_text("Updated.")

rawDescription: Unable to initialize a window and wait for a process to end in Python 3 + GTK+ 3


code:
for friend in tweepy.Cursor(api.friends).items():
    # Process the friend here
    process_friend(friend)

rawDescription: Full list of twitter "friends" using python and tweepy


rawDescription: How do I compute the variance of a column of a sparse matrix in Scipy?

description: Compute the variance of a column of a sparse matrix in Scipy


code:
if name in exons:
    y = exons[name]
    minDist = 99999 # large number
    minIdx = None
    minNum = None
    for i, (low,high) in enumerate(y):
        dlow = abs(low - ppos1)
        dhigh = abs(high - ppos1)
        dist = min(dlow, dhigh)
        if dist < minDist:
            minDist = dist
            minIdx = i
            minNum = 0 if dlow < dhigh else 1
    print(y[minIdx])
    print(y[minIdx][minNum])

rawDescription: Finding nearest set of numbers given a position


code:
def rank_teams():
    rank = []
    # iterate through teams and create a tuple with most important to least important
    # sort params. team bust be the last object
    for team in Team.objects.all():
        points = 0
        best_goal_gap = 0
        best_goals = 0
        best_away_goals = 0
        all_victories = 0
        for game in TeamGame.objects.filter(team_name=team):
            points += {'w':3, 'd':1, 'l':0}[game.result]
            best_goal_gap = max(best_goal_gap, game.goals_hit-game.goals_get)
            best_goals = max(best_goals, game.goals_get)
            if game.field == 'o':
                best_away_goals = max(best_away_goals, game.goals_get)
            if game.result == 'w':
                all_victories +=1
        rank.append((points, best_goal_gap, best_goals, best_away_goals, all_victories, team))
    #sort the teams
    rank.sort()
    team_rank = [team for p,g,b,a,v,team in rank]
    return team_rank

rawDescription: Creating scoreboard with complicated rules


code:
def __parse64(self, path_file):
    file_data = open(path_file, 'rb').read(-1)
    #This slurps the whole file as binary.
    self.encoded_string_file = b64.b64encode(file_data)

rawDescription: python implementation of 'readAsDataURL

description: Python implementation of 'readAsDataURL


code:
with open("Input.txt") as inFile:
    for item in inFile:
        datePart, holiday = item.split(", ", 1)
        month, day = datePart.split()

rawDescription: Tokenizing with different delimiters

description: Tokenizing with different delimiters


code:
class Item(models.Model):
    category = models.ForeignKey(Category, db_index=True)
    name = models.CharField(_('Name'), max_length=30)
    price = models.IntegerField(_('Price'))
    filters = models.ManyToManyField(Filter, through='ItemFilter')

class ItemFilter(models.Model):
    item = models.ForeignKey(Item)
    filter = models.ForeignKey(Filter)
    value = models.ForeignKey(FilterValue)

rawDescription: What is the best way to save this relation in Django?


code:
from Tkinter import Tk, PhotoImage, Label
from glob import glob
from itertools import cycle

# this method calls itself again after 1000 milliseconds
def show_next():
    image.configure(file=next(images))
    root.after(1000, show_next)

# cycle though gif images found in working directory
images = cycle(glob("*.gif"))

# build minimal test GUI
root = Tk()
image = PhotoImage()
Label(root, image=image).pack()

# start slide show and GUI main loop
show_next()
root.mainloop()

rawDescription: Python show two images with the after-method


code: QtGui.QFileDialog.getOpenFileName(None,"Load File","","Text (*.txt);;All Files (*)")

rawDescription: File menu in QtDesigner for Python

description: File menu in QtDesigner for Python


code:
import wx
class AScrolledWindow(wx.ScrolledWindow):
    def __init__(self, parent):
        self.parent = parent
        wx.ScrolledWindow.__init__(self, parent, -1, style=wx.TAB_TRAVERSAL)
        box = wx.BoxSizer(wx.VERTICAL)
        self.sizer = box
        self._labels = []
        self.button = wx.Button(self, -1, "Scroll Me")
        box.Add(self.button)
        self.Bind(wx.EVT_BUTTON,  self.OnClickTop, self.button)
        for y in xrange(1,30):
            self._labels.append(wx.StaticText(self, -1, "Label #%d" % (y,)))
            box.Add(self._labels[-1])
        self.SetSizer(self.sizer)
        fontsz = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT).GetPixelSize()
        self.SetScrollRate(fontsz.x, fontsz.y)
        self.EnableScrolling(True,True)

    def OnClickTop(self, event):
        self.Scroll(0, 50)

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Programmatic size change')
        sz = wx.BoxSizer(wx.VERTICAL)
        self.ascrolledwindow = AScrolledWindow(self)
        sz.Add(self.ascrolledwindow, 1, wx.EXPAND)
        self.SetSizer(sz)

def main():
    wxapp = wx.App()
    fr = TestFrame()
    fr.Show(True)
    fr.ascrolledwindow.Scroll(0,50)
    wxapp.MainLoop()

if __name__=='__main__':
    main()

rawDescription: How do I scroll to the bottom of a ScrolledWindow when using a sizer

description: Scroll to the bottom of a ScrolledWindow when using a sizer


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

D = np.random.randn(12*72).reshape((12, 72))
D[4, :] = np.nan
D[6, 6] = np.nan

D = np.ma.masked_invalid(D)

cmap = plt.get_cmap('bwr')
cmap.set_bad(color = 'k', alpha = 1.)

xbin = np.linspace(0, 12, 13)
ybin = np.linspace(-90, 90, 73)

fig = plt.figure()
ax = fig.add_subplot(111)
pl = ax.pcolormesh(xbin, ybin, D.T, cmap = cmap, edgecolors = 'None',
                vmin = -5, vmax = 5)
plt.show()

rawDescription: cmap.set_bad() not showing any effect with pcolor()

description: Cmap.set_bad() not showing any effect with pcolor()


code:
class MyCustomInlineForm(forms.ModelForm):
    matcher = forms.ChoiceField(choices=[(v['name'], v['name']) for v in matchers], label='Matcher')
    param0 = forms.CharField(label='First Param', required=False)
    param1 = forms.CharField(label='Second Param', required=False)

    def __init__(self, *args, **kwargs):
        super(MyCustomInlineForm, self).__init__(*args, **kwargs)
        if self.instance.pk:
            """ self.instance is the model for the current row.
                If there is a pk property that is not None, it means it's not
                a new, empty inline model but we are working with existing one."""
            self.initial['param0'], self.initial['param1'] = custom_way_to_load_params(self.instance)

    def save(self, commit=True):
        model = super(MyCustomInlineForm, self).save(True)
        param0 = self.cleaned_data['param0']
        param1 = self.cleaned_data['param1']
        custom_way_to_save_params(model, param0, param1)
        return model

class MyCustomInline(admin.TabularInline):
    min_num = 1
    extra = 0
    fields = ['matcher', 'param0', 'param1']
    model = MyModel
    form = MyCustomInlineForm

rawDescription: In django inline model admin, how to prefill extra fields with values


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

import simplejson as json
from xml import sax
try:
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

xml_string = '''<?xml version="1.0" encoding="iso-8859-1"?>
<n1:products xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:n7="http://foo.bar.tld" xmlns:n1="http://foo.bar.tld">
  <n1:product>
    <n1:status>
      <n7:created>2005-02-08T18:18:30.53</n7:created>
      <n7:updated>2008-09-18T10:29:58.26</n7:updated>
    </n1:status>
    <n1:productid>28321503</n1:productid>
    <n1:producttext>
      <n7:text>Some product info</n7:text>
      <n7:type>Info</n7:type>
    </n1:producttext>
    <n1:terms>
      <n7:term>
        <n7:number>1</n7:number>
        <n7:name>Term1</n7:name>
      </n7:term>
      <n7:term>
        <n7:number>2</n7:number>
        <n7:name>Term2</n7:name>
      </n7:term>
    </n1:terms>
  </n1:product>
</n1:products>
'''

def display(data):
    import pprint
    pp = pprint.PrettyPrinter(depth=10)
    pp.pprint(data)

class Element:
    def setData(self, key, value):
        self.__dict__[key] = value

    def setObject(self, key, object):
        if key in self.__dict__ and not isinstance(self.__dict__[key], (list, tuple)):
            prev_object = self.__dict__[key]
            self.__dict__[key] = []
            self.__dict__[key].append(prev_object)
            self.__dict__[key].append(object)
        elif key in self.__dict__:
            self.__dict__[key].append(object)
        else:
            self.__dict__[key] = object

    def jsonable(self):
        return self._traverse(self.__dict__)

    # http://stackoverflow.com/questions/1036409/recursively-convert-python-object-graph-to-dictionary/1118038#1118038
    def _traverse(self, obj):
        if isinstance(obj, dict):
            for k in obj.keys():
                obj[k] = self._traverse(obj[k])
            return obj
        elif hasattr(obj, "__iter__"):
            return [self._traverse(v) for v in obj]
        elif hasattr(obj, "__dict__"):
            data = dict([(key, self._traverse(value))
                for key, value in obj.__dict__.iteritems()
                if not callable(value) and not key.startswith('_')])
            return data
        else:
            return obj

class ObjBuilder(sax.ContentHandler):
    def __init__(self, node):
        sax.ContentHandler.__init__(self)
        self.obj = []
        self.node = node
        self.fetch = False
        self.__buffer = ''

    def startElementNS(self, name, qname, attrs):
        (ns, localname) = name
        if self.node == localname:
            self.fetch = True
            o = Element()
            self.rootobject = o
            self.obj.append(o)
        elif self.fetch:
            self.__buffer = ''
            o = Element()
            self.obj[-1].setObject(localname, o)
            self.obj.append(o)

    def characters(self,contents):
        if self.fetch:
            self.__buffer += contents.strip()

    def endElementNS(self, name, qname):
        (ns, localname) = name
        if self.node == localname:
            self.fetch = False
            display(self.rootobject.jsonable())
            data = self.rootobject.jsonable()
        elif self.fetch:
            if self.__buffer != '':
                self.obj[-2].setData(localname, self.__buffer)
            del self.obj[-1]
            self.__buffer = ''

if __name__ == '__main__':
    parser = sax.make_parser()
    parser.setContentHandler(ObjBuilder('product'))
    parser.setFeature(sax.handler.feature_namespaces, 1)

    inpsrc = sax.xmlreader.InputSource()
    inpsrc.setByteStream(StringIO(xml_string))
    parser.parse(inpsrc)

rawDescription: Python XML Sax to dictionary

description: Python XML Sax to dictionary


rawDescription: What is the most efficient way to strip all spaces and quotes from a string with python?


code:
if all(b==5 for b in base):
    do_something()

rawDescription: How to check all items of a list in python are equal to a?

description: Check all items of a list in python are equal to a


rawDescription: Shorting down many if clauses in Python

description: Shorting down many if clauses in Python


code:
n = 1
with open('sourcefile.txt') as input:
    with open('destination.txt', 'w') as output:
        for line in input:
            if line.startswith('%'):
                line = str(n) + line[1:]
                n += 1
            output.write(line)

rawDescription: regular expression - incremental replacement

description: Regular expression - incremental replacement


code:
import textwrap
s = 'This is a line of text over 10 characters'
textwrap.fill(s, width=10)

rawDescription: split text into lines by the number of characters

description: Split text into lines by the number of characters


code:
import re
regex = re.compile(r'(?P<foo>foo)|(?P<bar>bar)|(?P<baz>baz)')
data = {}
for match in regex.finditer('bar baz foo something'):
    data.update((k, v) for k, v in match.groupdict().items() if v is not None)

data

rawDescription: How to split string of biographical info into different dictionaries using regex, in Python?

description: Split string of biographical info into different dictionaries using regex, in Python


code:
#s3utils.py
from storages.backends.s3boto import S3BotoStorage
import  os

STATIC_ROOT = 'FOLDER/static'
MEDIA_ROOT = 'FOLDER/media'

class FixedS3BotoStorage(S3BotoStorage):
    def url(self, name):
        url = super(FixedS3BotoStorage, self).url(name)
        if name.endswith('/') and not url.endswith('/'):
            url += '/'
        return url

StaticRootS3BotoStorage = lambda: FixedS3BotoStorage(location=STATIC_ROOT)
MediaRootS3BotoStorage  = lambda: FixedS3BotoStorage(location=MEDIA_ROOT)

rawDescription: Django static file URLS are broken

description: Django static file URLS are broken


code:
def debug_info ( self ):
    for ( key, value ) in self.__dict__.items():
        print( key, '=', value )

rawDescription: python serval variables combine into a dict?

description: Python serval variables combine into a dict


code:
import __builtin__ as builtins # import builtins in python 3
llen = len
builtins.len = lambda a:llen(a) - 1

rawDescription: Overriding len in __init__.py - python

description: Overriding len in __init__.py - python


code:
class Rectangle:
    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h

    def expand(self, expand_num): # Method to expand/shrink the original rectangle
        return Rectangle(self.x - expand_num, self.y - expand_num, self.w + expand_num * 2, self.h + expand_num * 2)

    def __repr__(self):
        return "Rectangle("+str(self.x)+", " + str(self.y)+ ", " + str(self.w) + ", " + str(self.h) + ")"
r_orig = Rectangle(30,40,100,110)

try_expand = 0
while try_expand != "N" and try_expand !="n":
    input_num = input("How much would you like to expand or shrink r by? ")
    expand_num = int(input_num)
    print("Rectangle r expanded/shrunk = ", r_orig.expand(expand_num))
    try_expand = input("Would you like to try expanding or shrinking again? Y or N  ")
    print("")

rawDescription: Can't Find Semantic Issue

description: Can't Find Semantic Issue


rawDescription: GridSearchCV: print some expression each time a function completes a loop


rawDescription: basic bit flip algorithm

description: Basic bit flip algorithm


code:
optimizer = tf.train.AdagradOptimzer(0.01)

first_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                     "scope/prefix/for/first/vars")
first_train_op = optimizer.minimize(cost, var_list=first_train_vars)

second_train_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                      "scope/prefix/for/second/vars")
second_train_op = optimizer.minimize(cost, var_list=second_train_vars)

rawDescription: "freeze" some variables/scopes in tensorflow: stop_gradient vs passing variables to minimize


code:
def worker(self, queue):
    while True:
        tweet = queue.get()
        self.append(tweet)

rawDescription: python multiprocessing accessing data from multiprocessing queue not reading all data

description: Python multiprocessing accessing data from multiprocessing queue not reading all data


code:
import pylab as pl
import numpy as np

data = np.random.normal(size=10000)
pl.hist(data, bins=np.logspace(0.1, 1.0, 50))
pl.gca().set_xscale("log")
pl.show()

rawDescription: How to have logarithmic bins in a Python histogram

description: Have logarithmic bins in a Python histogram


code:
import sys
import os

def my_open(filename, mode):
    for path in sys.path:
        try:
            return open(os.path.join(path, filename), mode)
        except IOError:
            continue
    return None

rawDescription: IPython Search PYTHONPATH for file

description: IPython Search PYTHONPATH for file


code:
from django.contrib import admin

from models import News, NewsLang

class NewsLangInline(admin.TabularInline):
    model = NewsLang

class NewsAdmin(admin.ModelAdmin):
    inlines = [NewsLangInline]
admin.site.register(News, NewsAdmin)

rawDescription: Multiple model edition in django-admin

description: Multiple model edition in django-admin


code:
global counter
counter = 0

def space_bar():
    global counter
    counter =  counter + 1
    if counter % 2 != 0:
        pendown()
        begin_fill()
    else:
        penup()
        end_fill()

rawDescription: Filling in a turtle drawing after assigning to a key


code: suffix = ' ' * nb_spaces

rawDescription: create a string of variable length in python

description: Create a string of variable length in python


code: [i for j in a.values() for i in j.values()]

rawDescription: single list comprehension to unpack nested dictionary

description: Single list comprehension to unpack nested dictionary


rawDescription: Python : seperate particular string from group of strings


code:
from operator import itemgetter
for v in d.values():
    v.sort(key=itemgetter(2))

a = sorted(d.items())

rawDescription: sort list of tuples in a dictionary

description: Sort list of tuples in a dictionary


code:
from glob import iglob
from shutil import copy
from os.path import join

def copy_files(src_glob, dst_folder):
    for fname in iglob(src_glob):
        copy(fname, join(dst_folder, fname))

if __name__=='__main__':
    copy_files("123*.xml", "out_folder")

rawDescription: Python Windows File Copy with Wildcard Support


code:
import subprocess
subprocess.call('ipconfig /all')

rawDescription: How to list network interfaces, its configuration IP,netmask and gateway in Python/Windows

description: List network interfaces, its configuration IP,netmask and gateway in Python/Windows


code:
fmts =["12 sheet","4 sheet","48 sheet","6 sheet", "busrear", "phonebox","train"]
fmts.sort(key=lambda x: (int(x.split(None, 1)[0]) if x[:1].isdigit() else 999, x))
fmts

rawDescription: Python sort array of string with integers inside


code:
colors = { 'Lion' : ['gold', 'orange'],
           'Tiger' : ['blackorange', 'whiteblue']
         }
class Sample(Base):
    __tablename__ = 'sample'
    id = Column(Integer, primary_key=True)
    type = Column(Enum(u'Lion', u'Tiger', name='type'))
    color = Column(Enum('gold', 'orange', 'blackorange', 'whiteblue', name='color'))

from sqlalchemy import event

def my_check_sample_listener(mapper, connection, target):
    assert target.color in colors[target.type]

event.listen(Sample, 'before_insert', my_check_sample_listener)
event.listen(Sample, 'before_update', my_check_sample_listener)

sample = Sample(type='Tiger', color='orange')
session.add(sample)
session.commit() # Fails

sample = Sample(type='Lion', color='orange')
session.add(sample)
session.commit() # OK

sample.type = 'Tiger'
sample.color = 'whiteblue'
session.commit() # OK

sample.color = 'orange'
session.commit() # Fails

rawDescription: SQLAlchemy modelling a constraint based on a dictionary


code:
dogs = {}

def create_dog(name, age, gender):
    # maybe check if already exists
    dogs[name] = Dog(name, age, gender)

rawDescription: Trying to create a object from a class by calling a function


code:
from django.db.models import Sum
total_credit = Employees.objects.all(). \
               aggregate(total_credit=Sum('credit'))['total_credit']

return render_to_response('templatename.html',
                          {'total_credit': total_credit},
                          context_instance=RequestContext(request))

rawDescription: Is it possible to define and later redefine a variable in django templates?


code: return super(Parent, cls).__new__(cls, *args, **kwargs)

rawDescription: Python multiple inheritance: which __new__ to call?


rawDescription: matplotlib grouping values of x axis

description: Matplotlib grouping values of x axis


code:
d = {a: b for a, b in zip(range(1,60), range(40, 301))}
from pprint import pprint
pprint(d)

rawDescription: Printing a long dictionary in the PyCharm console


code:
import wx

class MainWindow(wx.Frame):
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)

        self.panel = wx.Panel(self)
        self.spin = wx.SpinCtrl(self.panel)
        self.button = wx.Button(self.panel, label="Update")

        self.sizer = wx.BoxSizer()
        self.sizer.Add(self.spin)
        self.sizer.Add(self.button)

        self.panel.SetSizerAndFit(self.sizer)
        self.Show()

        # Use EVT_CHAR_HOOK on Frame insted of wx.EVT_KEY_UP on SpinCtrl
        # to disable "on Enter go to next widget" functionality
        self.Bind(wx.EVT_CHAR_HOOK, self.OnKey)
        self.button.Bind(wx.EVT_BUTTON, self.OnUpdate)

        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(500)

        self.value = 0

    def OnKey(self, e):
        if e.GetKeyCode() == wx.WXK_RETURN: # Is the key ENTER?
            self.value = self.spin.GetValue() # Read SpinCtrl and set internal value
        else: # Else let the event out of the handler
            e.Skip()

    def OnUpdate(self, e):
        self.value = self.spin.GetValue() # Read SpinCtrl and set internal value

    def OnTimer(self, e):
        # Show internal value
        print(self.value)

app = wx.App(False)
win = MainWindow(None)
app.MainLoop()

rawDescription: wxpython - timers and binding text entry to enter

description: Wxpython - timers and binding text entry to enter


code: FROM = "Foo <contact@foo.com>"

rawDescription: How do I change the from line on a mail sent using smtplib?

description: Change the from line on a mail sent using smtplib


code:
import numpy as np
x = np.zeros(10)
positions = np.random.choice(np.arange(10), 3, replace=False)
x[positions] = np.random.normal(0,1,3)
x

rawDescription: Python: How to generate a vector with nonzero entries at random positions?


code:
s = [1, 2, 3, 5, 6, 9, 10, 11]

def runs(iterable):
    iterator = iter(iterable)
    start = end = next(iterator)

    for item in iterator:
        if item != end + 1:
            yield (start, end)
            start = item

        end = item

    yield (start, end)

lows, highs = zip(*runs(s))

lows
#>>> (1, 5, 9)

highs
#>>> (3, 6, 11)

rawDescription: Find the beginning and ending of consecutive numbers in a list


code:
from PIL import Image
import numpy as np

im = Image.open(filename)
data = np.asarray(im, dtype=np.uint8)
np.savetxt(out_filename, data.ravel(), fmt='%i')

rawDescription: How do I retrieve all of the raw rgb data in .txt format (eg. 255, 255, 255) from a set of images (.png, .jpg, .bmp, etc.)

description: Retrieve all of the raw rgb data in .txt format (eg. 255, 255, 255) from a set of images (.png, .jpg, .bmp, etc.)


rawDescription: Add a new root node to django-mptt tree

description: Add a new root node to django-mptt tree


rawDescription: How to find 2nd same word in a given string using python?

description: Find 2nd same word in a given string using python


code:
def __init__(self, *args, **kwargs):
    translation.activate('nl')
    super(SurveyForm, self).__init__(*args, **kwargs)
    self.fields['Q1'] = ...
    # or
    self.Q1 = ...

rawDescription: Django forms.py initialized wrongly?

description: Django forms.py initialized wrongly


code:
import csv
import random

with open("input.csv") as f:
    r = csv.reader(f)
    header, l = next(r), list(r)

a = [x[0] for x in l]
random.shuffle(a)

b = [x[1] for x in l]
random.shuffle(b)

with open("random.csv", "wb") as f:
    csv.writer(f).writerows([header] + zip(a, b))

rawDescription: using python to randomize csv file

description: Using python to randomize csv file


code: [data[indexes[i]:indexes[i+1]] for i in range(len(indexes)-1)]

rawDescription: Python list sorting conundrum

description: Python list sorting conundrum


code:
from googlemaps import GoogleMaps
gmaps = GoogleMaps(API_KEY)
lat, lng = gmaps.address_to_latlng(address)

rawDescription: How to get coordinates of address from Python

description: Get coordinates of address from Python


rawDescription: Django Many-To-Many filtering

description: Django Many-To-Many filtering


code: cell.value = 'Helloooo ladies'

rawDescription: OpenPyXL + How can I search for content in a cell in Excel, and if the content matches the search criteria update the content?


code:
tags="Hey guys! #stackoverflow really #rocks #rocks #announcement"
{tag.strip("#") for tag in tags.split() if tag.startswith("#")}

rawDescription: An elegant way to get hashtags out of a string in Python?


rawDescription: is it possible to work with multiline statements using doctest?

description: Is it possible to work with multiline statements using doctest


rawDescription: Python - Split string of numeric values with unknown delimiters


code:
import numpy as np
v = np.arange(10)
a = v[3:4]
a[0] = 100
v

rawDescription: What happens to Numpy/Scipy pointers when they are copied to local variable?


code:
def insomniac(duration):
    pass  # don't sleep

_original_sleep = time.sleep
time.sleep = insomniac

def dont_write(stuff):
    pass  # don't write

_original_write = sys.stdout.write
sys.stdout.write = dont_write

rawDescription: How to use AST to make specific statements commented out in Python?

description: Use AST to make specific statements commented out in Python


code:
import cv2
new_data = data.T[::-1]

# calculate the pixel coordinates of the
# computational domain corners in the data array
w,e,s,n = map_extent
dx = float(e-w)/new_data.shape[1]
dy = float(n-s)/new_data.shape[0]
x = (lon.ravel()-w)/dx
y = (n-lat.ravel())/dy

computational_domain_corners = np.float32(zip(x,y))

data_array_corners = np.float32([[0,new_data.shape[0]],
                                 [0,0],
                                 [new_data.shape[1],new_data.shape[0]],
                                 [new_data.shape[1],0]])

# Compute the transformation matrix which places
# the corners of the data array at the corners of
# the computational domain in data array pixel coordinates
tranformation_matrix = cv2.getPerspectiveTransform(data_array_corners,
                                                   computational_domain_corners)

# Make the transformation making the final array the same shape
# as the data array, cubic interpolate the data placing NaN's
# outside the new array geometry
mapped_data = cv2.warpPerspective(new_data,tranformation_matrix,
                                  (new_data.shape[1],new_data.shape[0]),
                                  flags=2,
                                  borderMode=0,
                                  borderValue=np.nan)

rawDescription: Very slow interpolation using `scipy.interpolate.griddata`


code:
Guess_Battleship_Board_Row = int(input("Guess the X coordinate:")) - 1
Guess_Battleship_Board_Column = int(input("Guess the Y coordinate:")) - 1

rawDescription: How to make this Battleship game more user friendly in terms of values?

description: Make this Battleship game more user friendly in terms of values


rawDescription: how do I sort a python list of dictionaries given a list of ids with the desired order?

description: Sort a python list of dictionaries given a list of ids with the desired order


code:
import HTMLParser     # html.parser in Python 3
h = HTMLParser.HTMLParser()
h.unescape('Bayern München')

rawDescription: 'Bayern München' -> 'Bayern München'

description: 'Bayern München' -> 'Bayern München'


code:
@csrf_exempt
def create_subject(request, subject):
    subject, created= Subjects.objects.get_or_create(
        name=subject,
        user=request.user,
        created_by=request.user)

    return HttpResponse(
        simplejson.dumps(
            list(models.Subject.objects.filter(id=subject.id).values()),
            indent=4
        )
    )

rawDescription: Django. Get values for all fields in one object


code:
UNESCAPED_SLASH_RE = re.compile(r'(?<!\\)/')
ptn, repl = UNESCAPED_SLASH_RE.split(line[1:])[:-1]

rawDescription: splitting and escaped forward slashes in Python

description: Splitting and escaped forward slashes in Python


code: array = [row for row in array if row['val'] >= 11]

rawDescription: Pop value from if less than x

description: Pop value from if less than x


code:
features = pipeline.named_steps['tfidf'].get_feature_names()
print(features[pipeline.named_steps['l1'].coef_ != 0])

rawDescription: Recover named features from L1 regularized logistic regression


code:
def delete_file(request_id, response, exception):
  if exception is not None:
    # Do something with the exception
    pass
  else:
    # Do something with the response
    pass

batch = service.new_batch_http_request(callback=delete_file)

for file in children["items"]:
  batch.add(service.files().delete(fileId=file["id"]))

batch.execute(http=http)

rawDescription: How to delete multiple files at once using Google Drive API

description: Delete multiple files at once using Google Drive API


code:
import sys
sys.path.append('c:/python')
import hello

hello.hello() # hello,world

rawDescription: How to install third party modules and libraries through flask web app

description: Install third party modules and libraries through flask web app


code: stateDF[stateDF['abbreviation_column'] == targetState]['Full Name']

rawDescription: Python Pandas Simple Lookup - Stumped

description: Python Pandas Simple Lookup - Stumped


code:
import tkinter

window = tkinter.Tk()
window.grid()

# changed to integers so we can loop through the
# values in press() and use them as indices
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def buttonCreator(labels):
    n = 0
    button = []
    for x in range(0, 3):
        for y in range(0, 3):
            if n <= len(labels) - 1:
                button.append(tkinter.Button(window, text=labels[n],
                                             command=lambda x=labels[n]:press(x)))
                button[n].grid(row=x, column=y)
            n += 1
    return button # added a return statement

def press(value):
    for x in numbers:
        # index is x-1 because 1 is in button[0], etc
        button[x-1]['relief'] = 'sunken' if x == value else 'raised'

button = buttonCreator(numbers)
window.mainloop()

rawDescription: How to associate the same operation to a set of buttons programmatically in tkinter?

description: Associate the same operation to a set of buttons programmatically in tkinter


code:
import asyncio
import aioredis

async def handle_msg(msg):
    print('Got Message:', msg)
    i = int(msg['sleep_for'])
    print('Sleep for {}'.format(i))
    await asyncio.sleep(i)
    print('End sleep')

async def reader(ch):
    while (await ch.wait_message()):
        msg = await ch.get_json()
        asyncio.ensure_future(handle_msg(msg))

async def main():
    sub = await aioredis.create_redis(('localhost', 6379))
    res = await sub.subscribe('chan:1')
    ch1 = res[0]
    tsk = await reader(ch1)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

rawDescription: AIORedis and PUB/SUB aren't asnyc

description: AIORedis and PUB/SUB aren't asnyc


rawDescription: SparkSQL: HQL script in file to be loaded on Python code


code:
class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        if kwargs.get('instance'):
            self.fields['myfield'].initial = 'custom value'

    class Meta:
        model = MyModel

rawDescription: Override form data when editing an object


code:
from collections import deque
from itertools import islice

def get_tuples(gen, n):
    q = deque(islice(gen, n))   # pre-load the queue with `n` values
    while q:                    # run until the queue is empty
        yield tuple(q)          # yield a tuple copied from the current queue
        q.popleft()             # remove the oldest value from the queue
        try:
            q.append(next(gen)) # try to add a new value from the generator
        except StopIteration:
            pass                # but we don't care if there are none left

rawDescription: Accessing consecutive items when using a generator


code:
#!/usr/bin/env python2

import pandas as p
from BeautifulSoup import BeautifulSoup
import urllib2
import StringIO

def main():
    url = "URL"
    html = urllib2.urlopen(url)
    soup = BeautifulSoup(html)
    # pre is the tag that the data is within
    tab_sepd_vals = soup.pre.string

    # make the StringIO object
    tsv = StringIO.StringIO(tab_sepd_vals)

    # something like this
    data = p.read_csv(tsv, sep='\t')

    # then what you had
    process(data)

rawDescription: How can I use pandas to parse CSV already loaded from somewhere else?

description: Use pandas to parse CSV already loaded from somewhere else


code: "<?xml-stylesheet type=\"text/xsl\" href=\"audit_stylesheet.xsl\" ?>"

rawDescription: How to add xml-stylesheet tag to XML file using python?

description: Add xml-stylesheet tag to XML file using python


code:
FRAMERATE = 30
clock = pygame.time.Clock()

while True:
    clock.tick(FRAMERATE)
    for event in pygame.event.get():
        ...

rawDescription: pygame program runs way too fast in windows 8 virtual machine

description: Pygame program runs way too fast in windows 8 virtual machine


code:
import re
s = ['LastModifiedTime','UserProfile','User-Profile']
[re.sub(r'(^|([a-z])\W?)([A-Z])', lambda m: m.group(2)+'_'+ m.group(3).lower() if m.group(1) else m.group(3).lower(), i) for i in s]

rawDescription: How can I write this Ruby gsub using Python re?

description: Write this Ruby gsub using Python re


rawDescription: Python get direct attributes of a class

description: Python get direct attributes of a class


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

names = ['foo', 'bar', 'baz']
x  = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
colors = ['crimson', 'burlywood', 'chartreuse']

y = zip(*x)
groups = len(x)
members = len(y)
pos = np.arange(groups)
width = 1. / (1 + members)

fig, ax = plt.subplots()
for idx, (serie, color) in enumerate(zip(y, colors)):
    ax.bar(pos + idx * width, serie, width, color=color)

ax.set_xticks(pos + width)
ax.set_xticklabels(names)

plt.show()

rawDescription: Plot x-ticks in histogram matplotlib

description: Plot x-ticks in histogram matplotlib


code:
import imaplib
import email

def extract_body(payload):
    if isinstance(payload,str):
        return payload
    else:
        return '\n'.join([extract_body(part.get_payload()) for part in payload])

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login("user", "password")
conn.select()
typ, data = conn.search(None, 'UNSEEN')
try:
    for num in data[0].split():
        typ, msg_data = conn.fetch(num, '(RFC822)')
        for response_part in msg_data:
            if isinstance(response_part, tuple):
                msg = email.message_from_string(response_part[1])
                subject=msg['subject']
                print(subject)
                payload=msg.get_payload()
                body=extract_body(payload)
                print(body)
        typ, response = conn.store(num, '+FLAGS', r'(\Seen)')
finally:
    try:
        conn.close()
    except:
        pass
    conn.logout()

rawDescription: Reading and parsing email from Gmail using C#, C++ or Python


code: codecs.encode(b'\x12', 'hex_codec')

rawDescription: How to use the 'hex' encoding in Python 3.2 or higher?

description: Use the 'hex' encoding in Python 3.2 or higher


code:
class Transaction(models.Model):
    @property
    def dec_amount(self):
        return Decimal(self.amount / 100).quantize(Decimal('0.01'))

rawDescription: Convert one QuerySet element in all objects from int to Decimal


code:
import re

bytegroup = r'(_[0-9a-z]{2})+'

def replacer(match):
    return toUtf(match.group())

rtext = re.sub(bytegroup, replacer, r, flags=re.I)

rawDescription: UTF-8 decoding with ascii code in it with Python


code:
import re
s = 'this1234is5678it'
re.split('(\d+)',s)

rawDescription: python: splitting strings where numbers and letters meet (1234abcd-->1234, abcd)

description: Python: splitting strings where numbers and letters meet (1234abcd-->1234, abcd)


code:
def singular_item(collection):
    return next(iter(collection), None)

rawDescription: Get one or None from collection

description: Get one or None from collection


code:
if os.path.exists("myWxApplication.exe"):
     self.SetIcon(wx.Icon("myWxApplication.exe",wx.BITMAP_TYPE_ICO))

rawDescription: How to use py2exe icon_resources in wxPython application?

description: Use py2exe icon_resources in wxPython application


code:
import inspect
import decorator

@decorator.decorator
def decor(my_func,*args,**kw):
    result=my_func(*args,**kw)
    return result

@decor
def my_func(key=1, value=False):
    pass
decorated_argspec = inspect.getargspec(my_func)
print(decorated_argspec)
# ArgSpec(args=['key', 'value'], varargs=None, keywords=None, defaults=(1, False))

rawDescription: How can I get the argument spec on a decorated function?

description: Get the argument spec on a decorated function


code:
import matplotlib.pyplot as plt
import numpy as np
from numpy.random import seed, randint

# Build function that connects two points with a curved line,
# and an arrow on the middle of it

seed(1679)

narrow = 3
rad_one = 50
numpoints = 3

random_points = list(randint(1,20,[numpoints,4]))
rpoints = [[(a,b),(c,d)] for a,b,c,d in random_points]

def curvline(start,end,rad,t=100,arrows=1,push=0.8):
    #Compute midpoint
    rad = rad/100.
    x1, y1 = start
    x2, y2 = end
    y12 = (y1 + y2) / 2
    dy = (y2 - y1)
    cy = y12 + (rad) * dy
    #Prepare line
    tau = np.linspace(0,1,t)
    xsupport = np.linspace(x1,x2,t)
    ysupport = [(1-i)**2 * y1 + 2*(1-i)*i*cy + (i**2)*y2 for i in tau]
    #Create arrow data
    arset = list(np.linspace(0,1,arrows+2))
    c = zip([xsupport[int(t*a*push)] for a in arset[1:-1]],
                      [ysupport[int(t*a*push)] for a in arset[1:-1]])
    dt = zip([xsupport[int(t*a*push)+1]-xsupport[int(t*a*push)] for a in arset[1:-1]],
                      [ysupport[int(t*a*push)+1]-ysupport[int(t*a*push)] for a in arset[1:-1]])
    arrowpath = zip(c,dt)
    return xsupport, ysupport, arrowpath

def plotcurv(start,end,rad,t=100,arrows=1,arwidth=.25):
    x, y, c = curvline(start,end,rad,t,arrows)
    plt.plot(x,y,'k-')
    for d,dt in c:
        plt.arrow(d[0],d[1],dt[0],dt[1], shape='full', lw=0,
                  length_includes_head=False, head_width=arwidth)
    return c

#Create figure
figure = plt.figure()
ax = plt.subplot(111)
for n1,n2 in rpoints:
    #First line
    plotcurv(n1,n2,rad_one,200,narrow,0.5)
    #Second line
    plotcurv(n2,n1,rad_one,200,narrow,0.5)
ax.set_xlim(0,20)
ax.set_ylim(0,20)
plt.show

rawDescription: Draw rounded fancyarrowpatch with midpoint arrow in matplotlib


code:
def concatenate_attributes(self):
    self.a = np.hstack([o.a for o in self.my_class_inst])
    self.arr = np.hstack([o.arr for o in self.my_class_inst])

rawDescription: concatenate numpy arrays that are class instance attributes in python

description: Concatenate numpy arrays that are class instance attributes in python


code:
import itertools, string

map(''.join, itertools.product(string.ascii_lowercase, repeat=3))
map(''.join, itertools.product(string.ascii_letters, repeat=3))
map(''.join, itertools.product(string.ascii_letters + string.digits, repeat=3))

rawDescription: How to generate a continuous string?

description: Generate a continuous string


code:
def preprocess_html(self, soup):
    if 'MY_MAGIC_KEYWORD' in soup.html.head.title.string.upper():
        return soup
    if len(soup.findAll(text=re.compile('my_magic_keyword', re.IGNORECASE))) > 0:
        return soup
    return None

rawDescription: Is there a way to filter feed articles by content in Calibre recipies?


code:
p = Person(name='Ami')
session.add(p)
session.commit()

rawDescription: SQLAlchemy events are not working

description: SQLAlchemy events are not working


code:
def headName():
    return Name[0].upper()
def tailName():
    return Name[1:].lower()

Name = input("Please enter a name ")
print(headName(), tailName(), sep="")

rawDescription: How to concatenate two functions on the same line?

description: Concatenate two functions on the same line


code:
import pandas as pd
import pandas.io.data as web
import datetime

start = datetime.datetime(2010, 9, 1)
end = datetime.datetime(2015, 5, 31)
df = web.DataReader("F", 'yahoo', start, end)

#Ensure date sorted --required
df = df.sort_index()

#identify custom season and set months june-august to null
count = 0
season = 1
for i,row in df.iterrows():
    if i.month in [9,10,11,12,1,2,3,4,5]:
        if count == 1:
            season += 1
        df.set_value(i,'season', season)
        count = 0
    else:
        count = 1
        df.set_value(i,'season',None)

#new data frame excluding months june-august
df_data = df[~df['season'].isnull()]
df_data['Adj Close'].groupby(df_data.season).mean()

rawDescription: Annual mean for Pandas dataset but not starting in January


code:
import os
from ftplib import FTP

# read in text file containing server login information and jam into dictionary
with open('server_file.txt','r') as tmp:
    servers = {}
    for r in tmp.read().split('\n'):
        rs = r.split(',') # split r by comma
        servers[rs[0]] = {'uname':rs[1],'pwd':[rs[2]]}

# if you want to create a new directory to save the file to
heute = dt.datetime.strftime(dt.datetime.today(),'%Y%m%d')
if os.path.isdir('my_dir' + heute)==False:
    os.mkdir('my_dir' + heute)

for s in servers:
    ftp = FTP(s,servers[s]['uname'],servers[s]['pwd'])
    ftp.cwd('desired_subdir')

    # if you want to download the last file I would us nlst
    with open('local_file','wb') as lf:
        ftp.retrbinary('RETR' + ftp.nlst()[-1], lf.write, 8*1024)

rawDescription: How to download a file from 3ftp sites

description: Download a file from 3ftp sites


code:
a = ['NO YES NO NO', 'YES YES NO YES', 'NO NO YES NO', ]
[s.split() for s in a]

rawDescription: How to make a list to be a list in a list

description: Make a list to be a list in a list


code: ascii_string = unicodedata.normalize('NFKD', unicode_string).encode('ascii','ignore')

rawDescription: Processing Utf-8 data in python

description: Processing Utf-8 data in python


code:
def custom_to_dict(self):
    return {
      'propertyId': self.propertyID,
      'propertyParentKey': self.propertyParentKey.urlsafe(),
      'propertyItems': [key.urlsafe() for key in self.propertyItems]
    }

rawDescription: Serializing NDB model with repeated key property


code:
def side_effect(value):
    return value + 1
m = MagicMock(side_effect=side_effect)
m(1)
m(2)
m.mock_calls

rawDescription: Mocking python function based on input arguments


code:
import re
row = '1        1.2345  5.2            4.3'
row_digits = re.findall('\d+\.*\d*', row)

rawDescription: How to separate columns with indefinite number of spaces

description: Separate columns with indefinite number of spaces


code:
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt4 import QtGui
from PyQt4.QtOpenGL import *

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()

        self.widget = glWidget(self)

        self.button = QtGui.QPushButton('Test', self)

        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.widget)
        mainLayout.addWidget(self.button)

        self.setLayout(mainLayout)

class glWidget(QGLWidget):

    def __init__(self, parent):
        QGLWidget.__init__(self, parent)
        self.setMinimumSize(640, 480)

    def paintGL(self):

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        glTranslatef(-2.5, 0.5, -6.0)
        glColor3f( 1.0, 1.5, 0.0 );
        glPolygonMode(GL_FRONT, GL_FILL);

        glBegin(GL_TRIANGLES)
        glVertex3f(2.0,-1.2,0.0)
        glVertex3f(2.6,0.0,0.0)
        glVertex3f(2.9,-1.2,0.0)
        glEnd()

        glFlush()

    def initializeGL(self):

        glClearDepth(1.0)
        glDepthFunc(GL_LESS)
        glEnable(GL_DEPTH_TEST)
        glShadeModel(GL_SMOOTH)

        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        gluPerspective(45.0,1.33,0.1, 100.0)
        glMatrixMode(GL_MODELVIEW)

if __name__ == '__main__':
    app = QtGui.QApplication(['Yo'])
    window = MainWindow()
    window.show()
    app.exec_()

rawDescription: PyQt OpenGL: drawing simple scenes

description: PyQt OpenGL: drawing simple scenes


code:
def equal(u, v):
   if u.D != v.D: return False  # domains must be equal
   return all(u.f.get(x, 0) == v.f.get(x, 0) for x in u.D)

rawDescription: Comparing Equality of Vectors as a Dictionary


code: connn.setopt(pycurl.WRITEFUNCTION, lambda data: real_impl(data, param1, param2))

rawDescription: pyCurl WRITEFUNCTION callback with parameters

description: PyCurl WRITEFUNCTION callback with parameters


code:
fid = open('output.csv','w')
Z1 = (kernel(positions).T, X.shape)
Z = kernel(positions).T
#for currentIndex,elem in enumerate(positions):
for currentIndex,elem in enumerate(Z):
  #if Z1[currentIneex]>0:
  s1 = '%f %f %f\n'%(positions[0][currentIndex], positions[1][currentIndex], Z[currentIndex] )

  fid.write(s1)
fid.close()

rawDescription: Save 2-D gaussian_kde output/meshgrid to csv python


code:
import MySQLdb
from lxml import etree
import config

def fast_iter(context, func, args=[], kwargs={}):
    # http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
    # Author: Liza Daly
    for event, elem in context:
        func(elem, *args, **kwargs)
        elem.clear()
        while elem.getprevious() is not None:
            del elem.getparent()[0]
    del context

def extract_paper_elements(element,cursor):
    pub={}
    pub['InventoryID']=element.attrib['ID']
    try:
        pub['PublisherClassID']=element.xpath('PublisherClass/@ID')[0]
    except IndexError:
        pub['PublisherClassID']=None
    pub['PublisherClassID']=element.xpath('PublisherClass/@ID')[0]
    for key in ('Name','Type','ID'):
        try:
            pub[key]=element.xpath(
                'PublisherClass/Publisher/PublisherDetails/{k}/text()'.format(k=key))[0]
        except IndexError:
            pub[key]=None
    sql='''INSERT INTO Publishers (InventoryID, PublisherClassID, Name, Type, ID)
           VALUES (%s, %s, %s, %s, %s)
        '''
    args=[pub.get(key) for key in
          ('InventoryID', 'PublisherClassID', 'Name', 'Type', 'ID')]
    print(args)
    # cursor.execute(sql,args)
    for bookdetail in element.xpath('descendant::BookList/Listing/Book/BookDetail'):
        pub['BookDetailID']=bookdetail.attrib['ID']
        for key in ('BookName', 'Author', 'Pages', 'ISBN'):
            try:
                pub[key]=bookdetail.xpath('{k}/text()'.format(k=key))[0]
            except IndexError:
                pub[key]=None
        sql='''INSERT INTO Books
               (PublisherID, BookDetailID, Name, Author, Pages, ISBN)
               VALUES (%s, %s, %s, %s, %s, %s)
            '''
        args=[pub.get(key) for key in
              ('ID', 'BookDetailID', 'BookName', 'Author', 'Pages', 'ISBN')]
        # cursor.execute(sql,args)
        print(args)

def main():
    context = etree.iterparse("book.xml", events=("end",), tag='Inventory')
    connection=MySQLdb.connect(
        host=config.HOST,user=config.USER,
        passwd=config.PASS,db=config.MYDB)
    cursor=connection.cursor()

    fast_iter(context,extract_paper_elements,args=(cursor,))

    cursor.close()
    connection.commit()
    connection.close()

if __name__ == '__main__':
    main()

rawDescription: How to parse this huge XML file with nested elements using lxml the efficient way?

description: Parse this huge XML file with nested elements using lxml the efficient way


code:
try:
    a = a
except NameError:
    a = "new"

rawDescription: Is there any expression in python that similar to ruby's ||=


code:
def products(A, B):
    out = []
    n = len(A)
    for offset in range(n):
        out.append(sum( A[i] * B[ (i + offset) % n ] for i in range(n)))
    return out

rawDescription: python: Sum of products of values in two nested lists

description: Python: Sum of products of values in two nested lists


code:
class _Version(object):
    def __init__(self, file):
        self.file = weakref.proxy(file)

rawDescription: Is this a good use case for weakref?

description: Is this a good use case for weakref


rawDescription: Converting a byte array to an image file


code:
sig = add.s(1, 2)
task = sig.apply_async()
task.revoke(terminate=True)

# ...

task  = sig.apply_async()  # Will relaunch a task with the same parameters.

rawDescription: How to re-launch (unrevoke) a revoked task with Celery?

description: Re-launch (unrevoke) a revoked task with Celery


code: self.mainListContent[:] = [new, list, of, widgets]

rawDescription: Changing contents of currently displayed listbox in urwid/python2.6


code:
def regenerate_mesh(source_mesh, vertices_to_delete, poly_to_delete):
    points = source_mesh.getPoints(om.MSpace.kWorld)
    num_points = len(points)
    i = 0
    while i < num_points:
        p1 = points[i]
        for p2 in vertices_to_delete['points']:
            if p1.x == p2.x and p1.y == p2.y and p1.z == p2.z:
                points.remove(i)
                num_points -= 1
                break
        else:
            i += 1

    polygon_counts, polygon_connects = source_mesh.getVertices()

    i = j = 0
    polygon_counts_length = len(polygon_counts)

    while i < polygon_counts_length:
        k = 0
        for poly in poly_to_delete:
            if poly == polygon_connects[j:j+polygon_counts[i]]:
                for l in range(polygon_counts[i]):
                    polygon_connects.remove(j)
                polygon_counts.remove(i)
                polygon_counts_length -= 1
                break
        else:
            while k < polygon_counts[i]:
                if polygon_connects[j+k] in vertices_to_delete['indices']:
                    for l in range(polygon_counts[i]):
                        polygon_connects.remove(j)
                    polygon_counts.remove(i)
                    polygon_counts_length -= 1
                    break
                k += 1
            else:
                j += k
                i += 1

    # update indices
    for vertex in sorted(vertices_to_delete['indices'], reverse=True):
        for index, new_vertex in enumerate(polygon_connects):
            if new_vertex > vertex:
                polygon_connects[index] -= 1
    new_mesh = om.MFnMesh()
    new_mesh.create(points, polygon_counts, polygon_connects)

rawDescription: Separate polygon shells into separate meshes in Maya API


code:
def grouper(n, iterable, fillvalue=None):
    "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

rawDescription: Taking four items instead of on in a for loop


code:
import threading

def do_unzipping():
    _.unzip(filePath ,uploadPath)

    update_ui("Unzipping Finished!")

threading.Thread(target = do_unzipping).start()
continue_with_unblocked_ui()

rawDescription: python zip extraction in background

description: Python zip extraction in background


code: any(7 == i[0] for i in a)

rawDescription: Find item in list inside list

description: Find item in list inside list


rawDescription: The best way to sort indexes in Text widget

description: Sort indexes in Text widget


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

x = np.linspace(0, 100, 1000)
y = np.sin(x)

ax = plt.subplot(111)
ax.plot(x, y)
ax.set_ylim(-50, 10)

ax2 = plt.axes([0.2, 0.2, .6, .2])
ax2.plot(x, y)

mark_inset(ax, ax2, loc1=2, loc2=1, fc="none", ec="0.5")

plt.show()

rawDescription: Maptlotlib: add zoomed region of a graph with anisotropic (axis dependent) zoom


code:
fig, axs = plt.subplots(2,5, figsize=(15, 6), facecolor='w', edgecolor='k')
fig.subplots_adjust(hspace = .5, wspace=.001)

axs = axs.ravel()

for i in range(10):

    axs[i].contourf(np.random.rand(10,10),5,cmap=plt.cm.Oranges)
    axs[i].set_title(str(250+i))

rawDescription: Python: subplot within a loop: first panel appears in wrong position


code:
s = r"{ 'name': 'Skrillex', 'Genre':'Dubstep', 'Bass': 'Heavy', 'thoughts': 'this\'s ahmazing'}"
s.replace("\\'", 'REPLACEMEOHYEAH').replace("'", '"').replace('REPLACEMEOHYEAH', "\\'")

rawDescription: Replacing an escaped single quote with an escaped double quote in Python


code:
import multiprocessing as mp
from sklearn.grid_search import ParameterGrid
from sklearn.metrics import precision_score,f1_score

def get_scores(params):
    model = SVC(kernel='rbf',cache_size=1000,class_weight='balanced',**params)
    model.fit(X,y)
    model.predict(Xt)
    precision = precision_score(yt_class, model.predict(Xt), average='weighted')
    f1 = f1_score(yt_class, model.predict(Xt), average='weighted')
    return precision, f1

grid = {'C':[1,10.0,50,100.0],'gamma':[0.00001,0.0001,0.001,0.01,0.1]}
param_grid = ParameterGrid(grid)
pool = mp.Pool(processes=8)

scores = pool.map_async(get_scores, param_grid).get()
# scores is a list of tuples [(precision_1, f1_1), (precision_2, f1_2)...]
# you can "unzip" it like this

precision, f1 = zip(*scores)

rawDescription: Python, running a loop on several CPUs

description: Python, running a loop on several CPUs


rawDescription: Python: create mask from two arrays

description: Python: create mask from two arrays


code: result=[dict(zip(self.keys, line)) for line in self.data if value in line]

rawDescription: Looking for a Python dict comprehension from a loop I wrote


code:
circle = list(range(1,101))
#100 people numbered 1-100

while len(circle) > 1: #run this code as long as the circle has more than one person
    print(str(circle[0]) + " kills " + str(circle[1]))
    survivor = circle[0] #save the survivor to a variable
    circle.pop(1) #remove the person killed from the list
    circle.pop(0) #remove the person who survives from the list
    #now the new start of the list is whoever is next in line

    circle.append(survivor) #re-add the survivor to the list at the end

print(str(circle[0]) + "is the winner") #print out the survivor

rawDescription: A puzzle that needs to be programmed with Python?


code: SESSION_COOKIE_AGE = 18 * 24 * 60 * 60

rawDescription: Increase time of Django Sessions

description: Increase time of Django Sessions


code:
#!/usr/bin/env python

from subprocess import check_output

from circuits.web import Controller, Server

class Root(Controller):

    def index(self):
        return check_output("python hello.py", shell=True)

app = Server("0.0.0.0:5000")
Root().register(app)

app.run()

rawDescription: Make a web service using Python to call Python script


rawDescription: pyPDF - Retrieve page numbers from document

description: PyPDF - Retrieve page numbers from document


code:
from sqlalchemy import Table, Integer, Column, ForeignKey
from sqlalchemy.orm import relationship, backref
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

association_table = Table('association', Base.metadata,
    Column('user_id', Integer, ForeignKey('user.id')),
    Column('thing_id', Integer, ForeignKey('thing.id'))
)

class User(Base):
    __tablename__ = 'user'
    # Whatever you need in this model
    inventory = relationship('Thing', secondary = association_table, backref = 'users')

class Thing(Base):
    __tablename__ = 'thing'
    # Whatever you need in this model

rawDescription: Many-to-many relationships without a linking table/model


code:
d = {}
wb = xlrd.open_workbook('foo.xls')
sh = wb.sheet_by_index(2)
for i in range(138):
    cell_value_class = sh.cell(i,2).value
    cell_value_id = sh.cell(i,0).value
    d[cell_value_class] = cell_value_id

rawDescription: Python Creating Dictionary from excel data


rawDescription: Converting a string into a list and removing prefixes and suffixes


code:
def range_squared(n):

    """Takes a non-negative integer value n and returns the list of the numbers, up to n-1 squared.
    """
    a = []
    for i in range(n):
        i = i ** 2
        a.append(i)

    return a

print (range_squared(3))

rawDescription: Why is my "return" outside the function?

description: Why is my "return" outside the function


code:
import csv
import sqlite3

from glob import glob; from os.path import expanduser
conn = sqlite3.connect( # open "places.sqlite" from one of the Firefox profiles
    glob(expanduser('~/.mozilla/firefox/*/places.sqlite'))[0]
)
cursor = conn.cursor()
cursor.execute("select * from moz_places;")

with open("out.csv", "wb") as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerow([i[0] for i in cursor.description]) # write headers
    csv_writer.writerows(cursor)

rawDescription: How do I create a CSV file from database in Python?

description: Create a CSV file from database in Python


code:
class Root(object):

    @cherrypy.expose
    def index(self):
        return 'Hello world'

class RestAPI(object):

    exposed = True

    def POST(self):
        return 'post'

    def GET(self):
        return 'get'

cherrypy.config.update({
    'global': {
        'environment': 'test_suite',
        'server.socket_host': '127.0.0.1',
        'server.socket_port': 8080,
    }
})

cherrypy.tree.mount(Root())

cherrypy.tree.mount(RestAPI(), '/api',
    {'/':
        {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}
    }
)

cherrypy.engine.start()
cherrypy.engine.block()

rawDescription: Combining REST dispatcher with the default one in a single CherryPy app


code:
try:
    import simplejson as json
except ImportError:
    import json

rawDescription: Importing a module based on installed python version?


rawDescription: Splitting list item into two items in place with regex


code:
import plotly.plotly as py
from plotly.graph_objs import Box, Figure

fig = Figure()
boxpoints_default = Box(y=[1, 2, 3, 2, 1, 10], name='default')
boxpoints_false = Box(y=[1, 2, 3, 2, 1, 10], boxpoints=False, name='no outliers')
boxpoints_all = Box(y=[1, 2, 3, 2, 1, 10], boxpoints='all', name='jitter boxpoints')

fig['data'].extend([boxpoints_default, boxpoints_false, boxpoints_all])
fig['layout'].update(title='Comparing boxplot "boxpoints" settings')

py.iplot(fig, filename='Stack Overflow 31497537')

rawDescription: How to create a boxplot not showing the outliers using Python and Plotly?

description: Create a boxplot not showing the outliers using Python and Plotly


code:
import regex

c = r'\red{here is \underline{underlined} text}'

c = regex.sub(r'\\red({((?>[^{}]+|(?1))*)})', r'\2', c)

rawDescription: python regular expression remove matching brackets file

description: Python regular expression remove matching brackets file


code:
class DataTablesForm(forms.Form):

    draw = forms.IntegerField()
    length = forms.IntegerField()
    start = forms.IntegerField()

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

        self.fields['_'] = forms.CharField()

        for i in range(0, 3):
            self.fields['columns[%d][data]' % i] = forms.CharField(required=False)
            self.fields['columns[%d][name]' % i] = forms.CharField(required=False)
            self.fields['columns[%d][orderable]' % i] = forms.BooleanField(required=False)
            self.fields['columns[%d][search][regex]' % i] = forms.BooleanField(required=False)
            self.fields['columns[%d][search][value]' % i] = forms.CharField(required=False)
            self.fields['columns[%d][searchable]' % i] = forms.BooleanField(required=False)

            self.fields['order[%d][column]' % i] = forms.IntegerField(required=False, min_value=0, max_value=2)
            self.fields['order[%d][dir]' % i] = forms.ChoiceField(required=False, choices=(('asc', 'asc'), ('desc', 'desc')))

        self.fields['search[regex]'] = forms.BooleanField(required=False)
        self.fields['search[value]'] = forms.BooleanField(required=False)

rawDescription: Django request.GET and jQuery Datatables


code:
import webapp2
from google.appengine.api import users

class BaseHandler(webapp2.RequestHandler):
  def get_user(self):
    #Maybe also adding some logic here or returning your own User model
    return users.get_current_user()

class MainPage(BaseHandler):
  def get(self):
    if self.get_user():
      self.response.headers['Content-Type'] = 'text/plain'
      self.response.out.write('Hello, ' + self.get_user().nickname())
    else:
      self.redirect(users.create_login_url(self.request.uri))

rawDescription: When using Python2.7 on App Engine, is it ok to reference per-request globals?


code:
from collections import Counter
Counter('common')
Counter('connor')
common = Counter('common') & Counter('connor') # intersection
common
sum(common.values())

rawDescription: common letters in two strings

description: Common letters in two strings


code:
import sqlite3

# open the SQL file and read the contents
f_contents = open("example.sql").read()

# Use regexes to split the contents into individual SQL statements.
# This is unrelated to the issues I'm experiencing, show I opted not
# to show the details. The function below simply returns a list of
# SQL statements
stmnt_list = split_statements(f_contents)

temp_db = sqlite3.connect(":memory:")

good_stmnts = []    # a list for storing all statements that executed correctly
for stmnt in stmnt_list:
    # try executing the statement
    try:
        temp_db.execute(stmnt)
    except Exception as e:
        print("Bad statement. Ignoring.\n'%s'" % stmnt)
        continue
    good_stmnts.append(stmnt)

temp_db.close()

rawDescription: Validate SQL Query Syntax with Python and SQLite


code: coordinates = [[data[index] for index in cluster] for cluster in clusters]

rawDescription: Python: Matching Index values to List values


rawDescription: Passing optional arguments to a function


code:
import networkx as nx
G = nx.Graph()
G.add_edge(1,2,weight=7)
G.add_edge(1,4,weight=3)
G.add_edge(2,3,weight=1)
G.add_edge(3,4,weight=100)
nx.adjacency_matrix(G).todense()
nx.shortest_path_length(G)
nx.shortest_path_length(G,weight='weight')
nx.utils.dict_to_numpy_array(nx.shortest_path_length(G,weight='weight'))

rawDescription: Python: Issues with Networkx Dijkstra algo


code:
seen = set()
L = []
if 'some_item' not in seen:
    L.append('some_item')
    seen.add('some_item')

rawDescription: Deleting duplicate items in a list or dictionary


code:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

X, Y, Z = np.mgrid[-1:1:10j, -1:1:10j, -1:1:10j]

T = np.exp(-X**2 - Y**2 - Z**2)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
scat = ax.scatter(X, Y, Z, c=T, cmap='jet')
fig.colorbar(scat, shrink=0.5, aspect=5)

rawDescription: Plot 3d cartesian grid with python

description: Plot 3d cartesian grid with python


code:
from datetime import datetime
Entry.objects.filter(pub_date=datetime(2008, 10, 18))

rawDescription: Filtering a complete date in django?

description: Filtering a complete date in django


code:
s = "banana 4 apple 2 orange 4"
lst = s.split()
dict(zip(lst[::2], lst[1::2]))

rawDescription: How can you convert a list of poorly formed data pairs into a dict?

description: Convert a list of poorly formed data pairs into a dict


code:
from tkinter import * # for python 2.7 use Tkinter

root = Tk()

frame1 = Frame(root, bg='red', height=20)
frame1.grid(row=0,column=0, sticky=W+E)

frame3 = Frame(root, bg='blue', height=20)
frame3.grid(row=0,column=1, sticky=W+E)

frame2 = Frame(root, bg='green',  height=20)
frame2.grid(row=1,column=0, sticky=W+E)

frame4 = Frame(root)
frame4.grid(row=2,columnspan=2, sticky=E+W)

l5 = Label(frame4, text='Output:', bg='orange').grid(row=0, column=0, sticky=E+W)
output = Listbox(frame4, height=5, width=50)
output.grid(row=1,column=0)
#output.pack(side=LEFT,  fill=BOTH, expand=1)

root.mainloop()

rawDescription: Expand a listbox over 2 columns Tkinter

description: Expand a listbox over 2 columns Tkinter


code:
from PIL import Image
im = Image.open('/home/students/sky1024px.jpg')
im.show()

rawDescription: Displaying an Image in the iPython qtconsole


code:
from __future__ import print_function

import warnings as _warnings
import os as _os

from tempfile import mkdtemp

class TemporaryDirectory(object):
    """Create and return a temporary directory.  This has the same
    behavior as mkdtemp but can be used as a context manager.  For
    example:

        with TemporaryDirectory() as tmpdir:
            ...

    Upon exiting the context, the directory and everything contained
    in it are removed.
    """

    def __init__(self, suffix="", prefix="tmp", dir=None):
        self._closed = False
        self.name = None # Handle mkdtemp raising an exception
        self.name = mkdtemp(suffix, prefix, dir)

    def __repr__(self):
        return "<{} {!r}>".format(self.__class__.__name__, self.name)

    def __enter__(self):
        return self.name

    def cleanup(self, _warn=False):
        if self.name and not self._closed:
            try:
                self._rmtree(self.name)
            except (TypeError, AttributeError) as ex:
                # Issue #10188: Emit a warning on stderr
                # if the directory could not be cleaned
                # up due to missing globals
                if "None" not in str(ex):
                    raise
                print("ERROR: {!r} while cleaning up {!r}".format(ex, self,),
                      file=_sys.stderr)
                return
            self._closed = True
            if _warn:
                self._warn("Implicitly cleaning up {!r}".format(self),
                           ResourceWarning)

    def __exit__(self, exc, value, tb):
        self.cleanup()

    def __del__(self):
        # Issue a ResourceWarning if implicit cleanup needed
        self.cleanup(_warn=True)

    # XXX (ncoghlan): The following code attempts to make
    # this class tolerant of the module nulling out process
    # that happens during CPython interpreter shutdown
    # Alas, it doesn't actually manage it. See issue #10188
    _listdir = staticmethod(_os.listdir)
    _path_join = staticmethod(_os.path.join)
    _isdir = staticmethod(_os.path.isdir)
    _islink = staticmethod(_os.path.islink)
    _remove = staticmethod(_os.remove)
    _rmdir = staticmethod(_os.rmdir)
    _warn = _warnings.warn

    def _rmtree(self, path):
        # Essentially a stripped down version of shutil.rmtree.  We can't
        # use globals because they may be None'ed out at shutdown.
        for name in self._listdir(path):
            fullname = self._path_join(path, name)
            try:
                isdir = self._isdir(fullname) and not self._islink(fullname)
            except OSError:
                isdir = False
            if isdir:
                self._rmtree(fullname)
            else:
                try:
                    self._remove(fullname)
                except OSError:
                    pass
        try:
            self._rmdir(path)
        except OSError:
            pass

import os
with TemporaryDirectory() as tmp_dir:
    print("Temporary directory path: %s" % tmp_dir)
    print(os.path.isdir(tmp_dir))

# here the temporary diretory does not exist any more.
print(os.path.isdir(tmp_dir))

rawDescription: tempfile.TemporaryDirectory context manager in Python 2.7

description: Tempfile.TemporaryDirectory context manager in Python 2.7


code:
user_button = Tkinter.Button(self.root,
                             text=name,
                             command=lambda name=name: self.a(name))

rawDescription: Tkinter assign button command in loop with lambda


code:
class Dicty(dict):
    def __getattr__(self, key):
        return self[key]

    def __setattr__(self, key, value):
        self[key] = value

rawDescription: Are there any 'gotchas' with this Python pattern?


code:
l.sort()

last_str = l[0]
filtered = [last_str]
app      = filtered.append

for str in l:
    if not str.startswith(last_str):
        last_str = str
        app(str)

# Commented because of the massive amount of data to print.
# print filtered

rawDescription: Obtain a list containing string elements excluding elements prefixed with any other element from initial list


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

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

# major ticks every 20, minor ticks every 5
major_ticks = np.arange(0, 101, 20)
minor_ticks = np.arange(0, 101, 5)

ax.set_xticks(major_ticks)
ax.set_xticks(minor_ticks, minor=True)
ax.set_yticks(major_ticks)
ax.set_yticks(minor_ticks, minor=True)

# and a corresponding grid

ax.grid(which='both')

# or if you want differnet settings for the grids:
ax.grid(which='minor', alpha=0.2)
ax.grid(which='major', alpha=0.5)

plt.show()

rawDescription: matplotlib: Change grid interval and specify tick labels

description: Matplotlib: Change grid interval and specify tick labels


code:
import re
import ast

m = re.match(r"abc \| pqr \| (\[[-0-9,]*\])", inString)
if m is not None:
    theList = ast.literal_eval(m.group(1))

rawDescription: Need a regex :(

description: Need a regex :(


code:
import asyncio
from concurrent.futures import ProcessPoolExecutor

print('running async test')

def say_boo():
    i = 0
    while True:
        print('...boo {0}'.format(i))
        i += 1

def say_baa():
    i = 0
    while True:
        print('...baa {0}'.format(i))
        i += 1

if __name__ == "__main__":
    executor = ProcessPoolExecutor(2)
    loop = asyncio.get_event_loop()
    boo = asyncio.ensure_future(loop.run_in_executor(executor, say_boo))
    baa = asyncio.ensure_future(loop.run_in_executor(executor, say_baa))

    loop.run_forever()

rawDescription: How to properly create and run concurrent tasks using python's asyncio module?

description: Properly create and run concurrent tasks using python's asyncio module


code:
from scipy.stats import norm
norm.logsf(1000)

rawDescription: Compute natural log of survival function of a Gaussian in Python/numpy/scipy


code:
while requests.get("http://169.254.169.254/latest/meta-data/spot/termination-time").status_code != 200:
    sleep(5)

rawDescription: ec2 spot instance graceful termination

description: Ec2 spot instance graceful termination


code: pd.concat([df.describe(), df2.describe()], keys = ['INVERNESS', 'RIVERCHASE'], axis = 1)

rawDescription: Formatting output on Ipython Notebook with Pandas


code:
ref_user = User.objects.get(
username=form.cleaned_data['referrer']
)

user = User.objects.create_user(
username=form.cleaned_data['username']
)

# Count the referrer's direct referrals
ref_recruits = DirectReferral.objects.filter(referrer=ref_user).count()

# Get newly created direct referral
get_ref = DirectReferral.objects.get(name=user)

#Check if ref_recruits is not divisible by 2 or paired
if ref_recruits % 2 == 0:
    get_ref.is_paired = True
    DirectReferral.objects.filter(referrer=ref_user, is_paired=False).update(is_paired=True)
else:
    get_ref.is_paired = False
get_ref.save()

rawDescription: Django: Update or Change previous saved model data


code:
for i in range(1,6):
    locals()["dog" + str(i)].feed()

rawDescription: Python3 - Iterate over object methods with similar names


code:
dom = xml.dom.minidom.parseString(document)

for node in dom.childNodes:
    if node.attributes:
        for key in node.attributes.keys():
            node.removeAttribute(key)

rawDescription: Remove all the attributes of every node in an XML document


code:
import mylogger

log_file_handle = mylogger.get_log()

rawDescription: Python, Keeping a dynamically named file open in a large modular program


code:
df1 = pd.DataFrame(df.From.str.rsplit(' ',1).tolist(), columns=['Name','Email'])
df1

rawDescription: Splitting a pandas DataFrame of email 'From' field into sender's name, email address


code:
t = Tk() # new window
t.update()
t.attributes("-alpha", 00)
t.state('zoomed') # maximize the window
height= t.winfo_height() # ...
width= t.winfo_width()

rawDescription: Python tkinter screen width and height secondary display


code:
import csv
list(csv.reader(lines, delimiter=';'))

rawDescription: Operations on a string as a list element in python


code: from projectname.settings import LOGIN_URL

rawDescription: Django: Dynamic LOGIN_URL variable

description: Django: Dynamic LOGIN_URL variable


code:
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()

# change this to read in your data
finder = BigramCollocationFinder.from_words(
   nltk.corpus.genesis.words('english-web.txt'))

# only bigrams that appear 3+ times
finder.apply_freq_filter(3)

# return the 5 n-grams with the highest PMI
finder.nbest(bigram_measures.pmi, 5)

rawDescription: tag generation from a text content

description: Tag generation from a text content


code:
class Mpwidget(FigureCanvas):
    def __init__(self, parent=None):
        self.figure = Figure(facecolor=(0, 0, 0))
        super(Mpwidget, self).__init__(self.figure) # this object _is_ your canvas
        self.setParent(parent)

        # plot random 3D data
        self.axes = self.figure.add_subplot(111, projection='3d')
        self.data = np.random.random((3, 100))
        self.axes.plot(self.data[0, :], self.data[1, :], self.data[2, :])

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    mw.show()

    # adjust the frame size so that it fits right after the window is shown
    s = mw.ui.frame_plot.size()
    mw.ui.plotWidget.setGeometry(1, 1, s.width() - 2, s.height() - 2)

    sys.exit(app.exec_())

rawDescription: Embed an interactive 3D plot in PySide

description: Embed an interactive 3D plot in PySide


code:
o = subprocess.check_output(cmd, shell=True)
g = re.search(r'count=(\d+)', o.decode("utf-8"), re.M|re.I)

rawDescription: How to count the total number of files to be synced using rsync?

description: Count the total number of files to be synced using rsync


code:
# get threshholds for first 9 bins
_, bins = pd.cut(df[df.play_count < 200].play_count, bins=9,retbins=True)

# append threshhold representing class with play_counts > 200
new_bins = pd.np.append(bins,float(max(df.play_count)))

# our categorized data
out = pd.cut(df.play_count,bins=new_bins)

# a histogram of the data with the updated bins
df.play_count.hist(bins=new_bins)

rawDescription: python pandas dataframe create bins only for data in threshold

description: Python pandas dataframe create bins only for data in threshold


code:
from os.path import join
from os import listdir, rmdir
from shutil import move

root = 'dstfolder/slave1'
for filename in listdir(join(root, 'slave')):
    move(join(root, 'slave', filename), join(root, filename))
rmdir(root)

rawDescription: Move child folder contents to parent folder in python


code: re.findall('(?<=\()(.*?)(?=\))', text, re.MULTILINE | re.DOTALL)

rawDescription: RegExp: Get text within parentheses

description: RegExp: Get text within parentheses


code:
import pandas as pd
import numpy as np

# ... load your data ...

# Create bins by rounding 'recall' to specified number of decimal places
df_unique["recall_bins"] = np.round(df_unique["recall"], 2)

# Group your data according to the bins you just created
groups = df_unique.groupby("recall_bins")

# Calculate the means of each group
precision_means = groups.aggregate({"precision": np.mean})

rawDescription: Pandas dataframe: Plotting meaningful plot


code:
s = 'generic/html/path/generic/generic/generic'
s.split('html/')[1].split('/')[0]

rawDescription: Find a string using BeautifulSoup in Python


code:
import numpy as np
x = np.random.randn(100)
x.max()
x.argmax()
x[27]
x[27] == x.max()

rawDescription: Get coordinates from a lambda function point


code:
for ext, sz in filedict.items():
  stats = (
    ext_str+ext, cnt_str+str(len(sz)), max_str+str(max(sz)),
    min_str+str(min(sz)), avg_str+str(calc_avg(sz))
    )
  stats_lst.append(stats)
for item in stats_lst:
  stats_str = '\n'.join(map(str, item))
  stats_print.append('\n')
  stats_print.append(stats_str)

msg = easygui.codebox(msg="",title=smmry_title, text=stats_print)

rawDescription: formatting content in easygui mesage box and adding scrollbar

description: Formatting content in easygui mesage box and adding scrollbar


code: [list(k[:i]) for i in range(1, len(k)+1)]

rawDescription: python function that return all initial segments of a list

description: Python function that return all initial segments of a list


code:
good_dirs = []
bad_dirs = []

for root, files, dirs in os.walk("/tmp/folder/B03"):
    # this will walk recursively depth first into B03
    # root will be the pwd, so we can test for that
    if root.count(".") == 1: ###i think aregex here might help
        good_dirs.append(root)
    else:
        bad_dirs.append(root)

rawDescription: Selecting folders using strings in Python


code:
sql_statement = """SELECT * FROM points WHERE strftime('%s', checkintime) == %s AND strftime('%s', checkintime) == %s""" % ('%H', time, '%w', day)

_points.execute(sql_statement)

rawDescription: SQLite Python AND statement not working

description: SQLite Python AND statement not working


code:
def _format_action_invocation(self, action):
    if not action.option_strings:
        metavar, = self._metavar_formatter(action, action.dest)(1)
        return metavar

    else:
        parts = []

        # if the Optional doesn't take a value, format is:
        #    -s, --long
        if action.nargs == 0:
            parts.extend(action.option_strings)

        # if the Optional takes a value, format is:
        #    -s ARGS, --long ARGS
        else:
            default = action.dest.upper()
            args_string = self._format_args(action, default)
            for option_string in action.option_strings:
                parts.append('%s %s' % (option_string, args_string))

        return ', '.join(parts)

rawDescription: Option value syntax for help message in python argparse


code:
def partition(array, lo, hi):
    pivot_index = hi
    pivot_value = array[pivot_index]
    store_index = lo

    for i in range(lo, hi):
        if array[i] <= pivot_value:
            array[i], array[store_index] = array[store_index], array[i]
            store_index += 1
    array[pivot_index], array[store_index] = array[store_index], array[pivot_index]
    return store_index

def quicksort(array, lo, hi):
    if lo < hi:
        p = partition(array, lo, hi)
        print(' '.join([str(i) for i in array]))
        quicksort(array, lo, p-1)
        quicksort(array, p+1, hi)

if __name__ == '__main__':
    size = int(input())
    ar = [int(i) for i in input().split()]
    quicksort(ar, 0, size-1)

rawDescription: Quicksort intermediate list printing Python


code:
df = DataFrame([[1,'hi'],[3,4]])
df.dtypes
df.convert_objects(convert_numeric=True)
df.convert_objects(convert_numeric=True).unstack().sum()

rawDescription: How to sum across entire DataFrame with mixed types?

description: Sum across entire DataFrame with mixed types


code:
class SiteSettings(models.Model):
      value = models.IntegerField() # Assuming you are only using integer value
      type  = models.CharField(unique=True) # could also make this the primary key

PAGE_SIZE = SiteSettings.Objects.filter(type="page_size").get()
# you should be able to do this as well since type is unique
PAGE_SIZE = SiteSettings.Objects.get(type="page_size")
PAGE_SIZE.value

rawDescription: Storing app constants in database

description: Storing app constants in database