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

a = 1.0*np.sin(np.linspace(0,5,51))+0.6
b = 8.0*np.cos(np.linspace(0,5,51))-5.0

fig,ax1 = plt.subplots()
l1, = ax1.plot(a,'r-', label='ax1')
plt.legend()
ax2 = ax1.twinx()
l2, = ax2.plot(b,'b-', label='ax2')

y1mn, y1mx = ax1.get_ylim() ; y1ticks = ax1.get_yticks()
y2mn, y2mx = ax2.get_ylim() ; y2ticks = ax2.get_yticks()

if y1mn*y1mx > 0 : error()
if y2mn*y2mx > 0 : error()

d1 = y1mx-y1mn
d2 = y2mx-y2mn

r1=-y1mx/y1mn
r2=-y2mx/y2mn

if d1>d2:
    if r1>r2:
        y2mx = -y2mn*r1
    else:
        y2mn = -y2mx/r1
else:
    if r2>r1:
        y1mx = -y1mn*r2
    else:
        y1mn = -y1mx/r2

ax1.set_ylim(y1mn,y1mx) ; ax1.set_yticks(y1ticks)
ax2.set_ylim(y2mn,y2mx) ; ax2.set_yticks(y2ticks)
for ax, c in ((ax1,'r'),(ax2,'b')):
    [lbl.set_color(c) for lbl in ax.get_yticklabels()]

ax1.grid(color='black', axis='x')
ax1.grid(color='red',   axis='y')
ax2.grid(color='blue',  axis='y')

plt.legend((l1,l2),('ax1','ax2'))
plt.show()

rawDescription: Guaranteeing 0 at same level on left and right y axes (Python matplotlib)


code:
from itertools import groupby
from operator import itemgetter

data = [1,1,1,1,0,0,0,0,0,1,1,1,0,0]
blocks = [map(itemgetter(0), itemgetter(0, -1)(list(g))) + [k] for k, g in groupby(enumerate(data), itemgetter(1))]
# [[0, 3, 1], [4, 8, 0], [9, 11, 1], [12, 13, 0]]

rawDescription: Extracting contiguous data from a list

description: Extracting contiguous data from a list


code:
import time

def main():
    while True:
        # Save time before starting
        start = time.time()
        # Run you job
        do_task()
        # Estimate how much time the task took (end-start)
        delay = time.time()-start
        # Wait for next 15 mins (counted from start, not from end)
        time.sleep((15*60)-delay)

if __name__ == "__main__":
    main()

rawDescription: Cronjob at given intervall

description: Cronjob at given intervall


code:
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import matplotlib.pyplot as plt

# Downloaded from http://biogeo.ucdavis.edu/data/gadm2/shp/DEU_adm.zip
fname = '/downloads/DEU/DEU_adm1.shp'

adm1_shapes = list(shpreader.Reader(fname).geometries())

ax = plt.axes(projection=ccrs.PlateCarree())

plt.title('Deutschland')
ax.coastlines(resolution='10m')

ax.add_geometries(adm1_shapes, ccrs.PlateCarree(),
                  edgecolor='black', facecolor='gray', alpha=0.5)

ax.set_extent([4, 16, 47, 56], ccrs.PlateCarree())

plt.show()

rawDescription: Draw a map of a specific country with cartopy?


code:
output = defaultdict(dict)

for person in people:
    output[person['Name']][person['Attribute']] = person['Value']

rawDescription: Convert list of dictionaries to nested dictionary


rawDescription: Python datetime formatting conversion

description: Python datetime formatting conversion


code:
import sys
from signal import signal, SIGTERM

signal(SIGTERM, lambda signum, stack_frame: sys.exit(1))
atexit.register(specific_cleanup_func)

rawDescription: python background thread cannot trigger function registered at atexit.register()

description: Python background thread cannot trigger function registered at atexit.register()


code:
from pylab import *

plot((0,1),(1.200001,1.200002))
ticklabel_format(useOffset=False)
show()

rawDescription: Remove bias in yticklabel in matplotlib

description: Remove bias in yticklabel in matplotlib


code:
def primes(n):
    primfac = []
    d = 2
    while d*d <= n:
        while (n % d) == 0:
            primfac.append(d)  # supposing you want multiple factors repeated
            n //= d
        d += 1
    if n > 1:
       primfac.append(n)
    return primfac

rawDescription: Prime factorization - list

description: Prime factorization - list


code:
x= download_buffer.read(1024)
while x:
    self.wfile.write(x)
    x= download_buffer.read(1024)

rawDescription: python: serve a file as it is being downloaded with urllib, BaseHTTPSserver

description: Python: serve a file as it is being downloaded with urllib, BaseHTTPSserver


code:
if 'sphinx' in sys.modules:
    in_mxds = [r"C:/test.mxd"]
else:
    in_mxds = arcpy.GetParameterAsText(1)

rawDescription: Check if Sphinx doc called the script

description: Check if Sphinx doc called the script


code:
data = list()
for root, dirs, files in os.walk(targetdir):
    for filename in files:
        nm, ext = os.path.splitext(filename)
        if ext.lower().endswith(('.xlsx', '.m4v', 'mov')):
            fullpath = os.path.join(os.path.abspath(root), filename)
            data.append((filename, filepath))
df1 = pd.DataFrame(data, columns=['filename', 'fullpath'])
print(df1)

rawDescription: How do I index os.walk into a dataframe?

description: Index os.walk into a dataframe


code:
import Tkinter as tk
import tkFont

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self._textFont = tkFont.Font(name="TextFont")
        self._textFont.configure(**tkFont.nametofont("TkDefaultFont").configure())

        toolbar = tk.Frame(self, borderwidth=0)
        container = tk.Frame(self, borderwidth=1, relief="sunken",
                             width=600, height=600)
        container.grid_propagate(False)
        toolbar.pack(side="top", fill="x")
        container.pack(side="bottom", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        text = tk.Text(container, font="TextFont")
        text.grid(row=0, column=0, sticky="nsew")

        zoomin = tk.Button(toolbar, text="+", command=self.zoom_in)
        zoomout = tk.Button(toolbar, text="-", command=self.zoom_out)
        zoomin.pack(side="left")
        zoomout.pack(side="left")

        text.insert("end", '''Press te + and - buttons to increase or decrease the font size''')

    def zoom_in(self):
        font = tkFont.nametofont("TextFont")
        size = font.actual()["size"]+2
        font.configure(size=size)

    def zoom_out(self):
        font = tkFont.nametofont("TextFont")
        size = font.actual()["size"]-2
        font.configure(size=max(size, 8))

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

rawDescription: How to stop Tkinter Text widget resize on font change?

description: Stop Tkinter Text widget resize on font change


code:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_FOLDER = os.path.abspath(os.path.dirname(__file__))

STATIC_ROOT = os.path.join(PROJECT_FOLDER, "static")
STATIC_URL = '/static/'

STATICFILES_DIRS = (os.path.join(BASE_DIR, "staticfiles"), )

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

rawDescription: blank django project serving static CSS file

description: Blank django project serving static CSS file


code:
def calculate_ot(t1,t2):
    t1 = datetime.strptime(t1, "%d-%m-%Y %H:%M")
    t2 = datetime.strptime(t2, "%d-%m-%Y %H:%M")
    days = ((t2 - t1).days * 86400)
    hours, rem = divmod((t2 - t1).seconds + days, 3600)
    start_ot = datetime.strptime("18:00 {}".format(t1.date()), "%H:%M %Y-%m-%d")
    end_ot = datetime.strptime("06:00 {}".format(t2.date()), "%H:%M %Y-%m-%d")

    total_pay = 0
    for x in range(hours): # loop in total hour time difference
        # if we are within the range of extras pay increase by normal rate plus ot
        if start_ot <= t1 < end_ot and t1 < t2:
            total_pay += 62 # or 62.20 you have conflicting examples
        else:
            # else just add basic pay rate
            total_pay == 50
        t1 += timedelta(hours=1) # keep adding another hour
    return total_pay

rawDescription: Python: Calculate hours within a specific time period


code:
import numpy as np
from scipy.io.wavfile import write

data = np.random.uniform(-1,1,44100) # 44100 random samples between -1 and 1
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
write('test.wav', 44100, scaled)

rawDescription: How to generate audio from a numpy array?

description: Generate audio from a numpy array


code:
class FooBarBaz:
    BAR = 123

    @classmethod
    def getBar(cls):
        return cls.BAR

rawDescription: How to reference the class itself in python

description: Reference the class itself in python


code:
from collections import namedtuple
two_d = namedtuple('twoDPoint', ['x', 'y'])
x = two_d(1, 2)
x = two_d(1, 2)
three_d = namedtuple('threeDPoint', ['x', 'y', 'z'])
x
y = three_d(*x, z=3)
y

rawDescription: add something to nametuple

description: Add something to nametuple


code:
matches = [ k for k in yourdictionary.keys() if 'gc' in k.lower() ]
number_of_matches = len(matches)

rawDescription: Accessing keys in dictionaries

description: Accessing keys in dictionaries


code:
class parent(object):

    def __init__(self):
        self.other = lambda param: other_class2(self,param)
        self.answer = None

class other_class2(object):

    def __init__(self,parent,inputed_param):
        self.parent = parent
        self.other_class = lambda param: other_class(self,param)
        self.input = inputed_param

rawDescription: Creating a parent.child class structure where the child has a


rawDescription: Writing numerical lookup functions in python


code: a[tuple(b)]

rawDescription: Indexing numpy array with another numpy array


code:
import time
expires = time.time() + 14 * 24 * 3600 # 14 days from now
time.strftime("%a, %d-%b-%Y %T GMT", time.gmtime(expires))

rawDescription: Python: Persistent cookie, generate `expires` field


code:
for p in practices:
    config = p.config

    config['alert']['emails'] = [
        '.{0}.fake'.format(x)
        for x in config['alert']['emails']
    ]

    p.config = config

    p.save()

rawDescription: Change list items that are within a dictionary


code:
import urllib2
f = urllib2.urlopen(str(Hyperlink), timeout=2)

rawDescription: Terminating a while loop after 2 seconds for a lengthy process within the loop


rawDescription: Python regex: replace strings in list

description: Python regex: replace strings in list


code:
def make_lists(mean):
    'without duplicates'
    for i in range(0, mean+1):
        for j in range(i, mean+1):
            k = mean * 3 - i - j

            assert (k+i+j) / 3.0 == float(mean), ((k+i+j) / 3.0) #just testing
            yield (i,j, k)

if __name__ == '__main__':
    print( list(make_lists(10)))

rawDescription: Find values for a given mean

description: Find values for a given mean


code:
def sum_first(lst):
    if not lst:
        return 0
    elif isnumber(lst[-1]):
        return lst[-1] + sum_first(lst[:-1])
    else:
        return sum_first(lst[:-1])

rawDescription: Add all numbers that are in a list

description: Add all numbers that are in a list


code: re.search(r'\bis\b', your_string)

rawDescription: Python regular expression match whole word


code:
#! /usr/bin/python
#filename modify_hob.py

# Open the file with filenames list.
with open('filenames.txt') as list_f:

    # Iterate over the lines, each line represents a file name.
    for filename in list_f:

        # Rewrite its content.
        with open(filename) as f:
            content = f.read()
        with open(filename, 'w') as f:
            f.write(content.replace('.txt', '.csv'))

rawDescription: Read filepaths from text file and modify each of them


code:
# This is an example for Josh, assuming you already have list call lines,
# lines = [(mark1, grad1), (mark2, grade2)......]
marks, grades = zip(*lines) # marks is a tuple of marks, grades is a tuple of grades.

rawDescription: How to create a dictionary python

description: Create a dictionary python


rawDescription: Plotting vectors in a coordinate system with R or python


code:
string.endswith('mp4') and string.startswith('/')

string.endswith('mp4') and "/" in string

string.endswith('mp4') and not "A" in string

rawDescription: how to check if a string ends in a certain way?

description: Check if a string ends in a certain way


code:
import webapp2
from webapp2_extras import jinja2

def jinja2_factory(app):
    j = jinja2.Jinja2(app)
    j.environment.filters.update({
        # Set filters.
        # ...
    })
    j.environment.globals.update({
        # Set global variables.
        'uri_for': webapp2.uri_for,
        # ...
    })
    return j

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(factory=jinja2_factory)

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)

rawDescription: webapp2 + jinja2: How can i get uri_for() working in jinja2-views

description: Webapp2 + jinja2: How can i get uri_for() working in jinja2-views


rawDescription: Using Selenium with Python: Clicking multiple 'expand' links that don't contain ID's


code:
from lxml import html as HTML
from lxml import etree as ET

...

doc_pattern = ('//a[@href['
           ' contains(., ".pdf") or'
           ' contains(., ".dot") or'
           ' contains(., ".rtf") or'
           ' contains(., ".doc") or'
           ' contains(., ".xls") or'
           ' contains(., ".docx") or'
           ' contains(., ".xlsx")'
           ']]')

def parse_page_html(pattern,
                    response=response,
                    HTML=HTML,
                    ET=ET
                    ):

            parser  = ET.HTMLParser(remove_comments=True, remove_blank_text=True)
            content = response.xpath(pattern)[0].extract()
            content = content.encode("ascii","ignore")
            content = content.replace('\r','').replace('\n','').replace('\t','')
            body    = HTML.fromstring(content, parser=parser)

            for links in body.xpath(doc_pattern):
                links.attrib['href'] = remove_absolute_url( links.attrib['href'] )

...
content = ET.tostring(body)
return content

rawDescription: Scrapy - Formatting Content and HTML attributes


code:
def get_exdate(self, id):
    return id and re.compile("Grid_exdate_").search(id)

df = pd.DataFrame()
exdate_list = []
for link in soup.find_all(id=self.get_exdate):
    exdate_list.append(link.string)

df['Grid_exdate'] = exdate_list

rawDescription: Beautifulsoup span id tags to pandas

description: Beautifulsoup span id tags to pandas


code:
def is_sublist(a, b):
    if a == []: return True
    if b == []: return False
    return b[:len(a)] == a or is_sublist(a, b[1:])

rawDescription: Python: find a list within members of another list(in order)


code:
def insert_menu():
    with open("index.html") as index:
        index_text = index.read()
    with open("menu.html") as menu:
        menu_text = menu.read()
    # I called it index2 so it doesn't overwrite... you can change that
    with open("index2.html", "w") as index2:
        index2.write(index_text.replace('<!-- Insert Menu here -->', menu_text))

rawDescription: Python: Write an entire file to another file after a specific line that meets a particular condition


code:
array2 = [{'id': 'A', 'name': 'Greg'},
{'id': 'Desk', 'name': 'Will'},
{'id': 'E', 'name': 'Craig'},
{'id': 'G', 'name': 'Johnson'}]
array1 = ['A', 'C', 'Desk']
filtered = [item for item in array2 if item['id'] not in array1]
filtered

rawDescription: How to delete from list?

description: Delete from list


rawDescription: Behavior of "round" function in Python

description: Behavior of "round" function in Python


code: numpy.transpose([numpy.tile(x, len(y)), numpy.repeat(y, len(x))])

rawDescription: Numpy: cartesian product of x and y array points into single array of 2D points


code:
import struct

def unpack(fmt, astr):
    """
    Return struct.unpack(fmt, astr) with the optional single * in fmt replaced with
    the appropriate number, given the length of astr.
    """
    # http://stackoverflow.com/a/7867892/190597
    try:
        return struct.unpack(fmt, astr)
    except struct.error:
        flen = struct.calcsize(fmt.replace('*', ''))
        alen = len(astr)
        idx = fmt.find('*')
        before_char = fmt[idx-1]
        n = (alen-flen)/struct.calcsize(before_char)+1
        fmt = ''.join((fmt[:idx-1], str(n), before_char, fmt[idx+1:]))
        return struct.unpack(fmt, astr)

print(unpack('b*','abc'))
# (97, 98, 99)

rawDescription: auto-repeat flag in a pack format string

description: Auto-repeat flag in a pack format string


code:
def table():
    with open('C:\\Python27\\test.csv', 'rb') as f:
        lines = []
        reader = csv.reader(f)
        if reader:
            for row in reader:
                lines.append(row)
            return lines
        else:
            return []

rawDescription: Cannot return list elements from Python function


code:
from selenium import webdriver
driver = webdriver.Firefox() # You can replace this with other web drivers
driver.get("http://kff.org/womens-health-policy/state-indicator/ultrasound-requirements/#")
source = driver.page_source # Here is your populated data.
driver.quit() # don't forget to quit the driver!

rawDescription: How to get the content from web browser using python?

description: Get the content from web browser using python


code:
for elemendid in tulemus: #check if there is a lower element from new list
    n = 0
    if hulgacopy[0] <= tulemus[n]:
        tulemus.insert(n, el)
        del hulgacopy[0]
        break
    n = n + 1
else:
    tulemus.append(el)
    del hulgacopy[0]

rawDescription: Insertion sort in Python

description: Insertion sort in Python


code: [ p.get("data-pid") for p in fromstring(source).xpath('/html/body/article/section/div[1]/div[2]/p') ]

rawDescription: pythons lxml.html, grab all at once

description: Pythons lxml.html, grab all at once


code:
defaults = dict(val1="val1", val2=3)
varname = 'MyModuleConfig'
with open('myconfig.py', 'w') as fh:
  fh.write('%s = %s\n' % (varname, repr(defaults)))
  # Produced file should contain :
  # MyModuleConfig = {'val2': 3, 'val1': 'val1'}

rawDescription: Output persistent data in valid python

description: Output persistent data in valid python


code:
gcols = ['store', 'date']
tmp[tmp.set_index(gcols).index.isin(df.set_index(gcols).index) == False]

rawDescription: Constructing pandas dataframe with rows conditional on their not existing in another dataframe python


rawDescription: Python substitute a word for a word and the next concatenated


code:
# Send an HTML email with an embedded image and a plain text message for
# email clients that don't want to display the HTML.

from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEImage import MIMEImage

# Define these once; use them twice!
strFrom = 'from@example.com'
strTo = 'to@example.com'

# Create the root message and fill in the from, to, and subject headers
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'test message'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'

# Encapsulate the plain and HTML versions of the message body in an
# 'alternative' part, so message agents can decide which they want to display.
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

# We reference the image in the IMG SRC attribute by the ID we give it below
msgText = MIMEText('Some HTML text and an image.

Nifty!', 'html')
msgAlternative.attach(msgText)

# This example assumes the image is in the current directory
fp = open('test.jpg', 'rb')
msgImage = MIMEImage(fp.read())
fp.close()

# Define the image's ID as referenced above
msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

# Send the email (this example assumes SMTP authentication is required)
import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp.example.com')
smtp.login('exampleuser', 'examplepass')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()

rawDescription: Sending Multipart html emails which contain embedded images


code: soup.find_all('a', text=['revoked', 'active', 'default'])

rawDescription: Python: Print Specific line of text out of TD tag


code:
import pandas as pd
import numpy as np
df = pd.DataFrame([np.random.rand(10) for _ in xrange(10)])
df.ix[[1,4,5],[3,4,5]]

rawDescription: How to loop through pandas data frame to extract specific rows and selected columns

description: Loop through pandas data frame to extract specific rows and selected columns


code:
import csv

# List of your files
file_names = ['file1', 'file2']

# Output list of generator objects
o_data = []

# Open files in the succession and
# store the file_name as the first
# element followed by the elements of
# the third column.
for afile in file_names:
    file_h = open(afile)
    a_list = []
    a_list.append(afile)
    csv_reader = csv.reader(file_h, delimiter=' ')
    for row in csv_reader:
        a_list.append(row[2])
    # Convert the list to a generator object
    o_data.append((n for n in a_list))
    file_h.close()

# Use zip and csv writer to iterate
# through the generator objects and
# write out to the output file
with open('output', 'w') as op_file:
    csv_writer = csv.writer(op_file, delimiter=' ')
    for row in list(zip(*o_data)):
        csv_writer.writerow(row)
op_file.close()

rawDescription: Combining columns of multiple files in one file - Python


code:
def call_back(self):
    show = tkMessageBox.showinfo
    if not self.text_n.get():
        show("Mandatory Information", "Please input an integer value for Number of Tessellations Cells")

    elif not self.text_id.get():
        show("Mandatory Information", "Please input an integer value for Tessellation Identifier")

    elif not domain_container:
        show("Mandatory Information", "Please input Domain")

    elif self.text_fmax.get() and not 0 <= int(self.text_fmax.get()) <= 180:
        show("Incorrect Value", "Face Flatness should be less than 180")

    elif not filename4:
        show("Mandatory Information", "Please input Output File Name")

    else:
        self.execute_neper_code()

rawDescription: IF-ELIF statements nested

description: IF-ELIF statements nested


code:
#!/usr/bin/env python3
"""Emulate `cat -v` behaviour.

use ^ and M- notation, except for LFD and TAB

NOTE: python exits on ^Z in stdin on Windows
NOTE: newlines handling skewed towards interactive terminal.
      Particularly, applying the conversion twice might *not* be a no-op
"""
import fileinput, sys

def escape(bytes):
    for b in bytes:
        assert 0 <= b < 0x100

        if  b in (0x09, 0x0a): # '\t\n'
            yield b
            continue

        if  b > 0x7f: # not ascii
            yield 0x4d # 'M'
            yield 0x2d # '-'
            b &= 0x7f

        if  b < 0x20: # control char
            yield 0x5e # '^'
            b |= 0x40
        elif  b == 0x7f:
            yield 0x5e # '^'
            yield 0x3f # '?'
            continue

        yield b

if __name__ == '__main__':
    write_bytes = sys.stdout.buffer.write
    for bytes in fileinput.input(mode="rb"):
        write_bytes(escape(bytes))

rawDescription: Safe escape function for terminal output


rawDescription: How to find something in a 2D array when only looking for one item of the 2D array?

description: Find something in a 2D array when only looking for one item of the 2D array


code:
def show_other(evt):
    f2 = wx.Frame(None,-1)
    c = wx.Choice(f2,-1,choices=['red','blue','green'])
    f2.Show()

a = wx.App(redirect = False)

f = wx.Frame(None,-1)
b = wx.Button(f,wx.ID_OK)
b.Bind(wx.EVT_BUTTON,show_other)
f.Show()
a.MainLoop()

rawDescription: How to open up a new child window when a button is clicked? (wxpython)

description: Open up a new child window when a button is clicked? (wxpython)


code: setattr(str, "hello", lambda: "Hello custom str!")

rawDescription: Force str() and the two string literals to create a custom string?


code:
import multiprocessing
from functools import partial

lock = None  # Global definition of lock
pool = None  # Global definition of pool

def make_network(initial_tag, max_tags=2, max_iter=3):
    global lock
    global pool
    lock = multiprocessing.Lock()
    pool = multiprocessing.Pool(8)

def get_more_tags():
    global lock
    pass

# this is a very expensive function that I would like to parallelize
# over a list of tags. It involves a (relatively cheap) call to an external
# database, which needs a lock to avoid simultaneous queries. It takes a
# list of strings (tags) as its sole argument, and returns a list of sets
# with entries corresponding to the input list.
f = partial(get_more_tags, max_tags=max_tags)

def _recursively_find_more_tags(tags, level):
    global pool
    if level >= max_iter:
        raise StopIteration
    new_tags = pool.map(f, tags)
    to_search = []
    for i, s in zip(tags, new_tags):
        for t in s:
            joined = ' '.join(t)
            print(i + "|" + joined)
            to_search.append(joined)
    try:
        return _recursively_find_more_tags(to_search, level + 1)
    except StopIteration:
        return None

_recursively_find_more_tags([initial_tag], 0)

rawDescription: Trouble using a lock with multiprocessing.Pool: pickling error


rawDescription: Regex not getting all numbers

description: Regex not getting all numbers


code:
import numpy as np
import pandas as pd

### create sample data
date = pd.date_range("2010-01-01", periods=365*3)
date = pd.Index(np.random.choice(date, 30000))
order_id = np.random.randint(10, 1000, size=30000)

df = pd.DataFrame({"date":date, "order_id":order_id})

### group by year and date
date = pd.Index(df["date"])
df2 = df["order_id"].groupby([date.year, date]).count()

### shift all year to 2000
date = df2.index.get_level_values(1)
new_date = pd.Index(pd.io.date_converters.parse_date_fields(np.ones(len(date))*2000, date.month, date.day))
year = df2.index.get_level_values(0)
df2.index = pd.MultiIndex.from_arrays([year, new_date])

### plot
p = df2.unstack(0).plot()
p.xaxis.set_ticklabels(range(1, 13));

rawDescription: Plotting date in Pandas independent of year


code:
doc.xpath("//a[re:match(text(), 'some text')]",
        namespaces={"re": "http://exslt.org/regular-expressions"})

rawDescription: How to use regular expression in lxml xpath?

description: Use regular expression in lxml xpath


code: [input[i:i+n] for i in range(0, len(input), n)]        # use xrange in py2k

rawDescription: Slicing a list into a list of sub-lists

description: Slicing a list into a list of sub-lists


code:
import scipy.io.wavfile

samplerate, data = scipy.io.wavfile.read("mywav.wav")

rawDescription: Audio spectrum extraction from audio file by python


order: desc

rawDescription: Python custom scripting in ElasticSearch


code:
from subprocess import Popen
midori_process = Popen(["midori"])

rawDescription: How to open an application in Python, and then let the rest of the script run?

description: Open an application in Python, and then let the rest of the script run


code:
while True:
    try:
        # your task
        # check if it was successful and if so
        break
    except Exception: # a relevant exception would be best
        pass # you could even use sleep() here(time module)
             #  to wait a few seconds between tries

rawDescription: How to sucessful launch a python cronjob?

description: Sucessful launch a python cronjob


code:
pt1 = (10, 20)
pt2 = (100, 200)
# calculate top left corner coords, width, height
min_x = min(pt1[0], pt2[0]) # left
min_y = min(pt1[1], pt2[1]) # top
width = max(pt1[0], pt2[0])-min_x
height = max(pt1[1], pt2[1])-min_y
# draw rectangle
dc.DrawRectangle(min_x, min_y, width, height)

rawDescription: Draw a rectangle with two points in wxpython


code:
with patch('models.my_function') as add_method:
    m = MyModel(name=name)
    m.save()

rawDescription: Mock never called

description: Mock never called


code:
div = self.driver.find_element_by_class_name('someclass')
div.find_element_by_css_selector('a').get_attribute('href')

rawDescription: selenium how to get the content of href within some targeted class

description: Selenium how to get the content of href within some targeted class


code:
import local_django.conf
import django.conf
django.conf.settings = local_django.conf.settings

rawDescription: Python: How can I override one module in a package with a modified version that lives outside the package?


code:
from pyspark.sql.functions import *
from pyspark.sql import Row

tbl = sc.parallelize([
        Row(first_name='Alice', last_name='Cooper'),
        Row(first_name='Prince', last_name=None),
        Row(first_name=None, last_name='Lenon')
    ]).toDF()

tbl.show()

def combine(c1, c2):
  if c1 != None and c2 != None:
    return c1 + " " + c2
  elif c1 == None:
    return c2
  else:
    return c1

combineUDF = udf(combine)

expr = [c for c in ["first_name", "last_name"]] + [combineUDF(col("first_name"), col("last_name")).alias("full_name")]

tbl.select(*expr).show()

#+----------+---------+------------+
#|first_name|last_name|   full_name|
#+----------+---------+------------+
#|     Alice|   Cooper|Alice Cooper|
#|    Prince|     null|      Prince|
#|      null|    Lenon|       Lenon|
#+----------+---------+------------+

rawDescription: Selecting values from non-null columns in a PySpark DataFrame


code:
from ib.opt import ibConnection, Connection, message
from ib.ext.Contract import Contract
from ib.ext.EWrapper import EWrapper
from ib.ext.EClientSocket import EClientSocket

def reply_handler(msg):
    print("Reply:", msg)

tws_conn = ibConnection(host='localhost', port=7496, clientId=1)
tws_conn.registerAll(reply_handler)
tws_conn.connect()

contract = Contract()
contract.m_symbol = 'GE'
contract.m_exchange = 'SMART'
contract.m_currency = 'USD'
contract.m_secType = 'STK'

#EClientSocket only methods
reply_handler(tws_conn.isConnected())
reply_handler(tws_conn.serverVersion())
reply_handler(tws_conn.TwsConnectionTime())

#EWrapper methods or calls to EWrapper methods
tws_conn.reqCurrentTime()
tws_conn.reqContractDetails(1, contract)
#tws_conn.disconnect()#don't forget at some point

rawDescription: IBpy Ewrapper methods don't work only EClientSocket


rawDescription: Convert a complex string to a list in python


code:
import win32com.client
import pywintypes

o = win32com.client.Dispatch("Outlook.Application")
ns = o.GetNamespace("MAPI")
profile = ns.Folders.Item("Profile Name")
contacts = profile.Folders.Item("Contacts")

rawDescription: Fetching Outlook Contacts with Python

description: Fetching Outlook Contacts with Python


code:
def apply_to_some(mylist, condition, function):
    return [function(x) if condition(x) else x for x in mylist]

rawDescription: Apply function to some values in list

description: Apply function to some values in list


code:
req = resource.request()
yield req
# do stuff
resource.release(req)

rawDescription: Simpy 3: Resources.Resource.request()/.release() WITHOUT 'with...as:'


code:
def ungzipResponse(r,b):
    headers = r.info()
    if headers['Content-Encoding']=='gzip':
        import gzip
        gz = gzip.GzipFile(fileobj=r, mode='rb')
        html = gz.read()
        gz.close()
        headers["Content-type"] = "text/html; charset=utf-8"
        r.set_data( html )
        b.set_response(r)

response = browser.open(url)
ungzipResponse(response, browser)
html = response.read()

rawDescription: Encoding problem downloading HTML using mechanize and Python 2.6


code: all(x%5 == 0 for x in l)

rawDescription: Check if all the elements in that list are divisible by some given integer or not


code:
import shapely.geometry as sg
import shapely.ops as so
import matplotlib.pyplot as plt

#constructing the first rect as a polygon
r1 = sg.Polygon([(0,0),(0,1),(1,1),(1,0),(0,0)])

#a shortcut for constructing a rectangular polygon
r2 = sg.box(0.5,0.5,1.5,1.5)

#cascaded union can work on a list of shapes
new_shape = so.cascaded_union([r1,r2])

#exterior coordinates split into two arrays, xs and ys
# which is how matplotlib will need for plotting
xs, ys = new_shape.exterior.xy

#plot it
fig, axs = plt.subplots()
axs.fill(xs, ys, alpha=0.5, fc='r', ec='none')
plt.show() #if not interactive

rawDescription: Plot unions of polygons in matplotlib

description: Plot unions of polygons in matplotlib


code:
from PIL import Image

im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600) )

rawDescription: Programmatically change image resolution


code:
class MyClass:

    # Defining seedFrom as a function outside
    # the class is also an option. Defining it
    # as a class method is not, since you still
    # have the problem of not having a class to
    # pass as the first argument when it is time
    # to declare __init__
    @staticmethod
    def seedFrom():
        ...

    def __init__(self, seed=seedFrom()):
        ...

rawDescription: Access class in optional parameter initialization


code:
def ampm(value):
    for k,v in dict1.items():
        value = value.replace(k,v)
    return value

rawDescription: Using dictionary to modify list values

description: Using dictionary to modify list values


code:
def lookup(x, y, l):
    for xx in xrange(x-l, x+l+1):
        for yy in xrange(y-l, y+l+1):
            yield (xx, yy)
list(lookup(50, 50, 10))

rawDescription: Pythonic way to generate this list

description: Pythonic way to generate this list


rawDescription: Flatten inner list without changing outer list in python


code:
from PIL import Image, ImageChops

point_table = ([0] + ([255] * 255))

def black_or_b(a, b):
    diff = ImageChops.difference(a, b)
    diff = diff.convert('L')
    diff = diff.point(point_table)
    new = diff.convert('RGB')
    new.paste(b, mask=diff)
    return new

a = Image.open('a.png')
b = Image.open('b.png')
c = black_or_b(a, b)
c.save('c.png')

rawDescription: Comparing two images pixel-wise with PIL (Python Imaging Library)


code: hexhash = hashlib.sha512("some text").hexdigest()

rawDescription: To SHA512-hash a password in MySQL database by Python


code:
import Tkinter as tk

class TimedToplevel(object):
    def __init__(self, parent, milliseconds):
        self.milliseconds = milliseconds
        self.parent = parent
        self.parent.after(milliseconds, self.create_window)

    def create_window(self):
        self.top = tk.Toplevel(self.parent)
        label = tk.Label(self.top, text="Your time has expired")
        button = tk.Button(self.top, text="Ok", command=self.dismiss)
        button.pack(side="bottom")
        label.pack(side="top", fill="both", expand=True)

    def dismiss(self):
        self.top.destroy()

class Example(tk.Frame):
    def __init__(self, master):
        self.alerts = []
        tk.Frame.__init__(self, master)
        t = tk.Text(self, width=40, height=4, wrap="word")
        t.pack(side="top", fill="both", expand=True)
        t.insert("end", "type here, to see that you can type " +
                 "while the timers are running")
        for delay in (5,10,15):
            label = "Alert me in %s seconds" % delay
            b = tk.Button(self, text=label,
                          command=lambda ms=delay*1000: self.alert(ms))
            b.pack(side="top")

    def alert(self, delay):
        alert=TimedToplevel(self, delay)
        self.alerts.append(alert)

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

rawDescription: How can i create a GUI using Tkinter that can operate on a function and still accept input?

description: Create a GUI using Tkinter that can operate on a function and still accept input


code:
from itertools import izip

def areFilesIdentical(filename1, filename2):
    with open(filename1, "rtU") as a:
        with open(filename2, "rtU") as b:
            # Note that "all" and "izip" are lazy
            # (will stop at the first line that's not identical)
            return all(myprint() and lineA == lineB
                       for lineA, lineB in izip(a.xreadlines(), b.xreadlines()))

rawDescription: Best way to check new-line-independent-identity of 2 files with python

description: Check new-line-independent-identity of 2 files with python


code:
import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):

        self.lbl = QtGui.QLabel("Ubuntu", self)

        combo = QtGui.QComboBox(self)
        combo.addItem("Ubuntu")
        combo.addItem("Mandriva")
        combo.addItem("Fedora")
        combo.addItem("Red Hat")
        combo.addItem("Gentoo")

        combo.move(50, 50)
        self.lbl.move(50, 150)

        combo.activated[str].connect(self.onActivated)

        self.setGeometry(300, 300, 300, 200)
        self.setWindowTitle('QtGui.QComboBox')
        self.show()

    def onActivated(self, text):

        self.lbl.setText(text)
        self.lbl.adjustSize()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

rawDescription: How the comboBox works?

description: How the comboBox works


code:
tips = sns.load_dataset("tips")
tips["weight"] = 10 * np.random.rand(len(tips))

tips["tip_and_weight"] = zip(tips.tip, tips.weight)

def weighted_mean(x, **kws):
    val, weight = map(np.asarray, zip(*x))
    return (val * weight).sum() / weight.sum()

g = sns.factorplot("size", "tip_and_weight", data=tips,
                   estimator=weighted_mean, orient="v")
g.set_axis_labels("size", "tip")

rawDescription: How to use a weighted mean estimator in seaborn factor plot (incl bootstrapping)?

description: Use a weighted mean estimator in seaborn factor plot (incl bootstrapping)


rawDescription: Define Pandas DataFrame as composite boolean condition


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("filename", help="The filename to be processed")
args = parser.parse_args()
if args.filename:
    with open(filename) as f:
        for line in f:
            name, _ = line.strip().split('\t')
            print(name)

rawDescription: Reading from a file with sys.stdin in Pycharm


code:
class MyClass(object):
    ...
    def __add__(self, other):
        if isinstance(other, str):
            return str(self) + other
        ...
    def __radd__(self, other):
        if isinstance(other, str):
            return other + str(self)
        ...

rawDescription: Can I overload operators for builtin classes in Python?

description: Overload operators for builtin classes in Python


code: a, b = wholeString.split('..')

rawDescription: Breaking 1 String into 2 Strings based on special characters using python


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

fig, axs = plt.subplots(1,2)

x = np.linspace(0, 1, 100)
X, Y = np.meshgrid(x, x)
Z = np.sin(X)*np.sin(Y)

levels = np.linspace(-1, 1, 40)

zdata = np.sin(8*X)*np.sin(8*Y)

cs = axs[0].contourf(X, Y, zdata, levels=levels)
fig.colorbar(cs, ax=axs[0], format="%.2f")

cs = axs[1].contourf(X, Y, zdata, levels=[-1,0,1])
fig.colorbar(cs, ax=axs[1])

plt.show()

rawDescription: Contour graph in python

description: Contour graph in python


code:
for i, group in df.groupby("Studentid"):
    plt.figure()
    x=group['DateProgress']
    y=group['BookProgress']
    plt.scatter(x,y)

    plt.plot(range(4))
    plt.xlim(-.5, 3.5)
    plt.ylim(-.5, 3.5)
    plt.xlabel('DateProgress')
    plt.ylabel('BookProgress')

    plt.gca().set_aspect('equal', adjustable='box')

    plt.title(i) #Label each plot

    plt.show()

rawDescription: Create multiple plots from Pandas dataframe


code:
from django.db.models import Count
Post.objects.annotate(comment_count=Count('comment'))

rawDescription: Adding the number of comments to a queryset in Django


code:
while True:
    result = channel.basic.get(queue='simple_queue', no_ack=False)
    if result:
        print("Message:", result['body'])
        channel.basic.ack(result['method']['delivery_tag'])
    else:
        print("Channel Empty.")
        sleep(1)

rawDescription: Synchronous and blocking consumption in RabbitMQ using pika


code:
oldList[1,23,4,3,5,345,4]
newList = oldlist[:3] + oldList[4:]
doSomthingToList(newList)

rawDescription: Python: Copying a list and removing an element at the same time


code:
new_dict = {}
for k, d in old_dict.iteritems():
    if d['check'] == 0 and d['status'] == 0:
        new_dict[k] = d

rawDescription: Get new dict from old dict with condition?


code:
import tokenize
from io import BytesIO
from keyword import iskeyword
s = "def twoπ(a,b):"
g = tokenize.tokenize(BytesIO(s.encode("utf-8")).readline)
for toktype, tokval, st, end, _ in g:
  if toktype == tokenize.NAME and iskeyword(tokval):
    print ("KEYWORD ", tokval)
  else:
    print(toktype, tokval)

rawDescription: Parse Python Identifier

description: Parse Python Identifier


code:
# if func() is defined in `a.py`
import a

oldObj, a.Obj = a.Obj, MockObj
a.func()
a.Obj = oldObj

rawDescription: patching a local instance

description: Patching a local instance


code: orig = pd.concat([pd.Series(...) for i in li], axis=1)

rawDescription: pandas merge timeseries, concat/append/...?

description: Pandas merge timeseries, concat/append/...


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

from PyQt4 import QtCore, QtGui

class myDialog(QtGui.QDialog):
    _buttons = 0

    def __init__(self, parent=None):
        super(myDialog, self).__init__(parent)

        self.pushButton = QtGui.QPushButton(self)
        self.pushButton.setText(QtGui.QApplication.translate("self", "Add Button!", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.clicked.connect(self.on_pushButton_clicked)

        self.scrollArea = QtGui.QScrollArea(self)
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QtGui.QWidget(self.scrollArea)
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 247))
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)

        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.verticalLayout.addWidget(self.pushButton)
        self.verticalLayout.addWidget(self.scrollArea)

        self.verticalLayoutScroll = QtGui.QVBoxLayout(self.scrollAreaWidgetContents)

    @QtCore.pyqtSlot()
    def on_pushButton_clicked(self):
        self._buttons  += 1
        pustButtonName = u"Button {0}".format(self._buttons)

        pushButton = QtGui.QPushButton(self.scrollAreaWidgetContents)
        pushButton.setText(pustButtonName)

        self.verticalLayoutScroll.addWidget(pushButton)

if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('myDialog')

    main = myDialog()
    main.show()

    sys.exit(app.exec_())

rawDescription: PyQt: Put scrollbars in this

description: PyQt: Put scrollbars in this


code:
from collections import defaultdict

table = """\
Cycle Test_Time  Current    Voltage
1     7.80E-002 0.00E+000   1.21E-001
1     3.01E+001 0.00E+000   1.19E-001
1     6.02E+001 0.00E+000   1.17E-001
2     9.02E+001 0.00E+000   1.14E-001
2     1.20E+002 0.00E+000   1.11E-001
2     1.50E+002 0.00E+000   1.08E-001
2     1.80E+002 0.00E+000   1.05E-001
2     2.10E+002 0.00E+000   1.02E-001
3     2.40E+002 0.00E+000   9.93E-002
3     2.70E+002 0.00E+000   9.66E-002
3     3.00E+002 0.00E+000   9.38E-002
3     3.10E+002 4.00E-001   1.26E+000"""

# Split into rows
table = table.splitlines()

# Split each row into values
table = [row.split() for row in table]

# Associate the column names with their index
headers = table.pop(0)
H = {x: i for i, x in enumerate(headers)}
time_index = H["Test_Time"]
cycle_index = H["Cycle"]

# Sort by Test_Time
table.sort(key=lambda row: float(row[time_index]))

# Associate each test with its cycle
D = defaultdict(list)
for row in table:
  D[int(row[cycle_index])].append(row)

# Present the information
print(*headers, sep='\t')
print("Latest row for each cycle")
for cycle in sorted(D.keys()):
  tests = D[cycle]
  latest_test = tests[-1]
  print(*latest_test, sep='\t')

print("All rows for last 5 cycles")
for cycle in sorted(D.keys())[-5:]:
  tests = D[cycle]
  for test in tests:
    print(*test, sep='\t')

print("All rows for cycles 4 through 30")
for cycle in sorted(D.keys()):
    if 4 <= cycle <= 30:
      tests = D[cycle]
      for test in tests:
        print(*test, sep='\t')

rawDescription: How can I sort that table in Python on Linux

description: Sort that table in Python on Linux


code:
from xml.etree.ElementTree import ElementTree
tree = ElementTree()
tree.parse("your_xml_file.xml")

rawDescription: Extracting Data from a .txt file using python


code:
local_stat = os.stat(localpath)
times = (local_stat.st_atime, local_stat.st_mtime)

sftp.put(localpath, remotepath)

sftp.utime(remotepath, times)

rawDescription: Preserve timestamp with Paramiko

description: Preserve timestamp with Paramiko


code: markerline, stemlines, baseline = stem(x, cos(x), '-')

rawDescription: plotting stem with a continuous line

description: Plotting stem with a continuous line


code:
x,y=meshgrid(linspace(0,1),linspace(0,1))
fig=plt.figure()
a=fig.add_subplot(111,axisbg='gray')
z=ma.masked_array(x**2-y**2,mask=y>-x+1)
a.contourf(z)

rawDescription: Set color to be used for an array masked values plotted with matplotlib contourf


code:
import socket

timeout = 10
socket.setdefaulttimeout(timeout)

rawDescription: HTTP Request Timeout

description: HTTP Request Timeout


rawDescription: Python pandas concatenate: join="inner" works on toy data, not on real data


code:
def distance(self, tile1, tile2):
    self.xDist = abs(tile1.col * TILE_SIZE - tile2.col * TILE_SIZE)
    self.yDist = abs(tile1.row * TILE_SIZE - tile2.row * TILE_SIZE)
    self.totalDist = self.diagCost * math.sqrt(self.xDist*self.xDist + self.yDist*self.yDist)
                     # or it might be self.straightCost, depending on their values.
                     # self.diagCost is probably right, though.
    return self.totalDist

rawDescription: Python A* algorithm not searching appropriately


code:
from collections import Counter
list_of_friends = ['bill','bill','mark','jenna','brad','mark']
S = set(list_of_friends)
bag = Counter(list_of_friends)
S.difference(bag - Counter(S))

rawDescription: Python 2.7: Get all unique values from a list, removing any value that is or has been repeated


code:
import sqlite3 as lite
...
cur = self.SQLiteDB.cursor()
cur.execute("SELECT * FROM SQLTable")
allSQLRows= cursor.fetchall()
self.myTableWidget.setRowCount(len(allSQLRows)) ##set number of rows
self.myTableWidget.setColumnCount(8) ##this is fixed for myTableWidget, ensure that both of your tables, sql and qtablewidged have the same number of columns

row = 0
while True:
    sqlRow = cur.fetchone()
    if sqlRow == None:
        break ##stops while loop if there is no more lines in sql table
    for col in range(0, 8): ##otherwise add row into tableWidget
        self.myTableWidget.setItem(row, col, QtGui.QTableWidgetItem(sqlRow[col]))
    row += 1

rawDescription: How to display data from mysql database in table using pyqt?

description: Display data from mysql database in table using pyqt


code:
import logging
from datetime import datetime

logger = logging.getLogger(__name__)

def printHello():
    print('Hello')
    logger.info('printed hello')
    printGoodbye()

def printGoodbye():
    print('Goodbye')
    logger.info('printed goodbye')

if __name__ == "__main__":
    FORMAT = '%(name)s - %(levelname)s - %(message)s'
    logging.basicConfig(filename='testlogger.log', level=logging.DEBUG, format=FORMAT)
    logger.info(datetime.now().strftime('%H:%M %d-%m-%Y.log'))
    logger.info('About to call function printHello')
    printHello()
    logger.info('Now exiting')

rawDescription: Am I using logging correctly?

description: Am I using logging correctly


code:
import tkinter as tk

class ResizableText:
    def __init__(self, text_max_width=20):
        self.text_width = text_max_width
        self.root = tk.Tk()

        self.text = tk.Text(self.root, width=self.text_width, height=1)
        self.text.pack(expand=True)

        self.text.bind("<Key>", self.check_key)
        self.text.bind("<KeyRelease>", self.update_width)

        self.root.mainloop()

    def check_key(self, event):
        # Ignore the 'Return' key
        if event.keysym == "Return":
            return "break"

    def update_width(self, event):
        # Get text content; ignore the last character (always a newline)
        text = self.text.get(1.0, tk.END)[:-1]
        # Calculate needed number of lines (=height)
        lines = (len(text)-1) // self.text_width + 1
        # Apply changes on the widget
        self.text.configure(height=lines)

rawDescription: Single line text entry UI element that word-wraps text in Tkinter?


code:
import re
s = 'This is a cake. It is 1.45 dollars and 2.38 kg.'
re.sub(r'(?<=\d)\.(?=\d)', '<replace>', s)

rawDescription: Special Case to Replace String Using Python with Regex


code:
from datetime import date, timedelta
...
n = (next_day - d.weekday()) % 7 # mod-7 ensures we don't go backward in time
next_run_date = d + timedelta(days=n)

rawDescription: Python: Given a Date and Weekday find the date of the next occurrence of a given weekday


code:
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon

fig = plt.figure()
ax = fig.add_subplot(111, aspect='equal')

triangle1 = Polygon(((0.05,0.1), (0.396,0.1), (0.223, 0.38)),
                    fc=(1,0,0,0.5), ec=(0,0,0,1), lw=2)
triangle2 = Polygon(((0.2,0.2), (0.5,0.4), (0.3, 0.6)),
                    fc=(1,0,0,0.5), ec=(0,0,0,1), lw=2)

ax.add_artist(triangle1)
ax.add_artist(triangle2)

plt.show()

rawDescription: How can I set different opacity of edgecolor and facecolor of a patch in Matplotlib

description: Set different opacity of edgecolor and facecolor of a patch in Matplotlib


code: thelist = [int(s) for s in thestring.split()]

rawDescription: Convert string values to list python

description: Convert string values to list python


rawDescription: Scipy rankdata reverse highest to lowest


code:
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo

class App(Frame):
    def __init__(self, master):
        Frame.__init__(self, master)
        self.filename = None
        button1 = Button(self, text="Browse for a file", command=self.askfilename)
        button2 = Button(self, text ="Count the file", command=self.word_count)
        button3 = Button(self, text="Exit", command=master.destroy)
        button1.grid()
        button2.grid()
        button3.grid()
        self.grid()
    def askfilename(self):
        filename = askopenfilename()
        if not filename.endswith(('.txt', '.py', '.java')):
            showwarning('Are you trying to annoy me?', 'How about giving me a TEXT or SOURCE CODE file, genius?')
        else:
            self.filename = filename
    def word_count(self):
        if self.filename:
            with open(self.filename) as f:
                n_lines = 0
                n_words = 0
                for line in f:
                    n_lines += 1
                    n_words += len(line.split())
            showinfo('Result', 'Your file has {} lines, and {} words'.format(n_lines, n_words))
        else:
            showwarning('No file selected', 'Select a file first')

root = Tk()
root.title("Word Counter")
root.geometry("500x500")
app = App(root)
root.mainloop()

rawDescription: How to make buttons call functions

description: Make buttons call functions


code:
s = "POINT (174.29635 -41.60557)"
longitude, latitude = map(float, s[7:-1].split())

rawDescription: most Pythonic way of achieveing these 4 lines?

description: Most Pythonic way of achieveing these 4 lines


code:
import uuid
str(uuid.uuid4()) + ".txt"

rawDescription: Use uuid.uuid4() to create new file

description: Use uuid.uuid4() to create new file


code:
import matplotlib.pyplot as plt
from sklearn import linear_model

# Funky hack to change the number of "stars" in the legend to be 1,
# I feel like there has to be a better way to do this...
from pylab import *
rcParams['legend.numpoints'] = 1

x = np.r_[0.,3.15,5.39,7.29,11.55,14.4,19.8,23.4,25.7]
y = np.r_[0,0.13,0.2,0.27,0.42, 0.51,0.68,0.80,0.88]
model = linear_model.LinearRegression()
X = x.reshape(-1,1)
Y = y.reshape(-1,1)
model.fit(X,Y)

blue_dot, = plt.plot(x,y, '*')
green_line, = plt.plot(x,model.predict(X), 'g')

plt.legend([(blue_dot, green_line)], ["DesiredKey"], loc='upper center')
plt.show()

rawDescription: Overlap legends in matplotlib

description: Overlap legends in matplotlib


code: map(list.__add__, L1, L2)

rawDescription: How to zip two lists of lists in Python?

description: Zip two lists of lists in Python


code:
plt.plot(x,y2, 'o', ms=14, markerfacecolor="None",
         markeredgecolor='red', markeredgewidth=5)

rawDescription: Plotting with a transparent marker but non-transparent edge


code:
def gcs_write_blob(dyn, blob):
    """ update google cloud storage dyn entity """

    gcs_file_name = '/%s/%s' % (default_bucket, dyn.filename)

    content_type = mimetypes.guess_type(dyn.filename)[0]
    if dyn.extension in ['js', 'css']:
        content_type += b'; charset=utf-8'

    with gcs.open(gcs_file_name, 'w', content_type=content_type,
                  options={b'x-goog-acl': b'public-read'}) as f:
        f.write(blob)

    return gcs_file_name

rawDescription: How can I set the MIME type upon writing a file to google cloud storage

description: Set the MIME type upon writing a file to google cloud storage


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

# dummy data
data2 = np.random.randint(low=0,high=450,size=200)

bins = [100,110,120,130,140,150,160,180,200,250,300,350,400]
binwidths = np.diff(bincenters)
binvals = [np.sum(data2>=thresh) for thresh in bins[:-1]]

plt.figure()
plt.bar(bins[:-1],binvals,width=binwidths,alpha=0.4)
plt.xlabel('threshold')
plt.ylabel('occurences')

rawDescription: Matplotib histogram - ploting values greatar then a given value


code:
# UNTESTED CODE, may contain a bug or two; also, you need to decide how to
# implement split_words
datareader = csv.reader(csvfile)
dicts = []
y = []

for row in datareader:
    y.append(row[-1])
    d = {"From": row[0]}
    for word in split_words(row[1]):
        d["Subject_" + word] = 1
    for word in split_words(row[2]):
        d["Body_" + word] = 1
    # etc.
    dicts.append(d)

# vectorize!
vectorizer = DictVectorizer()
X_train = vectorizer.fit_transform(dicts)

rawDescription: Machine Learning Email Prioritization - Python


code:
fn = 0
pltkw = {
    'actionable': {},
    'causes': {},
    'components': {},
    'labels': {},
}

for figure in ['actionable', 'causes', 'components', 'labels']:
    # get an axes and figure
    fig_, ax = plt.subplots(1, 1, figsize=(18, 10))
    # I assume you don't _really_ care about the figure numeber, but are using this
    # to get around the global state

    fig[figure] = fig_
    axs[figure] = ax
    frm[figure] = pd.DataFrame(data[figure], index=date_range)
    frm[figure].plot(ax=ax, **pltkw[figure])
    # pandas really should return the artists added, but oh well
    txs[figure] = ax.set_title(figure)
    yls[figure] = ax.set_ylabel('events')

figure = 'times'
fig_, ax = plt.subplots(1, 1, figsize=(18, 10))
frm[figure] = pd.DataFrame(data[figure])
axs[figure] = ax
ax.scatter(frm[figure]['hour'], frm[figure]['day'])
txs[figure] = ax.set_title(figure)
xls[figure] = plt.set_xlabel('hour 0-23')
# not sure this is really doing what you want, but ok
yls[figure] = plt.set_ylabel('mon,tue,wed,thu,fri,sat,sun')

plt.show()

rawDescription: matplotlib generating multiple figures, 1 blank, 1 wrong, fencepost error?

description: Matplotlib generating multiple figures, 1 blank, 1 wrong, fencepost error


code:
def colour():
    red = []
    for x in range(getWidth(img)):
        for y in range (getHeight(img)):
            r= getPixel(img,x,y)
            red.append(r)
    return red

rawDescription: Need help on returning a list in a function in python


code:
def main():
    import SHT1x
    logger.info("SERVER START")
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    termo = SHT1x.somefunction(dataPin, clkPin, SHT1x.GPIO_BOARD) #this one configures the log also

if __name__ == "__main__":
    import logging
    logging.basicConfig(filename='/home/pi/thermostat/server.log',
                        level=logging.DEBUG,
                        format='%(asctime)s %(message)s')
    logger = logging.getLogger(__name__)

    main()

rawDescription: Overwriting python log config

description: Overwriting python log config


code:
class myclass:

   def display(self, jobid):   #Self here is referencing my class
       zoom = NavigationToolbar2.zoom
       def new_zoom(toolbar, *args, **kwargs):
          ...
       NavigationToolbar2.zoom = new_zoom

rawDescription: Python - Class Scope

description: Python - Class Scope


code:
if 'campaign_id_crid' in request.args:
    pass # do something

rawDescription: Checking for the existence of a key in request.args in Flask


code:
def removesingleton(s, ch):
    '''
    (str,str)->(str)
    Returns a copy of s with all non-repeated instances of ch removed
    >>>removesingleton("Welcomee","e")
    Wlcomee
    >>>removesingleton("XabXXaX","X")
    abXXa
    '''
    return "".join(c for i, c in enumerate(s)     # enumerate gives us our index
                   if c != ch or          # keep any of:  non-matching characters
                      (i > 0 and s[i-1] == ch) or       # previous character was the same
                      (i < len(s)-1 and s[i+1] == ch))  # next character is the same

rawDescription: Remove Single Char in String Python 2.7

description: Remove Single Char in String Python 2.7


code:
import pandas as pd
pd.__version__
np.__version__
# DataFrame has copy method
bar = foo.iloc[3:5,1:4]
bar == foo.iloc[3:5,1:4] == foo_copy.iloc[3:5,1:4]
# Changing the view
# View and DataFrame still equal
# It is now different from a copy of original

rawDescription: Pandas: Subindexing dataframes: Copies vs views


code:
CoreFoundation = CDLL('/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation')

CoreFoundation.CFDictionaryCreateMutable.restype = OpaquePtr
CoreFoundation.CFStringCreateWithBytes.restype = OpaquePtr
def CFDictionaryAddStringKeyValue(d, k, v):
    ck = CoreFoundation.CFStringCreateWithBytes(None, k, len(k), 0, 0)
    cv = CoreFoundation.CFStringCreateWithBytes(None, v, len(v), 0, 0)
    CoreFoundation.CFDictionaryAddValue(d, ck, cv)
    CoreFoundation.CFRelease(ck)
    CoreFoundation.CFRelease(cv)

query = CoreFoundation.CFDictionaryCreateMutable(None, 0, None, None)
CFDictionaryAddStringKeyValue(query, "foo", "bar")

rawDescription: calling secitemcopymatching in python

description: Calling secitemcopymatching in python


code:
color_marker = [(c, m) for c in "rgbc" for m in "123"] #assuming there are 12 types of pizza
for cm, i, target_name in zip(color_marker, range(12), pizza_names):
    pl.scatter(X_r[pizza_target == target_name, 0], X_r[pizza_target == target_name, 1], c=cm[0], marker=cm[1], label=target_name)

rawDescription: How do I work with external data in scikit to perform PCA/LDA?

description: Work with external data in scikit to perform PCA/LDA


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

#-- Generate Data -----------------------------------------
# Using linspace so that the endpoint of 360 is included...
azimuths = np.radians(np.linspace(0, 360, 100))
zeniths = np.arange(0, 70, 10)
r, theta = np.meshgrid(zeniths, azimuths)
values1 = np.random.random((azimuths.size, zeniths.size))
values2 = np.random.random((azimuths.size, zeniths.size))

#-- Plot... ------------------------------------------------
fig, axs = plt.subplots(1, 2, figsize=(12,5),subplot_kw=dict(projection='polar'))
p1 = axs[0].contourf(theta, r, values1, 100)
p2 = axs[1].contourf(theta, r, values2, 100)

#-- obtaining the colormap limits
vmin,vmax = p2.get_clim()
#-- Defining a normalised scale
cNorm = mpl.colors.Normalize(vmin=vmin, vmax=vmax)
#-- Creating a new axes at the right side
ax3 = fig.add_axes([0.9, 0.1, 0.03, 0.8])
#-- Plotting the colormap in the created axes
cb1 = mpl.colorbar.ColorbarBase(ax3, norm=cNorm)
fig.subplots_adjust(left=0.05,right=0.85)
plt.show()

rawDescription: Adding a colorbar to a polar contourf multiplot


code:
[' '.join(item) for item in h.iteritems()]
map(' '.join, h.iteritems())

rawDescription: Is there a shorter code to join the keys and values for a dictionary?


rawDescription: Does pycassa's ColumnFamily.get_indexed_slices return a generator?


rawDescription: Realize padding and rounding up values via awk out of a python script with subprocess


code: dict: {'fixed_allowance_ids': [{'allowance_id': 1, 'allowance_name': 1}, {'allowance_id': 2, 'allowance_name': 2}]}

rawDescription: OpenERP one2many and one2many

description: OpenERP one2many and one2many


code:
if temp.upper() != "END":
    s += temp
    return getpayload(s)

rawDescription: Function returning None instead of expected string


code:
import platform
platform.machine()
platform.version()
platform.platform()
platform.uname()
platform.system()
platform.processor()

rawDescription: How to get the system info with Python?

description: Get the system info with Python


code:
directory = session.nlst()
print(directory)

rawDescription: Python 3 FTPLIB, NoneType Errors, and Uploads/Downloads


code:
def SetFont(self, *args, **kwargs):
        """ Set the font, then recalculate control size, if appropriate. """
        wx.TextCtrl.SetFont(self, *args, **kwargs)
        if self._autofit:
##            dbg('calculated size:', self._CalcSize())
            self.SetClientSize(self._CalcSize())
            width = self.GetSize().width
            height = self.GetBestSize().height
##            dbg('setting client size to:', (width, height))
            self.SetInitialSize((width, height))

rawDescription: Looking for a way to change width of wx.lib.masked.TimeCtrl instances (wxPython on windows)


code: {key: (value1, dict2[key]) for key, value1 in dict1.iteritems() if key in dict2}

rawDescription: Union of python dicts while keeping tuple of values


code:
# [in the plugin file]
from code import Model, instance

class MyModel(Model):
    def function1(*args, **kwargs):
        """
        Work to do by this model
        """
        # some specific model-dependent work
        return

newmodel = MyModel(a="a name", b="some other stuff")
instance.register(newmodel)

rawDescription: Redefinition of class method in python

description: Redefinition of class method in python


rawDescription: Python: list.extend without mutating original variable


code:
def clean_string(s):
    return ''.join(c for c in s if c != '7')

class Court(object):
    def __init__(self):
        self.docket_numbers = ["a1", "b277"]
        self.dispositions = ["happy", "sad77"]
    def clean(self):
        for item in [self.docket_numbers, self.dispositions]:
            if item is not None:
                item[:] = [clean_string(sub_item) for sub_item in item]

C = Court()
vars(C)
C.clean()
vars(C)

rawDescription: Iterate over object properties and change them?


code: dataList.sort(key=lambda x: x[1])

rawDescription: How to keep a list of lists sorted as it is created

description: Keep a list of lists sorted as it is created


code:
def cumulative_ols(
                   data_frame,
                   lhs_column,
                   rhs_column,
                   date_column,
                   min_obs=60,
                  ):
    """
    Function to perform a cumulative OLS on a Pandas data frame. It is
    meant to be used with `apply` after grouping the data frame by categories
    and sorting by date, so that the regression below applies to the time
    series of a single category's data and the use of `cumsum` will work
    appropriately given sorted dates. It is also assumed that the date
    conventions of the left-hand-side and right-hand-side variables have been
    arranged by the user to match up with any lagging conventions needed.

    This OLS is implicitly univariate and relies on the simplification to the
    formula:

    Cov(x,y) ~ (1/n)*sum(x*y) - (1/n)*sum(x)*(1/n)*sum(y)
    Var(x)   ~ (1/n)*sum(x^2) - ((1/n)*sum(x))^2
    beta     ~ Cov(x,y) / Var(x)

    and the code makes a further simplification be cancelling one factor
    of (1/n).

    Notes: one easy improvement is to change the date column to a generic sort
    column since there's no special reason the regressions need to be time-
    series specific.
    """
    data_frame["xy"]         = (data_frame[lhs_column] * data_frame[rhs_column]).fillna(0.0)
    data_frame["x2"]         = (data_frame[rhs_column]**2).fillna(0.0)
    data_frame["yobs"]       = data_frame[lhs_column].notnull().map(int)
    data_frame["xobs"]       = data_frame[rhs_column].notnull().map(int)
    data_frame["cum_yobs"]   = data_frame["yobs"].cumsum()
    data_frame["cum_xobs"]   = data_frame["xobs"].cumsum()
    data_frame["cumsum_xy"]  = data_frame["xy"].cumsum()
    data_frame["cumsum_x2"]  = data_frame["x2"].cumsum()
    data_frame["cumsum_x"]   = data_frame[rhs_column].fillna(0.0).cumsum()
    data_frame["cumsum_y"]   = data_frame[lhs_column].fillna(0.0).cumsum()
    data_frame["cum_cov"]    = data_frame["cumsum_xy"] - (1.0/data_frame["cum_yobs"])*data_frame["cumsum_x"]*data_frame["cumsum_y"]
    data_frame["cum_x_var"]  = data_frame["cumsum_x2"] - (1.0/data_frame["cum_xobs"])*(data_frame["cumsum_x"])**2
    data_frame["FactorBeta"] = data_frame["cum_cov"]/data_frame["cum_x_var"]
    data_frame["FactorBeta"][data_frame["cum_yobs"] < min_obs] = np.NaN
    return data_frame[[date_column, "FactorBeta"]].set_index(date_column)
### End cumulative_ols

rawDescription: Cumulative OLS with Python Pandas

description: Cumulative OLS with Python Pandas


code: status = models.ForeignKey(Status, related_name='+')

rawDescription: Auto-populate Django Model field

description: Auto-populate Django Model field


code:
class File(Base):
    __tablename__ = 'Files'
    id = Column(Integer, primary_key=True)
    title = ...

class Package(Base):
    __tablename__ = 'Package'

    id = Column(Integer, primary_key=True)
    file_id = Column(Integer, ForeignKey('Files.id'), nullable=False)
    title = ...

    file = relationship('File', backref='packages')

file1 = File(title='...')
# notice that we can now link the package to the file without having to think
# about ids - we're thinking in terms of the _objects_
pkg1 = Package(title='pkg1', file=file1)

# file1 will be automatically added
session.add(pkg1)

rawDescription: SQLAlchemy and Insertion with joins

description: SQLAlchemy and Insertion with joins


code:
if form.is_valid():
    object = form.save(commit=False)
    if request.user.is_authenticated():
        object.user = request.user
    object.save()

rawDescription: Saving additional information to the database conditionally when using a modelfrom in django


code:
s = 'hello, today!'
n = 3
[s[i::n] for i in range(n)]

rawDescription: Split string into blocks based on certain length


code:
import matplotlib.pyplot as plt ; plt.style.use('ggplot')
import numpy as np

# define the parameters
g = -9.81 # m/s^2
dZ = 60 # m
dx = 2e5 # m
omega = 7.2921e-5 # rad/s
phi = np.linspace(10,40) # deg
f = 2 * omega * np.sin(np.radians(phi)) # coriolis frequency, s^-1

# compute geostrophic wind, x-component
u_g = -1. * g/f * dZ/dx

# plot phi vs V_g
fig, ax = plt.subplots()
ax.plot(phi, u_g)
ax.set_xlabel('latitude (degrees)')
ax.set_ylabel('geostrophic wind, y-component (m/s)')
plt.show()

rawDescription: Plotting geostrophic wind plot in matplotlib


code:
if 'wsgi.file_wrapper' in environ:
    return environ['wsgi.file_wrapper'](filelike, block_size)
else:
    return iter(lambda: filelike.read(block_size), '')

rawDescription: Returning a file to a WSGI GET request

description: Returning a file to a WSGI GET request


code:
def make_account(key_name=None, **k):
    if key_name is None:
        raise ValueError('Must specify key_name for account!')
    return _Account(key_name=key_name, **k)

rawDescription: How to require a key_name when creating model classes with App Engine?

description: Require a key_name when creating model classes with App Engine


code:
import xml.etree.ElementTree as ET
def to_parseable(tree):
    t = ET.tostring(tree)
    t = t.lower()
    return ET.fromstring(t)

rawDescription: Case Insensitive findall in Python ElementTree


code:
class HttpResponseSeeOther(HttpResponseRedirect):
    status_code = 303

return HttpResponseSeeOther('/other-url/')

rawDescription: How to produce a 303 Http Response in Django?

description: Produce a 303 Http Response in Django


code:
df['flag'] = df.TimeSecO.map(lambda sec: (sec > df.TimeSecOT).values)
df['CumAmountBuy'] = df.flag.map(lambda f: np.dot(f,df['Size']*(df['Side']=='BUY')))
df['CumAmountSell'] = df.flag.map(lambda f: np.dot(f,df['Size']*(df['Side']=='SELL')))
df[['CumAmountBuy','CumAmountSell']]

rawDescription: conditional cumulative sum based on column comparison in pandas dataframe

description: Conditional cumulative sum based on column comparison in pandas dataframe


code:
import matplotlib as mpl
mpl.use("pgf")
pgf_with_pdflatex = {
    "pgf.texsystem": "pdflatex",
    "pgf.preamble": [
        r"\usepackage[author={me}]{pdfcomment}",
    ]
}
mpl.rcParams.update(pgf_with_pdflatex)

import matplotlib.pyplot as plt
plt.figure(figsize=(4.5,2.5))
plt.plot(range(5))

for i in range(5):
    plt.text(i,i,r"\pdftooltip{o}{(%d,%d)}"%(i,i))

plt.savefig("tooltips.pdf")

rawDescription: Create pdf with tooltips in python

description: Create pdf with tooltips in python


code:
import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_axes([1,1,1,1])
plt.plot([1,2])

savefig('test.png', bbox_inches='tight')

rawDescription: Matplotlib: Save figure as file from iPython notebook


code:
class IterEncoder(json.JSONEncoder):
    def default(self, obj):
        try:
            return list(obj)
        except TypeError:
            return super().default(obj)

class ComplexEncoder(IterEncoder):
    def default(self, obj):
        if isinstance(obj, complex):
            return [obj.real, obj.imag]

        return super().default(obj)

rawDescription: Specializing JSON object encoding with python 3


code:
import xml.etree.ElementTree as ET
root = ET.fromstring('''
<root>
    <first></first>
    <second></second>
    <third></third>
</root>
''')
new = ET.Element('new')
root.insert(2, new)  # <-----------
print(ET.tostring(root))

rawDescription: Python ElementTree: How to add SubElement at VERY specific position?


code:
labels, numL = label(array)
label_indices = [(labels == i).nonzero() for i in xrange(1, numL+1)]

rawDescription: Identify contiguous regions in 2D numpy array


code:
def get_latest_report():
    """ Returns the latest report from each app """
    return [app.report_set.latest('date') for app in App.objects.all()]

rawDescription: Filter latest record in Django

description: Filter latest record in Django


code:
from itertools import tee, izip, combinations

def partitions(items, k):
    N = len(items)

    def pairwise(iterable):  # Taken from itertools recipies
        a, b = tee(iterable)
        next(b, None)
        return izip(a, b)

    def applyPart(part, items):
        lists = []
        for l,h in pairwise([0] + part + [N]):
            lists.append(items[l:h])
        return lists

    for part in combinations(range(1, N), k - 1):
        yield applyPart(list(part), items)

rawDescription: Iterating over partitions in Python

description: Iterating over partitions in Python


code:
import re
lookup = {re.search('([A-Z][A-Za-z_]*$)', cls.__name__).group(1): cls for cls in BaseCls.__subclasses__()}
# {'A': <class '__main__.ChildClsA'>, 'B': <class '__main__.ClildClsB'>}

input_str = raw_input('Please enter one of {}:'.format(', '.join(lookup)))
new_class = lookup[input_str]()

rawDescription: How to decide creating a child class without using 'if' statement

description: Decide creating a child class without using 'if' statement


code: text.delete(1.0, END)

rawDescription: How to get rid of everything in a text widget

description: Get rid of everything in a text widget


code:
csfont = {'fontname':'Comic Sans MS'}
hfont = {'fontname':'Helvetica'}

plt.title('title',**csfont)
plt.xlabel('xlabel', **hfont)
plt.show()

rawDescription: How to change fonts in matplotlib (python)?

description: Change fonts in matplotlib (python)


code:
from django.contrib.auth.models import User
from django.contrib.auth import authenticate
u = User.objects.get(username='tester')
u.set_password('yolo')
u.save()
should_be_tester = authenticate(username='tester', password='yolo')
print(should_be_tester)

rawDescription: Django authenticate does not register a user


code:
def f():
  global num
  num += 1
dis.dis(f)

rawDescription: Why does Python provide locking mechanisms if it's subject to a GIL?


code:
from collections import defaultdict

index = defaultdict(list)

for line in file:
    # ...code that loops through file finding ID lines...
    index[id_value].append((fileposn,length))

rawDescription: Python: compare list items to dictionary keys twice in one for loop?


code:
cpy = [100, 89, 84, 73, 65, 6, 0]
days = ['Day 1','Day 2','Day 3','Day 6','Day 9','Day 14','Day 16']
df = pd.DataFrame({'day':days,'val':cpy})
df['dayint'] = df.day.apply(lambda x : int(x.split(' ')[-1]))
df.set_index(df.dayint, inplace=True)
df.val.plot()
plt.show()

rawDescription: How can I evenly space date data from a data frame?

description: Evenly space date data from a data frame


code:
import math, random

def updatebins(bins, binsize, x):
    i = math.floor(x / binsize)
    if i in bins:
        bins[i] += 1
    else:
        bins[i] = 1

def finalizebins(bins, binsize):
    imin = min(bins.keys())
    imax = max(bins.keys())
    a = [0] * (imax - imin + 1)
    b = [binsize * k for k in range(imin, imax + 1)]
    for i in range(imin, imax + 1):
        if i in bins:
            a[i - imin] = bins[i]
    return a, b

# A test with a mixture of gaussian distributions

def check(n):
    bins = {}
    binsize = 5.0
    for i in range(n):
        if random.random() > 0.5:
            x = random.gauss(100, 50)
        else:
            x = random.gauss(-200, 150)
        updatebins(bins, binsize, x)
    return finalizebins(bins, binsize)

a, b = check(10000)

# This must be 10000
sum(a)

# Plot the data
from matplotlib.pyplot import *
bar(b,a)
show()

rawDescription: Monte Carlo Simulation with Python: building a histogram on the fly


code:
(('-c', '--no-color'), dict(action='store_false', dest='color',
     help="Disable the use of ANSI color escapes.")),

rawDescription: terminal output from Python's behave package

description: Terminal output from Python's behave package


code:
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
import numpy as np

fig, ax = plt.subplots()
plt.subplots_adjust(bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
s = np.sin(5*np.pi*t)
l, = plt.plot(t,s)

viewwindow = 0.1

axpos = plt.axes([0.25, 0.1, 0.65, 0.03])
axslider = Slider(axpos, '', t[0]+viewwindow, t[-1]-viewwindow, valinit=(t[-1]+t[0])/2.0)

def update(val):
    new_pos = axslider.val
    ax.set_xbound(new_pos-viewwindow, new_pos+viewwindow)
    fig.canvas.draw_idle()

update(axslider.val)
axslider.on_changed(update)

plt.show()

rawDescription: Matplotlib plot with scalable/updatable x-axis


code:
#cheerypy.config is flat dictionary that you can modify using
cherrypy.config.update({'server.socket_host': '10.10.10.10',
                        'server.socket_port': 80,
                       })

rawDescription: Where is the Cherrypy config file located on Ubuntu?


rawDescription: Split line by +-*/() (as delimeters) without omitting them in python


code: GridSearchCV(clf, param_grid, cv=cv, scoring='accuracy', verbose=10)

rawDescription: Sklearn, gridsearch: how to print out progress during the execution?


code:
from copy import deepcopy

def algo (X):
    X = deepcopy(X)
    ...

rawDescription: Why is my (local) variable behaving like a global variable?


code:
df = pd.DataFrame(np.random.random((3,3)))
df
df2 = pd.DataFrame(np.random.random((3,1)))
df2
df[3] = df2
df.index = ['a', 'b', 'c']
df[4] = df2
df[5] = df2.values
df

rawDescription: Added column to existing dataframe but entered all numbers as NaN


code: max(enumerate(props), key = lambda tup: len(tup[1]))

rawDescription: How do I get the index of the largest list inside a list of lists using Python?

description: Get the index of the largest list inside a list of lists using Python


code:
class StrictMySQLDatabase(MySQLDatabase):
    def _connect(self, database, **kwargs):
        conn = super(StrictMySQLDatabase, self)._connect(database, **kwargs)
        cursor = conn.cursor()
        cursor.execute("SET SESSION sql_mode = 'STRICT_ALL_TABLES';")
        return conn

rawDescription: Set strict sql mode in peewee

description: Set strict sql mode in peewee


code:
start = len(entry.get()) - 1
entry.delete(start, END)

rawDescription: tkinter entry assistance Python3

description: Tkinter entry assistance Python3


code:
def func(prefix):
    g = globals()
    g[prefix + '_tomato'] = 123
    g[prefix + '_broccoli'] = 456

rawDescription: How do I create a function that will create a global (and assign it a value) with a name that contains the name of the argument given to it?

description: Create a function that will create a global (and assign it a value) with a name that contains the name of the argument given to it


rawDescription: How do you split a string in Python with multiple delimiters?

description: Split a string in Python with multiple delimiters


code:
import wx

SAVE_DATA = wx.NewId()
OPEN_DATA = wx.NewId()
CONFIGURE = wx.NewId()

class myFrame(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,'Menu', size=(300,200))
        panel = wx.Panel(self)

        status     = self.CreateStatusBar()
        menubar    = wx.MenuBar()
        firstMenu  = wx.Menu()
        secondMenu = wx.Menu()

        # create files
        firstMenu.Append(SAVE_DATA, 'Save Data'  , 'Save data')
        firstMenu.Append(OPEN_DATA, 'Open Data..', 'Open a new window')
        secondMenu.Append(CONFIGURE,'Configure..', 'Input Data here')
        # append to menu
        menubar.Append(firstMenu, 'File')
        menubar.Append(secondMenu,'Options')
        #
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.SaveData, id=SAVE_DATA)
        self.Bind(wx.EVT_MENU, self.OpenData, id=OPEN_DATA)
        self.Bind(wx.EVT_MENU, self.Configure, id=CONFIGURE)

    def SaveData(self, e):
        print("Save")

    def OpenData(self, e):
        print("Open")

    def Configure(self, e):
        print("Config")

if( __name__ == '__main__' ):
    app = wx.PySimpleApp()
    frame = myFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

rawDescription: wxpython - Menu interaction

description: Wxpython - Menu interaction


code:
contact_message = "Name: %s\nEmail: %s" % (
    form_name,
    form_email,
)

rawDescription: Line breaks Django string

description: Line breaks Django string


code:
import matplotlib.pyplot as plt

time = [1,2,3]
value = {}
value['x'] = [1,2,3]
value['y'] = [1,4,9]
value['z'] = [1,8,27]
for k, dimension in enumerate(['x', 'y', 'z']):
    plt.subplot(3, 1, k)
    plt.scatter(time, value[dimension])

plt.show()

rawDescription: How do I display multiple matplotlib.pyplot plots in the same figure?

description: Display multiple matplotlib.pyplot plots in the same figure


code:
class Project(Base):
    # ...
    submissions = relationship('Submission',
        backref='Project', order_by='Submission.timestamp')

rawDescription: How to sort an InstrumentedList based on timestamp?

description: Sort an InstrumentedList based on timestamp


code:
a = [1,2,3,4,5]
diff = 2
a_set = set(a)
sum(i-diff in a_set for i in a_set)

description: Program speed/unnecessary double-counting


code:
corr = lambda obj: obj.pivot('date', 'Item_Id', 'price').corr()
df.groupby('Location_Id').apply(corr)

rawDescription: Pairwise correlation

description: Pairwise correlation


code:
import operator
qs = []
for r in all_records:
    qs.append(Q(name=r.name, thing=r.thing, more=r.more))
Users.objects.filter(reduce(operator.or_, qs))

rawDescription: Efficiently doing thousands of lookups in a single table via django's ORM?


code:
# monkey-patch for parallel compilation
def parallelCCompile(self, sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
    # those lines are copied from distutils.ccompiler.CCompiler directly
    macros, objects, extra_postargs, pp_opts, build = self._setup_compile(output_dir, macros, include_dirs, sources, depends, extra_postargs)
    cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
    # parallel code
    N=2 # number of parallel compilations
    import multiprocessing.pool
    def _single_compile(obj):
        try: src, ext = build[obj]
        except KeyError: return
        self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)
    # convert to list, imap is evaluated on-demand
    list(multiprocessing.pool.ThreadPool(N).imap(_single_compile,objects))
    return objects
import distutils.ccompiler
distutils.ccompiler.CCompiler.compile=parallelCCompile

rawDescription: Speeding up build process with distutils


code:
def viewfile():
    with open('primelist2kk.dat', 'rb') as g:
        try:
            while True: # run until there's an exception
                h = pickle.load(g)
                print(h)
        except EOFError: # this will be raised by load() when there's no more data
            pass

rawDescription: How do I append a binary file?

description: Append a binary file


code:
import time

t_end = time.time() + 60 * 10
msgList =[]
while time.time() < t_end:
    msg = str(raw_input('Input Data :'))
    msgList.append(msg)
    print(msgList)

rawDescription: Time Looping Python

description: Time Looping Python


code:
results = ([],)
print(len(results))

rawDescription: Why isn't a tuple that contains two empty list an empty tuple?


code: #!/usr/bin/env python

rawDescription: running a python script with cron

description: Running a python script with cron


rawDescription: Using pandas to convert string timestamps


code:
import sys
import select

while True:
    message = select.select([sys.stdin], [], [], timeout=0.1)[0]
    if message:
        print(message)

rawDescription: Get input whilst looping code

description: Get input whilst looping code


code:
for filepath in glob.glob(filelocation): # for each file
    FILE = codecs.open(filepath, 'r', encoding="utf-8")
    contents = FILE.read().lower()
    if "every kind of asset" in contents:
        print("Found in " + os.path.basename(filepath))
        freq = contents.count("every kind of asset")
        print(freq)
    else:
        print("not found in " + os.path.basename(filepath))

rawDescription: String.count() returning 0 even though word exists in file


code:
f = open('sample.txt', 'r')
lines = f.readlines()
f.close()
name = 'ali'

for i in range(len(lines) - 1):
    line = lines[i]
    #Find 'name' index (-1 if not found)
    index = line.find(name)
    # If the wanted name is found
    if index != -1:
       #Split line by spaces to get name and number starting from name

       words = line[index:].split()
       # Name and number should be the first two elements in words list
       name_number =  words[0]+ ' ' + '5' #words[1]

       #
       # Do some thing with name_number
       #

       # the last '1' is to skip copying a space
       line  = line[:index] + name_number + ' ' + line[index + len(name_number) + 1:]

       # Save result
       lines[i] = line

output = open('sample.txt', 'w')
for line in lines:
    output.write(line)
output.close()

rawDescription: How to search for a term in a text file then replace that whole line with a new line in python?

description: Search for a term in a text file then replace that whole line with a new line in python


rawDescription: is the year-month part of a datetime variable still a time object in Pandas?

description: Is the year-month part of a datetime variable still a time object in Pandas


code:
rowids = [2, 5, 15]
rowids[listbox.curselection()]

rawDescription: Hidden id on tkInter Listbox

description: Hidden id on tkInter Listbox


code:
from collections import OrderedDict

cars = OrderedDict([(1, 'Camaro'), (2, 'SS Sedan'), (3, 'M3 Sedan'), (4, 'GLE Coupé')])

print('Help Rick P. choose a car to drive.\n')
print('\n'.join([str(key) + ' ' + val for key, val in cars.items()]) + '\n')

def choice_car(invalid = False):
    if invalid:
        choice = int(input('Invalid car. Try again...\n'))
    else:
        choice = int(input('Input the corresponding number to the car that you\'d like Rick P. to drive...\n'))
    car = cars.get(choice)
    if not car:
        return choice_car(True)
    return car

car = choice_car()

print(car)

rawDescription: Python. Issue with my interactive story

description: Python. Issue with my interactive story


code:
setup(
    extras_require={
        "test": ["pytest", "your other requirements"],
    },
)

rawDescription: Does python's new 'pip wheel' have any support for building wheels for the dependencies listed in tests_requires?


code:
class WSGICache(object):

    def __init__(self, app):
        self.app = app
        self.cache = {}

    def is_expired(self, environ):
        """Determine is the resource the request for already expired?

        """
        # FIXME: check is the resource expired, by looking
        # PATH_INFO, if it is a file, it might be last modified time
        # if it is an object from database, see what is the last modified time
        return False

    def __call__(self, environ, start_response):
        path = environ['PATH_INFO']
        cached = self.cache.get(path)
        # do we have valid cache?
        if self.is_expired(environ) or not cached:
            cached = list(self.app(environ, start_response))
            self.cache[path] = cached
        return cached

rawDescription: How to implement caching with WSGI?

description: Implement caching with WSGI


code:
import socket

class A:
    def __init__(self):
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

class B:
    def send(self, host, port):
        # send msg through A
        a = A()
        a.sock.connect((host, port))
        a.sock.sendall('Hello, world')

rawDescription: Can I work with a socket I created in another file in python?

description: Work with a socket I created in another file in python


code:
from xml.etree import ElementTree as et

class Kml(object):
    def __init__(self):
        self.root = et.Element('kml')
        self.doc = et.SubElement(self.root,'Document')

    def add_placemark(self,name,desc,lat,long,alt):
        pm = et.SubElement(self.doc,'Placemark')
        et.SubElement(pm,'name').text = name
        et.SubElement(pm,'description').text = desc
        pt = et.SubElement(pm,'Point')
        et.SubElement(pt,'coordinates').text = '{},{},{}'.format(lat,long,alt)

    def write(self,filename):
        tree = et.ElementTree(self.root)
        tree.write(filename)

kml = Kml()
kml.add_placemark('Location1','Description1',-120,45,0)
kml.add_placemark('Location2','Description2',60,-45,0)
kml.write('out.kml')

rawDescription: Creating a KML file from a XML file

description: Creating a KML file from a XML file


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

import sqlalchemy as sqAl
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.associationproxy import association_proxy

engine = sqAl.create_engine('sqlite:///m2m-w-a2.sqlite') #, echo=True)
metadata = sqAl.schema.MetaData(bind=engine)

Base = declarative_base(metadata)

class UserProfile(Base):
  __tablename__ = 'user'

  id            = sqAl.Column(sqAl.Integer, primary_key=True)
  full_name     = sqAl.Column(sqAl.Unicode(80))
  gender        = sqAl.Column(sqAl.Enum('M','F','D', name='gender'), default='D', nullable=False)
  description   = sqAl.Column(sqAl.Unicode(280))
  following     = association_proxy('followeds', 'followee')
  followed_by   = association_proxy('followers', 'follower')

  def follow(self, user, **kwargs):
    Follow(follower=self, followee=user, **kwargs)

  def __repr__(self):
    return 'UserProfile({})'.format(self.full_name)

class Follow(Base):
  __tablename__ = 'follow'

  followee_id   = sqAl.Column(sqAl.Integer, sqAl.ForeignKey('user.id'), primary_key=True)
  follower_id   = sqAl.Column(sqAl.Integer, sqAl.ForeignKey('user.id'), primary_key=True)
  status        = sqAl.Column(sqAl.Enum('A','B', name=u'status'), default=u'A')
  created       = sqAl.Column(sqAl.DateTime, default=sqAl.func.now())
  followee      = relationship(UserProfile, foreign_keys=followee_id, backref='followers')
  follower      = relationship(UserProfile, foreign_keys=follower_id, backref='followeds')

  def __init__(self, followee=None, follower=None, **kwargs):
    """necessary for creation by append()ing to the association proxy 'following'"""
    self.followee = followee
    self.follower = follower
    for kw,arg in kwargs.items():
      setattr(self, kw, arg)

Base.metadata.create_all(engine, checkfirst=True)
session = sessionmaker(bind=engine)()

def create_sample_data(sess):
  import random
  usernames, fstates, genders = ['User {}'.format(n) for n in range(4)], ('A', 'B'), ('M','F','D')
  profs = []
  for u in usernames:
    user = UserProfile(full_name=u, gender=random.choice(genders))
    profs.append(user)
    sess.add(user)

  for u in [profs[0], profs[3]]:
    for fu in profs:
      if u != fu:
        u.follow(fu, status=random.choice(fstates))

  profs[1].following.append(profs[3]) # doesn't work with followed_by

  sess.commit()

# uncomment the next line and run script once to create some sample data
# create_sample_data(session)

profs = session.query(UserProfile).all()

print(       '{} follows {}: {}'.format(profs[0], profs[3], profs[3] in profs[0].following))
print('{} is followed by {}: {}'.format(profs[0], profs[1], profs[1] in profs[0].followed_by))

for p in profs:
  print("User: {0}, following: {1}".format(
    p.full_name,  ", ".join([f.full_name for f in p.following])))
  for f in p.followeds:
    print(" " * 25 + "{0} follow.status: '{1}'"
          .format(f.followee.full_name, f.status))
  print("            followed_by: {1}".format(
    p.full_name,  ", ".join([f.full_name for f in p.followed_by])))
  for f in p.followers:
    print(" " * 25 + "{0} follow.status: '{1}'"
          .format(f.follower.full_name, f.status))

rawDescription: Many-to-many, self-referential, non-symmetrical relationship (twitter model) via Association Object in SqlAlchemy


code:
from django.db.models.signals import post_save
from yourapp.models import YourModel

def model_created(sender, **kwargs):
    the_instance = kwargs['instance']
    if kwargs['created']:
        do_some_stuff(the_instance)

post_save.connect(model_created, sender=YourModel)

rawDescription: How can I run some code when a Django model’s save method is called for the first time?

description: Run some code when a Django model’s save method is called for the first time


code:
from sqlalchemy import event
import weakref
transactions_with_flushes = weakref.WeakSet()

@event.listens_for(Session, "after_flush")
def log_transaction(session, flush_context):
    for trans in session.transaction._iterate_parents():
        transactions_with_flushes.add(trans)

def session_has_pending_commit(session):
    return session.transaction in transactions_with_flushes

rawDescription: How to check whether SQLAlchemy session is dirty or not

description: Check whether SQLAlchemy session is dirty or not


code:
from collections import defaultdict

def summarize(file_name):
    exo, hit, miss = 0, 1, 2  # indexes of according counts
    d = defaultdict(lambda: [0, 0, 0])  # keep all counts for each type of system
    with open(file_name, 'r') as input:
        for line in input:
            exos, planets = map(int, line.strip().split())  # split, cast to int
            if exos:
                d[planets][exo] += exos
                d[planets][hit] += 1
            else:
                d[planets][miss] += 1

    for key in sorted(d.keys()):
        print('{} {} {} {}'.format(key, d[key][exo], d[key][hit], d[key][miss]))

summarize('data.txt')

rawDescription: How do I count exoplanets per system in a file with over 10,000 lines in Python?

description: Count exoplanets per system in a file with over 10,000 lines in Python


rawDescription: how to split one dimentional array returned by numpy.loadtxt

description: Split one dimentional array returned by numpy.loadtxt


code:
sentence = 'A brown cat jumps over a person'
MyModel.objects.extra(where={"%s like CONCAT('%%', `name` ,'%%')"},
                      params=[sentence]).values()

rawDescription: In Django, how to find a term that is part of a string instead of containing that string?


code:
import subprocess
cmd =  "wpa_cli -i wlan3 status | grep ip_address"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, close_fds=True)
output = p.stdout.read()

rawDescription: getting p2p_device_address from wpa_cli status

description: Getting p2p_device_address from wpa_cli status


code:
parties = ['NDP', 'Green', 'Liberal', 'CPC']
winning_party = parties[total.index(max(total))]

rawDescription: Trying to fix my function

description: Trying to fix my function


code:
import pandas as pd

javaFrame = pd.DataFrame({'Java Jobs': [3593, 3585, 2080, 1920, 1571, 5, 5, 5, 5, 5],
     'Town': ['York,NY', 'NewYork,NY', 'Seattle,WA', 'Chicago,IL', 'Boston,MA', 'Holland,MI', 'Manhattan,KS', 'Vernon,IL', 'Clayton,MO', 'Waukegan,IL']}, index=[435, 212, 584, 624, 301, 79, 38, 497, 30, 90])
pythonFrame = pd.DataFrame({'Python Jobs': [2949, 2938, 1321, 1312, 1117, 5, 5, 5, 5, 5],
     'Town': ['NewYork,NY', 'York,NY', 'Seattle,WA', 'Chicago,IL', 'Boston,MA', 'Hanover,NH', 'Bulverde,TX', 'Salisbury,NC', 'Rockford,IL', 'Ventura,CA']}, index=[160, 11, 349, 91, 167, 383, 209, 203, 67, 256])

result = pd.merge(javaFrame, pythonFrame, how='outer').set_index('Town')
#               Java Jobs  Python Jobs
# Town
# York,NY          3593.0       2938.0
# NewYork,NY       3585.0       2949.0
# Seattle,WA       2080.0       1321.0
# Chicago,IL       1920.0       1312.0
# Boston,MA        1571.0       1117.0
# Holland,MI          5.0          NaN
# Manhattan,KS        5.0          NaN
# Vernon,IL           5.0          NaN
# Clayton,MO          5.0          NaN
# Waukegan,IL         5.0          NaN
# Hanover,NH          NaN          5.0
# Bulverde,TX         NaN          5.0
# Salisbury,NC        NaN          5.0
# Rockford,IL         NaN          5.0
# Ventura,CA          NaN          5.0

rawDescription: pandas combining dataframe

description: Pandas combining dataframe


code:
class QueueStream(object):
    def __init__(self):
        self._queue = Queue()

    def write(self, line):
        self._queue.put(line)

    def readlines(self):
        while True:
            item = self._queue.get()
            yield item
            self._queue.task_done()

rawDescription: Blocking version StringIO.readlines()

description: Blocking version StringIO.readlines()


code:
nans = [float("nan") for i in range(2)]
map(id, nans)
nans
nans[0] is nans[1]
nans[0] in nans
nans[0] in nans[1:]

rawDescription: Why in numpy `nan == nan` is False while nan in [nan] is True?


code: df2.loc['new_row'] = list

rawDescription: Python Pandas Adding to Dataframe

description: Python Pandas Adding to Dataframe


code:
def test4():
    form2 = Inputs()
    if request.method == "POST" and form1.validate_on_submit():
        prev_pdf = ["test1.pdf", "test2.pdf", "test3.pdf","test4.pdf", "test5.pdf"]
        new_pdf = ["ex1.pdf","ex2.pdf"]
        options = [("None","None")] + [(pdf, pdf) for pdf in new_pdf]
        for i in range(len(new_pdf)):
            form2.files.append_entry()
            form2.files[i].choices = options
            form2.files[i].default = "None"
        return render_template('test4.html', form1=form1, form2=form2, show=True, numfiles=len(prev_pdf), pdflist=prev_pdf)

rawDescription: How to import a variable from a route() decorator to form file in Flask?

description: Import a variable from a route() decorator to form file in Flask


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

im = Image.open("python.jpg")
a = np.asarray(im)

b = np.where(np.all(((a>[240, 40, 20]) * (a<=[255, 255, 150])), axis=2, keepdims=True), [80,0,0], [0,0,90])

plt.figure()
plt.subplot(1,2,1)
plt.imshow(a)
plt.subplot(1,2,2)
plt.imshow(b)
plt.show()

rawDescription: How do I find and modify a pixel's RGB color using PIL's im.point()?

description: Find and modify a pixel's RGB color using PIL's im.point()


code:
import queue
def find_connectivity(img, i, j):
    dx = [0, 0, 1, 1, 1, -1, -1, -1]
    dy = [1, -1, 0, 1, -1, 0, 1, -1]
    x = []
    y = []
    q = queue.Queue()
    if img[i][j] == 0:
        q.put((i, j))
    while q.empty() == False:
        u, v = q.get()
        x.append(u)
        y.append(v)
        for k in range(8):
            xx = u + dx[k]
            yy = v + dy[k]
            if img[xx][yy] == 0:
                img[xx][yy] = 2
                q.put((xx, yy))
    return x, y

rawDescription: Extract connected pixels in a binary image using python


code: ctypes.c_short(int('FF20', 16)).value

rawDescription: Word of Hexadecimal Value in Python

description: Word of Hexadecimal Value in Python


code:
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
from django.contrib.auth.models import Group

admin.site.unregister(User)
admin.site.unregister(Group)
admin.site.unregister(Site)

rawDescription: Hide Auth section in Django Admin

description: Hide Auth section in Django Admin


code:
import concurrent.futures as futures
import requests
from requests.exceptions import HTTPError

urllist = ...

def get_url_data(url, session):
    try:
        r = session.get(url, timeout=10)
        r.raise_for_status()
    except HTTPError:
        return None

    return r.text

s = requests.Session()

try:
    with futures.ThreadPoolExecutor(max_workers=5) as ex:
        future_to_url = {ex.submit(get_url_data, url, s): url
                         for url in urlist}

    results = {future_to_url[future]: future.result()
               for future in futures.as_completed(future_to_url)}
finally:
    s.close()

rawDescription: Scraping by sending concurrent requests with python