code: res=[[0]*nodes for i in range(nodes)]

rawDescription: generating a grid in a function

description: Generating a grid in a function


rawDescription: Python creating tuple groups in list from another list


code:
df2
df2.loc['3100a', 'attributes']

rawDescription: How to read part of the line into array with python-pandas?

description: Read part of the line into array with python-pandas


code:
f1 = open('file1', 'r')
f2 = open('file2', 'r')

for i in range(3): # your actual conditions go here
    # read in next line from each of the files and store them in a list of ints
    varlist1 = f1.readline().strip().split(', ')
    varlist1 = [int(x) for x in varlist1] # use float(x) here if a,b,... are floats
    varlist2 = f2.readline().strip().split(', ')
    varlist2 = [int(x) for x in varlist2] # use float(x) here if g,h,... are floats
    # do something more interesting with varlist1, varlist2 here
    # than just printing them
    print(varlist1)
    print(varlist2)

f1.close()
f2.close()

rawDescription: reading in files line by line and storing the content of each line in a list for further processing

description: Reading in files line by line and storing the content of each line in a list for further processing


code: {{ list_string|safe }}

rawDescription: Build list of links using Jinja2

description: Build list of links using Jinja2


code:
current = int(input("Current floor: "))
desti = int(input("Destination floor: "))
print(*range(current+1, desti))
between = list(range(current+1, desti))
between

rawDescription: How to find the numbers in between the numbers that the user has input?

description: Find the numbers in between the numbers that the user has input


code:
from authbind import authenticate

creds = 'foo\x00bar\x00'
authenticate(bytearray(creds))

rawDescription: Passing binary data from Python to C API extension


code: b, c, d = np.repeat(a[None, ...], 3, axis=0)

rawDescription: Create multiple unique copies of an array


code:
s = '''\
<html>
    <body>{list}
    </body>
</html>'''

item = '''
        <p>
            {0}
        </p>\
'''
print(s.format(list="".join(map(item.format, [1, 2, 3]))))

rawDescription: Python : for loop inside wsgi script

description: Python : for loop inside wsgi script


code:
ass = bom2['ASS'].tolist()
fin = bom2['FIN'].tolist()

# filter out rows of bom2 where FIN equals AF
nejsou = bom2[-bom2['FIN'].isin(ass)]

nee = list()  #will contain ASS not in FIN
for a in ass:
    if a not in fin: nee.append(a)

print(nejsou)
#    ASS FIN
# 0    A   F
# 1   AF   F
# 2  AF2   F

rawDescription: Pandas boolean indexing vs comparision of two lists: Same (?) methods, different results


code:
XB = np.linspace(-1,1,20)
YB = np.linspace(-1,1,20)
X,Y = np.meshgrid(XB,YB)
Z = np.exp(-(X**2+Y**2))
plt.imshow(Z,interpolation='none')

rawDescription: Python: Creating a 2D histogram from a numpy matrix


code:
from setuptools import setup

setup(name='foo',
      version='0.9.0',
      py_modules=['foo'],
      install_requires=['BeautifulSoup >= X.X.X'],
)

rawDescription: Require BeautifulSoup in a Python Package - What's needed in setup.py?


code:
class UserProfile(models.Model):
     filled_out_the_form = field.BooleanField(default=False)
     # ...

rawDescription: Open page at first login

description: Open page at first login


rawDescription: Given a "frozen" SciPy discrete random variable, how can I know its support?


rawDescription: Timestring regex: please simplify it and show how the colons can be dropped?


rawDescription: Incrementing values in a dict

description: Incrementing values in a dict


code: (df['Time'].diff() > 2).cumsum()

rawDescription: Pandas to form clusters based on diff column


code:
class Super1(object):
    def __init__(self, value1):
        self._value1 = value1

    @property
    def value1(self):
        return self._value1[0]

    @value1.setter
    def value1(self, value):
        self._value1[0] = value

rawDescription: How to link instance attributes to a numpy array?

description: Link instance attributes to a numpy array


rawDescription: Sampling histograms such that the sum over the sample is uniform


code:
def escape_repl(match):
    return '{0}[\W{0}]*'.format(re.escape(match.group(1)))

re.sub(r'([^\\])', escape_repl, string)

rawDescription: Python Regex: escaping a back-reference

description: Python Regex: escaping a back-reference


code:
line = "[aln-core]:10000000 sequences have been processed"

line.split(':')[1]
'10000000 sequences have been processed'

line.split(':')[1].split()[0]
'10000000'

rawDescription: print specific word(number) out of a line

description: Print specific word(number) out of a line


code: [[set(['House', 'hoose']), 400], [set(['Bonerphone', 'Bananphone']), 20], [set(['UniqueValue']), 777]]

rawDescription: How to merge similar items in a list

description: Merge similar items in a list


code:
def my_product(dicts):
    return (dict(izip(dicts, x)) for x in product(*dicts.itervalues()))
def generate_tests(testspec):
    return [dict(testspec.config.items() + x.items()) for x in my_product(testspec.variables)]

rawDescription: Is there a Python test framework suitable to run simulations


code:
class DataNET(Data):
    def __init__(self, logfile):
        Data.__init__(self, logfile)
        self._data = {}

rawDescription: Inheritance in Python, init method overrriding


code:
import matplotlib.pyplot as plt
import matplotlib.colors as colors

# Evil!!
colors.colorConverter.colors['b'] = (0.1, 1.0, 0.2)
# You may need to clear the cache that ColorConverter keeps if 'b'
# has alreadly been used...
colors.colorConverter.cache = {}

plt.plot(range(10), color='b')
plt.show()

rawDescription: Modifying the built-in colors of matplotlib


code: df[df > 0].dropna()

rawDescription: Filter rows of pandas dataframe whose values are lower than 0


code:
import StringIO

output = StringIO.StringIO()
with open(filepath, 'r') as f:
    for line in f:
        if 'Step' not in line:
            output.write(line)

rawDescription: Python: Split file by line match

description: Python: Split file by line match


rawDescription: Creating sublist from a give list of items


code:
adcvalue = str(adcvalue)
adcvalue = adcvalue[3:12]
adcvalue = int(adcvalue)

rawDescription: Reading data out of an ADC (MCP3001) with python (SPI)


code: x = [el for el in x if len(el) != 3]

rawDescription: How to conditionally delete elements from list-of-tuples?

description: Conditionally delete elements from list-of-tuples


code:
import model.client
o = model.client.clientclass()

rawDescription: python MySQL module class file name

description: Python MySQL module class file name


code: tap([], lambda x: x.append(1))

rawDescription: Ruby's tap idiom in Python

description: Ruby's tap idiom in Python


code: [('This is great!\n', 'good_review'), ('Wow, it amazes me...\n', 'good_review'), ('An hour of show, a lifetime of enlightment\n', 'good_review'), ('Comme si, Comme sa.\n', 'bad_review'), ('I just wasted my foo bar on this.\n', 'bad_review'), ('An hour of s**t, ****.\n', 'bad_review')]

rawDescription: How to read and label line by line a text file using nltk.corpus in Python

description: Read and label line by line a text file using nltk.corpus in Python


code:
test = list('abcdef')
s = list('here comes the sun')
counts = Counter(s)
distribution = Counter(counts[v] for v in set(test))
distribution

rawDescription: Python loop improvements

description: Python loop improvements


code: df.index = pd.DatetimeIndex(df.index)

rawDescription: Filling missing values pandas dataframe

description: Filling missing values pandas dataframe


code: include_dirs = ['Dir4/SubDir4.2', 'Dir1/SubDir4.2']

rawDescription: Excluding all but a single subdirectory from a file search


code:
with A() as a, B() as b:
    suite

rawDescription: how to write results in a for loop to multiple CSV files

description: Write results in a for loop to multiple CSV files


code:
if __name__ == "__main__":
    import sys
    args = sys.argv[1:]
    if len(args) != 2:
        print("This script requires exactly two command-line arguments!")
        exit(1)

    import my_module
    exit_code = my_module.run(args) or 0
    exit(exit_code)
else:
    raise ImportError("Run this file directly, don't import it!")

rawDescription: Designing a program entry point in python


code:
d = {"name": 'A', "children": ['B', 'C']}
import json as JSON
dj = JSON.dumps(d)
dj

rawDescription: Python library for creating tree graphs out of nested Python objects (dicts)


code:
responce[egg]()

#responce[egg](arg1, arg2, ...) # if function require arguments

rawDescription: Long elif chains vs dictionary with exec()


code:
import numpy as np

a = np.random.rand(1000, 1000)
a[a < 0.1] = np.nan    # set some random values to nan
b = np.ones_like(a)

rawDescription: Numpy: multiplying with NaN values without using nan_to_num


code:
@register.filter(expects_localtime=True)
def days_since(value, arg=None):
    try:
        tzinfo = getattr(value, 'tzinfo', None)
        value = date(value.year, value.month, value.day)
    except AttributeError:
        # Passed value wasn't a date object
        return value
    except ValueError:
        # Date arguments out of range
        return value
    today = datetime.now(tzinfo).date()
    delta = value - today
    if abs(delta.days) == 1:
        day_str = _("day")
    else:
        day_str = _("days")

    if delta.days < 1:
        fa_str = _("ago")
    else:
        fa_str = _("from now")

    return "%s %s %s" % (abs(delta.days), day_str, fa_str)

rawDescription: Django built-in timesince filter to show only Days


code:
m = re.match('blahblah(\d{4})(\d{2})', f)
if m:
    year = int(m.group(1))
    month = int(m.group(2))

    if (from_year, from_month) <= (year, month) <= (to_year, to_month):
        print(f)

rawDescription: regex to match file names blahblah_200901.csv (yyyymm)

description: Regex to match file names blahblah_200901.csv (yyyymm)


code:
from functools import wraps

def requires_foo(func):
    @wraps(func)
    def wrapped(self, *args, **kwargs):
        if not hasattr(self, 'foo') or not self.foo is True:
            raise Exception('You must have foo and be True!!')
        return func(self, *args, **kwargs)
    return wrapped

def requires_bar(func):
    @wraps(func)
    def wrapped(self, *args, **kwargs):
        if not hasattr(self, 'bar') or not self.bar is True:
            raise Exception('You must have bar and be True!!')
        return func(self, *args, **kwargs)
    return wrapped

class FooBar(object):

    @requires_foo                 # Make sure the requirement is met.
    def do_something_to_foo(self):
        pass

rawDescription: Python decorators just syntactic sugar?

description: Python decorators just syntactic sugar


rawDescription: Python List of Lists Incrementing

description: Python List of Lists Incrementing


rawDescription: Finding index values in a pandas data frame where columns are the same


code:
import nltk

# Given a corpus of texts
text1 = 'Lorem ipsum FOO dolor BAR sit amet'
text2 = 'Ut enim ad FOO minim veniam, '
text3 = 'Duis aute irure dolor BAR in reprehenderit '
text4 = 'Excepteur sint occaecat BAR cupidatat non proident'

# We split the texts into tokens, and form a TextCollection
mytexts = (
    [nltk.word_tokenize(text) for text in [text1, text2, text3, text4]])
mycollection = nltk.TextCollection(mytexts)

# Given a new text
text = 'et FOO tu BAR Brute'
tokens = nltk.word_tokenize(text)

# for each token (roughly, word) in the new text, we compute the tf_idf
for word in tokens:
    print('{w}: {s}'.format(w = word,
                            s = mycollection.tf_idf(word,tokens)))

rawDescription: positional index using python

description: Positional index using python


rawDescription: Disable/Remove argument in argparse

description: Disable/Remove argument in argparse


code: ts = ts.resample('5T')

rawDescription: Python pandas time series interpolation and regularization


code:
#!/usr/bin/env python3
import multiprocessing, time, random, sys
from PySide.QtCore import * # equivalent: from PyQt4.QtCore import *
from PySide.QtGui import *   # equivalent: from PyQt4.QtGui import *

def compute(num):
    print("worker() started at %d" % num)
    random_number = random.randint(1, 6)
    if random_number in (2, 4, 6):
        raise Exception('Random Exception in _%d' % num)
    time.sleep(random_number)
    return num

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.toolBar = self.addToolBar("Toolbar")
        self.toolBar.addAction(QAction('Add Task', self, triggered=self.addTask))
        self.list = QListWidget()
        self.setCentralWidget(self.list)

        # Pool of Background Processes
        self.pool = multiprocessing.Pool(processes=4)

    def addTask(self):
        num_row = self.list.count()
        self.pool.apply_async(func=compute, args=(num_row,), callback=self.receiveResult,
                              error_callback=self.receiveException)
        item = QListWidgetItem("item %d" % num_row)
        item.setForeground(Qt.gray)
        self.list.addItem(item)

    def receiveResult(self, result):
        assert isinstance(result, int)
        print("end_work(), where result is %s" % result)
        self.list.item(result).setForeground(Qt.darkGreen)

    def receiveException(self, exception):
        error = str(exception)
        _pos = error.find('_') + 1
        num_row = int(error[_pos:])
        item = self.list.item(num_row)
        item.setForeground(Qt.darkRed)
        item.setText(item.text() + ' Retry...')
        self.pool.apply_async(func=compute, args=(num_row,), callback=self.receiveResult,
                              error_callback=self.receiveException)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

rawDescription: multiprocessing GUI schemas to combat the "Not Responding" blocking

description: Multiprocessing GUI schemas to combat the "Not Responding" blocking


rawDescription: Python - Perform an operation on an element of a tuple


code:
def keys_as_dictionary(channel):
    """return a dictionay of times:values for <channel>"""
    keys = cmds.keyframe(channel, q=True, tc=True) or []
    values = cmds.keyframe(channel, q=True, vc=True) or []
    return dict(zip(keys, values))

def channels(object):
    """return a dictionary of <plug>:<channel_dict> for each animated plug on <object>"""
    keys = cmds.keyframe(object, n=True, q=True)
    result = {}

    for k in keys:
        plugs = cmds.listConnections(k, p=True)[0]
        result[plugs]= keys_as_dictionary(k)
    return result

rawDescription: Using keyframe() to display nested dictionary info using Python in Maya


code:
from random import choice
random_user = choice(online_users)

rawDescription: Unsure how/where to manipulate Django objects


code: result.state != "PENDING"

rawDescription: Find out whether celery task exists

description: Find out whether celery task exists


rawDescription: How to generalize elementwise matrix operations in numpy

description: Generalize elementwise matrix operations in numpy


rawDescription: SQLAlchemy + Requests Asynchronous Pattern


code:
def method(self, name='Thomas', age=27):
  the_age = age
  self.submethod(name, age=the_age)

rawDescription: python - get attribute to subclass

description: Python - get attribute to subclass


code:
import collections as coll
import itertools

atoms_1 = [1,2,3,4,5,6]
atoms_2 = [2,4,6,1,2,3]

def old():
    to_add = [(min(atom_1, atom_2), max(atom_1, atom_2)) for atom_1 in atoms_1 for atom_2 in atoms_2 if atom_2 != atom_1]
    add_dict = coll.defaultdict(list)
    for k, v in to_add:
        add_dict[k].append(v)
    return add_dict

def new():
    add_dict = coll.defaultdict(list)
    for atom_1, atom_2 in itertools.product(atoms_1, atoms_2):
        if atom_1 == atom_2: continue
        (atom_min, atom_max) = (atom_1, atom_2) if atom_1 < atom_2 else (atom_2, atom_1)
        add_dict[atom_min].append(atom_max)
    return add_dict

import timeit
print(timeit.timeit("old()", setup="from __main__ import old"))  # 20.76972103
print(timeit.timeit("new()", setup="from __main__ import new"))  # 10.9827100827

rawDescription: Python: Optimising a list comprehension which compares two integers


code:
from itertools import combinations

with open('test2.txt', 'wb') as outfh:
    writer = csv.writer(outfh, delimiter='\t')
    for (state1, features1), (state2, features2) in combinations(data, 2):
        overlap = len(features1 & features2)
        writer.writerow([
            len(features1), state1,
            len(features2), state2,
            overlap])

rawDescription: Creating iterators with counting intersection


code:
import itertools as it

def stream(nxt, safe, prod, cons, state, inputs):
    x = next(inputs)   # obtain x
    # xs = inputs
    y = nxt(state)
    if safe(state, y):
        yield y
        inputs = it.chain([x], inputs)   # put x back in the input
        yield from stream(nxt, safe, prod, cons, prod(state, y), inputs)
    else:
        yield from stream(nxt, safe, prod, cons, cons(state, x), inputs)

rawDescription: Convert a Haskell code to Python or pseudocode


code:
import dominate
from dominate import tags as tags# import time, subprocess, os, datetime, zipfile, shutil

doc = dominate.document(title="whatever")
with doc:
    tags.style("body{font-family:Helvetica}")
    tags.style("h1{font-size:x-large}")
    tags.style("h2{font-size:large}")
    tags.style("table{border-collapse:collapse}")
    tags.style("th{font-size:small;border:1px solid gray;padding:4px;background-color:#DDD}")
    tags.style("td{font-size:small;text-align:center;border:1px solid gray;padding:4px}")
    with tags.table():
        with tags.thead():
            tags.th("Test")
            tags.th("Pass%")
            tags.th("random")
            tags.th("Bugs / Notes")
        with tags.tbody():
            with tags.tr(): #Row 1
                tags.td("random", rowspan="5")
                tags.td("random", "%", rowspan="5", style="font-size:small;text-align:center;border:1px solid gray;padding:4px;background-color:#66FF66")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
            with tags.tr(): # Row 2 (only cols 3 and 4)
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
            with tags.tr(): # Row 3
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
            with tags.tr(): # Row 4
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
            with tags.tr(): # Row 5
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")
                tags.td("1", style = "font-size:small;text-align:center;padding:4px")

HTML_TMP = "/tmp/demo.html"
MAIL_TMP = "/tmp/demo.txt"

# save it to a file
with open(HTML_TMP, "w") as f:
     f.write(str(doc))

rawDescription: How to use rowspan in td using dominate tags library?

description: Use rowspan in td using dominate tags library


rawDescription: Regex expression to take text in double quotations except when ending with specifc word


rawDescription: Create a compress function in Python?

description: Create a compress function in Python


code:
x.append('bar')
x,y

rawDescription: Python list([]) and []

description: Python list([]) and []


code:
def filler(series):
    avg = series._get_numeric_data().mean()
    series.fillna(series.mode().values[0] if np.isnan(avg) else avg, inplace=True)

dfna.apply(filler)

rawDescription: Pandas fill np.nan issue

description: Pandas fill np.nan issue


code:
def customaxis(ax, c_left='k', c_bottom='k', c_right='none', c_top='none',
               lw=3, size=12, pad=8):

    for c_spine, spine in zip([c_left, c_bottom, c_right, c_top],
                              ['left', 'bottom', 'right', 'top']):
        if c_spine != 'none':
            ax.spines[spine].set_color(c_spine)
            ax.spines[spine].set_linewidth(lw)
        else:
            ax.spines[spine].set_color('none')
    if (c_bottom == 'none') & (c_top == 'none'): # no bottom and no top
        ax.xaxis.set_ticks_position('none')
    elif (c_bottom != 'none') & (c_top != 'none'): # bottom and top
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                      color=c_bottom, labelsize=size, pad=pad)
    elif (c_bottom != 'none') & (c_top == 'none'): # bottom but not top
        ax.xaxis.set_ticks_position('bottom')
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                       color=c_bottom, labelsize=size, pad=pad)
    elif (c_bottom == 'none') & (c_top != 'none'): # no bottom but top
        ax.xaxis.set_ticks_position('top')
        ax.tick_params(axis='x', direction='out', width=lw, length=7,
                       color=c_top, labelsize=size, pad=pad)
    if (c_left == 'none') & (c_right == 'none'): # no left and no right
        ax.yaxis.set_ticks_position('none')
    elif (c_left != 'none') & (c_right != 'none'): # left and right
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_left, labelsize=size, pad=pad)
    elif (c_left != 'none') & (c_right == 'none'): # left but not right
        ax.yaxis.set_ticks_position('left')
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_left, labelsize=size, pad=pad)
    elif (c_left == 'none') & (c_right != 'none'): # no left but right
        ax.yaxis.set_ticks_position('right')
        ax.tick_params(axis='y', direction='out', width=lw, length=7,
                       color=c_right, labelsize=size, pad=pad)

rawDescription: Turn off the the upper/right axis tick marks


code: MiniFieldStorage('a', 'aa')

rawDescription: POST data to Python CGI script via jQuery AJAX


code: d = {Foo(): "value1", Foo(): "value2"}

rawDescription: Hash function and table implementation for over 50k entries in python


code:
import numpy as np

x = [np.nan, 3.14, np.nan]
mx = np.ma.masked_invalid(x)

print(repr(mx))
# masked_array(data = [-- 3.14 --],
#              mask = [ True False  True],
#        fill_value = 1e+20)

rawDescription: Python Numpy mask NaN not working

description: Python Numpy mask NaN not working


code:
custom_format(timedelta(days=42, hours=9, minutes=46, seconds=4, microseconds=352515))
custom_format(timedelta(days=1, hours=9, minutes=46, seconds=4, microseconds=352515))
custom_format(timedelta(hours=9, minutes=46, seconds=4, microseconds=352515))

rawDescription: Too many decimal places when using timedelta


rawDescription: Factorizing a number in python

description: Factorizing a number in python


rawDescription: Assign to a slice of a Python list from a lambda


code: my_list = [[int(x) for x in rec] for rec in csv.reader(p, delimiter=',')]

rawDescription: Import csv as List of List of Integers

description: Import csv as List of List of Integers


code: document = ET.parse("myxmlfile.xml")

rawDescription: parsing an xml file for unknown elements using python ElementTree

description: Parsing an xml file for unknown elements using python ElementTree


code:
name1
1
name2
1
2
name3
1
2
3

rawDescription: Creating dictionaries from a text file

description: Creating dictionaries from a text file


code:
template = '''\
URL GOTO=https://www.url.com/{user1}
TAG POS=1 TYPE=BUTTON ATTR=TXT:Follow
WAIT SECONDS= 27'''

with open('users.txt') as file:
    for line in file:
        print(template.format(user1=line.strip()))

rawDescription: Print from txt file

description: Print from txt file


code:
import networkx as nx
G = nx.Graph()
# build your Graph
# G.add_node(), G.add_nodes_from(), G.add_edge(), G.add_edges_from()...

nodes = [(g, G.degree(g)) for g in G.nodes()]
# nodes like this: [(338, 4), (340, 7)...]
# item one is the node, and item two is the edges connected with this node

nodes.sort(key=lambda n: n[1], reverse=True)

# you wanna delete the third node and other nodes which edges at most 5, right?
G.remove_node(nodes[2][1])
for n, e in nodes:
    if e > 5:
        G.remove_node(n)

rawDescription: removing number of edges in python

description: Removing number of edges in python


code:
x=cat
if len(BestX) == 1 and (starcount) == 1:
  x=dog

rawDescription: If conditions are met define variable as _____


code:
import matplotlib.font_manager as fm
fp1 = fm.FontProperties(fname='/usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-B.ttf')

rawDescription: Writing Greek in matplotlib labels, titles


code:
import numpy as np
x=np.array(iterable)  #pretty easy :) although there could be more efficient ways depending on where "iterable" comes from.

rawDescription: Transform filter function to Python code


code:
results = [[a,b]
         for a,b in itertools.product(events_a ,events_b)
         if Grouper.time_delta(attr="time",hours = 3)(a,b)]

rawDescription: How to find two close events (that have a datetime component) in python

description: Find two close events (that have a datetime component) in python


code:
from time import sleep
from threading import Thread

from erlport.erlterms import Atom
from erlport import erlang

def start(receiver):
    Thread(target=receive_loop, args=[receiver]).start()
    return Atom("ok")

def receive_loop(receiver):
    while True:
        data = ""
        for chunk in ["Got ", "BIG ", "Data"]:
            data += chunk
            sleep(2)
        erlang.cast(receiver, [data])

rawDescription: Run python program from Erlang

description: Run python program from Erlang


code:
def get_cached(model, **kwargs):
    timeout = kwargs.pop('timeout', 60 * 60)
    key = '%s:%s' % (model, kwargs)
    result = cache.get(key)
    if result is None:
        result = model.objects.get(**kwargs)
        cache.set(key, result, timeout)
    return result

rawDescription: Patterns for dealing with memcache Caching in Django


code:
import pandas as pd
import numpy.random as random
n=1000
df = pd.DataFrame(random.randn(n), pd.date_range('2010-01-01', periods=n), columns=["data"])
df['Dates'] = df.index
df['week_days'] =df.index.weekday
df

rawDescription: Best way to generate day-of-week boxplots from a Pandas timeseries

description: Generate day-of-week boxplots from a Pandas timeseries


rawDescription: Sending http headers with python

description: Sending http headers with python


rawDescription: How to calculate difference on previous within set of grouped rows in a dataframe

description: Calculate difference on previous within set of grouped rows in a dataframe


code:
from re import split
mystr = "word,word,;word;word"
split("\W+", mystr)

rawDescription: Splitting with multiple delimiters

description: Splitting with multiple delimiters


code:
import re
KEYVAL = re.compile(r"([A-Z])\s*\((\d+)\)")

def max_item(row):
    return max((int(v),k) for k,v in KEYVAL.findall(row))[1]

def max_item_lines(fh):
    for row in fh:
        yield "%s\n" % max_item(row)

def process_file(infilename, outfilename):
    infile = open(infilename)
    max_items = max_item_lines(infile)
    outfile = open(outfilename, "w")
    outfile.writelines(max_items)
    outfile.close()

if __name__ == '__main__':
    import sys
    infilename, outfilename = sys.argv[1:]
    process_file(infilename, outfilename)

rawDescription: Getting the maximum value from dictionary


code:
# Take the lower part of the image
mask = np.tri(15, 12, dtype=bool)
# Apply mask to first channel
one_channel = img[:, :, 0][mask]
one_channel.shape
# Apply mask to all channels
pixels = img[mask]
pixels.shape
np.all(pixels[:, 0] == one_channel)

rawDescription: Copy channels in Numpy array

description: Copy channels in Numpy array


code:
def multiplyBy3(lst):
    return [item * 3 for item in lst] #construct a new list of item * 3.

rawDescription: Returning a list in a function

description: Returning a list in a function


code:
import os
import sys
import vlc

if __name__ == '__main__':
    #filepath = <either-some-url-or-local-path>
    filepath = "http://r1---sn-nfpnnjvh-1gil.c.youtube.com/videoplayback?source=youtube&newshard=yes&fexp=936100%2C906397%2C928201%2C929117%2C929123%2C929121%2C929915%2C929906%2C929907%2C929125%2C929127%2C925714%2C929917%2C929919%2C912512%2C912515%2C912521%2C906838%2C904485%2C906840%2C931913%2C904830%2C919373%2C933701%2C904122%2C932216%2C936303%2C909421%2C912711%2C907228%2C935000&sver=3&expire=1373237257&mt=1373214031&mv=m&ratebypass=yes&id=1907b7271247a714&ms=au&ipbits=48&sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cupn%2Cexpire&itag=45&key=yt1&ip=2a02%3A120b%3Ac3c6%3A7190%3A6823%3Af2d%3A732c%3A3577&upn=z3zzcrvPC0U&cp=U0hWSFJOVV9KUUNONl9KSFlDOmt4Y3dEWFo3dDFu&signature=D6049FD7CD5FBD2CC6CD4D60411EE492AA0E9A77.5D0562CCF4E10A6CC53B62AAFFF6CB3BB0BA91C0"
    movie = os.path.expanduser(filepath)
    savedcopy = "yt-stream.mpg"
    if 'http://' not in filepath:
        if not os.access(movie, os.R_OK):
            print ( 'Error: %s file is not readable' % movie )
            sys.exit(1)
    instance = vlc.Instance("--sout=#transcode{vcodec=mp4v,acodec=mpga,vb=800,ab=128,deinterlace}:duplicate{dst=file{dst=%s},dst=display}" % savedcopy)
    try:
        media = instance.media_new(movie)
    except NameError:
        print ('NameError: % (%s vs Libvlc %s)' % (sys.exc_info()[1],
                       vlc.__version__, vlc.libvlc_get_version()))
        sys.exit(1)
    player = instance.media_player_new()
    player.set_media(media)
    player.play()

    #dont exit!
    while(1):
        continue

rawDescription: Saving a stream while playing it using LibVLC


code:
with open(somecsv, 'rb') as infile:
    reader = csv.reader(infile, delimiter=';')
    headers = next(reader)
    data = list(reader)
    data = dict(zip(headers, zip(*data)))

rawDescription: Read a term-document matrix from csv using python


code:
import json

json_array = json.dumps(list_of_strings)
result = browser.execute_script("function findTexts(%s){//code here}" % json_array)

rawDescription: Splinter: How to pass argument to execute_script?


rawDescription: Ignoring duplicate entries in sparse matrix


code:
f = '/somefolder/somefolder2/201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'
re.search(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt', f).group(0)
f = 'c:\\somefolder\\somefolder2\\201308 - (82608) - MAC 2233-007-Methods of Calculus - Klingler, Lee.txt'
re.search(term + '\s-\s\(\d{5}\)\s-\s\w{3}\s\d{4}-\d{3}-[^\.]+\.txt', f).group(0)

rawDescription: Python - regex not escaping parentheses in alphanumeric string with symbols and a variable passed in to method


code:
import yaml

sql = """
query         : "SELECT * from cities
WHERE name='Unknown';"
count         : 0
level         : 1
name          : "Check for cities whose name should be null"
suggested_fix : "UPDATE cities SET name=NULL WHERE name='Unknown';"
"""

sql_dict = yaml.safe_load(sql)

print(sql_dict['query'])

rawDescription: Python: Read configuration file with multiple lines per key


code:
if not (input is None or
        var_a and var_a in input and
        var_b and var_b in input):
    pass

rawDescription: Python: more concise logic/syntax on empty strings


code:
from dateutil import tz
import datetime
utcnow = datetime.datetime.now(tz.tzutc())
then = parser.parse('Tue Feb 19 00:09:28 +1100 2013')
utcnow - then
(utcnow - then).days

rawDescription: Calculating days using string dates in Python


rawDescription: pyparsing - parsring a dates

description: Pyparsing - parsring a dates


code:
"""Now I can use either quote with more freedom: ' and "."""
'''Now I can use either quote with more freedom: ' and ".'''

rawDescription: String creation: Differences between two syntaxes ' ' and " "?


rawDescription: Parsing XML file to Python list of dictionaries


code:
import re

def translate_pattern(pattern):
    res = []
    for c in pattern:
        if c == '*':
            res.append('.+')  # 1 or more
        elif c == '.':
            res.append('.')   # exactly 1
        else:
            res.append(re.escape(c))  # anything else is a literal character
    return re.compile(''.join(res))

rawDescription: Check string by user-given regex

description: Check string by user-given regex


rawDescription: Biopython: How to get the compound name of the pdb file of a protein?


rawDescription: Python statsmodels OLS: how to save learned model to file


code: s1 = datetime.timedelta(seconds=1)

rawDescription: Why does timedelta function return 0 seconds when datetimes are perfect number of days apart?


code:
class Foo(object):

    def __call__(self, bar):
        return bar

foo = Foo()

rawDescription: Can a function in python behave like a class?


code:
_sentinel = object()

def my_function(a=10, b=_sentinel):
    if b is _sentinel:
        b = a

rawDescription: function default value not defined

description: Function default value not defined


code: df1.apply(lambda x: np.sum(df2.values == x.values), axis=1)

rawDescription: iterate through all dataframe columns

description: Iterate through all dataframe columns


code: mylist = [Item(1,2,3) for i in range(random_number)]

rawDescription: Creating a random number of class objects in python


code: from childfilename import childclassname

rawDescription: How could I make a good application architecture in Python with PyQt?


code:
triplets = dict.setdefault(key1, [])
if not any(triplet[0] == t for triplet in triplets):
    # no such t value found
    triplets.append((t, a, c))

rawDescription: Python: how to work with list to have unique values


code: arr = np.fromiter(cursor, dtype=[('row', int), ('col', int), ('value', float)])

rawDescription: Efficiently read a Python sparse matrix from SQL


code:
from tkinter import *

edit_recipe_window = Tk()
if 1:
    r = 1   #r == row
    c = 0   #c == column
    n = 1   #n == number
    a = 2   #a == checking what iteration
    y = ["count1","count2","count3","count4","count5"]
    for count in y:
        if a%2 == 0:
            print ("inside if, label values", r, c)
            Label(edit_recipe_window, text="Ingredient " + str(n)).grid(row=r, column=c)
            c += 1
            print("inside if, entry values",r,c)
            entry_box= Entry(edit_recipe_window)
            entry_box.grid(row=r, column=c)
            entry_box.insert(0, count)
            c = 0
            a += 1

        else:
            print ("inside else, label values", r, c)
            Label(edit_recipe_window, text="Quantity and Unit " + str(n)).grid(row=r, column=c)
            c += 1
            print ("inside else, entry values", r, c)
            quantity_box = Entry(edit_recipe_window)
            quantity_box.grid(row=r, column=c)
            quantity_box.insert(0, count)
            c = 0
            r += 1
            a += 1
        n += 1

rawDescription: Creating labels and entries in a for loop


code:
[key for key, group in groupby(li) if sum(1 for i in group) == 1]
[key for key, group in groupby(li) if len(tuple(group)) == 1]

rawDescription: Elegant way to remove contiguous repeated elements in a list?


code:
class NewLink(models.Model):
    upvote = models.IntegerField(default=0)
    downvote =  models.IntegerField(default=0)
    @property
    def votetotal(self):
        return self.upvote + self.downvote

rawDescription: Django: How to sum downvote and upvote elements of a model in models.py and not in views.py?


code:
for sentence in all_sentences[::30]:
    print(sentence)

rawDescription: Python multiple string replacement

description: Python multiple string replacement


code:
('value1', 'value2', 'value3_0', 'value4', 'value5_0', 'value6')
('value1', 'value2', 'value3_0', 'value4', 'value5_1', 'value6')
('value1', 'value2', 'value3_0', 'value4', 'value5_2', 'value6')
('value1', 'value2', 'value3_0', 'value4', 'value5_3', 'value6')
('value1', 'value2', 'value3_1', 'value4', 'value5_0', 'value6')
('value1', 'value2', 'value3_1', 'value4', 'value5_1', 'value6')
('value1', 'value2', 'value3_1', 'value4', 'value5_2', 'value6')
('value1', 'value2', 'value3_1', 'value4', 'value5_3', 'value6')

rawDescription: Spliting a string containing many values into many strings with one value for each


code: print(d.keys() > {"from", "to"})

rawDescription: python dictionary check if any key other than given keys exist

description: Python dictionary check if any key other than given keys exist


code:
diff = now - dt
days_diff = diff.days

rawDescription: Converting string to datetime then comparing it with datetime.now


rawDescription: how to print the text before the first index number?

description: Print the text before the first index number


code:
df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
df['date'] = pd.to_datetime(['21-11-2013', '22-11-2013'])
pd.DatetimeIndex(df.date) + pd.DateOffset(1)
pd.DatetimeIndex(df.date) + pd.offsets.Hour(1)

rawDescription: Pandas add one day to column

description: Pandas add one day to column


code:
import urllib
urllib
import BeautifulSoup
BeautifulSoup

rawDescription: How do I deactivate an egg?

description: Deactivate an egg


rawDescription: Filter with Array column with Postgres and SQLAlchemy


code:
mask = np.in1d(a[:, 0], b)
#array([False, False, False, False, False, False, False, False,  True,  True], dtype=bool)

rawDescription: Intersection of two numpy arrays of different dimensions by column


code:
import pandas as pd
d1 = pd.DataFrame({'ticker' : ['spx 5/25/2001 p500', 'spx 5/25/2001 p600', 'spx 5/25/2001 p700']})
d1.ticker.str.split().tolist()

rawDescription: Get last "column" after .str.split() operation on column in pandas DataFrame


code: self.zip = zipcode

rawDescription: How to return a value from a method back to the input value

description: Return a value from a method back to the input value


code:
import numpy as np
import pandas as pd
np.random.seed(2015)
df = pd.DataFrame(np.random.randint(10, size=(10,2))-5, columns=['col A', 'col B'])

idx = df[["col A","col B"]].abs().idxmax(axis=1)
idx = pd.MultiIndex.from_arrays([idx.index, idx.values])
df['new_col'] = df.stack().loc[idx].values

rawDescription: Python Pandas: Using an array to choose different columns for each value of a new column


code:
def findArticleWithAttr(tableattrib, user_url_input):
    articles = Something.objects.filter(**{tableattrib : user_url_input})

rawDescription: Python Basics: How can i set two function parameters in equations?


code: number = re.search(r'\d+', filename).group()

rawDescription: How do I find one number in a string in Python?

description: Find one number in a string in Python


code: c.InteractiveShellApp.exec_lines = ['import setup']

rawDescription: How to import a python file at the start of ipython notebook

description: Import a python file at the start of ipython notebook


code:
import numpy as np
np.array([3, 7]) - np.array([1, 2])

rawDescription: TypeError: unsupported operand type(s) for -: 'list' and 'list'


code:
B = Tkinter.Button(root, text = "Run", command = mainprg1)
B.grid(row = 4, column = 1)

rawDescription: display a series of interfaces using Tkinter in python

description: Display a series of interfaces using Tkinter in python


rawDescription: Alias to Launch Python .py Script

description: Alias to Launch Python .py Script


code:
class LazyUserBannedFlag(object):

    user_banned = None

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

    def __bool__(self):
        if self.user_banned is None:
            user_id = self.request.user.id
            self.user_banned = HellBanList.objects.filter(condemned_id=user_id).exists()
        return self.user_banned

class CalculateUserBannedMiddleware(object):

    def process_request(self, request):
        request.user_banned = LazyUserBannedFlag(request)

rawDescription: How to pass self object to Global variable (Django views)

description: Pass self object to Global variable (Django views)


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

class Foo:
    __metaclass__ = Final

class Bar(Foo):
    pass

rawDescription: Python's equivalent of .Net's sealed class


rawDescription: Summing a 1D array with a col of a 2D array


code: color = COLORS[args.color]

rawDescription: How do I pass the argument correctly in Python

description: Pass the argument correctly in Python


code: nodes + thelist

rawDescription: A Pythonic way to insert elements of a list into a list of lists?


code: ['albert', 'Tim', 'john']

rawDescription: dictionary help for python writing a code

description: Dictionary help for python writing a code


code:
s = pd.Series(['sent>12:56:51.748912'] * 10000)

# this removes the 'sent>' string from the beginning
s = s.str[5:]

rawDescription: Arithmetic on array of timestamps without year, month, day


code:
from collections import defaultdict, Counter
d = defaultdict(lambda: {'counter': Counter(), 'name': ''})
d['x']['counter']['b'] += 1
d['x']['counter']['c'] += 1
print(d)

rawDescription: How to use a specific data structure as the default_factory for a defaultdict?

description: Use a specific data structure as the default_factory for a defaultdict


rawDescription: How do I append values from a row in a CSV file into a list in python?

description: Append values from a row in a CSV file into a list in python


code: top10 = df.groupby('Country_FAO', sort=False).mean_area.sum().nlargest(10).index

rawDescription: Subsetting stacked pandas dataframe

description: Subsetting stacked pandas dataframe


code:
import sys
import os
import comtypes.client

wdFormatPDF = 17

in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

rawDescription: .doc to pdf using python

description: .doc to pdf using python


code:
class MyRouter(DefaultRouter):
    routes = [
        Route(
            url=r'^{prefix}/((?P<pk>\d+)/)?info$',
            mapping={'get': 'info'},
            name='{basename}-info',
            initkwargs={}
        )
    ] + DefaultRouter.routes

rawDescription: Django Rest Framework add class level method to api


code:
filename = "test.old.txt"
os.path.splitext(filename)

rawDescription: How do I change the name of a file path correctly in Python?

description: Change the name of a file path correctly in Python


code:
li = ['x, y, z', 'dogs, cats']

li2 = [elem.split(', ') for elem in li]
[v for val in li2 for v in val]

rawDescription: Python List of Strings created from a list of one string


code: [[x[4], x[9]] for x in data["interactions"][0]["input"]]

rawDescription: Python extract values from JSON

description: Python extract values from JSON


code: ['Three of Diamonds', 'Three of Spades']

rawDescription: Append one value from a dictionary to a list


code: lohilist = [int(x) for x in lohi.split()]

rawDescription: get raw input that is between a set of numbers with some checks

description: Get raw input that is between a set of numbers with some checks


code: arr[np.isnan(arr)] = True

rawDescription: Python. Remove from array (presented as list of lists) rows and columns by condition


code:
from functools import reduce
from operator import or_
reduce(or_, map(Counter, (d1, d2, d3)))

rawDescription: Python Concatenate N dictionaries with highest values for each key


code:
def rows_to_dict_list(cursor):
    columns = [i[0] for i in cursor.description]
    return [dict(zip(columns, row)) for row in cursor]

rawDescription: Making a Dictionary List with cx_Oracle

description: Making a Dictionary List with cx_Oracle


code:
df['w'] = [5, 6, 7,8]

def foo(x):
     return x._get_numeric_data().apply(axis=0, func=np.log2).mean()

df.groupby('type').apply(foo)

#              v         w
# type
# X     0.000000  2.321928
# Y     1.528321  2.797439

rawDescription: applying functions to groups in pandas dataframe

description: Applying functions to groups in pandas dataframe


code:
all_messages=[]
rs=q.get_messages(10)
while len(rs)>0:
    all_messages.extend(rs)
    rs=q.get_messages(10)

rawDescription: How to get all messages in Amazon SQS queue using boto library in Python?

description: Get all messages in Amazon SQS queue using boto library in Python


code:
from selenium import webdriver
from selenium.common.exceptions import StaleElementReferenceException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class wait_for_more_than_n_elements_to_be_present(object):
    def __init__(self, locator, count):
        self.locator = locator
        self.count = count

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            return len(elements) > self.count
        except StaleElementReferenceException:
            return False

url = "https://twitter.com/ndtv"
driver = webdriver.Firefox()
driver.maximize_window()
driver.get(url)

# initial wait for the tweets to load
wait = WebDriverWait(driver, 10)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[data-item-id]")))

# scroll down to the last tweet until there is no more tweets loaded
while True:
    tweets = driver.find_elements_by_css_selector("li[data-item-id]")
    number_of_tweets = len(tweets)

    driver.execute_script("arguments[0].scrollIntoView();", tweets[-1])

    try:
        wait.until(wait_for_more_than_n_elements_to_be_present((By.CSS_SELECTOR, "li[data-item-id]"), number_of_tweets))
    except TimeoutException:
        break

rawDescription: Parse the html code for a whole webpage scrolled down


code:
text = "taj mahal is in india"
for key, value in my_dict.iteritems():
    text = text.replace(key, 'key' % value)

text

rawDescription: Insert hyperlink into text using django

description: Insert hyperlink into text using django


code:
import IPython.ipapi
ipython = IPython.ipapi.get()

rawDescription: How to run an IPython magic from a script (or timing a Python script)

description: Run an IPython magic from a script (or timing a Python script)


code:
T, F = True, False
b_List = [T,T,T,F,F,F,F,T,T,T,F,F,T,F]
count_Trues(b_List)
b_List.extend([T,T])
count_Trues(b_List)
b_List.extend([F])
count_Trues(b_List)

rawDescription: Count the number of occurrences between markers in a python list


code:
import re
regex = re.compile(r"[^\W\d_]", re.UNICODE)
regex.findall("aä12_")

rawDescription: shorthand for [:alpha:] in python regex

description: Shorthand for [:alpha:] in python regex


code:
with open('A', 'r') as f:
    text = f.read()

with open('B', 'a') as f:
    f.write(text)

rawDescription: Write file A to file B in python

description: Write file A to file B in python


code: issubclass(a, b)

rawDescription: Python History and Design: Why issubclass() instead of rich comparisons?


code: sample = numpy.random.normal(0, 0.01, (200, 785))

rawDescription: Initialize a 200 by 785 numpy matrix with values from a Gaussian distribution with mean 0, std. deviation 0.01


code: LOGIN_URL = 'login'

rawDescription: login_required doesn't work when redirecting

description: Login_required doesn't work when redirecting


code:
import pyximport
pyximport.install(reload_support=True)
import my_functions as mf

rawDescription: how to reload a cython module interactively using pyximport

description: Reload a cython module interactively using pyximport


code: self.comboBox = QtGui.QComboBox(self)

rawDescription: PYQT Qcombobox set value selected to a variable


code: b_dict = {key: "cat" if key == "foo" else value for key, value in a_dict.iteritems()}

rawDescription: Shallow copy dict while overriding a key value pair using python


code:
import xmltodict
result = xmltodict.parse(r.text)

cd = result['dwml']['head']['product']['creation-date']['#text']
print("creation-date =",cd)

svt = result['dwml']['data']['time-layout']['start-valid-time']
print("\nstart-valid-time =", svt)
print("number of start-valid-time entries =", len(svt))

evt = result['dwml']['data']['time-layout']['end-valid-time']
print("\nend-valid-time =", evt)
print("number of end-valid-time entries =", len(evt))

pop = result['dwml']['data']['parameters']['probability-of-precipitation']['value']
print("\nprobability-of-precipitation =", pop)
print("number of probability-of-precipitation entries =", len(pop))

hqpf = result['dwml']['data']['parameters']['hourly-qpf']['value']
print("\nhourly-qpf =", hqpf)
print("number of hourly-qpf entries =", len(hqpf))

rawDescription: XML Parsing to .txt file Python

description: XML Parsing to .txt file Python


code:
thing = Bar()
print(thing.bar())
print(Bar.bar(2))

rawDescription: Class Function and Static Method at the Same Time? (Python)


code:
do = datetime.datetime.strptime("Dec 17 00:00:06", "%b %d %H:%M:%S")
do.strftime("%Y-%m-%d %H:%M:%S")
do1 = do.replace(year=2014)
do1.strftime("%Y-%m-%d %H:%M:%S")

rawDescription: Change Date Format In Python

description: Change Date Format In Python


code:
s = 'mqRMRsL'
result = ''.join(sorted(s, key=lambda c:(c.lower(), c)))
# LMmqRRs

rawDescription: How to alphabetize characters in a string, disregarding upper and lower case?

description: Alphabetize characters in a string, disregarding upper and lower case


code:
columns = list(izip_longest(*data))
columns[0]
columns[1]
columns[2]

rawDescription: Horizontal analysis by generating list using while-loop


rawDescription: Numpy - automatic implicit conversion of np.arrays into lists


code:
from collections import defaultdict
d = defaultdict(list)

with open('infile') as f:
    for line in f:
        k,v = line.strip().split()
        d[k].append(v)

rawDescription: Merge excecution logs into a report

description: Merge excecution logs into a report


code: [dict((k, v) for k, v in d.iteritems() if k != 'mykey1') for d in mylist]

rawDescription: Removing key values pairs from a list of dictionaries


code:
m = re.search(r'(?s)\bWashington\n*(?:(?!\n\n).)*?TinyReds\s+(\S+)', s)
m.group(1)

rawDescription: Python multiline regex search between sections


code:
import os
import fcntl
from subprocess import Popen, PIPE
def setNonBlocking(fd):
    """
    Set the file description of the given file descriptor to non-blocking.
    """
    flags = fcntl.fcntl(fd, fcntl.F_GETFL)
    flags = flags | os.O_NONBLOCK
    fcntl.fcntl(fd, fcntl.F_SETFL, flags)

p = Popen("./a.out", stdin = PIPE, stdout = PIPE, stderr = PIPE, bufsize = 1)
setNonBlocking(p.stdout)
setNonBlocking(p.stderr)

p.stdin.write("1\n")
while True:
    try:
        out1 = p.stdout.read()
    except IOError:
        continue
    else:
        break
out1 = p.stdout.read()
p.stdin.write("5\n")
while True:
    try:
        out2 = p.stdout.read()
    except IOError:
        continue
    else:
        break

rawDescription: Interactive input/output using python

description: Interactive input/output using python


code:
def transform_node(name, val):
    if isinstance(val, list):
        val = ("children", [transform_node(k,v) for x in val for k, v in x.items()])
    elif isinstance(val, dict):
        val = ("children", [transform_node(*kv) for kv in val.items()])
    else:
        val = ("value", val)
    return dict([("name", name), val])

rawDescription: Arbitrary Dictionary (JSON) to Stricter Tree-like Dictionary (JSON) in Python


code:
findword('file', "sparrow")
findword('file', "donkey")

rawDescription: How to know a position (.txt)

description: Know a position (.txt)


code:
#!/usr/bin/python

import sys
print(sys.argv)

rawDescription: Calling python script from awk

description: Calling python script from awk


code:
import unicodedata as ud
all_unicode = ''.join(unichr(i) for i in xrange(65536))
unicode_letters = ''.join(c for c in all_unicode
                          if ud.category(c)=='Lu' or ud.category(c)=='Ll')

rawDescription: An equivalent to string.ascii_letters for unicode strings in python 2.x?


code:
def access(obj, indexes):
    def _get_item(subobj, index):
        if isinstance(subobj, list) and index < len(subobj):
            return subobj[index]
        return None

    return reduce(_get_item, indexes, obj)

rawDescription: Access multidimensional list value by tuple of indices in python


rawDescription: Regular expression - street address pattern


rawDescription: numpy dot product in steps

description: Numpy dot product in steps


code:
class DictList:
    def __init__(self,*args):
        self.n = args

    def __add__(self,new):
        L = list(self.n)
        if isinstance(new,DictList):
            L.extend(new.n)
        elif isinstance(new,dict):
            L.append(new)
        else:
            raise TypeError('Must be instance of DictList or dict')
        return DictList(*L)

    def __radd__(self,new):
        return self.__add__(new)

    def __repr__(self):
        return 'DictList'+repr(self.n)

rawDescription: How do I build a __add__ method in a specific class in python?

description: Build a __add__ method in a specific class in python


rawDescription: Python time-lat-lon array manipulation and grouping


code:
app = Celery('tasks')
app.config_from_object('celeryconfig')

rawDescription: How to pass connection details in Celery

description: Pass connection details in Celery


code:
for idx, item in enumerate(iterable):
  pass

rawDescription: getting the index while interating a list

description: Getting the index while interating a list


code:
def startUSBProgress(self, message, operation):
    title = self.tr("Transferring USB Data")
    progress = QtGui.QProgressDialog(message, "", 0, 1, self)

    progress.setWindowModality(QtCore.Qt.WindowModal)
    progress.setCancelButton(None)
    progress.setMinimumDuration(500)  # half second
    progress.setWindowTitle(title)

    operation.completed.connect(progress.reset)
    operation.completed.connect(progress.deleteLater)
    operation.completed.connect(self.userEventFilter.disableFilter)
    operation.error.connect(progress.reset)
    operation.error.connect(progress.deleteLater)
    operation.error.connect(self.userEventFilter.disableFilter)

    self.userEventFilter.enableFilter()
    progress.setValue(0)

rawDescription: QProgressDialog not modal before minimum duration


code:
post = Post.objects.get(pk=1)
output = ( post.likes.all().count() + post.comments.all().count() ) / ( now() - post.created )

rawDescription: Django extras() - Logic/algorithm

description: Django extras() - Logic/algorithm


rawDescription: A more pythonic way

description: A more pythonic way


code:
h_desired_index = 1200

ascii_table = list(map(chr, range(128)))
h_index = ascii_table.index('h')
delta_index = h_desired_index - h_index
new_table = {}

for index, character in enumerate(ascii_table):
    new_table[character] = index + delta_index

rawDescription: Script to assign varying numbers to ascii characters


code:
@classmethod
def make_nonce(cls):
    """Generate pseudorandom number."""
    return str(random.randint(0, 100000000))

rawDescription: What is the standard method for generating a nonce in Python?


rawDescription: Numpy - weird behavior with plus equal with slicing


code: row.extend(x[1] for x in emails if x[0] == row[1])

rawDescription: How to combine two lists

description: Combine two lists


rawDescription: Python3: Calling functions saved as values (strings) in a dictionary


code:
file_count = 1
with open('INPUT_FILE.csv') as infile:
    for line in infile:
        if line.strip():
            with open('output_file_{}.txt'.format(file_count), 'w') as outfile:
                outfile.write(line)
                file_count += 1

rawDescription: Create dynamic text files with data from another CSV file using python


rawDescription: Using Python's pickle in Sage results in high memory usage


code:
def min_change(V, C):
    m, n = len(V)+1, C+1
    table = [[0] * n for x in xrange(m)]
    for j in xrange(1, n):
        table[0][j] = float('inf')
    for i in xrange(1, m):
        for j in xrange(1, n):
            aC = table[i][j - V[i-1]] if j - V[i-1] >= 0 else float('inf')
            table[i][j] = min(table[i-1][j], 1 + aC)
    return table[m-1][n-1]

rawDescription: Recursive change-making algorithm

description: Recursive change-making algorithm


code:
for chunk in iter(lambda: fin.read(4096), ''):
    yield chunk

rawDescription: Break a text file into smaller chunks in python


code:
import operator
c = list(map(operator.or_, a, b))

rawDescription: is there a binary OR operator in python that works on arrays?

description: Is there a binary OR operator in python that works on arrays


code:
arr #Original Array
zip(*arr) #Unpacking the array, transposes it
sorted(zip(*arr), key = itemgetter(1)) #Sort the Transpose Array based on second element
zip(*sorted(zip(*arr), key = itemgetter(1))) #Transpose the result to generate the original format

rawDescription: sorting multidimensional-array in python

description: Sorting multidimensional-array in python


code:
# -*- coding: utf-8 -*-
from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('yourappname', '0001_initial'),
    ]

    operations = [
    ]

rawDescription: Django 1.7 where to put the code to add Groups programatically?


code:
for word in lst:
    print(type(u"{}".format(word)))

rawDescription: How to add unicode character before a string? [Python]

description: Add unicode character before a string? [Python]


code:
if response == 'lower':
    highBound = randomNumber - 1

rawDescription: PYTHON - Reverse Number Guessing Game -

description: PYTHON - Reverse Number Guessing Game -


code:
import pandas as pd
import numpy as np

def fit (dataframe):
    return {  "param1": np.mean(dataframe["Term"]) + np.max(dataframe["month"]), "param2": np.std(dataframe["s"])}

rawDescription: pandas groupby apply function that take N-column frame and returns object

description: Pandas groupby apply function that take N-column frame and returns object


code:
import pandas as pd
import time

df = pd.DataFrame({'day': ['Thursday', 'Thursday', 'Friday', 'Friday', 'Saturday', 'Saturday'], 'values': [3,0,0,1,3,1]})
result = df.groupby('day').sum()
# Reseting the index
result.reset_index(inplace=True)
# Creating a new index as the weekday number for each day
result.index = result['day'].apply(lambda x: time.strptime(x, '%A').tm_wday)
# Renaming the index
result.index.names = ['weekday']
# Sorting by index
result.sort_index(inplace=True)
print(result)

rawDescription: correct way to sum values of second column for all unique values of first column pandas dataframe

description: Correct way to sum values of second column for all unique values of first column pandas dataframe


code:
from itertools import chain
item_gen = chain(d.values() for d in my_dicts_list)
# Edit: Note that this will give you values continuously (not what you want), should have done
# item_gen = (d.values() for d in my_dicts_list)

rawDescription: python iterating list of dicts

description: Python iterating list of dicts


code:
def user_in_group(user, group_name):
    return user.groups.filter(name=group_name).count()

rawDescription: Custom Permissions in Django with Problematic Model


code: map(lambda x: x, range(10))

rawDescription: What is the idiomatic way of invoking a list of functions in Python?


code:
shape = (Lmax+1, Lmax+1, maxvalue+1)
posvec = np.arange(1, Lmax+1)

len_idx  = np.repeat(Lvec, Lvec)
pos_idx  = np.broadcast_to(posvec, data.shape)[mask]
val_idx  = data[mask]
Y_copied = np.repeat(Y, Lvec)

mask = posvec <= Lvec[:,None]  # fill-value independent
lin_idx = np.ravel_multi_index((len_idx, pos_idx, val_idx), shape)
Y_avg = np.bincount(lin_idx, weights=Y_copied, minlength=np.prod(shape)) / n
Y_avg.shape = shape

rawDescription: np.bincount for 1 line, vectorized multidimensional averaging

description: Np.bincount for 1 line, vectorized multidimensional averaging


code:
from django.core.files.storage import FileSystemStorage

class MyFileStorage(FileSystemStorage):

    # This method is actually defined in Storage
    def get_available_name(self, name):
      return name # simply returns the name passed

rawDescription: Set Django's FileField to an existing file


code:
import numpy as np
import random

# Input counts matrix
mat = np.array([
    [5, 5, 2],
    [1, 1, 3],
    [6, 0, 4]
], dtype=np.int64)

# Build a list of (row,col) pairs, and a list of counts
keys, counts = zip(*[
    ((i,j), mat[i,j])
        for i in range(mat.shape[0])
        for j in range(mat.shape[1])
        if mat[i,j] > 0
])

rawDescription: Subsample a matrix python

description: Subsample a matrix python


code:
tup = l[0]
tup
x, y = tup
x
y
t, u = l[2] # alternatively
t
u

rawDescription: Store more than 1 value in python array?

description: Store more than 1 value in python array


code:
def sum_div(x, y):
    result_set = set()  # Sets cannot have duplicate values
    for k in range(x,y+1):
        sum_of_x_divisors = sum(get_divisors(k))
        for z in range(x,y+1):
            sum_of_y_divisors = sum(get_divisors(z))
            if sum_of_x_divisors == sum_of_y_divisors and k!= z:
                result_set.add(tuple(sorted((k,z))))  # compile the result set by sorting it and casting to a tuple, so duplicates are implicitly removed.
    for k, z in result_set:  # Print result set after it's been compiled
        print("({},{})".format(k, z))

rawDescription: How to stop a loop?

description: Stop a loop


rawDescription: pythonic way to iterate over a set/remnant set

description: Pythonic way to iterate over a set/remnant set


code:
res = df.groupby(['ticket_number', 'ticket_price'])['item'].apply(list).apply(pd.Series)
res

rawDescription: Pandas pivot or groupby for dynamically generated columns


rawDescription: merge items into a list based on index

description: Merge items into a list based on index


code: print('returning:', roll_die(6, 3, True))

rawDescription: Die roller using python

description: Die roller using python


code: newton(func, x0=0.5, fprime=deriv, tol=1e-12, maxiter=1000)

rawDescription: Finding zeros of a complex function in SciPy/NumPy


code:
class Person(object):
    def __init__(self, nation, name):
        self.nation = nation
        self.name = name

People = [Person("Canada", "Dave"), Person("Scotland", "Alex"), Person("Canada", "Fred"), Person("Japan", "Kenta")]

rawDescription: How to find number of different nations in list of Persons?

description: Find number of different nations in list of Persons


code:
A = numpy.array([1, 2, 3, 2, 3, 1, 2, 1, 3])
B = numpy.array([2, 3, 1, 2])
numpy.fromstring(A.tostring().replace(B.tostring(), ""), dtype=A.dtype)
array([1, 2, 3, 1, 3])
# note, here dtype is some int, I'm relying on the fact that:
# "1 matches 1" is equivalent to "0001 matches 00001"
# this holds as long as values of B are typically non-zero.
#
# this trick can conceptually be used with floating point too,
# but beware of multiple floating point representations of same number

rawDescription: Python: How to find the a unique element pattern from 2 arrays?


code: [p for p in partitions("4824") if not any(len(x) == len(y) == 1 for x, y in zip(p, p[1:]))]

rawDescription: python all combinations of subsets of a string

description: Python all combinations of subsets of a string


code:
geo_polygons = pd.DataFrame({'single_column':polygons}).single_column.apply(lambda x: Polygon(x)).values
geo_points = pd.DataFrame({'single_column':points}).single_column.apply(lambda x: Point(x[0], x[1])).values
# As you might noticed, the arrays have different length.

rawDescription: Apply a pairwise shapely function on two numpy arrays of shapely objects


code:
# if Surface
titleImage.get_rect().width

# if Sprite
titleImage.rect.width

rawDescription: Pygame Menu: calling another .py file

description: Pygame Menu: calling another .py file


code:
from operator import truediv

def f(vs):
    try:
        d = dict(vs)
    except ValueError:
        d = dict([vs])

    s = sum(d.values())
    return truediv(d.get("S", 0), s) if s else float('nan')

rdd = sc.parallelize([('a', ('H', 1)), ('b', (('H', 41), ('S', 1)))])
rdd.mapValues(f).collect()

## [('a', 0.0), ('b', 0.023809523809523808)]

rawDescription: Pyspark: operation on values based on type


code:
import fred
def pr(str):
    print(str*fred.count)

rawDescription: Breaking Python Circular Imports

description: Breaking Python Circular Imports


code:
class MenuMixin(object):
    def get_context_data(self, **kwargs):
        context = super(MenuMixin, self).get_context_data(**kwargs)
        context['menu'] = MenuModel.objects.all()
        return context

class MyListView(MenuMixin, ListView):
    ...

rawDescription: How to make a menu in a Base Template(Django)?

description: Make a menu in a Base Template(Django)


code:
print(mylist)
print(open(myfile).read())

with open(myfile) as f:
    mylist = eval(f.read())
    mylist.append(myitem)

with open(myfile, 'w') as f:
    f.write(str(mylist))

print(mylist)
print(open(myfile).read())

with open(myfile) as f:
    mylist = eval(f.read())
    mylist.remove(myitem)

with open(myfile, 'w') as f:
    f.write(str(mylist))

print(mylist)
print(open(myfile).read())

rawDescription: Python 2.7 - add and remove items from a list in a text file


code:
def calculate(constants, *args):
    num = 0

    for c, v in zip(constants, *args):
        num +=  c*v
    return num

rawDescription: Static lists in Python for arithmetic manipulations


code:
res = "".join(i + j for i, j in zip(u, l))
print(res)
# 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz'

rawDescription: Most pythonic way to interleave two strings


code:
from datetime import datetime, timedelta
from operator import itemgetter

data = [
    (datetime(2015, 1, 1), 666),
    (datetime(2015, 1, 3), 777),
    (datetime(2015, 2, 1), 888),
]

def add_missing(l):
    st = set(map(itemgetter(0), data))
    it = iter(l)
    start, end = data[0][0], data[-1][0]
    for day in range((end - start).days + 1):
        dte = start + timedelta(days=day)
        if dte not in st:
            yield (dte, 0)
        else:
            yield next(it)

data[:] = add_missing(data)

rawDescription: Python - fill the no-used dates

description: Python - fill the no-used dates


code:
PATH = Path(sys.argv[0]).parent.resolve()
image = PATH / "some_image.gif"

rawDescription: Accessing file path in the os

description: Accessing file path in the os


rawDescription: making new column in pandas dataframes based on filter

description: Making new column in pandas dataframes based on filter


code:
def on_copy(self, event):
   self.selection = self.ie.GetStringSelection(asHTML=False)

rawDescription: Wxpython cut copy paste and openfiledialog


code:
from pydub import AudioSegment
from pydub.utils import ratio_to_db, db_to_float

sound = AudioSegment.from_file('/your/file.wav')

# this is roughly -6.0
half_amplitude_in_db = ratio_to_db(0.5)

# these are all roughly the same result
half_amplitude1 = sound.apply_gain(half_amplitude_in_db)
half_amplitude2 = sound.apply_gain(-6.0)
half_amplitude3 = sound - 6.0

# Assuming 16-bit sound, that’s ~90dB dynamic range.
# so -45dB is half way to silent.
# Note: that is A LOT quieter
half_way_to_silent = sound - 45.0

rawDescription: Recreating Web Audio API GainNode behaviour


code: from mybp import mybp

rawDescription: Using route decorator in a Flask blueprint's submodule


code:
conn_pool = None

def makePool(host, port):
    global conn_pool
    pool = conn_pool = HTTPConnectionPool(host=host, port=port, maxsize=1)

def ReqOsrm(url_input):
    ul, qid = url_input

    try:
        response = conn_pool.request('GET', ul)
        json_geocode = json.loads(response.data.decode('utf-8'))
        status = int(json_geocode['status'])
        if status == 200:
            tot_time_s = json_geocode['route_summary']['total_time']
            tot_dist_m = json_geocode['route_summary']['total_distance']
            used_from, used_to = json_geocode['via_points']
            out = [qid, status, tot_time_s, tot_dist_m, used_from[0], used_from[1], used_to[0], used_to[1]]
            return out

        else:
            print("Done but no route: %d %s" % (qid, req_url))
            return [qid, 999, 0, 0, 0, 0, 0, 0]

    except Exception as err:
        print("%s: %d %s" % (err, qid, req_url))
        return [qid, 999, 0, 0, 0, 0, 0, 0]

if __name__ == "__main__":
    # run:
    pool = Pool(initializer=makePool, initargs=('127.0.0.1', 5005))
    calc_routes = pool.map(ReqOsrm, url_routes)
    pool.close()
    pool.join()

rawDescription: Python requests - threads/processes vs. IO


code:
def f(a, b):
  return ((v1[a] - v1[i]) / (v2[a] - v2[i]) for i in xrange(a, b+1))

rawDescription: How to write a generator for this function

description: Write a generator for this function


code:
# setup.py

from setuptools import setup
setup(
    name='mymodule',
    packages=['mymodule'],
    entry_points={
        'console_scripts' : [
            'mycommand = mymodule.script:main',
        ]
    },
    install_requires=[
        'requests',
    ]
)

rawDescription: Include python module (dependencies) installation in your python script


code:
while 1:
    cardNr = int(raw_input('Flip a card, any card of the total ' + str(len(cards)) + ': '))
    cards[cardNr-1].flip()  # -1 because humans don't count from 0 normally :)

rawDescription: Naming/Calling Generated Object w/ Classes


code: loc3 = set(loc1).intersection(loc2)

rawDescription: python: Find matching tuples in the list

description: Python: Find matching tuples in the list


code: sorted(l1, key=lambda l: int(l[2].rstrip(',')))

rawDescription: sort list of lists by specific index of inner list

description: Sort list of lists by specific index of inner list


code:
import os, sys

def relative_dir(apath):
  return os.path.realpath(
      os.path.join(os.path.dirname(apath),
      '../../../Path/To/Shared/Code'))

def addpack(apath):
  relative = relative_dir(apath)
  if relative not in sys.path:
    sys.path.append(relative)

rawDescription: Importing Python modules from a distant directory


code:
with open (absolute_base_file) as csvfile:
    csv_reader = csv.reader(csvfile)

rawDescription: Trying to read the first line of CSV file returns ['/']


rawDescription: How to remove rows with specific missing tags from a Pandas dataframe?

description: Remove rows with specific missing tags from a Pandas dataframe


code:
soup = BeautifulSoup('')
span = soup.find('span')
span
if span.has_key('id'):
    del span['id']

span

rawDescription: How to get the values of span tag in html using beautiful soup?

description: Get the values of span tag in html using beautiful soup


code:
# left borders
if x > 0:
    x = 0

# top borders
if y > 0:
    y = 0

# right borders
if x+image_width < screen_width:
    x = screen_width

# bottom borders
if y+image_height < screen_height:
    y = screen_height

rawDescription: Need a bordered scrolling mechanism in pygame


code: convert('IIrIIrIrIrIIrIIrIrIrII')

rawDescription: Regex: convert a string to a dictionary using dict comprehension and regex


code:
TestEntry = Entry(self, text="", width = 100)
TestEntry.grid(row=4,columnspan=6)

rawDescription: grid_remove on an Entry widget

description: Grid_remove on an Entry widget


rawDescription: Pandas - group by consecutive ranges

description: Pandas - group by consecutive ranges


code:
import numpy as np
from numba import jit, vectorize
import numexpr as ne

def numpy_complex_expr(A, B):
    return(A*B+4.1*A > 2.5*B)

def numexpr_complex_expr(A, B):
    return ne.evaluate('A*B+4.1*A > 2.5*B')

@jit
def numba_complex_expr(A, B):
    return A*B+4.1*A > 2.5*B

@vectorize(['u1(float64, float64)'])
def numba_vec(A,B):
    return A*B+4.1*A > 2.5*B

n = 1000
A = np.random.rand(n,n)
B = np.random.rand(n,n)

rawDescription: How does parakeet differ from Numba? Because I didn't see any improvements on some NumPy expressions


code: foods['Bread']['foodvalue'] # 10

rawDescription: Python: using dictionaries in conjunction with input


code:
mapping = {}
for a, b in list2:
    mapping.setdefault(b, []).append(a)
list3 = [[a, b] + mapping[b] for a, b in list1 if b in mapping]

rawDescription: Mapping 2 lists in Python 2.7

description: Mapping 2 lists in Python 2.7


code: {(i,k): int(x) for i, line in enumerate(Inputstring.splitlines()) for k, x in enumerate(line.split())}

rawDescription: Format for arrays in Python

description: Format for arrays in Python


code: del pkt[TCP].chksum

rawDescription: how to add http headers to a packet sniffed using scapy

description: Add http headers to a packet sniffed using scapy


rawDescription: Convert AWK regex to Python script

description: Convert AWK regex to Python script


code:
from flask import Blueprint

account_api = Blueprint('account_api', __name__)

@account_api.route("/account")
def accountList():
    return "list of accounts"

rawDescription: Split Python Flask app into multiple files


code:
isinstance('a-string', str)
isinstance(['a-string', 'another-string'], str)

rawDescription: Count number of strings

description: Count number of strings


code:
def func(b,*args):
    a = b.T
    total = [a[i]*args[i] for i in range(len(args)-1)]
    if sum(total) < 0:
        return True
    else:
        return False

rawDescription: Numpy to check if a solution exists such that each row is < 0?


code:
fig = plt.figure(figsize=(10, 2), facecolor='w')
fig.subplots_adjust(wspace=0.2)

ax1 = fig.add_axes([0.0, 0, 0.4, 1])
ax2 = fig.add_axes([0.6, 0, 0.4, 1])

ax1.barh(y, diff, align='center')
ax1.set_yticks(y)
ax1.yaxis.set_major_formatter(matplotlib.ticker.NullFormatter())

ax2.barh(y, data, align='center')
ax2.set_yticks(y)
ax2.set_yticklabels(['reference', 'something', 'something else', 'nothing', ],
                    ha='center', position=(-0.25, 0))

rawDescription: Matplotlib: Center tick-labels between subplots


code:
from flask import Flask
from celery import Celery

broker_url = 'amqp://guest@localhost'          # Broker URL for RabbitMQ task queue

app = Flask(__name__)
celery = Celery(app.name, broker=broker_url)
celery.config_from_object('celeryconfig')      # Your celery configurations in a celeryconfig.py

@celery.task(bind=True)
def some_long_task(self, x, y):
    # Do some long task
    ...

@app.route('/render/<id>', methods=['POST'])
def render_script(id=None):
    ...
    data = json.loads(request.data)
    text_list = data.get('text_list')
    final_file = audio_class.render_audio(data=text_list)
    some_long_task.delay(x, y)                 # Call your async task and pass whatever necessary variables
    return Response(
        mimetype='application/json',
        status=200
    )

rawDescription: Making an asynchronous task in Flask

description: Making an asynchronous task in Flask


code:
class Params:
    LINK_IP_ADDR = "10.8.22.89"
    STABILITY_SLEEP = 5
    MAIN_STATE_SLEEP = 30

rawDescription: Python- how can I set the value for params from another file


code:
# Keeps track of the last "seen" day
lastday=0

# Default 1st day if missing
if 1 not in monthvalues:
  monthvalues[1] = 1.23 #you need a default

# Default 31st day if missing
if 31 not in monthvalues:
  monthvalues[31] = 1.23 #you need a default

rawDescription: interpolate number sequence

description: Interpolate number sequence


code:
r,t   = np.meshgrid(np.linspace(r_a, r_b, circles),
                    np.linspace(0, 2 * np.pi, lines))
x = r * np.cos(t)
y = r * np.sin(t)

rawDescription: Creating a 2d mesh in python

description: Creating a 2d mesh in python


code:
@register.simple_tag
def madlibfield(formfield):
   return formfield.label % formfield

rawDescription: Any easy way to do 'mad lib' style form fields in Django?


rawDescription: How to resize subfigures when using ImageGrid from Matplotlib

description: Resize subfigures when using ImageGrid from Matplotlib


code:
import pandas as pd
from pandas import DataFrame
import re

df = DataFrame({'col1': {0: 'mf_lp_gn817_ml46',
 1: 'desktop_300x250_mf',
 2: 'desktop_300x250_mf',
 3: 'desktop_728x90_mf'},
 'col2': {0: 'd_nb_05340.gif ',
 1: 'mf_lp_fb824_ml46.html ',
 2: 'dd_lp_ig805.html ',
 3: 'mf_lp_fb824_ml46.html '}})

regex = r'_([a-z]{2}[0-9]{3})[_.]'
for index, row in df.iterrows():
    for column in row.keys():
        m = re.search(regex, row[column])
        if m is not None:
            df.ix[index, 'col3'] = m.group(1)

rawDescription: Match mixture of characters and digits in Pandas


code:
# -*- coding: utf-8 -*-
import base64
import logging
import os
import urllib
import uuid
import zlib

from flask import Flask
from flask import redirect
from flask import request
from flask import url_for
from flask.ext.login import LoginManager
from flask.ext.login import UserMixin
from flask.ext.login import current_user
from flask.ext.login import login_required
from flask.ext.login import login_user
from saml2 import BINDING_HTTP_POST
from saml2 import BINDING_HTTP_REDIRECT
from saml2 import entity
from saml2.client import Saml2Client
from saml2.config import Config as Saml2Config

# PER APPLICATION configuration settings.
# Each SAML service that you support will have different values here.
idp_settings = {
    u'example.okta.com': {
        u"metadata": {
            "local": [u'./example.okta.com.metadata']
        }
    },
}
app = Flask(__name__)
app.secret_key = str(uuid.uuid4())  # Replace with your secret key
login_manager = LoginManager()
login_manager.setup_app(app)
logging.basicConfig(level=logging.DEBUG)
# Replace this with your own user store
user_store = {}

class User(UserMixin):
    def __init__(self, user_id):
        user = {}
        self.id = None
        self.first_name = None
        self.last_name = None
        try:
            user = user_store[user_id]
            self.id = unicode(user_id)
            self.first_name = user['first_name']
            self.last_name = user['last_name']
        except:
            pass

@login_manager.user_loader
def load_user(user_id):
    return User(user_id)

@app.route("/")
def main_page():
    return "Hello"

@app.route("/saml/sso/<idp_name>", methods=['POST'])
def idp_initiated(idp_name):
    settings = idp_settings[idp_name]
    settings['service'] = {
        'sp': {
            'endpoints': {
                'assertion_consumer_service': [
                    (request.url, BINDING_HTTP_REDIRECT),
                    (request.url, BINDING_HTTP_POST)
                ],
            },
            # Don't verify that the incoming requests originate from us via
            # the built-in cache for authn request ids in pysaml2
            'allow_unsolicited': True,
            'authn_requests_signed': False,
            'logout_requests_signed': True,
            'want_assertions_signed': True,
            'want_response_signed': False,
        },
    }

    spConfig = Saml2Config()
    spConfig.load(settings)
    spConfig.allow_unknown_attributes = True

    cli = Saml2Client(config=spConfig)
    try:
        authn_response = cli.parse_authn_request_response(
            request.form['SAMLResponse'],
            entity.BINDING_HTTP_POST)
        authn_response.get_identity()
        user_info = authn_response.get_subject()
        username = user_info.text
        valid = True
    except Exception as e:
        logging.error(e)
        valid = False
        return str(e), 401

    # "JIT provisioning"
    if username not in user_store:
        user_store[username] = {
            'first_name': authn_response.ava['FirstName'][0],
            'last_name': authn_response.ava['LastName'][0],
            }
    user = User(username)
    login_user(user)
    # TODO: If it exists, redirect to request.form['RelayState']
    return redirect(url_for('user'))

@app.route("/user")
@login_required
def user():
    msg = u"Hello {user.first_name} {user.last_name}".format(user=current_user)
    return msg

if __name__ == "__main__":
    port = int(os.environ.get('PORT', 5000))
    if port == 5000:
        app.debug = True
    app.run(host='0.0.0.0', port=port)

rawDescription: Saml2 Service Provider in Python

description: Saml2 Service Provider in Python


code:
from multiprocessing import Pool
import time, pprint

def fun(av, bv):
    time.sleep(0.1)
    return (av, bv)

def data_stream(a, b):
    for i, av in enumerate(a):
        for j, bv in enumerate(b):
            yield (i, j), (av, bv)

def proxy(args):
    return args[0], fun(*args[1])

a = range(100, 400, 100)
b = range(100, 400, 100)
Y = {}

pool = Pool(processes=4)
results = pool.map(proxy, data_stream(a, b))
for k,v in results:
    Y[k] = v

pprint.pprint(Y)

rawDescription: Python multiprocessing for expensive operation with 2D array


rawDescription: Filtering a dictionary in Python ... again


code: mean = np.mean(y_avg[5:15])

rawDescription: Computing averages for a group of neighboring points in a line


code:
def second_lowest(l):
    l.sort()
    return l[1]

map(second_lowest, lst)

rawDescription: Is it possible to have multiple statements in a python lambda expression?


code:
class MyClass(object):
    def __init__(self):
        self.get_config()
        self.validate_config()
    def get_config(self):
        self.message = "hello"
    def validate_config(self):
        # I'm not claiming this is good practice, just
        # an example of multiple methods that share state
        if not self.message:
            raise Exception()
    def hello(self):
        print(self.message)

def main():
    MyClass().hello()

if __name__ == "__main__":
    main()

rawDescription: What is the pythonic way to instantiate a program based on a class


code:
thread = next(data['threads'].values())
print(thread['date'])

rawDescription: How do I navigate through a dictionary like this?

description: Navigate through a dictionary like this


code:
type(a)
type(a_expr)

rawDescription: How to evaluate an expression-tree in sympy?

description: Evaluate an expression-tree in sympy


code:
p = Parent()
c = Child()
p.children.append(c)

session.add(p)
session.add(c)
session.commit()

rawDescription: SQLAlchemy retrieve one-to-many entities by subquery


code:
('NL', '\n')
('NAME', 'x')
('OP', '=')
('STRING', "'hello there'")
('NEWLINE', '\n')
('NAME', 'if')
('NAME', 'x')
('OP', '==')
('STRING', '\'example "quotes" inside quotes\'')
('NAME', 'and')
('NAME', 'y')
('OP', '==')
('STRING', "'another example'")
('OP', ':')
('NAME', 'pass')
('NEWLINE', '\n')
('ENDMARKER', '')

rawDescription: Parsing python source into html - quotes


code:
finder = BigramCollocationFinder.from_words(nltk.corpus.genesis.words('english-web.txt'))
finder = AbstractCollocationFinder.from_words(nltk.corpus.genesis.words('english-web.txt',5))

rawDescription: How to get n-gram collocations and association in python nltk?

description: Get n-gram collocations and association in python nltk


code:
class Element(object):
    def __init__(self, elt_id, elt_description=None):
        self.id = elt_id
        self.description = elt_description
        if self.description is None:
            self.description = self.id

    def __or__(self, elt):
        return CombinedElement(self, elt, op="OR")

    def __and__(self, elt):
        return CombinedElement(self, elt, op="AND")

    def __invert__(self):
        return CombinedElement(self, op="NOT")

    def __str__(self):
        return self.id

class CombinedElement(Element):
    def __init__(self, elt1, elt2=None, op="NOT"):
        # ID1
        id1 = elt1.id
        if isinstance(elt1, CombinedElement):
            id1 = '('+id1+')'
        # ID2
        if elt2 is not None:
            id2 = elt2.id
            if isinstance(elt2, CombinedElement):
                id2 = '('+id2+')'
        # ELT_ID
        if op == "NOT" and elt2 is None:
            elt_id = "~"+id1
        elif op == "OR":
            elt_id = id1+" v "+id2
        elif op == "AND":
            elt_id = id1+" ^ "+id2
        # SUPER
        super(CombinedElement, self).__init__(elt_id)

a = Element("A")
b = Element("B")
c = Element("C")
d = Element("D")
e = Element("E")

print(a&b|~(c&d)|~e)

rawDescription: How do I deal with a logical expression in Python?

description: Deal with a logical expression in Python


rawDescription: Python 2.6 optimize nested loops

description: Python 2.6 optimize nested loops


rawDescription: Does my code prevent directory traversal?


code:
import dill
f = lambda x: x * 5
dill.dumps(f)

rawDescription: Python, cPickle, pickling lambda functions


code: application = webapp.WSGIApplication([(urllib.quote("/Ñöñ-ÅŞÇİİ"), MainPage)], debug=True)

rawDescription: Use non-ascii charachters in an app engine application


code:
b_object_list = B.objects.filter(name__in=['Bob', 'Jim'])
A.objects.filter(bs__in=b_object_list)

rawDescription: Django getting related objects in a many-to-many relationship


code:
logger = logging.getLogger('test.important_module')
logger.setLevel(logging.DEBUG)

rawDescription: Logging handlers not working without basicConfig


code:
import sqlite3
conn=sqlite3.connect(':memory:')
conn.execute('CREATE TABLE test (myCol)')
conn.commit()
myList = ('foo', 'bar', 'spam')
conn.executemany('INSERT into test values (?)', ((val,) for val in myList))
list(conn.execute('select * from test'))

rawDescription: How to update entire column with values in list using Sqlite3

description: Update entire column with values in list using Sqlite3


code:
# here is how you can get them
credentials = StorageByKeyName(CredentialsModel, user.user_id(),'credentials').locked_get()
...

rawDescription: Storing Credentials for Offline Access in Datastore


code:
from fractions import gcd
gcd(a,b)

rawDescription: Where should print function be used here?


code:
min_x = min(particleCoords.iteritems(),key = lambda item: item[1][0])
max_x = max(particleCoords.iteritems(),key = lambda item: item[1][0])

min_y = min(particleCoords.iteritems(),key = lambda item: item[1][1])
max_y = max(particleCoords.iteritems(),key = lambda item: item[1][1])

rawDescription: Finding Max and Min coordinates for X and Y in a dictionary


code:
class ObjectOne(models.Model):
    logo = models.ImageField(...)

class ObjectTwo(models.Model):
    logo = models.ImageField(...)

from django.db.models.signals import post_save
from signals import my_signal
post_save.connect(my_signal, dispatch_uid="001")

rawDescription: Copy the contents of an ImageField to a new file path in Django


code:
def doSomething():
    # Calculate some fancy stuff here
    global counter
    counter += 1

rawDescription: Is there any way to create an action counter for a script? (Python)


code:
from raven.middleware import Sentry
from raven.utils.wsgi import get_current_url, get_headers, get_environ
from raven.base import Client

def sentry_filter_factory(app, global_conf, **kwargs):
    """ Overwritten just to override the 'Sentry' class being used. """
    client = Client(**kwargs)
    return UserEnhancedSentry(app, client)

class UserEnhancedSentry(Sentry):
    """ Overriding raven.middleware.Sentry's handle_exception() method to inject
        the sentry.interfaces.User interface. This relies on the data previously
        being injected into the environ by our custom tween. """
    def handle_exception(self, environ):
        data={}
        data['sentry.interfaces.Http'] = {
            'method': environ.get('REQUEST_METHOD'),
            'url': get_current_url(environ, strip_querystring=True),
            'query_string': environ.get('QUERY_STRING'),
            # TODO
            # 'data': environ.get('wsgi.input'),
            'headers': dict(get_headers(environ)),
            'env': dict(get_environ(environ)),
        }
        if environ.has_key('myapp.user'):
            user_id, username, email = environ.get('myapp.user')
            ip_address = environ.get('HTTP_X_FORWARDED_FOR', environ.get('REMOTE_ADDR'))
            data['sentry.interfaces.User'] = {
                'id': user_id,
                'username': username,
                'email': email,
                'ip_address': ip_address,
            }
        event_id = self.client.captureException(data=data)
        return event_id

rawDescription: How do I report the currently logged in user to Sentry?

description: Report the currently logged in user to Sentry


code:
food_entry = Text(new_window_entry, width = 55, height = 2, wrap = WORD)
food_entry.grid(sticky = W, columnspan = 2)

rawDescription: Tkinter getting data from an entry box and sending it to other functions


code:
ind = np.triu_indices(4)
values = np.arange(16).reshape(4,4)[ind]
result = np.zeros((4,4),int)
result[ind]=values
values
result

rawDescription: Converting 1D numpy array into top triangle of matrix


code:
import numpy as np
import numba

# You could also just use @numba.jit or @numba.jit(nopython=True)
# here and get comparable timings.
@numba.jit('void(uint8[:])', nopython=True)
def primes_util(sieve):
    ssz = sieve.shape[0]
    for j in xrange(ssz):
        if sieve[j]:
            new_prime = 2*j + 3
            for k in xrange((2*j+6)*j+3, ssz, new_prime):
                sieve[k] = False

def primes_numba(limit):
    sieve = np.ones(limit // 2, dtype=np.uint8)
    primes_util(sieve)

    return [2] + (np.nonzero(sieve)[0]*2 + 3).tolist()

rawDescription: Numba slowing Code?

description: Numba slowing Code


code:
def foo(x, y):
    print(x, y)

coordinates = (42, 38)
foo(*coordinates)

rawDescription: Duck Typing and acceptance of tuples or integers in Python 3


rawDescription: Python strptime takes weekday name and returns tm_mday = 1 regardless of actual mday


code:
    #  OUTPUT (some newlines added)
    #
    #  [Manager] Subjects:A[ a1 ],A[ a2 ], Observers:B[ b1 ],B[ b2 ],B[ b3 ],B[ b4 ]
    #
    #  Deleting a1...
    #  A[ a1 ].__del__
    #  B[ b1 ].__del__
    #  B[ b2 ].__del__
    #  Removing dead subjects...
    #  Removing dead observers...
    #  [Manager] Subjects:A[ a2 ], Observers:B[ b3 ],B[ b4 ]
    #
    #  Deleting a2...
    #  A[ a2 ].__del__
    #  B[ b3 ].__del__
    #  B[ b4 ].__del__
    #  Removing dead subjects...
    #  Removing dead observers...
    #
    #  [Manager] Subjects:, Observers:

rawDescription: Using the observer pattern to delete an Observer when a Subject has been "deleted" in Python


code:
def readFile(self):

    f = open('contacts.txt', 'r')
    lines = f.readlines()
    cmd = str(input("Type in a contact REFERENCE name.\n"))
    lookup = cmd

    with open('contacts.txt') as myFile:
        for num, line in enumerate(myFile, 1):
            if lookup.upper() in line:

                print(lines[num - 1])
                print(lines[num])
                print(lines[num + 1])
                print(lines[num + 2])
                print(lines[num + 3])
                break
        else:
            print("Contact not found.")
    self.managerMenu()

rawDescription: Python Not finding phrase on any line other than first?


rawDescription: python csv into dictionary

description: Python csv into dictionary


code:
def mount(self, root, script_name="", config=None):
    ...

rawDescription: Python CherryPy server, what does cherrypy.Application do?


code: parsed.find('a').previousSibling # or something like that

rawDescription: Use BeautifulSoup to extract text before the first child tag


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

" Problem setup. "
# m**3/s**2, from EGM96.
GM = 3986004.415E8
# m, from EGM96.
a = 6378136.3
# Number of lattitudes and longitudes used to plot the geoid.
N_POINTS = 50
# Geocentric     latitudes and longitudes where the geoid will be visualised.
latitudes = np.linspace(0, 2*np.pi, N_POINTS)
longitudes = np.linspace(0, np.pi, N_POINTS)
# Radius at which the equipotential will be computed, m.
radius = 6378136.3
# Maximum degree of the geopotential to visualise.
MAX_DEGREE = 2

" EGM96 coefficients - minimal working example. "
Ccoeffs={2:[-0.000484165371736, -1.86987635955e-10, 2.43914352398e-06]}
Scoeffs={2:[0.0, 1.19528012031e-09, -1.40016683654e-06]}

" Compute the gravitational potential at the desired locations. "
# Gravitational potentials computed with the given geoid. Start with ones and
# just add the geoid corrections.
gravitationalPotentials = np.ones( latitudes.shape )

# Go through all the desired orders and compute the geoid corrections to the
# sphere.
for degree in range(2, MAX_DEGREE+1):
    # Contribution to the potential from the current degree and all
    # corresponding orders.
    temp = 0.
    # Legendre polynomial coefficients corresponding to the current degree.
    legendreCoeffs = special.legendre(degree)
    # Go through all the orders corresponding to the currently evaluated degree.
    for order in range(degree):
        temp += (legendreCoeffs[order]
                 * np.sin(latitudes)
                 * (Ccoeffs[degree][order]*np.cos( order*longitudes )
                    + Scoeffs[degree][order]*np.sin( order*longitudes )))

    # Add the contribution from the current degree.
    gravitationalPotentials = math.pow(a/radius, degree) * temp

# Final correction.
gravitationalPotentials *= GM/radius

" FIGURE THAT SHOWS THE SPHERICAL HARMONICS. "
fig = plt.figure(figsize=(12,8))
ax = Axes3D(fig)
ax.set_aspect("equal")
ax.view_init(elev=45., azim=45.)
ax.set_xlim([-1.5*radius, 1.5*radius])
ax.set_ylim([-1.5*radius, 1.5*radius])
ax.set_zlim([-1.5*radius, 1.5*radius])

# Make sure the shape of the potentials is the same as the points used to plot
# the sphere.

# Don't need the second copy of colours returned by np.meshgrid
gravitationalPotentialsPlot = np.meshgrid(
    gravitationalPotentials, gravitationalPotentials )[0]
# Normalise to [0 1]
gravitationalPotentialsPlot /= gravitationalPotentialsPlot.max()

" Plot a sphere. "
Xs = radius * np.outer(np.cos(latitudes), np.sin(longitudes))
Ys = radius * np.outer(np.sin(latitudes), np.sin(longitudes))
Zs = radius * np.outer(np.ones(latitudes.size), np.cos(longitudes))
equipotential = ax.plot_surface(
    Xs, Ys, Zs, facecolors=plt.get_cmap('jet')(gravitationalPotentialsPlot),
    rstride=1, cstride=1, linewidth=0, antialiased=False, shade=False)

plt.show()

rawDescription: Matplotlib plot_surface spurious faces

description: Matplotlib plot_surface spurious faces


code: os.kill(programID.pid, signal.SIGCONT)

rawDescription: Is there a way to pause a Python subprocess, specifically in Ubuntu?


rawDescription: merging values in rows dependent on values in columns

description: Merging values in rows dependent on values in columns


code:
for char in text:
    if char in chars:
        print("Character {0} found in row {1}".format(char, chars[char]))

rawDescription: Python string comparison whilst in loop

description: Python string comparison whilst in loop


code:
# y.py
def foo():
    raise RuntimeError("Nope.")

rawDescription: How can I replace a module without importing it?

description: Replace a module without importing it


rawDescription: How does this code snippet rotating a matrix work?


code:
params = dict(('@param{0}'.format(i), v) for i, v in enumerate(cus2))
sql = "select * from dataBase where cus IN ({0})".format(
    ', '.join(sorted(params.keys())))

cur = psql.execute(sql, con, params=params)
rows = cur.fetchall()
columns = [col_desc[0] for col_desc in cur.description]
cur.close()

result = DataFrame.from_records(rows, columns=columns, coerce_float=True)

rawDescription: Remove Brackets from Python List for an SQL query


code:
test("--van")
test("--car --id 123")
test("--model_no 55 --van")
test("--car --id") #invalid because there is no request value

rawDescription: Advice for using argparse

description: Advice for using argparse


code:
df.set_index(['ticker','date'], inplace=True)
df.sort_index(inplace=True)
df['diffs'] = np.nan

for idx in df.index.levels[0]:
    df.diffs[idx] = df.value[idx].diff()

rawDescription: Computing diffs within groups of a dataframe


code:
class YourModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.CharField: {'widget': TextInput(attrs={'size':'20'})},
        models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},
    }

admin.site.register(YourModel, YourModelAdmin)

rawDescription: Resize fields in Django Admin

description: Resize fields in Django Admin


code: left = int(math.log10(x))+1 if x >= 10 else 1

rawDescription: How to get the exact number of positions after a decimal for the round() function

description: Get the exact number of positions after a decimal for the round() function


code: result

rawDescription: Ranking values in a Dictionary (and taking care of ex-aequos correctly)


code:
class B(A):
    @property
    def attr(self):
        return super(B, self).attr

rawDescription: Overriding inherited properties’ getters and setters in Python


code:
from endless_pagination.views import AjaxListView

url(r'^entries/$', AjaxListView.as_view(model=Entry))

rawDescription: django-endless with class based views example

description: Django-endless with class based views example


code: P_centered = P - Ax_support

rawDescription: Finding the distance of points to an axis


code:
class MyFirstComponentTestCase(ComponentTestCase):
    xmlid = 'component1'
    var = 'one'

class MySecondComponentTestCase(ComponentTestCase):
    xmlid = 'component2'
    var = 'two'

rawDescription: python unittest multiple mixins

description: Python unittest multiple mixins


fname: func

rawDescription: Is it possible to do "extended" time monitoring in Python using a decorator?


code:
p = re.compile(r'^something/(?P<id1>\d+)/(?P<id2>\d+)/(?:(?P<id3>\d+))?(?:/(?P<id4>\d+)/?)?$')
import re
p.match('something/1/2/3/4').groups()
p.match('something/1/2/3/').groups()
p.match('something/1/2/3').groups()
p.match('something/1/2/').groups()

rawDescription: Passing 4 digits to view

description: Passing 4 digits to view


code:
urlpatterns = patterns("mine.views",
        url(r'^', 'csvupload'),)

rawDescription: django read file for importing details

description: Django read file for importing details


code: dd.groupby(lambda x: x.month).mean()

rawDescription: Average of a groupby or a groupby of an average on a dataframe with duplicated indexes


code:
output_list = []
prop_fields = [k for k in input_d[0] if k.startswith('o')]

# if keys are found:
if prop_fields:
    for k, g in itertools.group_by(input_d, key=operator.itemgetter(*prop_fields)):
        output_list.append(list(g))
else:
    output_list = [[d] for d in input_d]
return output_list

rawDescription: Sorting data dynamically based upon key names and value combos