code:
# pkg - A fancy software package
# Copyright (C) 2013  author (email)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/gpl.html.
"""pkg: a software suite for

Hey look at me I'm a long description
But how long am I?

"""

from __future__ import division, print_function

#ideas for setup/f2py came from:
#    -numpy setup.py: https://github.com/numpy/numpy/blob/master/setup.py 2013-11-07
#    -winpython setup.py: http://code.google.com/p/winpython/source/browse/setup.py 2013-11-07
#    -needing to use
#        import setuptools; from numpy.distutils.core import setup, Extension:
#        http://comments.gmane.org/gmane.comp.python.f2py.user/707 2013-11-07
#    -wrapping FORTRAN code with f2py: http://www2-pcmdi.llnl.gov/cdat/tutorials/f2py-wrapping-fortran-code 2013-11-07
#    -numpy disutils: http://docs.scipy.org/doc/numpy/reference/distutils.html 2013-11-07
#    -manifest files in disutils:
#        'distutils doesn't properly update MANIFEST. when the contents of directories change.'
#        https://github.com/numpy/numpy/blob/master/setup.py
#    -if things are not woring try deleting build, sdist, egg directories  and try again:
#        http://stackoverflow.com/a/9982133/2530083 2013-11-07
#    -getting fortran extensions to be installed in their appropriate sub package
#        i.e. "my_ext = Extension(name = 'my_pack._fortran', sources = ['my_pack/code.f90'])"
#        Note that sources is a list even if one file:
#        http://numpy-discussion.10968.n7.nabble.com/f2py-and-setup-py-how-can-I-specify-where-the-so-file-goes-tp34490p34497.html 2013-11-07
#    -install fortran source files into their appropriate sub-package
#        i.e. "package_data={'': ['*.f95','*.f90']}# Note it's a dict and list":
#        http://stackoverflow.com/a/19373744/2530083 2013-11-07
#    -Chapter 9 Fortran Programming with NumPy Arrays:
#        Langtangen, Hans Petter. 2013. Python Scripting for Computational Science. 3rd edition. Springer.
#    -Hitchhikers guide to packaging :
#        http://guide.python-distribute.org/
#    -Python Packaging: Hate, hate, hate everywhere :
#        http://lucumr.pocoo.org/2012/6/22/hate-hate-hate-everywhere/
#    -How To Package Your Python Code:
#        http://www.scotttorborg.com/python-packaging/
#    -install testing requirements:
#        http://stackoverflow.com/a/7747140/2530083 2013-11-07

import setuptools
from numpy.distutils.core import setup, Extension
import os
import os.path as osp

def readme(filename='README.rst'):
    with open('README.rst') as f:
        text=f.read()
    f.close()
    return text

def get_package_data(name, extlist):
    """Return data files for package *name* with extensions in *extlist*"""
    #modified slightly from taken from http://code.google.com/p/winpython/source/browse/setup.py 2013-11-7
    flist = []
    # Workaround to replace os.path.relpath (not available until Python 2.6):
    offset = len(name)+len(os.pathsep)
    for dirpath, _dirnames, filenames in os.walk(name):
        for fname in filenames:
            if not fname.startswith('.') and osp.splitext(fname)[1] in extlist:
#                flist.append(osp.join(dirpath, fname[offset:]))
                flist.append(osp.join(dirpath, fname))
    return flist

DOCLINES = __doc__.split("\n")
CLASSIFIERS = """\
Development Status :: 1 - Planning
License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Programming Language :: Python :: 2.7
Topic :: Scientific/Engineering
"""

NAME = 'pkg'
MAINTAINER = "me"
MAINTAINER_EMAIL = "me@me.com"
DESCRIPTION = DOCLINES[0]
LONG_DESCRIPTION = "\n".join(DOCLINES[2:])#readme('readme.rst')
URL = "http://meeeee.mmemem"
DOWNLOAD_URL = "https://github.com/rtrwalker/geotecha.git"
LICENSE = 'GNU General Public License v3 or later (GPLv3+)'
CLASSIFIERS = [_f for _f in CLASSIFIERS.split('\n') if _f]
KEYWORDS=''
AUTHOR = "me"
AUTHOR_EMAIL = "me.com"
PLATFORMS = ["Windows"]#, "Linux", "Solaris", "Mac OS-X", "Unix"]
MAJOR = 0
MINOR = 1
MICRO = 0
ISRELEASED = False
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)

INSTALL_REQUIRES=[]
ZIP_SAFE=False
TEST_SUITE='nose.collector'
TESTS_REQUIRE=['nose']

DATA_FILES = [(NAME, ['LICENSE.txt','README.rst'])]
PACKAGES=setuptools.find_packages()
PACKAGES.remove('tools')

PACKAGE_DATA={'': ['*.f95','*f90']}
ext_files = get_package_data(NAME,['.f90', '.f95','.F90', '.F95'])
ext_module_names = ['.'.join(osp.splitext(v)[0].split(osp.sep)) for v in ext_files]
EXT_MODULES = [Extension(name=x,sources=[y]) for x, y in zip(ext_module_names, ext_files)]

setup(
    name=NAME,
    version=VERSION,
    maintainer=MAINTAINER,
    maintainer_email=MAINTAINER_EMAIL,
    description=DESCRIPTION,
    long_description=LONG_DESCRIPTION,
    url=URL,
    download_url=DOWNLOAD_URL,
    license=LICENSE,
    classifiers=CLASSIFIERS,
    author=AUTHOR,
    author_email=AUTHOR_EMAIL,
    platforms=PLATFORMS,
    packages=PACKAGES,
    data_files=DATA_FILES,
    install_requires=INSTALL_REQUIRES,
    zip_safe=ZIP_SAFE,
    test_suite=TEST_SUITE,
    tests_require=TESTS_REQUIRE,
    package_data=PACKAGE_DATA,
    ext_modules=EXT_MODULES,
    )

rawDescription: packaging with numpy and test suite

description: Packaging with numpy and test suite


code:
with open('/path/to/my_file.txt', 'r') as my_file:
    text = my_file.read()
    text = text.replace("[", "")
    text = text.replace("]", "")

# If you wish to save the updates back into a cleaned up file
with open('/path/to/my_file_clean.txt', 'w') as my_file:
    my_file.write(text)

rawDescription: Remove double and single square brackets from text file generated from python


code:
lst = ["aaa 1", "aaa 2", "bbb 2", "ccc 3", "ddd 4"]
[x for x,y in enumerate(lst) if y.split()[0] == "aaa"]

rawDescription: Python - search for string in array

description: Python - search for string in array


code:
import random

answered = 0
correct = 0

ops = ['+', '-', '*',]
num1 = random.randint(0,10)
num2 = random.randint(0,10)
operation = random.choice(ops)

maths = eval(str(num1) + operation + str(num2))

combined = [num1, operation, num2]

def shuffle():
    global ops, num1, num2, operation, maths, combined
    ops = ['+', '-', '*',]
    num1 = random.randint(0,10)
    num2 = random.randint(0,10)
    operation = random.choice(ops)

    maths = eval(str(num1) + operation + str(num2))

    combined = [num1, operation, num2]

while answered <= 10:
    ans = int(input("what is {}".format(combined)))
    if ans == maths:
        answered += 1
        correct += 1
        print("Correct")
        print("Answered: ", answered, "Correct: ", correct)
        shuffle()
    elif ans != maths:
        answered = answered + 1
        print("Incorrect")
        shuffle()

rawDescription: I need help making this random quiz generator. i cant get the looping or the questioning part working correctly


code:
df_melted = pd.melt(df, id_vars="name", var_name="year")
df_melted.head()

rawDescription: Turn columns to rows in pandas

description: Turn columns to rows in pandas


code:
for line in section.split('/n'):
    t = re.match(r'(.*?)\s.*?\(flex1.com/\d+? (\d+)?\)',line)

rawDescription: parse text using bash/python

description: Parse text using bash/python


code: f.writelines('\t'.join(x for x in g if x) + '\n' for g in grouper(4, values))

rawDescription: write a new line text file python every 4 tabs

description: Write a new line text file python every 4 tabs


code:
def funcA(): return 'A'
def twice(n): return 2 * n
def swap(a, b): return b, a

rawDescription: How do you test one python function in a multifunction python file from the command line?

description: Test one python function in a multifunction python file from the command line


code: ui = uniformity(z_vals)

rawDescription: Does NumPy have a Uniformity function?

description: Does NumPy have a Uniformity function


code:
target_player = query_dict['player'].decode('utf-8')
target_player
print(target_player)

rawDescription: How to parse a utf-8 encoded query parameter with Python 2.6

description: Parse a utf-8 encoded query parameter with Python 2.6


code:
import numpy as np
import pandas as pd
from scipy.interpolate import interp1d

np.random.seed(1)
n = 10e4

df = pd.DataFrame({'a': np.random.randn(n),
                   'b': 10 + np.random.randn(n),
                   'c': 30 + np.random.randn(n)})

xs = [-10, 0, 10]
cvs = df.columns.values

rawDescription: Pandas: Make a new column by linearly interpolating between existing columns


rawDescription: QTableWidget Current Selection Change Signal


code:
from threading import local
_thread_locals = local()

def set_current_user(user):
    _thread_locals.user=user

def get_current_user():
    return getattr(_thread_locals, 'user', None)

rawDescription: DRY way to add created/modified by and time


code:
import re
s = 'myhost[01-03]'
k, num1, num2 = re.search(r'(.+?)\[(\d+)-(\d+)', s).groups() # Gets the first word, first number, second number and stores it to their respective variables
[k + '{0:02d}'.format(i) for i in range(int(num1), int(num2)+1)] # format function here is used to pad zeros if there is only one digit exists.

rawDescription: parse from pdsh format

description: Parse from pdsh format


code: location = hostname.split('.')[1]

rawDescription: How to compare list with actual string in Python?

description: Compare list with actual string in Python


code: wordcount = sorted(wordcount.items(), key=lambda x: x[1], reverse=True)

rawDescription: Python word count program from txt file

description: Python word count program from txt file


rawDescription: How does python sort a list of dictionaries?


rawDescription: (Python 3.3) Can not figure out why function creates infinite loop when value gets to 5


code:
cat, dog, fish = 5, 3, 7
animal_name(cat)
animal_name(dog)
animal_name(fish)

rawDescription: how to convert variable into string in python

description: Convert variable into string in python


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

description: Print lists within a tuple in straight line


code: print(inchConverter(centimeterValue))

rawDescription: Centimeter to Feet converter in Python

description: Centimeter to Feet converter in Python


code:
import time
import random
import sys

offset = random.randint(0,1)

for i in xrange(0, 20+offset):
    if i % 2 == 0:
        sys.stdout.write("Heads")
    else:
        sys.stdout.write("Tails")
    time.sleep(0.1)
    sys.stdout.flush()
    sys.stdout.write("\r")

rawDescription: Overwrite printed line in python 3.x on mac


code:
speed['reached_date'] = dates + speed['reached_time'].values
speed['diff'] = speed['created_time'] - speed['reached_date']

rawDescription: pandas date column subtraction

description: Pandas date column subtraction


code:
def complementary_pairs_number(complementary_diff, values):
    value_key = {} # dictionary storing indexes indexed by values
    for index, item in enumerate(values):
        value_key.setdefault(item,[]).append(index)
    # print(value_key)
    num_pairs = 0
    seen = set()
    for pos_value in value_key:
        if pos_value in seen: continue
        sym_value = complementary_diff - pos_value
        seen.add(sym_value)
        if sym_value in value_key:
            # print(pos_value, sym_value, value_key[pos_value],value_key[sym_value])
            n = len(value_key[pos_value])*len(value_key[sym_value])
            if pos_value == sym_value:
                num_pairs += n
            else:
                num_pairs += 2*n
    return num_pairs

rawDescription: Flattening nested loops / decreasing complexity - complementary pairs counting algorithm


rawDescription: uploading mixed data from csv into a numpy array in python

description: Uploading mixed data from csv into a numpy array in python


code: nums = (numpy.random.rand(1000) > 0.1).astype(int)

rawDescription: Generate random array of 0 and 1 with specific ratio (python , numpy)


code:
from collections import Counter
liste = [1,2,3,5,10]
liste2 = [[1,2,3,5,10], [1,2], [1,5,10], [3,5,10], [1,2,5,10]]
c = Counter()
for elt in liste2:
    nTuple = len(elt)   # number of elements in the syn
    for i in liste:
        if elt.count(i):
               myTuple = (i, nTuple)
               c[myTuple] += 1
for item in c.items():
    print(item)

rawDescription: Count how many times a tuple appears in ouput results


code:
def process_request(self, request):
    _thread_local.CONSTANT_NAME = CONSTANT_NAME

rawDescription: Set global constant cross all the view

description: Set global constant cross all the view


code:
setup(name = "Urban Planning",
      windows = [{'script': "main.py", "dest_base": "Urban_Planning"}],
      options = opts, # Dictionary of options
      zipfile = None, # Don't create zip file
      data_files = Mydata_files) # Add list of data files to folder

rawDescription: py2exe access 'other_resources'

description: Py2exe access 'other_resources'


code:
first_n(xrange(100), 10)
list(first_n(xrange(100), 10))

rawDescription: Truncating a python generator

description: Truncating a python generator


code:
def split_after(lst, n):
    for i in range(n):
        yield lst[i]
    yield lst[n:]

rawDescription: Python: Convert n-tuple to x-tuple where x < n


code:
key = 'a'
sum(len(dct[key]) for dct in dataset.values())

rawDescription: How to find the sum of the lengths of a list in a dictionary of dictionaries?

description: Find the sum of the lengths of a list in a dictionary of dictionaries


code: df['B'] = df1['E'].values

rawDescription: how to replace an entire column on Pandas.DataFrame

description: Replace an entire column on Pandas.DataFrame


code:
o.write("column1 header,column2 header,column3 header,"
        "column4 header,column5 header")

rawDescription: Clean code for writing long strings to file in Python


rawDescription: shortcut evalution of numpy's array comparison

description: Shortcut evalution of numpy's array comparison


rawDescription: Creating a new list from 2 lists based on the index return from a third list


code:
def myfilter(x):  # example filter, only take lists with 10 or more 1s
    return x.count(1)>=10

rawDescription: Recursive generation + filtering. Better non-recursive?


code:
def avg(myArray, N=2):
    cum = np.cumsum(myArray,0)
    result = cum[N-1::N]/float(N)
    result[1:] = result[1:] - result[:-1]

    remainder = myArray.shape[0] % N
    if remainder != 0:
        if remainder < myArray.shape[0]:
            lastAvg = (cum[-1]-cum[-1-remainder])/float(remainder)
        else:
            lastAvg = cum[-1]/float(remainder)
        result = np.vstack([result, lastAvg])

    return result

rawDescription: Fast way to take average of every N rows in a .npy array


code:
import random
import bisect
import collections

def downsample(freq, n):
    cumsums = []
    total = 0
    choices, weights = zip(*freq.items())
    for weight in weights:
        total += weight
        cumsums.append(total)
    assert 0 <= n <= total
    result = collections.Counter()
    for _ in range(n):
        rnd = random.uniform(0, total)
        i = bisect.bisect(cumsums, rnd)
        result[choices[i]] += 1
        cumsums = [c if idx<i else c-1 for idx, c in enumerate(cumsums)]
        total -= 1
    return result

freq = {'a': 5, 'c': 3, 'b': 2, 'd': 1}
print(downsample(freq, 5))

rawDescription: Python: downsample a population using frequency data


code: countries = [e[0] for e in x]

rawDescription: how to make new list of elements from existing list in python?

description: Make new list of elements from existing list in python


code:
c=db.cursor()
max_price=5
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""", (max_price,))

rawDescription: Python best practice and securest to connect to MySQL and execute queries


code: [list(x) for x in set.intersection(*sets)]

rawDescription: python - Common lists among lists in a list

description: Python - Common lists among lists in a list


code: arr3 = arr3.reshape(-1, 4)

rawDescription: Difference between every row and column in two DataFrames (Python / Pandas)


code:
#ef_Main.py

import wx
import  ef_UI as eU
import ef_Tool as eT

def main():
    efToolObj = eT.MyTools() # object of MyTools class
    app = wx.App(False)
    frame = eU.MyFrame(efToolObj) # Pass this to MyFrame so that it can make use of it
    frame.Show()
    app.MainLoop()

if __name__ == "__main__":
    main()

rawDescription: binding packages with wxpython

description: Binding packages with wxpython


code: M = d / S

rawDescription: Image foveation in Python

description: Image foveation in Python


rawDescription: How do I change order/grouping/level of pandas MultiIndex columns?

description: Change order/grouping/level of pandas MultiIndex columns


code:
def possibleSplits(value,validIncrements):
    ret = []
    for increment in validIncrements:
        if increment > value:
            continue
        if increment == value:
            ret.append([increment])
            continue
        if increment < value:
            remainder = value - increment
            toAdd = possibleSplits(remainder, validIncrements)
            for a in toAdd:
                ret.append([increment] + a)
    return ret

rawDescription: How to break down a number with another from list?

description: Break down a number with another from list


code:
import csv
with open ("lol.txt",'rb') as f:
    csv_r = csv.reader (f)
    entries = [ (name, float(p1), float(p2)) for name, p1, p2 in csv_r]

for index, entry in enumerate(entries):
    print ("%2i. %-10s %5.2f %5.2f" % (index, entry[0], entry[1], entry[2]))

choice = int(raw_input("Choose a number: "))

print (entries[choice])

rawDescription: Retrieving data from a csv, and assigning the value at row x and column y to a variable. Py3.2, *homework*


rawDescription: Print histogram in python 3

description: Print histogram in python 3


code:
for key in keywords:
    row = row.replace(key, "")

rawDescription: Outputting to csv without stop words

description: Outputting to csv without stop words


code:
d_flat.col2 = d_flat.col2.map(lambda x: 'col2_%d'%x)

#d_flad.col2
#0    col2_0
#1    col2_0
#2    col2_1
#3    col2_0
#4    col2_1
#5    col2_0
#6    col2_1
#Name: col2, dtype: object

rawDescription: Pivoted pandas dataframe to dictionary

description: Pivoted pandas dataframe to dictionary


code:
conn.execute(feedtag_table.insert(),
    [{'feedid': entry_id, 'tagid': tag_ids[tag]} for tag in hashtags])

rawDescription: Setting up/Inserting into Many-to-Many Database with Python, SQLALchemy, Sqlite


code:
class ImmutableList:
    def __init__ (self, actualList):
        self.__lst = actualList
    def __len__ (self):
        return self.__lst.__len__()
    def __getitem__ (self, key):
        return self.__lst.__getitem__(key)
    def __iter__ (self):
        return self.__lst.__iter__()
    def __reversed__ (self):
        return self.__lst.__reversed__()
    def __contains__ (self, item):
        return self.__lst.__contains__(item)
    def __repr__ (self):
        return self.__lst.__repr__()
    def __str__ (self):
        return self.__lst.__str__()

rawDescription: Wrapping big list in Python 2.7 to make it immutable


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--opt", action='append')

args = parser.parse_args()
print(args.opt)

rawDescription: How to iterate over all optional arguments and related values?

description: Iterate over all optional arguments and related values


rawDescription: Regexp - find a value between a part of the string and a second part of the string OR end of line


code:
class PicDisplay(ogl.ShapeCanvas):
    def add_picture(self, image_filename):
        new_img = ogl.BitmapShape()
        add_alpha = PicAdjuster()
        new_img.SetBitmap(add_alpha.adjust_pic(factor_alpha = 0.5))
        self.diagram.AddShape(new_img)

rawDescription: wxOGL wx.ShapeCanvas images

description: WxOGL wx.ShapeCanvas images


code:
def p4(n, _p=[1]):
    "Number of partitions of n."

    from math import sqrt

    def inner(n, _p=_p):
        k = int((sqrt(24*n+1)-1.) / 6.) + 1
        negative = not (k & 1)
        i = n - (k*(3*k+1) >> 1)
        assert i < 0
        s = 0
        if i + k >= 0:
            s = _p[i+k]
            if negative:
                s = -s
        assert i + 3*k - 1 >= 0
        for k in xrange(k-1, 0, -1):
            i += 3*k + 2
            negative = not negative
            t = _p[i] + _p[i + k]
            if negative:
                t = -t
            s += t
        assert i+k == n-1
        _p[n] = s

    if n < 0:
        raise ValueError("argument must be >= 0")
    oldlen = len(_p)
    if n >= oldlen:
        _p.extend([None] * (n - oldlen + 1))
        for nn in xrange(oldlen, n+1):
            inner(nn)
    return _p[n]

rawDescription: Euler's pentagonal number function in python


code:
base = data['AttrA'] # keep a reference to the original sort order
for key in data:
    data[key] = [x for (y,x) in sorted(zip(base, data[key]))]

rawDescription: Sorting values of a dictionary based on a list


rawDescription: PANDAS drop a range of rows from df

description: PANDAS drop a range of rows from df


code: myfile = open('Stats.txt','a') # append

rawDescription: Making A Statistics Text File (Python)

description: Making A Statistics Text File (Python)


code:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

points = np.array([[-1, -1, -1],
                      [1, -1, -1 ],
                      [1, 1, -1],
                      [-1, 1, -1],
                      [-1, -1, 1],
                      [1, -1, 1 ],
                      [1, 1, 1],
                      [-1, 1, 1]])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r = [-1,1]
X, Y = np.meshgrid(r, r)
ax.plot_surface(X,Y,1, alpha=0.5)
ax.plot_surface(X,Y,-1, alpha=0.5)
ax.plot_surface(X,-1,Y, alpha=0.5)
ax.plot_surface(X,1,Y, alpha=0.5)
ax.plot_surface(1,X,Y, alpha=0.5)
ax.plot_surface(-1,X,Y, alpha=0.5)
ax.scatter3D(points[:, 0], points[:, 1], points[:, 2])
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()

rawDescription: Plot surfaces on a cube

description: Plot surfaces on a cube


code: open('example_label.pdf', 'wb')

rawDescription: module base64.decode yields corrupt pdf base64 encoded file

description: Module base64.decode yields corrupt pdf base64 encoded file


code:
import numpy as np
import itertools
import matplotlib.pyplot as plt
m = 5
n = 5
x = np.zeros(shape=(m, n))
plt.figure(figsize=(5.15, 5.15))
plt.clf()
plt.subplot(111)
marker = itertools.cycle(('o', 'v', '^', '<', '>', 's', '8', 'p'))

plt.gca().set_color_cycle(None)
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    plt.plot(x, y, linestyle='', markeredgecolor='none', marker=marker.next())
plt.gca().set_color_cycle(None)
for i in range(1, n):
    x = np.dot(i, [1, 1.1, 1.2, 1.3])
    y = x ** 2
    plt.plot(x, y, linestyle='-')
plt.ylabel(r'$y$', labelpad=6)
plt.xlabel(r'$x$', labelpad=6)
plt.savefig('test.png')

rawDescription: How to set same color for markers and lines in a matplotlib plot loop?

description: Set same color for markers and lines in a matplotlib plot loop


code:
class DelayComponents(object):
    '''
    Delay Components Class
    '''

    ATTRS = ['prop_delay', 'trans_delay', 'proc_delay', 'queue_delay']

    def __init__(self, **delays):
        '''
        Constructor
        '''
        for attr in self.ATTRS:
            setattr(self, attr, round(float(delays.get(attr, 0)), 2))
        # No point in setting delay here - it's already done!

    def __setattr__(self, key, value):
        super(DelayComponents, self).__setattr__(key, value)
        # This avoids directly interacting with the __dict__
        if key in self.ATTRS:
            self.delay = sum(getattr(self, attr, 0) for attr in self.ATTRS)

rawDescription: Building variable dependencies

description: Building variable dependencies


rawDescription: How to convert numpy datetime64 into datetime

description: Convert numpy datetime64 into datetime


code:
import pickle

def test1():        #Function to be send
    return True

with open('data.pickle', 'wb') as f:    #On server you pickling function
    pickle.dump(test1.__code__, f)

rawDescription: Sending function pointer over network in python


code:
def to_secs(hours, minutes, seconds):
    return hours * 3600 + minutes * 60 + seconds

rawDescription: In Python I need a to_secs function to convert into seconds


code: pid_number = subprocess.check_output(['pgrep', sys.argv[1]])

rawDescription: Save output of bash command into variable inside if..else statement


code:
s = "(your data as a single multiline string)"

datalines = lambda s: [ln for ln in (line.strip() for line in s.splitlines()) if ln]

_, _, po_number, _, rem = s.split('**')
shipto, data = rem.split('SKU', 1)

po_number = datalines(po_number)[0]
shipto    = '\n'.join(datalines(shipto))
data      = datalines(data)[2:]

res = [[po_number, shipto, sku, prod, qty] for sku,prod,qty in zip(*([iter(data)]*3))]

rawDescription: How to extract information from text using regexes in Python?

description: Extract information from text using regexes in Python


code:
import re
patt = re.compile('^(MyExclusion|Bad)')
[i for i in fnmatch.filter(files, '*.java') if not patt.search(i)]

rawDescription: Filter for file extensions using os.walk but with restrictions on file name


code:
df.groupby('Country_FAO').sum()
df.groupby('Country_FAO').sum().nlargest(2, 'mean_area')
df.groupby('Country_FAO').sum().nlargest(2, 'mean_area').index

rawDescription: Accepting top rows in pandas dataframe based on grouping


code: [word for word in ('KöLN', 'K\xc3\xb6LN', 'Cologne') if word.isalpha()]

rawDescription: How can I check for unicode or escape sequences in a string?

description: Check for unicode or escape sequences in a string


code:
import re
topic_message = 'This topic is a-ok'

# Option 1: Invalidate one char in string.
re1 = re.compile(r"[<>/{}[\]~`]");
if re1.search(topic_message):
    print ("RE1: Invalid char detected.")
else:
    print ("RE1: No invalid char detected.")

# Option 2: Validate all chars in string.
re2 =  re.compile(r"^[^<>/{}[\]~`]*$");
if re2.match(topic_message):
    print ("RE2: All chars are valid.")
else:
    print ("RE2: Not all chars are valid.")

rawDescription: Efficient way to search for invalid characters in python


code: string = str(resp, encoding="utf-8")

rawDescription: writing persian text into a text file in the way which could be read in python

description: Writing persian text into a text file in the way which could be read in python


rawDescription: Concatanating array by While on Python

description: Concatanating array by While on Python


code:
with open(csv_file_location,'a') as f:
    f.write("%s\n" % file_location2)

rawDescription: Python: copy a file and rename multiple copies according to csv contents


code: timeit.timeit('for i in nums:pass', setup='nums = range(0,100)')

rawDescription: Why is direct indexing of an array significantly faster than iteration?


rawDescription: How do I merge two time series with different time values in python?

description: Merge two time series with different time values in python


code:
timer = QTimer(self)
timer.setInterval(20) # interval in ms
self.connect(timer, SIGNAL("timeout()"), self.update)
timer.start()

rawDescription: Qt4 w/ PySide: How to Create Animation Loop for a Widget?


code: help('help')

rawDescription: python help django navigation

description: Python help django navigation


code:
def my_settings_class(cls):
    for field in cls._settings_vars:
        add_getter_setter_and_property(cls, field)
    return cls

@my_settings_class
class _CanvasSettings:
    _settings_vars = ["size_x", "size_y", "lock_ratio"]
    def __init__(self, **kwargs):
        super().__init__()
        self._size_x = _int(kwargs, 'size_x', 320)
        self._size_y = _int(kwargs, 'size_y', 240)
        self._lock_ratio = _bool(kwargs, 'lock_ratio', True)

rawDescription: Is it possible to modify a class with a decorator


rawDescription: Showing what Twitter followers are followed by friends in Python/Django


code: stack.push(j.split(' '))

rawDescription: Reverse the order of words in python using stack


code:
df['labeled'], nobjs = ndimage.label(df['erosion'])
#      RF   mask dilation erosion  labeled
# 0   0.3   True     True    True        1
# 1   1.1   True     True    True        1
# 2   0.2   True     True    True        1
# 3   0.0  False     True    True        1
# 4   0.0  False     True    True        1
# 5   0.0  False     True    True        1
# 6   0.0  False     True    True        1
# 7   0.0  False     True    True        1
# 8   1.1   True     True    True        1
# 9   0.6   True     True    True        1
# 10  0.0  False     True   False        0
# 11  0.0  False     True   False        0
# 12  0.0  False    False   False        0
# 13  0.0  False     True   False        0
# 14  0.0  False     True   False        0
# 15  0.0  False     True   False        0
# 16  0.8   True     True    True        2
# 17  1.1   True     True    True        2
# 18  2.3   True     True    True        2
# 19  1.4   True     True    True        2
# 20  0.4   True     True    True        2
# 21  0.2   True     True    True        2
# 22  0.0  False     True   False        0
# 23  0.0  False     True   False        0
# 24  0.0  False    False   False        0
# 25  0.0  False    False   False        0
# 26  0.0  False    False   False        0
# 27  0.0  False    False   False        0

rawDescription: How to identify approximately (threshold defined) continuous non null data using pandas?

description: Identify approximately (threshold defined) continuous non null data using pandas


code:
l = [{1},{2,3},{4,5}]
if 3 in {x for s in l for x in s}:
    print("Here you are!")

rawDescription: Python: searching within a list

description: Python: searching within a list


code: pipe = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

rawDescription: Subprocess.Popen() => No output

description: Subprocess.Popen() => No output


code:
import os

def remove_line_from_file(filename, line_to_remove, dirpath=''):
    """Remove all occurences of `line_to_remove` from file
    with name `filename`, contained at path `dirpath`.
    If `dirpath` is omitted, relative paths are used."""
    filename = os.path.join(dirpath, filename)
    temp_path = os.path.join(dirpath, 'temp.txt')

    with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:
        for line in f_read:
            if line.strip() == line_to_remove:
                continue
            temp.write(line)

    os.remove(filename)
    os.rename(temp_path, filename)

def main():
    """Driver function"""
    directory = raw_input('directory: ')
    word = raw_input('word: ')

    dirpath, _, files = next(os.walk(directory))

    for f in files:
        remove_line_from_file(f, word, dirpath)

if __name__ == '__main__':
    main()

rawDescription: Deleting a line in multiple files in python


code:
def serve_forever(self):
    '''
    Run the server forever
    '''
    current_process()._manager_server = self
    try:
        try:
            while 1:
                try:
                    c = self.listener.accept()
                except (OSError, IOError):
                    continue
                t = threading.Thread(target=self.handle_request, args=(c,))
                t.daemon = True
                t.start()
        except (KeyboardInterrupt, SystemExit):
            pass
    finally:
        self.stop = 999
        self.listener.close()

rawDescription: Properly Designing a Multiprocessing.Manager Custom Object


code:
df1.columns = [x.strip() for x in df1.columns] # get rid of the leading/trailing spaces
df1 = df1.rename(columns={"Period (days)": "Period"})

rawDescription: Cleaning HTML tables with Pandas

description: Cleaning HTML tables with Pandas


code:
from docutils.core import publish_parts
from bs4 import BeautifulSoup

rst = get_rst_string_from_somewhere()

# get just the body of an HTML document
html = publish_parts(rst, writer_name='html')['html_body']
soup = BeautifulSoup(html, 'html.parser')

# docutils wraps the body in a div with the .document class
# we can just dispose of that div altogether
wrapper = soup.select('.document')[0]
wrapper.unwrap()

# knowing that docutils gives all sidebar elements the
# .sidebar class makes extracting those elements easy
sidebar = ''.join(tag.extract().prettify() for tag in soup.select('.sidebar'))

# leaving the non-sidebar elements as the document body
body = soup.prettify()

rawDescription: How do I convert a docutils document tree into an HTML string?

description: Convert a docutils document tree into an HTML string


code: midx = pd.MultiIndex.from_tuples([x.split()[::-1] for x in idx])

rawDescription: Create multiindex based on single index

description: Create multiindex based on single index


code:
def keep_secret(l: wordinput) -> list:
    for i in wordinput:
        if i != 'secret':
            print (i)

rawDescription: Avoiding to print out a certain word in a list


code:
songNames = ['song1', 'song2', 'song3']
artistNames = ['artist1', 'artist2', 'artist3']
dirNames = ['dir1', 'dir2', 'dir3']

songdata = [SongData(*params) for params in zip(songNames, artistNames, dirNames)]

rawDescription: How to print out __str__ for each instance of a class in a loop

description: Print out __str__ for each instance of a class in a loop


code: all(c in '10' for c in aString) and len(aString) == 8

rawDescription: How to tell if a string has exactly 8 1's and 0's in it in python

description: Tell if a string has exactly 8 1's and 0's in it in python


code:
#!~/project/env/bin/python

print('hello world!')

rawDescription: Install python module only on home folder in server


code: return type('OrderForm', (Form, StaticForm), form_fields)

rawDescription: How to subclass from a dynamic WTForm?

description: Subclass from a dynamic WTForm


code: df['TimeDiff'] = (df['time_x'] - df['time_y']).dt.seconds

rawDescription: How to create a new column based on time difference of two columns in Pandas?

description: Create a new column based on time difference of two columns in Pandas


code: list(itertools.product(*(('*', x) for x in seq)))

rawDescription: Looking for a tuple substitution algorithm


rawDescription: Numpy.cumsum in reverse

description: Numpy.cumsum in reverse


code:
from django.contrib.auth.models import User
class UserProfile(models.Model):
    user = models.ForeignKey(User)
    type = models.CharField(max_length=15)

rawDescription: How to handle multiple user type in Django

description: Handle multiple user type in Django


code:
for link in a.xpath('@href').extract():
    yield Request(link, callback=self.parse_category)

rawDescription: Why can i not crawl this website with Scrapy


code:
for x in doc:
   if x not in dic:dic[x]=1.

rawDescription: Implementation of TFIDF weighting scheme


code:
exclude = ['lulz', 'lmfao', '.png']
filter_func = lambda s: 'http://' in s and not any(x in s for x in exclude)

matching_lines = filter(filter_func, string_list)

rawDescription: If string does not contain any of list of strings in python


code:
import os
os.environ['PYTHONPATH']

rawDescription: No solutions for django.core.management error


code:
def sorted_group_by_start(inputlist, n):
    key = itemgetter(slice(n))
    return [list(g) for k, g in groupby(sorted(inputlist, key=key), key=key)]

rawDescription: Python. How to make list from another list by mark?


code:
# Map compiler types to (module_name, class_name) pairs -- ie. where to
# find the code that implements an interface to this compiler.  (The module
# is assumed to be in the 'distutils' package.)
compiler_class = { 'unix':    ('unixccompiler', 'UnixCCompiler',
                               "standard UNIX-style compiler"),
                   'msvc':    ('msvccompiler', 'MSVCCompiler',
                               "Microsoft Visual C++"),
                   'cygwin':  ('cygwinccompiler', 'CygwinCCompiler',
                               "Cygwin port of GNU C Compiler for Win32"),
                   'mingw32': ('cygwinccompiler', 'Mingw32CCompiler',
                               "Mingw32 port of GNU C Compiler for Win32"),
                   'bcpp':    ('bcppcompiler', 'BCPPCompiler',
                               "Borland C++ Compiler"),
                   'emx':     ('emxccompiler', 'EMXCCompiler',
                               "EMX port of GNU C Compiler for OS/2"),
                 }

rawDescription: Get the commands distutils passes to the compiler


code:
seen = set()
unique = [d for d in dictlist if id(d) not in seen and not seen.add(id(d))]

rawDescription: Is there a python builtin to extract the 'set' of unhashable objects from a list, using 'is' comparison?


code:
vsize=ax.get_position().size[1]  #fraction of figure occupied by axes
axesdpi= int((Fig.get_size_inches()[1]*vsize)/R)  #(or Yi*R according to what you want to do)

rawDescription: Plotting at full resolution with matplotlib.pyplot, imshow() and savefig()?


code:
vals = ['nontriallist', 'triallist', 'user']
a = pd.DataFrame({'survey_components': 0,
                  'segment': 3 * vals,
                  'persuasion': 30 * np.random.rand(9)})
a = a.set_index(['survey_components', 'segment'])
b = pd.DataFrame({'survey_components': 0,
                  'segment': vals,
                  'base_size': 30 * np.random.rand(3)})
b = b.set_index(['survey_components', 'segment'])

a.div(b.rename(columns={'base_size':'persuasion'}))

rawDescription: Dividing an Dataframe by an measure with overlapping indexes results in NAN


code:
import pickle
data = [["Ms Office", "CD", 7, "Microsoft"], ["Acrobat Reader", "DVD", 12, "Adobe"], ["Norton Antivirus", "DVD", 24, "Symantec"]]

rawDescription: How to load a nested list from a file without quotation marks?

description: Load a nested list from a file without quotation marks


code: { k: v['id1'] for k,v in a.items() if 'id1' in v }

rawDescription: Filter a dict of dict

description: Filter a dict of dict


code:
from ordered_set import OrderedSet
dir = OrderedSet(['abc','dsw','lol'])
dir[0]

rawDescription: how to get the key or values one by one from the dictionary?

description: Get the key or values one by one from the dictionary


code:
# an N x 5 array containing a regular mesh representing the stimulus params
stim_params = np.mgrid[:2, :3, :4, :5, :6].reshape(5, -1).T

# an N x 3 array representing the output values for each simulation run
output_vals = np.arange(720 * 3).reshape(720, 3)

# shuffle the rows for a bit of added realism
shuf = np.random.permutation(stim_params.shape[0])
stim_params = stim_params[shuf]
output_vals = output_vals[shuf]

rawDescription: Reorganizing an MxN 2D array of datapoints into an N-dimensional array


code:
df
df.dtypes

rawDescription: Filtering all rows with NaT in a column in Dataframe python


code:
with open(name, 'w') as o:
    o.write(code)

rawDescription: Input to a seperate file (let the user write text in a python program as it writes to a file)


rawDescription: Remove elements from the array

description: Remove elements from the array


code:
# create some fake data--a 2D NumPy array having 10,000 rows and 10 columns
D = NP.random.randn(10000 * 10).reshape(1000, 10)
# import the BallTree class (here bound to a local variable of same name)
from sklearn.neighbors import BallTree as BallTree
# call the constructor, passing in the data array and a 'leaf size'
# the ball tree is instantiated and populated in the single step below:
BT = BallTree(D, leaf_size=5, p=2)
# 'leaf size' specifies the data (number of points) at which
# point brute force search is triggered
# 'p' specifies the distance metric, p=2 (the default) for Euclidean;
# setting p equal to 1, sets Manhattan (aka 'taxi cab' or 'checkerboard' dist)
type(BT)

rawDescription: Most efficient way to calculate pairwise similarity of 250k lists


code:
import collections
Vertex = collections.namedtuple("Vertex", ["idx", "depth"])

rawDescription: Traversing a Graph in Breadth First in Python Using a List of List


rawDescription: Grabbing brackets and everything inside Regex


code:
from itertools import takewhile
name = "QWA_0230"

beg_name = "".join(takewhile(str.isalpha,name))
print(beg_name)
QWA

name2 = "QW0210"

beg_name = "".join(takewhile(str.isalpha,name2))
print(beg_name)
QW

rawDescription: Python Name Abreviation finding

description: Python Name Abreviation finding


code:
import ansible.utils as utils
import ansible.errors as errors
from ansible.plugins.lookup import LookupBase
import socket

class LookupModule(LookupBase):

    def __init__(self, basedir=None, **kwargs):
        self.basedir = basedir

    def run(self, terms, variables=None, **kwargs):

        hostname = terms[0]

        if not isinstance(hostname, basestring):
            raise errors.AnsibleError("ip lookup expects a string (hostname)")

        return [socket.gethostbyname(hostname)]

rawDescription: Arbitrary host name resolution in Ansible


code: urllib.quote('an Immunodeficiency Virus-1 replication by inhibiting acetylation of NF-\xce\xbaB')

rawDescription: Passing a string containing unicode to a RESTful API


rawDescription: How to query pandas dataframe using a specific value?

description: Query pandas dataframe using a specific value


code:
settings = dict({
    "template_path": os.path.join(os.path.dirname(__file__),"templates"),
    "static_path": os.path.join(os.path.dirname(__file__),"static"),
    "cookie_secret": "make it harde to guess ;) ",
    "xsrf_cookies": True,
    "debug": False,
    "gzip": True,
    "login_url": "/#login",
    "site_url":"http://localhost:8000",
})

rawDescription: WebService with Tornado.web and Authentication. Do you have Example?


code:
d['get'] = 'baz'
d.get('get')

rawDescription: How would I implement a dict with Abstract Base Classes in Python?


code:
for k, v in dict.iteritems():
    setattr(self, k, v)

rawDescription: Divide a dictionary into variables

description: Divide a dictionary into variables


code:
def Beta(x):
    y = x + 0.5
    return y
Beta.input_stype = "Float"

rawDescription: Get a python function attribute without running the function


code:
a = [['txt','stxt','pi','min','max'],['txt1','stxt1','pi1','min1','max1']]
b = [[0.45,1.23],[0.75,1.53]]
from xml.etree import ElementTree as ET
root =  ET.Element("xml")
for l1 in zip(a,b):
        sroot_root = ET.Element("Class ",name = l1[0][0])
        doc = ET.SubElement(sroot_root, "subclass" , name = l1[0][1])
        ET.SubElement(doc, l1[0][4], min = str(l1[1][0]),max = str(l1[1][1]))
        root.append(sroot_root)

tree = ET.ElementTree(root)
tree.write("test.xml")

rawDescription: Create XML file by iterating over lists in python


code:
arrayResult = []
for name in combined:
  arrayResult.append([ name ] + combined[name])

rawDescription: Merging 2d arrays

description: Merging 2d arrays


code:
exit = False
while not exit:
    option = raw_input("What would you like to do?")
    if option == "exit":
        exit = True

rawDescription: How do you define a variable in one function and change it in another with the first function recognizing it

description: Define a variable in one function and change it in another with the first function recognizing it


code:
main_list = []
second_list = []

def MyFunc():
    global main_list
    global second_list
    main_list = ['more', 'good', 'stuff']
    second_list = ['another', 'list']

MyFunc()

print(main_list) # ['more', 'good', 'stuff']
print(second_list) # ['another', 'list']

rawDescription: items not appending to list, or list returning as empty in python?

description: Items not appending to list, or list returning as empty in python


code:
from asyncio import iscoroutinefunction
iscoroutinefunction(g)

rawDescription: Why isn't a function declared as async of type types.CoroutineType?


code: df1.merge(df2, left_on=['year', 'name'], right_on=['date4', 'name'])

rawDescription: pandas dataframe join with where restriction

description: Pandas dataframe join with where restriction


rawDescription: Scrapy: How to extract all words that are inside HTML-tags?


rawDescription: Reverse AND inverse python3.x OrderedDict efficiently


code:
propfaid = h5py.h5p.create(h5py.h5p.FILE_ACCESS)
settings = list(propfaid.get_cache())
print(settings)
# [0, 521, 1048576, 0.75]

settings[2] *= 5
propfaid.set_cache(*settings)
settings = propfaid.get_cache()
print(settings)
# (0, 521, 5242880, 0.75)

rawDescription: How to set cache settings while using h5py high level interface?

description: Set cache settings while using h5py high level interface


code:
year_hour_means = df1.groupby(lambda x: (x.year, x.hour)).mean()
year_hour_means

rawDescription: Getting the average of a certain hour on weekdays over several years in a pandas dataframe


code:
order = np.lexsort(XYZ.T)
diff = np.diff(XYZ[order], axis=0)
uniq_mask = np.append(True, (diff != 0).any(axis=1))

rawDescription: Summing and removing repeated elements of Numpy Arrays


code:
for filename in os.listdir('.'):
    if fnmatch.fnmatch(filename, 'buffer*'):
        os.remove(filename)
    # etc.

rawDescription: Delete files with different names in Python


code:
word_features = get_word_features(get_words_in_tweets(tweets))
with open('wordFeatures.csv', 'w') as output:
    for ind, item in enumerate(word_features, 1):
        output.write("{}:{}\n".format(ind, item))

rawDescription: Python export csv data into file

description: Python export csv data into file


code: [item for item in list1 if item not in list2]

rawDescription: Find the element "left" in a list

description: Find the element "left" in a list


code:
from twisted.internet.defer import Deferred
from twisted.conch.scripts.cftp import ClientOptions
from twisted.conch.client.connect import connect
from twisted.conch.client.default import SSHUserAuthClient, verifyHostKey

def sftp(user, host, port):
    options = ClientOptions()
    options['host'] = host
    options['port'] = port
    conn = SFTPConnection()
    conn._sftp = Deferred()
    auth = SSHUserAuthClient(user, options, conn)
    connect(host, port, options, verifyHostKey, auth)
    return conn._sftp

rawDescription: twisted conch filetransfer

description: Twisted conch filetransfer


code:
BeautifulSoup(html).select("i.fa.fa-clock-o + span")
BeautifulSoup(html).select("i.fa.fa-clock-o + span")[0].text

rawDescription: Extract data with BeautifulSoup

description: Extract data with BeautifulSoup


code:
import struct
a = struct.pack('BBBB', 255, 253, 99, 0)
b = struct.unpack('>i', a)
b[0] / 256

rawDescription: Python: How to make integers from a stream of character data?


code:
import sys

# Map accel values to log levels
levels = (
    (10.0, logging.CRITICAL),
    (2.0, logging.WARNING),
    (1.0, logging.INFO),
    (-sys.maxsize, logging.DEBUG)
)

logging.log(next(lev[1] for lev in levels if lev[0] <= accel),
    "Collision with %0.1fG impact.", accel)

rawDescription: Python logging level based on variable value?


code:
#!/usr/bin/env python
"""Command.

Usage:
  command start ID
  command finish ID FILE
  command (-h | --help)
  command (-v | --version)

Arguments:
  ID
  FILE     input file

Options:
  -h --help     Show this screen.
  -v --version  Show version.

"""
from docopt import docopt
from schema import Schema, Use, Or, SchemaError

if __name__ == '__main__':
    args = docopt(__doc__, version='command alpha')

    id_schema = Use(int, error='ID should be an int')
    file_schema = Or(None, Use(open, error='FILE should be readable'))
    try:
        args['ID'] = id_schema.validate(args['ID'])
        args['FILE'] = file_schema.validate(args['FILE'])
    except SchemaError as e:
        exit(e)

    print(args)

rawDescription: docopt + schema validation

description: Docopt + schema validation


code: struct.unpack('<i', chunk + ('\0' if chunk[2] < 128 else '\xff'))

rawDescription: How to read integers from a file that are 24bit and little endian using Python?

description: Read integers from a file that are 24bit and little endian using Python


code:
import datetime

text.delete('1.0', 'end')
text.insert('end', datetime.datetime.now().strftime('%H:%M:%S')) # text.insert('end', label['text'])

rawDescription: can we code time in text widget?

description: Can we code time in text widget


code:
import os
import re

PACKAGE_NAME = 'thepackage'
HERE = os.path.abspath(os.path.dirname(__file__))
INIT = open(os.path.join(HERE, PACKAGE_NAME, '__init__.py')).read()
README = open(os.path.join(HERE, 'README.md')).read()

VERSION = re.search("__version__ = '([^']+)'", INIT).group(1)

rawDescription: setuptools dependency and import issue

description: Setuptools dependency and import issue


code:
from numpy.core.umath_tests import inner1d
dist = inner1d(deltas, deltas)

rawDescription: more efficient way to calculate distance in numpy?

description: More efficient way to calculate distance in numpy


code: get_ipython().display_formatter.formatters['text/plain'].for_type(int, lambda n, p, cycle: p.text("%X" % n))

rawDescription: How can I configure ipython to display integers in hex format?

description: Configure ipython to display integers in hex format


code:
headers = ['A', 'BC', 'HC', '3/D']
process = [ lambda params: int(params[0]),
            lambda params: int(params[1]+params[2]),
            lambda params: float('%.2f' % (params[2]/2)),
            lambda params: float('%.2f' % (3. / params[3]))]

rawDescription: A simple interpreter in python for generating report


rawDescription: python regex: get end digits from a string

description: Python regex: get end digits from a string


code: [('NORTHLANDER', 3), ('VOLT', 3)]

rawDescription: any python min like function which gives a list as result

description: Any python min like function which gives a list as result


code:
def find_latest(lst):
    prefixes = {}
    for entry in lst:
        code, value = entry[:2], int(entry.partition('.')[0][2:])
        if value > prefixes.get(code, (float('-inf'), ''))[0]:
            prefixes[code] = (value, entry)
    return [entry for value, entry in prefixes.values()]

rawDescription: maximum value of a unique string in a list

description: Maximum value of a unique string in a list


code:
common_string = ', '.join(common_words) + ';'
with open("common.txt", 'w') as common_file:
    common_file.write(common_string)

rawDescription: Merge two lines with grep or sed

description: Merge two lines with grep or sed


code:
a = ['a','b','c','d','e']
b = ['c','a','dog']
a_dict = dict((v, i) for i, v in enumerate(a))
sorted(b, key=lambda v: a_dict.get(v, float('inf')))

rawDescription: Sort python list with order of another, larger list


code:
graph_by_common_users = common_users.pivot(index='pair of websites', columns='used_at', values='common users')
#sort by column 2014
graph_by_common_users = graph_by_common_users.sort_values(2014, ascending=False)

ax = graph_by_common_users.plot(kind='barh', width=0.5, figsize=(10,20))
[label.set_rotation(25) for label in ax.get_xticklabels()]

rects = ax.patches
labels = [int(round(graph_by_common_users.loc[i, y])) for y in graph_by_common_users.columns.tolist() for i in graph_by_common_users.index]
for rect, label in zip(rects, labels):
    height = rect.get_height()
    ax.text(rect.get_width() + 3, rect.get_y() + rect.get_height(), label, fontsize=8)

rawDescription: Different sequence of names with pandas

description: Different sequence of names with pandas


code:
df = DataFrame({'A' : np.arange(5)},index=pd.date_range('20130101 09:00:00',freq='3T',periods=5))
df
df.index.freq

rawDescription: Python pandas dataframe - any way to set frequency programmatically?


code: (packet_jiffy / HZ) + btime

rawDescription: Convert "jiffy" time

description: Convert "jiffy" time


code:
class Person(object):
    def __init__(self,id,name,*items):
        self.id=id
        self.name=name
        self.items=set(items)

rawDescription: python list of dicts how to merge key:value where values are same?

description: Python list of dicts how to merge key:value where values are same


code:
import color

class Color(color.Color):
    ...

rawDescription: How to extend a class in python?

description: Extend a class in python


code:
import cPickle as pickle

primes_set = set([2,3,5])

pickle.dump(primes_set, open('primes_set.pkl', 'wb'))

my_primes = pickle.load(open('primes_set.pkl', 'rb')) # to load the object

rawDescription: How to save a list and access values in a list in Python--- .txt file

description: Save a list and access values in a list in Python--- .txt file


code:
# content of test_plat.py

import pytest

@pytest.mark.osx
def test_if_apple_is_evil():
    pass

@pytest.mark.linux2
def test_if_linux_works():
    pass

@pytest.mark.win32
def test_if_win32_crashes():
    pass

def test_runs_everywhere_yay():
    pass

rawDescription: Does pytest support "default" markers?

description: Does pytest support "default" markers


code:
form = TaskForm()
form.fields['owner'].queryset = Task.objects.filter(owner=request.user)

rawDescription: How to get the foreign keys related to drop down only of that user in Django ModelForm

description: Get the foreign keys related to drop down only of that user in Django ModelForm


code:
import numpy as np

lc=np.array([[1, 4, 3],
            [3, 2, 3],
            [1, 3, 3]])

la=np.array([[0.3, 0.8, 2.0],
            [5.0, 1.5, 0.5],
            [0.1, 1.0 ,3.2]])

calc_areas = []

for v in np.unique(lc):
    print(v, np.sum(la[lc==v]))
    calc_areas.append([v, np.sum(la[lc==v])])

calc_areas.sort(key=lambda v: v[1], reverse=True)
print("Max area", calc_areas[0])

rawDescription: Sum land area grid based on land class grid in Python


code:
regex = re.compile(r'(b)lack', flags=re.IGNORECASE)
regex.sub(r'\1***', s)

rawDescription: How to replace multiple words in a string through Python in a case insensitive way?

description: Replace multiple words in a string through Python in a case insensitive way


rawDescription: Convert string of 0s and 1s to byte in Python


rawDescription: python: iterate through dynamic list

description: Python: iterate through dynamic list


code:
seperators = re.compile(r'.*?(?:%s|$)' % '|'.join(a))
e = seperators.findall(text)

rawDescription: Replacing characters in a long string to split it, to keep the 'seperators'


code:
def combinations(basis, howmany):
    for index in range(0, len(basis) - howmany + 1):
        if howmany == 1:
            yield [basis[index]]
        else:
            this, remainder = basis[index], basis[index+1:]
            for rest in combinations(remainder, howmany - 1):
                yield [this] + rest

rawDescription: All 6-Number Permutations from a List

description: All 6-Number Permutations from a List


rawDescription: Python Array Index - Every X elements?

description: Python Array Index - Every X elements


code:
def get_enzyme_cogs(enzyme, gene_cog_dict):
    """Get all COGs of which there is at least one member gene in the enzyme."""
    cog_list = []
    for gene in enzyme:
        cog_list.extend(gene_cog_dict[gene])
    return set(cog_list)

def get_gene_by_gene_cogs(enzyme, gene_cog_dict):
    """Get COG memberships for each gene in enzyme."""
    cogs_list = []
    for gene in enzyme:
        cogs_list.append(set(gene_cog_dict[gene]))
    return cogs_list

def add_gene(target_enzyme_cogs, gene_cog_dict, cog_gene_dict, proposed_enzyme = None, fulfilled_cogs = None):
    """Generator for all enzymes with membership of all target_enzyme_cogs, without duplicate enzymes or redundant genes."""

    base_enzyme_genes = proposed_enzyme or []
    fulfilled_cogs = get_enzyme_cogs(base_enzyme_genes, target_enzyme_cogs, gene_cog_dict)

    ## Which COG will we try to find a member of?
    next_cog_to_fill = sorted(list(target_enzyme_cogs-fulfilled_cogs))[0]
    gene_members_of_cog = cog_gene_dict[next_cog_to_fill]

    for gene in gene_members_of_cog:

        ## Check whether any already-present gene's COG set is a subset of the proposed gene's COG set, if so skip addition
        subset_found = False
        proposed_gene_cogs = set(gene_cog_dict[gene]) & target_enzyme_cogs
        for gene_cogs_set in get_gene_by_gene_cogs(base_enzyme_genes, target_enzyme_cogs, gene_cog_dict):
            if gene_cogs_set.issubset(proposed_gene_cogs):
                subset_found = True
                break
        if subset_found:
            continue

        ## Add gene to proposed enzyme
        proposed_enzyme = deepcopy(base_enzyme_genes)
        proposed_enzyme.append(gene)

        ## Determine which COG memberships are fulfilled by the genes in the proposed enzyme
        fulfilled_cogs = get_enzyme_cogs(proposed_enzyme, target_enzyme_cogs, gene_cog_dict)

        if (fulfilled_cogs & target_enzyme_cogs) == target_enzyme_cogs:
            ## Proposed enzyme has members of every required COG, so yield
            enzyme = deepcopy(proposed_enzyme)
            proposed_enzyme.remove(gene)
            yield enzyme
        else:
            ## Proposed enzyme is still missing some COG members
            for enzyme in add_gene(target_enzyme_cogs, gene_cog_dict, cog_gene_dict, proposed_enzyme, fulfilled_cogs):
                yield enzyme

rawDescription: Python: Find subsets across many-to-many mapping


code:
return (method_returning_json()
          ['LongFieldGroup1']
          ['FieldGroup2']
          ['FieldGroup3']
          ['LongValue'])

rawDescription: pep8 compliance with long json dictionary lookup?

description: Pep8 compliance with long json dictionary lookup


code:
changes = {"cow":"gives milk", "dog":"bark"}

my_list = [item if item not in changes else "_".join([item, changes[item]])
           for item in my_list]

rawDescription: append/insert a value/string to a list element dynamically in Python?

description: Append/insert a value/string to a list element dynamically in Python


code:
import wx
wx.WXK_BACK

rawDescription: wxPython wx.KeyEvent GetKeyCode()

description: WxPython wx.KeyEvent GetKeyCode()


code:
def parse(self, response):
    requests = []
    for i in range(175):
        url = "http://b2.nourl.com/dp.asp"
        payload = {"AppKey": "94921000e1999f84a518725", "ComparisonType1_1": "LIKE", "Value1_1": "", "MatchNull1_1" : "N", "ComparisonType2_1" : "LIKE", "MatchNull2_1" : "N", "Value2_1" : "", "ComparisonType3_1": "=", "MatchNull3_1" : "N", "Value3_1" : "", "x":"69", "y":"27", "FieldName1" : "County", "Operator1": "OR", "NumCriteriaDetails1": "1", "Operator2" : "OR", "NumCriteriaDetails2" : "1", "FieldName3": "Year", "Operator3" : "OR", "NumCriteriaDetails3": "1", "PageID" : "2", "GlobalOperator": "AND", "NumCriteria" : "3", "Search" : "1", "cpipage": "%i" %i}
        requests.append(FormRequest(url, formdata = payload, callback = self.parse_data))
    return requests

rawDescription: Python - Scrapy, in a loop

description: Python - Scrapy, in a loop


code:
class BForm(forms.ModelForm):
    # either redefine the boolean field
    boolean_field = models.BooleanField(initial=False)

    class Meta:
        model = B

    # or override the __init__ method and set initial=False
    # this is a bit more complicated but less repetitive
    def __init__(self, *args, **kwargs):
        super(BForm, self).__init__(*args, **kwargs)
        self.fields['boolean_field'].initial = False

rawDescription: change model admin form default value

description: Change model admin form default value


code: [x * P for x in S]

rawDescription: Python - multiple list by a scalar

description: Python - multiple list by a scalar


code: indices_to_set = numpy.where(temp_board[index] == 0)

rawDescription: Numpy masked array modification

description: Numpy masked array modification


code: return int(frac * (2 ** adc_res) + 0.5)

rawDescription: What is causing a negative bias in my super-sampling simulation?


code: from __future__ import print_function

rawDescription: How do you find where a print statement is located?

description: Find where a print statement is located


code:
def start_requests(self):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36'}
    for i,url in enumerate(self.start_urls):
        yield Request(url,cookies={'over18':'1'}, callback=self.parse_item, headers=headers)

rawDescription: How to send cookie with scrapy CrawlSpider requests?

description: Send cookie with scrapy CrawlSpider requests


code:
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.tables import Table
cm = 2.54

def print_pdf(modeladmin, request, queryset):
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'

    elements = []

    doc = SimpleDocTemplate(response, rightMargin=0, leftMargin=6.5 * cm, topMargin=0.3 * cm, bottomMargin=0)

    data=[(1,2),(3,4)]
    table = Table(data, colWidths=270, rowHeights=79)
    elements.append(table)
    doc.build(elements)
    return response

rawDescription: How to make a simple table in ReportLab

description: Make a simple table in ReportLab


rawDescription: Merge three csv files with same headers in Python


code:
fsSchema = {'published': {'renders': {'SIM': ('fold1', 'fold2'), 'REN': ('fold1', 'fold2')}}}
list(flatten(fsSchema))

rawDescription: Concatenate sequence from a predefined datastructure


code:
A=[[[] for i in range(3)] for i in range(3)]
for i in range(3):
  for j in range(3):
    number=int(input("Please Enter Elements of Matrix A:"))
    A[i][j]=number

rawDescription: Creating a 3x3 matrix with user input numbers in python


code:
import pandas as pd
import matplotlib.pyplot as plt
#import seaborn as sns
import numpy as np

fig, ax = plt.subplots()
series = pd.Series(np.random.normal(0, 100, 1000))
series.plot(kind='hist', bins=50, ax=ax)
ax.annotate("",
            xy=(300, 5), xycoords='data',
            xytext=(300, 20), textcoords='data',
            arrowprops=dict(arrowstyle="->",
                            connectionstyle="arc3"),
            )

rawDescription: Labelling a matplotlib histogram bin with an arrow


code:
linfnorm = norm(X, axis=1, ord=np.inf)
X.astype(np.float) / linfnorm[:,None]

rawDescription: Normalizing rows of a matrix python

description: Normalizing rows of a matrix python


code: sum(mayorPowers, [])

rawDescription: Adding Lists Elements to 'Mega List'

description: Adding Lists Elements to 'Mega List'


rawDescription: What's a faster operation, re.match/search or str.find?


code:
rules = {4: {'s': 'No', 'a': 'e', 'p': 'No', 'c': 'No', 'l': 'e', 'f': 'No', 'm': 'No', 'n': {19: {'o': 'e', 'r': 'No', 'b': 'e', 'h': 'e', 'w': {21: {'p': 'e', 'w': 'e', 'd': {1: {'s': 'e', 'f': 'No', 'y': 'No'}}, 'g': 'e', 'l': {2: {'c': 'e', 'w': 'No', 'n': 'e', 'y': 'No'}}}}, 'k': 'e', 'n': 'e', 'y': 'e'}}, 'y': 'No'}}
string_rules = [] # to keep them in a list

def print_nested_count(myDict, parent=None, out=[], count=0):
    for key, value in myDict.items():
        if isinstance(value, dict):
            count += 1  # signifies recursion level
            if parent and count % 2 == 0:
                out.append("<{} = {}> AND ".format(parent, key))
            count = print_nested_count(value, key, out, count=count)
        else:
            if count:
                string_rules.append("".join(out).lstrip() + "<{} = {}> ----> {}".format(parent, key, value))
    if count % 2 == 0 and out:
        out.pop()
    count -= 1
    return count

rawDescription: Recursively getting all nested elements from a Dictionary


code: [tuple(indices[i] for i in sub) for sub in List2]

rawDescription: Replacing elements in a list with the indices of a list containing all unique elements


code:
def float_or_na(value):
    return float(value) if value != 'N/A' else None

rawDescription: Converting string to float python N/A

description: Converting string to float python N/A


code:
Mon - Wed, Fri - Sat
Mon - Wed, Fri - Sun
Mon - Tue, Sun
Wed - Thu, Sun
Wed, Fri - Sat

rawDescription: Converting for loops to recursion [finding consecutive weekdays]


code:
foo.__self__
is_unbound_method(foo)
MyClass.bar.__self__
is_unbound_method(MyClass.bar)
MyClass().bar.__self__
is_unbound_method(MyClass().bar)

rawDescription: How to test if an object is a function vs. an unbound method?

description: Test if an object is a function vs. an unbound method


code:
def chart_selector(slide, df_table, chart_type, *args, **kwargs):
    if chart_type == "bar":
        add_bar_chrt(slide, df_table, *args, **kwargs)

rawDescription: Python: setting a parameter for a function within a function


code:
x = [['UNITED STATES', 'OHIO, USA'],
     ['NEW YORK, USA', 'UNITED STATES'],
     ['LYON, FRANCE', 'FRANCE', '0'],
     ['FRANCE']]
zero = [i for i, row in enumerate(x) if '0' in row]

rawDescription: Placing a for loop within map?

description: Placing a for loop within map


code:
import sys
print("Welcome, master!")
sys.ps1 = "How may I serve you? "
sys.ps2 = "                 ... "

rawDescription: Where can I find the first text that loads in the Python shell and change it?


code: [' sodium propionate', '  sodium cacodylate', '  bis-tris propane', ' pH 8.0 ']

rawDescription: Regex and python to remove a certain format of string


code:
x = [(lambda x=x: x) for x in range(3)]
x[0]()

rawDescription: `x = [lambda: x for x in range(3)]`, what will `x[0]()` return?


code:
def dt_parse(date_string):

    dt = pd.datetime.strptime(date_string, '%Y %m %d %H')

    return dt

rawDescription: Parse dates when year month day and hour are in separate columns using pandas in python


code:
#import ujson
#import pprint as pp
import yaml
import pandas as pd
from pandas.io.json import json_normalize

def read_yaml(fn):
    with open(fn, 'r') as fi:
        return yaml.load(fi)

def filter_data(data):
    result_data = []
    for x in data:
        if 'id' not in x:   continue
        if 'fec' not in x['id']:    continue
        if 'thomas' not in x['id']: continue
        result_data.append(x)
    return result_data

fn = 'aaa.yaml'

df = json_normalize(filter_data(read_yaml(fn)), 'terms', [['id', 'fec'], ['id', 'thomas']])
print(df.head())

df.to_csv('out.csv')

rawDescription: Retrieving data from a yaml file based on a Python list


code:
import urllib.request, pprint

data = urllib.request.urlopen('http://www.maxmind.com/app/locate_my_ip')

fields = []
for line in data:
    if b'class=output' in line:
        fields.append(next(data).decode('iso-8859-1').strip())
data.close()

rawDescription: removing blank lines python

description: Removing blank lines python


code:
from xmlrpclib import ServerProxy
from xmlrpclib import ProtocolError
try:
    server = ServerProxy("http://admin:admin@localhost:8080/plone")
    server.get_schema('Document')
    return server
except ProtocolError:
    return None

rawDescription: xmlrpclib, wsapi4plone - check username and password

description: Xmlrpclib, wsapi4plone - check username and password


rawDescription: `pandas.DataFrame.apply` in a row by row operation


code:
def log_iterator(logger_names):
    '''
    Get an iterator that returns LogRecord objects whenever a log message is made.

    logger_names can be:
        a string identifying which logger you want to get messages iterate from
        a list identifying all the loggers you want to get messages from
        a dict<name: level> where level identifies the minimum loggling level
            you want (default is INFO)
    '''
    if isinstance(logger_names, str):
        logger_names = {logger_names: logging.INFO}
    elif isinstance(logger_names, (list, tuple)):
        logger_names = {name: logging.INFO for name in logger_names}

    log_message_event = threading.Event()
    for logger_name, log_level in logger_names.iteritems():
        logger = logging.getLogger(logger_name)
        logger.addHandler(FireOnLog(log_message_event, log_level))

    while True:
        log_message_event.wait()
        record = log_message_event.record
        log_message_event.clear()
        yield record

class FireOnLog(logging.StreamHandler):
    '''
    Custom loghandler that fires an event every time a log message
    comes in.  To use, you need to add this handler to a logger.
    '''

    def __init__(self, event, log_level = logging.INFO):
        '''
        Event is a threading.Event object
        log_level is the minimum logging level you want
        '''
        logging.StreamHandler.__init__(self)
        self._event = event
        self._log_level = log_level

    def emit(self, record):
        if record.levelno >= self._log_level:
            self._event.record = record
            self._event.set()

rawDescription: Python: Have an iterator iterate over log messages on a different thread


code: help(thing)

rawDescription: Difference between these Python structures


code:
import seaborn as sns
import pandas as pd
import numpy as np

x = pd.DataFrame(np.random.lognormal(size=(100, 6)),
             columns=list("abcdef"))

rawDescription: Automatic (whisker-sensitive) ylim in boxplots


code:
import shlex, subprocess
args_str = "./GetHostByVmname.pl –server 10.0.1.191 –username Administrator –password P@ssword1 –vmname RHTest"
args = shlex.split(args_str)
p = subprocess.Popen(args, stdout=subprocess.PIPE)

rawDescription: calling perl script that takes multiple input args in python

description: Calling perl script that takes multiple input args in python


rawDescription: How can I make python's argparse accept any number of [-R a b]s, and aggregate them into a list

description: Make python's argparse accept any number of [-R a b]s, and aggregate them into a list


rawDescription: Why do some arguments need to be defined, and others not? (Learn Python the Hard Way, ex. 25)


code:
sql = 'SELECT apr FROM aprtable WHERE neighborhood = ? and income = ? and risk_factor = ?'
args = ['Allston', 25000, 5]
cursor.execute(sql, args)
row = cur.fetchone()
if row is not None:
    apr = row[0]

rawDescription: Dynamically fill a python dictionary

description: Dynamically fill a python dictionary


code:
d = ImageDict()
d[3]
d[3]
d['bleh']
d['bleh']

rawDescription: Smart caching of expensive objects in Python


code:
def runcmd(self, prog, args, inputs):
    cmd = " ".join((prog, args))
    si = subprocess.STARTUPINFO()
    si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    process = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, startupinfo=si)
    return(process.communicate(input=inputs))

rawDescription: Python - ssh with password to foreign computer without using non-built-in modules


code:
grouped = df.groupby(["FIRST_NM", "LAST_NM"])
grouped.grouper.group_info[0]

rawDescription: Create a column with a random number for each exact match of two columns in pandas dataframe


code:
# row zero takes up all extra vertical space
# column 0 and column 1 take an equal amount of all horizontal space
self.root.grid_rowconfigure(0, weight=1)
self.root.grid_columnconfigure(0, weight=1)
self.root.grid_columnconfigure(1, weight=1)

f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")
f3.grid(row=1, column=0, columnspan=2, sticky="ew")

rawDescription: Python tkinter place put frame to the bottom


code:
seen = set()
new_a, new_b = [], []
for x, y in zip(a, b):
    if x not in seen:
        new_a.append(x)
        new_b.append(y)
        seen.add(x)

new_a
new_b

rawDescription: Remove duplicate from two lists without using loop?


rawDescription: datetime format change when save to csv file python

description: Datetime format change when save to csv file python


code:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

browser = webdriver.Firefox() # Get local session(use webdriver.Chrome() for chrome)
browser.get("http://www.example.com") # load page from some url
assert "example" in browser.title # assume example.com has string "example" in title

try:
    # temporarily make parent(assuming its id is parent_id) visible
    browser.execute_script("document.getElementById('parent_id').style.display='block'")
    # now the following code won't raise ElementNotVisibleException any more
    browser.find_element_by_xpath("//input[@itemcode='XYZ']").send_keys(1)
    # hide the parent again
    browser.execute_script("document.getElementById('parent_id').style.display='none'")
except NoSuchElementException:
    assert 0, "can't find input with XYZ itemcode"

rawDescription: Write value to hidden element with selenium python script


code:
pat = re.compile('\\\\##### To extract from here ####'
                 '.*?'
                 '(?=\\\\##### To extract till here ####)',
                 re.DOTALL)

rawDescription: how to match lines between pattern using re module in python

description: Match lines between pattern using re module in python


code:
def indices(start, end):
    lens = end - start
    np.cumsum(lens, out=lens)
    i = np.ones(lens[-1], dtype=int)
    i[0] = start[0]
    i[lens[:-1]] += start[1:]
    i[lens[:-1]] -= end[:-1]
    np.cumsum(i, out=i)
    return i

rawDescription: Construct Numpy index given list of starting and ending positions


code:
def can_access(f):
    if not hasattr(f, 'access_control'):
        return True

    # Use the access_control member set by the decorator
    return _eval_access(**f.access_control) == AccessResult.ALLOWED

rawDescription: Hiding inaccessible links in Jinja2 templates


code:
def average(files, newfile):
    import csv
    dict={}
    ln=[]
    max=0
    for x in files:
        with open(x+'.csv', 'rb') as file:
            reader = csv.reader(file, delimiter=',')
            l=[]
            for y in reader:
                l.append(y)
            dict[x]=l
            ln.append(x)

rawDescription: averaging datasets of varying length

description: Averaging datasets of varying length


code:
if not app.debug:
    import logging
    from themodule import TheHandlerYouWant
    file_handler = logging.handlers.SysLogHandler(address="Instance address")
    file_handler.setLevel(logging.WARNING)
    app.logger.addHandler(file_handler)

rawDescription: Two Flask Instances, One Console Message


code:
img = np.array(img)
colour_mask = img == 1  # or whichever colour you want
per_col_count = colour_mask.sum(axis=0)
std = np.std(per_col_count)

rawDescription: Perform a Standard Deviation on the values in a dictionary


code:
item = stats[field] if field in stats else very_expensive_initializer(field)
item.add(value)

rawDescription: How do I initialize values in a dictionary?

description: Initialize values in a dictionary


rawDescription: Calculating velocities and acceleration in python for large numpy arrays


code:
from functools import partial
def foo3(a, b, c):
    pass
foo4 = partial(foo3, c=1)
foo4.args, foo4.keywords
from inspect import getargspec
getargspec(foo4.func)

rawDescription: Get argument names of callable objects in the way they are called


code: ws1['D18'] = equipment.get()

rawDescription: Saving an OptionMenu selection to a variable in Tkinter for python 2.7


code: my_funcs=[mk_func(i) for i in (1, 3, 4, 7)]

rawDescription: An elegant way to make a list of functions in python


code:
# Some cell at the begining of the notebook
In [2]: HTML('''<style>
                    .right_aligned_df td { text-align: right; }
                    .left_aligned_df td { text-align: right; }
                    .pink_df { background-color: pink; }
                </style>''')

...

# Much later in your notebook
In [66]: HTML(df.to_html(classes='pink_df'))

rawDescription: How to format IPython html display of Pandas dataframe?

description: Format IPython html display of Pandas dataframe


rawDescription: Can polymorhpic_on be used over more than one column?


code:
#!/usr/bin/python
# encoding: utf-8

import sys
import csv
from ConfigParser import ConfigParser

ini = ConfigParser()
ini.readfp(sys.stdin)

#Find all keys in the INI file to build a row template and
#include a "game" field to store the section name.
rowTemplate = {"game":""}
for sec in ini.sections():
   for key,value in ini.items(sec):
       rowTemplate[key] = ""

#Write the CSV file to stdout with all fields in the first line
out = csv.writer(sys.stdout)
out = csv.DictWriter(sys.stdout, fieldnames=rowTemplate.keys())
out.writeheader()

#Write all rows
for sec in ini.sections():
   row = rowTemplate.copy()
   row["game"] = sec
   for key,value in ini.items(sec):
       row[key] = value
   out.writerow(row)

rawDescription: How to convert an INI file into an CSV file

description: Convert an INI file into an CSV file


code: {u'hello': u'world'}

rawDescription: How do you read in a text (.txt) file as a .py file in Python 2.7?

description: Read in a text (.txt) file as a .py file in Python 2.7


code:
s = 'this string is soooo\
     ooo loooong'

rawDescription: Line breaks in code within print statement causing spaces in values printed to screen


code:
class Foo(object):
    def __init__(self, bar, baz):
        self.bar = bar
        self.baz = baz

    def __eq__(self, other):
        if isinstance(other, type(self)):
            return self.bar == other.bar and self.baz == other.baz
        return False

    def __hash__(self):
        return hash(self.bar) ^ hash(self.baz)

rawDescription: Creating hashtable in Python 3 for my class


code:
sequence = "MSSPPPARSGFYRQEVTKTAWEVRAVYRDLQ"
fragments = [split_into_five_character_segments(s) for s in shrink(sequence)]

rawDescription: how can I generate a fragment library from a protein sequence file?

description: Generate a fragment library from a protein sequence file


code:
import somemodule
reload(somemodule)
somemodule.foo()

rawDescription: Python twisted web server caching and executing outdated code


code: print("In {} days, you'll have {}".format(*days(100)))

rawDescription: How can I count and display my recursive steps in Python?

description: Count and display my recursive steps in Python


code:
x=np.random.rand(2,2)
x.__array_interface__['data']
x[0].__array_interface__['data']
x[0,:].__array_interface__['data']
x[1,:].__array_interface__['data']

rawDescription: Numpy matrix row/column memory layout

description: Numpy matrix row/column memory layout


code: (x[:, np.newaxis] * y).T

rawDescription: Multiplication of two arrays in numpy

description: Multiplication of two arrays in numpy


code:
def mouse_moved(self, mouse_event):
    if mouse_event.inaxes:
        x, y = mouse_event.xdata, mouse_event.ydata
        self.mouse_moved_signal.emit(x,y)

rawDescription: matplotlib gui imshow coordinates

description: Matplotlib gui imshow coordinates


code:
def f(x, y=1, z=2, *args, **kwargs): pass
getargspec(f)
def g(x, y, z): pass
getargspec(g)

rawDescription: Why is inspect.getargspec() return unsized objects?


code:
data_files   = [ ("my_module",  ["local/lib/python2.7/dist-package/my_module/data1",
                                 "local/lib/python2.7/dist-package/my_module/data2"])]

rawDescription: setup.py not installing data files

description: Setup.py not installing data files


code:
import uuid
import os

def get_file_path(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s.%s" % (uuid.uuid4(), ext)
    return os.path.join('uploads/logos', filename)

rawDescription: Enforce unique upload file names using django?


code:
import csv
sample = open('~/sample.txt')
adr = csv.reader(sample, delimiter='|')
longest = 0
for row in adr:
    curLen = len(row[4].split(','))
    if curLen > longest:
        longest = curLen

sample.seek(0)
for row in adr:
    new = row[4].split(",")
    for i in range(len(new), longest):
        new.append(' ')
    newrow = row[0:4] + new + row[5:]
    print('|'.join(newrow))

rawDescription: Splitting a delimited file and storing into new column


code:
def img_array_to_single_val(arr, color_codes):
    result = numpy.ndarray(shape=arr.shape[:2], dtype=int)
    result[:,:] = -1
    for rgb, idx in color_codes.items():
        result[(arr==rgb).all(2)] = idx
    return result

rawDescription: Replacing RGB values in numpy array by integer is extremely slow


code: isinstance('hi', str)

rawDescription: Why isn't type(string) equal to "<class 'str'>"?


rawDescription: How to get the first value in a python dictionary

description: Get the first value in a python dictionary


rawDescription: Python: what's an elegant way to select specific rows of an 2-d array into a new array?


code: foo = [key for key, value in some_dict.items() if value['marked']]

rawDescription: Python 2 and 3 compatible way of iterating through dict with key and value


code:
def __init__(self, *args, **kwargs):
    super(MyClass, self).__init__(*args, **kwargs)
    self.__important_fields = ['target_type', 'target_id', 'target_object', 'number', 'chain', 'expiration_date']
    for field in self.__important_fields:
        setattr(self, '__original_%s' % field, getattr(self, field))

def has_changed(self):
    for field in self.__important_fields:
        orig = '__original_%s' % field
        if getattr(self, orig) != getattr(self, field):
            return True
    return False

rawDescription: django - comparing old and new field value before saving

description: Django - comparing old and new field value before saving


code:
def callback(label, instance, *args):
    label.text='kkk'

rawDescription: Kivy - editing label when button clicked


code:
from django.views.generic import ListView

class YourPaginatedListView(ListView):
    ...
    paginate_by = 20

rawDescription: How to iterate over objects in current page in class-based paginated view in Django?

description: Iterate over objects in current page in class-based paginated view in Django


code:
numberlist = [1, 2, 3]
val_list = [4, 5, 6]

mydictionary = dict(zip(numberlist,val_list))

rawDescription: Zip key value pairs in python

description: Zip key value pairs in python


code:
client.create_vertex({'name':'James'}) # single
client.create_vertex([{'name':'James'}, {'name':'Julie'}]) # batch

rawDescription: How to consolidate two Python classes containing methods with identical signatures?

description: Consolidate two Python classes containing methods with identical signatures


code:
def process(data, a_mask=None):
    if a_mask is None:
        a_mask = slice(None)  # every element
        b_mask = slice(0)     # no elements
    else:
        b_mask = -a_mask
    res_a = func_a(data[a_mask])
    res_b = func_b(data[b_mask])
    return res_a, res_b

rawDescription: python/numpy: negate or complement a slice

description: Python/numpy: negate or complement a slice


rawDescription: finding nth combination (incremental approach) of letters (list)

description: Finding nth combination (incremental approach) of letters (list)


code: if queue.empty(): break

rawDescription: Understanding Python Queues and Setting threads to run as a Daemon thread


code: np.savetxt('averaged_spectra.txt', np.transpose([x3,y3]), fmt="%6d  %26.18e")

rawDescription: How can I write arrays to a file with one column of int values and another float

description: Write arrays to a file with one column of int values and another float


code:
from django import forms

class MyValidationForm(forms.Form):
    first = forms.CharField()
    second = forms.IntegerField()
    third = forms.IntegerField()

rawDescription: Django how to validate POST Parameters

description: Django how to validate POST Parameters


code:
pts = np.random.rand(2, 100000)
plt.scatter(*pts, rasterized=True)
plt.savefig('rast.pdf')

rawDescription: matplotlib PdfPages - storing a lossy copy of a plot with lots of data

description: Matplotlib PdfPages - storing a lossy copy of a plot with lots of data


code:
def multiply(a, b):
    num_r, num_c, num_i = len(a), len(b[0]), len(a[0])
    m = [num_c * [None] for r in range(num_r)]
    for r in range(num_r):
        for c in range(num_c):
            x = 0
            for i in range(num_i):
                x += a[r][i] * b[i][c]
            m[r][c] = x
    return m

rawDescription: deleting and inserting values in matrix multiplication python language

description: Deleting and inserting values in matrix multiplication python language


code:
url(r'^location/$', views.LocationListAPIView.as_view(), name='location-list'),
url(r'^location/(?P<pk>[0-9]+)/$', views.LocationDetailAPIView.as_view(), name='location-detail')

rawDescription: How to get a response of multiple objects using rest_framework and Django

description: Get a response of multiple objects using rest_framework and Django


code: mature = age = np.clip(age-20, 0, 1)

rawDescription: Assigning numpy array based on condition


rawDescription: Can't properly split Wi-Fi signal string by space


code: print(text, end='')

rawDescription: Print without line feed

description: Print without line feed


code: python -V

rawDescription: Install python packages on OpenShift

description: Install python packages on OpenShift


code:
# tester.py, run this file

from PyQt4 import QtGui, QtCore
import random, sys, time
from threadHandler import MyThreadHandler

class MyModel(object):
    def dataCruncher(self, val):
        delay = random.randint(1,5)
        print('{} sleeping for {}'.format(val, delay))
        time.sleep(delay) # takes a long time to process data using val
        print('{} done sleeping'.format(val))
        return val

class MainWindow(QtGui.QMainWindow):
    def __init__(self, threadHandler):
        self.app = QtGui.QApplication(sys.argv)
        super(MainWindow, self).__init__()
        self.count = 0
        self.initUI()
        self.button_clicked_events = Event()
        self.threadHandler = threadHandler

    def initUI(self):
        # Layouts
        central = QtGui.QWidget()
        layout = QtGui.QVBoxLayout()

        self.button = QtGui.QPushButton('Press Me')
        self.text = QtGui.QLabel('?')

        layout.addWidget(self.button)
        layout.addWidget(self.text)

        central.setLayout(layout)
        self.setCentralWidget(central)

        self.button.clicked.connect(self._buttonClicked)

    def _buttonClicked(self):
        self.count += 1
        self.button_clicked_events(self.count)

    def setLabel(self, val):
        self.text.setText(str(val))

    def startup(self):
        self.show()
        result = self.app.exec_()
        return result

class Event(list):
    """Event subscription.

    A list of callable objects. Calling an instance of this will cause a
    call to each item in the list in ascending order by index.

    Example Usage:
    >>> def f(x):
    ...     print 'f(%s)' % x
    >>> def g(x):
    ...     print 'g(%s)' % x
    >>> e = Event()
    >>> e()
    >>> e.append(f)
    >>> e(123)
    f(123)
    >>> e.remove(f)
    >>> e()
    >>> e += (f, g)
    >>> e(10)
    f(10)
    g(10)
    >>> del e[0]
    >>> e(2)
    g(2)

    """
    def __init__(self):
        self.output = {}

    def __call__(self, *args, **kwargs):
        for f,key in self:
            output = f(*args, **kwargs)
            self.output[key] = output
        return self.output
    def __repr__(self):
        return "Event({})".format(list.__repr__(self))

if __name__ == '__main__':
    def checker(handler, window):
        if handler.isLastDone():
            val = handler.getLastResult()
            window.setLabel(val)
        else:
            window.setLabel('calculating...')

    random.seed()
    model = MyModel()
    threadHandler = MyThreadHandler()
    myWindow = MainWindow(threadHandler)

    threadHandler.createTimer(1, checker, threadHandler, myWindow)

    def getData(count):
        threadHandler.createOneShot(model.dataCruncher, count)

    myWindow.button_clicked_events.append((getData, 'dt'))

    result = myWindow.startup()
    print('ending')
    threadHandler.end()
    print('ended')
    sys.exit(result)

rawDescription: How can I stop a long-running function when it is called multiple times?

description: Stop a long-running function when it is called multiple times


code:
import numpy as np
from numpy import ma

a = np.random.randn(30, 90, 31, 2, 1)
b = np.random.randn(30, 90, 31, 2, 5)

mask = np.random.randn(30, 90, 31, 2) > 0
ext_mask = mask[..., np.newaxis]

def broadcasting(a=a, b=b, ext_mask=ext_mask):
    mb1 = ma.masked_array(*np.broadcast_arrays(b, ext_mask))

def allocating(a=a, b=b, ext_mask=ext_mask):
    m2 = np.empty(b.shape, dtype=bool)
    m2[:] = ext_mask
    mb2 = ma.masked_array(b, m2)

rawDescription: Extending numpy mask

description: Extending numpy mask


code:
map(float, re.findall(r'-?\d+\.?\d+', s))
list(map(float, re.findall(r'-?\d+\.?\d+', s)))

rawDescription: Splitting a string and adding the digits inside of the string


code:
studentIDToGet = "johnsmith001"
studentsCourses = list()
for course in courses:
    if studentIDToGet in course.studentIDs:
        studentsCourses.append(course.id)

rawDescription: What would you use for `n to n` relations in python?


rawDescription: pd.apply(pd.Series.interpolate) with more arguments

description: Pd.apply(pd.Series.interpolate) with more arguments


code:
print(norm.rvs(size=30))
print(bernoulli.rvs(.3,size=30))
print(poisson.rvs(1,2,size=30))
print(expon.rvs(5,size=30))

rawDescription: How can I generate random numbers in Python?

description: Generate random numbers in Python


rawDescription: Spark: Using iterator lambda function in RDD map()


code:
from functools import wraps

def extract_args(*names, **names_and_processors):
    user_args = ([{"key": name} for name in names] +
        [{"key": key, "type": processor}
            for (key, processor) in names_and_processors.items()])

    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            final_args, final_kwargs = args_from_request(user_args, args, kwargs)
            return f(*final_args, **final_kwargs)
        return wrapper
    return decorator if len(names) < 1 or not is_callable(names[0]) else decorator(names[0])

def args_from_request(to_extract, provided_args, provided_kwargs):
    # Ignoring provided_* here - ideally, you'd merge them
    # in whatever way makes the most sense for your application
    results = {}
    for arg in to_extract:
        result[arg["key"]] = request.args.get(**arg)
    return provided_args, results

rawDescription: Get query string as function parameters on flask


code:
for key in dict:
   if dict[key] > otherDict[key]:
       return False
return True

rawDescription: comparing dictionary values with same keys

description: Comparing dictionary values with same keys


code: [i * j for i, j in enumerate(xrange(60), start=40)]

rawDescription: What is correct python syntax for this kind of list comprehension?


code:
import matplotlib.pyplot as plt

g_title = "Big fat redacted title (Income Distribution)"
g_data_list = [['FIN.RES 47% - $41,888.08', 47],
               ['VSC 40% - $36,019.00', 40],
               ['AFTERMARKET 6% - $5,570.00', 6],
               ['GAP 7% - $6,528.00', 7]]

labels = []
sizes = []
for entry in g_data_list:
    labels.append(entry[0])
    sizes.append(entry[1])

ax = plt.subplot(111)
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.7, box.height])
patches, texts = ax.pie(sizes, startangle=90)
ax.legend(patches, labels, loc='center left',
          bbox_to_anchor=(1, 0.5), fontsize=8)
plt.axis('equal')
plt.suptitle(g_title, fontsize=14)
plt.savefig('test.png', dpi=300)

rawDescription: Move plot and legend in matplotlib

description: Move plot and legend in matplotlib


rawDescription: Align two strings perfectly (python)

description: Align two strings perfectly (python)


code: rows =  session.query(Campaign).all()

rawDescription: Can SQLAlchemy handle unknown polymorphic identities?


code:
hostname = 'dc1dbx1145.dc1.host.com'
hostname.split('.')[1]

rawDescription: How to compare list with actual string in Python?

description: Compare list with actual string in Python


code: product_list = Product.objects.filter(reduce(operator.and_, [Q(colors__name=c) for c in colors]))

rawDescription: Django filter filter with lists

description: Django filter filter with lists


code:
from time import sleep

# The number of seconds since last bought a stock, assumes start is 0
last_sold_stock_time = 0

# This is in seconds
wait_time = 1

# ten minutes in seconds
ten_minutes = 600
while True:
    # Figure out these values however you do
    cur_sum = 0
    total_sum = 1

    if last_sold_stock_time <= ten_minutes:
        condition_value = 0.35
    else:
        condition_value = 0.15

    if cur_sum/total_sum > condition_value:
        # Do something
        pass

    sleep(wait_time)
    last_sold_stock_time += wait_time

rawDescription: condition parameter adjustment

description: Condition parameter adjustment


code:
grouped = df.groupby([df[i].round(4) for i in df.columns])
subbed = grouped.apply(lambda g: g.apply(lambda row: g.irow(0), axis=1))
subbed.drop_index(level=list(df.columns), drop=True, inplace=True)

rawDescription: drop duplicates in Python Pandas DataFrame not removing duplicates

description: Drop duplicates in Python Pandas DataFrame not removing duplicates


code: [[datetime(int(y),int(m),int(d)) for d in days] for (y,m),days in my_dict.items()]

rawDescription: Trying to make a list of dates into date.year, date.month, date.day for comparrison?


code:
thetext[4:10]
thetext[:5]   # First 5 characters
thetext[-5:]  # Last 5 characters
thetext[:-1]  # All but the last character

rawDescription: longest match in python

description: Longest match in python


code: s = ''.join([line for line in anything])

rawDescription: Speed vs security vs compatibility over methods to do string concatenation in Python


code:
def sample(m, colors):
    """
    Parameters
    ----------
    m : number balls to draw from the urn
    colors : one-dimensional array of number balls of each color in the urn

    Returns
    -------
    One-dimensional array with the same length as `colors` containing the
    number of balls of each color in a random sample.
    """

    remaining = np.cumsum(colors[::-1])[::-1]
    result = np.zeros(len(colors), dtype=np.int)
    for i in range(len(colors)-1):
        result[i] = np.random.hypergeometric(colors[i], remaining[i+1], m)
        m -= result[i]
    result[-1] = m
    return result

rawDescription: Numpy drawing from urn

description: Numpy drawing from urn


code: sh.sh('-c', 'source ./test.sh; do-something-else-here')

rawDescription: Python's sh module - Running 'source' command


code: from common_application.models import Model_1, Model_2, Model_3

rawDescription: Django 1.8: How use Model from different application?


code: plt.plot(x2, y2, 'go-', label='HAlpha')

rawDescription: unable to join markers

description: Unable to join markers


rawDescription: Neatly pass positional arguments as args and optional arguments as kwargs from argpase to a function


code:
import operator

class CommonEqualityMixin(object):

    __slots__ = ()

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            if self.__slots__ == other.__slots__:
                 attr_getters = [operator.attrgetter(attr) for attr in self.__slots__]
                 return all(getter(self) == getter(other) for getter in attr_getters)

        return False

    def __ne__(self, other):
        return not self.__eq__(other)

rawDescription: Equality of Python classes using slots

description: Equality of Python classes using slots


code:
from bs4 import BeautifulSoup, Comment

soup = BeautifulSoup(html)
div = soup.find('div', 'booker-booking')
# remove comments
text = " ".join(div.find_all(text=lambda t: not isinstance(t, Comment)))
# -> u'\n    2\xa0rooms\n    \xb7\n    USD\xa00\n     \n'

rawDescription: beautifulsoup 4 + python: string returns 'None'

description: Beautifulsoup 4 + python: string returns 'None'


code:
start_urls = ['http://www.depressionforums.org/forums/forum/12-depression-central/']

rules = (
    Rule(SgmlLinkExtractor(restrict_xpaths=('//li[@class="next"]'), unique=True),
                           callback='parse_links',
                           follow=True),
)

...

def check_login_response(self, response):
    """Check the response returned by a login request to see if we are successfully logged in."""
    if "Username or password incorrect" in response.body:
        self.log("Login failed.")
    else:
        self.log("Successfully logged in. Let's start crawling!")
        # Now the crawling can begin.
        for url in self.start_urls:
            # explicitly ask Scrapy to run the responses through rules
            yield Request(url, callback=self.parse)

rawDescription: python Scrapy CrawlSpider: Rule doesn't apply after logged in, only crawled the first page

description: Python Scrapy CrawlSpider: Rule doesn't apply after logged in, only crawled the first page


code:
def bitwise_inner(n, m=12):
    bitmask = (1 << n) - 1
    m = min(m, 2*n)
    result = []
    chunk = 2**m
    for j in xrange(2**(2*n-m)):
        start = j * chunk
        items = np.arange(start, start+chunk, dtype=np.uint32)
        op = (items >> n) ^ (items & bitmask)
        # Keep only items with same number of 0s and 1s in the first n bits of op
        for k in xrange(n//2 - 1):
            # This removes a 1 from the binary representation of each number
            op &= op - 1
            mask = op != 0
            items = items[mask]
            op = op[mask]
        op &= op - 1
        result.append(items[op == 0])
    return sum([len(j) for j in result])

rawDescription: How to speed up a large number of inner products

description: Speed up a large number of inner products


code:
from collections import Counter
x = ['ab', 'ab', 'ac']
counts = Counter(x)

rawDescription: Python: Shortest way to extract and count elements from an array of String?


code:
text = 'qwerty'
it = enumerate(text)
next(it)
next(it)
next(it)
list(enumerate(text))

rawDescription: Confused by the enumerate() function in tutorials


code:
import json
import subprocess

with subprocess.Popen(["cat", "/tmp/foo.json"], stdout=subprocess.PIPE) as f:
    j = f.stdout.read()
o = json.loads(j.decode("UTF-8"))
print(o)
print(o["def1"]["val1"])
print(o["def2"]["9"]["val1"])

rawDescription: Distinguishing 2 lines in a file read

description: Distinguishing 2 lines in a file read


code: file = file.replace(key, value)

rawDescription: Python - Transliterate German Umlauts to Diacritic


rawDescription: Can't plot value counts for pie chart

description: Can't plot value counts for pie chart


code:
data
data[0][0]

rawDescription: Reading file with lists to list

description: Reading file with lists to list