code:
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randint(0,100,size=(10, 3)), columns=list('ABC'))
sdf = sqlContext.createDataFrame(df)
sdf.show()
sdf.printSchema()

rawDescription: Pandas dataframe in pyspark to hive

description: Pandas dataframe in pyspark to hive


code:
validation = {'studio': studio, 'movie': movie}
validation_errors = []
for key in validation:
    if not validation[key]:
        validation_errors.append(key)

rawDescription: How to get the string literal of a variable

description: Get the string literal of a variable


code:
from jinja2.ext import Extension

def visible_only(value):
    return [v for v in value if not getattr(v, "visible", False)]

class MyExtension(Extension):
    def __init__(self, environment):
        environment.filters["visible_only"] = visible_only

rawDescription: How do I create a jinja2 extension that registers a filter

description: Create a jinja2 extension that registers a filter


code: map(lambda x: '&%d;' % ord(x),string)

rawDescription: SOLVED: Python3 Convert all characters to HTML Entities


code:
import re
splitter = re.compile(r'(\s+|\S+)')
splitter.findall(s)

rawDescription: tokenize a string keeping delimiters in Python

description: Tokenize a string keeping delimiters in Python


code: directory = u'C:/path/to/root'

rawDescription: os.walk doesn't work correctly with unicode characters in path

description: Os.walk doesn't work correctly with unicode characters in path


code:
def formatmonth(self, year, month):
    self.year, self.month = year, month
    return super(EventsCalendar, self).formatmonth(year, month)

rawDescription: Creating links to django views using HTMLCalendar


rawDescription: How do you split a single-worded string into two?

description: Split a single-worded string into two


code: mcid = raw_input("MC ID (CTRL-D = done, 0 = sets, ? = lookup): ")

rawDescription: Backspace with `raw_input` in Python

description: Backspace with `raw_input` in Python


code:
table_info = []
conn = connections[dbname]
cursor = conn.cursor()
tables = conn.introspection.table_names(cursor)
found_models = conn.introspection.installed_models(tables)
for model in found_models:
    tablemeta = model._meta

rawDescription: Django: How do I perform database introspection with multiple databases?


code:
newSample = line.replace('better', s)
if newSample.lower() != oldSample.lower():
    file2.write(newSample)
oldSample = newSample

rawDescription: How to remove same adjacent line/lines in a file using python?

description: Remove same adjacent line/lines in a file using python


code:
import csv
with open("ABC.txt", "rb") as csvfile:
    reader = csv.reader(csvfile, delimiter="\t")
    data = list(reader)
minimum = data[2][17]

rawDescription: Python text file

description: Python text file


code:
import time

limit = 5
starttime = time.time()
for i, item in enumerate(recvData):
    if not i + 1 % limit:
        sleeptime =starttime + 60 - time.time()
        if sleeptime > 0:
            time.sleep(sleeptime)
        starttime = time.time()
    #processing code

rawDescription: Limit loop to N iterations per minute

description: Limit loop to N iterations per minute


code:
BigList = []
for filename in glob.glob(deferredDir + '/*'):
    PartList = csv.reader(open(filename))
    BigList.extend(PartList)

rawDescription: Appending lists from files to a single list in Python


token_type: Bearer

rawDescription: Google AdWords API access w/ grant_type password


rawDescription: how to define an array in python?

description: Define an array in python


code:
import re
regex = r"[a-z]+"
matches = re.findall(regex, strToScan)

rawDescription: Equivalent of Javascript "match" in python


code:
from allauth.account.signals import user_logged_in

@receiver(user_logged_in)
def login_fb_user(sender, request, user, **kwargs):
    socialuser = SocialAccount.objects.filter(user=user, provider="facebook")[0]
    # here login success

rawDescription: How can I run some code after success register, allauth, Django

description: Run some code after success register, allauth, Django


code:
enumerated = [range(start, end) for start,end in zip(breaks, breaks[1:])] + [[breaks[-1]]]
enumerated

rawDescription: Fully enumerate range from list of breakpoints


code:
def unanimous(it):
  it1, it2 = itertools.tee(it)
  return all(it1) or not any(it2)

rawDescription: Pythonic way to check if: all elements evaluate to False -OR- all elements evaluate to True


code: from nose.tools import assert_in, assert_is

rawDescription: python-nose: assertion library?

description: Python-nose: assertion library


code:
import pandas as pd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot as plt

def Profile(x,y,nbins,xmin,xmax,ax):
    df = DataFrame({'x' : x , 'y' : y})

    binedges = xmin + ((xmax-xmin)/nbins) * np.arange(nbins+1)
    df['bin'] = np.digitize(df['x'],binedges)

    bincenters = xmin + ((xmax-xmin)/nbins)*np.arange(nbins) + ((xmax-xmin)/(2*nbins))
    ProfileFrame = DataFrame({'bincenters' : bincenters, 'N' : df['bin'].value_counts(sort=False)},index=range(1,nbins+1))

    bins = ProfileFrame.index.values
    for bin in bins:
        ProfileFrame.ix[bin,'ymean'] = df.ix[df['bin']==bin,'y'].mean()
        ProfileFrame.ix[bin,'yStandDev'] = df.ix[df['bin']==bin,'y'].std()
        ProfileFrame.ix[bin,'yMeanError'] = ProfileFrame.ix[bin,'yStandDev'] / np.sqrt(ProfileFrame.ix[bin,'N'])

    ax.errorbar(ProfileFrame['bincenters'], ProfileFrame['ymean'], yerr=ProfileFrame['yMeanError'], xerr=(xmax-xmin)/(2*nbins), fmt=None)
    return ax

def Profile_Matrix(frame):
  #Much of this is stolen from https://github.com/pydata/pandas/blob/master/pandas/tools/plotting.py

    import pandas.core.common as com
    import pandas.tools.plotting as plots
    from pandas.compat import lrange
    from matplotlib.artist import setp

    range_padding=0.05

    df = frame._get_numeric_data()
    n = df.columns.size

    fig, axes = plots._subplots(nrows=n, ncols=n, squeeze=False)

    # no gaps between subplots
    fig.subplots_adjust(wspace=0, hspace=0)

    mask = com.notnull(df)

    boundaries_list = []
    for a in df.columns:
        values = df[a].values[mask[a].values]
        rmin_, rmax_ = np.min(values), np.max(values)
        rdelta_ext = (rmax_ - rmin_) * range_padding / 2.
        boundaries_list.append((rmin_ - rdelta_ext, rmax_+ rdelta_ext))

    for i, a in zip(lrange(n), df.columns):
        for j, b in zip(lrange(n), df.columns):

            common = (mask[a] & mask[b]).values
            nbins = 100
            (xmin,xmax) = boundaries_list[i]

            ax = axes[i, j]
            Profile(df[a][common],df[b][common],nbins,xmin,xmax,ax)

            ax.set_xlabel('')
            ax.set_ylabel('')

            plots._label_axis(ax, kind='x', label=b, position='bottom', rotate=True)
            plots._label_axis(ax, kind='y', label=a, position='left')

            if j!= 0:
                ax.yaxis.set_visible(False)
            if i != n-1:
                ax.xaxis.set_visible(False)

    for ax in axes.flat:
        setp(ax.get_xticklabels(), fontsize=8)
        setp(ax.get_yticklabels(), fontsize=8)

    return axes

rawDescription: Plotting profile hitstograms in python

description: Plotting profile hitstograms in python


code: alist.append(re.sub(r"\b(\w+[aeiouy]\w{3,})\s+(\w+)\b", r"\2 \1", line.upper(),flags=re.I))

rawDescription: Manipulation on files with python

description: Manipulation on files with python


code:
import wx
class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.quote = wx.StaticText(self.panel, label="Your quote:")
        self.result = wx.StaticText(self.panel, label="")
        self.result.SetForegroundColour(wx.RED)
        self.button = wx.Button(self.panel, label="Save")
        self.lblname = wx.StaticText(self.panel, label="Your name:")
        self.editname = wx.TextCtrl(self.panel, size=(140, -1))

        # Set sizer for the frame, so we can change frame size to match widgets
        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)

        # Set sizer for the panel content
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.quote, (0, 0))
        self.sizer.Add(self.result, (0, 1))
        self.sizer.Add(self.lblname, (1, 0))
        self.sizer.Add(self.editname, (1, 1))
        self.sizer.Add(self.button, (2, 0), (1, 2), flag=wx.EXPAND)

        # Set simple sizer for a nice border
        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

        # Use the sizers
        self.panel.SetSizerAndFit(self.border)
        self.SetSizerAndFit(self.windowSizer)

        # Set event handlers
        self.button.Bind(wx.EVT_BUTTON, self.OnButton)

    def OnButton(self, e):
        self.result.SetLabel(self.editname.GetValue())

app = wx.App(False)
frame = ExampleFrame(None)
frame.Show()
app.MainLoop()

rawDescription: Simple example of using wx.TextCtrl and display data after button click in wxpython - new to wx


code:
from django.forms import formset_factory
from django.shortcuts import render
from myapp.forms import ArticleForm, BookForm

def manage_articles(request):
    ArticleFormSet = formset_factory(ArticleForm)
    BookFormSet = formset_factory(BookForm)
    if request.method == 'POST':
        article_formset = ArticleFormSet(request.POST, request.FILES, prefix='articles')
        book_formset = BookFormSet(request.POST, request.FILES, prefix='books')
        if article_formset.is_valid() and book_formset.is_valid():
            # do something with the cleaned_data on the formsets.
            pass
    else:
        article_formset = ArticleFormSet(prefix='articles')
        book_formset = BookFormSet(prefix='books')
    return render(request, 'manage_articles.html', {
        'article_formset': article_formset,
        'book_formset': book_formset,
    })

rawDescription: Django formset without modelform not working


code:
import glob, gzip, re
import cPickle

logformat = re.compile(r'^\S+ \S+ \S+ \[([\w:/]+\s[+\-]\d{4})\] "(\S+) (\S+) .*" (\d+) (\d+) "([^"]*)" "[^"]*"')

with open('Logs.txt', 'w') as f_out:
    for i in glob.glob('*.gz'):
        with gzip.GzipFile(i,'r') as f_in:
            for txtline in f_in:
                parsedline = logformat.match(txtline)
                if parsedline:
                    f_out.write("time={t} size={s} url={u}".format(t=parsedline.group(1), s=parsedline.group(5), u=parsedline.group(3)))

rawDescription: Concat all the text files and their content contained in all the .zip files in a text file using Python


code:
if 3 in range(5):
    print('ok')
else:
    print('no')

rawDescription: Can I use an if statement to check if a number is within a range?

description: Use an if statement to check if a number is within a range


code:
with open(datafile) as f:
    d = {}
    for line in f:
        key,value = line.split(None,1)
        d[key] = ast.literal_eval(value)

rawDescription: How to read file into dictionary in python

description: Read file into dictionary in python


code:
import math
import pygame

pygame.init()
screen = pygame.display.set_mode((400,400))

while True:
    t = pygame.time.get_ticks() / 2  % 400 # scale and loop time
    x = t
    y = math.sin(t/50.0) * 100 + 200       # scale sine wave
    y = int(y)                             # needs to be int

    screen.fill((0,0,0))
    pygame.draw.circle(screen, (255,255,255), (x, y), 40)

    pygame.display.flip()

rawDescription: Python:Pygame: How to animate a ball to move in a sin() like pattern?


code:
def greet(name):
    def say_hi():
        print('Preparing to greet...')
        print('Hi', name, '!')
        print('Greeting given.')
    return say_hi

import dis

dis.dis(greet)

rawDescription: Higher order functions in Python

description: Higher order functions in Python


code:
fig, axes = plt.subplots(2, 2, figsize=(8,6), sharex=True)

for ax, plot in zip(axes.flat, plots):
    ax.plot(df.index, df[plot])
    ma = pd.rolling_mean(df[plot], window)
    ax.plot(df.index, ma)
    mstd = pd.rolling_std(df[plot], window)
    ax.fill_between(df.index, ma - 2*mstd, ma + 2*mstd, color='b', alpha=0.1)
    ax.set_title(plot)
    ax.yaxis.set_major_formatter(FuncFormatter(lambda x, p: '{:.0f}'.format(x)))
    ax.xaxis.set_major_formatter(DateFormatter('%d-%H:%M')) # or '%d.%m.%y'

fig.autofmt_xdate()  # This will rotate the xticklabels by 30 degrees so that all dates are readable.
fig.tight_layout()  # no need to call this inside the loop.

rawDescription: Matplotlib apply xaxis and yaxis number formatting


code:
def totalupdate(*args):
    global para
    try:
        para[0] = first_var.get()
        para[1] = second_var.get()
    except ValueError:
        # one or both values are not integers
        return
    newtotal = sum(para)
    total_var.set(newtotal)

rawDescription: Tkinter Trace and ValueError: invalid literal for int() with base 10: ''


rawDescription: python dataframe, get list of start and end of events

description: Python dataframe, get list of start and end of events


code:
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
xx = np.arange(0,5, .5)
yy = np.random.random( len(xx) )
plt.plot(xx,yy)
plt.gca().xaxis.set_major_locator(MaxNLocator(prune='lower'))
plt.show()

rawDescription: Overlapping y-axis tick label and x-axis tick label in matplotlib


code:
# set a value
if hasattr(request, 'session'):
    request.session['account_id'] = account_id

# read a value
if hasattr(request, 'session'):
    account_id = request.session['account_id']

rawDescription: Getting URL values in Django

description: Getting URL values in Django


code:
import fcntl
new_entry = "foobar"
with open("/somepath/somefile.txt", "a") as g:
    fcntl.flock(g, fcntl.LOCK_EX)
    g.write(new_entry)
    fcntl.flock(g, fcntl.LOCK_UN)

rawDescription: Python Multiple users append to the same file at the same time


code:
import timeit
s1 = "d = dict((str(n), 0) for n in range(1000000))"
timeit.repeat("d['a'] = d.get('a', 0) + 1", setup=s1)
s2 = """
from collections import defaultdict
d = defaultdict(int, ((str(n), 0) for n in range(1000000)))
"""
timeit.repeat("d['a'] += 1", setup=s2)

rawDescription: Avoid double lookup when updating dictionary integer members


code:
import logging
from systemd.journal import JournalHandler

log = logging.getLogger('demo')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
log.info("sent to journal")

rawDescription: How to log to journald (systemd) via Python?

description: Log to journald (systemd) via Python


code:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.factory import Factory

class MyWidget(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        button = Button(text="Print IDs", id="PrintIDsButton")
        button.bind(on_release=self.print_label)
        self.add_widget(button)

        # crate some labels with defined IDs
        for i in range(5):
            self.add_widget(Button(text=str(i), id="button no: " + str(i)))

        # update moved as per inclement's recommendation
        Clock.schedule_interval(self.update, 1 / 5.0)

    def print_label(self, *args):
        children = self.children[:]
        while children:
            child = children.pop()
            print("{} -> {}".format(child, child.id))
            # Add smiley by ID
            children.extend(child.children)
            if child.id == "PrintIDsButton":
                child.text = child.text + " :)"

    def update(self, *args):
        children = self.children[:]
        while children:
            child = children.pop()
            # remove smiley by ID
            if child.id == "PrintIDsButton":
                child.text  = "Print IDs"

class MyApp(App):
    def build(self):
        return MyWidget()

if __name__ == '__main__':
    MyApp().run()

rawDescription: Kivy: Manipulating dynamically created widgets in update function


code:
for i in read:
    # the string.replace() function don't do the change at place
    # it's return a new string with the new changes.
    a = i.replace(" - ",",")
    output.write(a)

rawDescription: Python: Replace function to edit files

description: Python: Replace function to edit files


code:
import sys
from PyQt4 import QtGui, QtCore

class TestSortFilterProxyModel(QtGui.QSortFilterProxyModel):
    def __init__(self, parent=None):
        super(TestSortFilterProxyModel, self).__init__(parent)
        self.filter = ['folder0/file0', 'folder1/file1'];

    def filterAcceptsRow(self, source_row, source_parent):
        index0 = self.sourceModel().index(source_row, 0, source_parent)
        filePath = self.sourceModel().filePath(index0)

        for folder in self.filter:
            if filePath.startsWith(folder) or QtCore.QString(folder).startsWith(filePath):
                return True;
        return False

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

        model = QtGui.QFileSystemModel(self)
        model.setRootPath(QtCore.QDir.currentPath())

        proxy = TestSortFilterProxyModel(self)
        proxy.setSourceModel(model)

        self.view = QtGui.QTreeView()
        self.view.setModel(proxy)

        self.setCentralWidget(self.view)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: PYQT files paths to tree

description: PYQT files paths to tree


code:
import subprocess

cmd = subprocess.Popen(['cd', '/home/time/tmprcc;', 'ino', 'build;',
                        'ino', 'upload;'], stdout=subprocess.PIPE)

while cmd.poll() is None:
    new_output = cmd.stdout.readline()
    do_something_function(new_output)

rawDescription: Sending output of os.system to web app

description: Sending output of os.system to web app


code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16

A = [[1, 3, 4, 5, 6, 7],
     [3, 3, 0, 7, 9, 2],
     [1, 3, 4, 5, 6, 6]]
X = ["A", "B", "C", "E", "F", "G"]
Y = ["R", "S", "T"]

m = len(Y)
n = len(X)

plt.figure(figsize=(n + 1, m + 1))
for krow, row in enumerate(A):
    plt.text(5, 10*krow + 15, Y[krow],
             horizontalalignment='center',
             verticalalignment='center')
    for kcol, num in enumerate(row):
        if krow == 0:
            plt.text(10*kcol + 15, 5, X[kcol],
                     horizontalalignment='center',
                     verticalalignment='center')
        plt.text(10*kcol + 15, 10*krow + 15, num,
                 horizontalalignment='center',
                 verticalalignment='center')

plt.axis([0, 10*(n + 1), 10*(m + 1), 0])
plt.xticks(np.linspace(0, 10*(n + 1), n + 2), [])
plt.yticks(np.linspace(0, 10*(m + 1), m + 2), [])
plt.grid(linestyle="solid")
plt.savefig("Table.png", dpi=300)
plt.show()

rawDescription: Drawing matrix plot in Python?

description: Drawing matrix plot in Python


code: dict(x[i:i+2] for i in range(0, len(x), 2))

rawDescription: Make dictionary from list with python

description: Make dictionary from list with python


code:
import Tkinter as tk
import tkFont

class App:
    def __init__(self):
        self.root = tk.Tk()
        self.count = 0
        l = tk.Label(text="Hello, world")
        l.pack()
        # clone the font, set the underline attribute,
        # and assign it to our widget
        f = tkFont.Font(l, l.cget("font"))
        f.configure(underline = True)
        l.configure(font=f)
        self.root.mainloop()

if __name__ == "__main__":
    app=App()

rawDescription: Underline Text in Tkinter Label widget?

description: Underline Text in Tkinter Label widget


code: sns.distplot(iris.petal_length, axlabel = 'Petal Length', hist_kws=dict(alpha=1))

rawDescription: How do I remove transparency from a histogram created using Seaborn in python?

description: Remove transparency from a histogram created using Seaborn in python


rawDescription: Python:How do I use "or" in a regular expression?


rawDescription: How to exchange values between specific keys in a dictionary?

description: Exchange values between specific keys in a dictionary


code:
import itertools

password = 'Dude123'
charList = ['d8','vV','','D8','','','']

finalString = [''.join(set((a, b))) for a, b in zip(password, charList)]

possibles = list(''.join(poss) for poss in itertools.product(*finalString))
possibles

rawDescription: How to print password combination (but with custom constraints for each index)

description: Print password combination (but with custom constraints for each index)


code:
listbox.bind( "<Double-Button-1>" , internet )

def internet():
    weblink = lb2.get(ACTIVE)
    webbrowser.open(weblink)

rawDescription: How to open a weblink by double clicking it in a tkinter list box?

description: Open a weblink by double clicking it in a tkinter list box


code:
list_string = '[[1, 1], [2, 2], [3, 3]]'
import ast
type(ast.literal_eval(list_string))

rawDescription: convert a string 2d list back to 2d list in python

description: Convert a string 2d list back to 2d list in python


code:
import csv

with open('data.txt', 'rb') as match_data:
    reader = csv.reader(match_data)
    match_data = {tuple(row[:2]): row for row in reader}

with open('m_list.txt', 'rb') as match_list, open('done.txt', 'wb') as outfile:
    reader = csv.reader(match_list)
    writer = csv.writer(outfile)

    for row in reader:
        row = tuple(row)
        if row in match_data:
            writer.writerow(match_data[row])

rawDescription: Matches lines from two separate files

description: Matches lines from two separate files


rawDescription: Change directory to the directory of a Python script


rawDescription: SQLAlchemy alphabetical ordering query

description: SQLAlchemy alphabetical ordering query


code:
import datetime
d = datetime.datetime(2010, 12, 25, 18, 25)
d + datetime.timedelta(hours = 8)

rawDescription: Changing the hour with datetime.replace() in python


rawDescription: square root of sum of square of columns in multidimensional array

description: Square root of sum of square of columns in multidimensional array


code:
vendor = get_vendor(hostname)
vendor_path = 'platform_%s.stats' % vendor
exec("import " + vendor_path)
stats = vendor_path.stats(constructor_args)

rawDescription: Load module with variable path in python 2.6?


code:
firstList = [[0, 9], [0, 4], [0]]
secondList = [[18], [19, 7], [20]]
all_results = []

for x in range(0, len(firstList)):
    values_of_first = firstList[x]
    values_of_second = secondList[x]
    values_of_first.sort()
    values_of_second.sort()
    tmp_results = []

    for subtractor in values_of_second:
        used_values = []
        for subtracted in values_of_first:
            if subtracted >= subtractor:
                break
            else:
                used_values.append(subtracted)
                tmp_results.append(subtractor - subtracted)

        for used_value in used_values:
            values_of_first.remove(used_value)

    tmp_results.sort()
    all_results.append(tmp_results)

rawDescription: subtract list of lists from list of lists with conditional arguments

description: Subtract list of lists from list of lists with conditional arguments


code:
class ToLoad(object):
    def __init__(self, var, func):
        self.var  = var
        self.func = func

    # style note: try to avoid overshadowing built-ins (e.g. type)
    def __get__(self, obj, cls):
        try:
            return getattr(obj, self.var)
        except AttributeError:
            value = getattr(obj, self.func)()
            setattr(obj, self.var, value)
            return value

class Foo(object):
    x = ToLoad('x', '_load_x')

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

    def _load_x(self):
        print('Loading {0} into x'.format(self.y))
        return self.y

a = Foo(1)
b = Foo(2)
print(a.x)
print(b.x)
print(a.x)

rawDescription: Descriptor for lazy loading

description: Descriptor for lazy loading


code:
def passed(item):
    try:
       return int(item) > 28
    except ValueError:
       return False
result = [filter(passed, item) for item in a]

rawDescription: Remove items from a list in Python using conditions


code:
from django.forms.models import model_to_dict
from django.contrib.auth.models import Group
g = Group.objects.filter()[0]
d = model_to_dict(g)
d

rawDescription: django: documented way to get an object's fields? / Meta documentation?

description: Django: documented way to get an object's fields? / Meta documentation


code:
from urlparse import urljoin

src = driver.find_element_by_name("iframe_name").get_attribute("src")
url = urljoin(base_url, src)

driver.get(url)
print(driver.page_source)

rawDescription: Get Iframe Src content using Selenium Python


code:
out = file("out.jpg", "wb")
out.write(file("someimage.jpg", "rb").read())
out.write(file("somehiddenfile.pdf", "rb").read())
out.close()

rawDescription: How do I hide a file inside an image with Python?

description: Hide a file inside an image with Python


code:
client = pymongo.MongoClient("localhost", 27017)
db = client.test
table = db.test
date = '20140501'
update = { "$set": {} }
update['$set']['value.' + date] = 23423423
table.find_one_and_update({'id':2}, update)

rawDescription: How to use variable in mongodb with python

description: Use variable in mongodb with python


code:
import codecs
InFile = codecs.open(path_to_registry, encoding='utf-16')

rawDescription: Python Binary? to Text/String

description: Python Binary? to Text/String


rawDescription: convert dpkt byte strings containing random characters

description: Convert dpkt byte strings containing random characters


code:
def response(context, flow):
    if flow.request.url == "...": # optionally filter based on some criteria...
        with decoded(flow.response):  # automatically decode gzipped responses.
            data = json.loads(flow.response.content)
            data["foo"] = "bar"
            flow.response.content = json.dumps(data)

rawDescription: Mitmproxy tampering GET and POST request/response in one script


code:
try:
    with open(filename, 'r+') as f:
        data = f.read()
        f.seek(0,0)
        f.write(header)
        f.write(data)
        #f.truncate() is not needed here as the file will always grow
    print("Header added to", filename)
except IOError:
    print("Sorry, could not open file for reading/writing")

rawDescription: Check to see if file exists is failing

description: Check to see if file exists is failing


code:
id_s = {c: i for i, c in enumerate(set(list))}
li = [id_s[c] for c in list]

rawDescription: Replacing python list elements with key

description: Replacing python list elements with key


code:
from wireless import Wireless
wireless = Wireless()
wireless.connect(ssid='ssid', password='password')

rawDescription: Python Wlan Manager

description: Python Wlan Manager


code:
for widget in myVBox.get_children():
    myVBox.remove(widget)

rawDescription: pygtk - dynamically update the widgets taking input from the gtk combo box

description: Pygtk - dynamically update the widgets taking input from the gtk combo box


code:
import os
import re
import shutil

def toInt(str):
  try:
    return int(str)
  except:
    return 0

filePath = "./"
extension = "png"

dirList = os.listdir(filePath)

regx = re.compile("[0-9]+a")

for filename in dirList:
  ext = filename[-len(extension):]
  if(ext != extension): continue
  rslts = regx.search(filename)
  if(rslts == None): continue
  pieces = regx.split(filename)
  if(len(pieces) < 2): pieces.append("")
  filenumber = toInt(rslts.group(0).rstrip("a"))
  newFileNum = (filenumber + 1) / 2
  fileChar = "b"
  if(filenumber % 2): fileChar = "a"
  newFileName = "%s%03d%s%s" % (pieces[0], newFileNum, fileChar, pieces[1])
  shutil.move("%s%s" % (filePath, filename), "%s%s" % (filePath, newFileName))

rawDescription: Automator/Applescript rename files if

description: Automator/Applescript rename files if


code:
def setUp(self):
    self.test_user = UserModel.objects.create_user("test_user", "test@user.com", "123456")

    self.application = Application(
        name="Test Application",
        redirect_uris="http://localhost",
        user=self.test_user,
        client_type=Application.CLIENT_CONFIDENTIAL,
        authorization_grant_type=Application.GRANT_AUTHORIZATION_CODE,
    )
    self.application.save()

def test_revoke_access_token(self):
    from datetime import datetime
    from django.utils import timezone

    tok = AccessToken.objects.create(
        user=self.test_user, token='1234567890',
        application=self.application, scope='read write',
        expires=timezone.now() + datetime.timedelta(days=1)
    )

rawDescription: How to test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication

description: Test an API endpoint with Django-rest-framework using Django-oauth-toolkit for authentication


code:
def _migrate_users(curs=None):
  users, next_curs, more = User.query().fetch_page(50, start_cursor=curs)
  for user in users:
    user.field1 = 'bla bla'
  ndb.put_multi(users)
  if more:
    deferred.defer(_migrate_users, next_curs, _queue='default')

rawDescription: Update for big count of NDB Entities fails


code:
class Test(webapp.RequestHandler):
    def err_user_not_found(self):
        self.redirect(users.create_login_url(self.request.uri))
    def get(self):
        user = users.get_current_user()
        # error path
        if not user:
            self.err_user_not_found()
            return

        # happy path
        greeting = ('Hello, ' + user.nickname())
        ...
        template_values = {"greeting": greeting,}

rawDescription: UnboundLocalError: local variable ... referenced before assignment


code:
at_most=2
df.apply(lambda x: (x[np.isfinite(x)][:at_most]).sum(), axis=1)

rawDescription: Pandas: Sum of first N non-missing values per row


code:
from openpyxl import Workbook
wb = Workbook(optimized_write = True)

ws = wb.create_sheet()

# now we'll fill it with 10k rows x 200 columns
for irow in xrange(10000):
    ws.append(['%d' % i for i in xrange(200)])

wb.save('new_big_file.xlsx') # don't forget to save!

rawDescription: Openpyxl hangs on save of large file

description: Openpyxl hangs on save of large file


code:
layers = [
    WMSLayer(wms=wmsInstance, layer_name=str(layer))
    for layer in wms.contents
    if {'EPSG:900913', 'EPSG:3857'}.intersection(wms[layer].crsOptions)
]

rawDescription: Use of set() and list comprehension

description: Use of set() and list comprehension


code:
themap = {C3: C3.parse}
for C in (str, C1, C2):
    themap[C] = C

def f(cls, value):
    wot = themap.get(cls)
    if wot is None:
        raise UnknownClass(repr(cls))
    return wot(value)

rawDescription: object factory that generates an object or list of objects

description: Object factory that generates an object or list of objects


rawDescription: While True Algorithm iteration Python

description: While True Algorithm iteration Python


code:
import socket,os
from threading import Thread

class socketListener(Thread):
    def run(self):
        soc = socket.socket(socket.AF_INET)
        soc.bind(('localhost',8000))
        soc.listen(0)
        client = soc.accept()

pid = os.getpid()
sl = socketListener()
sl.start()
input('Socket is listening, press any key to abort...')
os.kill(pid,9)

rawDescription: Python socket accept in the main thread prevents quitting


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

class Handle(webapp2.RequestHandler):
    def dispatch(self):
        self.session_store = sessions.get_store(request=self.request)
        try:
            webapp2.RequestHandler.dispatch(self)
        finally:
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
        return self.session_store.get_session()

class ValTest(Handle):
    def get(self):
        user = users.get_current_user()
        val = self.session.get("1")
        val += 1
        self.session["1"] = val
        self.response.write('value is: %i' % val)
        self.response.write('<form method="get" action="/valTst"><input type="submit" name="btn2" value="Incr"></form>')

class MainHandler(Handle):
    def get(self):
        self.response.write('Hello world!')
        user = users.get_current_user()
        self.session["1"] = 10
        self.response.write('<form method="get" action="/valTst"><input type="submit" name="btn" value="show"></form>')

### Configuration ###
config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'my-super-secret-key',
}

app = webapp2.WSGIApplication([
    ('/', MainHandler),
    ('/valTst', ValTest)
], debug=True, config=config)

rawDescription: Python Session variable

description: Python Session variable


rawDescription: Python is vs ==

description: Python is vs ==


code:
input_file="input_{0}.in".format(case)

os.system=("./a.out {0}".format(input_file))

rawDescription: running a script with an input file in python

description: Running a script with an input file in python


code:
import tempfile
with tempfile.NamedTemporaryFile() as t1, tempfile.NamedTemporaryFile() as t2, open('dasd.ini', 'w') as fyle:
    config = ConfigObj()
    config.filename = t1.file.name
    config['hey'] = "value1"
    config['test'] = "value2"
    config['another']['them'] = "value4"
    config.write()
    do_your_thing_with_(t2)
    t1.seek(0)
    t2.seek(0)
    fyle.write(t2.read())
    fyle.write(t1.read())

rawDescription: Writing to an existing file without overwriting/erasing


code:
import unittest
import mock  # possibly "from unittest import mock" depending on version.
import main_module

class TestMain(unittest.TestCase):
    def test_main(self):
        with mock.patch.object(main_module, 'print') as mock_print:
            mock_module.main()
        expected_calls = [mock.call('Argh!') for _ in range(100)]
        mock_print.assert_has_calls(expected_calls)

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

rawDescription: Unit Testing with Range Function

description: Unit Testing with Range Function


code:
import os

for dirpath, dirsInDirpath, filesInDirPath in os.walk("yourFolderContainingTxtFiles"):
    for myfile in filesInDirPath:
        sqlQuery = "LOAD DATA INFILE %s INTO TABLE xxxx (col1,col2,...);" % os.path.join(dirpath, myfile)
        # execute the query here using your mysql connector.
        # I used string formatting to build the query, but you should use the safe placeholders provided by the mysql api instead of %s, to protect against SQL injections

rawDescription: load multiple txt files into mysql

description: Load multiple txt files into mysql


code:
from nltk import ne_chunk, pos_tag, word_tokenize
from nltk.tree import Tree

def get_continuous_chunks(text):
    chunked = ne_chunk(pos_tag(word_tokenize(text)))
    prev = None
    continuous_chunk = []
    current_chunk = []
    for i in chunked:
            if type(i) == Tree:
                    current_chunk.append(" ".join([token for token, pos in i.leaves()]))
            elif current_chunk:
                    named_entity = " ".join(current_chunk)
                    if named_entity not in continuous_chunk:
                            continuous_chunk.append(named_entity)
                            current_chunk = []
            else:
                    continue
    return continuous_chunk

my_sent = "WASHINGTON -- In the wake of a string of abuses by New York police officers in the 1990s, Loretta E. Lynch, the top federal prosecutor in Brooklyn, spoke forcefully about the pain of a broken trust that African-Americans felt and said the responsibility for repairing generations of miscommunication and mistrust fell to law enforcement."
get_continuous_chunks(my_sent)

rawDescription: NLTK Named Entity recognition to a Python list


code:
import csv
from itertools import chain, islice, repeat

COLS = 9 # or pre-scan file to get max columns
FILL_VALUE = 0 # or None, or blank for instance
with open(filename) as fin:
    csvin = csv.reader(fin) # use appropriate delimiter/dialect settings
    non_blanks = (row for row in csvin if row[0]) # filter out rows with blank 1st col
    matrix = [list(islice(chain(row, repeat(FILL_VALUE)), COLS)) for row in non_blanks]

rawDescription: Deleting Multiple Rows in a Matrix

description: Deleting Multiple Rows in a Matrix


code:
# On the POSIX compliant platforms, `utcfromtimestamp(timestamp)` is
# equivalent to the following expression:
datetime(1970, 1, 1) + timedelta(seconds=timestamp)

# There is no method to obtain the timestamp from a `datetime` instance,
# but POSIX timestamp corresponding to a `datetime` instance `dt` can be
# easily calculated as follows. For a naive `dt`:
timestamp = (dt - datetime(1970, 1, 1)) / timedelta(seconds=1)

# And for an aware ``dt``::
timestamp = (dt - datetime(1970, 1, 1, tzinfo=timezone.utc)) / timedelta(seconds=1)

rawDescription: Why does the python datetime class have a 'fromtimestamp' method, but not a 'totimestamp' method?


code:
import subprocess
subprocess.Popen('ulimit -t 60; nice -n 15 cpuhog', shell=True)

rawDescription: Python: ulimit and nice for subprocess.call / subprocess.Popen?


rawDescription: Python list is not getting sorted using the built-in sort funtion


code:
#!/usr/bin/env python3
Input_sentence = ['(telco_name_list.event_date','reference.date)',"'testwebsite.com'",'data_flow.code',"'/long/page/data.jsp'"]

comma = False
result = []
for i in range(len(Input_sentence)):
  e = Input_sentence[i]
  if e.startswith("'"): continue
  e = e.replace('(', '').replace(')', '')
  if comma:
    print(",", end="")
  comma = True
  print(e, end="")
print()

rawDescription: Python - exclude data with double quotes and paranthesis


code:
import numpy as np
a = np.ones((3,4,5))
b = np.ones((3,4,5))
c = np.concatenate((a[...,np.newaxis],b[...,np.newaxis]),axis=3)

rawDescription: Concatenate two numpy arrays in the 4th dimension


code:
from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open('C:\\Documents and Settings\\GradeBook.xls')
xl.Visible = True    # optional: if you want to see the spreadsheet

rawDescription: How can I launch an instance of an application using Python?

description: Launch an instance of an application using Python


code:
for n in range(3):
    num = int(input("Enter a number: "))
    if num > 0:
        print("YES")
    else:
        print("NO")

rawDescription: If-Else Statement-Positive and Negative Integers


code:
import os
output = [dI for dI in os.listdir('foo') if os.path.isdir(os.path.join('foo',dI))]

rawDescription: How to get list of subdirectories names

description: Get list of subdirectories names


code:
def blend (color1, color2, weight = 0.5):
    return gtk.gdk.Color (
        color1.red_float   * weight + color2.red_float   * (1 - weight),
        color1.green_float * weight + color2.green_float * (1 - weight),
        color1.blue_float  * weight + color2.blue_float  * (1 - weight))

for state in gtk.StateType.__enum_values__:
    label.modify_fg (state, blend (label.style.fg[state], label.style.bg[state]))

rawDescription: Setting opacity on a PyGTK label

description: Setting opacity on a PyGTK label


code:
db_connection = MySQLdb.connect(
        host = settings['dbHost'],
        user = settings['dbUser'],
        passwd = settings['dbPass'],
        db = settings['dbName'],
        client_flag = MySQLdb.constants.CLIENT.FOUND_ROWS
        )

rawDescription: MySQLdb Python Update Matched row versus Changed rows


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

from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork

cookieJar = QtNetwork.QNetworkCookieJar()

networkAccessManager = QtNetwork.QNetworkAccessManager()
networkAccessManager.setCookieJar(cookieJar)

class myWebView(QtWebKit.QWebView):
    _windows = set()

    def __init__(self, parent=None):
        super(myWebView, self).__init__(parent)
        self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
        self.settings().setAttribute(QtWebKit.QWebSettings.JavascriptCanOpenWindows, True)

        self.page().setNetworkAccessManager(networkAccessManager)

        self.load(QtCore.QUrl("http://www.example.com"))

    @classmethod
    def _removeWindow(cls, window):
        if window in cls._windows:
            cls._windows.remove(window)

    @classmethod
    def newWindow(cls):
        window = cls()
        cls._windows.add(window)
        return window

    def closeEvent(self, event):
        self._removeWindow(self)
        event.accept()

    def createWindow(self, webWindowType):
        window = self.newWindow()
        if webWindowType == QtWebKit.QWebPage.WebModalDialog:
            window.setWindowModality(QtCore.Qt.ApplicationModal)

        window.show()

        return window

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

        self.centralwidget = QtGui.QWidget(self)

        self.webView = myWebView(self.centralwidget)

        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setText("New Window")
        self.pushButton.clicked.connect(lambda: self.webView.createWindow(QtWebKit.QWebPage.WebBrowserWindow))

        self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
        self.verticalLayout.addWidget(self.pushButton)
        self.verticalLayout.addWidget(self.webView)

        self.setCentralWidget(self.centralwidget)

if __name__ == "__main__":
    import  sys

    app  = QtGui.QApplication(sys.argv)
    main = myWindow()
    main.show()
    sys.exit(app.exec_())

rawDescription: Segmentation fault when assigning cookie jar to QWebView


rawDescription: merger two dict in python like this

description: Merger two dict in python like this


code:
# Setup sqlalchemy bindings
import sqlalchemy as s
from sqlalchemy.orm import sessionmaker
engine = s.create_engine('postgresql://<user>:<password>@<host>:<port>/<db_name>')
# Automatically read the database tables and create metadata
meta = s.MetaData()
meta.reflect(bind=engine)
Session = sessionmaker(bind=engine)
# Create a session, which can query the tables
session = Session()

# Build table instances without hardcoding tablenames
s_payment = meta.tables[models.Payment()._meta.db_table]
s_allocation = meta.tables[models.Allocation()._meta.db_table]
s_customer = meta.tables[models.Customer()._meta.db_table]
s_invoice = meta.tables[models.Invoice()._meta.db_table]

report = session.query(s_payment.c.amount, ...).all()

rawDescription: Django query with joins

description: Django query with joins


code:
def has22(nums):
   for i in range (len(nums)-1):
    if nums[i+1]==2 and nums[i]==2:
     return True
   return False

rawDescription: Find if there are consecutive occurrences of an int in a list of ints


code:
#

class MyFrame(wx.Frame):
    """"""

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

        banner = MyBanner()

        # do a bunch of stuff

        banner.Destroy() # or banner.Close()

        self.Show()

rawDescription: wxPython: How do you start a wxPython application from a wxPython application?

description: WxPython: How do you start a wxPython application from a wxPython application


code:
mygraph = {"A" : {"B" : 2, "C" : 4}, "B" : {"A" : 2}, "C" : {"A" : 4}, "D":{}}
g = igraph.Graph(directed=False)
g.add_vertices(mygraph.keys())
edges = [(start, end) for start in mygraph.keys() for end in mygraph[start].keys()]
# or if you only want to have undirected links only once:
edges = [edge for edge in edges if edge[0] > edge[1]]
g.add_edges(edges)
igraph.plot(g)

rawDescription: Python igraph: Fastest way to convert large graph to python-igraph graph


code: names = [n.lower() if isinstance(n, basestring) else n for n in names]

rawDescription: Python one-liner to make strings lowercase and leave numbers as is


code:
import sys, os
sys.path = [os.path.abspath(os.path.dirname(__file__))] + sys.path

rawDescription: Python imports for tests using nose - what is best practice for imports of modules above current package


code:
import pandas as pd
df = pd.DataFrame([1, 2, 3, pd.np.nan], columns=['A'])
filter_list = pd.Series([1, pd.np.nan])
na_equiv = 'NA'
df['A'].replace(pd.np.nan, na_equiv).isin(filter_list.replace(pd.np.nan, na_equiv))

rawDescription: pandas checking for nan not working using .isin()

description: Pandas checking for nan not working using .isin()


code:
import wx

def vibrate(win, count=20, delay=50):
    if count == 0: return
    x, y = win.GetPositionTuple()
    dx = 2*count*(.5-count%2)
    win.SetPosition((x+dx,y))
    wx.CallLater(delay, vibrate, win, count-1, delay)

app = wx.PySimpleApp()
frame = wx.Frame(None, title="Vibrator")
frame.Show()
frame.Bind(wx.EVT_LEFT_DOWN, lambda e:wx.CallAfter(vibrate, frame))
app.SetTopWindow(frame)
app.MainLoop()

rawDescription: Vibrate window in wxPython

description: Vibrate window in wxPython


code:
s = "\N{BLACK SPADE SUIT}"
s
import unicodedata
unicodedata.name(s)
ord(s)
hex(ord(s))

rawDescription: How to find out number/name of unicode character in Python?

description: Find out number/name of unicode character in Python


code:
def crawl(url):
    d = getPage(url)
    d.addCallback(handlePage)

def handlePage(output):
    urls = getAllUrls(output)
    for url in urls:
        crawl(url)

crawl(url)
reactor.run()

rawDescription: Web crawler Using Twisted

description: Web crawler Using Twisted


code:
from plotly.offline import plot
import plotly.graph_objs as go
import webbrowser
import numpy as np
import pandas as pd

# generate your Plotly graph here

N = 500
y = np.linspace(0, 1, N)
x = np.random.randn(N)
df = pd.DataFrame({'x': x, 'y': y})
data = [go.Histogram(x=df['x'])]

# plot it for offline editing
HTMLlink = plot(data, show_link=False, auto_open=False)[7:] #remove the junk characters
# now need to open the HTML file
with open(HTMLlink, 'r') as file :
  tempHTML = file.read()
# Replace the target strings
tempHTML = tempHTML.replace('displaylogo:!0', 'displaylogo:!1')
tempHTML = tempHTML.replace('modeBarButtonsToRemove:[]', 'modeBarButtonsToRemove:["sendDataToCloud"]')
with open(HTMLlink, 'w') as file:
  file.write(tempHTML)
del tempHTML

webbrowser.open(HTMLlink)

rawDescription: Adding config modes to Plotly.Py offline - modebar


code:
import re
m = re.search('(?<=abc)def', 'abcdef')
m.group(0)

rawDescription: Is it possible to statically create a regular expression in Python?


code:
def generate_matrix(size):
    """Generate lists which form an eye matrix"""
    for i in range(size):
        # First, we create a new list with size 0s.
        l = [0] * size
        # At the specified position, we place a 1.
        l[i] = 1
        # We yield this new list.
        yield l

rawDescription: Creating lists with alternate 0 and 1 as generator in Python


code: X[:, np.newaxis]

rawDescription: python: numpy reshaping an array

description: Python: numpy reshaping an array


code:
import numpy as np
datamin = -5
datamax = 5
numbins = 20
mybins = np.linspace(datamin, datamax, numbins)
myhist = np.zeros(numbins-1, dtype='int32')
for i in range(100):
    d = np.random.randn(1000,1)
    htemp, jnk = np.histogram(d, mybins)
    myhist += htemp

rawDescription: Numpy histogram of large arrays

description: Numpy histogram of large arrays


code:
b1 = np.zeros((4,5))
b2 = np.ones((4,3))
pos_v, pos_h = 2, 3  # offset
v_range1 = slice(max(0, pos_v), max(min(pos_v + b2.shape[0], b1.shape[0]), 0))
h_range1 = slice(max(0, pos_h), max(min(pos_h + b2.shape[1], b1.shape[1]), 0))

v_range2 = slice(max(0, -pos_v), min(-pos_v + b1.shape[0], b2.shape[0]))
h_range2 = slice(max(0, -pos_h), min(-pos_h + b1.shape[1], b2.shape[1]))

b1[v_range1, h_range1] += b2[v_range2, h_range2]

rawDescription: Adding different sized/shaped displaced NumPy matrices


code:
def newhotspot(self, *args):
    ...

rawDescription: python TypeError: object does not support indexing

description: Python TypeError: object does not support indexing


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

plt.close('all')

#------------------------------------------------------------- PREPARE DATA ----

t = np.arange(0, 7 * 10**5, 10)
x = np.abs(np.sin(t/10.**2)) * 300

#------------------------------------------------------------- FIG CREATION ----

fwidth = 8.
fheight = 5.

fig = plt.figure(figsize=(fwidth, fheight))
fig.patch.set_facecolor('white')

left_margin  = 0.6 / fwidth
right_margin = 0.25 / fwidth
bottom_margin = 0.75 / fheight
top_margin = 0.25 / fwidth
mid_margin = 0.1 / fwidth # horizontal space between subplots

#------------------------------------------------- DEF. PARAM. FOR EACH AXE ----

naxes = 3
f = [0.5, 0.3, 0.2] # Proportion of the figure's width taken by each axe.
                    # The sum must == 1

xmin = [200, 50, 0]
xmax = [8200, 200, 50]
xscl = [2000, 50 ,10]

ymin = 0
ymax = 320

color = ['green', 'blue', 'red']
labels = ['label1', 'label2', 'label3']

#------------------------------------------------------------ AXES CREATION ----

axes = [0] * naxes
handles = [0] * naxes
lines = []

wtot = 1 - (left_margin + right_margin + (naxes-1) * mid_margin)

x0 = left_margin
y0 = bottom_margin
h = 1 - (bottom_margin + top_margin)

for i in range(naxes):
    w = wtot * f[i]
    axes[i] = fig.add_axes([x0, y0, w, h], frameon=True)
    axes[i].patch.set_visible(False)

    if i == 0: # First axe to the left
        axes[i].spines['right'].set_visible(False)
        axes[i].tick_params(right='off', labelright='off')

    elif i == naxes - 1: # Last axe to the right
        axes[i].spines['left'].set_visible(False)
        axes[i].tick_params(left='off', labelleft='off',
                            right='off', labelright='off')
    else:
        axes[i].spines['left'].set_visible(False)
        axes[i].spines['right'].set_visible(False)
        axes[i].tick_params(left='off', labelleft='off',
                            right='off', labelright='off')

    x0 += w + mid_margin

    #--------------------------------------------------------- SETUP XTICKS ----

    if i == naxes-1:
        xtick = np.arange(xmin[i], xmax[i] + xscl[i]/2., xscl[i])
    else:
        xtick = np.arange(xmin[i]+xscl[i], xmax[i] + xscl[i]/2., xscl[i])

    axes[i].set_xticks(xtick)
    axes[i].xaxis.set_ticks_position('bottom')
    axes[i].tick_params(axis='x', direction='out', labelsize=8)

    xtickminor = np.arange(xmin[i], xmax[i] + xscl[i]/5., xscl[i] / 5.)
    axes[i].set_xticks(xtickminor, minor=True)
    axes[i].tick_params(axis='x', which='minor', direction='out')

    #------------------------------------------------------------ PLOT DATA ----

    handles[i] = axes[i].plot(t, x, color=color[i])

    axes[i].axis([xmin[i], xmax[i], ymin, ymax])
    axes[i].invert_xaxis()

    lines += handles[i] # for plotting the legend

#---------------------------------------------------------- SET AXE X LABEL ----

xlabel = 'Time (years)'
axes[0].set_xlabel(xlabel, fontsize=12, verticalalignment='bottom',
               horizontalalignment='center')
axes[0].xaxis.set_label_coords(0.5, 0.05, transform=fig.transFigure)

#------------------------------------------------------------------- LEGEND ----

axes[0].legend(lines, labels, loc=(0.1, 0.1), ncol=1, fancybox=True,
               fontsize=12)

#----------------------------------------------------------------- SHOW FIG ----

fig.savefig('SingleAxeThreeScale.pdf')
plt.show(block=False)

rawDescription: Break x axis in more than 2 parts (Python)


code:
import wx
class Example(wx.Frame):
    self.flt1 = NumCtrl(self, pos, size)
    self.btn1 = wx.Button(self, label, pos, size)
    self.btn1.bind(wx.EVT_BUTTON, btn1Click)

    def btn1Click(self,event):
        self.flt1.GetValue()

rawDescription: Getting IntCtrl and NumCtrl to return value after button(wx.Button) widget is pressed


code:
root = compile(open('codefile.py').read(), 'codefile.py', 'exec', _ast.PyCF_ONLY_AST)
root.body[1].body[0].value.func
f = root.body[1].body[0].value.func
vars(f)
f.attr
f.value.id

rawDescription: Name of the imported function in `_ast`

description: Name of the imported function in `_ast`


code:
import sqlite as sql
conn = sql.connect('test.db')
c = conn.cursor()
c.execute('''create table test(item text)''')
list = ['apple', 'orange', 'banana']
c.executemany('''insert into test values(%s)''', list)

rawDescription: python 2.4.3 sqlite executemany syntax

description: Python 2.4.3 sqlite executemany syntax


code:
import select
from systemd import journal

j = journal.Reader()
j.log_level(journal.LOG_INFO)

# j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
j.seek_tail()
j.get_previous()
# j.get_next() # it seems this is not necessary.

p = select.poll()
p.register(j, j.get_events())

while p.poll():
    if j.process() != journal.APPEND:
        continue

    # Your example code has too many get_next() (i.e, "while j.get_next()" and "for event in j") which cause skipping entry.
    # Since each iteration of a journal.Reader() object is equal to "get_next()", just do simple iteration.
    for entry in j:
        if entry['MESSAGE'] != "":
            print(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])

rawDescription: Reading systemd journal from Python script


code:
print("Starting script...")
sys.stdout.flush()
build.run()

rawDescription: Jenkins runs imported python script before main python script


code:
df = pd.DataFrame({"s": [1,4,2,4,3,1,1,2,4,3,2,1,4,3,2,3]})
df.groupby("s").cumcount()
df["s_cumcounts"] = df.groupby("s").cumcount()
df.sort_values(["s_cumcounts", "s"])
df = df.sort_values(["s_cumcounts", "s"])
del df["s_cumcounts"]

rawDescription: pandas python sorting according to a pattern

description: Pandas python sorting according to a pattern


code:
if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        pg.QtGui.QApplication.exec_()

rawDescription: pyqtgraph in pydev disappear after a second

description: Pyqtgraph in pydev disappear after a second


code:
with open(data_path("ref/073985f1c3e2f26c5be4a01073de42d3"), "r") as f:
    as_json = f.read()

blockmodel = BlockModel.from_json(as_json)
x3d = blockmodel.x3d

with open(data_path("made/x3d_ref.x3d"), "w") as f:
    f.write(x3d)

rawDescription: blockmodel: convert json to .x3d

description: Blockmodel: convert json to .x3d


code:
sort(ls[0:pivotIndex])
sort(ls[(pivotIndex + 1):len(ls)])

rawDescription: Why are the boundaries of my quicksort implementation awry?


code:
def pickClass():
    valid_classes = ["FIGHTER", "MAGE", "THIEF"]
    while True:
        print('What class are you? FIGHTER, MAGE, or THIEF?')
        classChoice = raw_input()
        if classChoice not in valid_classes:
            print("Invalid class")
        else:
            print("Are you sure you want to be a %s?" % classChoice)
            while True:
                _confirm = raw_input()
                if _confirm == 'YES':
                    print('So be it.')
                    return classChoice
                elif _confirm == 'NO':
                    break
                else:
                    print('YES or NO only!')

rawDescription: (Python) Go to a line within the function


code:
integer = []
floats = []
not_number = []
full_list = [100, 234, 'random', 5.23, 55.55, 'random2', 'vdfj.324.23tk.sdfklsj']

for i in full_list:
    if isinstance(i, int):
        integer.append(i)
    elif isinstance(i, float):
        floats.append(i)
    else:
        not_number.append(i)
print ('Integers ', integer)
print ('Floats ', floats)
print ('Not numbers ', not_number)

rawDescription: Python: Analyzing input to see if its an integer, float, or string


code:
import matplotlib as mpl
mpl.rc('figure.subplot', left=.2, right=.8, bottom=.2, top=.8)

rawDescription: Creating sets of default values for Matplotlib


code:
from itertools import combinations
[(y1-x1, y2-x2) for (x1, x2), (y1, y2) in combinations(myList, 2)]

rawDescription: Subtract all items in a list against each other


code:
class A(object):

        def f1(self):
                return []

        test1 = property(lambda x:x.f1())

rawDescription: Python property and method override issue: why subclass property still calls the base class's method


code:
import subprocess

find_p = subprocess.Popen(["find", ".", "-name", "*.txt"], stdout=subprocess.PIPE)
grep_p = subprocess.Popen(["xargs", "grep", "rabbit"], stdin=find_p.stdout)

grep_p.wait()

rawDescription: Capture output of complex shell command in python


code:
import cv2
import numpy

# Load the image
img = cv2.imread("/ContourTest.PNG")

# Split out each channel
blue, green, red = cv2.split(img)

def medianCanny(img, thresh1, thresh2):
    median = numpy.median(img)
    img = cv2.Canny(img, int(thresh1 * median), int(thresh2 * median))
    return img

# Run canny edge detection on each channel
blue_edges = medianCanny(blue, 0.2, 0.3)
green_edges = medianCanny(green, 0.2, 0.3)
red_edges = medianCanny(red, 0.2, 0.3)

# Join edges back into image
edges = blue_edges | green_edges | red_edges

# Find the contours
contours,hierarchy = cv2.findContours(edges, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

hierarchy = hierarchy[0] # get the actual inner list of hierarchy descriptions

# For each contour, find the bounding rectangle and draw it
for component in zip(contours, hierarchy):
    currentContour = component[0]
    currentHierarchy = component[1]
    x,y,w,h = cv2.boundingRect(currentContour)
    if currentHierarchy[2] < 0:
        # these are the innermost child components
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),3)
    elif currentHierarchy[3] < 0:
        # these are the outermost parent components
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)

# Finally show the image
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

rawDescription: Python OpenCV Contour tree hierarchy

description: Python OpenCV Contour tree hierarchy


code:
li = [['a','b'], ['c','d']]
[[v.decode("UTF-8") for v in elem] for elem in li]

rawDescription: convert strings in separate lists to unicode - python

description: Convert strings in separate lists to unicode - python


code:
labels1 = plt.clabel(CS, inline=True, use_clabeltext=True, fmt=formatter, fontsize=12)
labels2 = plt.clabel(CS, inline=1, fontsize=12)

for l in labels1+labels2:
    l.set_rotation(0)

rawDescription: change orientation of contour clabel text objects

description: Change orientation of contour clabel text objects


code:
(arr1 == arr2).all(axis=1)
Out[24]: array([ True,  True,  True,  True,  True], dtype=bool)

rawDescription: Python - Compare two 2D arrays by row

description: Python - Compare two 2D arrays by row


code:
from string import ascii_lowercase
for character in ascii_lowercase:
    c = character

rawDescription: Using the For statement in python 3x

description: Using the For statement in python 3x


code:
from itertools import chain
result = list(chain.from_iterable(foo(x) for x in arr))

rawDescription: Generator expression calling a function that returns lists


code: d.loc[pd.Series(d.index.month, d.index).isin([1, 2, 3])]

rawDescription: Pandas series - only keep value if index month is in [1,2,3]


code:
persons_by_jobs = defaultdict(list)
for person in persons:
    persons_by_jobs[person['job']].append(person['person'])

rawDescription: Best way to sort a dictionary into groups using Python

description: Sort a dictionary into groups using Python


code: extlinks = {'repo': ('http://github.com/some/project/%s', 'repo ')}

rawDescription: Role for referencing source code in repository


code:
df = df.reindex(columns=df2.columns.union(df.columns),
                index=df2.index.union(df.index))

rawDescription: Joining 2 data frames with overlapping data


code: sys.stdout.write('The coefficients a, b & c are : ' + str(coeff) + "\n")

rawDescription: Using sys.stdout.write() in Python instead of print


code: b = type(a)(args)

rawDescription: How do you create an instance of a class when the class type is stored in a variable?

description: Create an instance of a class when the class type is stored in a variable


rawDescription: How to make X axis in matplotlib/pylab to NOT sort automatically the values?

description: Make X axis in matplotlib/pylab to NOT sort automatically the values


code:
def coart(phonemeFile) :
    """ Coarticulation function where forward and backward coarticulation take place a  and parameter values are generated"""

    all_lines = []
    with open(phonemeFile, "r") as pFile :
        for line in pFile :
            next_line = []
            line = line.split()
            for syllable in line:
                if syllable == "'" :
                    continue
                next_coart = param_values.get(syllable)
                next_line.append( (syllable, next_coart) )
            all_lines.append(next_line)

rawDescription: working with a big list

description: Working with a big list


code: df[1] = df[0].dt.to_period('M')

rawDescription: Convert a column of timestamps into periods in pandas


code:
my_dict = lambda: defaultdict(my_dict)
d = my_dict()
for x in my_list:
    reduce(defaultdict.__getitem__, x.split("/"), d)

rawDescription: How to convert a strictly sorted list of strings into dict?

description: Convert a strictly sorted list of strings into dict


code:
# flag we set when we're done
finished = False

def finish():
    # ask the user for confirmation?
    global finished
    finished = True
    return

def handle_menu_input(choice):
    handled = True

    if choice == '1':
        print('map needs to be made and shown')
    elif choice == '2':
        print('stats need to be made and assinged to choice 2 in def menu')
    else:
        handled = False

    return handled

def menu():

    finished_menu = False
    while not finished_menu:

        print('Press "1" for map >>> "2" for stats >>> "3" for exit')
        choice = raw_input() # NOTE: changes behaviour in Python 3!

        if handle_menu_input(choice):
            # done
            pass
        elif choice == '3':
            print('You are exiting the menu. Press "M" at any time to return to the menu')
            finished_menu = True
        else:
            print('I did not recognize your command')
            menu()
    return

def userinput():
    print('Press 1 to attack an enemy >>> 2 to search a room >>> 3 to exit game')
    print('Press "M" for menu at any time')
    choice = raw_input() # NOTE: changes behaviour in Python 3!

    if choice == 'm':
        menu()
    elif choice == '1':
        print('attack function here')
    elif choice == '2':
        print('search function here')
    elif choice == '3':
        finish()

    # elif handle_menu_input(choice):
    #     # delegate menu functions?? ..
    #     # do this if you want to see maps anytime without going through the menu?
    #     # otherwise comment this elif block out.
    #     # (Problem is 1, 2 etc are overloaded)
    #     pass
    else:
        print('I did not recognize your command')
    return

def main():
    # main loop
    while not finished:
        userinput()
    return

if __name__ == "__main__":
    main()

rawDescription: Create a text menu in Python3x that is always available


code:
var1 = barcode[:3]   # first three characters
var2 = barcode[3:-1] # all characters from third to next-to-last
var3 = barcode[-1]   # last character

rawDescription: Need help splitting a string into 3 variables


code: [x + y for x in a for y in b]

rawDescription: Operating on each generated entity of a list comprehension


code:
labels = {}
for node in G.nodes():
    if node in hubs:
        #set the node name as the key and the label as its value
        labels[node] = node
#set the argument 'with labels' to False so you have unlabeled graph
nx.draw(G, with_labels=False)
#Now only add labels to the nodes you require (the hubs in my case)
nx.draw_networkx_labels(G,pos,labels,font_size=16,font_color='r')

rawDescription: Networkx specific nodes labeling

description: Networkx specific nodes labeling


code:
with open(filename) as f:
    file_str = f.read()

# do stuff with file_str

with open(filename, "w") as f:
    f.write(file_str)

rawDescription: Read in file - change contents - write out to same file


code:
""" Create your webdriver as 'driver' and then begin here """

parent_el = driver.find_element_by_class_name('question_text_edit')
children = parent_el.find_elements_by_css_selector('*')

html_string = ''.join([child.get_attribute('outerHTML') for child in children])

rawDescription: Extract html from to a specific tag with Python


rawDescription: How to retrieve the number of objects in a ManyToMany relationship backwards

description: Retrieve the number of objects in a ManyToMany relationship backwards


code: f = lambdify(((x, y, z),), lagrange_eqs(a))

rawDescription: Convert symbolic expressions to Python functions using SymPy


code:
vol_id = [x['ebs']['volume_id'] for x in node.extra['block_device_mapping']
          if x['device_name'] == '/dev/sdb'][0]
# vol_id is a string
volume = [v for v in driver.list_volumes() if v.id == vol_id][0]
# volume is a StorageVolume

rawDescription: Get volumes attached to a node with apache-libcloud


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

import sqlite3

import unicodecsv

con = sqlite3.connect('mycorpus.db')
cur = con.cursor()
cur.execute("CREATE TABLE Corpus(sid INT, scid INT, sent TEXT, PRIMARY KEY (sid))")

with open('corpus.tab', 'rb') as input_file:
    reader = unicodecsv.reader(input_file, delimiter="\t")
    data = [row for row in reader]

cur.executemany("INSERT INTO Corpus (sid, scid, sent) VALUES (?, ?, ?);", data)
con.commit()

rawDescription: How to put a textfile into sqlite simply using Python API?

description: Put a textfile into sqlite simply using Python API


code:
from django.conf import settings

settings.DEBUG = True   # Don't do this!

rawDescription: how to modify imported variables in python

description: Modify imported variables in python


code:
class player(pygame.sprite.Sprite):
    layer = 1
    def __init__ (self):
        pygame.sprire.Sprite.__init__(self)
        sprites.add(self, layer = self.layer)

rawDescription: PyGame : use of pygame.sprite.LayeredUpdates()


rawDescription: How to update one NumPy array with values from a different array?

description: Update one NumPy array with values from a different array


code:
def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
    stripped_data = data.get('objects') or data
    desired_format = self.determine_format(request)
    serialized = self.serialize(request, stripped_data, desired_format)
    response = response_class(content=serialized,
                              content_type=build_content_type(desired_format),
                              **response_kwargs)
    # Convert meta data to HTTP Headers
    for name, value in data.get('meta', {}).items():
        response[name] = str(value)
    return response

rawDescription: How to move the Tastypie meta information out to the http headers

description: Move the Tastypie meta information out to the http headers


code:
from string import lowercase

from scrapy.item import Field, Item
from scrapy.spider import BaseSpider
from scrapy.http import Request
from scrapy.selector import HtmlXPathSelector, Selector

from metacritic.items import MetacriticItem

class MetacriticSpider(BaseSpider):
    name = 'metacritic'
    allowed_domains = ['metacritic.com']

    max_id = 1 # your max_id value goes here!!!

    def start_requests(self):
        for c in lowercase:
            for i in range(self.max_id):
                yield Request('http://www.metacritic.com/browse/games/title/ps4/{0}?page={1}'.format(c, i), callback=self.parseps4)

    def parseGame(self, response):
        item = response.meta['item']
        hxs = HtmlXPathSelector(response)
        site = hxs.select('//div[@class="product_wrap"]')

        # get additional data!!!

        yield item

    def parseps4(self, response):
        hxs = Selector(response)
        sites = hxs.select('//div[@class="product_wrap"]')
        for site in sites:
            item = MetacriticItem()
            item['title'] = site.xpath('.//div[contains(@class, "basic_stat product_title")]/a/text()').extract()[0].strip()
            item['cscore'] = site.xpath('.//div[contains(@class, "basic_stat product_score brief_metascore")]/div[1]/text()').extract()[0].strip()
            item['uscore'] = site.xpath('.//div/ul/li/span[contains(@class, "data textscore")]/text()').extract()[0].strip()
            item['release'] = site.xpath('.//li[contains(@class, "stat release_date full_release_date")]/span[2]/text()').extract()[0].strip()

            link = site.xpath('.//div[contains(@class, "basic_stat product_title")]/a/@href').extract()[0]
            yield Request('http://www.metacritic.com/' + link, meta={'item': item}, callback=self.parseGame)

rawDescription: Web scraping rule creation

description: Web scraping rule creation


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

fig, ax = plt.subplots()

x = np.linspace(0, 1)
y = np.sin(4 * np.pi * x) * np.exp(-5 * x)

ax.plot(x, y, 'r')
ax.grid(True)
ax.margins(0.05) # 5% padding in all directions

plt.show()

rawDescription: Matplotlib padding between plot and axis


code:
plt.imshow(grid_resid[:,:], aspect="auto")
plt.xticks(np.arange(grid_resid.shape[1]),xp)
plt.show()

rawDescription: changing the xticks on a python plot

description: Changing the xticks on a python plot


code:
chunk, mask = 3, A['D'] == 1
mask -= mask.shift(-chunk).fillna(0)
A[mask[::-1].cumsum() > 0]

rawDescription: In Python (Pandas/Numpy), How to subset a df using a condition and a specific chunk size?


code:
triu = np.tri_indices(n, 1)
upper_tri = bivariate[triu].reshape(-1, Q*Q)
upper_tri /= upper_tri.sum(axis=1)
bivariate[triu] = upper_tri.reshape(-1, Q, Q)

rawDescription: Normalize Numpy Upper-triangular subarray


code:
seats, wanted = ['1', '0', '1', '0', '0', '0', '0', '0', '0', '0', 'E'], 3
from itertools import groupby
for occ, grp in groupby(enumerate(seats[:-1], 1), key = lambda x: x[1]):
    if occ == '0':
        available = list(grp)
        if len(available) >= wanted:
            print([seats[-1] + str(item[0]) for item in available[:wanted]])

# ['E4', 'E5', 'E6']

rawDescription: Counting items in list before consecutive instance


code:
vk.Session = MagicMock(name='session', return_value=None)
mock_vk_values = [{'uid': user.vk_uid}]
# create an explicit mock for the users attribute
users_mock = MagicMock(name='users')
users_mock.get = MagicMock(name='uid', return_value=mock_vk_values)
# create a mock for the api
api_mock = MagicMock(name='api', users=users_mock)
# this is where a lot of people get mocking wrong -
# mistaking the mock of a constructor with the object returned/created
vk.API = MagicMock(return_value=api_mock)

rawDescription: How to add the fictive method

description: Add the fictive method


code:
# statsmodels only needed to get the R mtcars dataset
import statsmodels.api as sm
mtcars = sm.datasets.get_rdataset('mtcars').data

mtcars.boxplot('mpg', by='cyl')

rawDescription: Plotting box plots in python without grouping the data


code:
outputwriter = csv.writer(open('output.csv', 'w'), delimiter = ' ')
for tup in in_tup:
    inputList = [tup[0]]
    inputList.extend(tup[1])
    outputwriter.writerow(inputList)

rawDescription: writing a line seperated by spaces to a csv file all elements in a different block

description: Writing a line seperated by spaces to a csv file all elements in a different block


code: from ..lib import customLib

rawDescription: python: forcing relative imports to search from script file

description: Python: forcing relative imports to search from script file


code:
for i in A.shape[0]:
    for j in A.shape[1]:
        if i == j:
            A[i,j] = -5

rawDescription: Changing diagonal of matrix using an if- statement?


code: data = [map(int, line.split()) for line in fname if line.strip()]

rawDescription: Creating a list of lists from an input file


rawDescription: Using In statement in django orm

description: Using In statement in django orm


code:
from flask import send_from_directory

@app.route('/my-url/')
def myurl():
    return send_from_directory(app.static_folder, 'foo/bar/index.html')

rawDescription: Mask file name on redirect

description: Mask file name on redirect


rawDescription: Executing subprocess from Python without opening Windows Command Prompt


code: from global_settings_file import GlobalSettingsClass

rawDescription: After splitting my code into one file per class - class can't see one "global class"


code: v6 = re.findall(r'(?s)----------\s*LOW VOLTAGE SUMMARY BY AREA.*?\r(ACTIVITY|\(VFSCAN\)).+?',wholefile)

rawDescription: Need modification to regex to work in other situations


code:
from flask import Flask
import thread
data = 'foo'
app = Flask(__name__)
@app.route("/")
def main():
    return data
def flaskThread():
    app.run()
if __name__ == "__main__":
    thread.start_new_thread(flaskThread,())

rawDescription: Is there any option getting ipython command prompt while running flask


code:
from io import TextIOWrapper

f = form.files['file']
text_f = TextIOWrapper(f.file, encoding='utf-8')

rawDescription: Django: uploaded file is binary. Is it possible to change to utf? So readline() returns unicode rather than bytes


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

    def test(self):
        return self.x + self.y

    def __str__(self):
        return str(self.test())

rawDescription: Python 3: Calling a class function inside of __init__


code:
import os
l = []
for root, dirs, files in os.walk('.'):
    if any(file.endswith('.mp3') for file in files):
        l.append(os.path.abspath(root))

rawDescription: Return list of directories containing specified extension


code:
import numpy
import theano

def something_complicated(x, y):
    z = x * y
    return z

x_value = numpy.array([[1., 2., 3.], [4., 5., 6.]], dtype=theano.config.floatX)
y_value = numpy.array([[7., 8., 9.], [10., 11., 12.]], dtype=theano.config.floatX)
theano.gradient.verify_grad(something_complicated, (x_value, y_value), rng=numpy.random)

rawDescription: verify_grad function: 'TensorVariable' object is not callable

description: Verify_grad function: 'TensorVariable' object is not callable


code:
class EnumMeta(type):
    def __new__(mcs, what, bases, dict):
        if not bases:
            # Enum itself
            return super().__new__(mcs, what, bases, dict)

        # Process subclass creation

rawDescription: Use a metaclass only for subclasses

description: Use a metaclass only for subclasses


code:
class Unit:
  """Very simple class to track a unit name, and an associated count."""
  def __init__(self, name, amount):
   self.name = name
   self.amount = amount

# Pre-existing lists of types and amounts.
list1 = ["Infantry", "Tanks", "Jets"]
list2 = [ 10, 20, 30]

# Create a list of Unit objects, and initialize using
# pairs from the above lists.
units = []
for a, b in zip(list1, list2):
  units.append(Unit(a, b))

rawDescription: Need help with python list manipulation

description: Need help with python list manipulation


code:
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10), label='Curve 1')

inset = fig.add_axes([.2, .64, .28, .24])
inset.scatter(range(3), range(3))

ax.legend()

plt.show()

rawDescription: Matlplotlib plot with inset: make legend in original axis


code:
import random
import string

code = ''

for i in range(1000): #1000 iterations
    if random.randint(0, 1):
        code += random.choice(string.ascii_letters)
    else:
        code = random.choice(string.ascii_letters)

    try:
        print ('trying code: ' + code)
        exec(code)
    except:
        print('It crashed... adding another letter... it will surely work now!')

rawDescription: Python 3 "Evolving" Code

description: Python 3 "Evolving" Code


code:
from threading import Semaphore, Timer
from functools import wraps

def ratelimit(limit, every):
    def limitdecorator(fn):
        semaphore = Semaphore(limit)
        @wraps(fn)
        def wrapper(*args, **kwargs):
            semaphore.acquire()
            result = fn(*args, **kwargs)
            timer = Timer(every, semaphore.release)
            timer.setDaemon(True)      # allows the timer to be canceled on exit
            timer.start()
            return result
        return wrapper
    return limitdecorator

rawDescription: Rate-limiting python decorator

description: Rate-limiting python decorator


code:
import numpy as np
from scipy.optimize import minimize
from matplotlib import pyplot as pp

# function that defines the model we're fitting
def gaussian(P, x):
    a, b, c = P
    return a*np.exp(-(x-b)**2 /( 2*c**2))

# objective function to minimize
def loss(P, x, y):
    yhat = gaussian(P, x)
    return ((y - yhat)**2).sum()

# generate a gaussian distribution with known parameters
amp = 1.3543
pos = 64.546
var = 12.234
P_real = np.array([amp, pos, var])

# we use the vector of real parameters to generate our fake data
x = np.arange(100)
y = gaussian(P_real, x)
# add some gaussian noise to make things harder
y_noisy = y + np.random.randn(y.size)*0.5

# minimize needs an initial guess at the model parameters
P_guess = np.array([1, 50, 25])

# minimize provides a unified interface to all of scipy's solvers. you
# can also access them individually in scipy.optimize, but the
# standalone versions have annoying differences in their syntax. for now
# we'll use the Nelder-Mead solver, which doesn't use the Jacobian. we
# also need to hand it x and y_noisy as additional args to loss()
res = minimize(loss, P_guess, method='Nelder-Mead', args=(x, y_noisy))

# res is a dict containing the results of the optimization. in particular we
# want the optimized model parameters:
P_fit = res['x']

# we can pass these to gaussian() to evaluate our fitted model
y_fit = gaussian(P_fit, x)

# now let's plot the results:
fig, ax = pp.subplots(1,1)
ax.hold(True)
ax.plot(x, y, '-r', lw=2, label='Real')
ax.plot(x, y_noisy, '-k', alpha=0.5, label='Noisy')
ax.plot(x, y_fit, '--b', lw=5, label='Fit')
ax.legend(loc=0, fancybox=True)

rawDescription: Scipy Fmin Guassian model to real data

description: Scipy Fmin Guassian model to real data


code:
fig, ax = plt.subplots(figsize=(3, 4))

ds.sel(time='2015-12-16T02:00:00').t2m.plot(ax=ax)

rawDescription: How do I set size of a Python xray 2Dplot?

description: Set size of a Python xray 2Dplot


code:
from sklearn.tree import DecisionTreeClassifier, export_graphviz
X = np.random.randn(10, 4)
y = array(["foo", "bar", "baz"])[np.random.randint(0, 3, 10)]
clf = DecisionTreeClassifier().fit(X, y)
export_graphviz(clf)

rawDescription: How can I draw a CART tree in Python, as I can in R?

description: Draw a CART tree in Python, as I can in R


code:
m,n = A.shape
B = sparse.vstack(( sparse.hstack(( sparse.coo_matrix((m,m)), A )),
                    sparse.hstack(( A.T, sparse.coo_matrix((n,n)) )) ))

rawDescription: Concatenate zero matrices and sparse matrices in Python


code:
from youtube_dl import YoutubeDL

class MyLogger(object):
    def debug(self, msg):
        print('debug information: %r' % msg)

    def warning(self, msg):
        print('warning: %r' % msg)

    def error(self, msg):
        print('error: %r' % msg)

options = {
    "restrictfilenames": True,
    "progress_with_newline": True,
    "logger": MyLogger(),
}

url = 'http://www.youtube.com/watch?v=BaW_jenozKc'
with YoutubeDL(options) as ydl:
    ydl.download([url])

rawDescription: How can I pipe a Python process' output in Python?

description: Pipe a Python process' output in Python


code:
grandtotal = 0

while True:
    price = int(input('Enter price. (0 to quit) '))
    if price == 0:
        break
    quan = int(input('Enter quantity. '))
    subtotal = price*quan
    print('The subtotal is %d' % subtotal)
    grandtotal = grandtotal + subtotal

print('The sum of the totals is %d' % grandtotal)

rawDescription: How can I loop this code?

description: Loop this code


code:
import json

params = json.dumps({"id":11111,"lastname":"oojamalip"})
# then
response, content = http.request(url, 'POST', body=params, headers={'Content-type':'application/json'})

rawDescription: RESTFUL POST with Python request to Glassfish Server


code:
import json
json.dumps([1, 2, 3, None, 4])

rawDescription: Convert Python None to JavaScript null

description: Convert Python None to JavaScript null


code:
def db_instance2dict(instance):
    from django.db.models.fields.related import ManyToManyField
    metas = instance._meta
    data = {}
    for f in chain(metas.concrete_fields, metas.many_to_many):
        if isinstance(f, ManyToManyField):
            data[str(f.name)] = {tmp_object.pk: db_instance2dict(tmp_object)
                                 for tmp_object in f.value_from_object(instance)}
        else:
            data[str(f.name)] = str(getattr(instance, f.name, False))
    return data

rawDescription: Django model instance fields with ManyToMany relationship to Dict


code:
shipments = [{
    'shipment':s,
    'pickup_address': s.pickup_address,
    'dropoff_address': s.dropoff_address,
    'billing_address': s.billing_address,
} for s in shipments]

return HttpResponse(simplejson.dumps(shipments, ensure_ascii=False, default=json_formatter), mimetype='application/json')

rawDescription: Django: Include related models in JSON string?


rawDescription: Python: Why is comparison between lists and tuples not supported?


code:
document = 'Level1.1\n\tLevel2.1\n\tLevel2.2\n\t\tLevel3\nLevel1.2'

stack = []  # keep track of ancestors ([0] - root, ..., [-1] - parent)
for line in x.splitlines():
    name = line.strip()
    level = line.count('\t')
    while len(stack) > level:
        stack.pop()
    node = Node(name=name, parent=stack[-1] if stack else None)
    node.save()
    stack.append(node)

rawDescription: Build tree from intended text string

description: Build tree from intended text string


code:
import multiprocessing

def f():
  return "hi"

if __name__ == "__main__":
    pool = multiprocessing.Pool(1) # Just a one-process Pool.
    result = pool.apply_async(f)
    try:
        print(result.get(10)) # Wait 10 seconds
    except multiprocessing.TimeoutError:
        print("Timed out")
        pool.terminate() # Kill all (1, in this case) processes in the pool
        pool.join()

rawDescription: How to return a value using python multiprocessing?

description: Return a value using python multiprocessing


code:
while True:
    cv2.imshow('image', img)
    cv2.waitKey(1)
    radius = input('Input radius')
    # recalculate image with new radius here...

rawDescription: How to update imshow() window for Python OpenCV CV2

description: Update imshow() window for Python OpenCV CV2


code:
s.image = pygame.image.load("sprite.png").convert() #load a sprite image
s.rect = b.image.get_rect() # set collision rectangle

rawDescription: How do I make and assign a rect to a sprite in pygame?

description: Make and assign a rect to a sprite in pygame


code:
def getwidth():
    print(lbl.winfo_width(), lbl.winfo_height())

rawDescription: How to get the width of tkinter widget?

description: Get the width of tkinter widget


code:
import re
regex = re.compile('([0-9]+)-([0-9]+)')
match = regex.search('some text.0-376.some text again')
if match:
    numbers = [int(s) for s in match.groups()]
    # numbers -> [0, 376]

rawDescription: How to extract numbers from string separated with minus sign

description: Extract numbers from string separated with minus sign


code: [(k, v) for (k, v) in D.iteritems() if 'Light' in k]

rawDescription: Python: Finding a (string) key in a dictionary that contains a substring


rawDescription: How can I get rows between dates in rethinkdb?

description: Get rows between dates in rethinkdb


rawDescription: Creating rank 3 numpy arrays

description: Creating rank 3 numpy arrays


code:
def replace_item(the_list,toBeReplaced,toBeReplacedBy):
        for index,each_item in enumerate(the_list):
            if isinstance(each_item,list):
                replace_item(each_item,toBeReplaced,toBeReplacedBy)
            else:
                if each_item == toBeReplaced:
                    the_list[index]=toBeReplacedBy

        return the_list

rawDescription: Replace certain elements in lists of lists


code:
from ctypes.util import find_library

find_library('pthread')

find_library('ssl')

find_library('png')

rawDescription: python ctypes, trying to find library names

description: Python ctypes, trying to find library names


code:
drive_filename = r'\\.\PhysicalDrive1'
log_filename = r'C:\ohMyPy\mft.txt'

with open(drive_filename, 'rb') as drive, open(log_filename, 'wb') as logfile:
    logfile.write(drive.read(1024*10000))

rawDescription: Get hex-values / raw data from $MFT on NTFS Filesystem


code:
from datetime import datetime, timedelta

def hours_ago(hours):
    return datetime.today() - timedelta(hours=hours)

def days_ago(days):
    return datetime.today() - timedelta(days=days)

rawDescription: In Python, how to I create a datetime with X hours?


code:
import wx

class Glowne(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)

        self.SetSize((800, 600))

        tekst = 'HELLO'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        self.btn = wx.Button(self, -1, "Change panel", (345, 100))

class Glowne1(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        tekst = 'Panel 2'
        font = wx.Font(18, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        wx.StaticText(self, -1, tekst, (300, 10)).SetFont(font)

        self.btn = wx.Button(self, -1, "Change panel", (345, 100))

class Program(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, 'Program')

        sizer = wx.BoxSizer()
        self.SetSizer(sizer)

        self.panel_one = Glowne(self)
        sizer.Add(self.panel_one, 1, wx.EXPAND)
        self.panel_one.btn.Bind(wx.EVT_BUTTON, self.show_panel_two)
        self.panel_two = Glowne1(self)
        sizer.Add(self.panel_two, 1, wx.EXPAND)
        self.panel_two.btn.Bind(wx.EVT_BUTTON, self.show_panel_one)
        self.panel_two.Hide()
        self.SetSize((800, 600))
        self.Centre()

    def show_panel_one(self, event):
        self.panel_one.Show()
        self.panel_two.Hide()
        self.Layout()

    def show_panel_two(self, event):
        self.panel_two.Show()
        self.panel_one.Hide()
        self.Layout()

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

rawDescription: wxPython - change panel by button

description: WxPython - change panel by button


code:
def isalambda(v):
  LAMBDA = lambda:0
  return isinstance(v, type(LAMBDA)) and v.__name__ == LAMBDA.__name__

rawDescription: How can I test whether a variable holds a lambda?

description: Test whether a variable holds a lambda


code:
from collections import defaultdict
from datetime import datetime
import csv
import glob
import os

# data structure is data[source][date, time][sensor] = value, with "" as default value
data = defaultdict(lambda: defaultdict(lambda: defaultdict(str)))
sensors = []

filelist = glob.glob("*.csv")

# read old files
for fn in filelist:
    sensor = os.path.splitext(fn)[0]
    sensors.append(sensor)
    with open(fn, 'rb') as f:
        reader = csv.DictReader(f, delimiter=";")
        for row in reader:
            date = datetime.strptime(row['date'], '%m/%d/%y')
            data[row['source']][date, row['hour']][sensor] = row['values']

sensors.sort() # note, this may not give the best sort order
header = ['date', 'hour', 'source'] + sensors

for source, source_data in data.iteritems():
    fn = "{}.csv".format(source)
    with open(fn, 'wb') as f:
        writer = csv.writer(f, delimiter=";")
        writer.writerow(header)
        for (date, time), hour_data in sorted(source_data.items()):
            values = [hour_data[sensor] for sensor in sensors]
            writer.writerow([date.strftime('%m/%d/%y'), time, source] + values)

rawDescription: Splitting, merging, sorting CSV

description: Splitting, merging, sorting CSV


rawDescription: Getting array from multidimensional array having maximum lead_id(any particular variable)


code:
from tkinter import *
from random import *

class Application(Canvas):

    X_OFFSET = 40
    Y_OFFSET = 40
    FILL = 'blue'
    LINE = 'black'

    @classmethod
    def main(cls):
        root = Tk()
        surface = cls(root)
        surface.grid()
        surface.after_idle(surface.draw_shape)
        root.mainloop()

    def draw_shape(self):
        x = randrange(int(self['width']) - self.X_OFFSET)
        y = randrange(int(self['height']) - self.Y_OFFSET)
        points = [(x + randrange(self.X_OFFSET), y + randrange(self.Y_OFFSET))
                  for point in range(randint(3, 10))]
        self.create_polygon(points, fill=self.FILL, outline=self.LINE)
        self.after(1000, self.draw_shape)

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

rawDescription: How to draw random shapes in python?

description: Draw random shapes in python


code:
db.define_table('message',Field('body'),Field('author',db.auth_user))
@auth.requires_login()
def index():
    db.message.author.default=auth.user.id
    db.message.author.writable=False
    return dict(form=crud.create(db.message),
                messages=db(db.message.id>0).select())

rawDescription: Django, Turbo Gears, Web2Py, which is better for what?


code:
for i,x in enumerate(t):
    if x.tag == i:
        aNodes = x.find('a')
        if aNodes is not None and len(aNodes) > 0:
            del t[i]
            for j, y in enumerate(x.findall('/nodes()')): #doing x.findall to take in text elements as well as a elements.
                t.insert(i+j,y)

rawDescription: Going deeper with xpath node()

description: Going deeper with xpath node()


code:
import re
strs = 'Consumer spending in the US rose to about 62% of GDP in 1960, where it stayed until about 1981, and has since risen to 71% in 2013'
re.findall(r'\w+\s\d+.*?\s\w+',strs)

rawDescription: Extract number followed and preceded by the words


code: df['Prev. 2 Avg'] = df.Input.rolling(2).mean().shift()

rawDescription: Reference previous rows in Dataframe calculations (Pandas)


code:
import sys
from PIL import Image

img = Image.open(sys.argv[1])
pal = img.getpalette()
width, height = img.size
actual_transp = img.info['actual_transparency'] # XXX This will fail.

result = Image.new('LA', img.size)

im = img.load()
res = result.load()
for x in range(width):
    for y in range(height):
        t = actual_transp[im[x, y]]
        color = pal[im[x, y]]
        res[x, y] = (color, t)

result.resize((64, 64), Image.ANTIALIAS).save(sys.argv[2])

rawDescription: Resizing png image with PIL loses transparency


code: x[x.mask] = fill_value

rawDescription: Numpy - Change value of masked constants


rawDescription: How to substitute values from array to values in list in Python?

description: Substitute values from array to values in list in Python


code:
rows.nonzero()
indices = rows.nonzero()[0]
indices
sparse[indices, :]

rawDescription: Index a SciPy sparse matrix with an array of booleans


code:
from sympy.utilities.lambdify import lambdify

func = lambdify((x1, x2), r.diff(x1))

func(1, 2) # evaluate the function efficiently at (1, 2)

rawDescription: Making a symbolic function callable -- sympy


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

rawDescription: implementing chop() using iterators

description: Implementing chop() using iterators


code:
id = glGenTextures(1)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glBindTexture(GL_TEXTURE_2D, id)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)

rawDescription: why wont this code display my texture in pyopenGL

description: Why wont this code display my texture in pyopenGL


code:
from apscheduler.scheduler import Scheduler

# Start the scheduler
sched = Scheduler()
sched.start()

# Schedule job_function to be called every 5 seconds
@sched.interval_schedule(seconds=5)
def incr_score():
    score += 100

rawDescription: Pygame simple score system

description: Pygame simple score system


rawDescription: Python appending elements of a list to a string


code:
import re
s= input('Enter a Sentence: ')
re.sub('[AEIOUaeiou]','',s)

rawDescription: Remove characters in a string without multiple calls to str.replace


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

def main():
    #-- Generate some data ----------------------------------------------------
    nx = 10
    x = np.linspace(0, 2*np.pi, 10)
    y = 2 * np.sin(x)

    groups = [('GroupA', (x[0], x[nx//3])),
              ('GroupB', (x[-2*nx//3], x[2*nx//3])),
              ('GroupC', (x[-nx//3], x[-1]))]

    #-- Plot the results ------------------------------------------------------
    fig = plt.figure()
    ax = fig.add_subplot(111)

    # Give ourselves a bit more room at the bottom
    plt.subplots_adjust(bottom=0.2)

    ax.plot(x,y, 'k^')

    # Drop the bottom spine by 40 pts
    ax.spines['bottom'].set_position(('outward', 40))

    # Make a second bottom spine in the position of the original bottom spine
    make_second_bottom_spine(label='Treatment')

    # Annotate the groups
    for name, xspan in groups:
        annotate_group(name, xspan)

    plt.xlabel('Dose')
    plt.ylabel('Response')
    plt.title('Experimental Data')

    plt.show()

def annotate_group(name, xspan, ax=None):
    """Annotates a span of the x-axis"""
    def annotate(ax, name, left, right, y, pad):
        arrow = ax.annotate(name,
                xy=(left, y), xycoords='data',
                xytext=(right, y-pad), textcoords='data',
                annotation_clip=False, verticalalignment='top',
                horizontalalignment='center', linespacing=2.0,
                arrowprops=dict(arrowstyle='-', shrinkA=0, shrinkB=0,
                        connectionstyle='angle,angleB=90,angleA=0,rad=5')
                )
        return arrow
    if ax is None:
        ax = plt.gca()
    ymin = ax.get_ylim()[0]
    ypad = 0.01 * np.ptp(ax.get_ylim())
    xcenter = np.mean(xspan)
    left_arrow = annotate(ax, name, xspan[0], xcenter, ymin, ypad)
    right_arrow = annotate(ax, name, xspan[1], xcenter, ymin, ypad)
    return left_arrow, right_arrow

def make_second_bottom_spine(ax=None, label=None, offset=0, labeloffset=20):
    """Makes a second bottom spine"""
    if ax is None:
        ax = plt.gca()
    second_bottom = mpl.spines.Spine(ax, 'bottom', ax.spines['bottom']._path)
    second_bottom.set_position(('outward', offset))
    ax.spines['second_bottom'] = second_bottom

    if label is not None:
        # Make a new xlabel
        ax.annotate(label,
                xy=(0.5, 0), xycoords='axes fraction',
                xytext=(0, -labeloffset), textcoords='offset points',
                verticalalignment='top', horizontalalignment='center')

if __name__ == '__main__':
    main()

rawDescription: How do I plot multiple X or Y axes in matplotlib?

description: Plot multiple X or Y axes in matplotlib


code:
class MyUser(models.Model):
    blocked_users = models.ManyToManyField(..., through="BlockedUser")

class BlockedUser(models.Model):
    user = models.ForeignKey(MyUser)
    blocked = models.ForeignKey(MyUser)

    def save(self, *args, **kwargs):
        # Only allow this relationship to be created if
        if self.user != self.blocked:
            super(BlockedUser, self).save(*args, **kwargs)

rawDescription: How can I init ManyToMany field in django models that can't relate to itself(object level)?

description: Init ManyToMany field in django models that can't relate to itself(object level)


code:
import numpy as np
import scipy as sp

rawDescription: How to make a script execute just after python shell starts and before customuer's input?

description: Make a script execute just after python shell starts and before customuer's input


rawDescription: Python 2.7 runtime code changes

description: Python 2.7 runtime code changes


code:
#!/usr/local/cpython-3.3/bin/python

import pprint

def recursive_remover(unwanted_keys, dictionary):
    for key, value in list(dictionary.items()):
        if isinstance(value, dict):
            recursive_remover(unwanted_keys, value)

        if key in unwanted_keys:
            del dictionary[key]

def main():
    mydict={'user': {'key1':'12','key2':'20','key3':30},
           'status':{'newk':'12','user':{'key1':'12','key2':'20','key3':30}}}

    unwanted_keys = { 'key1', 'key2' }

    recursive_remover(unwanted_keys, mydict)

    pprint.pprint(mydict)

main()

rawDescription: Removing multiple keys from nested dictionaries


rawDescription: How to count number of rows in a group in pandas group by object?

description: Count number of rows in a group in pandas group by object


code:
c = Counter(mylist)
duplicates = [ x for x,y in c.items() if y > 1 ]

rawDescription: Python: Listing the duplicates in a list


code:
from datetime import datetime
from django.contrib import admin

class eventInline(admin.TabularInline):
    def queryset(self, request):
        qs = super(eventInline, self).queryset(request)
        return qs.filter(eventdate__gte=datetime.now)
    model = event
    extra = 1

class VenueAdmin(admin.ModelAdmin):
   inlines = [eventInline,]

rawDescription: timestamp filtering in the admin for an inlines

description: Timestamp filtering in the admin for an inlines


code:
how_many = int(input("How many students do you want to add? "))
students = []
for i in range(how_many):
    students.append(input("Student Name: "))

rawDescription: Use loop to name variables

description: Use loop to name variables


code:
A=[ [1,2,3],
    [4,5,6],
    [7,8,9] ]

f = open('myfile','w')

for i in A:
    for j in i:
        print(j),
        f.write(str(j) + ' ')
    f.write("\n")

f.close()

rawDescription: Print output in text file in Python - Newline issue