code:
def readslice(inputfilename,ndim):
    shape = (ndim,ndim,ndim)
    fd = open(fname, 'rb')
    data = np.fromfile(file=fd, dtype=np.double).reshape(shape)
    fd.close()
    return data

rawDescription: how to read an outputted fortran binary NxNxN matrix into Python

description: Read an outputted fortran binary NxNxN matrix into Python


code: b = [i for sub in a for i in sub]

rawDescription: in Python, How to join a list of tuples into one list?

description: In Python, How to join a list of tuples into one list


code:
import pandas as pd
import numpy as np

keys1 = [1,2,3,4]
keys2 = [3,4,5,6]
data1 = np.reshape(range(20),[4,5])
data2 = np.reshape(range(20,40),[4,5])

df1 = pd.DataFrame(data1,index=keys1)
df2 = pd.DataFrame(data2,index=keys2)

df1[-(df1.index.isin(df2.index))]

rawDescription: remove keys from pandas.dataframe

description: Remove keys from pandas.dataframe


code:
# Conditional
if youAreInSoft1():
    import Soft1.Controls as Controls
    import Soft1.Rigs as Rigs
else:
    import Soft2.Controls as Controls
    import Soft2.Rigs as Rigs

reload(Controls)
reload(Rigs)

#Variable
import Soft1 as _soft1
import Soft2 as _soft2

Controls = None
Rigs = None

global Controls, Rigs

if youAreInSoft1():
    Controls = _soft1.Controls
    Rigs = _soft1.Controls
else:
    Controls = _soft2.Controls
    Rigs = _soft2.Controls

reload(Controls)
reload(Rigs)

rawDescription: Python __import__ specific submodules, depending in which software we are


code: sorted(persons.values(), key=lambda item: item[2])

rawDescription: How do I sort dictionary values in a way that respects capitalization?

description: Sort dictionary values in a way that respects capitalization


rawDescription: How do I create a datetime in Python from milliseconds?

description: Create a datetime in Python from milliseconds


code:
sock.getOutPutStream().write(Integer.toString(mPort))

s._PORT = int(s._sock.recv(1024))

rawDescription: Java int to python int

description: Java int to python int


code:
Count('article', distinct=True)

def queryset(self, request):
    qs = super(MyAdmin, self).queryset(request)
    qs = qs.annotate(models.Count('article', distinct=True))
    qs = qs.annotate(models.Count('comments', distinct=True))
    return qs

rawDescription: How to annotate more than one value in Django

description: Annotate more than one value in Django


code: arr3 = np.array([np.dot(arr2.T , np.dot(arr1[i] , arr2)) for i in range(arr1.shape[0])])

rawDescription: Numpy np.dot() on multidimensional arrays


code: [a[i] for i in b]

rawDescription: getting elements in a list according to list of indices in python

description: Getting elements in a list according to list of indices in python


code:
class Application(Frame):
    global yearcal
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.month = 5
        self.year = 2014
        self.color_clicked = 'lightskyblue'
        now = datetime.datetime.now()
        self.thisyear = now.year
        self.thismonth = now.month
        self.today = now.day
        self.textcolor = 'purple'
        self.bgcolor = 'gray'
        self.today_color = 'palegreen1'
        self.apt_color = 'light coral'

        # create button and label and pass the application instance
        # so that they can reference its attributes and methods
        self.my_button = MyButton(self)
        self.my_label = MyLabel(self)

class MyButton(Button):
    def __init__(self, app_instance, *args, **kwargs):
        Button.__init__(self, *args, **kwargs)

        self['bg'] = app_instance.backgroundcolor
        self['fg'] = app_instance.textcolor

        self['relief'] = FLAT

class MyLabel(Label):
    def __init__(self, app_instance, *args, **kwargs):
        Label.__init__(self, *args, **kwargs)

        self['fg'] = app_instance.textcolor

rawDescription: Super() python not working for subclass tkinter


code:
writer = ex.csv.writer(f, delimiter=',')
writer.writerow(['Date'] + codes) # codes seems to be a list, so just append it
for i in range(delta.days + 1):
    date = real_date + ex.timedelta(days=i)
    rates = exchrates(date)
    writer.writerow([date] + [rates[j.upper()] for j in codes]) # only one line

rawDescription: Python - for loop to write across a row in CSV


code: getattr(test, a_string)

rawDescription: how to access the class variable by string in Python?

description: Access the class variable by string in Python


code: max(xrange(1, 10001), key=chain_length)

rawDescription: Python max-by function?

description: Python max-by function


code:
import android
import hashlib
import time
name = 0
droid = android.Android()

f_in = open('input.txt', 'r',)
f_out = open('output.txt', 'w',)
for line in f_in.readlines():
    hash_object = hashlib.md5 (line)
    f_out.write(hash_object.hexdigest())

f_in.close()
f_out.close()

rawDescription: The best way to open two files

description: Open two files


code:
from os import listdir
from os.path import isfile, join
files = [file for file in listdir(path) if isfile(join(path, file))]

rawDescription: putting file names in a array using os in python

description: Putting file names in a array using os in python


code:
from scipy.stats import norm
x = np.arange(20)
mu = 5
sigma = 3
mypdf = norm.pdf(x=x, loc=mu, scale=sigma)

rawDescription: Is there an easy way to get probability density of normal distribution with the help of numpy?


code: plt.plot([1, 2], [2, 3], marker='.', markersize=1, linestyle='None')

rawDescription: Matplotlib plotting lines even though marker='.' is specified


code:
@app.route('/api/foo')
def foo():
    return "Foo is special!"

@app.route('/api/<name>')
def generic(name):
    return "Hello %s!" % name

rawDescription: Overriding Flask add_url_rule to route a specific URL


code: map(lambda x: dict(x, b=x['b']+1), d)

rawDescription: Return dictionary with one changed element


code:
def name_gender(user_name, boy_names=None, girl_names=None):
    uname = user_name.lower()
    girliness = 0
    for girl_name in girl_names:
        if girl_name in uname:
            girliness += 1
    boyishness = 0
    for boy_name in boy_names:
        if boy_name in uname:
            boyishness += 1
    if boyishness > girlishness: return 'Male'
    if girlishness > boyishness: return 'Female'
    return 'Androgynous'

rawDescription: Method to find gender of a username - with or without dictionary


code:
mylist = ['ham and eggs', 'spam and legs', "it's time to die, mr bond!"]
import difflib
close_matches = difflib.get_close_matches('spam and eggs', mylist)
close_matches
set(mylist) - set(close_matches)

rawDescription: Compute similarity of all elements in a list, to a single sentence


code:
import tkinter as tk

def toggle(item):
    #Destorys all existing widgets in frame
    for wid in wid_frame.winfo_children():
        wid.destroy()

    #Packs the selected widget
    if item == 'Label':
        tk.Label(wid_frame, text='Hello World!').pack()

    elif item == 'Button':
        tk.Button(wid_frame, text='Hello World!').pack()

    elif item == 'Entry':
        tk.Entry(wid_frame).pack()

root = tk.Tk()

var = tk.StringVar()
var.set("Pick Widget")

op_menu = tk.OptionMenu(root, var, "None", "Label", "Button", "Entry", command=toggle)
op_menu.pack()
op_menu.config(width=15)

wid_frame = tk.Frame(root)
wid_frame.pack()

root.mainloop()

rawDescription: Option Menu to work- tkinter

description: Option Menu to work- tkinter


code:
df = pd.DataFrame([['abc', 1], ['cde', 2], ['efg', 3]])
df
df[0].str.contains('c')
df[df[0].str.contains('c')]

rawDescription: Extracting rows for a Pandas dataframe in Python


code:
def tokenize(usr_string):
    """
    (str) -> list
    Takes a string, and tokenizes it.  Basically turning the string
    representing a mathimatical formual and making it into a list, eliminating
    the white spaces
    >>> '3 + 5 + (-4)
    [3, +, 5, +, (, -4, )]
    """
    token_list = []
    i = 0
    while i < len(usr_string):
#Add values to the list that arent white space, + or -
        if usr_string[i] != ' ' and usr_string[i] != '+' and usr_string[i] != '-':
            token_list.append(usr_string[i])
            i += 1
        elif usr_string[i] == '+' or usr_string[i] == '-':
#Determine if that + or - is an operator or pos / neg
            sign = positive_or_add(usr_string, i)
            if sign == True: #Operator
                token_list.append(usr_string[i])
                i += 1
            elif sign == False: #pos / neg
                token_list.append(usr_string[i] + usr_string[i + 1])
                i += 2
        elif usr_string[i] == ' ':
            i += 1
            continue
        else:
            print('something messed up')
            i += 1
    return token_list

rawDescription: Else statement is executing along with if statement


rawDescription: Regex Python Extract Informations

description: Regex Python Extract Informations


code:
df = psql.read_sql(('select "Timestamp","Value" from "MyTable" '
                     'where "Timestamp" BETWEEN %(dstart)s AND %(dfinish)s'),
                   db,params={"dstart":datetime(2014,6,24,16,0),"dfinish":datetime(2014,6,24,17,0)},
                   index_col=['Timestamp'])

rawDescription: Pandas read_sql with parameters

description: Pandas read_sql with parameters


code:
import geopy
import geopy.distance
# your data
ccoordinate_list = [(11.6702634, 72.313323), (11.6723698, 78.114523), (31.67342698, 78.465323), (12.6702634, 72.313323), (12.67342698, 75.465323)]
coordinate = (11.6723698, 78.114523)
# the solution
pts = [ geopy.Point(p[0],p[1]) for p in ccoordinate_list ]
onept = geopy.Point(coordinate[0],coordinate[1])
alldist = [ (p,geopy.distance.distance(p, onept).km) for p in pts ]
nearest_point = min(alldist, key=lambda x: (x[1]))[0] # or you can sort in by distance with sorted function

rawDescription: Python:- Given a list of tuple coordinates, find the nearest coord to a specified coord (Google Maps Coords))


code:
import csv
import re

re_AISD = re.compile(r'(AISD)\s+(\S+)')
re_WKU = re.compile(r'(WKU)\s+(\S+)')
re_Filed = re.compile(r'(Filed)\s+(.*?\d{4})')
re_Int = re.compile(r'(Int.)  Cl.\s+(\w+ \d+)')

FLD_REGEXPES = (re_AISD, re_WKU, re_Filed, re_Int)

def get_field(line):
    for ree in FLD_REGEXPES:
        rx = ree.search(line)
        if rx:
            return (rx.group(1), rx.group(2))
    return (None, None)

def convert_file(fname):
    fields = 'WKU Filed Int. AISD'.split()
    f = open(fname, 'r')
    lines = f.readlines()
    f.close()
    with open(fname + '.csv', 'wb') as oufile:
        writer = csv.DictWriter(oufile, fieldnames=fields, restval = '?', dialect='excel-tab')
        writer.writerow(dict((h, h) for h in fields))
        rec = {}
        for line in lines:
            k, v = get_field(line)
            if k:
                print('[%s]=[%s]' % (k, v))
                if k == 'WKU': # start of new record
                    if rec:
                        writer.writerow(rec)
                    rec = {}
                rec[k] = v
        if rec:
            writer.writerow(rec)

rawDescription: extract variables from text and write into csv with python

description: Extract variables from text and write into csv with python


code:
# -*- coding=utf-8 -*-
from __future__ import absolute_import
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider,Rule,Spider
from car.items import Car58Item

class CarSpider (CrawlSpider):
    name ='car'
    allowed_domains = ['58.com']
    start_urls = ['http://quanguo.58.com/ershouche']
    rules = [Rule(LinkExtractor(allow=('/pn\d+')),'parse_item')] #//页面读取策略

    def parse_item(self,response):
        trs = response.xpath("//div[@id='infolist']/table[@class='tbimg']/tr")[1:-2]
        items = []
        #for tr in sel.xpath("id('infolist')/table/tr"):
        for tr in trs:
            item = Car58Item()
            item['url'] = tr.xpath("td[@class='img']/a/@href").extract()
            item['tip'] = tr.xpath("td[@class='t']/a/font/text()").extract()
            item['name'] = tr.xpath("td[@class='t']/a[1]/text()").extract()
       #     item['description'] = trs.xpath("/td[@class='t']/a/text()").extract()
            item['size'] = tr.xpath("td[@class='t']/p").extract()
            item['region'] = tr.xpath("td[@class='tc']/a/text()").extract()
            item['amt'] = tr.xpath("td[@class='tc']/b/text()").extract()
            items.append(item)
        return items

rawDescription: Scraping Data from a table with Scrapy but not Scraped Items


code:
import numpy as np
mydf = np.empty( (2,), dtype=[('file_name',object),('file_size_mb',float)] )
mydf['file_name'][0]='foobarasdf.tif'
mydf['file_name'][1]='arghtidlsarbda.jpg'
mydf

rawDescription: numpy recarray strings of variable length

description: Numpy recarray strings of variable length


code:
def generate_server_id():
    """Generates 20 random hex characters"""
    try:
        return "".join("%02x" % ord(c) for c in generate_random_bytes(10))
    except TypeError:
        return "".join("%02x" % c for c in generate_random_bytes(10))

rawDescription: Crypto Random get_random_bytes is not working in py3 as it did in py2


rawDescription: Python: How to flood-fill part of a bitmap enclosed by a black border with my chosen color?


rawDescription: Typecasting to 'int' in Python generating wrong result


code:
def get_all_files_from(my_path):
    """return all filename in a folder, without their path"""
    file_list_without_path = [ f for f in listdir(my_path) if isfile(join(my_path,f)) and 'Desktop.ini' not in f ]
    return file_list_without_path

rawDescription: Python messing up my windows folder icon


code:
for i in ReadOnlyCollection:
    i.doSomething()

rawDescription: .Item property of readOnlycollection doesn't work


rawDescription: Pandas: Product of specific columns

description: Pandas: Product of specific columns


code:
lst_fact = map(fact, lst)
reduce(operator.mul, lst_fact)

rawDescription: How does reduce function work?

description: How does reduce function work


code:
from subprocess import *
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]

rawDescription: How to get stdout into a string (Python)

description: Get stdout into a string (Python)


code: my_list_of_dicts.sort(key=lambda x: (x['day'], priority_list.index(x['job'])))

rawDescription: How to sort a list of dictionaries by 2 keys, one of which is based on an arbitrary list

description: Sort a list of dictionaries by 2 keys, one of which is based on an arbitrary list


code:
def swap(l, o, n): #pass the list, old position, new position
    l[n], l[o] = l[o], l[n]

rawDescription: List remains unaltered even after commiting a swap function in python


code:
A
B = NP.array([ 0,  1,  0,  1,  0])
# convert the indexing array to a boolean array
B = NP.array(B, dtype=bool)
# index A against B--indexing array is placed after the ',' because
# you are selecting columns
res = A[:,B]
res

rawDescription: compress numpy array(matrix) by removing columns using another numpy array as mask

description: Compress numpy array(matrix) by removing columns using another numpy array as mask


code:
def worker(stop, jobs):
    for j in jobs:
        if stop:
            break
        j.do()

stop = []
d = deferToThread(worker)

# This will make the list eval to true and break out of the loop.
stop.append(None)

rawDescription: How to add timeout to twisted defer

description: Add timeout to twisted defer


code:
from SendKeys import SendKeys
SendKeys('%{TAB}')

rawDescription: Focus Window to CMD Prompt Python

description: Focus Window to CMD Prompt Python


code:
string = r"/\?%"
test = "This is my string % my string ?"
any(elem in test for elem in string)
test2 = "Just a test string"
any(elem in test2 for elem in string)

rawDescription: String contains character set in python

description: String contains character set in python


code:
from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self, path):
        QtGui.QWidget.__init__(self)
        pixmap = QtGui.QPixmap(path)
        layout = QtGui.QGridLayout(self)
        for row in range(4):
            for column in range(4):
                label = QtGui.QLabel(self)
                label.setPixmap(pixmap)
                layout.addWidget(label, row, column)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window(sys.argv[1] if len(sys.argv) else '')
    window.setGeometry(500, 300, 300, 300)
    window.show()
    sys.exit(app.exec_())

rawDescription: How to insert a QImage into an NxN grid layout in PyQt4?

description: Insert a QImage into an NxN grid layout in PyQt4


code:
import xlsxwriter

# Create a new workbook and add a worksheet.
workbook = xlsxwriter.Workbook('percent.xlsx')
worksheet = workbook.add_worksheet()

# Create a percentage number format.
percent_format = workbook.add_format({'num_format': '0%'})

# Write a number as a percentage.
worksheet.write('A1', .12, percent_format)

workbook.close()

rawDescription: Ho can I remove the green arrow from a cell on excel file using python-xlsxwriter


code:
import scrapy
from scrapy.spiders import XMLFeedSpider

class MySpider(XMLFeedSpider):
    name = 'myspider'
    start_urls = ['url_here']

    itertag = "item"

    def parse_node(self, response, node):
        for link in node.xpath(".//link/text()").extract():
            yield scrapy.Request(link.strip(), callback=self.parse_link)

    def parse_link(self, response):
        print(response.url)

rawDescription: Extracting links from xml using scrapy

description: Extracting links from xml using scrapy


code: soup.select("a > b")

rawDescription: BeautifulSoup, Recursively Address A Tag with String Argument


code:
dic = {}

while True:
    line = input('Enter Line: ')
    line = line.lower()
    if not line:
        break
    for word in line.split():    # <-----
        dic.setdefault(word, 0)  # <-----
        dic[word] += 1           # <-----
for line, n in sorted(dic.items()):
    print(line, n)

rawDescription: Python beginner issues - dictionary

description: Python beginner issues - dictionary


code:
class ThumbListWidget(QtGui.QListWidget):
    _drag_info = []
    def __init__(self, type, name, parent=None):
        ...
        self._dropping = False

    def dropEvent(self, event):
        if event.mimeData().hasUrls():
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            # event.setDropAction(QtCore.Qt.MoveAction)
            self._dropping = True
            super(ThumbListWidget, self).dropEvent(event)
            self._dropping = False

    def rowsInserted(self, parent, start, end):
        if self._dropping:
            self._drag_info.append((start, end))
            self.emit(QtCore.SIGNAL("dropped"), self._drag_info)
        super(ThumbListWidget, self).rowsInserted(parent, start, end)

rawDescription: PyQt: How to get listItem Index after ListView's onDrop()


code:
def findValue(list):
    result = []
    i = 0
    length = len(list)
    while i < length:
        x = list[i]
        if x < 0:
            result.append(i)
        i = i + 1
    return result

print(findValue([-3,7,-4,3,2,-6]))

rawDescription: Python: return index of values in a list that are less than a target value using a "while" loop


code:
tmp = x[:len(x)-len(x)%1000] # drop some points to make length a multiple of 1000
tmp = tmp.reshape((1000,-1)) # split into pieces of 1000 points
tmp = tmp.reshape((-1,1000)) # alternative: split into 1000 pieces
figure(); hold(True)         # plot minimum and maximum in the same figure
plot(tmp.min(axis=0))
plot(tmp.max(axis=0))

rawDescription: how to plot on a smaller scale

description: Plot on a smaller scale


code:
import sqlite3
conn = sqlite3.connect('Database/testDB.db')

rawDescription: Making it pythonic: create a sqlite3 database if it doesn't exist?


rawDescription: Selenium Python how to get text(html source) from <div>


code:
from itertools import groupby

with open('input') as fin, open('output', 'w') as fout:
    stripped_lines = (line.rstrip('*\n') for line in fin)
    split_lines = (line.split(None, 1) for line in stripped_lines if line)
    for k, g in groupby(split_lines, lambda L: L[0]):
        fout.write('{}\n{}\n\n'.format(k, '\n'.join(el[1] for el in g)))

rawDescription: File parsing python recursive

description: File parsing python recursive


code:
def RightClickRibbonTabs(self,event):
    pos = self.ScreenToClient(wx.GetMousePosition())
    index, tab = self._ribbon.HitTestTabs(pos)
    page = self._ribbon._pages[index].page
    pos = wx.GetMousePosition()
    x, y = pos.x, pos.y
    self.popupmenu = wx.Menu()
    RenameTab = self.popupmenu.Append(-1, "Rename Tab")
    self.Bind(wx.EVT_MENU, lambda event : self.RenameTab(event, x, y, page), RenameTab)
    pos = wx.GetMousePosition()
    x, y = pos.x, pos.y
    pos = self.ScreenToClient(pos)
    self.PopupMenu(self.popupmenu, pos)

rawDescription: How to get object from right click event in wxribbon bar in wxpython?

description: Get object from right click event in wxribbon bar in wxpython


code:
def createLab(size=16):
    maze = createMaze()
    answerPath=[]
    visual=['o']*(size**2)
    pos=[(size**2)/2,(size**2)/2]
    lat='north'
    for move in maze:
        pos = pos[:] #pos is now a new list with the same values as the previous pos
        #Alternatively: pos = list(pos)
        answerPath.append(pos)

        if move=='straight':
            if lat=='north': pos[1]=pos[1]+size
            elif lat=='south': pos[1]=pos[1]-size
            elif lat=='east': pos[0]=pos[0]+1
            elif lat=='west': pos[0]=pos[0]-1

        elif move=='left':
            if lat=='north': pos[1]=pos[1]-1; lat='west'
            elif lat=='south': pos[1]=pos[1]+1; lat='east'
            elif lat=='east': pos[0]=pos[0]+size; lat='north'
            elif lat=='west': pos[0]=pos[0]-size; lat='south'

        elif move=='right':
            if lat=='north': pos[1]=pos[1]+1; lat='east'
            elif lat=='south': pos[1]=pos[1]-1; lat='west'
            elif lat=='east': pos[0]=pos[0]-size; lat='south'
            elif lat=='west': pos[0]=pos[0]+size; lat='north'
    return answerPath, maze, pos

rawDescription: Python list assignment not working

description: Python list assignment not working


code:
def varimax(Phi, gamma = 1, q = 20, tol = 1e-6):
    from numpy import eye, asarray, dot, sum
    from numpy.linalg import svd
    p,k = Phi.shape
    R = eye(k)
    d=0
    for i in xrange(q):
        d_old = d
        Lambda = dot(Phi, R)
        u,s,vh = svd(dot(Phi.T,asarray(Lambda)**3 - (gamma/p) * dot(Lambda, diag(diag(dot(Lambda.T,Lambda))))))
        R = dot(u,vh)
        d = sum(s)
        if d/d_old < tol: break
    return dot(Phi, R)

rawDescription: perform varimax rotation in python using numpy

description: Perform varimax rotation in python using numpy


code:
class AccessorType(type):
    def __init__(self, name, bases, d):
        type.__init__(self, name, bases, d)
        accessors = {}
        prefixs = ["get_", "set_", "del_"]

        for k in d.keys():
            v = getattr(self, k)
            for i in range(3):
                if k.startswith(prefixs[i]):
                    accessors.setdefault(k[4:], [None, None, None])[i] = v

        for name, (getter, setter, deler) in accessors.items():
            # create default behaviours for the property - if we leave
            # the getter as None we won't be able to getattr, etc..
            # [...] some code that implements the above comment
            setattr(self, name, property(getter, setter, deler, ""))

rawDescription: How are Python metaclasses different from regular class inheritance?


code:
import _winreg
c = _winreg.ConnectRegistry("SOMEMACHINE", _winreg.HKEY_CLASSES_ROOT)
c = _winreg.ConnectRegistry("10.10.40.9", _winreg.HKEY_CLASSES_ROOT)

rawDescription: Python code to scan network to obtain the computer name


code:
def list_parser(l):
    for t in l:
        items = t[0].split(", ")
        category = items[0]
        number_strings = items[1:]
        numbers = [float(n) for n in number_strings]
        yield (category, numbers)

rawDescription: Converting a tuple into two parts

description: Converting a tuple into two parts


code:
x = random.rand(100)
threshold = 0.2
indices = abs(x) < threshold
x[indices] = x[indices] - threshold*x[indices]

rawDescription: Manipulate array values in an easy way

description: Manipulate array values in an easy way


code:
df1
df2
df3
df3/df1.where(df3 >= 0, df2)

rawDescription: Selective division of pandas data frames


code: d = json.loads(your_string)

rawDescription: Parsing Strings with Python

description: Parsing Strings with Python


code:
import itertools
mydict = {'a': 10, 'b': 20, 'c':30}
result = [mydict[x] + mydict[y] for x, y in itertools.combinations(d, 2)]

rawDescription: Python - recursive loop within a dict

description: Python - recursive loop within a dict


code:
import ctypes
ctypes.windll.shell32.ShellExecuteW(None, u'open', u'explorer.exe',
                                    u'/n,/select, ' + file_path, None, 1)

rawDescription: Open and Select Item in Windows Explorer (Unicode) - Python


code:
import argparse
parser = argparse.ArgumentParser(
    description='This is my utility description.',
    epilog='The exit status will be 0 if everything is fine, '
           'and 1 if an input-file or an output-directory does not exist')

parser.print_help()

rawDescription: Is it possible for argparse's --help to show my exit statuses?


code: df['ticker'] = df['Company Name'].str.extract("\((.*)\)")

rawDescription: Extract substring from string in dataframe


code:
row = cursor.fetchone()

while row is not None:
    var += row[0]
    row = cursor.fetchone()

cursor.close()
cnx.close()

rawDescription: Sum 2 tuple in python

description: Sum 2 tuple in python


code:
def track_past_city(x,y):
    return (x[0]+y[0],x[1:]+y[1:]) #0 is how far you've gone, #[1:] is where you've been

def shortestPath(Cities,Distances):
    if len(Cities)==1: return 0, Cities[0]
    else: return min( map( lambda n: (track_past_city((Distances[Cities[0],Cities[n]],Cities[0]),shortestPath(Cities[n:],Distances))), range(1,len(Cities))) )

shortestPath(["A","B", "C", "D", "E"],d)
# (10, ('A', ('C', ('D', 'E'))))

rawDescription: Python shortest distance

description: Python shortest distance


code:
from bs4 import BeautifulSoup

data = """

something here

        desired text

"""
soup = BeautifulSoup(data, "html.parser")

comment = soup.find("div", {"class": "GCARQJCDEXD"}).find("div", {"class": ""})
print(comment.get_text(strip=True))

rawDescription: How to access text inside this <div class> that has no id? Using BeauitfulSoup

description: Access text inside this <div class> that has no id? Using BeauitfulSoup


code: elt1, elt2 = random.sample(foo, 2)

rawDescription: Randomly choosing two elements in a list


code:
import json
import urllib2

data = json.load(urllib2.urlopen('http://someurl/path/to/json'))

rawDescription: How do I request and process JSON with python?

description: Request and process JSON with python


code:
import pandas

a=[['Date', 'letters', 'numbers', 'mixed'], ['1/2/2014', 'a', '6', 'z1'], ['1/2/2014', 'a', '3', 'z1'], ['1/3/2014', 'c', '1', 'x3']]
df = pandas.DataFrame.from_records(a[1:],columns=a[0])

b= [['a', 'b', 'c'], ['a', 'b', 'c']]
df2 = pandas.DataFrame.from_records(b[1:],columns=b[0], index=[1])

df=df.join(df2,how='outer')

print(df)
#        Date letters numbers mixed    a    b    c
# 0  1/2/2014       a       6    z1  NaN  NaN  NaN
# 1  1/2/2014       a       3    z1    a    b    c
# 2  1/3/2014       c       1    x3  NaN  NaN  NaN

rawDescription: Pandas: join a row of data on specific index number


code:
stats = { 'copied': 5, 'skipped': 14 }
print( 'Copied: {copied}, Skipped: {skipped}'.format( **stats ) )  #use ** to "unpack" a dictionary

rawDescription: Python 3.2: How to pass a dictionary into str.format()


code:
def getmembers(object, predicate=None):
    """Return all members of an object as (name, value) pairs sorted by name.
    Optionally, only return members that satisfy a given predicate."""
    results = []
    for key in dir(object):
        try:
            value = getattr(object, key)
        except AttributeError:
            continue
        if not predicate or predicate(value):
            results.append((key, value))
    results.sort()
    return results

rawDescription: How to get actual list of names of object if custom __dir__ implemented?

description: Get actual list of names of object if custom __dir__ implemented


rawDescription: Python, matching two list's elements

description: Python, matching two list's elements


rawDescription: How to represent the variable by combine static & varying parts

description: Represent the variable by combine static & varying parts


code:
a = np.random.rand(10, 10)
b = np.random.rand(10, 10)
b[-1] = b[0] + b[1] # one row is a linear combination of two others
np.linalg.matrix_rank(a)
np.linalg.matrix_rank(b)
def is_invertible(a):
    return a.shape[0] == a.shape[1] and np.linalg.matrix_rank(a) == a.shape[0]

is_invertible(a)
is_invertible(b)

rawDescription: How to decide a whether a matrix is singular in python-numpy?

description: Decide a whether a matrix is singular in python-numpy


code: self.fields['title'].required = False

rawDescription: How can I reuse django form for searching?

description: Reuse django form for searching


code:
import nltk
from nltk.chunk.util import tagstr2tree
from nltk import word_tokenize, pos_tag
text = "John Rose Center is very beautiful place and i want to go there with Barbara Palvin. Also there are stores like Adidas ,Nike ,Reebok Center."
tagged_text = pos_tag(word_tokenize(text))

grammar = "NP:{<NNP>+}"

cp = nltk.RegexpParser(grammar)
result = cp.parse(tagged_text)

print(result)

rawDescription: NLTK - Chunk grammar doesn't read commas


code:
import random

def gerador_aleatorio():
    return random.randint(0, 10)

def histograma():
    hist = {}

    for count in range(int(input('Enter amount of rand nums: '))):
        num = gerador_aleatorio()
        hist.setdefault(num, 0)
        hist[num] += 1

    for number in hist:
        print(str(number) + ': ' + str(hist[number]))

histograma()

rawDescription: Python: Function to count an output of another function


code:
from java.lang import Runtime
process = Runtime.getRuntime().exec("ls -l")
output = process.getInputStream() # process' output is our input
# read output somehow, while the process is generating it

rawDescription: running external commands with control over the input/output in jython 2.1

description: Running external commands with control over the input/output in jython 2.1


code:
format_strings = ','.join(['%s'] * len(list_of_ids))
cursor.execute("DELETE FROM foo.bar WHERE baz IN (%s)" % format_strings,
                tuple(list_of_ids))

rawDescription: imploding a list for use in a python MySQLDB IN clause

description: Imploding a list for use in a python MySQLDB IN clause


code:
import time

# Repeat forever
while True:
    post_to_twitter() # Call your function
    # Sleep for 60 seconds / minute * 30 minutes
    time.sleep(60 * 30)

rawDescription: Run my python3 program on remote Ubuntu server every 30 min


code:
def timer_compare(time1, time2='', note='@', time3=None):
    if time3 is None:
        time3 = time.clock()

rawDescription: time.clock(), Odd result when used inside function-definition

description: Time.clock(), Odd result when used inside function-definition


code:
from requests.packages.urllib3.connection import HTTPConnection

def patched_new_conn(self):
    """ Establish a socket connection and set nodelay settings on it

    :return: a new socket connection
    """
    # resolve hostname to an ip address; use your own
    # resolver here, as otherwise the system resolver will be used.
    hostname = your_dns_resolver(self.host)
    try:
        conn = socket.create_connection(
            (hostname, self.port),
            self.timeout,
            self.source_address,
        )
    except AttributeError: # Python 2.6
        conn = socket.create_connection(
            (hostname, self.port),
            self.timeout,
        )
    conn.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY,
                    self.tcp_nodelay)
    return conn

HTTPConnection._new_conn = patched_new_conn

rawDescription: Python 'requests' library - define specific DNS?


code:
import types
p = Person("Bar", -1)
new_hash_method = lambda obj: hash(obj.name)
p.__hash__ = types.MethodType(new_hash_method, p)
for i in xrange(0, len(union_list)):
    union_list[i].__hash__ = types.MethodType(new_hash_method, union_list[i])
set(union_list).intersection(p)

rawDescription: Python custom set intersection

description: Python custom set intersection


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

def _h(i):
    return np.log(i) + 0.5772156649

def _c(n):
    if n > 2:
        h = _h(n-1)
        return 2*h - 2*(n - 1)/n
    if n == 2:
        return 1
    else:
        return 0

def _anomaly_score(score, n_samples):

    score = -score/_c(n_samples)

    return 2**score

def _split_data(X):
    ''' split the data in the left and right nodes '''
    n_samples, n_columns = X.shape
    n_features = n_columns - 1
    m = M = 0
    while m == M:
        feature_id = np.random.randint(low=0, high=n_features)
        feature = X[:, feature_id]
        m = feature.min()
        M = feature.max()
        #print(m, M, feature_id, X.shape)

    split_value = np.random.uniform(m, M, 1)
    left_X = X[feature <= split_value]
    right_X = X[feature > split_value]
    return left_X, right_X, feature_id, split_value

def iTree(X, add_index=False, max_depth = np.inf):
    ''' construct an isolation tree and returns the number of step required
    to isolate an element. A column of index is added to the input matrix X if
    add_index=True. This column is required in the algorithm. '''

    n_split = {}
    def iterate(X, count = 0):

        n_samples, n_columns = X.shape
        n_features = n_columns - 1

        if count > max_depth:
            for index in X[:,-1]:
                n_split[index] = count
            return

        if n_samples == 1:
            index = X[0, n_columns-1]
            n_split[index] = count
            return
        else:
            lX, rX, feature_id, split_value = _split_data(X)
            # Uncomment the print to visualize a draft of
            # the construction of the tree
            #print(lX[:,-1], rX[:,-1], feature_id, split_value, n_split)
            n_samples_lX, _ = lX.shape
            n_samples_rX, _ = rX.shape
            if n_samples_lX > 0:
                iterate(lX, count+1)
            if n_samples_rX >0:
                iterate(rX, count+1)

    if add_index:
        n_samples, _ = X.shape
        X = np.c_[X, range(n_samples)]

    iterate(X)
    return n_split

class iForest():
    ''' Class to construct the isolation forest.

    -n_estimators: is the number of trees in the forest,

    -sample_size: is the bootstrap parameter used during the construction
    of the forest,

    -add_index: adds a column of index to the matrix X. This is required and
    add_index can be set to False only if the last column of X contains
    already indeces.

    -max_depth: is the maximum depth of each tree
    '''
    def __init__(self, n_estimators=20, sample_size=None, add_index = True,
                 max_depth = 100):
        self.n_estimators = n_estimators
        self.sample_size = sample_size
        self.add_index = add_index
        self.max_depth = max_depth
        return

    def fit(self, X):
        n_samples, n_features = X.shape
        if self.sample_size == None:
            self.sample_size = int(n_samples/2)

        if self.add_index:
            X = np.c_[X, range(n_samples)]

        trees = [iTree(X[np.random.choice(n_samples,
                                          self.sample_size,
                                          replace=False)],
                       max_depth=self.max_depth)
                 for i in range(self.n_estimators)]

        self.path_length_ = {k:None for k in range(n_samples)}
        for k in self.path_length_.keys():
            self.path_length_[k] = np.array([tree[k]
                                             for tree in trees
                                             if k in tree])
        self.path_length_ = np.array([self.path_length_[k].mean() for k in
                                      self.path_length_.keys()])
        self.anomaly_score_ = _anomaly_score(self.path_length_, self.sample_size)
        return self

rawDescription: isolation forest algorithm in python

description: Isolation forest algorithm in python


code:
fig, ax = plt.subplots(1, 1)
ax.xaxis.set_tick_params(labeltop='on')

rawDescription: how to show tick labels on top of matplotlib plot?

description: Show tick labels on top of matplotlib plot


code:
# The figure subplot parameters.  All dimensions are a fraction of the
# figure width or height
figure.subplot.left    : 0.125  # the left side of the subplots of the figure
figure.subplot.right   : 0.9    # the right side of the subplots of the figure
figure.subplot.bottom  : 0.1    # the bottom of the subplots of the figure
figure.subplot.top     : 0.9    # the top of the subplots of the figure
figure.subplot.wspace  : 0.2    # the amount of width reserved for blank space between subplots
figure.subplot.hspace  : 0.2    # the amount of height reserved for white space between subplots

rawDescription: x position of matplotlib

description: X position of matplotlib


code:
import subprocess
process = subprocess.Popen('command', cwd = 'directory')

rawDescription: Python change the working directory for an exe opened with startfile


code:
#!/usr/bin/python3

from matplotlib import pyplot as plt

data = [range(n) for n in range(4,9)]

for i, x in enumerate(data):

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.pie(x, labels=x)

    fig.savefig("pie{0}.png".format(i))

rawDescription: Python Matplotlib: how to create a pie chart with variable values


code:
inFile = open(inFilename, "r")
outFile = open(outFilename, "w")
skipping = False
for line in infile:
    if skipping:
        # don't print this line no matter what,
        # hut stop skipping if "]" in line
        if "]" in line:
            skipping = False
    elif line.startswith("["):
        # don't print this line either no matter what,
        # and start skipping if "]" _not_ in the line
        skipping = "]" not in line
    else:
        outfile.write(line)
infile.close()
outfile.close()
if skipping:
    raise ValueError("hit end of file with unclosed '['!")

rawDescription: Removing text contained within brackets

description: Removing text contained within brackets


rawDescription: Scraping Contact Information from Several Unique Sites with Python


code:
allsectornames = set( sec['sname'] for record in dataset['users'] for sec in record['sectors'] )

summarize = lambda record:  record[ 'id' ]   # customize this to return whatever details you want (even just return the whole record itself if you prefer)

result = [
    {
        'sname':sname,
        'count':len(matches),
        'matches':[ summarize( match ) for match in matches ]
    }
    for sname in allsectornames
    for matches in [[
        record for record in dataset['users'] if sname in [ sec['sname'] for sec in record['sectors'] ]
    ]]
]

print(result)

rawDescription: How to group by nested list of dictionary key in python

description: Group by nested list of dictionary key in python


code:
class Day(models.Model):
    name = models.CharField(max_length=9)
    ordering = models.PositiveIntegerField()

    class Meta:
        ordering = ['ordering']

rawDescription: Order model by field with choices

description: Order model by field with choices


code:
db_test = DAL('mysql://root:root@localhost/test')

sqlddl = """CREATE TEMPORARY TABLE tmp LIKE people;
INSERT INTO tmp SELECT * FROM people;
INSERT INTO tmp SELECT * FROM people;"""

for statement in sqlddl.split(';'):
    db_test.executesql(statement.strip())

sqlselect = "SELECT * FROM tmp;"
results = db_test.executesql(sqlselect)

rawDescription: Using the web2py DAL with temp tables

description: Using the web2py DAL with temp tables


code:
import time
import datetime

# if for some reason this script is still running
# after a year, we'll stop after 365 days
for i in xrange(0,365):
    # sleep until 2AM
    t = datetime.datetime.today()
    future = datetime.datetime(t.year,t.month,t.day,2,0)
    if t.hour >= 2:
        future += datetime.timedelta(days=1)
    time.sleep((future-t).seconds)

    # do 2AM stuff

rawDescription: In Python, how can I put a thread to sleep until a specific time?


code:
import random

class Creature(object):

    def __init__(self, name, attr1, attr2):
        self.name = name
        self.attr1 = attr1
        self.attr2 = attr2

Squirrel = Creature("Squirrel", 20, 1)
Dog = Creature("Dog", 50, 10)
Goblin = Creature("Goblin", 100, 100)

creature_list = [Squirrel, Dog, Goblin]

def get_creature():
    return random.choice(creature_list)

current_target = get_creature()
print("The current target is %s, and has %s, and %s"
     % (current_target.name, current_target.attr1, current_target.attr2))

rawDescription: How to get attributes from an object using a variable as object name

description: Get attributes from an object using a variable as object name


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

# generate some random points
n = 1000
x = 4 * np.random.randn(n) + 15
y = 2 * np.random.randn(n) + 10

# somehow compute center of cloud, use e.g. medium or mean
x0 = np.median(x)
y0 = np.median(y)

# compute radius
r = np.sqrt((x - x0)**2 + (y - y0)**2)
t = 80 # percent
r0 = np.percentile(r, t)
n_within = (r < r0).sum()

# make plot
plt.plot(x, y, '.')
circle = plt.Circle((x0, y0), r0, color='r', fill=False)
plt.gca().add_artist(circle)
plt.title('Found center at ({:.2f}, {:.2f})\n'
          '{}% radius is {:.2f}\n'
          '{} / {} points within circle'.format(
          x0, y0, t, r0, n_within, n))
plt.axis([0, 30, 0, 20])
plt.show()

rawDescription: Plot circle that contains 80% (x, y) points


code:
from io import StringIO
import pandas as pd

players=StringIO(u"""Name,SICs
Agric,0100-0199
Agric,0210-0211
Food,2048-2048
Soda,1198-1200""")

df = pd.DataFrame.from_csv(players, sep=",", parse_dates=False).reset_index()

df2 = pd.DataFrame(columns=('Name', 'SIC'))

count = 0
for idx,r in df.iterrows():
    data = r['SICs'].split("-")
    for i in range(int(data[0]), int(data[1])+1):
                   df2.loc[count] = (r['Name'], i)
                   count += 1

rawDescription: Expand pandas dataframe based on range in a column


code:
tokens = [
    ('text', 'This '),
    ('enter', 'code'),
        ('text', 'example'),
    ('exit', None),
    ('text', ' is '),
    ('enter', 'a'),
        ('text', 'neither'),
        ('attr', ('href', '/defn/neither')),
    ('exit', None),
    ('text', ' complete[1] '),
    ('enter', 'strong'),
        ('text', 'nor'),
    ('exit', None),
    ('text', ' trite, '),
    ('enter', 'strong'),
        ('text', 'though '),
        ('enter', 'em'),
            ('text', 'simple'),
        ('exit', None),
    ('exit', None),
    ('text', '.')
]

rawDescription: Tokenizing complex input

description: Tokenizing complex input


code:
def __iter__(self):
    return iter(self.list)

rawDescription: Python - implement __iter__ or return a list's __iter__


code:
@gen_test
def test_async_sleep(self):
    start = time.time()
    resp1, resp2 = yield [
        self.http_client.fetch(self.get_url('/async')),
        self.http_client.fetch(self.get_url('/async')),
    ]
    diff = time.time() - start

rawDescription: How can I write a Tornado unit test which tests concurrency

description: Write a Tornado unit test which tests concurrency


code:
fig = plt.figure(figsize=(10,8))
a1 = fig.add_subplot(521)
a2 = fig.add_subplot(522)
a3 = fig.add_subplot(523)
a4 = fig.add_subplot(524)
a5 = fig.add_subplot(525)
a6 = fig.add_subplot(526)
a7 = fig.add_subplot(527)
a8 = fig.add_subplot(528)
a9 = fig.add_subplot(529)
a10 = fig.add_subplot(5,2,10)

rawDescription: Using matplotlib show 10 subplots all at once


rawDescription: How do I query a Django ForeignKey model field by choices' readable names?

description: Query a Django ForeignKey model field by choices' readable names


code:
def gz():
    """this should defer to gx and gy to
       generate [1, 2, 3, 11, 12, 13]"""
    yield from gx()
    yield from gy()

rawDescription: generators that defer to sub-generators

description: Generators that defer to sub-generators


rawDescription: Sort list of dictionaries by another list


rawDescription: How to do a non-greedy lstrip()

description: Do a non-greedy lstrip()


code:
attrs = {}

attrs["Attr1"] = "stamps"
attrs["Attr2"] = "ghouls"
if Attr != "":
    attrs["Attr3"] = Attr

node = ET.SubElement(root_node, "ChildNode1", **attrs)

rawDescription: How to conditionally insert an attribute into a node in Python using xml.etree.ElementTree

description: Conditionally insert an attribute into a node in Python using xml.etree.ElementTree


code:
import regex
s = 'cuacducucibcl'
regex.sub(r'((?:a|(?<!^)\G)[^abc\n]*)c', r'\1x', s)

rawDescription: Regex for matching this string

description: Regex for matching this string


code:
def getHostsFromList(master):
    p1 = subprocess.Popen(['get_hosts_from_master', master], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(['grep', '-v', '\['], stdin=p1.stdout, stdout=subprocess.PIPE)
    return p2.communicate()[0]

for master in getMasterList():
    for host in getHostsFromList(master):
        doStuff(host)

rawDescription: iterate over multiple nested results using subprocess

description: Iterate over multiple nested results using subprocess


code:
fh = open("NUL","w")
subprocess.Popen("taskkill /PID " + str(p.pid), stdout = fh, stderr = fh)
fh.close()

rawDescription: Disable console output from subprocess.Popen in Python


code: user_choice_list = userChoice.objects.filter(questionnaire=user_questionnaire.id).values('id' , 'question_id' , 'questionnaire_id', 'question__text', 'votes', 'text')

rawDescription: Django JOIN with column renaming to queryset


code:
from ghost import Ghost
ghost = Ghost()
page, extra_resources = ghost.open("http://jeanphi.fr")
assert page.http_status==200 and 'jeanphix' in ghost.content

rawDescription: Simulate user browsing by code

description: Simulate user browsing by code


code:
#__author__ = 'Telefonmann'
# -*- coding: utf-8 -*-

import os, re, ntpath, shutil

for root, dirs, files in os.walk(os.getcwd()):
    for file in files: # loops through directories and files
        if file.endswith(('.txt')): # only processes txt files
            full_path = ntpath.splitdrive(ntpath.join(root, file))[1]
            # builds correct path under Win 7 (and probably other NT-systems

            with open(full_path, 'r', encoding='utf-8') as f:
                content = f.read().replace('\n', '') # remove newline

                r = re.compile('To\s*cite\s*this\s*article:\s*(.*?),\s*Journal\s*of\s*Quantitative\s*Linguistics\s*,')
                m = r.search(content)
                # finds substring inbetween "To cite this article: " and "Journal of Quantitative Linguistics,"
                # also finds typos like "Journal ofQuantitative ..."

                if m:
                    full_title = m.group(1)

            print("full_title: {0}".format(full_title))
            full_title = (full_title.replace('<','') # removes/replaces forbidden characters in Windows file names
                .replace('>','')
                .replace(':',' -')
                .replace('"','')
                .replace('/','')
                .replace('\\','')
                .replace('|','')
                .replace('?','')
                .replace('*',''))

            pdf_name = full_path.replace('txt','pdf')
            # since txt and pdf files only differ in their format extension I simply replace .txt with .pdf
            # to get the right name

            print('File: '+ file)
            print('Full Path: ' + full_path)
            print('Full Title: ' + full_title)
            print('PDF Name: ' + pdf_name)
            print('....................................')
            # for trouble shooting

            dirname = ntpath.dirname(pdf_name)
            new_path = ntpath.join(dirname, "{0}.pdf".format(full_title))

            if ntpath.exists(full_path):
                print("all paths found")
                shutil.copy(pdf_name, new_path)
                # makes a copy of the pdf file with the new name in the respective directory

rawDescription: How read contents of txt files in different directories and rename other files according to


code:
def get_invert_dict(dict):
  {c:{key:v} for key,value in a.iteritems() for c,v in value.iteritems()}

#Test the function
new_dict = get_invert_dict(a)
print(new_dict)

rawDescription: use def in order to print a lot of values

description: Use def in order to print a lot of values


code: df2 = df1[df1[0].isin(conditions)]

rawDescription: Filter pandas dataframe using multiple conditions defined in list


code:
import inspect
inspect.getmembers(SomeClass, inspect.isdatadescriptor)

rawDescription: How can i get the property attributes in django model

description: Get the property attributes in django model


code:
import re
string = u"hey, nice to meet you! I'm Dennis."
re.split(r'(?<=[.,!])\s*', string)

rawDescription: python split strings with multi-delimiter and keep those delimiters python--fast way

description: Python split strings with multi-delimiter and keep those delimiters python--fast way


code:
code = [ grade.split()[0] for grade in grades ]
num = num = [ 100 if not int( grade.split()[1]) else int( grade.split()[1]) for grade in grades ] # replace 0 to 100

indexs  = [ i for i,item in enumerate(num) if item == min(num) ] # find min
#remove min in code and num
num = [ item for i, item in enumerate(num) if i not in indexs]
code = [ item for i, item in enumerate(code) if i not in indexs]

#weight grade based on code then average
numD= [ n for c,n in zip(code,num) if c == 'D' ]
numM= [ n for c,n in zip(code,num) if c == 'M' ]
final  =  sum(numD)*0.4/len(numD) +sum(numM)*0.6/len(numM)

rawDescription: List has multiple data types in a string format


code:
with open(file) as fin:
     rows = ( line.split('\t') for line in fin )
     d = { row[0]:row[1:] for row in rows }

rawDescription: Dictionary of lists from tab delimited file


code:
from webob import Request
r = Request.blank('/')
a_view_function(r)

rawDescription: Library like fakeweb for Python

description: Library like fakeweb for Python


code:
import urllib
from urllib.request import urlopen

x = None

class ABC(object):

 def __init__(self):
   global x
   x=urlopen('http://www.google.com/').read()

rawDescription: Global variables in Python 3 class

description: Global variables in Python 3 class


code:
def reverse(data_list):
    length = len(data_list)
    s = length

    new_list = [None]*length

    for item in data_list:
        s = s - 1
        new_list[s] = item
    return new_list

rawDescription: creating a reverse method for a python list from scratch

description: Creating a reverse method for a python list from scratch


code: set(d_1.keys()) == set(d_2.keys())

rawDescription: Pythonic way to check if two dictionaries have the identical set of keys?


code:
def to_complete(request):
    if request.session.has_key("complete_pending"):
        return HttpResponseForbidden()
    request.session["complete_pending"] = True
    return MobileResponseRedirect('/battle/complete/')

def complete(request):
    if not request.session.get("complete_pending", False):
        return HttpResponseForbidden()
    request.session["complete_pending"] = False
    do_something()
    return MobileResponse(html)

rawDescription: How to prohibit back or refresh using django

description: Prohibit back or refresh using django


code:
import matplotlib.pyplot as plt
import itertools

# put all the linestyles you want in the list below (non exhaustive here)
style=itertools.cycle(["-","--","-.",":",".","h","H"])

# assuming xseries and yseries previously created (each one is a list of lists)
for x,y in zip(xseries,yseries):
    plt.plot(x,y,"b"+style.next())
plt.show()

rawDescription: Matplotlib different dashed lines instead of coloured lines


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

box = dict(facecolor='yellow', pad=5, alpha=0.2)

fig = plt.figure()
fig.subplots_adjust(left=0.2, wspace=0.6)

# ax1 = fig.add_subplot(221)
ax1 = plt.subplot2grid((3,2), (0,0))
ax1.plot(2000*np.random.rand(10))
ax1.set_title('ylabels not aligned')
ax1.set_ylabel('misaligned 1', bbox=box)
ax1.set_ylim(0, 2000)

# ax3 = fig.add_subplot(223)
ax3 = plt.subplot2grid((3,2), (1,0))
ax3.set_ylabel('misaligned 2',bbox=box)
ax3.plot(np.random.rand(10))

labelx = -0.3  # axes coords

# ax2 = fig.add_subplot(222)
ax2 = plt.subplot2grid((3,2), (0,1))
ax2.set_title('ylabels aligned')
ax2.plot(2000*np.random.rand(10))
ax2.set_ylabel('aligned 1', bbox=box)
ax2.yaxis.set_label_coords(labelx, 0.5)
ax2.set_ylim(0, 2000)

# ax4 = fig.add_subplot(224)
ax4 = plt.subplot2grid((3,2), (1,1))
ax4.plot(np.random.rand(10))
ax4.set_ylabel('aligned 2', bbox=box)
ax4.yaxis.set_label_coords(labelx, 0.5)

ax5 = plt.subplot2grid((3,2), (2,0), colspan=2)
ax5.plot(np.random.rand(10))
ax5.set_ylabel('misaligned 3', bbox=box)

plt.show()

rawDescription: matplotlib, subplot that takes the space of two plots

description: Matplotlib, subplot that takes the space of two plots


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

d = np.sort(np.random.randint(0,1000,1000)).cumsum()

# Percentile values
p = np.array([0.0, 25.0, 50.0, 75.0, 100.0])

perc = mlab.prctile(d, p=p)

plt.plot(d)
# Place red dots on the percentiles
plt.plot((len(d)-1) * p/100., perc, 'ro')

# Set tick locations and labels
plt.xticks((len(d)-1) * p/100., map(str, p))

plt.show()

rawDescription: Percentiles on X axis with matplotlib

description: Percentiles on X axis with matplotlib


code:
import mongoegine as mdb

class User(mdb.Document):
    name = mdb.StringField()

    meta = {'allow_inheritance': True}

class ActiveUser(User):
    activation_date = mdb.DateTimeField()

class TempUser(User):
    date_limit = mdb.DateTimeField()

class Building(mdb.Document):
    address = mdb.StringField()

class Stuff(mdb.Document):
    user = mdb.ReferenceField(User)
    building = mdb.ReferenceField(Building)
    currently_with = mdb.StringField(options=['user','building'],required=True)

    @property
    def current_holder_of_stuff(self):
        if self.currently_with == "user":
            return self.user
        else:
            return self.building

rawDescription: How to let a Reference Field accept multiple Document schemas in MongoEngine?

description: Let a Reference Field accept multiple Document schemas in MongoEngine


code:
# this should be fairly self explanitory if you know ipf
# seed_matrix is your best bet at the totals, col_marginals are
# observed column marginals and row_marginals is the same for rows

def simple_ipf(seed_matrix, col_marginals, row_marginals, tolerance=1, cnt=0):
    assert np.absolute(row_marginals.sum() - col_marginals.sum()) < 5.0

    # first normalize on columns
    ratios = col_marginals / seed_matrix.sum(axis=0)
    seed_matrix *= ratios
    closeness = np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum()
    assert np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum() < .01
    # print "row closeness", closeness
    if closeness < tolerance:
        return seed_matrix

    # first normalize on rows
    ratios = row_marginals / seed_matrix.sum(axis=1)
    ratios[row_marginals == 0] = 0
    seed_matrix = seed_matrix * ratios.reshape((ratios.size, 1))
    assert np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum() < .01
    closeness = np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum()
    # print "col closeness", closeness
    if closeness < tolerance:
        return seed_matrix

    if cnt >= 50:
        return seed_matrix

    return simple_ipf(seed_matrix, col_marginals, row_marginals,
                      tolerance, cnt+1)

rawDescription: Shifting disaggregate distribution to match more aggregate level distribution


code:
filechooser = Gtk.FileChooserButton()
filechooser.set_action(Gtk.FileChooserAction.SELECT_FOLDER)
filechooser.set_create_folders(True)

rawDescription: Gtk.FileChooserButton does not react to Gtk.FileChooserAction.SELECT_FOLDER


code: list[list.index('foo')] = None

rawDescription: Efficient way to replace a single element in the list with None


code:
import win32com.client

TASK_STATE = {0: 'Unknown',
              1: 'Disabled',
              2: 'Queued',
              3: 'Ready',
              4: 'Running'}

scheduler = win32com.client.Dispatch('Schedule.Service')
scheduler.Connect()

folders = [scheduler.GetFolder('\\')]
while folders:
    folder = folders.pop(0)
    folders += list(folder.GetFolders(0))
    for task in folder.GetTasks(0):
        print('Path       : %s' % task.Path)
        print('State      : %s' % TASK_STATE[task.State])
        print('Last Run   : %s' % task.LastRunTime)
        print('Last Result: %s\n' % task.LastTaskResult)

rawDescription: Python check for Completed and failed Task Windows schedluer


code:
s = 1
locals()['s']

rawDescription: python equivalent of get() in R (= use string to retrieve value of symbol)

description: Python equivalent of get() in R (= use string to retrieve value of symbol)


code:
def resize(new_size):
     g = pgmagick.Geometry(new_size) # distinguishes whether width, height or both given
     image = pgmagick.Image(img)
     rw, rh = image.size()
     w, h = g.width(), g.height()
     if w and not h:
         g.height(int(rh * w * 1.0/rw))
     elif h and not w:
         g.width(int(rw * h * 1.0/rh))

     image.scale(g)

rawDescription: Pgmagick resize image by width

description: Pgmagick resize image by width


rawDescription: Adding two pandas Series while keeping (non-numeric) values at non-matching indices


code:
from algorithms.sorting import bubble_sort
my_list = bubble_sort.sort(my_list)

rawDescription: How to specify algorithm used to sort a list in python

description: Specify algorithm used to sort a list in python


code:
#!/usr/bin/env python

import sys
from PyQt4 import QtGui, QtCore
from popplerqt4 import Poppler

class Application(QtGui.QApplication):

    def __init__(self):
        QtGui.QApplication.__init__(self, sys.argv)

        scene = QtGui.QGraphicsScene()
        scene.setBackgroundBrush(QtGui.QColor('darkGray'))
        layout = QtGui.QGraphicsLinearLayout(QtCore.Qt.Vertical)
        document = Poppler.Document.load('/home/test.pdf')
        document.setRenderHint(Poppler.Document.Antialiasing)
        document.setRenderHint(Poppler.Document.TextAntialiasing)
        for number in range(document.numPages()):
            page = document.page(number)
            image = page.renderToImage(100, 100)
            pixmap = QtGui.QPixmap.fromImage(image)
            container = QtGui.QLabel()
            container.setFixedSize(page.pageSize())
            container.setStyleSheet("Page { background-color : white}")
            container.setContentsMargins(0, 0, 0, 0)
            container.setScaledContents(True)
            container.setPixmap(pixmap)
            label = scene.addWidget(container)
            layout.addItem(label)

        graphicsWidget = QtGui.QGraphicsWidget()
        graphicsWidget.setLayout(layout)
        scene.addItem(graphicsWidget)
        self.view = View(scene)
        self.view.show()

class View(QtGui.QGraphicsView):

    def __init__(self, parent = None):
        QtGui.QGraphicsView.__init__(self, parent)

    def wheelEvent(self, event):

        if event.delta() > 0:
            self.scale(1.1, 1.1)
        else:
            self.scale(0.9, 0.9)

if __name__ == "__main__":
        application = Application()
        sys.exit(application.exec_())

rawDescription: How to prevent zooming in QScrollArea from being so rickety-flickery?

description: Prevent zooming in QScrollArea from being so rickety-flickery


code:
def __i(x, y):
    assert(0 <= x < 80)
    assert(0 <= y < 60)
    i = (y*80 + x) * 4
    return i
def red(x, y):
    return __a[__i(x, y)]
def green(x, y):
    return __a[__i(x, y) + 1]
def blue(x, y):
    return __a[__i(x, y) + 2]
def rgb(x, y):
    i = __i(x, y)
    return __a[i], __a[i + 1], __a[i + 2]
def set_rgb(x, y, r, g, b):
    i = __i(x, y)
    _a[i] = r
    _a[i + 1] = g
    _a[i + 2] = b

# example:
r, g, b = rgb(23, 33)

rawDescription: Using python's map() with multiple args?

description: Using python's map() with multiple args


code:
def parse(self, response):
    links = LinkExtractor().extract_links(response)
    return (Request(url=link.url, callback=self.parse_page) for link in links)

def parse_page(self, response):
    # name = manipulate response.url to be a unique file name
    with open(name, 'wb') as f:
        f.write(response.body)

rawDescription: Scrapy keep all unique pages based on a list of start urls


code:
new_data = {}
# so here create a new file with multiple rows
# where "attribute to change" is the attribute modified
# here  between 5 and 95 in steps of 5
for i in range(5, 100, 5):
    with open(file_name) as data_file:
        data = json.load(data_file)

    # give the new row a unique id_num
    data[str(id_num)] = data.pop('1')
    generate_config_file(data[str(id_num)], 'attribute to change', i)
    new_data[str(id_num)] = data[str(id_num)]
    #json.dump(data, out_file, sort_keys=True, indent=4)
    id_num += 1
json.dump(new_data, out_file, sort_keys=True, indent=4)

rawDescription: Mass creation of JSON config files

description: Mass creation of JSON config files


code:
from sklearn import datasets
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_curve,auc
from statsmodels.tools import categorical
import numpy as np

iris = datasets.load_iris()
# Use only data for 2 classes.
X = iris.data[(iris.target==0) | (iris.target==1)]
Y = iris.target[(iris.target==0) | (iris.target==1)]

# Class 0 has indices 0-49. Class 1 has indices 50-99.
# Divide data into 80% training, 20% testing.
train_indices = list(range(40)) + list(range(50,90))
test_indices = list(range(40,50)) + list(range(90,100))
X_train = X[train_indices]
X_test = X[test_indices]
y_train = Y[train_indices]
y_test = Y[test_indices]

###########################################################################
###### Convert categorical variable to matrix and merge back with training
###### data.

# Fake categorical variable.
catVar = np.array(['a']*40 + ['b']*40)
catVar = categorical(catVar, drop=True)
X_train = np.concatenate((X_train, catVar), axis = 1)

catVar = np.array(['a']*10 + ['b']*10)
catVar = categorical(catVar, drop=True)
X_test = np.concatenate((X_test, catVar), axis = 1)
###########################################################################

# Model and test.
clf = GradientBoostingClassifier(learning_rate=0.01,max_depth=8,n_estimators=50).fit(X_train, y_train)

prob = clf.predict_proba(X_test)[:,1]   # Only look at P(y==1).

fpr, tpr, thresholds = roc_curve(y_test, prob)
roc_auc_prob = auc(fpr, tpr)

print(prob)
print(y_test)
print(roc_auc_prob)

rawDescription: How to handle categorical variables in sklearn GradientBoostingClassifier?

description: Handle categorical variables in sklearn GradientBoostingClassifier


code: np.outer(np.arccos(xxa), nd)

rawDescription: Multiplying column and row vectors in Numpy


code:
from operator import itemgetter

with open('test.txt') as infile, open('out.txt', 'w') as outfile:
    # sort input by 2nd column
    sorted_lines = sorted(
        (line.strip().split() for line in infile),
        key=itemgetter(1)
        )

    # output sorted input
    for line in sorted_lines:
        outfile.write('\t'.join(line))
        outfile.write('\n')

    # discard duplicates in already sorted sequence => uniq items
    prev_item = None
    for item in (line[1] for line in sorted_lines):
        if item != prev_item:
            prev_item = item
            outfile.write(item)
            outfile.write('\n')

rawDescription: sorting file according to a column and getting uniq elements

description: Sorting file according to a column and getting uniq elements


code:
@route('<path:re:.+[^/]$>')
def add_slash(path):
    return redirect(path + "/")

rawDescription: bottle: global routing filter

description: Bottle: global routing filter


code:
import threading

def starter_function(cmd_to_execute):
    os.system(cmd_to_execute)

execution_thread = threading.Thread(target=starter_function, args=(cmd_to_execute,))

execution_thread.start()

rawDescription: Run command in python

description: Run command in python


code: f = open('/tmp/x', 'wb+')

rawDescription: Python tell() behaving differently regarding position in the current file


code:
class HDD(Base):
    __abstract__ = True
    _id = ...

class SDD(HDD):
    __tablename__ = 'SSDs'

class SSD2(HDD):
    __tablename = 'SSDs2'

rawDescription: SQLAlchemy - Multiple Classes, Identical Tables


code: plt.xlabel(r'Primary T$_{\rm eff}$')

rawDescription: remove italics in latex subscript in matplotlib

description: Remove italics in latex subscript in matplotlib


rawDescription: Doing math in pandas using .ix

description: Doing math in pandas using .ix


code:
from io import StringIO
foo = StringIO()
for letter in map(chr, range(128)):
    foo.write(letter)
foo.seek(0)
foo.read()

rawDescription: Most efficient way to print strings in Python?


code:
db = MySQLdb.connect(host="your_host", user="your_user", passwd="your_password", db="your_database")
write_cursor = db.cursor()
write_list = []
for obj in parsed_objs:
  key = 'randomstring'
  write_list.append( (key,obj[key]) )

sql = "REPLACE INTO infos (`key`, `value`) VALUES (%s, %s)"
write_cursor.executemany(sql, write_list)
db.commit()

rawDescription: python best way to insert 60k rows in mysql

description: Python best way to insert 60k rows in mysql


code:
#!/usr/bin/env python

import numpy as np
import h5py

# Define a color palette
pal =  np.array([[0,     0, 168],
                 [0,     0, 252],
                 [0,   168, 252],
                 [84,  252, 252],
                 [168, 252, 168],
                 [0,   252, 168],
                 [252, 252,  84],
                 [252, 168,   0],
                 [252,   0,   0]],
                 dtype=np.uint8
               )

# Generate some data/image
x = np.linspace(0,pal.shape[0]-1)
data,Y = np.meshgrid(x,x)

# Create the HDF5 file
f = h5py.File('test.h5', 'w')

# Create the image and palette dataspaces
dset = f.create_dataset('img', data=data)
pset = f.create_dataset('palette', data=pal)

# Set the image attributes
dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
dset.attrs['IMAGE_SUBCLASS'] =  'IMAGE_INDEXED'
dset.attrs['IMAGE_MINMAXRANGE'] = np.array([0,255], dtype=np.uint8)
dset.attrs['PALETTE'] = pset.ref

# Set the palette attributes
pset.attrs['CLASS'] = 'PALETTE'
pset.attrs['PAL_VERSION'] = '1.2'
pset.attrs['PAL_COLORMODEL'] = 'RGB'
pset.attrs['PAL_TYPE'] = 'STANDARD8'

# Close the file
f.close()

rawDescription: Add raster image to HDF5 file using h5py


code:
from collections import Counter
from itertools import chain
def most_pop(pop):
    pop_flat = chain.from_iterable(range(i,j+1) for i,j in pop)
    return Counter(pop_flat).most_common()
most_pop([(1920, 1939), (1911, 1944), (1920, 1955), (1938, 1939)])[0]

rawDescription: Find the year with the most number of people alive in Python


code: [ a*a for a in xrange(1, n + 1) ]

rawDescription: Counting and generating perfect squares

description: Counting and generating perfect squares


code:
class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I'm the 'x' property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

rawDescription: how do you pass a value to a @property in python?

description: Pass a value to a @property in python


code: gtk_message_dialog_get_message_area ()

rawDescription: Is it possible to change the position of a Gtk.MessageDialog's caption text?


code:
df.to_excel(writer, sheet)
wb = writer.book
ws = writer.sheets[sheet]
ws.write(1, 4, "DataFrame contains ...")
writer.save()

rawDescription: python pandas: manually write to the same Excel after exporting a dataframe

description: Python pandas: manually write to the same Excel after exporting a dataframe


code:
import array, struct

two_unsigned_bytes = [255, 254] # represented by ints
byte_array = array.array("B", two_unsigned_bytes)
# change above to "b" if the ints represent signed bytes ie. in range -128 to 127
signed_16_bit_int, = struct.unpack(">h", byte_array)
float_result = float(signed_16_bit_int)

rawDescription: Convert a list of ints to a float

description: Convert a list of ints to a float


code:
from dateutil.parser import parse

s1 = "2013-11-21T07:45:51ZUTC+0000" # Europe
s2 = "2013-11-21T13:15:28ZUTC+0530" # India

def new_parse(string):
    return parse(string.replace('ZUTC', ''))

dt1 = new_parse(s1)
dt2 = new_parse(s2)
print (dt1-dt2).total_seconds()

rawDescription: How to calculate the difference of two time strings with timezone in Python 2.7?

description: Calculate the difference of two time strings with timezone in Python 2.7


code:
post_data = [('name','Gladys'),]     # a sequence of two element tuples
result = urllib2.urlopen('http://example.com', urllib.urlencode(post_data))
content = result.read()

rawDescription: How to send a POST request using django?

description: Send a POST request using django


code: total_inches_of_snow = sum(inches_of_snow(date) for date in list_of_dates)

rawDescription: which is faster and efficient between generator expression or itertools.chain for iterating over large list?

description: Which is faster and efficient between generator expression or itertools.chain for iterating over large list


code: min(q, key = lambda x: (x[1],x[0]))

rawDescription: Comparing lists with min() based on out of order comparisons


code:
ani = wx.animate.Animation("anim.gif")
frameImage = ani.GetFrame(cur_frame)
bmp = frameImage.ConvertToBitmap()
dc.DrawBitmap(bmp, 0, 0)

rawDescription: How to display .gif (with animation) on dc

description: Display .gif (with animation) on dc


code:
for i in xrange(1, 100):
    subprocess.call(["./task.sh", arg1], cwd=("/some%d" % i))

rawDescription: Running a subprocess bash script from Python in the scripts current directory


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

# Generate some data to plot...
x = np.arange(1000)
y = np.random.normal(0, 1, y.size).cumsum()
y[y < 0] *= -1

fig, ax = plt.subplots()
# Fill the region between your curve and 0 with red...
ax.fill_between(x, y, facecolor='red', edgecolor='none')

# Optional...
ax.grid(True, color='white', linewidth=2, linestyle='-')
ax.set(axisbelow=True, axis_bgcolor='lightgray')
ax.tick_params(direction='out')

plt.show()

rawDescription: How to get a nice API Plot in Python?

description: Get a nice API Plot in Python


code: print(type(x))

rawDescription: How to display the datatype of a variable in a python file

description: Display the datatype of a variable in a python file


code:
import datetime
from dateutil.rrule import *

now = datetime.datetime.today().date()
days = rrule(MONTHLY, dtstart=now, bymonthday=25)
print (days[0])  # datetime.datetime(2016, 2, 25, 0, 0)
print (days[1])  # datetime.datetime(2016, 3, 25, 0, 0)

rawDescription: How to get the next specific day of the month in Python?

description: Get the next specific day of the month in Python


code: import sys; print(sys.path)

rawDescription: Python 2 to Python 3: can't get multiprocessing to work


code:
from multiprocessing import Lock, Process, Queue, current_process
import time

class Testclass(object):
    def __init__(self, x):
        self.x = x

def toyfunction(testclass):
    testclass.product = testclass.x * testclass.x
    return testclass

def worker(work_queue, done_queue):
    try:
        for testclass in iter(work_queue.get, 'STOP'):
            print(testclass.counter)
            newtestclass = toyfunction(testclass)
            done_queue.put(newtestclass)

    except:
        print('error')

    return True

def main():

    counter = 1

    database = []
    while counter <= 100:
        database.append(Testclass(10))
        counter += 1
        print(counter)

    workers = 8
    work_queue = Queue()
    done_queue = Queue()
    processes = []

    start = time.clock()
    counter = 1

    for testclass in database:
        testclass.counter = counter
        work_queue.put(testclass)
        counter += 1
        print(counter)

    print('items loaded')
    for w in range(workers):
        p = Process(target=worker, args=(work_queue, done_queue))
        p.start()
        processes.append(p)
        work_queue.put('STOP')

    for p in processes:
        p.join()

    done_queue.put('STOP')

    newdatabase = []
    for testclass in iter(done_queue.get, 'STOP'):
        newdatabase.append(testclass)

    print(time.clock()-start)
    print("Done")
    return(newdatabase)

if __name__ == '__main__':
    database = main()

rawDescription: Python multiprocessing IOError: [Errno 232] The pipe is being closed


code: df.apply(lambda r: r[r['Fields']-1:3].sum(), axis=1)

rawDescription: Pandas: Adding x number of Columns together based on value (x) in another column


code:
stringexample = 'mystri$ng&'
'*' in stringexample
'$' in stringexample
'&' in stringexample

rawDescription: Check special character in string?

description: Check special character in string


code:
xml_as_str = '''
<v1:Header>
    <v2:Person>Foo Bar</v2:Person>
    <v2:Email>foo@bar.com</v2:Email>
</v1:Header>
'''
from lxml import etree
tree = etree.fromstring(xml_as_str, etree.XMLParser(recover=True))
tree.xpath('//*[local-name()="v2:Person"]/text()')[0]

rawDescription: Why etree.find doesn't find the element for the provided example


code:
some_lst = [1, 2, 3, 4, 5, 6, 7, 8, 9]
l = [some_lst[i:i+3] for i in xrange(len(some_lst)-2)]

rawDescription: What is the most elegant way of finding the n-length consecutive sub-lists of a list in Python?


code:
class TableItem(QTableItem):
    def alignment(self):
        if is_pseudo_number(self.text()):
            return Qt.AlignRight
        return QTableItem.alignment(self)
...

table.setItem(row, column, TableItem('5.5.5'))

rawDescription: PyQt 3.3.6: How to align text in a table?


code:
global_cache = {}
def cached_listdir(path):
    res = global_cache.get(path)
    if res is None:
        res = os.listdir(path)
        global_cache[path] = res
    return res

rawDescription: python 3 replacement for dircache?

description: Python 3 replacement for dircache


code:
from azure.servicebus import ServiceBusService

key_name = 'RootManageSharedAccessKey' # SharedAccessKeyName from Azure portal
key_value = 'hdckR8xd*********************u5a84RoZSQHE=' # SharedAccessKey from Azure portal
service_namespace = 'myservice-ns' # service bus namespace

sbs = ServiceBusService(service_namespace,
                        shared_access_key_name=key_name,
                        shared_access_key_value=key_value)

sbs.create_event_hub('myhub')

sbs.send_event('myhub', '[{"name":"derek", "gender":"male"},{ "DeviceId":"dev-01", "Temperature":"37.0" }]')

rawDescription: How to send multiple messages to eventhub using python

description: Send multiple messages to eventhub using python


code: reduce(lambda lst, val: [min(lst[0],val[0]), max(lst[1],val[1])],iter(d.itervalues()))

rawDescription: How can I get upper and lower limits in 1 line?

description: Get upper and lower limits in 1 line


rawDescription: Iterating through two separate panda data frames via for loop in python?


code:
import curses

with curses_screen() as stdscr:
    pad = curses.newpad(100, 100)
    pad.addstr(0,0, curses.longname())
    for i in range(1, 10):
        pad.addstr(i,0, str(i))

    coord = 5, 5, 20, 75
    pad.refresh(0, 0, *coord)

    KEY_UP, KEY_DOWN = 'AB'
    y = 0
    for c in iter(pad.getkey, 'q'):
        if c in '\x1b\x5b': continue # skip escape seq
        y -= (c == KEY_UP)
        y += (c == KEY_DOWN)
        y = min(max(y, 0), 9)
        pad.refresh(y, 0, *coord)

rawDescription: How to move pad correctly?

description: Move pad correctly


code:
import lxml.html

def add_css_code(webpageString, linkString):
    root = lxml.html.fromstring(webpageString)
    link = lxml.html.fromstring(linkString).find('.//link')
    head = root.find('.//head')
    title = head.find('title')
    if title == None:
        where = 0
    else:
        where = head.index(title) + 1
    head.insert(where, link)
    return lxml.html.tostring(root)

webpageString1 = "<title>test</title>webpage content"
webpageString2 = "webpage content"
linkString = "<link rel='stylesheet' type='text/css'>"

print(add_css_code(webpageString1, linkString))
print(add_css_code(webpageString2, linkString))

rawDescription: How to insert a HTML element in a tree of lxml.html

description: Insert a HTML element in a tree of lxml.html


code:
import re

text = "Kn"
text = re.escape(text) # make sure there are not regex specials
authors = Author.objects.filter(name__iregex=r"(^|\s)%s" % text)

rawDescription: Django: search on first letters of individual words within phrase?


rawDescription: I want selenium to click on a link

description: I want selenium to click on a link


code:
from sqlalchemy import select
from sqlalchemy.sql import table, column
t = table('t', column('a'), column('b'), column('c'))
stmt1 = select([t])
print(stmt1)
print(stmt1.with_only_columns([t.c.b, t.c.a]))
print(stmt1.with_only_columns([stmt1.c.b, stmt1.c.a]))
stmt1_cols = dict((c.key, c) for c in stmt1.inner_columns)
stmt1_cols
dict(t.c)
assert stmt1_cols == dict(t.c)
print(stmt1.with_only_columns([stmt1_cols['b'], stmt1_cols['a']]))

rawDescription: Rearrange columns in SQLAlchemy Select object


code: import LongModuleName as LM

rawDescription: When is 'from SomeFile import *' okay

description: When is 'from SomeFile import *' okay


code:
from lxml import etree

doc = etree.fromstring(some_xml)
nodes = doc.xpath('some_query')
for node in nodes:
    if node.text == 'smth':
        reference = node

reference.text = 'hello world'

rawDescription: reference to lxml node

description: Reference to lxml node


rawDescription: How to map series in different DataFrames

description: Map series in different DataFrames


rawDescription: Trying to make columns in text file from python


code:
def clean_string(string_val):
    string_val = '\"' + string_val.replace('\"','\'\'') + '\"'
    string_val = string_val.replace('$','\$')
    return string_val[:150]

rawDescription: Shorten string in python

description: Shorten string in python


code:
def checkStuff():
    doSomething = all(i.myAttribute == 'banana' for i in listOfObjects)
    if doSomething: print("All bananas, sir!")

rawDescription: Retrieving if all objects in a list have the same value for an attribute in python


code:
temp = self.largeArray = zeros((1000,1000,10,20))
for y in temp.size:
    for x in temp1.size:
        self.largeArray[x,y] = train()

rawDescription: Memory never released when using Python classes and numpy


rawDescription: How to reverse key value pair in list with dictionary as value?

description: Reverse key value pair in list with dictionary as value


code:
from twisted.internet.protocol import ServerFactory
from twisted.internet.task import deferLater
from twisted.internet import reactor
from twisted.internet.interfaces import ITCPTransport
from twisted.protocols.basic import LineReceiver

class MyProtocol(LineReceiver):
    delimiter = "\n"
    def connectionMade(self):
        if ITCPTransport.providedBy(self.transport):
            self.transport.setTcpKeepAlive(1)
        self.pendingResponses = []

    def lineReceived(self, line):
        split = line.rstrip("\r").split(None, 1)
        command = split[0]
        if command == b"request":
            # requesting a computed response
            payload = split[1]
            self.sendLine("thinking...")
            (self.factory.service.computeResponse(payload)
             .addCallback(self.sendResponses))
        elif command == b"ok":
            # acknowledging a response; send the next response
            if self.pendingResponses:
                self.sendOneResponse()
            else:
                self.sendLine(b"done")

    def sendOneResponse(self):
        self.sendLine(b"response " + self.pendingResponses.pop(0))

    def sendResponses(self, listOfResponses):
        self.pendingResponses.extend(listOfResponses)
        self.sendOneResponse()

class MyFactory(ServerFactory):
    protocol = MyProtocol

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

class MyService(object):
    def computeResponse(self, request):
        return deferLater(
            reactor, 1.0,
            lambda: [request + b" 1", request + b" 2", request + b" 3"]
        )

from twisted.internet.endpoints import StandardIOEndpoint
endpoint = StandardIOEndpoint(reactor)

endpoint.listen(MyFactory(MyService()))
reactor.run()

rawDescription: Twisted: how to read from a client socket after writing data to the same socket?


code:
import os
try:
    user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
    user_paths = []

rawDescription: How do I find out my python path using python?

description: Find out my python path using python


code:
import requests
r = requests.head('http://bit.ly/IFHzvO', allow_redirects=True)
print(r.url)

rawDescription: How to un-shorten (resolve) a url using python, when final url is https?

description: Un-shorten (resolve) a url using python, when final url is https


code:
root = FloatLayout()
layout = BoxLayout(orientation='vertical')
layout1 = BoxLayout()
button1 = TextInput()
button2 = TextInput()
button3 = Button()
layout.add_widget(button1)
layout1.add_widget(button2)
layout1.add_widget(button3)
layout.add_widget(layout1)
root.add_widget(layout)

rawDescription: How to colspan in kivy with python

description: Colspan in kivy with python


code:
self.context['view'].__class__.__name__
# or type(self.context['view']).__name__  something like that.

rawDescription: Get view's name in serializer

description: Get view's name in serializer


code:
new_list = [{'Table':'A', 'Column':'C1', 'DataType':'int'},
        {'Table':'A', 'Column':'C2', 'DataType':'varchar'},
        {'Table':'A', 'Column':'C2', 'DataType':'numeric'}
       ]

header = new_list[0].keys()

with open('my_log.log', 'w') as log:
   log.write("|".join(header))
   log.write("\n")
   for data in new_list:
      log.write("|".join(data[h] for h in header))
      log.write("\n")

rawDescription: Writing list of dict into file in python


code:
class UniqueVisitsRecord(db.Model):
    # be careful setting date correctly if processing at midnight
    activity_date = db.DateProperty()
    event_count = IntegerProperty()

rawDescription: Getting DISTINCT users on Google App Engine


code:
def changeAge(newAge):
    import bar
    bar.age = newAge

rawDescription: How to change value of variable from a different .py file in Python

description: Change value of variable from a different .py file in Python


rawDescription: Overly complex expression to calculate the change in a Pandas series


code:
import ast
x = "[['AX34 GFV','Name: John Smith1, Address: x, Number: x'],['AX34 ABC','Name: John Smith2, Address: x, Number: x'],['UH23 HUH','Name: John Smith3, Address: x, Number: x'],['ER54 SDR','Name: John Smith4, Address: x, Number: x'],['FE54 VMR','Name: John Smith5, Address: x, Number: x'],['23FX SDA','Name: John Smith6, Address: x, Number: x'],['AS23 JAS','Name: John Smith7, Address: x, Number: x']]  "
ast.literal_eval(x)

rawDescription: Importing data as list from text files

description: Importing data as list from text files


rawDescription: IN condition in Where clause on Peewee

description: IN condition in Where clause on Peewee


code:
def __init__(self, *args, **kwargs):
    if 'context' in kwargs:
        context = kwargs.get('context')
        if 'user' in context:
            if SOME_CONDITION:
                super(AlbumSerializer, self).__init__(*args, **kwargs)
                self.fields.pop(key)
                pass
    super(AlbumSerializer, self).__init__(*args, **kwargs)

rawDescription: Generate custom response in DRF ModelSerializer


code:
def is_ascii(s):
    return all(ord(c) < 128 for c in s)

rawDescription: How to check if a string in Python is in ASCII?

description: Check if a string in Python is in ASCII


code:
html = '''

header

blah blah

eiaoiefj

capture this next

don'tcapturethis

capture this too but separately after "capture this next"

'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
quote1 = soup.blockquote
quote1.text
quote2 = quote1.find_next_siblings('blockquote')
quote2.text

rawDescription: Finding next occuring tag and its enclosed text with Beautiful Soup


code:
import dis
dis.dis(lambda: None not in x)
dis.dis(lambda: not None in x)

rawDescription: "None not in" vs "not None in"

description: "None not in" vs "not None in"


code:
def diff_elements(lst):
    """
    >>> list(diff_elements([]))
    []
    >>> list(diff_elements([1]))
    []
    >>> list(diff_elements([1, 2, 4, 7]))
    [1, 2, 3]
    """
    as_iter = iter(lst)
    last = next(as_iter)
    for value in as_iter:
        yield value - last
        last = value

rawDescription: Better solution to get differences between list elements


code: keys = sorted(preprocessing_dict.keys(), key=len, reverse=True)

rawDescription: replace text from dictionary priority to longer string

description: Replace text from dictionary priority to longer string


rawDescription: regular expression: match everything but single forward- and double backslashes

description: Regular expression: match everything but single forward- and double backslashes


code:
x = np.timedelta64(2069211000000000, 'ns')
days = x.astype('timedelta64[D]')
days / np.timedelta64(1, 'D')

rawDescription: extracting days from a numpy.timedelta64 value

description: Extracting days from a numpy.timedelta64 value


code:
import numpy as np
uf = np.frompyfunc(f, 2, 1) # f has 2 inputs, 1 outputs
pd.DataFrame(uf.outer(rows, columns), index=rows, columns=columns)

rawDescription: How to create a pandas DataFrame from its indexes and a two variable function?

description: Create a pandas DataFrame from its indexes and a two variable function


code:
import time

start_time = time.time()
interval = 1
for i in range(20):
    time.sleep(start_time + i*interval - time.time())
    f()

rawDescription: Run a python function every second

description: Run a python function every second


rawDescription: How to remove the items from list in python based on other list

description: Remove the items from list in python based on other list


code:
from lxml import etree
import requests

s = "http://www.dlib.org/dlib/november14/beel/11beel.html"
page = requests.get(s)
tree = etree.HTML(page.text)
element = tree.xpath('./body/form/table[3]/tr/td/table[5]')
content = etree.tostring(element[0])

rawDescription: XPath/Python - How to get different html tags and text inside a <div>


code: next(x for x in l if x[1] == 2)[0]

rawDescription: Python: search through list of tuples

description: Python: search through list of tuples


code:
class MyClass:
    def func(arg):
        if isinstance(arg, MyClass):
            # if we were called as arg.func(), get the actual arg value from an attribute
            arg = arg.some_attribute

        # do something with arg
        print(arg)

# MyClass.func can be called like a static method
MyClass.func("foo")

# or as a method of an instance of MyClass
instance = MyClass()
instance.some_attribute = "bar"
instance.func()

rawDescription: class method with static and not-static overloads

description: Class method with static and not-static overloads


code:
img = np.empty((100,100,1),dtype = np.uint16)
image = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
cvuint8 = cv2.convertScaleAbs(image)
cvuint8.dtype

rawDescription: How to convert an image from np.uint16 to np.uint8?

description: Convert an image from np.uint16 to np.uint8


code:
import understand

understand_file="C:\\Users\\dlewin\\myproject.udb"
#Create a list with all the cases from the file
def find_cases(file):
    returnList =  []
    for lexeme in file.lexer(False,8,False,True):
      if ( lexeme.text() == "case" ) :
        returnList.append(lexeme.line_end())    #line nb
        returnList.append(lexeme.text())        #found a case
    return returnList

def find_identifiers(file):
  returnList =  []

  # Open the file lexer with macros expanded and  inactive code removed
  for lexeme in file.lexer(False,8,False,True):
    if(lexeme.token() == "Identifier"):
        returnList.append(lexeme.line_end())    #line nb
        returnList.append(lexeme.text())        #identifier found
  return returnList

db = understand.open(understand_file )                                      # Open Database
file = db.lookup("mysourcefile.cpp","file")[0]
print (file.longname())
liste_idents = find_identifiers(file)
liste_cases = find_cases(file)

rawDescription: Parsing a C file in python for analyses

description: Parsing a C file in python for analyses


code:
num = 0
for ch in my_string:
    num = num << 8 + ord(ch)

rawDescription: Represent string as an integer in python


code:
lis = [{'date': 1, 'value':5}, {'date':2,'value':3}]
{x['date']:x['value'] for x in lis}

rawDescription: Python: Map list of two-entry-dicts to dict with first entry as key and second as value


code:
children = {5,10,15,20,25}

globalChildren = {1,2,3,4,5,6,7,8,9,10,12,14,16,18,20}

difference = children - globalChildren
shared = children & globalChildren

rawDescription: Arithmetic operators on lists/tuples in Python?


code:
import ctypes
c_long_p = ctypes.POINTER(ctypes.c_long)
some_long = ctypes.c_long(42)
ctypes.addressof(some_long)
ctypes.cast(4300833936, c_long_p)
ctypes.cast(4300833936, c_long_p).contents

rawDescription: How to dereference a memory location from python ctypes?

description: Dereference a memory location from python ctypes


code:
from fabric.api import run
from fabric.tasks import execute

def do_something():
    run("echo $RANDOM")

if __name__ == "__main__":
    execute(do_something, hosts=["username@host"])

rawDescription: fabric API direct call in python

description: Fabric API direct call in python


code:
import re

# Sliced_string will only contain the characters between '==See also==' and '==References=='
sliced_string = re.findall(r'==See also==(.*?)==References==', my_string, re.DOTALL)[-1]

# Removes stars and brackets
for unwanted_char in '[]*':
    sliced_string = sliced_string.replace(unwanted_char, '')

# Creates a list of strings (also removes empty strings)
final_list = sliced_string.split('\n')
final_list = [elem for elem in final_list if elem != '']

print(final_list)

rawDescription: Parsing large strings in python

description: Parsing large strings in python


code: ['%s%d' % (base_url, x) for x in number_list]

rawDescription: Increment scraping using Scrapy on the URL


code:
import uuid
import base64

gen_id = uuid.uuid1()  # generate type 1 UUID
id_bytes = gen_id.bytes  # take its bytes
encoded = base64.b64encode(id_bytes, b'-_')
encoded = encoded.replace(b'=', b'')  # remove the padding bytes
result = '_' + encoded.decode('ascii')  # result is a string with '_' prepended.

rawDescription: Converting Bitwise Java Operations into Python


code:
def SocketWrapper(object):
    def __init__(self, socket):
        self.socketobj = socket # use whatever you are already doing

    def fileno(self):
        return self.socketobj.fileno()

rawDescription: create fileno on class?

description: Create fileno on class


code:
@app.url_value_preprocessor
def pull_lang_code(endpoint, values):
    g.lang_code = values.pop('lang_code', None)

rawDescription: Flask Get the url variables in a before request?


code: mypic = makeCopyOfAPicture(picOriginal)

rawDescription: (jython/ python)Can't seem to get the image copy I made in jython outside of my def


code:
from scipy.interpolate import spline
sx = np.array(x2+x3)
sy = np.array(y2+y3)
t  = np.arange(sx.size,dtype=float)
t /= t[-1]
N  = np.linspace(0,1,2000)
SX = spline(t,sx,N,order=4)
SY = spline(t,sy,N,order=4)

plt.plot(x1,y1, 'r')
plt.plot(x2,y2, 'b')
plt.scatter(x3,y3,c='k')

plt.plot(SX, SY,'g',alpha=.7,lw=3)
plt.show()

rawDescription: Extend line to smoothly connect with another line


code:
def find(l):
  return (i for i in l if l.count(i)==1)
l= [0, 1, 1, 7, 8, 3, 9, 3, 9]
list(find(l))

rawDescription: How to find all numbers in a list that are not part of a pair - using python 3

description: Find all numbers in a list that are not part of a pair - using python 3


code:
import ctypes

EnumWindows = ctypes.windll.user32.EnumWindows
EnumWindowsProc = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int))
GetWindowText = ctypes.windll.user32.GetWindowTextW
GetWindowTextLength = ctypes.windll.user32.GetWindowTextLengthW
SendMessage = ctypes.windll.user32.SendMessageW
IsWindowVisible = ctypes.windll.user32.IsWindowVisible

def foreach_window(hwnd, lParam):
    if IsWindowVisible(hwnd):
        length = GetWindowTextLength(hwnd)
        buff = ctypes.create_unicode_buffer(length + 1)
        GetWindowText(hwnd, buff, length + 1)

        if(buff.value == "Choose File to Upload"): #This is the window label
            SendMessage(hwnd, 0x0100, 0x09, 0x00000001 )
    return True

EnumWindows(EnumWindowsProc(foreach_window), 0)

rawDescription: Open file from windows file dialog with python automatically


code:
import numpy as np
import pandas as pd
x = np.random.randn(5)
y = np.sin(x)
df = pd.DataFrame({'x':x, 'y':y})
df.plot('x', 'y', kind='scatter')

rawDescription: Creating Pandas Dataframe between two Numpy arrays, then draw scatter plot


code: names = db.people.find({'age': {'$gt': 18}}).distinct('name')

rawDescription: Load single field from pymongo as an array