code:
#!/usr/bin/env python3

"""
Some functionality useful when working with IPython as a shell replacement.
"""

import subprocess
import tempfile
import os

def ShellEval(command_str):
    """
    Evaluate the supplied command string in the system shell.
    Operates like the shell eval command:
       -  Environment variable changes are pulled into the Python environment
       -  Changes in working directory remain in effect
    """
    temp_stdout = tempfile.SpooledTemporaryFile()
    temp_stderr = tempfile.SpooledTemporaryFile()
    # in broader use this string insertion into the shell command should be given more security consideration
    subprocess.call("""trap 'printf "\\0`pwd`\\0" 1>&2; env -0 1>&2' exit; %s"""%(command_str,), stdout=temp_stdout, stderr=temp_stderr, shell=True)
    temp_stdout.seek(0)
    temp_stderr.seek(0)
    all_err_output = temp_stderr.read()
    allByteStrings = all_err_output.split(b'\x00')
    command_error_output = allByteStrings[0]
    new_working_dir_str  = allByteStrings[1].decode('utf-8') # some risk in assuming index 1. What if commands sent a null char to the output?

    variables_to_ignore = ['SHLVL','COLUMNS', 'LINES','OPENSSL_NO_DEFAULT_ZLIB', '_']

    newdict = dict([ tuple(bs.decode('utf-8').split('=',1)) for bs in allByteStrings[2:-1]])
    for (varname,varvalue) in newdict.items():
        if varname not in variables_to_ignore:
            if varname not in os.environ:
                #print("New Variable: %s=%s"%(varname,varvalue))
                os.environ[varname] = varvalue
            elif os.environ[varname] != varvalue:
                #print("Updated Variable: %s=%s"%(varname,varvalue))
                os.environ[varname] = varvalue
    deletedVars = []
    for oldvarname in os.environ.keys():
        if oldvarname not in newdict.keys():
            deletedVars.append(oldvarname)
    for oldvarname in deletedVars:
        #print("Deleted environment Variable: %s"%(oldvarname,))
        del os.environ[oldvarname]

    if os.getcwd() != os.path.normpath(new_working_dir_str):
        #print("Working directory changed to %s"%(os.path.normpath(new_working_dir_str),))
        os.chdir(new_working_dir_str)
    # Display output of user's command_str.  Standard output and error streams are not interleaved.
    print(temp_stdout.read().decode('utf-8'))
    print(command_error_output.decode('utf-8'))

rawDescription: From Python execute shell command and incorporate environment changes (without subprocess)?


code:
from datetime import datetime
import pandas as pd
from dateutil.parser import parse

def parse_date_or_ts(data):
    try:
        return parse(data)
    except ValueError:
        # let's assume data is a utc timestamp now
        return datetime.utcfromtimestamp(int(data))

df = pd.read_csv('data.csv',
                 parse_dates={'date': [0]},  # index 0 is our date/ts mixed column
                 date_parser=parse_date_or_ts)

rawDescription: Can I use pandas read_csv converter conditionally?

description: Use pandas read_csv converter conditionally


rawDescription: Python Pandas Dataframe select row by max value in group


code:
def get_pro_lists(pk):
    general = General.objects.get(pk=pk)
    product_list = Product.objects.all()
    shop_list = general.shop_set.all()
    return product_list, shop_list

def passProList(request):
    product_list, shop_list = get_pro_lists(1)
    return {
        'product_list': product_list,
        'shop_list': shop_list,
    }

rawDescription: DJANGO: Variables passed to templates by context manager not reflecting admin changes


code:
def to_pair(item):
    i, word = item
    return (word, len(pre[i]))

final_dict = dict(map(to_pair, enumerate(words)))

rawDescription: Can I do this with a map function?

description: Do this with a map function


code:
import subprocess
f=open('file.txt','w')
subprocess.call(['sqlite3', 'dbname.db', '-csv', 'select * from table'], stdout=f)

rawDescription: Periodically outputting SQL table to a file


code:
with open('filename.txt', 'r') as f:
    lines = [[letter for letter in line] for line in f.read().split('\n')]

rawDescription: How can I turn symbols and whitespace into a 2D list? Best way?

description: Turn symbols and whitespace into a 2D list? Best way


code:
from itertools import combinations
import networkx as nx
data = [10,20,30,40]
edges = ( (s[0],t[0],s[1]+t[1]) for s,t in combinations(enumerate(data),2))
G = nx.Graph()
G.add_weighted_edges_from(edges)
G.edges(data=True)

rawDescription: Coordinates to graph

description: Coordinates to graph


code: import_transform: lambda x: [x] if x else None

rawDescription: AppEngine Bulk Upload List Property

description: AppEngine Bulk Upload List Property


code:
def get_text(node):
    text = [node.text] + [child.tail for child in node]
    return ''.join(x for x in text if x is not None)

rawDescription: How to get text value of XML node when it has child nodes

description: Get text value of XML node when it has child nodes


code:
import abc
import numbers

class C(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def is_number(self):
        return self.x > self.y and not self.y % 3

class NumberMeta(abc.ABCMeta):
    def __instancecheck__(self, obj):
        if type(obj) is C:
            return obj.is_number()
        return abc.ABCMeta.__instancecheck__(obj)

numbers.Number.__class__ = NumberMeta

assert isinstance(C(4, 3), Number)
assert not isinstance(C(1, 3), Number)
assert not isinstance(C(4, 2), Number)

rawDescription: Can a user-defined class be made to conditionally test as an instance of another class?


rawDescription: How to count number of distinct entries in 2d list?

description: Count number of distinct entries in 2d list


code:
class FilterForm(forms.Form):
    cuisine = forms.ModelChoiceField(queryset=Cuisine.objects.all(),
                                     empty_label="All")

rawDescription: Adding 'all' value to a foreign key form field based on model?


code:
def wraptopi(x):
        pi = np.pi
        x = x - np.floor(x/(2*pi)) *2 *pi
        return x[x >= pi] - 2*pi

y[0] = wraptopi(y[0])
y[1] = wraptopi(y[1])

rawDescription: Wrap all members of an array to pi in python


rawDescription: Iterate the start of a list without any import in Python


code:
import glob, os
import zipfile
import defaultdict

# We organize the file names in a dict to
# make writing out the zip files easier

d = defaultdict(list)

for fname in glob.glob(r'C:\FILES\*'):
   d[fname.split('_')[0]].append(fname)

# Now we go through each file by letter
for letter in d:
    with zipfile.ZipFile(letter+'.zip','w') as f:
        for filename in d[letter]:
            f.write(filename, os.path.basename(filename), zipfile.ZIP_DEFLATED)

rawDescription: How to zip up files of same prefix in python?

description: Zip up files of same prefix in python


code:
for word in sentencelist:
        print(noduplicateslist.index(word))

rawDescription: Comparing one list to see in which position it appears in another


code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.latax.state.la.us/Menu_ParishTaxRolls/TaxRolls.aspx")
elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$ddParish")
elem.send_keys("TERREBONNE PARISH")
elem.send_keys(Keys.RETURN)

elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$ddYear")
elem.send_keys("2013")
elem.send_keys(Keys.RETURN)

elem = driver.find_element_by_id("ctl00_ContentPlaceHolderMain_rbSearchField_1")
elem.click()

APN = 'APN # here'
elem = driver.find_element_by_name("ctl00$ContentPlaceHolderMain$txtSearch")
elem.send_keys(APN)
elem.send_keys(Keys.RETURN)

# Access the PDF
elem = driver.find_element_by_link_text('Generate Report')
elem.click()
elements = driver.find_elements_by_tag_name('a')
elements[1].click()

rawDescription: Navigating a website in python, scraping, and posting


code:
# Read the names
with open('names.txt') as f_names:
    names = dict(line.strip().split(':') for line in f_names)

# Read the numbers
with open('numbers.txt') as f_numbers:
    numbers = list(line.strip() for line in f_numbers)

# Replace numbers with names
with open('numbers.txt', 'w') as f_output:
    for n in numbers:
        f_output.write(names[n] + '\n')

rawDescription: Python multiple pairs replace in txt file


code:
import numpy as np
def bas_sub(baseline_y, spectrum_y):
    assert len(baseline_y)==len(spectrum_y), "Baseline and spectrum shoud have the same length."
    spectrum_new =  np.copy(spectrum_y)-baseline_y
    return spectrum_new

rawDescription: Numpy: Try/except inside the function fails to print the message


code:
for i, val in enumerate(array):
    array[i] += 1

rawDescription: Python equivalent of php's foreach($array as $key => &$value)


code:
class Base(object):
    @classmethod
    def make(cls, *args, **kwargs):
        print("Base.make(%s, %s) start" % (args, kwargs))
        print("Base.make end")

class Foo(Base):
    @classmethod
    def make(cls, *args, **kwargs):
        print("Foo.make(%s, %s) start" % (args, kwargs))
        super(Foo, cls).make(*args, **kwargs)
        print("Foo.make end")

class Bar(Base):
    @classmethod
    def make(cls, *args, **kwargs):
        print("Bar.make(%s, %s) start" % (args, kwargs))
        super(Bar, cls).make(*args, **kwargs)
        print("Bar.make end")

class FooBar(Foo,Bar):
    @classmethod
    def make(cls, *args, **kwargs):
        print("FooBar.make(%s, %s) start" % (args, kwargs))
        super(FooBar, cls).make(*args, **kwargs)
        print("FooBar.make end")

fb = FooBar.make(1, 2, c=3)

rawDescription: why and how to use Python's super(type1, type2)?

description: Why and how to use Python's super(type1, type2)


code:
class LayerList(list):

    def __init__(self, id):
        self.id=id
        return super(LayerList, self).__init__()

    def append(self, item):
        #test if the item is a Layer
        if not isinstance(item,Layer):
            #do whathever treatment you wanna do
            raise Exception("Not a Layer object")

        #test if the item is already in the list
        if item in self:
            #do whathever treatment you wanna do
            raise Exception("Item already in list")

        #check if the layer has the attr "layer_id"
        if not hasattr(item,"layer_id"):
            setattr(item,"layer_id",None)
        item.layer_id=self.id

        #call the parents 'append'
        super(LayerList, self).append(item)

    def pop(self, i=None):
        item = super(LayerList, self).pop(i)
        item.layer_id=None
        return item

rawDescription: Homogeneous lists in Python

description: Homogeneous lists in Python


code:
#!/usr/bin/env python3
from github import Github

ACCESS_USERNAME = 'username'
ACCESS_PWD = "password"
client = Github(ACCESS_USERNAME, ACCESS_PWD, per_page=100)
user = client.get_user('ELLIOTTCABLE')
repo_list = [repo.name for repo in user.get_repos() if not repo.fork]
print(repo_list)

for j in repo_list:
    repo = user.get_repo(j)
    lang = repo.language
    print(j,':',lang)

rawDescription: Why I got Exception "403 Repository access blocked'' using pygithub?


code:
import pylab as py
import numpy as np
from scipy import optimize

# Generate a
y = np.random.standard_normal(10000)
data = py.hist(y, bins = 100)

# Equation for Gaussian
def f(x, a, b, c):
    return a * py.exp(-(x - b)**2.0 / (2 * c**2))

# Generate data from bins as a set of points
x = [0.5 * (data[1][i] + data[1][i+1]) for i in xrange(len(data[1])-1)]
y = data[0]

popt, pcov = optimize.curve_fit(f, x, y)

x_fit = py.linspace(x[0], x[-1], 100)
y_fit = f(x_fit, *popt)

plot(x_fit, y_fit, lw=4, color="r")

rawDescription: Un-normalized Gaussian curve on histogram


code:
def post(self, request, *args, **kwargs):
    self.object = self.get_object()
    form_class = self.get_form_class()
    form = self.get_form(form_class)
    formset = OfficeContactInline(request.POST, instance=self.object)
    if form.is_valid() and formset.is_valid():
        self.object = form.save()
        contacts = formset.save(commit=False)
        # New with Django 1.7
        for del_contact in formset.deleted_objects:
            del_contact.delete()

        for contact in contacts:
            contact.office = self.object
            contact.save()
        formset.save_m2m()
        return HttpResponseRedirect(self.get_success_url())
    else:
        return render(request, self.template_name, self.get_context_data(form=form, formset=formset))

rawDescription: Django Many To Many Management with inline formset


code:
def __init__(self,parent):
    ...
    self.b2 = Button(self, text="Reset", command=self.b2Press)
    self.b2.pack()

def b2Press(self):
    self.winfo_toplevel().wm_geometry("")

rawDescription: What controls automated window resizing in Tkinter?


code:
import random

while True:

    first_num = random.randint(1,500)
    second_num = random.randint(1,500)

    print (first_num)
    print (second_num)

    answer = int(input('Entrer la somme des deux nombres: '))

    if answer == first_num + second_num:
        print("It's correct!")
        break
    else:
        print("It's wrong!")
        tryAgain = raw_input('Try again? [(y)/n] ')
        if tryAgain.lower() == 'n':
            break

rawDescription: python how can I generate new random numbers each time a loop iterates?

description: Python how can I generate new random numbers each time a loop iterates


code:
a = numpy.arange(10, dtype='f8')
a /= 100000
a
numpy.set_printoptions(suppress=True)
a

rawDescription: Numpy/Scipy - printing matrix class as floating point


code:
with open('test.csv') as in_file, open('new.csv', 'w') as out_file:
    for line in csv.reader(in_file, delimiter='$'):
        out_file.write(','.join(sorted(line)[2:])+'\n')

rawDescription: Sorting CSV file with delimiter in Python


rawDescription: Replacing NumPy array entries with their frequencies / values from dictionary


code:
from celery.task.control import revoke
revoke(task_id, terminate=True)

rawDescription: Architecting a multithreaded app with a REST interface


code:
num_days = 3
current_date = datetime.datetime.now()
current_date
future_date = current_date.replace(day=current_date.day + num_days)
future_date

rawDescription: how to create a date object in python representing a set number of days

description: Create a date object in python representing a set number of days


code:
from string import ascii_lowercase as al
from itertools import product
lis = ["".join(p) for i in xrange(1,6) for p in product(al, repeat = i)]

rawDescription: Genearte a list of strings compound of letters from other list in Python


code:
for i,row in enumerate(Mtx): #same thing as `for i in range(len(Mtx)):`
    Mtx[i][i]=0
    Mtx[i][i]=1-sum(Mtx[i])

    ##could also use (if it makes more sense to you):
    #row[i]=0
    #Mtx[i][i]=1-sum(row)

rawDescription: Assigning a list element with the sum of the other elements


code:
from subprocess import check_output
import concurrent.futures
import pandas as pd

def get_average(filename_str):
    average = check_output(['proprietary_mean_calculator', filename_str])
    return float(average)

with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    waveform_averages = executor.map(get_average, waveform_dumps)

# to make the result a pd.Series, if desired:
waveform_averages = pd.Series(waveform_averages, index=waveform_dumps.index)

rawDescription: How do I parallelize a `map` function on a pandas Series object?

description: Parallelize a `map` function on a pandas Series object


rawDescription: Controlling socket timeouts in external libraries


code:
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

def toggle(_):
    button.status ^= True

    color = [0, 1, 0] if button.status else [1, 0, 0]
    button.color = color
    button.hovercolor = color

    button.canvas.motion_notify_event(0,0)

button = Button(plt.axes([.1, .1, .8, .8]), 'Press me')
button.status = True
button.on_clicked(toggle)

plt.show()

rawDescription: Manually call draw for matplotlib

description: Manually call draw for matplotlib


code:
import glob
import shutil

dest = 'testfiles/'

files = glob.glob('*/*test.csv')

for file in files:
    shutil.move(file, dest)

rawDescription: Python script to extract all subdirectories according to filename


code:
import ctypes
crypt = ctypes.CDLL('libcrypt.so').crypt
crypt.argtypes = ctypes.c_char_p, ctypes.c_char_p
crypt.restype = ctypes.POINTER(ctypes.c_char * 13)
crypt('tea', 'ea')
crypt('tea', 'ea').contents
crypt('tea', 'ea').contents.value

rawDescription: Function returns pointer to array with result. How to access this data


code:
def allAlarms():
    listDefault.set("Select ID")
    cbox['values'] = alarmIDs
def carAlarms():
    listDefault.set("Select ID")
    cbox['values'] = carAlarmIDs
def houseAlarms():
    listDefault.set("Select ID")
    cbox['values'] = houseAlarmIDs

allAlarms = ttk.Radiobutton(self.master, text="All", variable=self.alarmType,
                            value="alarmIDs", command=allAlarms).place(x=10, y=30)

carAlarms = ttk.Radiobutton(self.master, text="Car Alarms", variable=self.alarmType,
                            value="carAlarmIDs", command=carAlarms).place(x=10, y=50)

houseAlarms = ttk.Radiobutton(self.master, text="House Alarms", variable=self.alarmType,
                              value="houseAlarmIDs", command=houseAlarms).place(x=10, y=70)

rawDescription: Changing values of a ttk.Combobox by using ttk.Radiobuttons - Python


code:
your_samples = numpy.random.choice(your_array[1], size=20, replace=False,
                                   p=your_array[0])

rawDescription: Sample from weighted histogram

description: Sample from weighted histogram


code:
import struct
import binascii

binascii.hexlify(struct.pack('<H', 1050))
# '1a04'

binascii.hexlify(struct.pack('<H', 100))
# '6400'

binascii.hexlify(struct.pack('<H', 5))
# '0500'

rawDescription: network packet lengths in python

description: Network packet lengths in python


code:
def chunker(target, blocksize=DEFAULT_BLOCK_SIZE):
    data = []
    buffer = ''
    buffer_len = 0
    target_send = target.send
    while True:
        try:
            input_data = yield
            data.append(input_data)
            buffer_len += len(input_data)
            if buffer_len >= blocksize:
                buffer = ''.join(data)
                chunks, leftover = divmod(buffer_len, blocksize)
                for i in xrange(0, chunks*blocksize, blocksize):
                    target_send(buffer[i:i+blocksize])
                buffer = buffer[-leftover:] if leftover else ""
                buffer_len = len(buffer)
                data = [buffer] if buffer else []
        except CleanUp:
            buffer = ''.join(data)
            if buffer:
                target_send(buffer)
            target_send("")

rawDescription: How to go about improving this coroutine

description: Go about improving this coroutine


code:
def enterEvent(self, evt):
    # defer labeling the axes until an 'enterEvent'. then set
    # the x label
    r = int(10 * random.random())
    self.axes.set_xlabel(str(r))
    self.canvas.draw()

rawDescription: PyQt4, matplotlib, modifying the axis labels of an existing plot


code:
import matplotlib.pyplot as plt
import matplotlib.image as mimage
import matplotlib.cbook as cbook
datafile = cbook.get_sample_data('logo2.png', asfileobj=False)
im = mimage.imread(datafile)
fig, ax = plt.subplots(figsize=(5.,5.))
myaximage = ax.imshow(im,
                      aspect='auto',
                      extent=(20, 80, 20, 80),
                      alpha=0.5)
ax.plot(range(100))
plt.show()

rawDescription: How to center a plotted image?

description: Center a plotted image


code:
res = [-1]*(max(b)+1)   # create a list of required size with only -1's

for i, v in zip(b, a):
    res[i] = v

rawDescription: Python sort array by another positions array


code:
app = QtGui.QApplication([])

width = 500
height = 200
fontsize1 = width/40
fontsize2 = width/15
scalefactor = width/50

w = QtGui.QWidget()
w.resize(width, height)

# =======   widgets col 1
img = QtGui.QLabel()
pm = QtGui.QPixmap('del_w.xpm')
iw = pm.width()/scalefactor
ih = pm.height()/scalefactor
npm = pm.scaled(iw,ih)          # scaled pixmap
img.setPixmap(npm)

# =======   widgets col 2
top_label = QtGui.QLabel('Percent') #, size='30pt', bold=True)
top_label.setAlignment(QtCore.Qt.AlignCenter)
top_label.setStyleSheet('font-size: {}pt; font-style: bold'.format(fontsize1))
bottom_label = QtGui.QLabel('85')
bottom_label.setAlignment(QtCore.Qt.AlignCenter)
bottom_label.setStyleSheet('font-size: {}pt; font-style: bold; color: 606060'.format(fontsize2))

# =======   widgets col 3
hr_plot = pg.PlotWidget()
hr_plot.showGrid(x=False, y=True)
pen = pg.mkPen(color='#39e4f8', width=4)

hr_plot.plot(data, pen=pen,
         symbol='o',
         symbolPen=pen,
         symbolBrush='#FFFFFF',
         symbolSize=4)

# ======= gridlayout
layout = QtGui.QGridLayout()
w.setLayout(layout)

# ======= add widgets to layout
layout.addWidget(img,0,0)
layout.addWidget(top_label,0,1)
layout.addWidget(bottom_label,1,1,2,1)  # rowspan = 2, colspan = 1
layout.addWidget(hr_plot, 0, 2,5,1) # rowspan = 5, colspan = 1

# ======= set stretchfactors for column
layout.setColumnStretch(0,1)
layout.setColumnStretch(1,2)
layout.setColumnStretch(2,5)

w.showMaximized()
app.exec()

rawDescription: How to set correct sizes for columns in layout - PySide/PyQt

description: Set correct sizes for columns in layout - PySide/PyQt


code:
result = re.sub(
    r"""(?x) # verbose regex
    \b    # Start of word
    (?=   # Look ahead to ensure that this word contains...
     \w*  # (after any number of alphanumeric characters)
     \d   # ...at least one digit.
    )     # End of lookahead
    \w+   # Match the alphanumeric word
    \s*   # Match any following whitespace""",
    "", subject)

rawDescription: Regex replace mixed number+strings

description: Regex replace mixed number+strings


code:
import numpy, scipy.optimize, scipy.interpolate
x = numpy.linspace(0,2*numpy.pi, 20)
x
y1sampled = numpy.sin(x)
y2sampled = numpy.cos(x)
y1int = scipy.interpolate.interp1d(x,y1sampled,kind='cubic')
y2int = scipy.interpolate.interp1d(x,y2sampled,kind='cubic')
scipy.optimize.fsolve(lambda x: y1int(x) - y2int(x), numpy.pi)
scipy.optimize.fsolve(lambda x: numpy.sin(x) - numpy.cos(x), numpy.pi)

rawDescription: Python fsolve does not take array of floats. How to implement it?


rawDescription: How to left adjust and control spacing between values?

description: Left adjust and control spacing between values


code:
setting.INSTALLED_APPS = 10
app.autodiscover_tasks(settings.INSTALLED_APPS, force=True) #is called with installed_apps = 10, so it give you an output.

rawDescription: Lambda use case confusion

description: Lambda use case confusion


code:
import threading
import time

class Thread1(threading.Thread):
    def __init__(self, condition_obj):
        super().__init__()
        self.daemon = True
        self.condition_obj = condition_obj
        self.start()

    def run(self):
        with self.condition_obj:
            while True:
                self.condition_obj.wait()
                print('write done by t1')

class Thread2(threading.Thread):
    def __init__(self, condition_obj):
        super().__init__()
        self.daemon = True
        self.condition_obj = condition_obj
        self.start()

    def run(self):
        with self.condition_obj:
            while True:
                self.condition_obj.wait()
                print('write done by t2')

if __name__ == '__main__':
    condition = threading.Condition()
    t1 = Thread1(condition)
    t2 = Thread2(condition)

    counter = 0

    while True:
        print("main...")
        counter += 1
        if counter == 5 or counter == 10:
            with condition:
                condition.notify_all()
        time.sleep(1)

    t1.join()
    t2.join()

rawDescription: How to launch two threads and lock them before running, and executing only when unlocked?

description: Launch two threads and lock them before running, and executing only when unlocked


code:
class SomeModel(models.Model):
    field_x_float = models.DecimalField(max_digits=6, decimal_places=4)
    field_x_int = models.DecimalField(max_digits=6, decimal_places=0)

    def save(self, *args, **kwargs):
        self.field_x_int = round(int(self.field_x_float))
        super(SomeModel, self).save(*args, **kwargs)

rawDescription: Django - save float as INT to model

description: Django - save float as INT to model


code:
def clearAll():
    doc = FreeCAD.ActiveDocument
    for obj in doc.Objects:
        doc.removeObject(obj.Label)

rawDescription: Python: How do I programmatically clear the viewer in FreeCAD?


rawDescription: [PyQt4]QPushButton Focus Light

description: [PyQt4]QPushButton Focus Light


rawDescription: What signal is passed to child process on terminating main program?


code:
import pylab as pl
from pandas import *
data = DataFrame(np.random.randn(500).reshape(100,5), columns=list('abcde'))
axes = data.hist(sharey=True, sharex=True)
pl.suptitle("This is Figure title")

rawDescription: add title to collection of pandas hist plots

description: Add title to collection of pandas hist plots


code:
def collatz(number):

    if number % 2 == 0:
        print(number // 2)
        return number // 2

    elif number % 2 == 1:
        result = 3 * number + 1
        print(result)
        return result

n = input("Give me a number: ")
while n != 1:
    n = collatz(int(n))

rawDescription: Making a collatz program automate the boring stuff


code:
def split_suite_into_chunks(num_threads, suite):
    # Compute num_threads such that the number of threads does not exceed the value passed to the function
    # Keep num_threads to a reasonable number of threads
    if num_threads < 0: num_threads = 1
    if num_threads > 8: num_threads = 8
    num_tests = suite.countTestCases()
    s = []
    s_tmp = unittest.TestSuite()
    n = round(num_tests / num_threads)
    for case in suite:
        if n <= 0 and s_tmp.countTestCases() > 0:
            s.append([s_tmp, None])
            num_threads -= 1
            num_tests -= s_tmp.countTestCases()
            s_tmp = unittest.TestSuite()
            n = round(num_tests / num_threads)
        s_tmp.addTest(case)
        n -= 1
    if s_tmp.countTestCases() > 0:
        if s_tmp.countTestCases() > 0: s.append([s_tmp, None])
        num_tests -= s_tmp.countTestCases()
    if num_tests != 0: print("Error: num_tests should be 0 but is %s!" % num_tests)
    return s

rawDescription: Can we limit the number of tests running in parallel with testtools.ConcurrentStreamTestSuite


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox()
driver.get("http://www.chadbourne.com/search/people?az[b]=b")

wait = WebDriverWait(driver, 20)

# get more results
more_results = wait.until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, ".load-more-pager-wrapper .pager-next"))
)
more_results.click()

rawDescription: Cannot find element using Selenium find_element_by_partial_link_text


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

def barchart(data, labels):
    pos = np.arange(len(data)) + 0.5  # the bar centers on the y axis
    plt.barh(pos, data.sort_index(), align='center', height=0.25)
    plt.yticks(pos, labels.sort_index())

data1 = pd.DataFrame({'key': list('ABCDE'), 'val': np.random.randn(5)})

new_keys = list('EDACB')
data1['ii'] = [new_keys.index(x) for x in data1.key]

data1 = data1.set_index('ii')
barchart(data1["val"], data1["key"])
plt.show()

rawDescription: How to determine the order of bars in a matplotlib bar chart

description: Determine the order of bars in a matplotlib bar chart


code:
with open('test.json') as json_data:
    for line in json_data:
        dataline = json.loads(line)
        row = []
        for i in keys:#iterate through keys
            try:
                row.append(dataline[i])
            except:
                pass
        data.append(row)
json_data.close()

rawDescription: Missing data in Python

description: Missing data in Python


code: max(0, x)

rawDescription: Subtract Unless Negative Then Return 0

description: Subtract Unless Negative Then Return 0


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

from ctypes import windll, Structure, c_long, byref #windows only

class RECT(Structure):
    _fields_ = [
    ('left',    c_long),
    ('top',     c_long),
    ('right',   c_long),
    ('bottom',  c_long),
    ]
    def width(self):  return self.right  - self.left
    def height(self): return self.bottom - self.top

def onTop(window):
    SetWindowPos = windll.user32.SetWindowPos
    GetWindowRect = windll.user32.GetWindowRect
    rc = RECT()
    GetWindowRect(window, byref(rc))
    SetWindowPos(window, -1, rc.left, rc.top, 0, 0, 0x0001)

rawDescription: Pygame set window on top without changing it's position


code:
import py7zlib
class SevenZFile(object):
    @classmethod
    def is_7zfile(cls, filepath):
        '''
        Class method: determine if file path points to a valid 7z archive.
        '''
        is7z = False
        fp = None
        try:
            fp = open(filepath, 'rb')
            archive = py7zlib.Archive7z(fp)
            n = len(archive.getnames())
            is7z = True
        finally:
            if fp:
                fp.close()
        return is7z

    def __init__(self, filepath):
        fp = open(filepath, 'rb')
        self.archive = py7zlib.Archive7z(fp)

    def extractall(self, path):
        for name in self.archive.getnames():
            outfilename = os.path.join(path, name)
            outdir = os.path.dirname(outfilename)
            if not os.path.exists(outdir):
                os.makedirs(outdir)
            outfile = open(outfilename, 'wb')
            outfile.write(self.archive.getmember(name).read())
            outfile.close()

rawDescription: Example of how to use PyLZMA

description: Example of how to use PyLZMA


code:
import getpass
pw = getpass.getpass()

rawDescription: Read password from stdin

description: Read password from stdin


code:
''.join(str[i] for i in lOfIndexes)
'elS'

rawDescription: Extracting characters from a string using a list of indexes


code:
# assuming ans is a list
bid_size, ask_size, bid_price, ask_price = ans
cur.execute('''
  INSERT INTO Histdata
  (Name,BidSize,AskSize,BidPrice,AskPrice)
  VALUES(%s,%s,%s,%s,%s)''', ('test', bid_size, ask_size, bid_price, ask_price))

rawDescription: How to reference Python object and place it into MySQL?

description: Reference Python object and place it into MySQL


code:
driver = self.driver
# Uncoment to enable login
driver.get( "https://reg.racingpost.com/mpp/sign_in.sd" )
driver.find_element_by_id( "input-email").clear()
driver.find_element_by_id( "input-email").send_keys("email@address.com" )
driver.find_element_by_id( "input-pwd").clear()
driver.find_element_by_id( "input-pwd").send_keys("passw0rd" )
driver.find_element_by_id( "button-sign-in").click()
time.sleep(8)
driver.get('http://www.racingpost.com/horses2/cards/card.sd?race_id=636248&r_date=2015-10-28#raceTabs=sc_' )
#time.sleep(10)
driver.execute_script( "window.stop()")
print(driver.page_source.encode( 'utf-8'))

rawDescription: Login into a Form using Python3. Not able to do it with python-requests


code:
import pandas as pd
#create dataframe
df = pd.DataFrame({
'Rank' : [5, 1, 10 ,6, 9],
'Status' : ['Closed', 'Closed', 'Open', 'Closed', 'Open']
})

# sort and reindex
df = df.sort('Rank').reset_index()

#slice to first occurrence of your value
df.loc[: df[(df['Status'] == 'Open')].index[0], :]

rawDescription: Python PANDAS: Drop All Rows After First Occurrence of Column Value


code:
my_dict = {}
with open('your_file_name') as f:
    for line in f:
        elements = line.split()
        my_dict[elements[0]] = [float(e) for e in elements[1:]]

rawDescription: reading key and values into python dict from text

description: Reading key and values into python dict from text


rawDescription: Numpy chain comparison with two predicates


code:
pricelist = []
for i, x in enumerate(wb.sheets()):
    header_cells = x.row(0)
    num_rows = x.nrows - 1
    curr_row = 0
    header = [each.value for each in header_cells]
    while curr_row < num_rows:
        curr_row += 1
        row = [int(each.value)
               if isinstance(each.value, float)
               else each.value
               for each in work_sheet.row(curr_row)]
        value_dict = dict(zip(header, row))
        value_dict['turnarround'] = x.name

        pricelist.append(value_dict)

print(pricelist)

rawDescription: xlrd spreadsheet to python list of dicts

description: Xlrd spreadsheet to python list of dicts


code:
def cost(request, offset):
    if request.method == 'POST':
        project = Project.objects.get(title=offset)
        date = request.POST.get('date', '')
        cost = request.POST.get('cost', '')
        cost_obj = Cost(project=project, date=date, cost=cost)
        cost_obj.save()
        return HttpResponseRedirect('/')

rawDescription: How do I save data from a ModelForm to database in django?

description: Save data from a ModelForm to database in django


code:
class MetaFoo(type):
    _ro = "RO"
    _rw = "RW"

    def _get_ro(self):
        return self._ro
    def _get_rw(self):
        return self._rw
    def _set_ro(self, value):
        raise AttributeError("class 'Foo' attribute 'ro' is not writable!")
    def _set_rw(self, value):
        self._rw = value
    ro = property(_get_ro, _set_ro)
    rw = property(_get_rw, _set_rw)

class Foo(object):
    __metaclass__=MetaFoo

assert Foo.rw == "RW"
Foo.rw = 'NEW'
assert Foo.rw == "NEW"

assert Foo.ro == "RO"

try:
    Foo.ro = 'X'
except Exception as e:
    assert str(e) == "class 'Foo' attribute 'ro' is not writable!", str(e)
    assert type(e) == AttributeError

rawDescription: Python static class property set/get

description: Python static class property set/get


rawDescription: Dictionary Iteration Speeds

description: Dictionary Iteration Speeds


code:
from django.utils.timesince import timeuntil

def expiration(self):
    return timeuntil(self.date_offered)

rawDescription: Python/Django Calculate Expiration for Model


code:
bounds = self.canvas.bbox(myText)  # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]

rawDescription: How do I find out the size of a canvas item in Python/Tkinter?

description: Find out the size of a canvas item in Python/Tkinter


code:
now = datetime.now()  # get current datetime
q = db.Query(Promotion)
q = q.filter('end_date <', now)
for promo in q.run():  # loop over filtered promos
    promo.delete()  # delete instance from datastore

rawDescription: how to remove if end date is expired in python google app engine

description: Remove if end date is expired in python google app engine


code:
apsched = BlockingScheduler()

def doSomethingRecurring():
    pass  # Do something really interesting here..

apsched.add_job(doSomethingRecurring, trigger='interval', seconds=2)

apsched.start() # will block

rawDescription: Best way to keep Python script running?

description: Keep Python script running


code:
cars_without_tag_price = '''//td[
    @class="cars" and not(span[@class="colorRed"])
]/following-sibling::td[@class="price"]/span[@class="amount-value"]
'''

for price_node in tree.xpath(cars_without_tag_price):
    price_str = price_node.text.strip()

rawDescription: Xpath - Search within xpath results

description: Xpath - Search within xpath results


code:
import pytest
import fireblog.login as login

pytestmark = pytest.mark.usefixtures("test_with_one_theme")

class Test_groupfinder:
    def test_success(self, persona_test_admin_login, pyramid_config):
        emails = ['id5489746@mockmyid.com', persona_test_admin_login['email']]
        for email in emails:
            res = login.groupfinder(email)
            assert res == ['g:admin']

    def test_failure(self, pyramid_config):
        fake_email = 'some_fake_address@example.com'
        res = login.groupfinder(fake_email)
        assert res == ['g:commenter']

rawDescription: How can I use a pytest fixture as a parametrize argument?

description: Use a pytest fixture as a parametrize argument


code: [[foo(m, f) for f in F] for m in M]

rawDescription: To preallocate or not to preallocate lists in Python


code:
with open('data/'+file_name, "rU") as files:
    data = [x[3] for x in csv.reader(files) if str(x[0]) == '2010']

rawDescription: Extract data of a column by matching data of other column in Python


code:
class LogLock(object):
    def __init__(self, name):
        self.name = str(name)
        self.lock = Lock()

    def acquire(self, blocking=True):
        log.debug("{0:x} Trying to acquire {1} lock".format(
            id(self), self.name))
        ret = self.lock.acquire(blocking)
        if ret == True:
            log.debug("{0:x} Acquired {1} lock".format(
                id(self), self.name))
        else:
            log.debug("{0:x} Non-blocking aquire of {1} lock failed".format(
                id(self), self.name))
        return ret

    def release(self):
        log.debug("{0:x} Releasing {1} lock".format(id(self), self.name))
        self.lock.release()

    def __enter__(self):
        self.acquire()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.release()
        return False    # Do not swallow exceptions

rawDescription: Logging lock acquire and release calls in multi-threaded application


code:
import urllib.request
f = urllib.request.urlopen("http://stackoverflow.com")
print(f.read())

rawDescription: Python 3 Get HTTP page

description: Python 3 Get HTTP page


code:
from pyPdf import PdfFileWriter, PdfFileReader
inputpdf = PdfFileReader(file("abc.pdf", "rb"))

for i in range(inputpdf.numPages // 2):
    output = PdfFileWriter()
    output.addPage(inputpdf.getPage(i * 2))
    if i * 2 + 1 <  inputpdf.numPages:
        output.addPage(inputpdf.getPage(i * 2 + 1))
    outputStream = file("abc-page%s.pdf" % i, "wb")
    output.write(outputStream)
    outputStream.close()

rawDescription: PDFPY splitting large PDF into multiple page files


code: a += 3 if condition else 1

rawDescription: I would like explanations on python list management optimisation, and overall, optimisation


code:
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection

import pylab as p

fig = plt.figure()
pts1 = []
pts2 = []
for i in range(100):
    pts1.append([i,i])
    pts2.append([-i-3,-i])
lines = LineCollection([pts1,pts2], linestyles='solid')
subplt = fig.add_subplot(111,aspect='equal')
subplt.add_collection(lines)
subplt.autoscale_view(True,True,True)
p.show()

rawDescription: Matplotlib autoscale

description: Matplotlib autoscale


code:
import matplotlib.pyplot as plt
plt.subplot('111', axisbg='#FFF999')
plt.plot([1,2,3])
plt.grid(b=True, which='major', color='b', linestyle='-')
plt.show()

rawDescription: Need help styling matplotlib graph

description: Need help styling matplotlib graph


code:
from PyQt4 import QtGui
from PyQt4 import QtCore
import sys

def main():

    app     = QtGui.QApplication(sys.argv)
    tabs    = QtGui.QTabWidget()
    pushButton1 = QtGui.QPushButton("QPushButton 1")
    pushButton2 = QtGui.QPushButton("QPushButton 2")

    tab1    = QtGui.QWidget()
    tab2    = QtGui.QWidget()
    tab3    = QtGui.QWidget()

    vBoxlayout  = QtGui.QVBoxLayout()
    vBoxlayout.addWidget(pushButton1)
    vBoxlayout.addWidget(pushButton2)

    #Resize width and height
    tabs.resize(250, 150)

    #Move QTabWidget to x:300,y:300
    tabs.move(300, 300)

    #Set Layout for Third Tab Page
    tab3.setLayout(vBoxlayout)

    tabs.addTab(tab1,"Tab 1")
    tabs.addTab(tab2,"Tab 2")
    tabs.addTab(tab3,"Tab 3")

    tabs.setWindowTitle('PyQt QTabWidget Add Tabs and Widgets Inside Tab')
    tabs.show()

    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: Which widget in PySide is similar to wxNotebook control in wxwidgets?


code:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=%s)(PORT=1521)))(CONNECT_DATA=(SID=%s)))'
                % (get_env_variable('LIMS_MIGRATION_HOST'), get_env_variable('LIMS_MIGRATION_SID')),
        'USER': 'LIMS_MIGRATION',
        'PASSWORD': get_env_variable('LIMS_MIGRATION_PASSWORD'),
    },
    'mdm_db': {
        'ENGINE': 'django.db.backends.oracle',
        'NAME': '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=GB3P)(PORT=1521)))'
                '(CONNECT_DATA=(SID=GB3P)))',
        'USER': 'MDM',
        'PASSWORD': get_env_variable('MDM_DB_PASSWORD'),
        'TEST': {
            'MIRROR': 'default',  # Added this setting
        }
    },
}

rawDescription: Django ignores router when running tests?


code:
a = np.arange(9).reshape(3,3)
a
mask = np.array([True,False,True])
mask.dtype ## verify we have a bool array
a[mask,0]

rawDescription: Numpy indexing with a one dimensional boolean array


code:
from flask import current_app
current_app.config['attribute_name']

rawDescription: Setting global attributes in Flask framework


rawDescription: Weasel Program, Not understanding Python random library?


code:
t = [ dict(name=d['name'], value=v) for d in t for v in d['value'] ]
t

rawDescription: Duplicate python dict for each value

description: Duplicate python dict for each value


code:
for k in dict1.keys():
    if k in dict2:
        if dict1[k] != dict2[k]:
            return False
return True

rawDescription: python comparing dictionaries

description: Python comparing dictionaries


code:
@app.route(app.config["PROJECT_ROUTE"] + "<project_id>",
    methods=["GET", "POST"])
def project_show(project_id):
    """
    Show the files contained in a specific project. It also allows the user
    to upload a new document, much like projects().

    :param int project_id: The ID of the desired project.
    """

    process_form = forms.DocumentProcessForm(prefix="process")

    # The template needs access to the ID of each file and its filename.
    # Note where this has been moved!
    process_form.files.choices = []
    file_objects = GetFiles(project_id) # database query, this works fine
    for file_object in file_objects:
        process_form.files.choices.append((file_object.id,
            os.path.split(file_object.path)[1]))

    if process_form.validate_on_submit(): #This works fine
        files = request.form.getlist("process-files")
        if len(files) > 0:
            if request.form["action"] == process_form.DELETE:
                delete(files)
            elif request.form["action"] == process_form.PROCESS:
                #TODO: process these files.
                pass

    return render_template("document_list.html",
        process_form=process_form,
        allowed_extensions=app.config["ALLOWED_EXTENSIONS"])

rawDescription: MultiCheckboxField fails validation

description: MultiCheckboxField fails validation


code:
class Profile(BaseProfile):
    user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE)
    ...

rawDescription: Combining abstract model class and multi-table inheritance in Django


code:
font1 = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL, False, u'Consolas')
myTextCtrl.SetFont(font1)

rawDescription: Changing the font on a wxPython TextCtrl widget


rawDescription: Getting the "feels like" temperature based on wind and temperature in python


code:
def training(sess, train_op, loss, global_step):
    while (True):
         _, loss_value = sess.run([train_op, loss])
         current_step = sess.run(global_step)
         if current_step >= FLAGS.max_steps: break

rawDescription: Sharing variables between threads in Tensorflow


code:
dis.dis(lambda: [0.5]*100000)
dis.dis(lambda: [0.5 for i in range(100000)])

rawDescription: Why is the use of [e] * n (for creating a list of single item repeated n times) fast in Python?


code:
os.path.join("foo", "bar", "baz")
os.path.split(_)

rawDescription: which one should I use: os.sep or os.path.sep?

description: Which one should I use: os.sep or os.path.sep


code:
import pympler.asizeof as asizeof
asizeof.asizeof({'a': None})
asizeof.asizeof({})
import sys

sys.getsizeof({})
sys.getsizeof({'a':None})

rawDescription: Memory consumption of a dictionary: set a value None vs. delete the item


rawDescription: Copying a row screws up pandas column data types, how to avoid?


code:
from guess_language import guessLanguage
guessLanguage('ボウリング・フォー・コロンバイン(字幕版)')

rawDescription: How to use guess-language package

description: Use guess-language package


code:
img = Image()
# any editing of 'img'
img.save( 'mytiff.tif' )

rawDescription: what is the valid format for a tiff image in python

description: What is the valid format for a tiff image in python


code:
class CouponField(forms.CharField):
    def to_python(self, value):
        return value.lower()

class StripeSubscriptionSignupForm(forms.Form):
    coupon = CouponField(max_length=30,
        required=False,
        validators=[validate_coupon],
        label=mark_safe("
Promo Code
")
    )

rawDescription: Convert all CharField Form Field inputs to lowercase in Django forms


code:
fd = urllib.urlopen(cover)
# Drop the entire PIL part
covr = MP4Cover(fd.read(), getattr(
            MP4Cover,
            'FORMAT_PNG' if cover.endswith('png') else 'FORMAT_JPEG'
        ))
fd.close() # always a good thing to do

audio['covr'] = [covr] # make sure it's a list
audio.save()

rawDescription: Python Mutagen: add cover photo/album art by url?


rawDescription: Finding control in Windows Forms panel

description: Finding control in Windows Forms panel


code:
from reportlab.lib import utils

def get_image(path, width=1*cm):
    img = utils.ImageReader(path)
    iw, ih = img.getSize()
    aspect = ih / float(iw)
    return Image(path, width=width, height=(width * aspect))

story = []
story.append(get_image('stack.png', width=4*cm))
story.append(get_image('stack.png', width=8*cm))
frame.addFromList(story, c)

rawDescription: Image aspect ratio using Reportlab in Python


code:
from rest_framework.response import Response

class RegistroViewSet(viewsets.ViewSet): #just ViewSet, not ModelViewSet
    def list(self, request):
        queryset = Registro.objects.all()
        serializer = RegistroSerializer(queryset, many=True)
        my_dict = {'success': True, 'data':serializer.data}
        return Response(my_dict)

rawDescription: Nested JSON with Django apiREST

description: Nested JSON with Django apiREST


code:
soup.find('li', attrs={'id':'SalesRank'}).find('b', text="Sellers Rank:")
soup.find('li', attrs={'id':'SalesRank'}).find('b', text="Sellers Rank:").next_sibling

rawDescription: get text oustside tags using Beautifulsoup

description: Get text oustside tags using Beautifulsoup


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

# create supporting points in polar coordinates
r = np.linspace(0,1.25,50)
p = np.linspace(0,2*np.pi,50)
R,P = np.meshgrid(r,p)
# transform them to cartesian system
x, y = R * np.cos(P), R * np.sin(P)

rho = np.sqrt(x**2 + y**2)

# Calculate & Plot
z = rho**2
fig = plt.figure()
ax = fig.gca(projection='3d')
surf = ax.plot_surface(x, y, z, rstride=1, cstride=1,  cmap=cm.bone, alpha=0.15, linewidth=0.25)
plt.show()

rawDescription: Fixing jagged edges of 3D plot, selecting an appropriate mask


code:
import os
for direc in direc_list:
    if direc.startswith(new_direc + os.sep):
        dir_list.remove(direc)

rawDescription: How to refine list of directories to remove subdirectories of newly added directory

description: Refine list of directories to remove subdirectories of newly added directory


code:
from openpyxl import load_workbook
from openpyxl.cell import column_index_from_string as col_index
from openpyxl.cell import get_column_letter as col_letter

def del_col(s, col, cmax=None, rmax=None):
    col_num = col_index(col) - 1
    cols = s.columns
    if isinstance(cmax, str):
        cmax = col_index(cmax)
    cmax = cmax or s.get_highest_column()
    rmax = rmax or s.get_highest_row()
    for c in range(col_num, cmax - 1):
        # print("working on column %i" % c)
        for r in range(0, rmax):
            cols[c][r].value = cols[c+1][r].value
    for r in range(0, rmax):
        cols[c+1][r].value = ''

    return s

if __name__ == '__main__':
    wb = load_workbook('input_book.xlsx')
    ws = wb.active
    # or by name: ws = wb['SheetName']
    col = 'D'
    del_col(ws, col)
    wb.save('output_book.xlsx')

rawDescription: Delete column from xls file

description: Delete column from xls file


code:
class A():
    def __init__(self, i):
        self.i = i
        if i > 0:
            self.a = A(i-1)

a = A(3)
a.i
a.a.i
a.a.a.i
a.a.a.a.a

rawDescription: How do you recursively call a constructor (__init__) in Python?

description: Recursively call a constructor (__init__) in Python


code: os.system("xterm -hold -e python script1.py")

rawDescription: command prompt equivalent in linux

description: Command prompt equivalent in linux


code: fields = [f.name for f in app1.EmployeeExtended._meta._fields() if f not in app1.Employee._meta._fields()]

rawDescription: Django- How to Exclude parent class' fields


code:
for root, dirs, files in os.walk(path):
    files = [f for f in files if not f[0] == '.']
    dirs[:] = [d for d in dirs if not d[0] == '.']
    # use files and dirs

rawDescription: os.walk without hidden folders

description: Os.walk without hidden folders


code:
a = array.array('f')
a.fromfile(f, n)

rawDescription: Python : read array in binary file

description: Python : read array in binary file


code:
def _process_args(cls, attr, out_args, out_kwargs):
    try:
        args = getattr(cls, attr)
    except AttributeError:
        return

    if isinstance(args, Mapping):  # it's a dictionary
        out_kwargs.update(args)
    else:  # it's a list
        if isinstance(args[-1], Mapping):  # it has a dictionary at the end
            out_kwargs.update(args.pop())

        out_args.extend(args)

class Base():
    @declared_attr
    def __mapper_args__(cls):
        args = []
        kwargs = {}

        for mixin in reversed(cls.mro()):
            _process_args(mixin, '__mapper_args__', args, kwargs)

        _process_args(mixin, '__local_mapper_args__', args, kwargs)

        return kwargs  # mapper only takes dict

    @declared_attr
    def __table_args__(cls):
        args = []
        kwargs = {}

        for mixin in reversed(cls.mro()):
            _process_args(mixin, '__table_args__', args, kwargs)

        _process_args(cls, '__local_table_args__', args, kwargs)

        args.append(kwargs)  # [item, item, ...,  kwargs]
        return tuple(args)

rawDescription: Combining __table_args__ with constraints from mixin classes in SQLAlchemy


code:
class GetStats(webapp2.RequestHandler):
    def post(self):
        ...
        somevar = "somevalue"
        application.registry['variable'] = somevar
        ...

# outside class
application.registry.get('variable')

rawDescription: Python webapp2: get value of variable of RequestHandler class


code:
class Sample(PMBaseModel):
    id = peewee.PrimaryKeyField()
    location = peewee.CharField(max_length=255)
    position = peewee.IntegerField()

    class Meta:
        db_table = 'sample'
        order_by = ['-location']

rawDescription: How to use default sorting in peewee meta class

description: Use default sorting in peewee meta class


rawDescription: Firebase .validate not working as expected with $location


code:
UID   = 1
EUID  = 2

def owner(pid):
    '''Return username of UID of process pid'''
    for ln in open('/proc/%d/status' % pid):
        if ln.startswith('Uid:'):
            uid = int(ln.split()[UID])
            return pwd.getpwuid(uid).pw_name

rawDescription: How could I get the user name from a process id in python on Linux?


code:
import copy
if 'career_business' in add_sal and 'career_business' in add_perc and \
      add_sal['career_business'] == add_perc['career_business']:
   add_all = copy.deepcopy( add_sal )
   add_all['percent'] = add_perc['percent']

rawDescription: python: merging dictionaries by identical value of key

description: Python: merging dictionaries by identical value of key


code:
select_3_and_4.transform = select_3_and_4.__call__
select_3_and_4.fit = lambda x: select_3_and_4

rawDescription: How can I use a custom feature selection function in scikit-learn's `pipeline`

description: Use a custom feature selection function in scikit-learn's `pipeline`


code:
@classmethod
def configurable_default(cls):
    if hasattr(select, "epoll"):
        from tornado.platform.epoll import EPollIOLoop
        return EPollIOLoop
    if hasattr(select, "kqueue"):
        # Python 2.6+ on BSD or Mac
        from tornado.platform.kqueue import KQueueIOLoop
        return KQueueIOLoop
    from tornado.platform.select import SelectIOLoop
    return SelectIOLoop

rawDescription: how dose Tornado.ioloop.IOloop.start function work

description: How dose Tornado.ioloop.IOloop.start function work


code:
import numpy as np
def resizeArray(data, newLength):

    datalength = len(data)
    if (datalength == newLength): return data

    appendIndices = []
    appendNow = 0
    step = newLength / datalength
    increase =  step % 1
    for i in np.arange(0, datalength-2, step):
        appendNow += increase
        if appendNow >= 1:
            appendIndices.append(round(i,0))
            appendNow = appendNow % 1

    #still missing values due to floating errors?
    diff = newLength - datalength - len(appendIndices)
    if diff > 0:
        for i in range(0, diff):
            appendIndices.append(np.random.randint(1, datalength - 2))

    #insert average at the specified indizes
    appendVals = [(data[i] + data[i+1]) / 2 for i in appendIndices]
    a = np.insert(data, appendIndices, appendVals)

    return a

rawDescription: Reshape array on xAxis and fill with mean value in Python?


code:
import logging
import logging.config

log_dict = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level': 'INFO',
            'formatter': 'standard',
            'class': 'logging.StreamHandler',
        },
        'file_handler': {
            'level': 'INFO',
            'filename': '/tmp/mylogfile.log',
            'class': 'logging.FileHandler',
            'formatter': 'standard'
        }
    },
    'loggers': {
        '': {
            'handlers': ['file_handler'],
            'level': 'INFO',
            'propagate': True
        },
    }
}
logging.config.dictConfig(log_dict)
logging.info("test")

rawDescription: Basic logging dictConfig in Python

description: Basic logging dictConfig in Python


code:
import json

def get_all_json(teststr):
    decoder = json.JSONDecoder()
    # Find first possible JSON object start point
    sliceat = teststr.find('{')
    while sliceat != -1:
        # Slice off the non-object prefix
        teststr = teststr[sliceat:]
        try:
            # See if we can parse it as a JSON object
            obj, consumed = decoder.raw_decode(teststr)
        except Exception:
            # If we couldn't, find the next open brace to try again
            sliceat = teststr.find('{', 1)
        else:
            # If we could, yield the parsed object and skip the text it was parsed from
            yield obj
            sliceat = consumed

rawDescription: How to find all dictionaries from a long string in python

description: Find all dictionaries from a long string in python


rawDescription: Pulling the value from parsed XML in Python (only)


code:
# simulate binary
b = [chr(c) for c in xrange(256)]

# write to file
with open('file.txt', 'wb') as fout:
    fout.write(''.join(b))

rawDescription: Python - Storing integers all less than 256 with ASCII


code:
import ctypes
dll = ctypes.CDLL('your.dll')
Open = dll.Open
Open.argtypes = [ctypes.POINTER(ctypes.c_void_p),ctypes.c_char_p]
Open.restype = ctypes.c_uint16
Handle = ctypes.c_void_p()
result = Open(ctypes.byref(Handle),'c:\\Config.xml')

rawDescription: how to pass char pointer as argument in ctypes python

description: Pass char pointer as argument in ctypes python


code: map(list,zip(*[i for i in zip(*l) if len(set(i))>1]))

rawDescription: python - best way to leave only unique fields in a set of lists

description: Python - best way to leave only unique fields in a set of lists


code:
from distutils.core import setup
import py2exe
import sys, os, site, shutil

site_dir = site.getsitepackages()[1]
include_dll_path = os.path.join(site_dir, "gnome")

gtk_dirs_to_include = ['etc', 'lib\\gtk-3.0', 'lib\\girepository-1.0', 'lib\\gio', 'lib\\gdk-pixbuf-2.0', 'share\\glib-2.0', 'share\\fonts', 'share\\icons', 'share\\themes\\Default', 'share\\themes\\HighContrast']

gtk_dlls = []
tmp_dlls = []
cdir = os.getcwd()
for dll in os.listdir(include_dll_path):
    if dll.lower().endswith('.dll'):
        gtk_dlls.append(os.path.join(include_dll_path, dll))
        tmp_dlls.append(os.path.join(cdir, dll))

for dll in gtk_dlls:
    shutil.copy(dll, cdir)

# -- change main.py if needed -- #
setup(windows=['main.py'], options={
    'py2exe': {
        'includes' : ['gi'],
        'packages': ['gi']
    }
})

dest_dir = os.path.join(cdir, 'dist')

if not os.path.exists(dest_dir):
    os.makedirs(dest_dir)

for dll in tmp_dlls:
    shutil.copy(dll, dest_dir)
    os.remove(dll)

for d  in gtk_dirs_to_include:
    shutil.copytree(os.path.join(site_dir, "gnome", d), os.path.join(dest_dir, d))

rawDescription: Creating an installer for a python GTK3 application


rawDescription: Matplotlib: How to convert a histogram to a discrete probability mass function?


code:
def convert(nom):
    temp_dict = {}
    for key, value in nom:
        if key in temp_dict:
            temp_dict[key] += value
        else:
            temp_dict[key] = value

    result = []
    for key, value in nom:
        if key in temp_dict:
            result.append((key, temp_dict[key]))
            del temp_dict[key]
    return result

nom = [('orange', 2), ('apple', 4), ('appo', 3), ('appo', 2)]
nom = convert(nom)
print(nom)

rawDescription: Python adding numbers for the tuples in the list with similar string


code:
class Game(models.Model):
    # Other fields...
    on = models.ForeignKey('Member', blank = True)

class Member(models.Model):
    # Other fields...
    game = models.ForeignKey(Game)

rawDescription: How to have two models reference each other Django

description: Have two models reference each other Django


code:
application = tornado.web.Application([
    (r'/blah', BlahHandler),
    ], **settings)

# Create an HTTP server listening on localhost, port 8080.
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080, address='127.0.0.1')

rawDescription: How do you run the Tornado web server locally?

description: Run the Tornado web server locally


rawDescription: how to get all possible combination of items from 2-dimensional list in python?

description: Get all possible combination of items from 2-dimensional list in python


code:
# inital settings
size = 50 # size of the maxtrix = size*size
n = 20 # number of points in the matrix
vmin=0 # min value for the colorcoding
vmax=3 # max value for the colorcoding

#generate the data and colormap
data = np.vstack((np.random.randint(0,size,n),np.random.randint(0,size,n))).T
param = np.random.randint(vmin,vmax+1,n)
# 0-red, 1-blue, 2-green and 3-yellow
cmap = mpl.colors.ListedColormap([[1,0,0], [0,0,1], [0,1,0], [1,1,0]])

# create the n*x matrix and x/y 'colorbars'
mtrx = np.zeros((size,size))-1
xcolors = np.zeros(size)-1
ycolors = np.zeros(size)-1

# map the data to the n*n matrix and x/y 'colorbars'
for n, item in enumerate(data):
    x, y = item

    xcolors[x] = param[n]
    ycolors[y] = param[n]
    mtrx[x,y] = 1 # relace 1 with param [n] to color the matrix points as well

# mask all 'empty' matrix entries
mtrx = np.ma.masked_values(mtrx,-1)
xcolors = np.ma.masked_values(xcolors.reshape((xcolors.size,1)),-1)
ycolors = np.ma.masked_values(ycolors.reshape((1,xcolors.size)),-1)

fig = plt.figure(figsize=(6,6))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
x_ax = fig.add_axes([0.05, 0.1, 0.05, 0.8])
y_ax = fig.add_axes([0.1, 0.05, 0.8, 0.05])

ax.imshow(mtrx, cmap=plt.cm.Greys_r, interpolation='none')
ax.set_title('My matrix')

x_ax.imshow(xcolors, cmap=cmap, interpolation='none',vmin=vmin, vmax=vmax)
y_ax.imshow(ycolors, cmap=cmap, interpolation='none',vmin=vmin, vmax=vmax)

for tmpax in [ax, y_ax, x_ax]:
    tmpax.set_yticks([])
    tmpax.set_xticks([])
    tmpax.set_yticklabels([])
    tmpax.set_xticklabels([])

ax.set_xticks(np.arange(-0.5,size + .5,1))
ax.set_yticks(np.arange(-0.5,size + .5,1))
ax.grid(True, color='w', linestyle='-')

plt.setp(y_ax, frame_on=False)
plt.setp(x_ax, frame_on=False)

rawDescription: associate a colorbar with a matrix graph

description: Associate a colorbar with a matrix graph


code:
string = "Parameter Value:     0.12345"
s = string.split(":")
num = 100.123
":".join([s[0],("%0.5f"%(num)).rjust(len(s[1]))])
string

rawDescription: Line up decimal place when using re.sub to replace decimal numbers of varying size


code: {{ model_instance.get_gender_display }}

rawDescription: Django: How to map different words to specific charfield entries (should be a simple answer)


rawDescription: Nested dataframe in pandas

description: Nested dataframe in pandas


code:
import matplotlib.pyplot as plt
f = open('wind.txt', 'r')
min_vector = []
max_vector = []
z_vector = []
for line in f.readlines():
    line = line.strip()
    columns = line.split()
    z = columns[0]
    u_min = columns[1]
    u_10 = columns[2]
    u_max = columns[4]
    u_90 = columns[3]

    min_vector.append(u_min)
    max_vector.append(u_max)
    z_vector.append(z)

plt.plot(min_vector, z_vector, '-o')
plt.plot(max_vector, z_vector, '-o')

rawDescription: Plotting with lines connecting points

description: Plotting with lines connecting points


code:
for record in data:
    new_rec = [record[0], " ".join(record[1:])]

rawDescription: write csv file with special format in python

description: Write csv file with special format in python


code:
from autobahn.wamp.types import CallOptions

options = CallOptions(on_progress=None, timeout=120, disclose_me=True)
res = session.call(u'com.example.function', server_events, options=options)

rawDescription: Caller identification in Autobahn | Python


rawDescription: How to change directory according to the username?

description: Change directory according to the username


code:
@app.route('/punch/<punchid>', methods=['GET', 'POST'])
@login_required
def punch(punchid):
    if not g.user.is_manager:
        flash('You are not authorized to access this function.')
        return redirect(url_for('user', nickname=g.user.nickname))

    # only for the initial GET request do you want to load
    # the original data from the model into the form
    punch = Punch.query.get(punchid)
    # should throw in a check to make sure this exists, and abort(404) if not...
    if request.method == 'GET':
        form = PunchForm()
        form.notes.data = punch.notes
        form.timestamp.data = punch.timestamp
    if form.validate_on_submit():
        punch.notes = form.notes.data
        punch.timestamp = form.timestamp.data
        print(form.errors)
        db.session.commit()
        flash('Your changes have been saved.')
        return render_template('report.html', punches=Punch.query.order_by(desc(Punch.timestamp)).all())
    else:
        flash('Please check to see that you have entered information correctly.')
    return render_template('punch.html', form=form, punch=punch)

rawDescription: Trouble Updating Records with WTForms in Flask


code:
import hashlib
import brypt
def myhash(word):
    salt = "$2a$06$" + hashlib.sha1(word).hexdigest()[0:22] + "$"
    return bcrypt.hashpw(word, salt)

rawDescription: Converting an blowfish encyption algo from php to python


code:
from __future__ import print_function

import boto3
import json

def lambda_handler(event, context):
    client = boto3.client('lambda')
    client.invoke_async(
        FunctionName='my_long_task_running_function_executer',
        InvokeArgs=json.dumps(event)
    )
    return {"result": "OK"}

rawDescription: How to invoke Lambda function with Event Invocation Type via API Gateway?

description: Invoke Lambda function with Event Invocation Type via API Gateway


code:
master = "in.csv"
filelist = "file.csv", "file2.csv"
joined = "joined.csv"
dict1 = {}

with open(master, 'r') as csvfile:
    for row in csv.reader(csvfile):
        key = row[0]
        dict1[key] = row[1:]  # note this does not check for duplicate keys

for filename in filelist:
    with open(filename, 'rb') as csvfile:
        seen = set()
        for row in csv.reader(csvfile):
            key = row[0]
            if key in dict1:
                if key in seen:
                    print('Error: duplicate key %r in file %r - ignored' %
                                   (key, filename))
                else:
                    dict1[key].append(row[1])
                    seen.add(key)
            else:  # key not in master
                pass  # ignore

        # add null entry for any keys not present in this file
        for key in dict1:
            if key not in seen:
                dict1[key].append(None)

# write the data in the merged dictionary into a new csv file
with open(joined, 'wb') as newcsvfile:
    csv.writer(newcsvfile).writerows(
        ([key]+attrlist) for key, attrlist in sorted(dict1.iteritems()))

rawDescription: Join Multiple Files Dictionary

description: Join Multiple Files Dictionary


code:
deltas = MyArray[1]-MyArray[0]
diff_days = np.fromiter((d.days for d in deltas), dtype=float, count=len(deltas))

rawDescription: Get days property from numpy.ndarray of timedelta objects


code:
n = int(input("Number of values in list: "))
numbers = []
average=0
for i in range(n):
    lists = float(input("Enter a floating number: "))
    numbers.append(lists)
    average=average+ lists/n

print (numbers)

# filter numbers not greater than average, and get a new list
numbers = [n for n in numbers if n <= average]
print (numbers)

rawDescription: Python- delete items of list

description: Python- delete items of list


code:
logs = pd.DataFrame(columns=['time', 'article_id', 'user_id'])
# regc = re.compile(r'\[(?P<time>.*?)\] "GET (.*?=)(?P<article_id>\d+)(&.*?=)(?P<user_id>\d+)')
# alternative regexp that might be more efficient
regc = re.compile(r'\[(?P<time>.+)\] "GET (?:.+article_id=)(?P<article_id>\d+)(?:&user_id=)(?P<user_id>\d+)')

for line in log_file:
    m = regc.match(line)
    time = m.group('time')
    article_id = m.group('article_id')
    user_id = m.group('user_id')
    logs.append([time, article_id, user_id])

rawDescription: Getting a Pandas dataframe from an Apache log file


code: result = [int(x) for x in a]

rawDescription: Lists in Python

description: Lists in Python


rawDescription: SKLearn how to get decision probabilities for LinearSVC classifier


code:
from bs4 import BeautifulSoup
chunk = '
BLA bla bla html...
Copyright 2014 NPR

'
BeautifulSoup(chunk, 'html.parser')
BeautifulSoup(chunk, 'html5lib')
BeautifulSoup(chunk, 'lxml')
BeautifulSoup(chunk, 'xml')

rawDescription: Python3, BeautifulSoup dropping a paragraph tag


code: " ".join(result.strings)

rawDescription: Suggestions on get_text() in BeautifulSoup


code:
hist, bins = np.histogram(norm2d.ravel(), normed=True, bins=100)
threshold = bins[np.cumsum(hist) * (bins[1] - bins[0]) > 0.8][0]
mnorm2d = np.ma.masked_less(norm2d,threshold)
com = ndimage.measurements.center_of_mass(mnorm2d)
plt.imshow(norm2d, origin="lower")
plt.scatter(*com[::-1])
plt.show()

rawDescription: ndimage's center_of_mass to calculate the position of a Gaussian peak

description: Ndimage's center_of_mass to calculate the position of a Gaussian peak


code:
import urllib
q = ['with space1', 'with space2']
qescaped = map(urllib.quote, q)

rawDescription: PY: Url Encode without variable name

description: PY: Url Encode without variable name


rawDescription: Python range() with negative strides

description: Python range() with negative strides


code: '\N{BLACK SPADE SUIT}'

rawDescription: Special characters in Python strings when writing code


code: lines = [r for r in open(thefile) if not r.isspace() and r[0] != '#']

rawDescription: python style question around reading small files

description: Python style question around reading small files


rawDescription: How do I convert datetime to date (in Python)?

description: Convert datetime to date (in Python)


rawDescription: How to get list of filenames from directory with same extension using python?

description: Get list of filenames from directory with same extension using python


rawDescription: Pandas groupby columns without multiindex


code:
def foo():pass
bar=foo
spam=bar     #another reference to foo function object
spam.__name__,bar.__name__
spam is foo,bar is foo
import sys
sys.getrefcount(foo)  # no of variable pointing to that object

rawDescription: How do I get a functions current name in python

description: Get a functions current name in python


code:
import random

def weighted_choice(weights):
    totals = []
    running_total = 0

    for w in weights:
        running_total += w
        totals.append(running_total)

    rnd = random.random() * running_total
    for i, total in enumerate(totals):
        if rnd < total:
            return i

rawDescription: Select random item with weight

description: Select random item with weight


code:
import unittest
from wsgiref import util
import io

class WSGIBaseTest(unittest.TestCase):
    '''Base class for unit-tests. Provides up a simple interface to make requests
    as though they came through a wsgi interface from a user.'''
    def setUp(self):
        '''Set up a fresh testing environment before each test.'''
        self.cookies = []
    def request(self, application, url, post_data = None):
        '''Hand a request to the application as if sent by a client.
        @param application: The callable wsgi application to test.
        @param url: The URL to make the request against.
        @param post_data: A string.'''
        self.response_started = False
        temp = io.StringIO(post)
        environ = {
            'PATH_INFO': url,
            'REQUEST_METHOD': 'POST' if post_data else 'GET',
            'CONTENT_LENGTH': len(post),
            'wsgi.input': temp,
            }
        util.setup_testing_defaults(environ)
        if self.cookies:
            environ['HTTP_COOKIE'] = ';'.join(self.cookies)
        self.response = ''
        for ret in application(environ, self._start_response):
            assert self.response_started
            self.response += str(ret)
        temp.close()
        return response
    def _start_response(self, status, headers):
        '''A callback passed into the application, to simulate a wsgi
        environment.

        @param status: The response status of the application ("200", "404", etc)
        @param headers: Any headers to begin the response with.
        '''
        assert not self.response_started
        self.response_started = True
        self.status = status
        self.headers = headers
        for header in headers:
            # Parse out any cookies and save them to send with later requests.
            if header[0] == 'Set-Cookie':
                var = header[1].split(';', 1)
                if len(var) > 1 and var[1][0:9] == ' Max-Age=':
                    if int(var[1][9:]) > 0:
                        # An approximation, since our cookies never expire unless
                        # explicitly deleted (by setting Max-Age=0).
                        self.cookies.append(var[0])
                    else:
                        index = self.cookies.index(var[0])
                        self.cookies.pop(index)
    def new_session(self):
        '''Start a new session (or pretend to be a different user) by deleting
        all current cookies.'''
        self.cookies = []

rawDescription: Python 3.x WSGI Testing framework

description: Python 3.x WSGI Testing framework


code:
from natlinkutils import GrammarBase

class CatchAll(GrammarBase):

    # this spec will catch everything
    gramSpec = """
        <start> exported = {emptyList};
    """

    def initialize(self):
        self.load(self.gramSpec, allResults=1)
        self.activateAll()

    def gotResultsObject(self, recogType, resObj):
        for x in range(0, 100):
            try:
                possible_interpretation = resObj.getWords(x)
                # do whatever sort of filtering you want here
            except Exception:
                break

c = CatchAll()
c.initialize()

def unload():
    global c
    if c:
        c.unload()
    c = None

rawDescription: python dragonfly to recognize similar words

description: Python dragonfly to recognize similar words


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

iris = sns.load_dataset("iris")

g = sns.PairGrid(iris)
g = g.map(plt.scatter)

xlabels,ylabels = [],[]

for ax in g.axes[-1,:]:
    xlabel = ax.xaxis.get_label_text()
    xlabels.append(xlabel)
for ax in g.axes[:,0]:
    ylabel = ax.yaxis.get_label_text()
    ylabels.append(ylabel)

for i in range(len(xlabels)):
    for j in range(len(ylabels)):
        g.axes[j,i].xaxis.set_label_text(xlabels[i])
        g.axes[j,i].yaxis.set_label_text(ylabels[j])

plt.show()

rawDescription: Seaborn PairGrid: show axes labels for each subplot


code:
#!/usr/bin/env python
import os
import subprocess
from multiprocessing.pool import ThreadPool

def run(i):
    working_dir = "dir/Run/" + str(i + 1)
    return i, subprocess.call(os.path.join(working_dir, 'mej'), cwd=working_dir)

results = ThreadPool().map(run, range(n))

rawDescription: Running multiple external programs

description: Running multiple external programs


code:
import re
word1 = " ".join(re.findall("[a-zA-Z]+", st))

rawDescription: Extracting only characters from a string in Python


code:
with requests.session() as s:
    s.post(url, data=user_data)
    r = s.get(url_2)

rawDescription: pass session cookies in http header with python urllib2?

description: Pass session cookies in http header with python urllib2


code:
from flask import redirect, url_for, survey_id

@app.route('/success/<int:result_id>')
def success(result_id):
     # replace this with a query from whatever database you're using
     result = get_result_from_database(result_id)
     # access the result in the tempalte, for example {{ result.name }}
     return render_template('success.html', result=result)

@app.route('/survey')
def survey():
    if request.method == 'POST':
        # replace this with an insert into whatever database you're using
        result = store_result_in_database(request.args)
        return redirect(url_for('success', result_id=result.id))

    # don't need to test request.method == 'GET'
    return render_template('survey.html')

rawDescription: Redirect to other view after submitting form


code:
import unittest

class UnderTest(object):

    def f1(self, i):
        return i + 1

    def f2(self, i):
        return i + 2

class TestMeta(type):

    def __new__(cls, name, bases, attrs):
        funcs = [t for t in dir(UnderTest) if t[0] == 'f']

        def doTest(t):
            def f(slf):
                ut=UnderTest()
                getattr(ut, t)(3)
            return f

        for f in funcs:
            attrs['test_gen_' + f] = doTest(f)
        return type.__new__(cls, name, bases, attrs)

class T(unittest.TestCase):

    __metaclass__ = TestMeta

    def testOne(self):
        self.assertTrue(True)

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

rawDescription: How do I concisely implement multiple similar unit tests in the Python unittest framework?

description: Concisely implement multiple similar unit tests in the Python unittest framework


code:
import pickle
pickle.dump(model, open('./output.bin', 'w'))

rawDescription: How to create a "serialized" version of a model?

description: Create a "serialized" version of a model


code:
def __init__(self, *args, **kwargs):
     super(YourForm, self).__init__(*args, **kwargs)
     self.fields['password'].widget.attrs['class'] = 'classname'

rawDescription: django widget as argument

description: Django widget as argument


code:
from django.forms.models import modelformset_factory

def new_quote(request):
    QuoteFormSet = modelformset_factory(Quote, form=QuoteForm, extra=2)
    if request.method == 'POST':
        formset = QuoteFormSet(request.POST, request.FILES)
        if formset.is_valid():
            formset.save()
            #
            return HttpResponseRedirect('/')
    else:
        formset = QuoteFormSet()

    return render(request, 'request_quote_form.html', {
    'formset': formset,
    })

rawDescription: Save a formset of custom forms django

description: Save a formset of custom forms django


code:
def create_window(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_position(gtk.WIN_POS_CENTER)
    self.window.connect("key-press-event", self.keypress)
    self.window.connect("focus-out-event", self.cancel)
    self.window.connect("destroy", self.cancel)
    self.entry = gtk.Entry(200)
    button = gtk.Button("go")
    button.connect("clicked", self.command)
    box = gtk.HBox()
    box.add(self.entry)
    box.add(button)
    self.window.add(box)
    self.window.set_keep_above(True)
    self.window.show_all()
    gtk.idle_add(self.bring_to_front)

def bring_to_front(self):
    self.window.present()

rawDescription: Force grab keyboard focus pygtk

description: Force grab keyboard focus pygtk


rawDescription: Spliting a string with range() in python


rawDescription: Python: How to use multiple list with a for loop


rawDescription: Easiest way to slice border particles in a 3D numpy array with variable border size


code:
pub_dict = {}
for p in pub:
   if not p.key in pub_dict:
      pub_dict[p.key] = []
   pub_dict[p.key].append(p)

rawDescription: Losing elements in python code while creating a dictionary from a list?


code:
plt.plot(list1, 'r.')
plt.plot(list2, 'b.')
plt.plot(list3, 'g.')
plt.ylabel('cosine values')
plt.yscale('log')
plt.show()

rawDescription: Python: pyplot - plot smooth curves with less clutter and show data points on the curve


code:
import pytest

@pytest.fixture
def backends():
    """Mapping of possible backend ids to their constructor functions."""
    return {
        1: connect_to_backend_1,
        2: connect_to_backend_2
    }

@pytest.fixture(scope="session", params=[1, 2])
def backend(request, backends):
    """Parametrized backend instance."""
    return backends[request.param]()

def test_contract_method_1(backend):
    result = run_contract_method_1()
    assert result == backend.get_data()

def test_contract_method_2(backend):
    result = run_contract_method_2()
    assert result == backend.get_data()

rawDescription: Running one test with different versions of fixture


code: bookmark, created = Bookmark.objects.get_or_create(user=request.user, link=link)

rawDescription: How does Django compare objects?

description: How does Django compare objects


code:
class TagParser(HTMLParser):

    def __init__(self, url, tag):
        HTMLParser.__init__(self)
        self.tag = tag
        req = urllib2.urlopen(url)
        self.feed(req.read())

    def handle_starttag(self, tag, attrs):
        if tag != self.tag: return
        for name, value in attrs:
            print("Found Tag({2}) --> [{0}]{1}".format(name, value, self.tag))

rawDescription: Python: Check the value of a variable passed as a parameter in another method?


code: plt.xlabel('position %d' % entries, fontproperties=font_manager.FontProperties(size=10))

rawDescription: How to put a variable in x/y label using matplotlib module pyplot

description: Put a variable in x/y label using matplotlib module pyplot


code:
from pytube import YouTube

yt = YouTube("http://www.youtube.com/watch?v=Ik-RsDGPI5Y")
video = yt.get('mp4', '720p')
video.download('/tmp/')  # downloaded video to /tmp directory

rawDescription: Obtaining youtube's page load time via pycurl


code:
self.ui.txtName.setProperty("rules", QtCore.QVariant())
#or
self.ui.txtName.setProperty("rules", None)

rawDescription: How to remove property of ui component in pyside/pyqt?

description: Remove property of ui component in pyside/pyqt


code:
x = input("Write True or False\n")

if x == "True":
  print("its true")
elif x == "False":
    print ("its false")
else:
    print("don't know what that is")

rawDescription: How to convert input as boolean type and use it in if and else statement?

description: Convert input as boolean type and use it in if and else statement


code:
import matplotlib.pyplot as plt
import numpy as np
x = [1.81,1.715,1.78,1.613,1.629,1.714,1.62,1.738,1.495,1.669,1.57,1.877,1.385]
y = [0.924,0.915,0.914,0.91,0.909,0.905,0.905,0.893,0.886,0.881,0.873,0.873,0.844]

# This is the string that should show somewhere over the plotted line.
line_string = 'name of line'

# plotting
fig, ax = plt.subplots(1,1)
l, = ax.plot(x,y)
pos = [(x[-2]+x[-1])/2., (y[-2]+y[-1])/2.]
# transform data points to screen space
xscreen = ax.transData.transform(zip(x[-2::],y[-2::]))
rot = np.rad2deg(np.arctan2(*np.abs(np.gradient(xscreen)[0][0][::-1])))
ltex = plt.text(pos[0], pos[1], line_string, size=9, rotation=rot, color = l.get_color(),
     ha="center", va="center",bbox = dict(ec='1',fc='1'))

def updaterot(event):
    """Event to update the rotation of the labels"""
    xs = ax.transData.transform(zip(x[-2::],y[-2::]))
    rot = np.rad2deg(np.arctan2(*np.abs(np.gradient(xs)[0][0][::-1])))
    ltex.set_rotation(rot)

fig.canvas.mpl_connect('button_release_event', updaterot)
plt.show()

rawDescription: Print string over plotted line (mimic contour plot labels)


code:
import matplotlib as mpl
import matplotlib.pyplot as plt

min, max = (-40, 30)
step = 10

# Setting up a colormap that's a simple transtion
mymap = mpl.colors.LinearSegmentedColormap.from_list('mycolors',['blue','red'])

# Using contourf to provide my colorbar info, then clearing the figure
Z = [[0,0],[0,0]]
levels = range(min,max+step,step)
CS3 = plt.contourf(Z, levels, cmap=mymap)
plt.clf()

# Plotting what I actually want
X=[[1,2],[1,2],[1,2],[1,2]]
Y=[[1,2],[1,3],[1,4],[1,5]]
Z=[-40,-20,0,30]
for x,y,z in zip(X,Y,Z):
    # setting rgb color based on z normalized to my range
    r = (float(z)-min)/(max-min)
    g = 0
    b = 1-r
    plt.plot(x,y,color=(r,g,b))
plt.colorbar(CS3) # using the colorbar info I got from contourf
plt.show()

rawDescription: Matplotlib - add colorbar to a sequence of line plots


code:
@task(ignore_result=True)
def copy_media(path):
    copy_media.update_state(state=STARTED, meta={'total': total, 'done': done})
    documents = Document.objects.all()
    total = documents.count()
    copy_media.update_state(state=STARTED, meta={'total': total, 'done': done})
    job = TaskSet(tasks=[process_doc.subtask((document, path))
                         for document in documents])
    results = job.apply_async()
    doc_name = ''
    while not results.ready():
        done = results.completed_count()
        if done:
            last = done - 1
            for idx in xrange(last, -1, -1):
                if results[idx].ready():
                    doc_name = results[idx].result
                    break
        copy_media.update_state(state=STARTED, meta={'total': total, 'done': done, 'doc-name': doc_name})
        time.sleep(0.25)

@task()
def process_doc(document, path):
    # Do some stuff
    return document

rawDescription: In Celery how to update the state of a main task until all his subtasks are done?


code: angle = (angle + 0.2 + pi) % (2*pi) - pi

rawDescription: Sum of two angles

description: Sum of two angles


code:
for eachId in listOfIds:
    successful = False
    while not successful:
        response = makeRequest(eachId)
        if response == 'market is closed':
            time.sleep(24*60*60) #sleep for one day
        else:
            successful = True

rawDescription: repeat an iteration of for loop

description: Repeat an iteration of for loop


code:
import psycopg2, glob

sitesizetemp = glob.glob('C:/Data/Sheltered BLPUs/CSVs/sitesize*.csv')
conn = psycopg2.connect("dbname=postgres user=postgres")
cur = conn.cursor()

for f in sitesizetemp:
    cur.execute("COPY sitesize FROM '%s' DELIMITER ',' CSV;" % f)

conn.commit()
cur.close()
conn.close()

rawDescription: COPY to Postgres using glob result in Python


code:
_list = []

def calc_average(total):
    return total / 5

def determine_grade(grade):
    if grade >= 90 and grade <= 100:
        return 'A'
    elif grade >= 80 and grade <= 89:
        return 'B'
    elif grade >= 70 and grade <= 79:
        return 'C'
    elif grade >= 60 and grade <= 69:
        return 'D'
    else:
        return 'F'

while True:
    grade = int(input('Enter grade: '))
    _list.append(grade)

    avg = calc_average(sum(_list))
    abc_grade = ' '.join([determine_grade(mark) for mark in _list])

    if len(_list) > 5:
        break

print('Average grade is: ', avg)
print('Letter grades for entered grades are: ', abc_grade)

rawDescription: determining average grades and displaying letter grades

description: Determining average grades and displaying letter grades


code:
for k in a.keys():
     a[k] = a[k] * 2

rawDescription: Python: How to multiply values of a Counter object?


code:
data = {'banana' : ['file1.txt', 'file2.txt', 'file3.txt'],
        'apple' : ['file2.txt', 'file3.txt']}
query = "banana apple"

result = None
for key in (k for k in query.split() if k in data):
    if result is None:
        result = set(data[key])
    else:
        result &= set(data[key])

rawDescription: How can I return common values of dictionary values for specific keys?

description: Return common values of dictionary values for specific keys


code:
if request.method == 'POST':
    formset = ReplyFormSet(request.POST, queryset=Reply.objects.none())
    if formset.is_valid():
        ...
else:
    formset = ReplyFormSet(queryset=Reply.objects.none())

rawDescription: My modelformset displays already validated forms


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

    def __getattr__(self, name):
        if name.startswith('sp_'):
            value = getattr(self.wrapped, name)
            setattr(self, name, value)
            return value

        raise AttributeError

rawDescription: How to add new attributes to struct_spwd type while preserving its original attributes?

description: Add new attributes to struct_spwd type while preserving its original attributes


rawDescription: Convert image to a digital matrix using python


code:
from collections import defaultdict
import math

DF = defaultdict(int)
for filename in glob.glob(os.path.join(path, '*.txt')):
    words = re.findall(r'\w+', open(filename).read().lower())
    for word in set(words):
        if len(word) >= 3 and word.isalpha():
            DF[word] += 1  # defaultdict simplifies your "if key in word_idf: ..." part.

# Now you can compute IDF.
IDF = dict()
for word in DF:
    IDF[word] = math.log(doccounter / float(DF[word])) # Don't forget that python2 uses integer division.

rawDescription: Finding document frequency using Python

description: Finding document frequency using Python


code:
import numpy as np
import scipy.misc
import scipy.ndimage
import skimage.feature
import skimage.morphology

# parameters
THRESHOLD = 128

# read image
im = scipy.misc.imread("JPh65.png")
# convert to gray image
im = im.mean(axis=-1)
# find peaks
peak = skimage.feature.peak_local_max(im, threshold_rel=0.9, min_distance=10)
# make an image with peaks at 1
peak_im = np.zeros_like(im)
for p in peak:
    peak_im[p[0], p[1]] = 1
# label peaks
peak_label, _ = scipy.ndimage.label(peak_im)
# propagate peak labels with watershed
labels = skimage.morphology.watershed(255 - im, peak_label)
# limit watershed labels to area where the image is intense enough
result = labels * (im > THRESHOLD)

rawDescription: python separate round particles by offsetting contours / shrinking polygones

description: Python separate round particles by offsetting contours / shrinking polygones


code:
import sys

if len(sys.argv) == 1:
    sys.argv.append("py2exe")

rawDescription: Using Py2Exe without command line

description: Using Py2Exe without command line


code:
from zipfile import ZipFile
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

in_memory = StringIO.StringIO(original_zip_data)
zf = ZipFile(in_memory, "a")

zf.writestr("file.txt", "some text contents")

zf.close()

new_zip_data = in_memory.getvalue()

rawDescription: Python: Open zip file from binary instead of filename


code:
import StringIO
import csv

csvfile = StringIO.StringIO("""queryInclude,yahoo,value1
queryInclude,yahoo,value2
queryInclude,yahoo,value3
queryExclude,yahoo,value4
queryExclude,yahoo,value5
queryInclude,google,value6
queryExclude,google,value7""")

reader = csv.reader(csvfile, delimiter=',', quotechar='|')

dict1={}
for row in reader:
    key1, provider, value1 = row
    if not dict1.has_key(key1):
        dict1[key1] = {}
    if not dict1[key1].has_key(provider):
        dict1[key1][provider] = []
    dict1[key1][provider].append(value1)

rawDescription: python import csv lists

description: Python import csv lists


code:
import re
input = "foo%22somedata%2Bgoeshere%22testbarbaz"
re.findall('foo%22.*?(?=%22)', input)

rawDescription: How to find anything that matches excluding a certain string?

description: Find anything that matches excluding a certain string


code:
import re

str = """
Trying 10.40.81.167...

    Connected to 10.40.81.167.

    Escape character is '^]'.

    ####################################
    #                                  #
    # RADIUS authorization disabled    #
    # Enter local login/password       #
    #                                  #
    ####################################
    bt6000 login: admin
    Password:
    Please, fill controller information at first time (Ctrl+C to abort):
    ^C
    Controller information filling canceled.
    ^Cadmin@bt6000# firmware info
    Active boot partition:  1
    Partition 0 (reserved):
            Firmware:       Energomera-2.3.1
            Version:        10117
    Partition 1 (active):
            Firmware:       Energomera-2.3.1_01.04.15c
            Version:        10404M
    Kernel version: 2.6.38.8 #2 Mon Mar 2 20:41:26 MSK 2015
    STM32:
            Version:        bt6000 10083
            Part Number:    BT6024
            Updated:        27.04.2015 16:43:50
    admin@bt6000#
"""
re.findall(r"Firmware:.*?([0-9].*)\s", str)
reserved_firmware = re.search(r"reserved.*\s*Firmware:.*?([0-9].*)\s", str).group(1)
reserved_firmware
active_firmware = re.search(r"active.*\s*Firmware:.*?([0-9].*)\s", str).group(1)
active_firmware

rawDescription: Python 3, extract info from file problems


code:
data = np.arange(24).reshape(4,6)
data_split = data.reshape(2, 2, 3, 2)
data_split = np.transpose(data_split, (0, 2, 1, 3))
data_split = data_split.reshape(-1, 2, 2) # this makes a copy
data_split

rawDescription: Is not it possible to patch the 2d array into array of subarry using functions available in numpy?


code: pd.rolling_sum(df, 60)

rawDescription: Sum rainfall over last 60 days in Pandas?


code:
import boto

conn = boto.connect_s3('REGION')
bucket = conn.get_bucket('BUCKET')

for k in bucket.list():
  bucket.copy_key(new_key_name=k.key, src_bucket_name=bucket.name, src_key_name=k.key, encrypt_key=True)

rawDescription: AWS S3: Enable encryption through API/Script


code:
def showDialog(self):
    filename = QtGui.QFileDialog.getOpenFileName(self, 'Open File', '.')
    filename2 = str (filename)

    pdml = open(filename2)
    WiresharkXML.parse_fh(pdml, packet_cb)   # NOTE: No assignment on this line any more!
    self.lineEdit.setText('The PDML File Contains '+ str(num_packets)+' Packets')

rawDescription: Python and QT returns "none" when parsing PDML files


code:
import Tkinter

class Store1:
    def __init__(self):
        self.variable = Tkinter.IntVar()
        self.variable1 = Tkinter.IntVar()

    def add(self):
        counter1 = self.variable
        counter1.set(counter1.get() + 1)
        counter2 = self.variable1
        counter2.set(counter2.get() + 1)
        return counter1.get(), counter2.get()

class Main(Tkinter.Tk):
    def __init__(self, *args, **kwargs):
        Tkinter.Tk.__init__(self, *args, **kwargs)
        counter1 = Store1()
        counter2 = Store1()

        def both_counters():
            counter1.add()
            counter2.add()

        self.label  = Tkinter.Label(self, textvariable=counter1.variable)
        self.button = Tkinter.Button(self, command=both_counters, text='+1')
        self.label.pack()
        self.button.pack()

        self.label1  = Tkinter.Label(self, textvariable=counter2.variable1)
        self.button1 = Tkinter.Button(self, command=both_counters, text='+1')
        self.label1.pack()
        self.button1.pack()

root = Main()
root.mainloop()

rawDescription: why aren't both labels updated?

description: Why aren't both labels updated


code:
import num2word
dir(num2word)
num2word.to_card(1111)

rawDescription: How to convert integer to its English representation in Python?

description: Convert integer to its English representation in Python


code:
import copy

class A(object):
    def __init__(self, sth, blah):
        self.sth = sth
        self.blah = blah

    def do_sth(self):
        print(self.sth, self.blah)

class B(A):
    def __init__(self, param):
        self.param = param

    def do_sth(self):
        print(self.param, self.sth, self.blah)

a = A("one", "two")
almost_b = copy.deepcopy(a)
almost_b.__class__ = B
B.__init__(almost_b, "three")
almost_b.do_sth() # it would print "three one two"

rawDescription: Create a subclass object with initialized parent object


code:
import subprocess

def openSettings():
    subprocess.Popen([r"C:\Windows\System32\DpiScaling.exe"])

openSettings()

rawDescription: How can I get control panel display setting on python?

description: Get control panel display setting on python


code:
X = 42               # Create new object 42, bind name X to it.
L = []
L.append(X)          # Bind L[0] to the 42 object.
X = 666              # Create new object 666, bind name X to it.
print(L)             # Will not see the 666.

rawDescription: Python; name bindings are not object references?


code:
a = ['hello','how','are','hello','you']
b = ['hello','how','you','today']

cnt_a = {}
for w in a:
    cnt_a[w] = cnt_a.get(w, 0) + 1

for w in b:
    if w in cnt_a:
        cnt_a[w] -= 1
        if cnt_a[w] == 0:
            del cnt_a[w]

[y for k, v in cnt_a.items() for y in [k] * v]

rawDescription: Python, finding unique words in multiple lists


code:
lst = []
for dontCareAboutTheName in "your string goes here":
    lst.append(1)
print(sum(lst))

rawDescription: Trying to understand a Python line code

description: Trying to understand a Python line code


code:
def find_K_highest(lst, k):
    # escape index error
    if k>len(lst):
        k=len(lst)
    # the output array
    idxs = [None]*k
    to_watch = range(len(lst))
    # do it k times
    for i in range(k):
        # guess that max value is at least at idx '0' of to_watch
        to_del=0
        idx = to_watch[to_del]
        max_val = lst[idx]
        # search through the list for bigger value and its index
        for jj in range(len(to_watch)):
            j=to_watch[jj]
            val = lst[j]
            # check that its bigger that previously finded max
            if val > max_val:
                idx = j
                max_val = val
                to_del=jj
            # append it
        idxs[i] = idx
        del to_watch[to_del]
        # return answer
    return idxs

rawDescription: Getting the indices of the X largest numbers in a list


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

data = np.array([(1,1),(2,9),(2,3),(2,1),(2,2)])
letters = ['A', 'B', 'H', 'A', 'H']
colormap = {'A':(1,0,0),'B':(0,0,1),'H':(0,1,0)}

N = data.max() + 5
# color the background white (1 is white)
arr = np.ones((N,N,3))

for (x,y), letter in zip(data,letters):
    # color the point at (x,y) black
    arr[x,y] = (0,0,0)
    # color the x=0 band
    arr[0,y] =  colormap[letter]
    # color the y=N-1 band
    arr[x,N-1] =  colormap[letter]

arr = arr.swapaxes(0,1)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)

ax.imshow(arr, interpolation='nearest')
ax.invert_yaxis()
# ax.axis('off')
plt.show()

rawDescription: numbers that correspond to a row and column on a matrix

description: Numbers that correspond to a row and column on a matrix


code:
import multiprocessing
from multiprocessing import Queue, Process
from Queue import Empty as QueueEmpty

# Assuming this text is very very very very large
text="Here I am writing some nonsense\nBut people will read\n..."

def read(q):
   """Read the text and put in a queue"""
   for line in text.split("\n"):
       q.put(line)

def work(qi, qo):
   """Put the line into the queue out"""
   while True:
        try:
            data = qi.get(timeout = 1) # Timeout after 1 second
            qo.put(data)
        except QueueEmpty:
            return # Exit when all work is done
        except:
            raise # Raise all other errors

def join(q):
    """Join all the output queue and write to a text file"""
    f = open("file.txt", w)
    while True:
         try:
            f.write(q.get(timeout=1))
         except QueueEmpty:
            f.close()
            return
         except:
            raise

def main():
   # Input queue
   qi = Queue()
   # Output queue
   qo = Queue()
   # Start the producer
   Process(target = read, args = (qi, )).start()
   # Start 8 consumers
   for i in range(8):
        Process(target = work, args = (qi, qo, )).start()
   # Final process to handle the queue out
   Process(target = join, args = (qo, )).start()

rawDescription: Python - multithreading / multiprocessing


code:
import bokeh
from bokeh.charts import Scatter, output_file, show
from bokeh.models import HoverTool

p = Scatter(df, x='colA', y='colB', title="Foo", color="navy",
            xlabel="A", ylabel="B", tools="hover")

hover = p.select(dict(type=HoverTool))
hover.tooltips = [("C", "@colC")]
hover.mode = 'mouse'
output_file("scatter.html")
show(p)

rawDescription: Using a specific column to populate tooltips in Bokeh