code:
import requests
url='http://forecast.weather.gov/MapClick.php?lat=47.6062&lon=-122.3321&FcstType=digitalDWML'
r = requests.get(url)

import xmltodict
result = xmltodict.parse(r.text)
cd = result['dwml']['head']['product']['creation-date']['#text']
print("creation-date =",cd)
pop = result['dwml']['data']['parameters']['probability-of-precipitation']['value']
print("\nprobability-of-precipitation =", pop)
hqpf = result['dwml']['data']['parameters']['hourly-qpf']['value']
print("\nhourly-qpf =", hqpf)

rawDescription: XML Parsing to .txt file Python

description: XML Parsing to .txt file Python


code:
import numpy as np

fout = "outfile.txt"

coord = np.array([[0.0,0.0,0.0],[1.0,1.0,1.0],[2.0,2.0,2.0]],float)
nodnr = np.array([[1,2,3]],int)
arr = np.column_stack([nodnr.T, coord])
np.savetxt(fout, arr,
           fmt='{}{}{}'.format('%d', ' '*5, ' '.join(['%14.6E']*coord.shape[1])))

rawDescription: Formatted output from numpy arrays

description: Formatted output from numpy arrays


code: audio_playlist = range(1, 91)

rawDescription: Pythonic way of setting path from list

description: Pythonic way of setting path from list


code: parcelID = values[0]

rawDescription: Python List to String Conversion

description: Python List to String Conversion


code: plt.plot(x, y, '.', color="#e0e0e0")

rawDescription: HTML colors in matplotlib

description: HTML colors in matplotlib


code:
s = "2839 WYATT WY WYSE"
re.sub(r'\bWY\b', 'WAY', s)
s = "2839 WYATT WY"
re.sub(r'\bWY\b', 'WAY', s)

rawDescription: How do I replace a portion of a string using python regex

description: Replace a portion of a string using python regex


code:
dcs = [col for col in df.columns if col.startswith("dc")]
df.groupby(dcs)["uid"].unique()

rawDescription: How to speed up complex/difficult data filtering in pandas

description: Speed up complex/difficult data filtering in pandas


code:
import re
import os
import json

regex = re.compile(r'\$(\w+|\{[^}]*\})')
json_str = '{"key":"${FOO}"}'
def os_expandvar(match):
    v = match.group(1)
    if v.startswith('{') and v.endswith('}'):
        v = v[1:-1]
    return json.dumps(os.environ.get(v, ''))[1:-1]
print(regex.sub(os_expandvar, json_str))

rawDescription: How to expand environment variable in python while escaping special characters?

description: Expand environment variable in python while escaping special characters


code:
s = "asd##1-2####qwe"
import re
[m.start() for m in re.finditer(r'(?=##)', s)]

rawDescription: How to find a index of an item in a string?

description: Find a index of an item in a string


code:
def addTerm(self, term):
   node = self.tree
   for c in term:
      c = c.lower()
      if re.match("[a-z]",c):
         node = node[0].setdefault(c,[{},None])
   node[1] = term

rawDescription: Accessing a Dynamically Generated Nested Dictionary


code:
def delimited(file, delimiter='\n', bufsize=4096):
    buf = ''
    while True:
        newbuf = file.read(bufsize)
        if not newbuf:
            yield buf
            return
        buf += newbuf
        lines = buf.split(delimiter)
        for line in lines[:-1]:
            yield line
        buf = lines[-1]

rawDescription: How to read records terminated by custom separator from file in python?

description: Read records terminated by custom separator from file in python


code:
for polygon in polygonize(mls):
    print(polygon)

rawDescription: Splitting self-intersecting polygon only returned one polygon in shapely in Python


code:
from itertools import count

def remap(fieldnames):
    price_count = count(1)
    return ['price{}'.format(next(price_count)) if f.startswith('price') else f
            for f in fieldnames]

rawDescription: How to handle csv file with duplicate fieldnames when reading with csv.DictReader?

description: Handle csv file with duplicate fieldnames when reading with csv.DictReader


rawDescription: Select certain rows (condition met), but only some columns in Python/Numpy


code: dir(obj) == sorted(obj.__dict__.keys() + dir(obj.__class__))

rawDescription: Is it possible to delete a method from an object (not class) in python?


code:
from glob import glob1
from types import FunctionType

VIEW_METHOD_PREFIX = ''    # Whatever you like here, I suggest you use something
VIEWS_DIR          = 'views'    # Where you views are

def import_views():

    for filename in glob1(VIEWS_DIR, '*.py'):
        if filename == '__init__.py':    # I assume you don't want that
            continue

        module_name = os.path.basename(filename).split('.py')[0]

        # You might need to change this, depending on where you run the file:
        imported_module = __import__(
            module_name, fromlist=[module_name,])

        for method_name, method in imported_module.__dict__.iteritems():
            if not isinstance(method, FunctionType):
                continue
            if not method_name.startswith(VIEW_METHOD_PREFIX):
                continue
            globals()[method_name] = method

rawDescription: Django sub-directory views and import all files from __init__.py


code:
mask = mesh.x > para[1]
myValues[mask] = mesh.x[mask]
myValues[mask] -= para[1]
myValues[mask] **= para[2]
myValues[mask] *= para[0]

rawDescription: Numpy array variable avoid bad calculations


rawDescription: Tabulate data frame with groupby and size methods


rawDescription: How to read from a text file in python line by line

description: Read from a text file in python line by line


code:
for dataset in values:
    for key, value in mapping.items():
        values[dataset][key] += value
    with open(dataset + ".txt") as out:
        out.write(dataset)
        for key,value in values[dataset]:
            out.write("{},{}\n".format(key,value))

rawDescription: Manipulating text files

description: Manipulating text files


code:
def do_option1(args):
    param1 = args.param1
    # And continue

rawDescription: Python Command Line Arguments: Calling a function


code:
# Define a convenience function to add items to the dictionary
def add_item(d,i): d[i.kod] = i

# Create a new dictionary
items = {}

# Add items to dictionary
add_item(items, vara(223, "Hat", 150, 100))
add_item(items, vara(225, "Shirt", 450, 10))

rawDescription: function and classes, usage of __str__

description: Function and classes, usage of __str__


code: set(dir(Cls)) - set(dir(object))

rawDescription: Iterate over object attributes in python


code:
def fileno(self):
    """Invoke the underlying file object's fileno() method."""
    return self.fileobj.fileno()

rawDescription: python subprocess with gzip

description: Python subprocess with gzip


code:
{'Monday':  False,
    'Tuesday': True,
    'Wednesday': False,
    'Thursday': False,
    'Friday': True,
    'Saturday': False,
    'Sunday': True,
}

rawDescription: What kind of return is that?

description: What kind of return is that


code:
def getlines(somefile):
    f = open(somefile).readlines()
    lines = [line for line in f if not line.startswith("#") and not line.strip() == ""]
    return lines

entries = getlines(input("Name of input file: "))
animal_visits = {}
month_visits = [0] * 13

for entry in entries:
    # count visits for each animal
    animal = entry[:3]
    animal_visits[animal] = animal_visits.get(animal, 0) + 1

    # count visits for each month
    month = int(entry[4:6])
    month_visits[month] += 1

print("Total Number of visits for each animal")
for x in sorted(animal_visits):
    print(x, "\t", animal_visits[x])

print("====================================================")
print("Month with highest number of visits to the stations")
print(month_visits.index(max(month_visits)))

rawDescription: Files in python

description: Files in python


code:
#pythran export loop(int, float, float, float, float, float, float, float, float)
from math import exp, sqrt, pi
import cmath
from numpy import empty

def loop(n,A, sigma, sin_phi, cos_phi,
                                 sin_theta, cos_theta, mu_x, mu_y):
    out = empty((n,n), dtype=complex)
    for x in range(n):
        for y in range(n):
            out[x,y] = calculate_amplitude(x,y,A, sigma, sin_phi, cos_phi,
                                           sin_theta, cos_theta, mu_x, mu_y)
    return out

def calculate_amplitude(x, y, A, sigma, sin_phi, cos_phi,
                        sin_theta, cos_theta, mu_x, mu_y):

    amplitude = (A * (1 / (sqrt(2  * pi * (sigma ** 2 / cos_theta ** 2)))) *
                 exp(-((cos_phi *(x - mu_x) + sin_phi *(y - mu_y)) ** 2 * cos_theta ** 2) /
                     (2 * sigma ** 2)) *
                 (1 / (sqrt(2 * pi * sigma ** 2))) *
                 exp(-((-sin_phi * (x - mu_x) + cos_phi * (y - mu_y)) ** 2) /
                     (2 * sigma ** 2)) *
                 cmath.exp(1j *(2 * pi / 0.650) * sin_theta *
                           (cos_phi * (x - mu_x) + sin_phi * (y - mu_y))))

    return amplitude

rawDescription: Python simulation shows slow performance, how to speed array calculation up


rawDescription: Python optimization problem?

description: Python optimization problem


rawDescription: Running separate code while a socket server is running?


rawDescription: How to unpack a tuple into more values than the tuple has?

description: Unpack a tuple into more values than the tuple has


code:
def make_callback(m_type, other_type, *attrs):
    def callback(msg):
        m = m_type(msg)
        a = other_type()
        for attr in attrs:
            setattr(a, attr, getattr(m, attr))
        pub2.publish(a)
    return callback

callback1 = make_callback(MsgX, msg_type_a, 'x')
callback2 = make_callback(MsgY, msg_type_b, 't', 'u')

rawDescription: How to avoid similar function definitions in Python?

description: Avoid similar function definitions in Python


rawDescription: Accuracy of deriving the CDF using integration


code:
with open('testing', 'r') as y:
    originalList = [line.strip().split()[0] for line in y]

rawDescription: python when adding values from a file to a list. only add the first column from that list

description: Python when adding values from a file to a list. only add the first column from that list


code: (arr > threshold) * 255

rawDescription: What's the fastest way to threshold a numpy array?


code: 'George 25'.split(' ')

rawDescription: How to assign user input to a variable or list

description: Assign user input to a variable or list


code:
from contextlib import ExitStack

with ExitStack() as stack:
    if not args.files:
       files = [sys.stdin]
    else:
       files = [stack.enter_context(open(name)) for name in args.files]

    if not args.output:
       output_file = sys.stdout
       stack.callback(output_file.flush) # flush instead of closing
    else:
       output_file = stack.enter_context(open(args.output, 'w'))

    process(files, output_file)

rawDescription: Context manager for optionally redirected I/O


code: all(isinstance(n, int) for n in lst)

rawDescription: Test type of elements python tuple/list

description: Test type of elements python tuple/list


code:
import MySQLdb as mdb

con = mdb.connect('localhost', 'testuser', 'test623', 'testdb')

rawDescription: How to transfer SQL Group by results to a Pandas dataframe

description: Transfer SQL Group by results to a Pandas dataframe


rawDescription: Python Numpy Addition

description: Python Numpy Addition


code:
import subprocess
output = subprocess.check_output(["staf", "server2.com PROCESS START SHELL COMMAND 'uname' WAIT RETURNSTDOUT STDERRTOSTDOUT"])

rawDescription: Create python map based on bash output

description: Create python map based on bash output


code:
args.debug
args('debug')

rawDescription: Confusing code involving lambda function


code:
from Tkinter import *

class loginWindow(Toplevel):
    def __init__(self, title, parent):
        Toplevel.__init__(self, parent)
        # Save parent reference to use it
        self.parent = parent
        self.parent.title(u"Geometry Calc - Login")
        Button(self, text="Login", command=self.login).pack()

    def login(self):

        access =  True # Used to test if a user can login.

        if access:
            # Close Toplevel window and show root window
            self.destroy()
            self.parent.deiconify()
        else:
            self.parent.quit()

class main(Tk):
    def __init__(self, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.title(u"Geometry Calc")  # Nadpis
        self.geometry("695x935")  # Rozmery v px
        self.config(background="white")
        self.resizable(width=FALSE, height=FALSE)  # Zakážeme změnu rozměrů uživatelem - zatím..

        menubar = Menu(self)

        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Konec", command=self.quit)
        menubar.add_cascade(label="Soubor", menu=helpmenu)
        helpmenu = Menu(menubar, tearoff=0)
        helpmenu.add_command(label="O programu", command=self.createAbout)
        menubar.add_cascade(label="Pomoc", menu=helpmenu)
        self.config(menu=menubar)

        canvas = Canvas(self, width=691, height=900)
        canvas.pack(expand=1, fill=BOTH)

        # Hide root window
        self.withdraw()

        # Lunch login window
        loginWindow('Frame', self)

    def createAbout(self):
        pass

    def quit(self):
        self.destroy()

app = main()

app.mainloop()

rawDescription: Two windows: First Login after that main program


code:
@app.route('/count/')
def count_100():
    """
        this starts a counter task and returns a response immediately
    """
    task = counter.delay()
    # this will return an empty json object with 202 http code status
    # which means requests is still in progress and a Location header
    return jsonify(), 202, dict(Location=url_for('status', task_id=task.id))

rawDescription: How to retrieve meta from celery backend storage

description: Retrieve meta from celery backend storage


code:
def show(text):
    return True

rawDescription: How to suppress execution of statement in script called with execfile?

description: Suppress execution of statement in script called with execfile


code:
class Service(models.Model):
    name = models.CharField(max_length=5)

class Order(models.Model):
    name = models.CharField(max_length=5)
    service = models.ForeignKey(Service)

class Rating(models.Model):
    order = models.OneToOneField('Order')
    grade = models.IntegerField()

rawDescription: Django sort queryset according to related objects


code:
db = connection.DBNAME
db = getattr(connection,'DBNAME')

rawDescription: Set a mongo database in pymongo without eval(). Maybe setattr()?


code:
my_dir = QtGui.QFileDialog.getExistingDirectory(
    self,
    "Open a folder",
    expanduser("~"),
    QtGui.QFileDialog.ShowDirsOnly
)

rawDescription: PyQt: QFileDialog.getExistingDirectory using a default directory, user independant


code: np.concatenate((a,z), axis=1)

rawDescription: Good ways to "expand" a numpy ndarray?

description: Good ways to "expand" a numpy ndarray


code:
import re
from pprint import pprint

s ="< one > < two > < three > < four : 1 > < five : 2 > < six : 3 > < seven : 4 > < eight : 5 > < nine : 6 >"
res = re.findall('< (.*?) >', s)
pprint(res)

rawDescription: Deleting a character in an element of a 2D list in Python


rawDescription: output of numpy.where(condition) is not an array, but a tuple of arrays: why?

description: Output of numpy.where(condition) is not an array, but a tuple of arrays: why


code:
x = [1]
y = [2]
z = x + y
x.append(1)
assert z == [1, 2] # z is not effected by the mutation of x

rawDescription: Concatenate generators in python 3 with +-operator


code:
def csr_add_sparse_vec(sps_mat, sps_vec) :
    """Adds a sparse vector to every row of a sparse matrix"""
    # No checks done, but both arguments should be sparse matrices in CSR
    # format, both should have the same number of columns, and the vector
    # should be a vector and have only one row.

    rows, cols = sps_mat.shape
    nnz_vec = len(sps_vec.data)
    nnz_per_row = np.diff(sps_mat.indptr)
    longest_row = np.max(nnz_per_row)

    old_data = np.zeros((rows * longest_row,), dtype=sps_mat.data.dtype)
    old_cols = np.zeros((rows * longest_row,), dtype=sps_mat.indices.dtype)

    data_idx = np.arange(longest_row) < nnz_per_row[:, None]
    data_idx = data_idx.reshape(-1)
    old_data[data_idx] = sps_mat.data
    old_cols[data_idx] = sps_mat.indices
    old_data = old_data.reshape(rows, -1)
    old_cols = old_cols.reshape(rows, -1)

    new_data = np.zeros((rows, longest_row + nnz_vec,),
                        dtype=sps_mat.data.dtype)
    new_data[:, :longest_row] = old_data
    del old_data
    new_cols = np.zeros((rows, longest_row + nnz_vec,),
                        dtype=sps_mat.indices.dtype)
    new_cols[:, :longest_row] = old_cols
    del old_cols
    new_data[:, longest_row:] = sps_vec.data
    new_cols[:, longest_row:] = sps_vec.indices
    new_data = new_data.reshape(-1)
    new_cols = new_cols.reshape(-1)
    new_pointer = np.arange(0, (rows + 1) * (longest_row + nnz_vec),
                            longest_row + nnz_vec)

    ret = sps.csr_matrix((new_data, new_cols, new_pointer),
                         shape=sps_mat.shape)
    ret.eliminate_zeros()

    return ret

rawDescription: Adding a very repetitive matrix to a sparse one in numpy/scipy?


rawDescription: How to keep dates as index and inner join keys in multiple excel files

description: Keep dates as index and inner join keys in multiple excel files


code: [z for x in lis for y in x for z in y]

rawDescription: Convert this list, tuple in list only in Python?


rawDescription: Slicing a string with standard indexing

description: Slicing a string with standard indexing


code: sortedlist = sorted(reader, key=lambda row: row[3], reverse=True)

rawDescription: sort csv by column

description: Sort csv by column


code:
result = pd.concat(
    [df2, pd.concat([df2[[col]].merge(
                         df1[[col, 'FREQ_' + str(col), 'SUCCESS_' + str(col)]],
                         on=col, how='left').iloc[:, 1:]
                     for col in df2], axis=1)],
    axis=1)

rawDescription: Python Pandas: DataFrame as a Lookup Table


code:
i, j = (i, j) if i < j else (j, i) # make sure i < j
s = s[:i] + s[j] + s[i+1:j] + s[i] + s[j+1:]

rawDescription: Python Swap two digits in a number?

description: Python Swap two digits in a number


code:
string = 'someString'
globals()[string] = dict()

rawDescription: Usng the value of a string as a variable name


rawDescription: How to reference an element of a list inside itself?

description: Reference an element of a list inside itself


code:
def prob_tree_with_check(data,ini,visited=frozenset()):
    """Generator of all end points of the probability tree contained
       in data, starting with ini. Check if a previously visited branch
       of the tree is visited again and raise RuntimeError in that case"""
    if ini in visited:
        raise RuntimeError("Branch allready visited: %r"%ini)
    visited = visited.union((ini,))
    for prob,path in data[ini]:
        no_more,more = map(tuple,partition(lambda x: x in data,path))
        if more:
            for node in itertools.product( *[prob_tree_with_check(data,x,visited) for x in more] ):
                new_prob,new_path = functools.reduce(lambda acum,new: (acum[0]*new[0],acum[1]+new[1]),node,(prob,tuple()))
                yield new_prob, no_more + new_path
        else:
            yield prob, no_more

mydata_bad = {1: [[.9, [2,3]], [.1, [4,5]]],
          4: [[.2, [6,7]], [.5, [8,9]], [.3, [10,11,12]]],
          5: [[.4, [13,14]], [.6, [15,16,1]]] # <-- try to go back to 1
          }

rawDescription: Calculate probabilities in a probability tree


code: numpy.linspace(0, 1, num=50, endpoint=True)

rawDescription: Binning values of a function in Python (numpy)


code:
indices = frozenset(range(step))
arr = np.array(i % step2 in indices for i in range(len(seq)))
result = seq[arr]

rawDescription: What is the pythonic way of slicing an array into stepped contiguous blocks?


code:
from itertools import groupby
lst = [[ 'something', ',', 'eh' ], ',', ['more'], ',', 'yet more', '|', 'even more' ]
[list(group) for key, group in groupby(lst, lambda x: x!=',') if key]

rawDescription: Splitting a list of lists and strings by a string


code:
7.79690480232
20 ['/atlanta', '/new-york', '/san-francisco', '/washington-dc', '/charlotte', '/miami', '/philadelphia', '/houston', '/minneapolis', '/phoenix', '/san-diego', '/nashville', '/austin', '/boston', '/chicago', '/dallas', '/denver', '/los-angeles', '/seattle', '/portland']
13 ['http://feeds.feedburner.com/scoutmob/atl', 'http://feeds.feedburner.com/scoutmob/nyc', 'http://feeds.feedburner.com/scoutmob/sf', 'http://scoutmob.com/washington-dc.rss', 'http://scoutmob.com/nashville.rss', 'http://scoutmob.com/austin.rss', 'http://scoutmob.com/boston.rss', 'http://scoutmob.com/chicago.rss', 'http://scoutmob.com/dallas.rss', 'http://scoutmob.com/denver.rss', 'http://scoutmob.com/los-angeles.rss', 'http://scoutmob.com/seattle.rss', 'http://scoutmob.com/portland.rss']
4.76977992058
310

rawDescription: Python creating worker queues

description: Python creating worker queues


code:
def handle_enter():
    draw.counter = 0

onkey(handle_enter, 'Enter')

rawDescription: Count number of times function with varying arguments executed, then reset count when ENTER key pressed


code:
adict={}
for ddtt in dt:
    data=ddtt.split()
    if data[0] in adict:
        adict[data[0]].append(data[1])
    else:
        adict[data[0]] = [data[1]]

rawDescription: Split list of datetime objects into keys(date) and values(times)


code:
class TestSomething(unittest.TestCase): # Fixed that for you!

    def test_first_case(self):
         my_module.MY_CONFIG_VARIABLE = True
         self.assertEqual(my_module.my_function_with_global_var(), "First result")

    def test_second_case(self):
         my_module.MY_CONFIG_VARIABLE = False
         self.assertEqual(my_module.my_function_with_global_var(), "Second result")

rawDescription: Modifying global variables in Python unittest framework


code:
import numpy as np
from scipy import integrate, special

# P \int_{-1}^1 dx 1/(x - wvar) * (1 + sin(x))
print(integrate.quad(lambda x: 1 + np.sin(x), -1, 1, weight='cauchy', wvar=0))
# -> (1.8921661407343657, 2.426947531830592e-13)

# Check against known result
print(2*special.sici(1)[0])
# -> 1.89216614073

rawDescription: Python: Find principal value of an integral numerically


code:
def flatten_n(n, iterable):
    for x in reduce(apply, [itertools.chain.from_iterable] * n, iterable):
        yield x

rawDescription: How to flatmap list of lists of lists of tuples in Python?

description: Flatmap list of lists of lists of tuples in Python


code:
isinstance(a, A)
type(a) == A

rawDescription: Why use isinstance() instead of type()?

description: Why use isinstance() instead of type()


code: C[d,e,f + b[:, np.newaxis]].mean(axis=1).dot(g)

rawDescription: Speed up for loop with numpy

description: Speed up for loop with numpy


code: x = raw_input("Enter X:")

rawDescription: How to make a simple GUI for inputting a single variable in Python?

description: Make a simple GUI for inputting a single variable in Python


code:
import pandas as pd
df = pd.DataFrame(['present absent absent present'.split(),
                   'absent present present absent'.split()],
                  columns='elem_1_pre elem_1_post elem_2_pre elem_2_post'.split(),
                  index='Observation1 Observation2'.split(),)
df = pd.DataFrame(df.values == 'present',
                  columns=df.columns,
                  index=df.index)
print(df)
#              elem_1_pre elem_1_post elem_2_pre elem_2_post
# Observation1       True       False      False        True
# Observation2      False        True       True       False

for i in range(1,3):
    elem = ['elem_{i}_{s}'.format(i=i, s=suf) for suf in ('pre', 'post')]
    change = 'elem_{i}_change'.format(i=i)
    df[change] = df[elem[0]] ^ df[elem[1]]
print(df.ix[:, 'elem_1_change elem_2_change'.split()])

rawDescription: Pandas: extract and select data from columns using a pattern


code:
import re
from random import randint

def select(m):
    choices = m.group(1).split('|')
    return choices[randint(0, len(choices)-1)]

def spinner(s):
    r = re.compile('{([^{}]*)}')
    while True:
        s, n = r.subn(select, s)
        if n == 0: break
    return s.strip()

rawDescription: How to make article spinner regex?

description: Make article spinner regex


code:
# people.py

from bulbs.model import Node, Relationship
from bulbs.property import String, Integer, DateTime
from bulbs.utils import current_datetime

class Person(Node):

    element_type = "person"

    name = String(nullable=False)
    age = Integer()

class Knows(Relationship):

    label = "knows"

    created = DateTime(default=current_datetime, nullable=False)

rawDescription: How to Emulate Assignment Operator Overloading in Python?


rawDescription: Need a way to load embedded, escaped JSON strings in Python


code:
from openpyxl import load_workbook
from time import time

#Load workbook
start_time = time()
wb = load_workbook('output.xlsx')
print('Elapsed time for loading workbook: %.2f' % (time.time()-start_time))

#Read all data
start_time = time()
ws = wb.active
cell_range1 = ws['A1':'P20001']
cell_range2 = ws['Q1':'AF20001']
print('Elapsed time for reading workbook: %.2f' % (time.time()-start_time))

#Save to a new workbook
start_time = time()
wb.save("output_tmp.xlsx")
print('Elapsed time for saving workbook:  %.2f' % (time.time()-start_time))

rawDescription: Optimize add row in xls file with xlwt

description: Optimize add row in xls file with xlwt


rawDescription: Extracting keys-values from dictionary

description: Extracting keys-values from dictionary


code: df = pd.DataFrame(np.random.randn(100, 5), columns = list("abcde"))

rawDescription: Pandas create random samples without duplicates


code:
from ast import literal_eval
s = r'Hello,\nworld!'
print(literal_eval("'%s'" % s))

rawDescription: How to remove all the escape sequences from a list of strings?

description: Remove all the escape sequences from a list of strings


code:
class C:
    def __init__(self, a, b):
        self.a = a
        self.b = b

    def method3(self):
        return self.a.method1() + self.b.method2()

c1 = C(a1, b)
c2 = C(a2, b)

rawDescription: Changing base class in a inheritance hierarchy


code:
import a
import b

rawDescription: Can I import a built-in module twice in both my script and custom module

description: Import a built-in module twice in both my script and custom module


code:
def next_prev(f):
  previous_value = f(0)
  i = 1
  while True:
    next_value = f(i)
    yield next_value, previous_value
    i += 1
    previous_value = next_value

rawDescription: Recurrence sequence in Java / Python / Mathematica


code:
def run_test():
    ask_costsurfacepath_path()
    ask_outputpath_path()
    CostSurfacefn = config.costsurfacepath
    startCoord = (config.startX,config.startY)
    stopCoord = (config.stopX,config.stopY)
    outputPathfn = config.outputpath
    main(CostSurfacefn,outputPathfn,startCoord,stopCoord)

if __name__ == "__main__":
    run_test()

rawDescription: Run a python script from another python script using IF __name__ == "__main__" statement


code:
from registration.view import activate
def custom_activate(request, backend,
         template_name='registration/activate.html',
         success_url=None, extra_context=None, **kwargs):
    success_url = request.GET.get('next', None)
    return activate(request, template_name=template_name, success_url=success_url, extra_context=None, **kwargs)

rawDescription: Getting `django-registration` to send you to the page you were originally trying to visit


code:
def choose_acct():
    while True:
        get = raw_input(r'Adap1, Adap2, Adap3, or Adap4? ')
        result = adict.get(get, None)
        if result:
            return result

rawDescription: How to change a global variable in python?

description: Change a global variable in python


code: self.servers = { 1998: None, 1999: None }

rawDescription: Client-side Socket on Python, how to contact multiple server?


code:
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages

pie_data = [[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 0, 1, 2]]
pie_slice_labels = ['A', 'B', 'C', 'D']
pie_labels = ['1', '2', '3']

PDFfile = PdfPages('output.pdf')

for i in range(4):
    fig, axes = plt.subplots(ncols=len(pie_labels) + 1, figsize=(6,2))

    for ax, data, label in zip(axes, pie_data, pie_labels):
        wedges, labels = ax.pie(data)
        ax.set(title=label, aspect=1)

    # Instead of creating a dummy pie, just use the artists from the last one.
    axes[-1].legend(wedges, pie_slice_labels, loc='center', fontsize='small')
    axes[-1].axis('off')
    # Alternately, you could do something like this to place the legend. If you
    # use this, you should remove the +1 from ncols above.
    # fig.legend(wedges, pie_slice_labels, loc='center right', fontsize='small')
    # fig.subplots_adjust(right=0.8) # Make room for the legend.

    PDFfile.savefig()

PDFfile.close()

rawDescription: PyPlot: Trying to generate shared legend in a subplot, but legends don't appear properly when embedded in a loop. (PDF backend)


rawDescription: python recursive vectorization with timeseries

description: Python recursive vectorization with timeseries


code:
clientEndpoint = ProcessEndpoint(reactor, "/path/to/child", ("argv",), ...)
serverEndpoint = StandardIOEndpoint(reactor)

rawDescription: Can Perspective Broker be used over stdio instead of TCP?


code:
version-1
filter_id = 4567
q = (session.query(Table3)
    .filter(Table3.table2.has(Table2.table1_id == filter_id))
    )
q.delete(synchronize_session=False)

rawDescription: SQLAlchemy - delete grandchildren

description: SQLAlchemy - delete grandchildren


code:
FormSet = modelformset_factory(MyModel, form=MyForm, extra=0)
if request.method == 'POST':
    formset = FormSet(request.POST, request.FILES)
    # Your validation and rest of the 'POST' code
else:
    query = MyModel.objects.filter(condition)
    paginator = Paginator(query, 10) # Show 10 forms per page
    page = request.GET.get('page')
    try:
        objects = paginator.page(page)
    except PageNotAnInteger:
        objects = paginator.page(1)
    except EmptyPage:
        objects = paginator.page(paginator.num_pages)
    page_query = query.filter(id__in=[object.id for object in objects])
    formset = FormSet(queryset=page_query)
    context = {'objects': objects, 'formset': formset}
    return render_to_response('template.html', context,
                              context_instance=RequestContext(request))

rawDescription: Paginate Django formset

description: Paginate Django formset


code:
timeit('float(i) ** j', setup='i, j = 7, 5')
timeit('i ** float(j)', setup='i, j = 7, 5')
timeit('float(i ** j)', setup='i, j = 7, 5')
timeit('math.pow(i, j)', setup='import math; i, j = 7, 5')

rawDescription: Exponentials in python x.**y vs math.pow(x, y)


code:
SPACE_BETWEEN_KEYS_AND_VALUES = 1
MINIMUM_PADDING = 10
padded_width = max(MINIMUM_PADDING, max(len(x) for x in my_dict.iterkeys()) + SPACE_BETWEEN_KEYS_AND_VALUES)
for key in my_dict:
    print("{key: <{width}}{value}".format(key=key, width=padded_width, value=my_dict[key]))

rawDescription: how to neatly print a dictionary

description: Neatly print a dictionary


rawDescription: Python Image Reverse

description: Python Image Reverse


code:
utc_time = datetime(2016, 1, 12, 9, 0)
(utc_time - datetime(1970, 1, 1)).total_seconds()

rawDescription: Convert TLE times (decimal days) to seconds after epoch


code:
srv.py
import socket
import threading
import struct
import string

class clientThread(threading.Thread):
    def __init__(self, serv):
        threading.Thread.__init__(self)
        self.server = serv
        self.clientList = []
        self.running = True
        print("Client thread created. . .")
    def run(self):
        print("Beginning client thread loop. . .")
        while self.running:
            for client in self.clientList:
                message = client.sock.recv(self.server.BUFFSIZE)
                if message != None and message != "":
                    client.update(message)

class clientObject(object):
    def __init__(self,clientInfo):
        self.sock = clientInfo[0]
        self.address = clientInfo[1]
    def update(self,message):
        self.sock.send("Testamundo.\r\n".encode())

class Server(object):
    def __init__(self):
        self.HOST = 'localhost'
        self.PORT = 22085
        self.BUFFSIZE = 1024
        self.ADDRESS = (self.HOST,self.PORT)
        self.clientList = []
        input("Press enter to start the server. . .")
        self.running = True
        self.serverSock = socket.socket()
        self.serverSock.bind(self.ADDRESS)
        self.serverSock.listen(2)
        self.clientThread = clientThread(self)
        print("Starting client thread. . .")
        self.clientThread.start()
        print("Awaiting connections. . .")
        while self.running:
            clientInfo = self.serverSock.accept()
            print("Client connected from {}.".format(clientInfo[1]))
            self.clientThread.clientList.append(clientObject(clientInfo))

        self.serverSock.close()
        print("- end -")

serv = Server()

rawDescription: python multithreaded server

description: Python multithreaded server


code:
inds
i, j = zip(*inds)
i
j

rawDescription: How to extract a list of elements given by their indices from a numpy array efficiently?

description: Extract a list of elements given by their indices from a numpy array efficiently


code:
a = "hello"
b = a.upper
b()

rawDescription: How do I derive from hashlib.sha256 in Python?

description: Derive from hashlib.sha256 in Python


code:
import matplotlib.backends.backend_tkagg as backend
from matplotlib.figure import Figure

fig = Figure()
ax = fig.add_subplot(111)

manager = backend.new_figure_manager_given_figure(1, fig)
manager.show()
backend.show.mainloop()

rawDescription: Prevent matplotlib statefulness

description: Prevent matplotlib statefulness


code: filter(name="Comp1" and "Employee")

rawDescription: Multi-Group rules in view to enforce redirect?


rawDescription: Filter a pandas dataframe using values from a dict


rawDescription: Adding a value to each column sequentially


code: rangesets = [rangeset_001, rangeset_002, rangeset_003]

rawDescription: python, functions combination via names and chain them to invoke in-a-go?

description: Python, functions combination via names and chain them to invoke in-a-go


rawDescription: regular expression for ipaddress and mac address

description: Regular expression for ipaddress and mac address


code:
def add_to_multidict(multidict, key, value):
    if key in multidict:
        if isinstance(multidict[key], tuple):
            multidict[key] = multidict[key] + (value,)
        else:
            multidict[key] = (multidict[key], value)
    else:
        multidict[key] = value

rawDescription: adding values to default dict list of values

description: Adding values to default dict list of values


code: a = df.pivot_table(index='Date', columns='Student', values='Grade')

rawDescription: Format a dataframe using Python pandas

description: Format a dataframe using Python pandas


code:
import asyncio
from test import Test

@asyncio.coroutine
def oldmain():
    print("entering with")
    with Test() as t:
        yield from t.init_state()
        print("in here")

loop = asyncio.get_event_loop()
loop.run_until_complete(oldmain())

rawDescription: Coroutine in python between 3.4 and 3.5, How can I keep backwords compatibility?


code:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.animation import Animation
from kivy.properties import NumericProperty

import math

kv = '''
<Dial>:
    canvas:
        Rotate:
            angle: root.angle
            origin: self.center
        Color:
            rgb: 1, 0, 0
        Ellipse:
            size: min(self.size), min(self.size)
            pos: 0.5*self.size[0] - 0.5*min(self.size), 0.5*self.size[1] - 0.5*min(self.size)
        Color:
            rgb: 0, 0, 0
        Ellipse:
            size: 50, 50
            pos: 0.5*root.size[0]-25, 0.9*root.size[1]-25
'''
Builder.load_string(kv)

class Dial(Widget):
    angle = NumericProperty(0)

    def on_touch_down(self, touch):
        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        self.prev_angle = calc if calc > 0 else 360+calc
        self.tmp = self.angle

    def on_touch_move(self, touch):
        y = (touch.y - self.center[1])
        x = (touch.x - self.center[0])
        calc = math.degrees(math.atan2(y, x))
        new_angle = calc if calc > 0 else 360+calc

        self.angle = self.tmp + (new_angle-self.prev_angle)%360

    def on_touch_up(self, touch):
        Animation(angle=0).start(self)

class DialApp(App):
    def build(self):
        return Dial()

if __name__ == "__main__":
    DialApp().run()

rawDescription: Rotating an object on a touch event in kivy


code:
import re
from mmap import ACCESS_READ, mmap

with open(path_to_sdf) as f, mmap(f.fileno(), 0, access=ACCESS_READ) as s:
    # arg = re.escape(arg) # call it if you want to match arg verbatim
    output = re.findall(r'INSTANCE {}((?!^\)).*\n)+'.format(arg).encode(), s,
                        flags=re.DOTALL | re.MULTILINE)

rawDescription: Python multi-line pattern matching

description: Python multi-line pattern matching


code:
from datetime import datetime, timedelta
arrayX = []
dt = datetime(2008, 7, 11)
while dt <= datetime(2008, 7, 14):
    arrayX.append(int(dt.strftime('%Y%m%d')))
    dt += timedelta(days=1)

rawDescription: Assigning a range to an array in Python

description: Assigning a range to an array in Python


code:
sums = [items[0].freq]
for i in items[1:]:
    sums.append(sums[-1] + i.freq)

rawDescription: Python: Summing class instances inside a list


code:
print(" ".join(["Row{:<2}".format(i) for i in range(1, len(numbers[0])+1)]))
for w1 in numbers:
    print("".join(["{:<6}".format(i) for i in w1]))

rawDescription: how to print lists within a tuple in straight line

description: Print lists within a tuple in straight line


code:
class Rectangle(object):
    def __init__(self, top_corner, width, height):
        self._x = top_corner[0]
        self._y = top_corner[1]
        self._width = width
        self._height = height

    def get_bottom_right(self):
        d = self._x + self.width
        t = self._y + self.height
        return (d,t)

rawDescription: Pygame Rect class

description: Pygame Rect class


rawDescription: modify sign of numbers in two different columns of an RDD in PySpark

description: Modify sign of numbers in two different columns of an RDD in PySpark


code:
cntry = content.xpath('//tr[@class="data1"]/td[1]/text()')
cntry = [str(x).strip() for x in cntry]

rawDescription: Duplicates when extracting data from html table using lxmk.html.xpath()


code:
#!/usr/bin/env python3
from contextlib import ExitStack
from subprocess import CalledProcessError, Popen, PIPE

def kill(process):
    if process.poll() is None: # still running
        process.kill()

with ExitStack() as stack: # for proper cleanup
    processes = []
    for command in [['someprocess'], ['anotherprocess']]:  # start child processes
        processes.append(stack.enter_context(Popen(command, stdout=PIPE)))
        stack.callback(kill, processes[-1]) # kill on someprogram exit

    fds = [p.stdout.fileno() for p in processes]
    someprogram = stack.enter_context(
        Popen(['someprogram'] + ['/dev/fd/%d' % fd for fd in fds], pass_fds=fds))
    for p in processes: # close pipes in the parent
        p.stdout.close()
# exit stack: wait for processes
if someprogram.returncode != 0: # errors shouldn't go unnoticed
   raise CalledProcessError(someprogram.returncode, someprogram.args)

rawDescription: Multiple pipes in subprocess

description: Multiple pipes in subprocess


code: import codecs

rawDescription: IronPython stack trace cites a line in "except" block


code:
data = np.copy(a['data'])
data

rawDescription: improve speed when reading a binary file

description: Improve speed when reading a binary file


code: d.update((k, frozenset(v)) for k, v in d.iteritems())

rawDescription: Convert sets to frozensets as values of a dictionary


code:
import pandas as pd

df1 = pd.DataFrame({'Column1': [1,2,3,4,5],
    'Column2': ['a','b','c','d','e'],
    'Column3': ['r','u','k','j','f']})

df2 = pd.DataFrame({'Column1': [1,1,1,2,2,3,3], 'ColumnB': ['a','d','e','r','w','y','h']})

dfs = pd.DataFrame({})
for name, group in df2.groupby('Column1'):
    buffer_df = pd.DataFrame({'Column1': group['Column1'][:1]})
    i = 0
    for index, value in group['ColumnB'].iteritems():
        i += 1
        string = 'Column_' + str(i)
        buffer_df[string] = value

    dfs = dfs.append(buffer_df)

result = pd.merge(df1, dfs, how='left', on='Column1')
print(result)

rawDescription: Comparing two dataframes of different length row by row and adding columns for each row with equal value


code: df.groupby(['GridCode', 'Key'])

rawDescription: pandas - groupby by partial string

description: Pandas - groupby by partial string


code: [(k,len(list(l))) for k, l in groupby(s)]

rawDescription: Splitting a string into consecutive counts?


code:
for character in x:
    print(test[character])

rawDescription: How do I make a 'while' loop print the results of the loop's conditional into a single line(concatenated)?

description: Make a 'while' loop print the results of the loop's conditional into a single line(concatenated)


code: reduce(lambda df1, df2: pd.merge(df1, df2, how='outer'), dfs)

rawDescription: pandas: merge several dataframes

description: Pandas: merge several dataframes


code:
from abc import ABCMeta, abstractproperty

class ValidateProperty:
    def __init__(inst, exception, arg_type, valid):
        # called on the @ValidateProperty(...) line
        #
        # save the exception to raise, the expected argument type, and
        # the validator code for later use
        inst.exception = exception
        inst.arg_type = arg_type
        inst.validator = valid
    def __call__(inst, func):
        # called after the def has finished, but before it is stored
        #
        # func is the def'd function, save it for later to be called
        # after validating the argument
        def check_accepts(self, value):
            if not inst.validator(value):
                raise inst.exception('value %s is not valid' % value)
            func(self, value)
        return check_accepts

class AbstractTestClass(metaclass=ABCMeta):
    @abstractproperty
    def luminance(self):
        # abstract property
        return
    @luminance.setter
    @ValidateProperty(Exception, int, lambda x: 0 <= x <= 15)
    def luminance(self, value):
        # abstract property with validator
        return

class TestClass(AbstractTestClass):
    # concrete class
    val = 7
    @property
    def luminance(self):
        # concrete property
        return self.val
    @luminance.setter
    def luminance(self, value):
        # concrete property setter
        # call base class first to activate the validator
        AbstractTestClass.__dict__['luminance'].__set__(self, value)
        self.val = value

tc = TestClass()
print(tc.luminance)
tc.luminance = 10
print(tc.luminance)
tc.luminance = 25
print(tc.luminance)

rawDescription: How to provide value validation at abstract class level?

description: Provide value validation at abstract class level


code: [chr(ord('A') + i) for i in range(int(math.ceil(df.scale / 3000)))]

rawDescription: Change count into an array of letters, python


code:
from flask.ext.restful import Resource

class ApiPage(Resource):
    def __init__(self, bar):
        self.bar = bar

    def get(self):
        serialized = str(my_bar)
        return serialized

rawDescription: Flask Restful add resource parameters

description: Flask Restful add resource parameters


code: [(k, v.items()) for k,v in d.items()]

rawDescription: Merging part of tuples in Python

description: Merging part of tuples in Python


code:
with open("filename.txt", "r+b") as f:
     f.write(chr(10) + chr(20) + chr(30) + chr(40))

rawDescription: Simple edit to a binary file in python

description: Simple edit to a binary file in python


code: MultiLabelBinarizer().fit_transform([["A", "B"], ["C", "D"]])

rawDescription: Forming a sparse feature matrix data frame in Pandas


code:
from pprint import pprint
pipeline = [{'$group': {'_id': '$city', 'cust_ids': {'$addToSet': '$cust_id'}, 'count': {'$sum': 1}}}]
pprint(list(collection.aggregate(pipeline)))

rawDescription: Passing variables onto a MongoDB Query

description: Passing variables onto a MongoDB Query


code:
colorlist = np.rec.fromarrays([[row[0]  for row in query],
                               [row[1:] for row in query]], desc)

rawDescription: Tuple Numpy Data Type

description: Tuple Numpy Data Type


rawDescription: Selenium: how to get the entire html as a string?


code:
from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")

driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get('https://google.com')

rawDescription: Python/Selenium incognito/private mode

description: Python/Selenium incognito/private mode


code:
from datetime import datetime, date, time
a = datetime.combine(date.today(), time(0, 6, 58))
b = datetime.combine(date.today(), time(0, 7, 12))

rawDescription: A way to convert numbers to seconds

description: A way to convert numbers to seconds


code:
class Final(type):
    def __new__(cls, name, bases, classdict):
        for b in bases:
            if isinstance(b, Final):
                raise TypeError("type '{0}' is not an acceptable base type".format(b.__name__))
        return type.__new__(cls, name, bases, dict(classdict))

class Foo:
    __metaclass__ = Final

class Bar(Foo):
    pass

rawDescription: How does python prevent a class from being subclassed?


code:
# your original code minus data and imports
fig = plt.figure()
gs1 = gridspec.GridSpec(3, 3)
gs1.update(left=0.1, right=0.9, bottom=0.5, hspace=0.0)
ax0a = plt.subplot(gs1[0, :])
ax0b = plt.subplot(gs1[1, :])
ax0c = plt.subplot(gs1[2, :])
ax0a.set_xticklabels([])
ax0b.set_xticklabels([])
ax0a.plot(x,y)
nbins = len(ax0a.get_xticklabels())
ax0a.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0b.plot(x,y)
ax0b.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))
ax0c.plot(x,y)
ax0c.yaxis.set_major_locator(MaxNLocator(nbins=nbins, prune='upper'))

# play around with location and font of offset text here
ax0a.get_yaxis().get_offset_text().set_x(-0.075)
ax0a.get_yaxis().get_offset_text().set_size(10)
ax0b.get_yaxis().get_offset_text().set_x(-0.075)
ax0b.get_yaxis().get_offset_text().set_size(10)
ax0c.get_yaxis().get_offset_text().set_x(-0.075)
ax0c.get_yaxis().get_offset_text().set_size(10)
plt.show()

rawDescription: Matplotlib subplot y-axis scale overlaps with plot above


code:
wall1 = Wall(c.WHITE, 40, 40, 20, 80)
wall2 = Wall(color=c.BLUE, wall=wall1)

rawDescription: Python - how to copy object (so it would not reference old object) without using deepcopy?


code: [('person 1', 'name1'), ('person 2', 'name2'), ('person 3', 'name3'), ('person N', 'nameN')]

rawDescription: Dynamically Naming Groups in Python Regular Expressions


code: set(i[:3] for i in dems)

rawDescription: check if a string is in a list, and if not append it to that list

description: Check if a string is in a list, and if not append it to that list


code:
def simple(value, target):
    if value < target:
        value = min(value + 1, target)
    else:
        value = max(value - 1, target)
    return value

rawDescription: Ramping Joystick values over time

description: Ramping Joystick values over time


code:
import numpy as np
import pandas as pd
import scipy.sparse as sparse

df = pd.DataFrame(np.arange(1,10).reshape(3,3))
arr = sparse.coo_matrix(([1,1,1], ([0,1,2], [1,2,0])), shape=(3,3))
df['newcol'] = arr.toarray().tolist()
print(df)

rawDescription: Add numpy array as column to Pandas data frame


code:
for variable, value in vars(classobj).items():
    if variable[:1] == '_':
        recordString += value

rawDescription: Get the value of variables in dir(class)


code: output = [list(group) for key,group in groupby(sorted(words,key=sorted),sorted)]

rawDescription: Finding and grouping anagrams by Python

description: Finding and grouping anagrams by Python


rawDescription: Count all characters, then print how many of which characters


code: r = NumericProperty(0)

rawDescription: How do I change the color of my widget in Kivy at run time?

description: Change the color of my widget in Kivy at run time


code:
(u'Bao Bao Issey Miyake', [u'SILVER'])
(u'Nancy Gonzalez', [u'BLACK', u'BLUSH'])
(u'Bao Bao Issey Miyake', [u'GUNMETAL'])
...
(u'Saint Laurent', [u'WINE'])
(u'Fendi', [u'BLUE', u'FUCHSIA', u'WATER GREEN'])
(u'Prada', [u'LAGO'])

rawDescription: web scraping using python beautifulsoup getting values after equal sign

description: Web scraping using python beautifulsoup getting values after equal sign


code:
l = Location(...)
l.save()

rawDescription: Set parent of model during creation

description: Set parent of model during creation


rawDescription: writing to CSV in python

description: Writing to CSV in python


code: out.write('{} -> {} [ label="{}" ];\n'.format(start,end,weight))

rawDescription: How can python write a dot file for GraphViz asking for some edges to be colored red?


code:
from django.conf import settings
from django.utils.log import configure_logging

configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)

rawDescription: Using Django log

description: Using Django log


rawDescription: Python simple number comparison

description: Python simple number comparison


code: lines = [line.strip() for line in open(filename)]

rawDescription: what is a quick way to import a text file in python?

description: What is a quick way to import a text file in python


code: offset = current_time.utcoffset()

rawDescription: Get new datetime object according to timezone difference


code:
d3 = {}
for k1,v1 in d1.iteritems():
    for k2,v2 in d2.iteritems():
        d3[k1+'_'+k2] = v1+v2

rawDescription: Combining keys and values of two different dicts


rawDescription: Writing to a csv file in a particular format


rawDescription: Not plotting 'zero' in matplotlib or change zero to None [Python]


code:
hor = self.canvas.create_line(hstart, hend, tags=("crosshair",))
ver = self.canvas.create_line(vstart, vend, tags=("crosshair",))
...
self.canvas.delete("crosshair")

rawDescription: How do I delete and create new lines simultaneously

description: Delete and create new lines simultaneously


code:
from pyspark.sql import functions as F
df.agg(*[F.min(col) for col in df.columns] + [F.avg(col) for col in df.columns]).collect()

rawDescription: Spark reduceByKey on several different values


code:
class UtcFormatter(logging.Formatter):
    converter = time.gmtime

rawDescription: django: keep UTC time in logging while changing the TIME_ZONE settings

description: Django: keep UTC time in logging while changing the TIME_ZONE settings


code:
from collections import defaultdict
d = {'foo': 123, 'bar': 456}
d['baz']
d = defaultdict(lambda: -1, d)
d['baz']

rawDescription: How to set default value to all keys of a dict object in python?

description: Set default value to all keys of a dict object in python


code:
i = 0
while i < 5:
    print(i)
    if condition:
        i=i-1
    i += 1

rawDescription: Moving back an iteration in a for loop

description: Moving back an iteration in a for loop


code:
from jinja2 import Environment
env = Environment()
env.globals['foo'] = lambda: "foobar"
env.from_string('{% macro bar() %}{{ foo() }}{% endmacro %}{{ bar() }}').render()

rawDescription: Accessing global attributes from inside a macro in Jinja2


code:
lhash = hash(litem)
if lhash in lfunctionHashes:
    lmarshalledClosureValues.append([lhash, None])
else:
    lfunctionHashes.add(lhash)
    lmarshalledClosureValues.append([lhash, marshal.dumps(litem.func_code), MarshalClosureValues(litem.func_closure, lfullIndex), litem.__module__])

rawDescription: Can I restore a function whose closure contains cycles in Python?

description: Restore a function whose closure contains cycles in Python


code: success_url = reverse('base-form')

rawDescription: Django, redirected url is not proper

description: Django, redirected url is not proper


code: ts = ts.ix[ts.index.map(BDay())]

rawDescription: Mask timeseries outside business hours

description: Mask timeseries outside business hours


rawDescription: Parsing Lines in Python: Use RE or Not?

description: Parsing Lines in Python: Use RE or Not


code:
import re

counter = None

def datacnv( st ):
    global counter
    return "<rvn=\""+ next(counter) +"\">" + st + "</rvn>\n"

def rowcnv( st ):
    global counter

    counter = iter("".join([str(x) for x in range(1,10)]))

    st = re.sub("(?i)<R>(.*?)</R>", lambda m: datacnv(m.group(1)), st)

    return "<row>\n" + st + "</row>\n"

def tblcnv( st, val ):
    Tcolspec = ''
    Endval = int(val) + 1
    for i in range(1, Endval):
        l = str(i)
        Tcolspec += "<colspec col='" + l + "' colwidth=''/>\n"
    Theader = re.sub(r"(?i)
","\n
\n" + Tcolspec + "\n", st)

    Theader = re.sub("(?i)<ROW>(.*?)</ROW>", lambda m: rowcnv(m.group(1)), Theader)

    return Theader

in_data = open("in.txt", "r")
out_data = open("out.txt", "w")
Rdata = in_data.read().lower()
in_data.close()
Rval = Rdata.replace("\n", " ")

Rval = re.sub("(?i)(<TABLE.*?cols=\"(\d+).*?
)", lambda m: tblcnv(m.group(1), m.group(2)), Rval)
out_data.write(Rval)

out_data.close()

rawDescription: looping over multiple patterns in python regular expressions

description: Looping over multiple patterns in python regular expressions


code:
z,z,z,c,x,s,e,f,d,s,f     #3 z's, line index 2 (3rd line)
e,p,l,l,k,k,z,z,q,e,r     #2 z's, line index 4 (5th line)
a,e,f,g,h,d,e,g,z,g,z     #2 z's, line index 1 (2nd line)
w,e,r,f,g,s,v,h,d,b,z     #1 z, line index 6 (7th line)
t,r,y,e,u,i,o,p,d,f,j     #0 z's, line index 7 (8th line)

rawDescription: python: How to compare values in a list by row?

description: Python: How to compare values in a list by row


rawDescription: Does a Celery worker check queues in a specific order?


code: a, b, c, d = [f(i) for i in xrange(1, 5)]

rawDescription: How to pass an integer series as argument to a python function?

description: Pass an integer series as argument to a python function


code:
import re

nums = [-2.802479940, 3.796137791, -0.012, 0.035, 0, 100, -200]
decimal_digits = 10
for n in nums:
    num = ('{:.'+str(decimal_digits-1)+'E}').format(n)

    # increment exponent
    exp_search = re.search(r'E(\+|-)\d+', num).group()
    sign = exp_search[1]
    exp = int(exp_search[1:])
    new_exp = exp + 1
    exponent = 'E'+sign+'{:02}'.format(abs(new_exp))

    # move decimal place over
    sig_search = re.search(r'-?\d\.\d+', num).group()
    stripped = sig_search.replace('.', '')
    if stripped[0] == '-':
        significand = stripped.replace('-', '-.')
    else:
        significand = '0.' + stripped

    # glue significand and exponent back together
    new_num = significand + exponent
    print('{:>20}{:>20}'.format(num, new_num))
    assert float(num) == float(new_num)

rawDescription: Converting decimal to specific scientific notation format


code:
b_arr_np = np.asarray([b_arr], dtype=np.uint8)

b_arr_np
Out[319]: array([[202,   0,   0,   0]], dtype=uint8)

b_arr_np.tostring()

Out[320]: '\xca\x00\x00\x00'

rawDescription: numpy trimming trailing zeros in byte strings

description: Numpy trimming trailing zeros in byte strings


rawDescription: Sort Versions in Python

description: Sort Versions in Python


rawDescription: Python: re.sub pattern and replacement wildcard


code:
def getGuessedWord(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: string, comprised of letters and underscores that represents
      what letters in secretWord have been guessed so far.
    '''

    i=0
    gameWord=''
    while i in range(len(secretWord)):
        gameWord += ' '
        if(secretWord[i] in lettersGuessed):
            gameWord += secretWord[i]
        else:
            gameWord += '_'
        i += 1
    return gameWord

rawDescription: Code to edit a string with either '_' or a character hangs


code: mylist.sort(key=lambda sl: (sl.count('YES') + sl.count('MAYBE'), -sl.count('NO')), reverse=True)

rawDescription: Sorting a list of lists by item frequency in Python 2.3


code:
table['percent_false']  = table[False] / table.All * 100
table['percent_true']  = table[True] / table.All * 100

rawDescription: Pandas Column Calculations in PivotTable


code: A[['family','id','mum','dad']+list(B)]

rawDescription: Numpy: order array as other array - by string order


code:
import sys
from colorama import Fore

special_words = ['bannanas', 'eggs', 'ham']
my_text = ''
while True:
    c = getch()
    if ord(c) == 13:  # newline, stop
        break
    my_text += c

    sys.stdout.write("\r")  # move to the line beginning
    for word in my_text.split():
        if word in special_words:
            sys.stdout.write(Fore.RED + word + " ")
        else:
            sys.stdout.write(Fore.RESET + word + " ")
    sys.stdout.flush()

# reset the color when done
print(Fore.RESET)

rawDescription: how to change some words color real time while accepting input in python

description: Change some words color real time while accepting input in python


code:
import os
import glob
from collections import defaultdict
import pandas as pd

def read_files(filelist):
    # `dfs` - will contain a list of DFs
    # that will be concatenated later on
    dfs = []
    for fn in filelist:
        # parse column name from filename
        col = os.path.splitext(os.path.split(fn)[-1])[0]
        # read individual CSV (as data blocks from defaultdict) into temp DF
        # and add this temporary DF into `dfs` list
        dfs.append(pd.read_csv(
                        fn,
                        parse_dates=[0],
                        header=None,
                        index_col='date',
                        names=['date', col]
                   )
        )
    # return concatenated horizontally (axis=1) DF
    return pd.concat(dfs, axis=1)

def main():
    data_files_mask = r'D:\temp\.data\36827502\*.csv'
    df = read_files(glob.glob(data_files_mask))
    print(df)

if __name__ == '__main__':
    main()

rawDescription: Pandas: how to merge horizontally multiple CSV (key,value) files and name `value` columns in the resulting DF using filenames


rawDescription: Replacing two elements of a list in place with a function [python 3]


code:
data[0]
data[0]['Name']

rawDescription: Data type to save a table

description: Data type to save a table


code:
class BaseMixin(object):
    @classmethod
    def create(cls, **kw):
        obj = cls(**kw)
        db.session.add(obj)
        db.session.commit()

rawDescription: How to add a custom function/method in sqlalchemy model to do CRUD operations?

description: Add a custom function/method in sqlalchemy model to do CRUD operations


code:
from random import choice
picked = [choice(randomnumbers) for _ in range(5)]

rawDescription: how to generate 5 numbers from a list?

description: Generate 5 numbers from a list


code:
@patch("foo.load",side_effects=["a","b","c"])
@patch("bar.check",return_value=True)
def test_mytest(mock_check,mock_load):
    take_action()
    assert mock_load.called
    assert mock_check.called

rawDescription: patch multiple methods from different modules (using Python mock)

description: Patch multiple methods from different modules (using Python mock)


code:
'AAA'.lower() == 'aaa'
True
'Aa'.lower() == 'aA'.lower()
True

rawDescription: Python dict.keys to list results in duplicates


code:
class BaseTaxes(db.Document):
    name = db.StringField()
    value = db.IntegerField()

    meta = {'allow_inheritance': True}

    def apply_tax(self, value):
        return value*(1+self.value)

rawDescription: How can I use multiple models to point to the same collection?

description: Use multiple models to point to the same collection


code:
class Item(models.Model):
    group = models.ForeignKey(Group)

rawDescription: Iterate Items in Their Specific Groups in Django


code:
print(const.x)
# bar

rawDescription: python binding of variable to particular expression

description: Python binding of variable to particular expression


code:
class UpdateTimeView(APIView):

    def get(self, request, format=None):
        latest_publish = Publish.objects.latest('created_time')
        latest_meeting = Meeting.objects.latest('created_time')
        latest_training = Training.objects.latest('created_time')
        latest_exhibiting = Exhibiting.objects.latest('created_time')

        return Response({
            "publish_updatetime": latest_publish.created_time,
            "meeting_updatetime": latest_meeting.created_time,
            "training_updatetime": latest_training.created_time,
            "exhibiting_updatetime": latest_exhibiting.created_time,
        })

rawDescription: How can I register a single view (not a viewset) on my router?

description: Register a single view (not a viewset) on my router


code: average = sum(len(word.strip(string.punctuation)) for sentence in text for word in sentence.split()) / sum(len(sentence.split()) for sentence in text)

rawDescription: average number of characters per word in a list

description: Average number of characters per word in a list


code: [[words for words in some_list if kw in words.split()] for kw in search_strings]

rawDescription: Searching list using keywords from other list?


code:
[['no tag', u'peanut butter1'],
 [u'ul', u'peanut butter2'],
 [u'li', u'2.0 to 6.0 mg of 17\u03b2-estradiol and'],
 [u'li', u'0.020 mg of ethinylestradiol;'],
 [u'li', u'0.25 to 0.30 mg of drospirenone and'],
 [u'br', u'peanut butter3'],
 ['no tag', u'peanut butter4']]

rawDescription: Use Beautifulsoup to turn <ul> <li> and unbounded items into a list


code: print("The volume of the cylinder is {:.5f}cm\u00b3".format(volume))

rawDescription: Two Decimals in Python?

description: Two Decimals in Python


code:
import numpy as np
a = np.arange(20).reshape((10,-1))
a

rawDescription: What is the best way to create a moving window in 2D array ond order pixels


code: foo.reshape(-1, 2)

rawDescription: Reshaping an array to 2-D by specifying only the column size


code:
import numpy
numpy.std(zip(*myList)[i])

rawDescription: finding standard deviation down a column of data in python

description: Finding standard deviation down a column of data in python


rawDescription: Using 'If' to run the loop again?

description: Using 'If' to run the loop again


rawDescription: How do I properly use the sub function of regular expression in Python?

description: Properly use the sub function of regular expression in Python


code: foo: {{ request.GET.foo }}

rawDescription: Confirming generic FormView POST success back to user with POST data


rawDescription: Compare the keys of two dictionaries present inside two lists and subtracting their respective values


code:
lines={}
for i in (2,5):
   lines[i]=linecache.getline('/tmp/lines.txt', i).strip()

lines

rawDescription: numpy - Python - Selectively import parts of the .txt file

description: Numpy - Python - Selectively import parts of the .txt file


code:
high = int(input('Enter the high integer for the range: ')) # Enter the high integer
low = int(input('Enter the low integer for the range: '))   # Enter the lower integer
num = int(input('Enter the integer for the multiples: '))   # Enter integer to find multiples

multiples = []
for value in range(high, low-1, -1):
    if value % num == 0:
        multiples.append(value)

rawDescription: Count even and odd numbers and the totals (python)


rawDescription: Django filter ManyToMany field

description: Django filter ManyToMany field


code:
import ConfigParser
ConfigParser.ConfigParser()
c.read('config.ini')
c.get('SomeSection', 'SomeOption')
c.items('SomeSection')

rawDescription: ConfigParser - Same Option Loop

description: ConfigParser - Same Option Loop


code:
dt1=np.dtype(type)
dt1

rawDescription: Placing a numpy record array inside another record array python


code:
class Employee(models.Model):
    """(Workers, Staff, etc)"""
    name = models.CharField(blank=True, null=True, max_length=100)

    def save(self, *args, **kwargs):
        self.full_clean() # performs regular validation then clean()
        super(Employee, self).save(*args, **kwargs)

    def clean(self):
        """
        Custom validation (read docs)
        PS: why do you have null=True on charfield?
        we could avoid the check for name
        """
        if self.name:
            self.name = self.name.strip()

class EmployeeForm(ModelForm):
    class Meta:
        model = Employee

    def clean_name(self):
        """
        If somebody enters into this form ' hello ',
        the extra whitespace will be stripped.
        """
        return self.cleaned_data.get('name', '').strip()

rawDescription: (Django) Trim whitespaces from charField


rawDescription: Facing issue while rounidng up a value in python


code:
grouped = df.groupby(['Year', 'ageGrp'], as_index=True)
final = grouped.agg(np.sum).dropna()
print(final)

rawDescription: Python pandas.cut

description: Python pandas.cut


code: timeit.timeit('lfactor(number)', 'from __main__ import lfactor, number')

rawDescription: What is the logic behind the <statement, setup> design for timeit?


rawDescription: pandas to_csv and then read_csv results to numpy.datetime64 messed up due to utc

description: Pandas to_csv and then read_csv results to numpy.datetime64 messed up due to utc


code: d = {str(row.getValue("Column1")): (str(row.getValue("Column2")), str(row.getValue("Column3")), str(row.getValue("Column4"))) for i in arcpy.SearchCursor(xls,"[Column1] = 'Lake_Huron'")}

rawDescription: Adding Multiple Values to Single Key in Python Dictionary Comprehension


code:
def bullsandcows(given, number):
    given, number = map(str, (given, number))
    for g, n in zip(given, number):
        if g == n:
            yield (g, None)
        elif g in number:
            yield (None, g)

rawDescription: Reduce list of tuples

description: Reduce list of tuples


code:
def addrs(location):
    gq = GeocodeQuery("zh-tw", "tw")
    gq.get_geocode(location)
    return pd.Series([gq.get_lat(), gq.get_lng()])

df[['lat','lng']] = df['location'].apply(addrs)
df.to_csv('./birdsIwant.csv')

rawDescription: How can I use python pandas add new columns in specific index

description: Use python pandas add new columns in specific index


code:
for row in allRows:
    results.append([data.get_text() for data in row.find_all(['th', 'td'])])

rawDescription: The second row and third row should be a single row


code:
import sys
import os

original_path = sys.path[:]
def reset():
    # Teardown
    sys.path = original_path[:]
    if "resource" in sys.modules:
        del sys.modules["resource"]

import resource
print("resource exists")

reset()

sys.path.insert(0, os.path.join(os.getcwd(), "mocked_modules"))

try:
    import resource
except ImportError:
    print("resource does not exist")

reset()
import resource
print("resource exists")

rawDescription: Is it possible to simulate the unavailability of modules in Python tests?


code: data = re.split(',', line)

rawDescription: How can I make this search faster in python?

description: Make this search faster in python


rawDescription: webpage data extraction automation

description: Webpage data extraction automation


rawDescription: parsing a url in python with changing part in it

description: Parsing a url in python with changing part in it


code: weights = {'east':1,'west':2,'south':3}

rawDescription: apply a function to a pandas Dataframe whose retuned value is based on other rows

description: Apply a function to a pandas Dataframe whose retuned value is based on other rows


code:
hash_counter = Counter(x[3] for x in self.files_for_json)
for (file,inode,image,hash) in self.files_for_json:
    if hash_counter[hash]>1:
        # duplicated hash
        ...

rawDescription: Remove BOTH items from list, if one value appears multiple times (Python)


code: [False] * 10

rawDescription: Initialize list with same bool value

description: Initialize list with same bool value


code: import weakref

rawDescription: Can I iterate over a class in Python?

description: Iterate over a class in Python


code:
proc = subprocess.Popen(['fst-mor', 'NOUN.A'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)
output, err = proc.communicate('INPUT_A_STRING_HERE')

rawDescription: Calling multi-level commands/programs from python


rawDescription: How to convert mongodb document to binary in Python?

description: Convert mongodb document to binary in Python


code:
import datetime
somevalue = '31/12'
day, month = map(int, somevalue.split('/'))
datetime.date.today().replace(month=month, day=day)
someothervalue = '1/1'
day, month = map(int, someothervalue.split('/'))
datetime.date.today().replace(month=month, day=day)

rawDescription: Convert strange date format to standard date format


code:
xarr.dtype.names=['name1','name2']
xarr

rawDescription: convert list of tuples to structured numpy array

description: Convert list of tuples to structured numpy array


code:
import unittest
res = type("TR", (unittest.TestResult, ), dict(addError=lambda self, test, err: setattr(self, '_last_err', err)))()
unittest.FunctionTestCase(lambda: [][0])(res)
res._last_err

rawDescription: Raising and catching exceptions in an expression in Python 3


code:
import numpy as np
from scipy.stats import logistic

def func2(y, X, thresholds):
    ll = 0.0

    for row in zip(y, X):
        if row[0] == 0:
            ll += logistic.logcdf(thresholds[0] - row[1])
        elif row[0] == len(thresholds):
            ll += logistic.logcdf(row[1] - thresholds[-1])
        else:
            ll += np.log(
                np.maximum(
                    10 ** -5,
                    logistic.cdf(thresholds[row[0]] - row[1]) -
                     logistic.cdf(thresholds[row[0] - 1] - row[1])
                )
            )
    return ll

y = np.array([0, 1, 2])
X = [2, 2, 2]
thresholds = np.array([2, 3])
print(func2(y, X, thresholds))

rawDescription: How to speed up my numpy loop using numpy.where()

description: Speed up my numpy loop using numpy.where()


code:
({'z':z} for x in ({'y':y} for y in ({'x':x} for x in 'abc')))
              #SAME AS#
for x in 'abc':
    x2 = {'x':x}
    y2 = {'y':x2}
    z2 = {'z':y2}
    yield z2
              #SAME AS#
def f(x):
    return {'z':{'y':{'x':x}}}
return [f(x) for x in 'abc']     # or map(f,'abc')

rawDescription: What's wrong with this nested list comprehension?


code: df2.xs('b', axis=1, level=1)

rawDescription: How can I generalize my pandas data grouping to more than 3 dimensions?

description: Generalize my pandas data grouping to more than 3 dimensions


code:
import uuid
uuid.uuid1()

rawDescription: When using Parallel Python, is there any way to tell on which machine the job has run?


code:
import os
os.write(sys.stdout.fileno(), clear_screen_seq)

rawDescription: Python - Clearing the terminal screen more elegantly


code:
from calendar import month_abbr

month_to_index = {month.upper(): i for i, month in enumerate(month_abbr[1:])}

rawDescription: list of dictionary Sorting in python

description: List of dictionary Sorting in python


rawDescription: How to remove lines in a Matplotlib plot

description: Remove lines in a Matplotlib plot


rawDescription: How to delete elements from list of tuples?

description: Delete elements from list of tuples


code:
import pandas as pd
import itertools

df = pd.read_csv("avito_trend.csv",
                      parse_dates=[2])

def f(df):
    #print (sorted(df['address'].unique()))
    dfs = []
    for x in [list(x) for x in itertools.combinations(sorted(df['address'].unique()), 2)]:
        c1 = df.loc[df['address'].isin([x[0]]), 'ID']
        c2 = df.loc[df['address'].isin([x[1]]), 'ID']
        c = pd.Series(list(set(c1).intersection(set(c2))))
        dfs.append(pd.DataFrame({'common users':len(c), 'pair of websites':' and '.join(x)}, index=[0]))
    return pd.concat(dfs)

common_users = df.groupby([df['used_at'].dt.year]).apply(f).reset_index(drop=True, level=1).reset_index()
#print common_users

graph_by_common_users = common_users.pivot(index='pair of websites', columns='used_at', values='common users')

#change order of columns
graph_by_common_users = graph_by_common_users[[2015,2014]]
graph_by_common_users = graph_by_common_users.sort_values(2014, ascending=False)
#print graph_by_common_users

rawDescription: Sort strings in column and print graph

description: Sort strings in column and print graph


code:
angles = np.linspace(-np.pi, np.pi, 10)
angles

rawDescription: Numpy converting range of angles from (-Pi, Pi) to (0, 2*Pi)


code:
title1 = Paragraph("Title", myStyle)
title2 = Paragraph("Page", myStyle)
cell = [title1, title2]               # put this in a single cell of your table

rawDescription: How to insert a carriage return in a ReportLab paragraph?

description: Insert a carriage return in a ReportLab paragraph


rawDescription: Splitting string in Python starting from the first numeric character


code: MyFirstDict, MySecondDict = pickle.load(open("two_dict_pkl_file", "rb"))

rawDescription: Working with two dictionaries in one pickle file


rawDescription: Python : Get many list from a list

description: Python : Get many list from a list


rawDescription: converting include file in C to python

description: Converting include file in C to python


rawDescription: CSV string to decimal in python list

description: CSV string to decimal in python list


code: substring1 or substring2 or substring3

rawDescription: If substring in string, when substring has multiple values


code:
df_ = pd.DataFrame(index=index, columns=columns)
df_ = df_.fillna(0) # with 0s rather than NaNs

rawDescription: Creating an empty Pandas DataFrame, then filling it?


rawDescription: Pandas - turning a DF column into an Index but without duplication


rawDescription: iGraph Python, convert edge list to tuple and add.edges

description: IGraph Python, convert edge list to tuple and add.edges


code:
import ast
result = ast.literal_eval(''.join(["{", s.replace(" ", ", "), "}"]))

rawDescription: Whether eval() better than self-analysis?


code:
json_data['BrowserInfo'] = {}
for line in BrowserInfo.splitlines():
    k,v = line.split(': ', 1)
    json_data['BrowserInfo'][k] = v

rawDescription: Adding new key-value pairs to JSON files using Python


code:
idx *= -1
idx += 1
idx

rawDescription: MultiIndexing in pandas based on column conditions


code:
list_dict = {}
for item in alist:
    list_dict[item] = []

rawDescription: Create empty lists with name of attributes of other lists


code:
from bs4 import BeautifulSoup
from pprint import pprint
import requests
response = requests.get('http://www.homedepot.com/p/Husky-41-in-16-Drawer-Tool-Chest-and-Cabinet-Set-HOTC4016B1QES/205080371')
soup = BeautifulSoup(response.content)
reviews = []
for rating in soup.find_all(itemprop='review'):
    data = {}
    for item in rating.find_all(itemprop=True):
        data[item['itemprop']] = item.attrs.get('content') or item.get_text()
    reviews.append(data)

reviews.sort(key=lambda i: i['datePublished'], reverse=True)
pprint(reviews)

rawDescription: beautifulsoup with internal javascript

description: Beautifulsoup with internal javascript


code:
def bar(x):
    return x * x

def foo(y):
    return bar(y)

view.apply(foo, 5) # NameError on bar
view.push(dict(bar=bar)) # send bar
view.apply(foo, 5) # 25

rawDescription: Best way to pass local variables to ipyparallel cluster

description: Pass local variables to ipyparallel cluster


code:
'Asia/Shanghai' in tzones['CST']
'US/Central' in tzones['CST']

rawDescription: Python: All possible Timezone Abbreviations for given Timezone Name (and vise versa)


rawDescription: Python strip a string from config file

description: Python strip a string from config file


code:
import math

sorted(some_list, key=lambda f: math.modf(f))

rawDescription: How can I sort a list by the decimals only?

description: Sort a list by the decimals only


code:
finfo32 = np.finfo(np.float32)
finfo64 = np.finfo(np.float64)

finfo32.resolution = 1e-6
finfo64.resolution = 1e-15

rawDescription: Python floating-point precision format specifier


code:
from genshi.template import MarkupTemplate

tpl_xml = '''
<doc xmlns:py="http://genshi.edgewall.org/">
<p py:for="i in data">${i}</p>
</doc>
'''

tpl = MarkupTemplate(tpl_xml)
stream = tpl.generate(data=xrange(10000000))

with open('output.xml', 'w') as f:
    stream.render(out=f)

rawDescription: Generating very large XML files in Python?


code:
import numpy as np
a = np.zeros((6,6), dtype=np.int)
s = ndimage.generate_binary_structure(2,2) # Binary structure
ndimage.convolve(a,s)

rawDescription: Iterate with binary structure over numpy array to get cell sums


code:
from application.coders.foo import foo_coder_fucntion
from application.coders.bar import bar_coder_function

rawDescription: Python importing (in Flask)

description: Python importing (in Flask)


code:
import pwd

try:
    pwd.getpwnam('someusr')
except KeyError:
    print('User someusr does not exist.')

rawDescription: Tell if a given login exists in Linux using Python


code: time[times] = [result[times], os.stat(result[times]).st_mtime]

rawDescription: Some trouble when sorting a dictionary

description: Some trouble when sorting a dictionary


code:
xml = parse("path/fileName.xml")
nameList = xml.findall("sub/name") #find all objects in <name> brackets
for i in nameList:
    i.text #convert item in the list to a readable string
    #do comparison here

rawDescription: How can I force ElementTree to write to disk every change?

description: Force ElementTree to write to disk every change


code:
itElement = iter(elementList)
a = next(itElement)
for b in itElement:
    elementSize1 = b - a
    percentOfPathElement.append(elementSize1/pathLength)
    a = b

rawDescription: Iterate through list and going back one item


rawDescription: What are some good ways to set a path in a Multi-OS supported Python script


code: sum_val = np.einsum('ijk->k',myArray) # k represents axis = 2

rawDescription: perform 2-d operation on each layer of a 3-d stack

description: Perform 2-d operation on each layer of a 3-d stack


code: from PyQt5.QtCore import *

rawDescription: Animating a rectangle in PyQT5: Why is QtCore not defined?


code:
from django.template.defaulttags import register
...
@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

def index(request):
    years = Expense.objects.dates('date','year')
    expenses = Expense.objects.all().values()
    amounts = {}

    for expense in expenses:
        year = expense['date'].year
        if year in amounts:
            amounts[year] = amounts[year] + expense['amount']
        else:
            amounts[year] = expense['amount']

    return render(request, 'index.html', {'years': years, 'amounts': amounts})

rawDescription: Django - Get a QuerySet of Months and the amount of expense


code:
l=[i for sub in [dc[k] for k in sorted(dc)] for i in sub if i == '3']
l
l[0]
l[1:] #all the rest...

rawDescription: Dictionary Iteration Python

description: Dictionary Iteration Python


code: sum(n*(n-1)//2 for n in index2count.values())

rawDescription: Deterministic python generator for K disparate M-sized subsets of a set


code:
for k,v in mydict.items():
  print(k, '=', v)

rawDescription: concatenate items in dictionary in python using list comprehension

description: Concatenate items in dictionary in python using list comprehension


code: numpy.where(x == x.min())

rawDescription: How to return all the minimum indices in numpy

description: Return all the minimum indices in numpy


code:
from cStringIO import StringIO
timeit(lambda: truncate(StringIO()))
timeit(lambda: new(StringIO()))
timeit(lambda: truncate(StringIO('abc' * 1000)))
timeit(lambda: new(StringIO('abc' * 1000)))

rawDescription: how do I clear a stringio object?

description: Clear a stringio object


code:
inputtext = "{% url 'funcname'  first =fir    second = sec    third=thi    %}"
[r[:1] + tuple(chain(*re.findall(r'(\w+)\s*=\s*(\w+)', r[1])))
 for r in re.findall(r"{% url '(\w+)'((?:\s+\w+\s*=\s*\w+)*) \s*%}", inputtext)]

rawDescription: python 3.3 RE How to grab possible groups?

description: Python 3.3 RE How to grab possible groups


rawDescription: How do I build a python string from a raw (binary) ctype buffer?

description: Build a python string from a raw (binary) ctype buffer


code: "command1; command2; command3"

rawDescription: Execute Commands Sequentially in Python

description: Execute Commands Sequentially in Python


code:
import sys

class MyModule(object):
    def __init__(self, arg=None):
        if arg == 'is_A':
            self.my_string = 'spam'
        else:
            self.my_string = 'eggs'

        def called_by(arg):  # nested function - no self
            import sys
            # Replace module entry with new instance of MyModule
            sys.modules[__name__] = MyModule(arg)

        self.called_by = called_by

# Replace module entry in sys.modules[__name__] with a default instance of
# MyModule (and create an additional reference to original module so it's not
# deleted)
_ref, sys.modules[__name__] = sys.modules[__name__], MyModule()
del sys  # clean-up namespace (optional)

rawDescription: unify python script keeping specific parts

description: Unify python script keeping specific parts


code:
from pyramid_simpleform import Form
from pyramid_simpleform.renderers import FormRenderer
from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound
from pyramid.url import route_url
from ..forms import RegistrationForm
from ..models import DBSession, User

@view_config(route_name='accounts_register', renderer='/accounts/register.mako',
            request_method='GET')
def register(request):
    """Shows the empty form."""
    form = Form(request, schema=RegistrationForm)
    return {'renderer': FormRenderer(form)}

@view_config(route_name='accounts_register', renderer='/accounts/register.mako',
            request_method='POST')
def register_post(request):
    """Receives the posted form."""
    form = Form(request, schema=RegistrationForm)
    if form.validate():
        user = User(form.data['email'], form.data['password'])
        DBSession.add(user)
        return HTTPFound(route_url('profile', request))
    return {'renderer': FormRenderer(form)}

rawDescription: Determining if a simple_form object has been submitted yet


code:
with some_connection as cursor:
    do_something_with(cursor)

rawDescription: Context manager for Python's MySQLdb

description: Context manager for Python's MySQLdb


code:
byte_array = np.round(255.0 * (pixel_array - minVal) / (maxVal - minVal - 1.0)).astype(np.uint8)
byte_array[pixel_array == paddingVal] = 0

rawDescription: What is an efficient way to scale a int16 numpy array to a int8 numpy array


code:
if menu == "3":
    break

rawDescription: python restart the program after running a method

description: Python restart the program after running a method


code: r = urlopen(url, encodeddata)

rawDescription: Sending javascript form data in python (post request)?


code:
def func(a_dict: dict):
    """Some function to do something to a dictionary."""
    ...

rawDescription: How to add python docstring for dict

description: Add python docstring for dict


rawDescription: Matplotlib quiver scale

description: Matplotlib quiver scale


rawDescription: Python MyHashTable class: search method with linear probing


rawDescription: Compare entries in columns from CSV files and extract matched - python


code:
print(function(1, x = 2))
print(function([2, 3]))

rawDescription: Changing a function implementation in Python


rawDescription: How to create a datetime object with PyYAML

description: Create a datetime object with PyYAML


rawDescription: How to convert PlayJSON list to Scala Map (complex)?

description: Convert PlayJSON list to Scala Map (complex)


rawDescription: Python - Exact Number of Arguments Defined by Variable


code: q = [ [ 0 for j in range(2) ] for i in range(2) ]

rawDescription: Python variable value reset out of loop

description: Python variable value reset out of loop


code:
rng_to_set = lambda rng: set(range(rng[0], rng[1]))

encompasses = lambda a, b: rng_to_set(a).issuperset(rng_to_set(b))
encompassed_by = lambda a, b: rng_to_set(a).issubset(rng_to_set(b))

l = [[1, 1], [2, 8], [3, 16], [4, 16], [5, 25]]
print ( encompasses( l[2] , l[3] ) )  # True
print ( encompassed_by( l[4] , l[2] )) # False
print ( encompassed_by( l[3] , l[2] )) # True

rawDescription: How to approach list of range of values

description: Approach list of range of values


code:
import numpy as np
import itertools as it
from matplotlib import pyplot as plt
import scipy as sp

inputs = list(it.product([0,1,2],[0,1,2]))
i = 0
lattice = range(0,len(inputs))
for pair in inputs:
    lattice[i] = mksite(pair[0], pair[1])
    i = i +1

rawDescription: How to find all neighbors of a given point in a delaunay triangulation using scipy.spatial.Delaunay?

description: Find all neighbors of a given point in a delaunay triangulation using scipy.spatial.Delaunay


code:
for b in [B1, B2, B3]:
    b.bind("<Enter>", SetColor)
    b.bind("<Leave>", ReturnColor)

rawDescription: How to bind multiple widgets with one "bind" in Tkinter?

description: Bind multiple widgets with one "bind" in Tkinter


code:
X, Y, Z, col, shape = zip(*data)

fig = plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')

ax.scatter(X, Y, Z, c=col)

rawDescription: How to change shapes/colours for 3d points with matplot3d

description: Change shapes/colours for 3d points with matplot3d


rawDescription: How to split a poem in python

description: Split a poem in python


code:
def walk(node):
    """
    Recursively yield all descendant nodes in the tree starting at *node*
    (including *node* itself), in no specified order.  This is useful if you
    only want to modify nodes in place and don't care about the context.
    """
    from collections import deque
    todo = deque([node])
    while todo:
        node = todo.popleft()
        todo.extend(iter_child_nodes(node))
        yield node

rawDescription: What type of tree traversal does the ast module use?


code: df

rawDescription: Removing duplicates from Pandas dataFrame with condition for retaining original


rawDescription: scipy.sparse.hstack(([1], [2])) -> "ValueError: blocks must be 2-D". Why?

description: Scipy.sparse.hstack(([1], [2])) -> "ValueError: blocks must be 2-D". Why


code:
def function_sin_xsqaured_plus_3(x):
    return np.sin(x**2+3)

r_sums(function_sin_xsqaured_plus_3,np.linspace(1,4,200),'left')

rawDescription: Passing a math equation as an argument (numpy)


code:
gby_list = []
for key, vals in gby:
    gby_list.append(key, list(vals))

rawDescription: Python groupby behaves strangely

description: Python groupby behaves strangely


code:
from collections import Counter
c = Counter()

c

rawDescription: Comparing two lines in csv file - Python


code:
while verify:
    verify = False
    return Name(SN)

rawDescription: Python27: Checking to see if user input is correct. When select Yes, returns name correctly. When select no, return is 'None'


rawDescription: Why are lists linked in Python in a persistent way?


code: a = UserInput.create_new_user("foo", "whatever", "is", "needed", "here")

rawDescription: Applying the @staticmethod, python3

description: Applying the @staticmethod, python3


code:
import numpy as np
np.searchsorted(intervals, (6, 6.1, 6.2, 6.5, 6.8, 7), side='left')
np.searchsorted(intervals, (6, 6.1, 6.2, 6.5, 6.8, 7), side='right')

rawDescription: Fastest way to check which interval index a value is


code:
values = {entry['ptid']: entry['Value'] for entry in dctData}
items = {}
for item in dctItems:
    items.setdefault(item['item'], []).append(values[item['ptid']])

rawDescription: Link two dictionaries lists and calculate average values


code:
class C(A, B):
    def methodA(self):
        if whatever:
            A.methodA(self)
        else:
            B.methodA(self)

rawDescription: Calling parent class with multiple inheritance in python


code:
def run_sql_safe(sql, input_tuple=(), get_update_id=False, debug = False):
    from contextlib import closing
    conn = get_mysql_connection()
    with closing(conn):
        cursor = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor)
        with closing(cursor):
            cursor.execute(sql, input_tuple)
            conn.commit()
            if get_update_id:
                return cursor.lastrowid

rawDescription: Why the "cursor.lastrowid" return 3?

description: Why the "cursor.lastrowid" return 3


rawDescription: Remove mean from numpy matrix

description: Remove mean from numpy matrix


code:
def second_smallest(numbers):
    m1, m2 = float('inf'), float('inf')
    for x in numbers:
        if x <= m1:
            m1, m2 = x, m1
        elif x < m2:
            m2 = x
    return m2

rawDescription: Python - Find second smallest number

description: Python - Find second smallest number


rawDescription: Generator-expression, getting the same results of using append or extend in a for-loop


code:
import collections

class CaseInsensitiveDict(collections.Mapping):
    def __init__(self, d):
        self._d = d
        self._s = dict((k.lower(), k) for k in d)
    def __contains__(self, k):
        return k.lower() in self._s
    def __len__(self):
        return len(self._s)
    def __iter__(self):
        return iter(self._s)
    def __getitem__(self, k):
        return self._d[self._s[k.lower()]]
    def actual_key_case(self, k):
        return self._s.get(k.lower())

rawDescription: Case insensitive dictionary search with Python


code:
from collections import OrderedDict

class UnsortableList(list):
    def sort(self, *args, **kwargs):
        pass

class UnsortableOrderedDict(OrderedDict):
    def items(self, *args, **kwargs):
        return UnsortableList(OrderedDict.items(self, *args, **kwargs))

yaml.add_representer(UnsortableOrderedDict, yaml.representer.SafeRepresenter.represent_dict)

rawDescription: Can PyYAML dump dict items in non-alphabetical order?


code:
class Base:
    def __new__(cls, *args, **kw):
        self = super.__new__(*args, **kw)
        if not hasattr(cls, _instances):
            cls._instances = []
        cls._instances.append(self)
        return self

    @classmethod
    def call_all(cls, method_name, *args, **kw):
        results = []
        for instance in cls._instances:
            results.append(getattr(instance, method_name)(*args, **kw))
        return results

    def __del__(self):
        self.__class__._instances.remoce(self)

class vehicles:
    class car(Base):

        def drive():
            pass

car1 = car()
car2 = car()
car3 = car()

car.call_all("drive")

rawDescription: Calling a method on all instances of an object


code:
import numpy as np
from scipy import ndimage

a = np.zeros((8,8), dtype=np.int)
a[1,1] = a[1,2] = a[2,1] = a[2,2] = a[3,1] = a[3,2] = 1
a[5,5] = a[5,6] = a[6,5] = a[6,6] = a[7,5] = a[7,6] = 1
lbl, numpatches = ndimage.label(a)

def func(x):
    return x.sum()

print(ndimage.labeled_comprehension(a, lbl, index=range(1, numpatches+1),
                                    func=func, out_dtype='float', default=None))
# [6 6]

rawDescription: Python - Apply a function over a labeled multidimensional array


rawDescription: How can I add an element at the top of an OrderedDict in python?

description: Add an element at the top of an OrderedDict in python


code:
if foo == 'blah':
    do_blah_thing()
do_one()
do_two()
do_three()

rawDescription: Python Ternary Style: Is this good or bad style?


code:
def cmp_key(lst):
    itersect_size = len(zset.intersection(lst))
    return intersect_size,-len(lst)

list_with_biggest_intersection = max(iterable,key = cmp_key)

rawDescription: "MOST-OF" in python

description: "MOST-OF" in python


code:
def eventFilter(self,target,event):
    if target == self.__ui.comboBox:
        if(event.type()== QtCore.QEvent.Wheel):#block wheel on combobox
            return True
    elif target == self.__ui.tableWidget.viewport():
        if(event.type()== QtCore.QEvent.MouseButtonDblClick):#block MouseButtonDblClick in table
            return True
        if(event.type()== QtCore.QEvent.MouseButtonRelease):#block MouseButtonRelease in table
            return True
    return False

rawDescription: looking for a SIGNAL in tablewidget

description: Looking for a SIGNAL in tablewidget


code: wordlist = [w for w in wordlist if w.startswith(word)]

rawDescription: Python- The Word Game "Ghost", File I/O, and List question


rawDescription: Card Game Simulation list.remove

description: Card Game Simulation list.remove


code:
periodic_callback = PeriodicCallback(
    functools.partial(my_function, x, y),
    10)

rawDescription: tornado PeriodicCallback: provide callback with arguments?

description: Tornado PeriodicCallback: provide callback with arguments


code:
def removeMin(my_list):
  smallestIndex = 0
  smallest = my_list[0]
  for i,val in enumerate(my_list):
    if val < smallest:
        smallest = val
        smallestIndex = i
  my_list.pop(smallestIndex)
  return my_list

rawDescription: How do I remove an element from a list without using .remove

description: Remove an element from a list without using .remove


code:
t = tf.constant(42.0)
u = tf.constant(37.0)
tu = tf.matmul(t, u)
ut = tf.matmul(u, t)
with sess.as_default():
   tu.eval()  # runs one step
   ut.eval()  # runs one step
   sess.run([tu, ut])  # runs a single step

rawDescription: In TensorFlow, what is the difference between Session.run() and Tensor.eval()?


code:
from Tkinter import *

class PropertyDialog(Toplevel):
    def __init__(self, root, string):
        Toplevel.__init__(self)
        self.wm_overrideredirect(1)
        self.root = root
        self.\
             geometry('+%d+%d' %
                      (root.winfo_pointerx(),
                       root.winfo_pointery()))
        try:
            self.tk.call('::Tk::unsupported::MacWindowStyle',
                                         'style', self._w,
                                         'help', 'noActivates')
        except TclError:
            pass
        window_frame = Frame(self)
        window_frame.pack(side=TOP, fill=BOTH, expand=True)
        exit_frame = Frame(window_frame, background='#ffffe0')
        exit_frame.pack(side=TOP, fill=X, expand=True)
        button = Button(exit_frame, text='x', width=3, command=self.free,
               background='#ffffe0', highlightthickness=0, relief=FLAT)
        button.pack(side=RIGHT)
        text_frame = Frame(window_frame)
        text_frame.pack(side=TOP, fill=BOTH, expand=True)
        label = Label(text_frame, text=string, justify=LEFT,
                      background='#ffffe0',
                      font=('tahoma', '8', 'normal'))
        label.pack(ipadx=1)
        self.attributes("-topmost", True)

    def free(self):
        self.destroy() # first we destroy this one

def bind():
    """
    toggle property window creation mode
    """
    root.bind('<ButtonPress-1>', create)

def create(event):
    """
    Create actual window upon mouse click
    """
    dialogs.append(PropertyDialog(root, 'help me'))

root = Tk()
dialogs = []
root.geometry('%dx%d' % (300,400))

Button(root, text='create', command=bind).pack()

root.mainloop()

rawDescription: Force set tkinter window to always have focus


code:
fruits = FruitList([Fruit('apple', 'red'), Fruit('banana', 'yellow')])

print("An apple is %s" % fruits['apple'].color)

rawDescription: Accessing list of Python objects by object attribute


rawDescription: Best way to remove all characters of a string until new line character is met python?

description: Remove all characters of a string until new line character is met python


code:
Example
width, height = 100, 100
im = QImage(width, height, QImage.Format_ARGB32)

for x in range(im.width()):
    for y in range(im.height()):
        im.setPixel(x, y, QColor(255, x * 2.56, y * 2.56, 255).rgb())

im.save('sample.png')

rawDescription: How do I change each pixel

description: Change each pixel


code:
import numpy as np
from numpy.polynomial.legendre import leggauss, legval

def f(x):
    if 0 <= x <= 1:
        return 1
    if -1 <= x <= 0:
        return -1

# of course you could write a vectorized version of
# this particular f(x), but I assume you have a more
# general piecewise function
f = np.vectorize(f)

deg = 100
x, w = leggauss(deg) # len(x) == 100

L = np.polynomial.legendre.legval(x, np.identity(deg))
# Sum L(xi)*f(xi)*wi
integral = (L*(f(x)*w)[None,:]).sum(axis=1)

c = (np.arange(1,51) + 0.5) * integral[1:51]

x_fine = np.linspace(-1, 1, 2001) # 2001 points
Lfine = np.polynomial.legendre.legval(x_fine, np.identity(51))

# sum_1_50 of c(n) * Ln(x_fine)
cLn_sum = (c[:,None] * Lfine[1:51,:]).sum(axis=0)

rawDescription: python: integrating a piecewise function

description: Python: integrating a piecewise function


code:
import scrapy
t = """<html lang="en">
<body>
<div>
    <script type="text/javascript">
        var i = {a:['O'Connor Park']}
    </script>

</div>
</body>
</html>
"""
selector = scrapy.Selector(text=t, type="html")

# extracting the <script> element as raw HTML
selector.xpath('//div/script').extract_first()

# only getting the text node inside the <script> element
selector.xpath('//div/script/text()').extract_first()

rawDescription: How to extract raw html from a Scrapy selector?

description: Extract raw html from a Scrapy selector


code: {k: v for e in wp_users for k, v in zip(('ID', 'post_author'), e)}

rawDescription: multiple key value pairs in dict comprehension

description: Multiple key value pairs in dict comprehension


rawDescription: Python Scipy How to traverse upper/lower trianglar portion non-zeros from csr_matrix


code:
from BeautifulSoup import BeautifulSoup
text = '

'
pool = BeautifulSoup(text)
result = []
for tag in pool.findAll(True,{'id':True}) :
    result.append(tag['id'])

rawDescription: Getting id names with beautifulsoup

description: Getting id names with beautifulsoup