code:
import heapq

k_smallest = heapq.nsmallest(k, input_list)

rawDescription: fastest method of getting k smallest numbers in unsorted list of size N in python?

description: Fastest method of getting k smallest numbers in unsorted list of size N in python


code:
FLOW = flow_from_clientsecrets(Path_to_client_secrets.json,scope='https://www.googleapis.com/auth/blogger',message=MISSING_CLIENT_SECRETS_MESSAGE)

storage = Storage('blogger.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
    credentials = run(FLOW, storage)

rawDescription: I have got the "Authorization code" for my app. But how can I use it to post in the blogger using gdata-python-client?


code: pointer = hash(obj)

rawDescription: Create python object from memory address (using gi.repository)


code:
OpenMayaUI.MUiMessage.removeCallback(callBack)
view = OpenMayaUI.M3dView.active3dView()
view.refresh(True, True)

rawDescription: OpenGL Python Maya API drawing without MPxLocator plugin


code:
itemlist = delimitedList(oneOf("alpha beta gamma"), '|')
def ensureNoDuplicates(tokens):
    if len(set(tokens)) != len(tokens):
        raise ParseException("duplicate list entries found")
itemlist.setParseAction(ensureNoDuplicates)

rawDescription: pyparsing matching any combination of specified Literals

description: Pyparsing matching any combination of specified Literals


code:
x = -1
y = -2
z = -4
x.bit_length()
y.bit_length()
z.bit_length()

rawDescription: Detect int32 overflow using 0xFFFFFFFF masking in Python?


code:
import numpy as np

# generate a random numpy array as loaded from cv2.imread()
img = np.random.randint(255, size=(8, 10, 3))

for (x,y,z), value in np.ndenumerate(img):
    do_something(img[x,y,z]) # modify to do whatever you want

rawDescription: cv::MatIterator equivalent in numpy

description: Cv::MatIterator equivalent in numpy


code:
s = "abcdEfgh"
re.sub(r'\B[A-Z]\B', lambda x: '_' + x.group().lower(), s)
re.sub(r'\B[A-Z]\B', lambda x: '_' + x.group().lower(), 'AbcdEfgH')

rawDescription: how to detect case change in python text and insert underscore on change location?

description: Detect case change in python text and insert underscore on change location


code:
import re
p = re.compile(r'(?<=-)\d+(?=/|$)')
test_str = "http://52portal/flood-2011-year-39090/gallery?p=3\nhttp://52portal/flood-2011-year-39090"
print(re.findall(p, test_str))

rawDescription: Regex match based on prefix and sufix

description: Regex match based on prefix and sufix


code:
vector
m = 5
np.zeros((m,1),dtype=vector.dtype) + vector
np.tile(vector,(m,1))

rawDescription: Numpy - create matrix with rows of vector


code:
def pandas_processor(rows, fields, columns, cacheable):
    return DataFrame.from_records(rows, columns=columns)

data = db(db.mytable.id > 0).select(processor=pandas_processor)

rawDescription: web2py webserver - Best way to keep connection to external SQL server?

description: Web2py webserver - Best way to keep connection to external SQL server


rawDescription: Omit one parameter from view in Django?

description: Omit one parameter from view in Django


code:
with open('android/2014-07-09.json', 'rb') as json_file:
    data = demjson.decode_file(json_file, strict=False)

rawDescription: Format of a .json file to be loaded in python


code:
with open("FirstMondayArticle.html") as f, open("twoVoweledWordList.txt", "w") as out:
    for line in file:
        for word in line.split():
            if len(word) - len(word.lower().translate(None,"aeiou")) > 1:
                out.write("{}\n".format(word.rstrip()))

rawDescription: trying to print to a text file with words that only have two or more occurring vowels

description: Trying to print to a text file with words that only have two or more occurring vowels


code:
classifiedmat = np.zeros(samplemat.shape)
classifiedmat[:] = nbinmat

rawDescription: Vectorized element assignment involving comparisons between matrices in Numpy


code: server = Server('your_server_name', get_info=ALL, use_tls=True)  # default tls configuration on port 636

rawDescription: Is it possible to connect to eDirectory with python ldap3?


rawDescription: Why is complex class not optimized to cache objects in Python?


code:
def _product_supplier_name(self, cr, uid, ids, name, arg, context=None):
    res = {}
    for product in self.browse(cr, uid, ids, context):
        supplier_name = ""
        if len(product.seller_ids) > 0:
            supplier_name = product.seller_ids[0].name.name
        res[product.id] = supplier_name
    return res

rawDescription: Sorting OpenERP table by functional field


code:
replace_func = sqlalchemy.func.regexp_replace(
    table.c.column,
    your_string_to_replace,
    your_string_to_replace_with
)
table.update(values={table.c.column: replace_func})

rawDescription: SQLAlchemy query to find and replace a substring if it exists in a column


rawDescription: how to add the option scaling to the tcp packet with scapy

description: Add the option scaling to the tcp packet with scapy


code:
import random
import sys
import timeit

numbers = [random.randrange(sys.maxsize) for _ in range(10000)]
set_ = set(numbers)
fset = frozenset(numbers)
present = random.choice(numbers)
notpresent = -1
test = 'present in s; notpresent in s'

settime = timeit.timeit(
    test,
    'from __main__ import set_ as s, present, notpresent')
fsettime = timeit.timeit(
    test,
    'from __main__ import fset as s, present, notpresent')

print('set      : {:.3f} seconds'.format(settime))
print('frozenset: {:.3f} seconds'.format(fsettime))

rawDescription: Set vs. frozenset performance

description: Set vs. frozenset performance


code: line1 = (line)

rawDescription: Search for a word in two files and print the the two lines with tabular separation


code:
import utide
import os

matfile = os.path.join(os.path.dirname(os.path.abspath(utide.__file__)),
                       'ut_constants.mat')

rawDescription: Python package setup with data files

description: Python package setup with data files


rawDescription: openpyxl Fetch value from excel and store in key value pair

description: Openpyxl Fetch value from excel and store in key value pair


code: newlist = filter(None, origlist)

rawDescription: elegant list manipulation in python

description: Elegant list manipulation in python


code:
import csv

valid_pset = set("""
    AA AE AH AO AW AY B CH D DH EH ER EY F G HH IH IY
    JH K L M N NG OW OY P R S SH T TH UH UW V W Y Z ZH
    """.strip().split())
valid_gset = set("abcdefghijklmnopqrstuvwxyz_")

valid_row = 0
invalid_row = 0
p_count = 0
g_count = 0

with open('test.csv','r') as f:
    reader = csv.reader(f)
    # Skip headers
    next(reader)
    try:
        line = 1
        for p,g in reader:
            phonemes = p.split()
            graphemes = g.split()
            line += 1

            valid = True
            if len(phonemes) != len(graphemes):
                print("Line {}: Number of phonemes and graphemes differ.".format(line))
                valid = False

            bad_p = [p for p in phonemes if p not in valid_pset]
            if bad_p:
                print("Line {}: Invalid phonemes {}".format(line, bad_p))
                valid = False

            graphemes = list(''.join(graphemes))
            bad_g = [g for g in graphemes if g not in valid_gset]
            if bad_g:
                print("Line {}: Invalid graphemes {}".format(line, bad_g))
                valid = False

            if valid:
                valid_row += 1
                p_count += len(phonemes)
                g_count += len(graphemes)
            else:
                invalid_row += 1
    except ValueError:
        pass

print("Valid rows: {}, Invalid rows: {}, p_count: {}, g_count: {}".format(
    valid_row, invalid_row, p_count, g_count))

rawDescription: Checking condition for each line in CSV

description: Checking condition for each line in CSV


code: not (90 % 15) and print("yes")

rawDescription: Python: using Perl style logical not and boolean negation


code:
import string
from google.appengine.api.search import Query
Query('"%s"' % string.printable.replace('"', '').replace('\\', ''))

rawDescription: Escaping search queries for Google's full text search service


code:
# main.py
import file1, file2, file3

def main(filename):
    fname_one = file1.generate_csv(filename)
    fname_two = file2.generate_csv(fname_one)
    fname_three = file3_generate_csv(fname_two)

rawDescription: Is there any way to run three python files sequentially as the output of each file depends on the other


rawDescription: Session generation from log file analysis with pandas


code: data[row1], data[row2] = data[row2], data[row1]

rawDescription: Matrix data structure

description: Matrix data structure


code: ''.join(sorted(string, key=lambda s:(s.lower(), s)))

rawDescription: How to sort a string alphabetically, with lower case letters coming before their upper case equivalents?

description: Sort a string alphabetically, with lower case letters coming before their upper case equivalents


code:
from os import listdir
from osgeo import gdalnumeric

image_list = []
for file_path in os.listdir("somedirectory"):
    image_array = gdalnumeric.LoadFile(file_path)
    image_list.append(image_array)

#image_list[i] will access to each image array

rawDescription: Read ERDAS Imagine Images into Individual Variables as Matrices


code:
class Foo:
    def __init__(self):
        self.ls = []

rawDescription: some confusion with variables declared by python class

description: Some confusion with variables declared by python class


rawDescription: Python: how do I create a list of combinations from a series of ranges of numbers


code:
import shelve

myShelve = shelve.open('my.shelve')
value = myShelve['a']
value += 1
myShelve['a'] = value

rawDescription: Pickle versus shelve storing large dictionaries in Python


code:
af = lambda pmu, pmin, pmode, pmax: (pmu-pmin)*(2*pmode-pmin-pmax)/((pmode-pmu)*(pmax-pmin))
bf = lambda pmu, pmin, pmode, pmax: (pmax-pmu)/(pmu-pmin)*((pmu-pmin)*(2*pmode-pmin-pmax)/((pmode-pmu)*(pmax-pmin)))

e=5.
shape=4.
max=10.
mu_d = np.arange(0, 10, 0.05)
d = np.arange(0.025, 60.025, 0.05)

Rp_max = max
e1_96 = 1.96 * e
e0_75 = 0.75 * e
for i in xrange(nlocs): # e.g 153413
   for mu_d_j in mu_d: # mu_d has 121 values
        Rp_min = mu_d_j - e1_96
        Rp_mode = mu_d_j - e0_75
        Rp_mu=(Rp_min+Rp_max+shape*Rp_mode)/(shape+2)

   dist = st.beta.cdf(d, a=af(Rp_mu, Rp_min, Rp_mode, Rp_max), b=bf(Rp_mu, Rp_min, Rp_mode, Rp_max), loc=Rp_min, scale=1-Rp_min)

rawDescription: Speeding up calculation of beta Pert distribution in Python


code: result = result.drop(['base','license','disclaimer','start_date','end_date'], axis=1)

rawDescription: Trouble Parsing data from JSON response with Python/Pandas Library


code:
inp = raw_input()
op1, operator, op2 = inp.split(" ")
op1 = int(op1)
op2 = int(op2)
# use op1, operator and op2

rawDescription: Python reading in one line multiple types for a calculator


rawDescription: combining two slicing operations

description: Combining two slicing operations


code:
s = x.groupby('ID1').apply(calc)
DataFrame(s.tolist(),index = s.index,columns = ['ID2','top3avg'])

rawDescription: Outputting a DataFrame instead of Series from a multiple return value groupby-apply operation


code:
from itertools import permutations

def getPass():
    return (''.join(p) for p in permutations('4$5%6^7&'))

for i in getPass():
    print(i)
    input('\ncontinue?')

rawDescription: Python itertools combinations iteration control


code:
with time_measure('test_method:sum1'):
    sum1 = sum(range(a, b))

rawDescription: Decorator to time specific lines of the code instead of whole method?


code:
result = []
for element in data:
    result.append(''.join(element))
print(result)

rawDescription: List conversion/transformation

description: List conversion/transformation


code:
import tkinter as tk
import random

# --- functions ---

def move(x, y):
    global empty_x, empty_y, game_running

    if game_running:
        # check and move tile
        if (empty_x, empty_y) in [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]:

            board[empty_y][empty_x].set( board[y][x].get() )
            board[y][x].set(' ')

            empty_x = x
            empty_y = y

            if won():
                # show label "Hurray"
                label.lift()
                game_running = False

    else:
        # start game again

        shuffle() # shuffle tiles

def won():

    number = 0

    for y, row in enumerate(board):
        for x, string_var in enumerate(row, 1):
            number += 1
            if number == 9 and string_var.get() == ' ':
                #print('A |%s| %d' % (char, number))
                return True
            elif string_var.get() != str(number):
                #print('B |%s| %d' % (char, number))
                return False

    return True

def shuffle():
    global empty_x, empty_y, game_running

    # TODO: shuffle tiles

    # it can't put tiles in random places
    # because it can create game without solution

    # clear all tiles to recognize (in next step) empty tiles
    for row in board:
        for element in row:
            element.set('')

    # put tiles in random places
    for number in range(1, 10):
        while True:
            row = random.randint(0, 2)
            col = random.randint(0, 2)
            if board[row][col].get() == '':
                board[row][col].set(str(number))
                print('row/col/number:', row, col, number)
                break

    # last number remove:
    print('empty row/col:', row, col)
    board[row][col].set(' ')
    empty_x = col
    empty_y = row

    # hide label
    label.lower() # or frame.lift()

    # start game
    game_running = True

# --- main ---

root = tk.Tk()

# --- data ---

# StringVar needs `root` - it has to be after tk.Tk()

# empty board
board = [
#    [tk.StringVar(value='1'), tk.StringVar(value='2'), tk.StringVar(value='3')],
    [tk.StringVar(), tk.StringVar(), tk.StringVar()],
    [tk.StringVar(), tk.StringVar(), tk.StringVar()],
    [tk.StringVar(), tk.StringVar(), tk.StringVar()],
]

# create label hidden behide frame

label = tk.Button(root, text="Hurray!\n\n(click here to play again)", command=shuffle)
label.grid(row=0, column=0, ipadx=50, ipady=50)

# create frame with buttons to fast show/hide label

frame = tk.Frame(root)
frame.grid(row=0, column=0) # the same (row,col) to hide label

buttons = []

for y, row in enumerate(board):

    buttons_row = []

    for x, string_var in enumerate(row):

        b = tk.Button(frame, textvariable=string_var, width=10, height=5, command=lambda x=x,y=y:move(x,y))
        b.grid(row=y, column=x)

        buttons_row.append(b)

    buttons.append(buttons_row)

# start game

shuffle() # shuffle tiles

# start "the engine"

root.mainloop()

rawDescription: Interactive game board with Tkinter Python - label updating


code: df.pivot_table(index=['values'], columns=['age_col', 'convert_me'], aggfunc=len).fillna(0)

rawDescription: R dcast equivalent in python pandas

description: R dcast equivalent in python pandas


code:
def sum(t):
    f = lambda x: 2 * (x**2) if x > 1 else x
    op = lambda x, y: x + y
    return combine(f, op, t + 1)

rawDescription: I need help on a summing pattern

description: I need help on a summing pattern


code:
deque10 = partial(deque, maxlen=10)
deque10()
deque10(range(20))

rawDescription: Issues with generating a defaultdict with a deque


code:
import re
print(int(re.findall("\-?\d+",s))[0])
10
In [22]: s = "-100A"
In [23]: int(re.findall("\-?\d+",s)[0])
Out[23]: -100

In [24]: int(s.translate(None,ascii_letters))
Out[24]: -100

rawDescription: Converting str to int in Python, only numbers w/o characters


rawDescription: Python Tkinter - hide a widget after some time


code:
import numpy as np
from scipy import ndimage

data = np.arange(3*5*9).reshape((3,5,9)).astype(np.float)
coords = np.array([[1.2, 3.5, 7.8], [0.5, 0.5, 6.8]])
zi = ndimage.map_coordinates(data, coords.T)

rawDescription: Multivariate spline interpolation in python/scipy?


code:
for i in seq:
    some_function(i)

rawDescription: When does using list comprehension in Python become inefficient?


code:
X_test
y_test

rawDescription: Scikit-learn train_test_split with indices


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

class Worker(QObject):
  sendOutput = pyqtSignal(QString)

  def __init__(self):
    super(Worker, self).__init__()
    self.process = QProcess()
    self.setupProcess()

  def __del__(self):
    self.process.terminate()
    if not self.process.waitForFinished(10000):
      self.process.kill()

  def setupProcess(self):
    self.process.setProcessChannelMode(QProcess.MergedChannels)
    self.process.readyReadStandardOutput.connect(self.readStdOutput)
    self.process.start("htop")

  @pyqtSlot()
  def readStdOutput(self):
    output = QString(self.process.readAllStandardOutput())
    # Do some extra processing of the output here if required
    # ...
    self.sendOutput.emit(output)

class MyQProcess(QWidget):
  def __init__(self):
   super(QWidget, self).__init__()
   layout = QVBoxLayout()
   self.edit = QTextEdit()
   self.thread = QThread()

   self.setupConnections()

   self.edit.setWindowTitle("QTextEdit Standard Output Redirection")
   layout.addWidget(self.edit)
   self.setLayout(layout)
   self.show()

  def setupConnections(self):
    self.worker = Worker()
    self.thread.finished.connect(self.worker.deleteLater)
    self.worker.sendOutput.connect(self.showOutput)

    self.worker.moveToThread(self.thread)
    self.thread.start()

  def __del__(self):
    if self.thread.isRunning():
      self.thread.quit()
      # Do some extra checking if thread has finished or not here if you want to

  #Define Slot Here
  @pyqtSlot(QString)
  def showOutput(self, output):
    #self.edit.clear()
    self.edit.append(output)

def main():
    app = QApplication(sys.argv)
    w   = MyQProcess()

    return app.exec_()

if __name__ == '__main__':
    main()

rawDescription: Start QProcess from within QDialog that is used as a progress monitor


code:
import os, sys, re

pdf_list = []
doc_list = []

pdf = re.compile(r'.*Shandeep.*\.pdf')
doc = re.compile(r'.*Shandeep.*\.doc')

for r, d, f in os.walk(r'/Users/shandeepkm/Desktop/Networking materials'):
    for items1 in f:
        match1 = re.findall(pdf, items1)
        match2 = re.findall(doc, items1)
        if match1:
            pdf_list.append(match1)
        if match2:
           doc_list.append(match2)

rawDescription: I want all PDF or DOCs to come first in the output


code:
def iterate_line(img, r_thresh, g_thresh, b_thresh, y):
    """
        This function returns true only when a given row at a height "y"
        from the origin(top - left) if fully black and false othrwise
    """
    for i in img[y]:
        if not((i[0]<b_thresh) and (i[1]<g_thresh) and i[2]<b_thresh):
            return False
    return True

rawDescription: Detect if image has border using PIL

description: Detect if image has border using PIL


code:
salesChar = MongoDumps("sales", "char")
first = salesChar.collection.find_one()

rawDescription: Can't connect to MongoDB database and collection through class variables


code:
from tzlocal import get_localzone
local_tz = get_localzone()

rawDescription: Get system local timezone in python

description: Get system local timezone in python


code:
from flask import Flask

app = Flask("yourapp")
# ... snip ...

rawDescription: How to have different py files to handle different routes?

description: Have different py files to handle different routes


code:
import itertools
...
self.questions = itertools.cycle(self.QStringList)
...
# this will always return the next question, looping
# back over the list as needed
next_question = self.questions.next()

rawDescription: Repeatedly iterating through a list, with buttons and bindings


rawDescription: Matplotlib: add custom tick label and remove custom tick on y-axis


code:
import time

def SimpleFunction():
    return 1

def ExpensiveFunction():
    time.sleep(0.001)
    return 1

def test(func):
    num_cases = 100
    BigMatrix = np.zeros((num_cases,num_cases))

    for CasesX in range(0,num_cases):
        for CasesY in range(CasesX,num_cases):
            BigMatrix[CasesX,CasesY] = func()

rawDescription: Better method to calculate half of symmetrical numpy matrix?


code:
included = {'d{:03d}'.format(i) for i in range(15, 26)}  # a set

binary = sorted(f for f in glob.glob('C:/Users/Desktop/bin/*.bin') if f[21:25] in included)

rawDescription: python access selective files with glob module

description: Python access selective files with glob module


rawDescription: How do I fill a list in python with elements from a specific set?

description: Fill a list in python with elements from a specific set


code:
def similarity_score(l1, l2):
    c1, c2 = Counter(l1), Counter(l2)
    return length_similarity(c1, c2) * counter_cosine_similarity(c1, c2)

rawDescription: Compute the similarity between two lists


code:
tmp = actualize_files_and_folders(self)
process = Process(target=tmp)

rawDescription: python simple thread not working

description: Python simple thread not working


code:
for base in C.__bases__:
    if 'foo' in base.__dict__:
        print("Attribute: '{}' defined in {}".format("foo", base))

rawDescription: How would I find where a function originates in source code?


code:
data['sex'].replace(0, 'Female',inplace=True)
data['sex'].replace(1, 'Male',inplace=True)

rawDescription: Pandas - replacing column values

description: Pandas - replacing column values


code:
df2 = df.T.dot(df)
df3 = df2.stack().reset_index()
df3[df3.level_0 != df3.level_1]

rawDescription: finding similarities in rows for a pandas dataframe

description: Finding similarities in rows for a pandas dataframe


code:
from collections import Counter
numbers = list(map(int, "9 8 7 5 3 0 1 -2 -3 1 2".split()))
longest_snake(numbers, Counter(numbers), [])

rawDescription: Longest Snake Sequence in an Array

description: Longest Snake Sequence in an Array


code:
def magicFunction(a, b):
    a = [tuple(sorted(d.items())) for d in a]
    b = [tuple(sorted(d.items())) for d in b]
    return [dict(kvs) for kvs in set(a).difference(b)]

rawDescription: subtract two "lists of dictionaties"

description: Subtract two "lists of dictionaties"


code:
from dateutil.parser import parse
from datetime import datetime

def parse_time(s):
    try:
        ret = parse(s)
    except ValueError:
        ret = datetime.utcfromtimestamp(s)
    return ret

rawDescription: Parsing datetime in Python..?

description: Parsing datetime in Python..


code:
def permutation_rec1(onelist, prelist):
    if not onelist:
        yield prelist

    lenlist = len(onelist)
    for i, oneitem in enumerate(onelist):
        leftlist = [onelist[j] for j in range(lenlist) if j != i]
        for res in permutation_rec1(leftlist, prelist + [oneitem]):
            yield res

rawDescription: why can't yield be used in a recursive function

description: Why can't yield be used in a recursive function


code:
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
add_module_names = False

rawDescription: Remove package and module name from sphinx function


code:
def memory():
    import os
    from wmi import WMI
    w = WMI('.')
    result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE IDProcess=%d" % os.getpid())
    return int(result[0].WorkingSet)

rawDescription: Total memory used by Python process?

description: Total memory used by Python process


code:
def foo():
    for x in range(10):
        sent_value = (yield x)
        yield sent_value

rawDescription: Attempting to understand yield as an expression


code:
List1 =[7,6,9]
repr(List1.sort())
List1                  #though list is sorted

rawDescription: Can anyone explain why this sorting won't work?


code:
import numpy as np
import copy

pairs = [(2, 3), (3, 4), (4, 5)]
array_of_arrays = np.array([np.arange(a*b).reshape(a,b) for (a, b) in pairs])

a = copy.deepcopy(array_of_arrays)

rawDescription: Deep copy of a np.array of np.array

description: Deep copy of a np.array of np.array


code:
#convert string column TC_NUM to dataframe df3
df3 = pd.DataFrame([ x.split(',') for x in df2['TC_NUM'].tolist() ])
#convert string df3 to float df3
df3 = df3.astype(float)

rawDescription: Python CSV joining columns

description: Python CSV joining columns


code:
df_met['wind_speed'].plot(figsize=(12,4))
df_wave['significant_wave_height'].plot(secondary_y=True);
ax = gca();
lines = ax.left_ax.get_lines() + ax.right_ax.get_lines()
ax.legend(lines, [l.get_label() for l in lines])

rawDescription: How to get multiple legends from multiple pandas plots

description: Get multiple legends from multiple pandas plots


code:
from bisect import bisect
import re

with open("test.txt") as f:
    r = re.compile("(\d+)")
    for line in f:
        a, b = line.lstrip("0123456789. ").split(">> ")
        a_keys = [int(i.group()) for i in r.finditer(a)]
        b_keys = [int(i.group()) for i in r.finditer(b)]
        a = a.strip("()\n").split(",")
        b = b.strip("()\n").split(",")
        for ele, k in zip(a, a_keys):
            ind = bisect(b_keys, k, hi=len(b) - 1)
            ind -= k - b_keys[ind] < b_keys[ind-1] - k
            print("{} -> {}".format(ele, b[ind]))

rawDescription: Combine words with nearest index

description: Combine words with nearest index


rawDescription: Round pandas data frame/series

description: Round pandas data frame/series


code:
def set_up_only_once():
    # run the actual setup (or nothing if already vandalized):
    set_up()
    # vandalize it so it never walks again:
    import sys
    sys.modules['logutils'].set_up = lambda: None

rawDescription: Logging in a Framework

description: Logging in a Framework


rawDescription: matplotlib imshow fixed aspect and vertical colorbar matching master axis height

description: Matplotlib imshow fixed aspect and vertical colorbar matching master axis height


code:
from string import Template

practise_dict = {"Additional_Details":"blah blah blah blah", "Title": "Mr", "Surname": "Smith", "URL": "/test/tester"}

msg = """From: John Smith <no-reply@somethingsomething.co.uk>
To: $Title $Surname <blah@blahblah.co.uk>
MIME-Version: 1.0
Content-type: text/html
Subject: New Website Enquiry
This is an e-mail message to be sent in HTML format
$Additional_Details
$Date
This is HTML message.

This is headline.

`"""

s = Template(msg).safe_substitute(**practise_dict)
print(s)

rawDescription: Ignore str.format(**foo) if key doesn't exist in foo


code: phi0 = list(phi_store)

rawDescription: How to store value instead of the list that the symbol stands for in python?

description: Store value instead of the list that the symbol stands for in python


code:
devnull = open('/dev/null', 'w')
process = subprocess.Popen(shlex.split(command_line), stdout=devnull, stderr=devnull)
retcode = process.wait()

rawDescription: python subprocess hide stdout and wait it to complete

description: Python subprocess hide stdout and wait it to complete


code:
if __name__ == "__main__":
    if LOGGING:
        old_stdout = sys.sdout
        with open(PATH_TO_LOGFILE, 'a') as logf:
            sys.stdout = logf
            main()
        sys.stdout = old_stdout

rawDescription: How can I determine who is calling my Python script?

description: Determine who is calling my Python script


code: '\n'.join( ','.join(row) for row in mylist )

rawDescription: Insert new row every 2nd element of python list


code:
class Baz(Foo):
    pass

bazzer = Baz()
bazzer.change_mutable()
bazzer.change_immutable()

rawDescription: Mutating objects referenced by class attributes through instance methods


code: zip(fields, row)

rawDescription: Populating a SQLite3 database from a .txt file with Python


code:
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.csrf',
    # Custom Context Proccessors
    'apps.your-app.context_processor.datetime',

)

rawDescription: Django - When I extend index.html I don't get the variables


code:
newlist = []
for sublist in l:
    for i in sublist:
        newlist.append(i)

print(newlist)
[[1, 1], [2, 2, 2], [3, 3], [4, 4], [5, 5, 5]]

rawDescription: Turn a non-symmetric 3 dimensional list into a 2 dimensional list of lists in python


code: sum(1 for x in d.values() if some_condition(x))

rawDescription: Python 2.7 Counting number of dictionary items with given value


code:
dtidx = pd.date_range('2012-01-01', periods=5, freq='30min')
pd.to_timedelta(dtidx.freq)

rawDescription: Convert pandas freq string to timedelta

description: Convert pandas freq string to timedelta


code:
from itertools import product, combinations

class Blob:

    def __init__(self, ID, Name, Unit, Weight):
        self.ID = ID
        self.Name = Name
        self.Unit = Unit
        self.Weight = Weight

    def __repr__(self):
        return 'Object: ID={ID}, Name={Name}, Unit={Unit}, Weight={Weight}'.format(Name=self.Name, ID=self.ID, Unit=self.Unit, Weight=self.Weight)

blobs = [Blob(1, 'AA', 'A', 1.0),
         Blob(2, 'AA', 'A', 1.0),
         Blob(3, 'AA', 'B', 1.0),
         Blob(4, 'AA', 'B', 1.0),
         Blob(5, 'AA', 'C', 1.0),
         Blob(6, 'AA', 'C', 1.0),
         Blob(7, 'AA', 'D', 1.0),
         Blob(8, 'AA', 'D', 1.0),
         Blob(9, 'AA', 'E', 1.0),
         Blob(10,'AA', 'E', 1.0),
         Blob(11,'AA', 'F', 1.0),
         Blob(12,'AA', 'F', 1.0),
         Blob(13,'AA', 'F', 1.0),
         Blob(14,'AA', 'F', 1.0)]

# Break down into selectable sub-groups by unit name
groups = {k: [b for b in blobs if b.Unit==k] for k in ['A','B','C','D','E','F']}
# Special treatment for unit F: expand to combination chunks of length 3
groups['F'] = combinations(groups['F'], 3)
# Create the list of all combinations
selected = list(product(*groups.values()))

rawDescription: Python algorithm - picking objects from a list based on object attributes


code:
models.py
from django.db import models

class CheckList(models.Model):
    name = models.CharField(max_length=255)
    checklist_type = models.ForeignKey('CheckListType')
    options = models.ManyToManyField('CheckListOption', blank=True)

    def __unicode__(self):
        return self.name

class CheckListType(models.Model):
    name = models.CharField(max_length=255)
    options = models.ManyToManyField('CheckListOption')

    def __unicode__(self):
        return self.name

class CheckListOption(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

rawDescription: Django Model Design - Many-To-Many Fields


code:
Explanation
y[:, ::2]

rawDescription: Insert 0s into 2d array

description: Insert 0s into 2d array


code:
@tornado.web.authenticated
@tornado.web.asynchronous
def post(self):
    async_fetch(..., self._on_fetch_response)

def _on_fetch_response(self, response):
    async_mail(response, self._on_mail_response)

def _on_mail_response(self, response):
    self.render(...) # render() automatically calls self.finish()

rawDescription: chaining asynchronous operations before writing to client (python - tornado)

description: Chaining asynchronous operations before writing to client (python - tornado)


code:
for item in xrange(len(x)):
      x[x.keys()[item]] = re.findall(r'123[A-Z\.]{7}', ''.join(y[item]))

rawDescription: Making a dictionary where values are based off of a regex - Python


code:
p1 = subprocess.Popen(['./main/foo', '--config', 'config_file'],
  stderr=subprocess.STDOUT,
  stdout=subprocess.PIPE)
p2 = subprocess.Popen(['tee', 'temp.log'], stdin=p1.stdout)
p1.stdout.close() # drop our own handle so p2's stdin is the only handle on p1.stdout
stdout, _ = p2.communicate()

rawDescription: How to avoid passing shell constructs to executable using Popen

description: Avoid passing shell constructs to executable using Popen


code:
int(' 1')
' 1'.isdigit()
' 1'.strip().isdigit()

rawDescription: How to map a list to integers

description: Map a list to integers


code:
import csv

with open('testfile', 'rb') as f:
  with open('outcsv.csv', 'wb') as o:
    # Specify your field names
    fieldnames = ('id', 'red', 'green', 'blue', 'yellow', 'black')

    # Here we create a DictWriter, since your data is suited for one
    writer = csv.DictWriter(o, fieldnames=fieldnames)

    # Write the header row
    writer.writerow(dict((h, h) for h in fieldnames))

    # General idea here is to build a row until we hit a blank line,
    # at which point we write our current row and continue
    new_row = {}
    for line in f.readlines():
      # This will split the line on a comma/space combo and then
      # Strip off any commas/spaces that end a word
      row = [x.strip(', ') for x in line.strip().split(', ')]
      if not row[0]:
        writer.writerow(new_row)
        new_row = {}
      else:
        # Here we write a blank string if there is no corresponding value;
        # otherwise, write the value
        new_row[row[0]] = '' if len(row) == 1 else row[1].strip()

    # Check new_row - if not blank, it hasn't been written (so write)
    if new_row:
      writer.writerow(new_row)

rawDescription: Python to extract and sort data from a file


code:
('myfile.txt', '2')
('secondfile.txt', '1')

rawDescription: Adding an additional argument to an argparse argument


code:
from urlparse import parse_qs
parse_qs(unquoted.split('?', 1)[1])
parsed = parse_qs(unquoted.split('?', 1)[1])
timestamp, event = parsed['tmsid'][0].split('_', 2)[:2]
timestamp, event

rawDescription: simple regex for following string

description: Simple regex for following string


code:
df = pd.read_excel('NewFile.xlsx')
symbols = ['G']

for symbol in symbols:
    ClosingPrice = DataReader(symbol, 'yahoo', datetime(2015,1,1), datetime(2015,9,1))
    ClosingPrice = ClosingPrice.reset_index()
    ClosingPrice['Symbol'] = symbol
    df = df.append(ClosingPrice)

writer = pd.ExcelWriter('NewFile.xlsx', engine='xlsxwriter')
df.to_excel(writer,sheet_name='Sheet1',index=False)
writer.save()

rawDescription: How to write data to excel using python for stock data being pulled from yahoo

description: Write data to excel using python for stock data being pulled from yahoo


code: value_rank = [(ordered_v.index(v)+1)+(ordered_v.count(v)-1)/2 for v in ordered_v]

rawDescription: python ranking a list of values, using average rank for non-unique values

description: Python ranking a list of values, using average rank for non-unique values


code:
from collections import Counter

counts = Counter(s)

for character, count in counts.most_common():
    print(character, '-', count)

rawDescription: How can I count the number of each character in a Python string?

description: Count the number of each character in a Python string


code:
from plugin_stuff import Plugin

class Foo(Plugin):
    """An example plugin."""
    self.menu_entry = {'Tools': {'Foo': self.bar}}
    def bar(self):
        return "foo plugin!"

rawDescription: Bundle a Python app as a single file to support add-ons or extensions?


rawDescription: python check if one before last digit is odd

description: Python check if one before last digit is odd


code: pixel_string = ''.join([chr(x) for x in pixel_list])

rawDescription: Python: Serial Transmission

description: Python: Serial Transmission


code: y = x.ravel().view(dtype=[('x', x.dtype), ('y', x.dtype)])

rawDescription: how to sort a scipy array with order attribute when it does not have the field names?

description: Sort a scipy array with order attribute when it does not have the field names


code:
users_df['Epoch'] = users_df['Date'] + ' ' + users_df['Time']
users_df['Epoch'] = users_df['Epoch'].apply(lambda tStamp: int(time.mktime(time.strptime(tStamp, pattern))))

rawDescription: In Pandas, how to find occurrence of one value from one dataframe in another dataframe?


rawDescription: How can I keep this Pandas column type as datetime when using MultiIndex?

description: Keep this Pandas column type as datetime when using MultiIndex


code:
import os
import fnmatch
import time
import shutil
import re

# directories
base_dir = "C:\new"
standard_dir = os.path.join(base_dir, "standard")

# find files in directory. based on yours, but modified to return a list.
def find_dir(path, name_filter):
    result = []
    for root, dirs, files in os.walk(path):
        for filename in fnmatch.filter(files, name_filter):
            result.append(os.path.join(root, filename))
    return result

# getting today's directory. you can rearrange year-month-day as you want.
def todays_dir():
    date_str = time.strftime("%Y_%m_%d")
    return os.path.join(base_dir, "daily_" + date_str)

# copy a file from one directory to another
def copy(filename, from_dir, to_dir):
    from_file = os.path.join(from_dir, filename)
    to_file = os.path.join(to_dir, filename)
    shutil.copyfile(from_file, to_file)

# main logic
today_dir = todays_dir()
pdfs = find_dir(today_dir, "*.pdf")
excels = find_dir(today_dir, "*.xls")
if len(pdfs) == 0:
    copy("standard1.pdf", standard_dir, today_dir)
    copy("standard2.pdf", standard_dir, today_dir)
    if len(excels) == 1:
        excel_name = os.path.splitext(excels[0])[0]
        excel_num = re.findall(r"\d+", excel_name)[-1]
        copy(excel_num + ".pdf", standard_dir, today_dir)

rawDescription: Python: Loop through directory, check if certain amount of files is in there, if not; copy 2 files from other directory and one file based on name


code: f = open('testtext.txt')

rawDescription: Unable to read complete text file in Python


code:
@app.route('/<path>', methods=['GET'])
@require_login
def just_render(path):
    if path in ['home', 'files', 'about', 'contact']:
         template = '{file_name}.html'.format(file_name=path)
         return render_template(template)

rawDescription: Is there a way to easily define routes for multiple standard pages in Flask?


code: [word for word in txt if len(word) <= 4]

rawDescription: Python - Print only words with less than five characters


code:
import numpy as np
matrix = np.zeros((10, 10))

rawDescription: Initializing an n-dimensional matrix elegantly in Python


code: ct = len([x for x in mylist if x['Predicate'] == 'I-s' and x['S_Length'] == '5.1'])

rawDescription: get count of repeated values in a list of dictionary

description: Get count of repeated values in a list of dictionary


code:
from sqlalchemy import event

event.listen(ModelObject.__table__,
         'after_create',
          DDL("CREATE UNIQUE INDEX term_year ON %(fullname)s "
              "(EXTRACT(YEAR FROM start_date), term)",
              on = 'postgresql'
              )
         )

rawDescription: Compound UniqueConstraint with a function


code:
import itertools as it
import time

player_pool = [['Ender Inciarte', '0.283', 'OF', '3900'],
               ['A.J. Pollock', '0.304', 'OF', '4900'],
               ['Jamie Romak', '0.349', 'OF', '2000'],
               ['Adam Jones', '0.258', 'OF', '3700'],
               ['Paul Goldschmidt', '0.343', '1B', '5600'],
               ['Chris Davis', '0.306', '1B', '4300'],
               ['Aaron Hill', '0.245', '2B/3B', '2400'],
               ['Jimmy Paredes', '0.276', '1B/2B', '3000'],
               ['Jake Lamb', '0.283', '3B', '3700'],
               ['Manny Machado', '0.315', '3B', '4400'],
               ['Welington Castillo', '0.31', 'C', '3800'],
               ['Caleb Joseph', '0.266', 'C', '3200'],
               ['Xander Bogaerts', '0.318', '3B/SS', '4300'],
               ['Eugenio Suarez', '0.294', 'SS', '3000']]

# create a player hash for later use, I hope player names are unique
player_hash = {p[0]: p for p in player_pool}

# use sets for the pools so that we can use set difference later on
catcher_pool = set()
first_pool = set()
second_pool = set()
third_pool = set()
short_pool = set()
of_pool = set()

# fill the pools only with player names
for player, stats in player_hash.items():
    if 'C' in stats[2]:
        catcher_pool.add(player)
    if '1B' in stats[2]:
        first_pool.add(player)
    if '2B' in stats[2]:
        second_pool.add(player)
    if '3B' in stats[2]:
        third_pool.add(player)
    if 'SS' in stats[2]:
        short_pool.add(player)
    if 'OF' in stats[2]:
        of_pool.add(player)

start = time.time()
playing = set()
all_rosters = []
roster = [None]*8

# create a little generator that only yields players from a pool
# which are currently not playing
def filtered(pool):
    for player in pool-playing:
        playing.add(player)
        yield player
        playing.remove(player)

with open('rosters.txt', 'w') as f:
    for catcher in filtered(catcher_pool):
        roster[0] = catcher
        for first_baseman in filtered(first_pool):
            roster[1] = first_baseman
            for second_baseman in filtered(second_pool):
                roster[2] = second_baseman
                for third_baseman in filtered(third_pool):
                    roster[3] = third_baseman
                    for shortstop in filtered(short_pool):
                        roster[4] = shortstop
                        for outfielders in it.combinations(of_pool-playing, 3):
                            roster[5:8] = outfielders
                            # append result to a list instead of printing
                            # this is a lot faster
                            all_rosters.append(roster[:])
                            # if our list is bigger than 1e6, write it to file
                            if len(all_rosters) > 10**6:
                                f.writelines(repr(roster)+'\n'
                                             for roster
                                             in all_rosters)
                                all_rosters = []
    # don't forget to write the last batch which did not reach 1e6
    f.writelines(repr(roster)+'\n' for roster in all_rosters)

end = time.time()
elapsed = end - start

# make sure no roster contains double names
assert all(len(set(roster)) == len(roster) == 8 for roster in all_rosters)

# make sure all rosters are unique
assert len(all_rosters) == len(set(tuple(roster) for roster in all_rosters))

print(len(all_rosters))
print(elapsed)

rawDescription: loop to make every combination of several lists

description: Loop to make every combination of several lists


code: my_precision = '{:.{}f}'.format

rawDescription: How can I format a float to variable precision in Python?

description: Format a float to variable precision in Python


code:
from PyQt4 import QtGui
from Form import Ui_Form

class FormProc(QtGui.QWidget):
    def __init__(self, parent=None):
        super(FormProc, self).__init__(parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)

    def closeEvent(self, event):
        print('close event')

rawDescription: python qwidget closeEvent doesn't call

description: Python qwidget closeEvent doesn't call


code:
#!/usr/bin/env python
import argparse

...

rawDescription: Passing in multiple options for argparse in Python


code:
import logging, logging.handlers, atexit, sys

filename = 'mylogfile.txt'
logLevel = logging.DEBUG

formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
streamhandler = logging.StreamHandler(sys.stderr)
streamhandler.setLevel(logLevel)
streamhandler.setFormatter(formatter)
memoryhandler = logging.handlers.MemoryHandler(
    capacity=1024*100,
    flushLevel=logging.ERROR,
    target=streamhandler
)

filehandler = logging.FileHandler(filename)
filehandler.setLevel(logLevel)
filehandler.setFormatter(formatter)
logger = logging.getLogger()
logger.setLevel(logLevel)
logger.addHandler(memoryhandler)
logger.addHandler(filehandler)
def flush():
    memoryhandler.flush()
atexit.register(flush)
logging.debug("Logger has Initialized")
sys.stderr.write("I'd like this printed on the console first\n")

rawDescription: Setting up Python logging manually with MemoryHandler and FileHandler


rawDescription: Remove a level from a pandas MultiIndex

description: Remove a level from a pandas MultiIndex


code:
from flask import Flask, redirect, request, render_template
app = Flask("the_flask_module")

@app.route('/', methods=["GET", "POST"])
def post_redirect_get():
    if request.method == "GET":
        return render_template("post_redirect_get.html")
    else:
        # Use said data.
        return redirect("target", code=303)

@app.route("/target")
def target():
    return "I'm the redirected function"

app.run(host="0.0.0.0", port=5001)

rawDescription: Internal Redirect in Flask

description: Internal Redirect in Flask


code:
def schoolDetails(request, school_id):
    try:
        school = PublicSchools.objects.get(pk=school_id)
    except school.DoesNotExist:
        raise Http404
    return render(request, 'detail.html', {'school': school})

rawDescription: Django, How to pass data object from one template to another template


rawDescription: Adding parts of a line to make a sequence line (python)


code:
import games

GAMES = ('color', 'food', 'car', 'toy')

while True:
    question = input('Please enter your question: ').lower()
    if not question:
        break
    for g in GAMES:
        if g in question:
            getattr(games, g)()
            break
    else:
        print('I do not have a game for that! :(')

print('Byebye!')

rawDescription: How to iterate through modules with user input?

description: Iterate through modules with user input


code:
l.sort(key=lambda x: x.count('+'))
l

rawDescription: How to sort list of strings by count of a certain character?

description: Sort list of strings by count of a certain character


code:
class ModelWithImage(models.Model):
    image = models.ImageField(
        upload_to='images',
    )

rawDescription: Download a remote image and save it to a Django model


rawDescription: Google App Engine: Webtest simulating logged in user and administrator


code: a = array.array('i',(0 for _ in xrange(10)))

rawDescription: Can you create traditional fixed length and type arrays in Python?

description: Create traditional fixed length and type arrays in Python


code:
foo.split(s1)
foo.split(s2)
foo.split(s3)
foo.split(s4)
foo.split('( ) abcde')

rawDescription: Python re.compile, split string into two while preserving delimiter


rawDescription: Getting and storing methods of a python class


code:
import scrapy
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor

class DesidimeItem(scrapy.Item):
    deal = scrapy.Field()
    link = scrapy.Field()

class DesidimeSpider(CrawlSpider):
    name = "desidime"
    allowed_domains = ["desidime.com"]
    start_urls = ["http://www.desidime.com/forums/hot-deals-online"]

    rules = [
        Rule(LinkExtractor(restrict_xpaths="//td[not(@*)]/div[not(@*)]/a[not(@class)]"),
             callback="parse_items",
             follow=True),
    ]

    def parse_items(self, response):
        for deal in response.xpath("//div[@class='user-comment-text'][1]"):
            item = DesidimeItem()

            item["deal"] = deal.xpath(".//p/text()").extract()
            item["link"] = deal.xpath(".//p[1]/a[1]/@href").extract()

            yield item

rawDescription: scrapy spider code check

description: Scrapy spider code check


code:
def dot_aligned(seq):
    snums = [str(n) for n in seq]
    dots = [len(s.split('.', 1)[0]) for s in snums]
    m = max(dots)
    return [' '*(m - d) + s for s, d in zip(snums, dots)]

rawDescription: Formatting Numbers So They Allign On Decimal Point


code:
for d in celldict.itervalues():
    celldict['Week'] = '1238'

rawDescription: format python output to json

description: Format python output to json


code:
['root.Child2.subChild3',
 'root.Child2.subChild2',
 'root.Child.subChild',
 'root.Child.subChild2']

rawDescription: Traverse json object to print root to children path separeted by dot


code:
# positions of each tick, relative to the indices of the x-values
ax.set_xticks(np.interp(new_ticks, s.index, np.arange(s.size)))

# labels
ax.set_xticklabels(new_ticks)

rawDescription: How can I set the x-axis tick locations for a bar plot created from a pandas DataFrame?

description: Set the x-axis tick locations for a bar plot created from a pandas DataFrame


code:
df = df.groupby(['window_end', 'item']).result.apply(lambda x: x.value_counts().to_frame().T)
df = df.fillna(0).astype(int).reset_index(level=2, drop=True)
df['total'] = df.sum(axis=1)

rawDescription: How to group rows within a time period using Python

description: Group rows within a time period using Python


code: sorted(list(uniq), key=lambda i:i[2])

rawDescription: filter out "reversed" duplicated tuples from a list in Python

description: Filter out "reversed" duplicated tuples from a list in Python


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

rawDescription: read and display raw image using python

description: Read and display raw image using python


code: first, second = zip(*list_values)[:2]

rawDescription: multiple assignments in one line for loop python

description: Multiple assignments in one line for loop python


code: sp = subprocess.Popen(['command', 'arg1', 'arg2'], stdout=subprocess.PIPE)

rawDescription: How to store multiple stdout results in a variable?

description: Store multiple stdout results in a variable


code:
print("You entered %.1f\" which converts into "
      "%.2fm." % (imp_height_flt, imp_height_converted))

rawDescription: How to eliminate white space after backslash (Python 3.4)

description: Eliminate white space after backslash (Python 3.4)


rawDescription: Python: compare an array element-wise with a float


code:
timeit(main, number=1000)
timeit(main_2, number=1000)

rawDescription: Improving performance of iterative 2D Numpy array with multivariate random generator


code:
import numpy as np

def recover_homogenous_affine_transformation(p, p_prime):
    '''
    Find the unique homogeneous affine transformation that
    maps a set of 3 points to another set of 3 points in 3D
    space:

        p_prime == np.dot(p, R) + t

    where `R` is an unknown rotation matrix, `t` is an unknown
    translation vector, and `p` and `p_prime` are the original
    and transformed set of points stored as row vectors:

        p       = np.array((p1,       p2,       p3))
        p_prime = np.array((p1_prime, p2_prime, p3_prime))

    The result of this function is an augmented 4-by-4
    matrix `A` that represents this affine transformation:

        np.column_stack((p_prime, (1, 1, 1))) == \
            np.dot(np.column_stack((p, (1, 1, 1))), A)

    Source: https://math.stackexchange.com/a/222170 (robjohn)
    '''

    # construct intermediate matrix
    Q       = p[1:]       - p[0]
    Q_prime = p_prime[1:] - p_prime[0]

    # calculate rotation matrix
    R = np.dot(np.linalg.inv(np.row_stack((Q, np.cross(*Q)))),
               np.row_stack((Q_prime, np.cross(*Q_prime))))

    # calculate translation vector
    t = p_prime[0] - np.dot(p[0], R)

    # calculate affine transformation matrix
    return np.column_stack((np.row_stack((R, t)),
                            (0, 0, 0, 1)))

rawDescription: Determining a homogeneous affine transformation matrix from six points in 3D using Python


code: file.write(r"""a=input("What would you like?\n")""")

rawDescription: How can I write to a file but keep \n as it is rather than adding a new line in python?

description: Write to a file but keep \n as it is rather than adding a new line in python


code:
from functools import partial, reduce
reduce(partial(pd.merge, on=0, how='outer'), [df1, df2, df3])

rawDescription: trying to read multiple .csv into separate data-frame columns

description: Trying to read multiple .csv into separate data-frame columns


code: new = np.column_stack([x, y])

rawDescription: Load data from file and normalize

description: Load data from file and normalize


code:
def main():
    curses.initscr()

    try:
        curses.cbreak()
        for i in range(3):
            time.sleep(1)
            curses.flash()
            pass
        print( "Hello World" )
    finally:
        curses.endwin()

rawDescription: Python curses dilemma

description: Python curses dilemma


rawDescription: How do I split multiple characters without using any imported libraries?

description: Split multiple characters without using any imported libraries


code: sorted(mylist,key=lambda x: x[1] in order and order.index(x[1]) or len(order)+1)

rawDescription: sorting list of tuples by arbitrary key

description: Sorting list of tuples by arbitrary key


code:
if (xmax - x0) % h == 0:
    pass

rawDescription: Determining if a Variable is Numerically an Integer in Python


code:
with open('textA.txt', 'w') as f:
    f.write('\n'.join(lines[1:72]))

rawDescription: .txt file does not print after being written with a join lines command in python


rawDescription: How can I set up token-based authentication for web services using Python?

description: Set up token-based authentication for web services using Python


code:
{nk: (nv.split('_') if '_' in k else v)
 for k, v in dic.items() for nk, nv in zip(k.split('_'), v)}

rawDescription: split a key in a dic and its values to multiple keys/values

description: Split a key in a dic and its values to multiple keys/values


code:
lastlast = last = None
for value in my_sequence:
    if lastlast is not None:
        do_something(lastlast, value)
    lastlast, last = last, value

rawDescription: How can you manipulate the iterator in a for loop in python?

description: Manipulate the iterator in a for loop in python


rawDescription: Read and Write the specific content

description: Read and Write the specific content


code:
# imported or present in app/__init__.py
@templatefilter
def datestr(date):
    return datetime.strftime(date, '%Y-%m-%d')

rawDescription: django: defining tags/filters/manager actions with less ceremony

description: Django: defining tags/filters/manager actions with less ceremony


rawDescription: Joining dataframes with different datetime frequencies


code:
def tariffs(group):
    for key, value in group.items():
        for tariff in value['trf']:
            name = ''.join(ncode, vendor, extra, value['suffix'], tariff)
            yield [name, value['latest_cost'], value['rrp'], value['rb']]

rawDescription: More Pythonic (or perhaps functional) way of creating this list?


code: df

rawDescription: Transform dataframe to get co-author relationships


code:
class Son:
    father_id = Column(Integer, ForeignKey('father.id'))
    father = relationship("Father", backref="sons")

rawDescription: Relation 1 to N in Django

description: Relation 1 to N in Django


code:
pd.set_option('max_rows',10)
dfx = df_all_but_parms.copy()

rawDescription: Pandas: Merge hierarchical data

description: Pandas: Merge hierarchical data


code:
class URL_Cleaner(object):
    def __init__(self, old_strings):
        """Cleans URL of 'http://www.'"""
        self.old_strings = old_strings
        cleaned_strings = self.clean_strings()
    def clean_strings(self):
        """Clean the strings"""
        accumulator = []
        for string in self.old_strings:
            string = string.replace("http://", "").replace("www.", "")
            # this might be better as string = re.sub("http://(?:www.)?", "", string)
            # but I'm not going to introduce re yet.
            accumulator.append(string)
        return accumulator
        # this whole function is just:
        ## return [re.sub("http://(?:www.)?", "", string, flags=re.I) for string in self.old_strings]
        # but that's not as readable imo.

rawDescription: Class returns empty list

description: Class returns empty list


code:
for x in xs:
    for y in ys:
        if bad:
            break
    else:
        continue

    break

rawDescription: python: exit out of two loops

description: Python: exit out of two loops


rawDescription: How do I remove identical items from a list and sort it in Python?

description: Remove identical items from a list and sort it in Python


rawDescription: Is there a simpler way to change 0xff01 into \\uff01?


code:
def redirect_url(default='index'):
    return request.args.get('next') or \
           request.referrer or \
           url_for(default)

rawDescription: Redirect back in Flask

description: Redirect back in Flask


code:
# The following code converts list2 into a multivalue dictionary

from collections import defaultdict

list2_dict = defaultdict(list)

for item in list2:
    list2_dict[item['timestamp']].append((item['name'], item['value']))

rawDescription: Looping at specified indexes

description: Looping at specified indexes


rawDescription: sorting elements out of the list

description: Sorting elements out of the list


code: if len(thelist) != len(set(id(x) for x in thelist)): ...

rawDescription: Is there a way to inspect the (differing) internal structures of Python objects that test as equal (==)?


rawDescription: What actually happens when broadcasting a NumPy array


rawDescription: How to combine 2d lists elements in python - Taking order in consideration

description: Combine 2d lists elements in python - Taking order in consideration


code: ['Library', 'SSHClient', 'with name', 'node']

rawDescription: pyparsing whitespace match issues

description: Pyparsing whitespace match issues


rawDescription: Writing JSON to file - wrong format?

description: Writing JSON to file - wrong format


code:
class Product(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    qty_stock = db.Column(db.Integer)
    requested_products = db.relationship('RequestedProducts')
    ordered_products = db.relationship('OrderedProducts')

    @hybrid_property
    def diff_orders(self):
        return sum(op.quantity - op.qty_delivered
                   for op in self.ordered_products)

    @diff_orders.expression
    def diff_orders(cls):
        return (db.select([db.func.coalesce(
            db.func.sum(
                db.func.coalesce(
                    OrderedProducts.quantity - OrderedProducts.qty_delivered, 0)
            ), 0)])
            .where(OrderedProducts.product_id == cls.id)
            .label("diff_orders")
        )

    @hybrid_property
    def diff_requests(self):
        return sum(op.quantity - op.qty_supplied
                   for op in self.requested_products)

    @diff_requests.expression
    def diff_requests(cls):
        return (db.select([db.func.coalesce(
            db.func.sum(
                db.func.coalesce(
                    RequestedProducts.quantity - RequestedProducts.qty_supplied, 0)
            ), 0)])
            .where(RequestedProducts.product_id == cls.id)
            .label("diff_requests")
        )

rawDescription: Order by issue when outer joining two tables in sqlalchemy


code:
def reverseList(listXS, i=1):
    if i == len(listXS) - 1:
        return
    listXS[i-1], listXS[-i] = listXS[-i], listXS[i-1]
    reverseList(listXS, i+1)

rawDescription: Using recursion to reverse a list in python?


code:
def replaceable(var):
    if 'type' in var and var['type'] == 'object' and \
        'properties' in var and isinstance(var['properties'],dict):
        return var['properties']
    return {}

rawDescription: Python: Is it possible to do a "deep class override"?


code:
from operator import itemgetter
from itertools import groupby
from pprint import pprint
inputlist = [
  {'perm':'copy',   'id':1, 'name':'user1'},
  {'perm':'read',   'id':1, 'name':'user1'},
  {'perm':'delete', 'id':2, 'name':'user2'},
  {'perm':'copy',   'id':2, 'name':'user2'},
  {'perm':'update', 'id':3, 'name':'user3'}
]
[{'perms': dict.fromkeys((p['perm'] for p in group), True), 'id': key[0], 'user': key[1]}
 for key, group in groupby(inputlist, itemgetter('id', 'name'))]
pprint(_)
per_user = {}
for perm in inputlist:
    key = perm['id'], perm['name']
    per_user.setdefault(key, {})[perm['perm']] = True

[{'perms': value, 'id': key[0], 'user': key[1]} for key, value in per_user.iteritems()]
pprint(_)

rawDescription: Group by a list of dicts by two values in python


code:
import re

conditions = ['CELSIUS', 'FROM_KELVIN']

def eval_conditional(matchobj):
    statement = matchobj.groups()[1].split('#else')
    statement.append('') # in case there was no else statement
    if matchobj.groups()[0] in conditions: return statement[0]
    else: return statement[1]

def parse(text):
    pattern = r'#if\s*(\S*)\s*((?:.(?!#if|#endif))*.)#endif'
    regex = re.compile(pattern, re.DOTALL)
    while True:
        if not regex.search(text): break
        text = regex.sub(eval_conditional, text)
    return text

if __name__ == '__main__':
    i = open('input.txt', 'r').readlines()
    g = ''.join([x.split(';')[0] for x in i if x.strip()])
    o = parse(g)
    open('output.txt', 'w').write(o)

rawDescription: parse nested conditional statements

description: Parse nested conditional statements


code:
vals
vecs

rawDescription: finding the real eigenvectors of a real symmetric matrix in numpy or scipy

description: Finding the real eigenvectors of a real symmetric matrix in numpy or scipy


code: features[word] = (word in document)

rawDescription: Naive Bayes Classifier error

description: Naive Bayes Classifier error


code:
a,b,c = np.unique(data, return_index=True, return_inverse=True)
c # almost!!!
np.argsort(b)[c]

rawDescription: numpy replace groups of elements with integers incrementally

description: Numpy replace groups of elements with integers incrementally


code:
d['Links'] = ', '.join(['{}: {}'.format(*item) for item in links.items()])
d['Sources'] = ', '.join(['{}: {}'.format(*item) for item in sources.items()])

rawDescription: Python - Writing a dictionary of dictionaries to CSV


code:
def enum_ordered(labels):
  if len(labels) == 1:
    yield labels[0]
  else:
    for i in range(1, len(labels)):
      for left in enum_ordered(labels[:i]):
        for right in enum_ordered(labels[i:]):
          yield Node(left, right)

rawDescription: Enumerate all full (labeled) binary tree


rawDescription: How to check if text is "empty" (spaces, tabs, newlines) in Python?

description: Check if text is "empty" (spaces, tabs, newlines) in Python


code:
multi_df = pd.concat(x, keys = range(len(x)))
result = multi_df.groupby(level = [0]).apply(np.mean)

rawDescription: Pandas appending Series to DataFrame to write to a file


code:
with plt.rc_context({'lines.linewidth': 5}):
    plt.plot([1, 2, 3, 4])

rawDescription: Is there a context manager for temporarily changing matplotlib settings?


rawDescription: in python; convert list of files to file like object

description: In python; convert list of files to file like object


code:
def genIndexList(length, ID):
   id0 = str(ID)[0]
   for i in xrange(length):
       i3 = format(decimalToBase3(i), '012d')
       p = sum(map(int, id0 + i3[::2])) % 3
       yield '{}{}{}'.format(ID, i3, p)

rawDescription: Improve efficiency of modules

description: Improve efficiency of modules


rawDescription: How to search a document for IP addresses

description: Search a document for IP addresses


code:
entities = Entity.objects.order_by('start_time')
list_of_lists = [list(g) for t, g in groupby(entities, key=extract_date)]

rawDescription: Django model group by datetime's date

description: Django model group by datetime's date


code:
for num in numbers:
    numEven(num),
for num in numbers:
    moreThan100(num),
for num in numbers:
    adding30(num)

rawDescription: Python: Calling Functions Inside of a ForLoop


code:
x  = np.unique(data['x'])
y  = np.unique(data['y'])
ix = (data['x']-x.min())/(x[1]-x[0])
iy = (data['y']-y.min())/(y[1]-y[0])

# create output array
r  = np.ones((x.size,y.size), float)*np.nan   # default value is NaN
r[ix.astype(int), iy.astype(int)] = data['value']

rawDescription: Nx3 column data to 2d matrix for image processing


code:
df.groupby(df.id).sum()
# or
df.groupby('id').sum()

rawDescription: Sum up non-unique rows in DataFrame

description: Sum up non-unique rows in DataFrame


rawDescription: Using a file to store optparse arguments


code:
def getNGramBeginsWords(nGram, length):
    # dict is the name of a builtin function, which you are confusingly overwriting
    # dict = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    wlist = open('/home/will/workspace/Genie/src/resources/TWL06.txt', 'r')
    nGram = nGram.upper()
    words = []
    for line in wlist:
        # an empty string evaluates to False in a binary context; also no need for those brackets
        stripline = line.strip().upper() # you keep doing this; I added the upper here.
        # you don't need this if, because you immediately test length
        #if stripline: #I know I changed this, but you only refer to the stripped version below
        # pipe | is bitwise OR. I bet you don't want that
        if len(nGram)>len(stripline) or len(stripline)!= length:
            continue
        # s = stripline[:len(nGram)] #you only use this once
        # you don't need to check that stripline is of length again; you already did that
        # also, you can just use `endswith` instead of slicing
        if stripline.endswith(nGram):
            words.append(stripline)
    return words

rawDescription: Passing in Global Variables in Python

description: Passing in Global Variables in Python


code:
for key in countries:
    if some_strng in countries:
        code_country.append([key,countries[key]])

rawDescription: need to iterate through dictionary to find slice of string

description: Need to iterate through dictionary to find slice of string


code:
data = {'Fruits' : ['Mango', 'Banana', '', '', 'Apple'],
        'Trees' : ['Pine', 'Bamboo', '', '', '', '', ''],
        'Laptops' : ['Sony', '', '', 'LG', 'Acer', '']}
{k: filter(bool, v) for k, v in data.iteritems()}

rawDescription: How to remove empty entries from a list in a dictionary using python

description: Remove empty entries from a list in a dictionary using python


code:
class Tests(object):
    def __init__(self):
        messages = []
        self.pdict = None

    def parsetest(self):
        try:
            self.pdict = parser.parseProtocol(id)
        except:
            self.messages.append("parsing failed")
        else:
            self.messages.append("parsing worked")

    def retrievetest(self):
        if self.pdict is None:
            raise Exception("called retrievetest() before successfully parsing")
        try:
            file = parser.getFile(self.pdict['filePath'])
        except:
            self.messages.append("file not found")
        else:
            self.messages.append("file retrieved")

rawDescription: Pythonic way to handle updating status string between functions?


rawDescription: Initializing list of numpy arrays

description: Initializing list of numpy arrays


code:
def func(mystr):
  b = {'a':'b','c':'d','e':'f'}
  return [b[k] for k in mystr if k in b]

rawDescription: Working with python dictionaries

description: Working with python dictionaries


code:
walk1 = copy.deepcopy(lwalk1)
walk2 = copy.deepcopy(lwalk2)
...

rawDescription: I can't get my class to work in pygame

description: I can't get my class to work in pygame


code:
list(reversed('10.11.12.13'.split('.')))
sorted(addr_list, key=lambda ip: list(reversed(ip.split('.'))))

rawDescription: Sort on the last octet of an IP address in a Python list


code:
class FakeDate(datetime.date):
     @classmethod
     def today(cls):
         return cls(1999, 12, 31)
     def __add__(self, other):
         res = super(FakeDate, self).__add__(other)
         return type(self)(res.year, res.month, res.day)

rawDescription: How to mock the "+" operator in python (specifically datetime.date + datetime.timedelta)

description: Mock the "+" operator in python (specifically datetime.date + datetime.timedelta)


code:
from datetime import datetime

sorted(ConfigList, key=lambda d: datetime.strptime(d, '%d-%m-%Y'))

rawDescription: Sorting a list of dates in Python

description: Sorting a list of dates in Python


code: a = pd.pivot_table(g, index='Class', columns='A', values=0).fillna(0)

rawDescription: Dataframe use grouped row values as a row name


code: [item for sublist in values[1:] for item in sublist]

rawDescription: Python loop through list starting at index


code:
modify_file("C:\\Users\\Malik\\Desktop\\release_0.5\\release_0.5\\5075442.xml",
            "abc..",
            "!--Delivery Date:")

rawDescription: Find a string and insert text after it in Python


code:
from xml.dom.minidom import parseString
from os.path import basename

dom = parseString(raw_xml)

feature = {}
for f in dom.getElementsByTagName('feature'):
    name = basename(f.getAttribute('name'))
    state = f.getAttribute('state').lower() == 'true'
    feature[name] = state

rawDescription: Fastest way to code iteration through XML items using python's minidom?


rawDescription: Pandas sorting by value and then by index


code:
m = re.match(p, line)
m.groups()

rawDescription: Python regular expression: extract multiple matching groups


rawDescription: python map array of dictionaries to dictionary?

description: Python map array of dictionaries to dictionary


code: [[n for n in x if n in y] for x, y in zip(a, b)]

rawDescription: How to get intersection of each element of two nested lists in python?

description: Get intersection of each element of two nested lists in python


rawDescription: Converting an array containing the power set of a positive integer d to an array with standard elementary vectors in d-dimensional space


code:
for line in reader:
    all_decisions.append(map(int, line))
    if all_decisions[-1][4] != 1:
        all_decisions.extend(islice(reader, 4))

rawDescription: Continue to read in file after if statement


rawDescription: Regex replace in List Comprehension in Python


code:
'''create all r vectors'''
R = np.empty((K.shape[0], K.shape[1], 3))
R[:,:,0] = np.repeat(np.arange(K.shape[0]), K.shape[1]).reshape(K.shape) * distX
R[:,:,1] = np.arange(K.shape[1]) * distY
R[:,:,2] = z

'''create all ksi vectors'''
KSI = np.empty((img.shape[0], img.shape[1], 3))
KSI[:,:,0] = np.repeat(np.arange(img.shape[0]), img.shape[1]).reshape(img.shape) * distX
KSI[:,:,1] = np.arange(img.shape[1]) * distY
KSI[:,:,2] = z

# vectorized 2-norm; see http://stackoverflow.com/a/7741976/4323
KSInorm = np.sum(np.abs(KSI)**2,axis=-1)**(1./2)

# loop over entire K, which is same shape as img, rows first
# this loop populates K, one pixel at a time (so can be parallelized)
for i in xrange(K.shape[0]):
    for j in xrange(K.shape[1]):

        print(i, j)

        KSIdotR = np.dot(KSI, R[i,j])
        temp = img * np.exp(1j * k * KSIdotR / KSInorm)

        '''interpolate so that we can do the integral and take the integral'''
        temp2 = rbs(a, b, temp.real)
        K[i,j] = temp2.integral(0, n, 0, m)

rawDescription: Need help vectorizing code or optimizing


code: login(request, user)

rawDescription: Django - Settings and Accessing Session Variables in Different Generic Views


rawDescription: pythonic way to sort a list of lists by the last item of the inner list

description: Pythonic way to sort a list of lists by the last item of the inner list


code:
def encrypt(data, shift):
    result = ''
    for c in data:
        c_num = ord(c)

        #  is the letter lower case a - z?
        if (c_num >= ord('a')) and (c_num <= ord('z')):
            #  get the letter number from 0 - 26
            c_num = c_num - ord('a')
            # shift the number
            c_num += shift
            # wrap the number every 26 numbers
            c_num = c_num % 26
            # now increase a by the new amount
            c_num += ord('a')
            result += chr(c_num)

        #  is the letter upper case A - Z?
        elif (c_num >= ord('A')) and (c_num <= ord('Z')):
            #  get the letter number from 0 - 26
            c_num = c_num - ord('A')
            # shift the number
            c_num += shift
            # wrap the number every 26 numbers
            c_num = c_num % 26
            # now increase a by the new amount
            c_num += ord('A')
            result += chr(c_num)

    return result

encrypt('aAbB', 2)
'cCdD'

encrypt('afZz', 2)
'chBb'

rawDescription: Python Simple Encryption program: How to loop Z back to A


code: user_made_array = ["Bob" if item in array[0] else item for item in user_made_array]

rawDescription: How to compare the elements of a list to a 2-dimensional list (without using a huge amount of if/elif/else statements)

description: Compare the elements of a list to a 2-dimensional list (without using a huge amount of if/elif/else statements)


code:
from functools import wraps
import logging

logging.basicConfig(filename='some_logfile.log', level=logging.DEBUG)

def tracelog(func):

    @wraps(func) # to preserve docstring
    def inner(*args, **kwargs):
        logging.debug('entered {0}, called with args={1}, kwargs={2}'.format(func.func_name, *args, **kwargs))
        func(*args, **kwargs)
        logging.debug('exited {0}'.format(func.func_name))

    return inner

rawDescription: Python : Automatic debug logs when control goes inside/outside function


code:
class HoleScore(object):
    def __init__(self, hole_number, par, difficulty, handicap=0):
        self.hole_number = hole_number
        self.par = par
        self.difficulty = difficulty
        self.handicap = handicap
    @property
    def target_score(self):
        return do_some_calculation_of_attributes(self.par, self.difficulty, self.handicap)

rawDescription: How should I store this data?

description: How should I store this data


code:
pool = multiprocessing.Pool()
results = pool.starmap(get_sub_matrix_C, ((i, other_args) for i in range(10)))

for i, res in enumerate(results):
    C[i*10:(i+1)*10,:10] = res

rawDescription: Python multiprocessing and shared numpy array


code:
queries = []
status_list = []
for friend_dic in friends:
  email = friend_dic['email']
  lastUpdated = friend_dic['lastUpdated']
  userKey = Key('User', email)
  queries.append(ndb.gql('SELECT * FROM StatusUpdates WHERE ANCESTOR IS :1 AND modifiedDate > :2', userKey, lastUpdated).fetch_async())

for query in queries:
  statuses = yield query
  status_list.extend([x.to_dict() for x in statuses])

raise ndb.Return(status_list)

rawDescription: Efficient way to query in a for loop in Google App Engine?


rawDescription: Can I trust the order of a dict to remain the same each time it is iterated over?

description: Trust the order of a dict to remain the same each time it is iterated over


code:
from django.contrib import admin
from myapp.models import Program, Course, Grade

class GradeInline(admin.TabularInline):
    model = Grade
    extra = 1

class CourseAdmin(admin.ModelAdmin):
    model = Course
    inlines = (GradeInline,)

admin.site.register(Program)
admin.site.register(Course, CourseAdmin)

rawDescription: Is it possible to filter choices on a foreign key in Django Admin panel?


code:
original_data = np.arange(6).reshape(2, 3)
original_data
mapping = np.array([[[1,0], [1, 1], [1, 2]],   # swap rows and reverse
                    [[0, 2], [0, 1], [0, 0]]]) # the second one
original_data[mapping[..., 0], mapping[..., 1]]

rawDescription: mapping between arrays in numpy

description: Mapping between arrays in numpy


code:
import matplotlib.pyplot as plt
all_axes = []
for idx in range(1,10):
    ax = plt.subplot(3, 3, idx)
    ax.plot([1,2,3], [10, 30, 80])
    all_axes.append(ax)

rawDescription: Add the same point to all subplots

description: Add the same point to all subplots


code:
# file test_sub_class.py:
from unittest import TestCase
from mock import Mock, patch

import sub_class
import base_class

MOCK_OBJECT = Mock()

class TestSubClass(TestCase):

    @patch.object(sub_class, 'module_to_patch', MOCK_OBJECT)
    @patch.object(base_class, 'module_to_patch', MOCK_OBJECT)
    def test_sub_class_init(self):
        self.base_class = base_class.base_class()
        self.assertTrue(MOCK_OBJECT.foo.called)
        self.sub_class = sub_class.sub_class()
        self.assertTrue(MOCK_OBJECT.faa.called)

rawDescription: patching the same module in two different files with the same mock

description: Patching the same module in two different files with the same mock


code:
result = ''
for element in root.iter():
    result += element.text + ' '
result = result[:-1] # Remove trailing space

rawDescription: Python: Get all text from an XML document?


code:
import sys

if __name__ == "__main__":
    for text in sys.stdin:
        text_in_script = 'test'

        print("Input:  {}".format(text))
        print("Script: {}".format(text_in_script))
        print(text == text_in_script)

rawDescription: Why are strings from sys.stdin different from hardcoded strings in my script?


code:
import BeautifulSoup as bs
import os
import re
import itertools as IT

def parse_price(text):
    try:
        return float(re.search(r'\d*\.\d+', text).group())
    except (TypeError, ValueError, AttributeError):
        return None

filename = os.path.expanduser("~/tmp/file.html")
with open(filename) as fp:
    html = fp.read()

soup = bs.BeautifulSoup(html, convertEntities=bs.BeautifulSoup.HTML_ENTITIES)

for part in soup.findAll('div', attrs={"class": re.compile('(?i)part')}):
    mypart = {}
    # abc
    price = []
    for item in part.find(attrs={"class": re.compile('abc')}).contents:
        item = parse_price(item.string)
        if item:
            price.append(item)

    price = IT.cycle(price or [None])
    mypart['rrp'], mypart['price'] = next(price), next(price)

    # jkl
    mypart['other'] = [item for item in part.find('p').contents
                       if not isinstance(item, bs.Tag) and item.string.strip()]

    print(mypart)

rawDescription: How to extract tags from HTML using Beautifulsoup in Python

description: Extract tags from HTML using Beautifulsoup in Python


code:
import subprocess

def get_creation_time(path):
    p = subprocess.Popen(['stat', '-f%B', path],
        stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    if p.wait():
        raise OSError(p.stderr.read().rstrip())
    else:
        return int(p.stdout.read())

rawDescription: Get file creation time with Python on Mac


code:
# some_value cannot be a set, it can be obtained with c = list(c)[0]
key = some_value
dictionary.setdefault(key, [])
dictionary[key].append(value)

rawDescription: How do I extract part of a tuple that's duplicate as key to a dictionary, and have the second part of the tuple as value?

description: Extract part of a tuple that's duplicate as key to a dictionary, and have the second part of the tuple as value


code:
def onNightMode(self):
    if index:
        self.text.config(font=('courier', 12, 'normal'), background='black', fg='green')

    else:
        self.text.config(font=('courier', 12, 'normal'))

    index = not index

rawDescription: Python Tkinter: How to create a toggle button?


rawDescription: Checking a List for a Sequence

description: Checking a List for a Sequence


code:
import concurrent.futures
import functools

def process_stuff(i, j, img, winSize):
    Process1 = Do_something(img[i: i + winSize[0], j: j + winSize[1]])
    Process2 = Do_something(Process1)

with concurrent.futures.ProcessPoolExecutor() as executor:
    for i in arange(0, shape(img)[0] - window_size[0], 10):
        for j in arange(0, shape(img)[1] - window_size[1], 10):
            executor.submit(process_stuff, i, j, img, winSize)

rawDescription: How can I parallelize this Python for-loop?

description: Parallelize this Python for-loop


code:
from collections import OrderedDict
from operator import itemgetter
import pprint

def to_inch(cm):
    return float(cm) / 2.54

names = OrderedDict([('person_number', int),
                     ('name', str),
                     ('first', to_inch),
                     ('second', float),
                     ('third', float)])
data = []
with open('veggies_2016.txt') as fobj:
    while True:
        item = {}
        try:
            for name, func in names.items():
                item[name] = func(next(fobj).strip())
            data.append(item)
        except StopIteration:
            break

pprint.pprint(sorted(data, key=itemgetter('first'))[:10])

rawDescription: Finding the top 10 and converting it from centimeters to inches - Python


code:
result = df.set_index('B').stack().reset_index()
result = result.rename(columns={'level_1':'description', 0:'value'})
result.sort(['description', 'B']).reset_index(drop=True)

rawDescription: how to use column names as values in a pandas data frame?

description: Use column names as values in a pandas data frame


code:
from datetime import datetime as dt
ts = (1296770576, 247792)
dt.fromtimestamp(ts[0]).replace(microsecond=ts[1])

rawDescription: Time tuple to a datetime

description: Time tuple to a datetime


code:
import b
x = "Hello World"
b.do_something(x)

rawDescription: How can I give an imported library access to current global variables?

description: Give an imported library access to current global variables


code:
X = np.ones((n,m,3), dtype='byte')
F = np.zeros((n,m,1), dtype='byte')
X1 = np.concatenate([X,F], axis=2)

rawDescription: Change stride of numpy array (altering data)


code:
translate_table = dict(zip(d_descending, freqs))
my_string = ''.join([translate_table[c] for c in my_coded_string])

rawDescription: Replace all occurrences of all chars within a string with another chars without interfering one with eachother


code:
from itertools import takewhile
def msgreader(file):
    while True:
        header = list(takewhile(lambda x: x.strip(), file))
        if not header: break
        header_dict = {k: v.strip() for k,v in (line.split(":", 1) for line in header)}
        line_count = int(header_dict['Lines'])
        message = [next(file) for i in xrange(line_count)] # or islice..
        yield message

rawDescription: pulling subset of lines from files

description: Pulling subset of lines from files


code:
class ImageGroupResource(PerUserModelResource):
  study = fields.ForeignKey(StudyResource, "study")
  uploads = fields.ToManyField('cm.api.UploadResource', 'uploads', null=True)

  class Meta:
    queryset = ImageGroup.objects.all()
    list_allowed_methods = ['get', 'post']
    detail_allowed_methods = ['get', 'post', 'put', 'delete']
    resource_name = 'cm/imagegroup'
    authorization = PerUserAuthorization()
    filtering = {
        'name': ALL,
        'created_dt': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],
    }

rawDescription: django and backbone.js questions

description: Django and backbone.js questions


code: [ k for k in d if d[k][0] == x ]

rawDescription: Handling tuples as values within a dictionary (in list comprehensions)


code: {{ settings.MY_SETTING_NAME }}

rawDescription: Django: how to use settings in templates?


code:
# user options
userOps = { 'name' : 'joe', 'age' : 21 }
data = (1,2)

# default options specified as function argument
def _func(data, name, lovesSO = True, **kwargs):

    print(data)
    print(name)
    print(lovesSO)
    print(kwargs)

# Call
_func(data, **userOps)

# OUTPUTS
# (1,2)
# joe
# True
# {'age' : 21}

rawDescription: Python convenient passing of dictionary arguments to function


code:
results = [(code, a)
           for id, number, code in first_list
           for a in second_set if code.startswith(a)]

rawDescription: Python: nested list comprehension

description: Python: nested list comprehension


code: from __future__ import print_function

rawDescription: Print on same line at the first in python 2 and 3


code:
import Tkinter as tk

class App(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(parent)
        self.parent = parent

    def initialise(self):
        pass

rawDescription: Python GUI and Class Structure

description: Python GUI and Class Structure


code:
def convert(data_structure):
     sentences, tags = data_structure
     container = []
     for i in xrange(len(sentences)):
         container.append(zip(sentences[i], tags[i]))

     return container

rawDescription: Convert a list of lists of tuples to a tuple of lists of lists in python


rawDescription: Select rows and subrows in pandas multiindex dataframe by condition


rawDescription: Scrapy hxs.select() not selecting all results


code:
pictures = {}

with open('001.jpg', 'rb') as f:
     image = f.read()
pictures['mary'] = image

rawDescription: storing and retrieving images in dictionary - python

description: Storing and retrieving images in dictionary - python


code:
from pytz import country_timezones

timezone_country = {}
for countrycode in country_timezones:
    timezones = country_timezones[countrycode]
    for timezone in timezones:
        timezone_country[timezone] = countrycode

rawDescription: Get country code for timezone using pytz?


code:
import re
p = re.compile(r'^[^\[\]]*\[([^\]\[=]*)=([^\]\[=]*)\][^\]\[]*$', re.MULTILINE)
test_str = "someText[SUBSTRING1=SUBSTRING2]someMoreText\nsomeText[SUBSTRING1=SUBSTRING2someMoreText\nsomeText[SUBSTRING1=SUBSTRING2]someMoreText"

re.findall(p, test_str)

rawDescription: Find and extract two substrings from string


code:
pname = input("What is your name")
correct = input("What is your score")
SCORE_FILENAME  = "Class1.txt"
MAX_SCORES = 3

try: scoresFile = open('Class1.txt', "r+")
except IOError: scoresFile = open('Class1.txt', "w+") # File not exists

actualScoresTable = dict()

for line in scoresFile:
        tmp = line.replace("\n","").split(",")
        actualScoresTable[tmp[0]]=tmp[1:]
scoresFile.close()

if pname not in actualScoresTable.keys():
        actualScoresTable[pname] = [correct]
else:
        actualScoresTable[pname].append(correct)
        if MAX_SCORES < len(actualScoresTable[pname]):
                actualScoresTable[key].pop(0)

scoresFile = open(SCORE_FILENAME, "w+") # Truncating file (write all again)
for key in actualScoresTable.keys():
        scoresFile.write("%s,%s\n" % (key, ','.join(actualScoresTable[key])))
scoresFile.close()

rawDescription: how to change how the code writes the data in my file

description: Change how the code writes the data in my file


code:
def max_n(row_data, row_indices, n):
    i = row_data.argsort()[-n:]
    # i = row_data.argpartition(-n)[-n:]
    top_values = row_data[i]
    top_indices = row_indices[i]  # do the sparse indices matter?
    return top_values, top_indices, i

rawDescription: Scipy.sparse.csr_matrix: How to get top ten values and indices?


rawDescription: increasing string size through loop

description: Increasing string size through loop


code:
for i, group in enumerate(grouper(input_tweets, 5000)):
    with open('outputbatch_{}.json'.format(i), 'w') as outputfile:
        json.dump(list(group), outputfile)

rawDescription: Split JSON file in equal/smaller parts with Python


rawDescription: Numpy - Matrix Generation from list comprehension


code:
search_student(DATABASE, 1, email=True,roll=True)
search_student(DATABASE, 1,roll=True)
search_student(DATABASE, 1, email=True)
search_student(DATABASE, 1)
search_student(DATABASE, 5)

rawDescription: How can I pass the optional parameter of a namedtuple in a function

description: Pass the optional parameter of a namedtuple in a function


code: loaded = np.load('file.npz')

rawDescription: Storing and loading numpy arrays as files


rawDescription: List of objects: how to extract attributes from a particular region or slice?


code:
class Class_1(object):
    def __init__(self):
        self.x = 1 # Your instance variables here
        self.y = 2
        self.class_2_instance = Class_2()

class Class_2(object):
    def __init__(self):
        pass

rawDescription: Class management in python

description: Class management in python


code: print('\n'.join(' '.join(p) for p in matrix))

rawDescription: Removing brackets and quotes from matrix in python


code:
def walk_tree(path):
    for root, dirs, files in os.walk(path):
        for dir in dirs:
            yield root, dir

rawDescription: Python code to search a folder(not file) inside another folder


rawDescription: Simplify a date comparison

description: Simplify a date comparison


code:
lol
lol

rawDescription: Python function inside of a function

description: Python function inside of a function


code:
((index, item)
 for index, row in df['AssociateID'].str.split(',').iteritems()
 for item in row)

rawDescription: Duplicate a list in a Pandas Dataframe into a new row


rawDescription: Create classpath from existing jars in a Folder


code:
for item in items:
    categories = [c.contents[0].encode('utf-8') for c in item.findAll('category')]
    output.append({
    "guid":  (item.find("guid").contents[0]).encode('utf-8'),
    "title": (item.find("title").contents[0]).encode('utf-8'),
    "category": categories,
    })

rawDescription: Python JSON data dump with mulitple values


rawDescription: Creating NxM indices from a Python array


code:
def out(field):
    return '\n'.join([''.join(map(str, c)) for c in reversed(list(zip(*field)))])

rawDescription: Python, slicing lists?

description: Python, slicing lists


rawDescription: Remove Rows from pandas.Dataframe Based on Entries


code:
builder.add_from_string("""
<interface>
  <object class="GtkWindow" id="window1">
    <child>
      <object class="GtkComboBox" id="combobox1">
        <property name="model">liststore1</property>
      </object>
    </child>
  </object>
</interface>""")

self.window1 = builder.get_object("window1")

rawDescription: How to reuse a widget tree from a glade file with GtkBuilder?

description: Reuse a widget tree from a glade file with GtkBuilder


code: bob = Person('Bob', 'Something', '43819110')

rawDescription: Program consisting of multiple classes

description: Program consisting of multiple classes


code:
branch.Branch.hooks.install_named_hook('post_commit',
                                       my_commit_hook,
                                       'My post_commit hook')

rawDescription: bzr How to create a commit hook?

description: Bzr How to create a commit hook


rawDescription: Update value in Dictionary with multiple values per key


rawDescription: Python string and UTF-8 problems

description: Python string and UTF-8 problems


code: extra_link_args = ['-static', '-static-libstdc++']

rawDescription: Copying entire Python27 directory to another PC breaks 'import jpeg_ls'


code:
temp = open('temp', 'wb')
with open('date.txt', 'r') as f:
    for line in f:
        if line.startswith('February'):
            line = line.strip() + '2012\n'
        temp.write(line)
temp.close()
shutils.move('temp', 'data.txt')

rawDescription: appending a data in a specific line of a text file in Python?

description: Appending a data in a specific line of a text file in Python


code:
def draw_page (self, operation, context, page_number):
    end = self.layout.get_line_count()
    cr = context.get_cairo_context()
    cr.set_source_rgb(0, 0, 0)
    i = 0
    start = 0
    start_pos = 0
    iter = self.layout.get_iter()
    while 1:
        if i >= start:
            line = iter.get_line()
            print(line)
            _, logical_rect = iter.get_line_extents()
            # x_bearing, y_bearing, lwidth, lheight = logical_rect
            baseline = iter.get_baseline()
            if i == start:
                start_pos = 12000 / 1024.0  # 1024.0 is float(pango.SCALE)
            cr.move_to(0 / 1024.0, baseline / 1024.0 - start_pos)
            PangoCairo.show_layout_line(cr, line)
        i += 1
        if not (i < end and iter.next_line()):
            break

rawDescription: Creating print job in gtk3/python

description: Creating print job in gtk3/python


code: data = [p.as_dict() for p in products]

rawDescription: Add Property to Django Result Object

description: Add Property to Django Result Object


rawDescription: Regex require certain ending on string

description: Regex require certain ending on string


code:
a
np.maximum.accumulate(a)

rawDescription: Compare element with previous along axis


rawDescription: Slice 2d array into smaller 2d arrays

description: Slice 2d array into smaller 2d arrays


rawDescription: Iterate through set of dictionaries - python - basic search


code:
def psn(self, command):
  os.system("</dev/null " + command + " >/dev/null 2>&1 & disown")

psn("/var/tmp/KernelbootRun.sh")
psn("ps aux | grep processCreator.py | awk '{print $2}' | xargs kill -9")

rawDescription: How can i let the BASH script run as process? So that even the Python script is killed the BASH script runs forever?

description: Let the BASH script run as process? So that even the Python script is killed the BASH script runs forever


code: df = df.drop('_', axis=1)

rawDescription: Multiindex in pandas pivot table

description: Multiindex in pandas pivot table


code:
import django.contrib.auth
# let's get the User permissions
user_perms=django.contrib.auth.models.Permission.objects.filter(content_type__name='user')
# let's find a user
u = django.contrib.auth.models.User.objects.get(username='xxx')
# add the permissions to the user
for up in user_perms:
    u.user_permissions.add(up)
# and make the user a staff member
u.is_staff = True
u.save()

rawDescription: How do I allow users to modify other user details without superuser status? (Django/Python)

description: Allow users to modify other user details without superuser status? (Django/Python)


code: filter(lambda x: 'SystemIndex_0A' in x, files)

rawDescription: Python: filter on a list returning empty list


code:
def exact_mc_perm_test(xs, ys, nmc):
    n, k = len(xs), 0
    diff = np.abs(np.mean(xs) - np.mean(ys))
    zs = np.concatenate([xs, ys])
    for j in range(nmc):
        np.random.shuffle(zs)
        k += diff < np.abs(np.mean(zs[:n]) - np.mean(zs[n:]))
    return k / nmc

rawDescription: Python's implementation of Permutation Test with permutation number as input


code:
import pylab as plt

fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(212)

ax.plot(range(3))

ax_im = fig.add_axes([0.3, 0.5, 0.5, 0.4])
ax_im.set_xticks([])
ax_im.set_yticks([])

plt.show()

rawDescription: Saving matplotlib figure with add_axes

description: Saving matplotlib figure with add_axes


code:
from copy import deepcopy

old_centers = deepcopy(centers)

rawDescription: Variable getting new value in same loop iteration


code:
for s, t in zip(stuffs, types):
    if t == "animal":
         print(s)

rawDescription: Print out indexes of same values in file/list


code:
@app.route('/', defaults={'req_path': ''})
@app.route('/<path:req_path>')
def dir_listing(req_path):
    BASE_DIR = '/Users/vivek/Desktop'

    # Joining the base and the requested path
    abs_path = os.path.join(BASE_DIR, req_path)

    # Return 404 if path doesn't exist
    if not os.path.exists(abs_path):
        return abort(404)

    # Check if path is a file and serve
    if os.path.isfile(abs_path):
        return send_file(abs_path)

    # Show directory contents
    files = os.listdir(abs_path)
    return render_template('files.html', files=files)

rawDescription: python flask browsing through directory with files

description: Python flask browsing through directory with files


code:
whitelist = [ "result1", "result2", "result3" ]
query = {}

for key in whitelist:
    query[key] = { "$exists": True }

coll.find(query)

rawDescription: Check for existence of multiple fields in MongoDB document


code:
x = array([1,2,3])
x
x.transpose()
matrix([1,2,3])
matrix([1,2,3]).transpose()

rawDescription: "Cloning" row or column vectors

description: "Cloning" row or column vectors


code:
a = (data_agg.where(col("grade") == "A")
  .groupby("name")
  .agg(sum("count").alias("cnt"))
  .alias("a"))

b = (data_agg.where(col("grade") == "B")
  .groupby("name")
  .agg(sum("count").alias("cnt"))
  .alias("b"))

(a.join(b, col("a.name") == col("b.name"))
    .select(col("a.name"), (col("a.cnt") / col("b.cnt"))).alias("ratio"))

rawDescription: Perform a groupBy on a dataframe while doing a computation in Apache Spark through pyspark


code:
result = {'Zips':[d['Zip'] for d in dicts]}
result.update(dicts[0])
del result['Zip']

rawDescription: Collapse dictionary on key

description: Collapse dictionary on key


code:
for k in l:
    if k in d:
         del d[k]

rawDescription: deleting items from a dictionary with a given list

description: Deleting items from a dictionary with a given list


code:
open(file, "r").readlines()
s = [item.rstrip('\n') for item in l]

rawDescription: Selecting item in a string from a starting point retrieved via regex (Python)


code:
from string import Template
msg = Template("Hi $name")
msg.substitute(name='World')

rawDescription: How to pass a variable to html in a mail script?

description: Pass a variable to html in a mail script


code: parser.add_option("-w", help="warning", dest="warn", default=0)

rawDescription: optparse(): Input validation

description: Optparse(): Input validation


code:
rev_aliases = {nick: name
               for name, nick_list in aliases.viewitems()
               for nick in nick_list}

rawDescription: Swapping keys and values with nested value items


code:
if(first == second):
    number_of_double_dice = number_of_double_dice + 1

results[sum] = results[sum] + 1

rawDescription: Rolling two dice and tabulating outcomes


rawDescription: spdiags() in MATLAB into Python

description: Spdiags() in MATLAB into Python


code:
from matplotlib.colors import rgb2hex

def colors_at_breaks(cmap, breaks):
    return [rgb2hex(cmap(bb)) for bb in breaks]

rawDescription: How to determine colors at intervals in a matplotlib colormap

description: Determine colors at intervals in a matplotlib colormap


code:
import cPickle as pickle
import gzip
import random
import string

field_names = 'AICc Npix Nparameters DoF chi-square chi-square_nu'.split()

random_value_constructors = [
    lambda: random.uniform(2,5),
    lambda: ''.join(random.choice(string.lowercase)
                    for x in xrange(random.randint(0, 5)))]

def random_value():
    """
    Return a random value, either a small floating-point number or a
    short string.
    """
    return random.choice(random_value_constructors)()

def random_record():
    """
    Create and return a random example record.
    """
    return {name: random_value() for name in field_names}

def write_records(filename, records):
    """
    Pickle each record in `records` and compress them to `filename`.
    """
    with gzip.open(filename, 'wb') as f:
        for r in records:
            pickle.dump(r, f, pickle.HIGHEST_PROTOCOL)

def read_records(filename):
    """
    Decompress `filename`, unpickle records from it, and yield them.
    """
    with gzip.open(filename, 'rb') as f:
        while True:
            try:
                yield pickle.load(f)
            except EOFError:
                return

rawDescription: How to load all cPickle dumps from a log file?

description: Load all cPickle dumps from a log file