code:
from pyspark.accumulators import AccumulatorParam

class VectorAccumulatorParam(AccumulatorParam):
    def zero(self, value):
        return []
    def addInPlace(self, val1, val2):
        return val1 + [val2] if type(val2) != list else val2 #Had to do this check because without it the result would be a list with all the tuples inside of another list.

rawDescription: How to change a global variable value inside a task map or reduce in Apache Spark using Python

description: Change a global variable value inside a task map or reduce in Apache Spark using Python


rawDescription: Python / assign tuple

description: Python / assign tuple


code:
def row_value(row):
    return float(row.split(" ")[2])

for rows in grouped_rows.values():
    has_positive = any([ row_value(row) >= 0 for row in rows ])
    has_negative = any([ row_value(row) < 0 for row in rows ])

    if has_positive and not has_negative:
        write_rows(file_1, rows)
    elif has_positive and has_negative:
        write_rows(file_2, rows)
    else:
        write_rows(file_3, rows)

rawDescription: Categorize the data into three groups (positive,negative) (positive,positive) (negative,negative) based on $3 and $2


rawDescription: Python: how to search dictionary and copy values?


code: df.groupby('Cancer Stage').mean()

rawDescription: Pandas Building a subtable based on means


rawDescription: Given f, is there an automatic way to calculate fprime for Newton's method?


code:
from neo4jrestclient.client import GraphDatabase
gdb = GraphDatabase("http://localhost:7474/db/data/")
idx =  gdb.nodes.indexes.create("urltags")
url_node = gdb.nodes.create(url="http://foo.bar", type="URL")
tag_node = gdb.nodes.create(tag="foobar", type="TAG")

rawDescription: Indexing nodes in neo4j in python

description: Indexing nodes in neo4j in python


code:
s1 = df.apply((lambda x: {"deviceid": x["deviceid"], "devicetype": [x["devicetype"]]}), axis=1)
s1[0]

rawDescription: Pandas DataFrame to Dict Format with new Keys


code:
class BlogPostSerializer(serializer.ModelSerializer):
    # ...
    like_by_request_user = BlogPostLikesSerializer(source='get_last_like', required=False)

rawDescription: Django (django-rest-framework) looking for a best practice to find out wether request.user exists in blog.likes


code:
def normalize(s):
    return ''.join(normalize_char(c) for c in s)

normalize('Móżdżek') == 'Mozdzek'

rawDescription: Translating letters not in 7bit ASCII to ASCII (like ń to n and ą to a)


code:
def possibilities(available_products, target_price):
    if target_price == 0 or not available_products:
        return []
    this_price = available_products[0]
    remaining_products = available_products[1:]
    results = []
    for qty in range(1 + target_price / this_price):
        remaining_price = target_price - qty*this_price
        if remaining_price == 0:
            results.append([qty] + [0] * len(remaining_products))
        else:
            for option in possibilities(remaining_products, remaining_price):
                results.append([qty] + option)
    return results

rawDescription: How do I get a list of every possible combination of product prices to reach a target cost?

description: Get a list of every possible combination of product prices to reach a target cost


code:
class TestSomething( unittest.TestCase ):
    def setUp( self ):
        self.requiredSomething = FakeSomethingOrOther()
        self.requiredSomething.attribute_required_by_test= 12
        self.requiredSomething.special_answer_required_by_test = 32
        self.to_be_tested = ActualThing( self.requiredSomething )

rawDescription: Python library for creating stubs/fake objects


code: ln -s /Users/xyz/long/absolute/path/to/script.py /usr/bin/script.py

rawDescription: How to define a system-wide alias for a Python script?

description: Define a system-wide alias for a Python script


code:
for i in range(10):
    for j in range(i, 10):
        print("{}x{}={}".format(j, i, i*j), end=" ")
    print()

rawDescription: Python Multiplication Table

description: Python Multiplication Table


rawDescription: Convert list of lists to custom dictionary


rawDescription: Writing to a .txt file (UTF-8), python

description: Writing to a .txt file (UTF-8), python


code:
while argument:
    counter+=1
    clock.tick(FRAMERATE)

rawDescription: How to only get a key entry once per second? (or delay the time in between two keyboard entry)

description: Only get a key entry once per second? (or delay the time in between two keyboard entry)


rawDescription: what is the efficient way to work with matrix in numpy

description: What is the efficient way to work with matrix in numpy


code:
from django.conf import settings

@property
def picture_url(self):
    if self.picture:
        result = self.picture.url
    else:
        return settings.STATIC_URL + 'images/default-picture.png'

rawDescription: Media_url for uploaded image or static_url for default picture


code:
sifra_in_file = set()
kategorija_in_file = set()
with open("komad_namestaja.txt", "r") as namestaj:
    for line in namestaj:
        line = line.trim()  # Remove trailing '\n'
        sifra, naziv, boja, kolicina, cena, kategorija = line.split('|')  # Assume no field has '|'
        sifra_in_file.add(sifra)
        kategorija_in_file.add(kategorija)

rawDescription: Writing new line in file with unique parameter


code:
{country_name:
 {'location': location, 'population': population, 'president': president}}

rawDescription: python, how to update the dictionaries by mutating its format?

description: Python, how to update the dictionaries by mutating its format


rawDescription: How do I parse with LXML recursively in an elegant way?

description: Parse with LXML recursively in an elegant way


code:
proc = subprocess.Popen(
    'python %s/python_repeat_deckgen_remote.py -m %s' % (path, module),
    shell = True)
status = proc.wait()

rawDescription: Issues with subprocess.Popen() replacing os.system()


code:
a=[]
cols=3
rows=2
a = [[0]*rows for i in range(cols)]
a

rawDescription: How to print a matrix of a multiple lists

description: Print a matrix of a multiple lists


code:
def function(variable):
    if variable == 9:
        #do these things
        variable = 0
    elif variable > 9:
        variable += 1
        #do these things until its time to
        #do the other stuff
    return variable

rawDescription: in Python Is there ever a good time to use a global variable inside a class?

description: In Python Is there ever a good time to use a global variable inside a class


code: [sublist for sublist in a['Mylist'] if sublist[1] != 8]

rawDescription: eliminate entries in dictionary based on a certain value

description: Eliminate entries in dictionary based on a certain value


code:
from win32com.client import GetObject
wim = GetObject('winmgmts:')
print([o.Caption for o in wim.ExecQuery("Select * from Win32_OperatingSystem")][0])

rawDescription: How to determine Windows Edition in Python with using Squish QT?

description: Determine Windows Edition in Python with using Squish QT


code:
# generate sample data
data = Roessler().integrate(np.arange(0,1000,0.1))[:,2]
data += np.random.normal(size=data.shape)

rawDescription: Most efficient way to filter a long time series Python


rawDescription: For loop when iterating over lines in a file?


code:
import Tkinter as tk
from multiprocessing import Event,Process,Pipe
from time import sleep

# count from 0 to infinity, writing the value to a pipe
def count(pipe,stop):
    ii = 0
    while not stop.is_set():
        ii+=1
        pipe.send(ii)
        sleep(1)

class UpdatingGUI(tk.Frame):
    def __init__(self,parent):
        tk.Frame.__init__(self,parent)
        self.parent = parent
        self.parent_pipe, self.child_pipe = Pipe()
        self.stop_event = Event()

        # label to show count value
        self.updating_txt = tk.StringVar()
        self.updating_txt.set("Waiting...")
        self.updating_lbl = tk.Label(self,textvariable=self.updating_txt)
        self.updating_lbl.pack()

        # button that will stay responsive to requests while count is on going
        self.quit_btn = tk.Button(self,text="Quit",command=self.quit)
        self.quit_btn.pack()

        # launch count as a process
        self.counter = Process(target=count,args=(self.child_pipe,self.stop_event))
        self.counter.start()

        # call an update method to check the pipe and update the label
        self.update()

    def quit(self):
        self.stop_event.set()
        self.parent.destroy()

    def update(self):
        # While the pipe has data, read and update the StringVar
        while self.parent_pipe.poll():
            self.updating_txt.set(self.parent_pipe.recv())

        # set the update method to run again in 1 seconds time
        self.parent.after(1000,self.update)

def main():
    root = tk.Tk()
    gui = UpdatingGUI(root)
    gui.pack()
    root.mainloop()

if __name__ == "__main__":
    main()

rawDescription: Python: How to update a GUI with variables from a separate process while maintaining GUI interactivity


code:
from pyparsing import Suppress, QuotedString, Forward, Group, Dict, ZeroOrMore

LBRACE,RBRACE = map(Suppress, "{}")
qs = QuotedString('"')

# forward-declare value, since this expression will be recursive
# (will contain expressions which use contain value's)
value = Forward()

key_value = Group(qs + value)
struct = LBRACE + Dict(ZeroOrMore(key_value)) + RBRACE

# define content of value using <<= operator
value <<= qs | struct

# define top-level parser
parser = Dict(key_value)

rawDescription: Parsing a CS:GO script file in Python

description: Parsing a CS:GO script file in Python


code:
t = turtle.Pen()
turtle_command = input()
command = commands[turtle_command]

command(t)

rawDescription: How To create a shortcut for a command in python 3.3


code: im = Image.open(file)

rawDescription: Open process and save specific images in related folder


code: [(sprite, timer), (sprite, timer), ...]

rawDescription: Pygame sprites timer

description: Pygame sprites timer


code:
['200 blah', '400 bar', 'foobar bar']
['Foobar', 'Bar']

rawDescription: Match everything until certain pattern

description: Match everything until certain pattern


code:
def annualize_spend_365(row):
    if row['days']/(float(365)) < 1:
        return (row['spend_365day']/(row['days']/float(365)))
    else:
        return row['spend_365day']

rawDescription: Applying similar functions across multiple columns in python/pandas


code:
class Token(object):
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return "<Token %s>" % self.id

rawDescription: Debugger attribute view for python

description: Debugger attribute view for python


rawDescription: Python, Tkinter: How to get the handle of all canvas objects using their IDs or tags?


code:
clupdate = Command()
args = ['--type','opinions','--solr-url','dummy']
kwargs = {
    'solr_url': 'http://127.0.0.1:8983/solr/collection1',
    #'type': 'opinions',
    'update': True,
    'everything': True,
    'do_commit': True,
    'traceback': True,
}
call_command(clupdate, *args, **kwargs)

rawDescription: call_command argument is required

description: Call_command argument is required


code: set(mammals) & set(apes)

rawDescription: Cross Reference 2 lists in Python 2.x

description: Cross Reference 2 lists in Python 2.x


code: oldest = min(datetimes)

rawDescription: Find oldest/youngest datetime object in a list


code: tag = models.OneToOneField(Tag)

rawDescription: Django ManyToMany realtionship doesn't work


code: pCorp = [ re.sub(r'\b(\S+?)\1\S*\b', '', line) for line in pCorp ]

rawDescription: Text mining and NLP: from R to Python

description: Text mining and NLP: from R to Python


code:
res.loc['2000-01-01']  # KeyError without using loc
res.loc[pd.Timestamp('2000-01-01')]

rawDescription: Index a Pandas dataframe grouped by date and aggregated?


code:
...
content = response.read()
data = json.loads(content)
if not data.get('results', {}).get('companies'):
    break
...

rawDescription: Check response using urllib2

description: Check response using urllib2


code:
from module import changename
from module import getname

print(getname())
changename()
print(getname())

rawDescription: Python: Import all variables

description: Python: Import all variables


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

method=2
fs=12
height=600
width=3200
my_dpi=200

x, y = np.meshgrid(np.linspace(0,1000,100),np.linspace(0,31*100,32))
if method ==1:
    z=np.random.rand(32,100)
else:
    z=-3*np.random.rand(32,100)

fig = plt.figure(figsize=(width/my_dpi, height/my_dpi), dpi=my_dpi)
ax = fig.add_subplot(111)
a = ax.pcolor(x,y,z,cmap='jet', vmin=z.min(), vmax=z.max())
ax.set_ylabel('Height above Radar [m]',fontsize=fs)
ax.set_xlabel('Time UTC',fontsize=fs)
ax.set_title("MRR roof 20150729")
plt.axis([x.min(), x.max(),y.min(), y.max()])

cbar = plt.colorbar(a,pad=0.01)
cbar_title='Reflectivity [dBZ]'
plt.text(1.065, .5, cbar_title, fontsize=fs, verticalalignment='center',
         rotation = 90, transform = ax.transAxes)

plotname="somename"+str(method)+".png"
plt.savefig(plotname,dpi=my_dpi, bbox_inches='tight')

rawDescription: How can I keep the bbox tight on 3 sides and constant for different plots using colorbar

description: Keep the bbox tight on 3 sides and constant for different plots using colorbar


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

def plotData(inData,color):
    x,y = zip(*inData)

    xMap = assignIDs(x)
    xAsInts = np.array([xMap[i] for i in x])

    pearR = np.corrcoef(xAsInts,y)[1,0]
    # least squares from:
    # http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.lstsq.html
    A = np.vstack([xAsInts,np.ones(len(xAsInts))]).T
    m,c = np.linalg.lstsq(A,np.array(y))[0]

    plt.scatter(xAsInts,y,label='Data '+color,color=color)
    plt.plot(xAsInts,xAsInts*m+c,color=color,
             label="Fit %6s, r = %6.2e"%(color,pearR))
    plt.xticks(xMap.values(),xMap.keys())
    plt.legend(loc=3)

rawDescription: Drawing a correlation graph in matplotlib


code:
def __setitem__(self, n, val):
    maxindex = n
    if isinstance(maxindex, slice):
        maxindex = maxindex.indices(len(self))[1]
    while len(self) <= maxindex:
        self.append(self.dft)
    return super(ilist, self).__setitem__(n, val)

rawDescription: Is there a simple way to override the list object's method __getitem__?


code:
from collections import Counter
from operator import itemgetter
print(Counter(map(itemgetter(3), lst)))
# Counter({5: 3, 4: 1}) and it will act as a normal dictionary

rawDescription: Finding occurence in list of lists

description: Finding occurence in list of lists


code:
for wid in container.winfo_children():
    if type(wid).__name__ == "Entry":
        wid.destroy()

rawDescription: Python grid_remove

description: Python grid_remove


code:
with open(filename) as f:
    for word in words(f):
        do_stuff(word)

rawDescription: Anyway to input next string from a file in python


code:
namespace = {'dummyfunction': dummyfunction}
#or
namespace = {f.__name__:f for f in [dummyfunction]}

rawDescription: getting string representation of function from a list having function name and arguments

description: Getting string representation of function from a list having function name and arguments


code:
replace = {}
with open('input1') as fd:
    for line in fd:
        head, tail = line.split('=')
        for idt in tail.split(','):
            replace[idt] = head

rawDescription: Multiple word replacement by list - duplicates creation


code:
plt.rc('text', usetex=True)
plt.rc('font', family='serif')
plt.rcParams['xtick.major.pad'] = 10
plt.rcParams['ytick.major.pad'] = 10

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

rawDescription: Axis and tick labels are bold when they shouldn't be - Matplotlib


code:
var=0
def f():
    global var
    var=1
f()
print(var)
# 1  <---- the var outside the "def f" block is affected by calling f()

rawDescription: Python: Difference between 'global' & globals().update(var)


code:
def counts_all(targets, paragraph, word_regex=r"\w+"):
    tally = dict((target, 0) for target in targets)
    for word in re.findall(word_regex, paragraph):
        if word in tally:
            tally[word] += 1
    return [tally[target] for target in targets]

def counts_iter(targets, paragraph, word_regex=r"\w+"):
    tally = dict((target, 0) for target in targets)
    for matchobj in re.finditer(word_regex, paragraph):
        word = matchobj.group()
        if word in tally:
            tally[word] += 1
    return [tally[target] for target in targets]

rawDescription: Using map() to get number of times list elements exist in a string in Python


code:
transform_gen = (transform(y) for y in some_iterable)
filtered_list = [x for x in transform_gen if condition(x)]

rawDescription: How to filter on results of a comprehension?

description: Filter on results of a comprehension


code: # {"plot": plot_div, "table": table_div})

rawDescription: Embedding bokeh plot and datatable in flask


code:
import logging

logging.basicConfig(level=logging.DEBUG)

rawDescription: Log all requests from the python-requests module


code: lines = [[next(f).rstrip(), next(f).rstrip()] for line in f if line[0] == "-"]

rawDescription: PYTHON: Inserting all lines into array/list after specific symbol


rawDescription: PHP and Python unpack return different results from same source


code:
for b in results:
    for word in words:
       if word in str(b):
           break
    else:  # This else clause is executed only if no break occurs.
        new_list.append(b)

rawDescription: Loop - If one from several conditions is True append to new list


code:
dummy = ''.join(chr(ord(c) + 5) for c in dummy)
print(dummy)

rawDescription: Python String Manipulation Concerns

description: Python String Manipulation Concerns


code:
import gtk
dialog = gtk.MessageDialog(None, gtk.DIALOG_DESTROY_WITH_PARENT,
        gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, "Hello world")
response = dialog.run()
dialog.destroy()

rawDescription: Change gtk.BUTTONS_YES_NO in different language


rawDescription: Return a list of all variable names in a python nested dict/json document in dot notation


code:
cut.columns = cut.columns.map(lambda x: str(x) + '_a')
df2 = df.merge(cut, left_on=['userid'], right_index=True, how='left')

rawDescription: Operating on multiindex dataframe

description: Operating on multiindex dataframe


code:
import gmpy2
from gmpy2 import mpz, isqrt

def factors(n):
    n = mpz(n)

    result = set()
    result |= {mpz(1), n}

    def all_multiples(result, n, factor):
        z = n
        f = mpz(factor)
        while z % f == 0:
            result |= {f, z // f}
            f += factor
        return result

    result = all_multiples(result, n, 2)
    result = all_multiples(result, n, 3)

    for i in range(1, isqrt(n) + 1, 6):
        i1 = i + 1
        i2 = i + 5
        if not n % i1:
            result |= {mpz(i1), n // i1}
        if not n % i2:
            result |= {mpz(i2), n // i2}
    return result

print(factors(12345678901234567))

rawDescription: Most efficient way to find all factors with GMPY2 (or GMP)?


code:
def num_words(word_list, letter):
    total = []
    for word in word_listlist:
        if ( word.startswith(letter) ):
            total.append(word)
    return total

new_list = ["hello","bring","http","bin"]

print(num_words(new_list,"h"))
print(len(num_words(new_list,"h")))

rawDescription: Python Coding Help Needed, searching list by first letter of each word


code:
tmp_vec = np.power(np.arange(1, n+1) , -alpha)
tmp_vec

rawDescription: Python Custom Zipf Number Generator Performing Poorly


code:
def is_valid_vector(a):
    distance, angle = a
    threshold_distance = max(10.0, -0.008 * angle**2 + 0.4 * angle + 25.0)
    return (distance <= threshold_distance)

rawDescription: Counting Cars OpenCV + Python Issue

description: Counting Cars OpenCV + Python Issue


code:
d = {}
with open('yourfile.tsv') as tsvfile:
  next(tsvfile)
  for line in tsvfile:
    row = line.split()
    componentid, subcomponent, measurement = row[0], row[1], float(row[2])
    if not componentid in d:
      d[componentid] = [subcomponent, measurement]
    else:
      assert measurement == d[componentid][-1]
      d[componentid] = d[componentid][:-1] + [subcomponent, measurement]

rawDescription: Sorting and reorganizing a CSV file as a python dictionary


code: sorted('hello')

rawDescription: Is there a version of list.sort() that returns the sorted list?


rawDescription: Python Pandas qcut behavior with # of observations not divisible by # of bins


code:
import operator
from functools import reduce # in Python 3 reduce is part of functools

a = input("Enter number : ")
lst = [int(digit) for digit in a]

add = sum(lst[1::2])
mul = reduce(operator.mul, lst[::2],1)

print("add =",add,"mul =",mul,"result =",add+mul)

rawDescription: sum of the product of even positioned digits

description: Sum of the product of even positioned digits


code:
def is_factor(f, n):
    return n % f == 0

rawDescription: Finding a multiple function keeps returning true and is there a simpler way to write this function in python


code:
from msvcrt import getch
letter1 = getch()
letter2 = getch()
print("Pressed", letter1, "and", letter2)

rawDescription: Supersede users need to press enter after inputting a string in python 3.0


code:
from functools import wraps
from urllib import urlencode, quote, unquote
from flask import Flask, request, redirect, url_for, render_template

app = Flask(__name__)

def confirmation_required(desc_fn):
    def inner(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            if request.args.get('confirm') != '1':
                desc = desc_fn()
                return redirect(url_for('confirm',
                    desc=desc, action_url=quote(request.url)))
            return f(*args, **kwargs)
        return wrapper
    return inner

@app.route('/confirm')
def confirm():
    desc = request.args['desc']
    action_url = unquote(request.args['action_url'])

    return render_template('_confirm.html', desc=desc, action_url=action_url)

def you_sure():
    return "Are you sure?"

@app.route('/')
@confirmation_required(you_sure)
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(debug=True)

rawDescription: Flask Confirm Action

description: Flask Confirm Action


code:
def calc(startval, sequence=None):
     sequence = sequence or []

rawDescription: How to set default value for variable?

description: Set default value for variable


code:
# module dataparser.py

INFO01, INFO02, INFO03, INFO04 = map(slice, ((0,2),(2,10),(10,15),(15,20)))

def parse(infos, data):
    return [data[info] for info in infos]

rawDescription: Designing a reusable parser in python

description: Designing a reusable parser in python


code:
history = History()
...
history.add(stringval.get())

rawDescription: Undo and Redo in an Tkinter Entry widget?


code: df = df.sort(['date'])

rawDescription: Insert rows according to value of columns in pandas dataframes


code:
from django.db.models import Max

def count_numbers():
    max_value = Item.objects.all().aggregate(Max('number'))
    return max_value['number__max'] + 1

class Item(models.Model):
    number = models.IntegerField(default=count_numbers)
    description = models.CharField(max_length=200)

rawDescription: Django 1.8 - How to pre-populate a Model's field based on values in that Model's QuerySet


code:
with ZipFile(self.archive_name, "r") as archive:
    print(id(archive))

with ZipFile(self.archive_name, "r") as archive:
    print(id(archive))

rawDescription: Python variables scope in loops example

description: Python variables scope in loops example


code: [sum(x, []) for x in list1]

rawDescription: Python - Flatten lists of lists of two different types in one function


rawDescription: Python CSV: write rows according to dict mapping


code:
# this is equivalent to np.einsum('kij', A)
print(np.einsum('kij->ijk', A).shape)
# (3, 4, 2)

# this is the behavior you are expecting
print(np.einsum('ijk->kij', A).shape)
# (4, 2, 3)

rawDescription: How to understand the result of this np.einsum('kij',A)?

description: Understand the result of this np.einsum('kij',A)


code:
def binaryDisplay (num, bits):
    return ' '.join ('X' if num & (1 << b) else ' ' for b in range (bits - 1, -1, -1) )

for minute in range (60):
    print (minute, binaryDisplay (minute, 6) )

rawDescription: How to push this six-line code in to list comprehension in Python

description: Push this six-line code in to list comprehension in Python


code:
for car in carList:
    print("Your car is a {}, {}, {}.").format(car['level']['status'], car['color'], car['type'])

rawDescription: Generate String From List

description: Generate String From List


code:
import random
l = [1, 2, 3, 4, 5]
random.shuffle(l)
l

rawDescription: Randomizing a list of tuples without random.shuffle() python


rawDescription: Redraw plot in same window with scipy / voronoi_plot_2d


code:
config_options = {}

def load_configuration(filename):
    with open(filename) as f:
        for line in f:
            # get key and value
            config_options[key] = value

rawDescription: Good practice with conditional imports

description: Good practice with conditional imports


code:
print("Which category would you like to view? Savory, Dessert, Cake, Soup or Drink?")
print(os.listdir(raw_input().lower()))

rawDescription: Making Python display .txt files from directories


code: mysql < dump.sql

rawDescription: Python Sh Library for MySQL dump import using output redirection (<) pipes


rawDescription: Python Trie, Unable to list from trie

description: Python Trie, Unable to list from trie


code:
def over(rgba1, rgba2):
    if rgba1.shape != rgba2.shape:
        raise ValueError("rgba1 and rgba2 shall have same size")
    alpha1 = np.expand_dims(rgba1[:, :, 3] / 255., axis=3)
    alpha2 = np.expand_dims(rgba2[:, :, 3] / 255., axis=3)
    alpha = 1. - (1.-alpha1) * (1.-alpha2)
    C1 = rgba1[:, :, 0:3]
    C2 = rgba2[:, :, 0:3]
    C = (alpha1 * C1 + (1-alpha1) * alpha2 * C2) / alpha
    rgba =  np.empty_like(rgba1, dtype = np.uint8)
    rgba[:, :, 0:3] = C
    rgba[:, :, 3] = 255 * alpha[:, :, 0]
    return rgba

rawDescription: Making figure transparent with colored background


code: locals()

rawDescription: How to get the list of all initialized objects and function definitions alive in python?

description: Get the list of all initialized objects and function definitions alive in python


code:
box.WhiteBox().use("green") #created once
print("REPAINTED:", box.WhiteBox().name, box.WhiteBox().color) #two more times...

rawDescription: Why doesn't imported class attribute change in Python?


code:
df["value"] = 1
pivoted = pd.pivot_table(df, values="value", columns="color", index=["gender","age"], aggfunc="count", fill_value=0)
pivoted

rawDescription: Weighting values based on variables

description: Weighting values based on variables


rawDescription: Split a string with python re

description: Split a string with python re


rawDescription: A* Search - least number of hops?

description: A* Search - least number of hops


code:
import urllib2
r = urllib2.urlopen('http://httpbin.org/redirect-to?url=http://example.com/%3Ffoo=bar')
r.geturl()

rawDescription: I can not get the whole url when the server redirect me by using urllib2.urlopen(url).geturl()


code:
sorter = np.argsort(b)
sorter[np.searchsorted(b, a, sorter=sorter)]

rawDescription: Getting the indices of several elements in a NumPy array at once


code:
f = Foo()
f.f1=1
f.f2=2

buf = (c_char * 100)()
memmove(buf, byref(f), sizeof(f))

rawDescription: How to copy bytes from a ctypes Structure to a buffer created from create_string_buffer

description: Copy bytes from a ctypes Structure to a buffer created from create_string_buffer


code:
def parse (s):
    #Just throwing away all parentheses and commata
    s = s.replace ('(', ' ').replace (')', ' ').replace (',', ' ')
    #Return reversed polish notation
    return s.split () [::-1]

def evaluate (ops):
    stack = []
    while ops:
        op = ops [0]
        ops = ops [1:]
        try:
            stack.append (int (op) )
            continue
        except: pass
        try:
            stack.append (float (op) )
            continue
        except: pass
        if op == 'add':
            arg1, arg2 = stack.pop (), stack.pop ()
            stack.append (arg1 + arg2)
            continue
        if op == 'multiply':
            arg1, arg2 = stack.pop (), stack.pop ()
            stack.append (arg1 * arg2)
            continue
        if op == 'abs':
            arg1 = stack.pop ()
            stack.append (abs (arg1) )
            continue
        raise Exception ('Unkown instruction "{}".'.format (op) )
    return stack [0]

ops = parse ('abs(multiply(4,add(-4,2)))')
print (evaluate (ops) )
ops = parse ('multiply add 1 2 add add 3 4 5')
print (evaluate (ops) )

rawDescription: recursively computing expression python

description: Recursively computing expression python


code:
c = np.empty(len(a)+len(b), dtype=a.dtype)
c[::2] = a
c[1::2] = b

rawDescription: Fastest way to mix arrays in numpy?

description: Fastest way to mix arrays in numpy


code: pivoted_list = [pivot(df) for df in mylist]

rawDescription: Looping through a list in Python and creating new objects based on items


code:
xiter = iter(x)
list(roundrobin(xiter, xiter, y))

rawDescription: Merge 2 lists at every x position

description: Merge 2 lists at every x position


code: os.rename("a.txt", "b.txt")

rawDescription: Stuck at os.rename()

description: Stuck at os.rename()


code: bar(key1=value1, key2=value2)

rawDescription: numpy, scipy, pass large number of variables between functions, modules, easily

description: Numpy, scipy, pass large number of variables between functions, modules, easily


code:
import re
from itertools import *

A = ["blah blah", "blah blah", "blah", "list"]

#
# from itertools doc page
#
def flatten(listOfLists):
  "Flatten one level of nesting"
  return list(chain.from_iterable(listOfLists))

def pairwise(t):
  it = iter(t)
  return izip(it,it)

#
# Transform data
#
list_of_lists = [re.split("[ ,]", item) for item in A]
# [['blah', 'blah'], ['blah', 'blah'], ['blah'], ['list']]
a_words = flatten(list_of_lists)
a_pairs = pairwise(a_words)

with open("output.csv", "wb") as f:
    writer = csv.writer(f)
    writer.writerows(a_pairs)

rawDescription: Writing a results to a .txt file using csv


code:
import sys
import wx

class TestFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(TestFrame, self).__init__(*args, **kwargs)

        actualrules = ['Hello World', 'Goodbye Universe',
                       "Let's go to the mall"]

        panel = wx.Panel(self)
        self.list = wx.ListCtrl(panel, size=(1000, 1000), style=wx.LC_REPORT)
        self.list.InsertColumn(0, 'Rules')
        for i in actualrules:
            self.list.InsertStringItem(sys.maxint, i)

        pSizer = wx.BoxSizer(wx.VERTICAL)
        pSizer.Add(self.list, 0, wx.ALL, 5)
        panel.SetSizer(pSizer)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(vSizer)

if __name__ == '__main__':
    wxapp = wx.App(False)
    testFrame = TestFrame(None)
    testFrame.Show()
    wxapp.MainLoop()

rawDescription: ListCtrl set string item

description: ListCtrl set string item


code:
result = None
# Go trough one array
for i in x:

    # The element repeats in the other list...
    if i in y:

        # Store the result and break the loop
        result = i
        break

rawDescription: First common element from two lists

description: First common element from two lists


code:
import csv

def load_data(filename):
    with open(filename, "r") as f:
        reader = csv.reader(f, delimiter=";")
        return list(reader)

data = load_data("data.txt")
for row in data:
    print(row[2])

rawDescription: reading data from file and printing specific colums per line

description: Reading data from file and printing specific colums per line


code:
import random
with open('/etc/passwd') as f:
    print (random.choice(list(f)))

rawDescription: Randomly generate a line from a .txt file


code: [line for (time,line) in sorted(zip(listofTimes, listofLines))]

rawDescription: Sort list by given order of indices -Python


code:
word ='somelongword'
guesses = ['a', 'd', 'm', 'e', 's']
[c if c in guesses else '*' for c in word]

rawDescription: Python 3, for a in list and a in word:

description: Python 3, for a in list and a in word:


code:
x,y = np.meshgrid(range(data.shape[0]), range(data.shape[1]))
x *= 50./data.shape[0]
y *= 50./data.shape[1]

rawDescription: Editing Python plot surface ticks and their frequency


code:
any(c.isdigit() for c in 'Martijn Pieters')
any(c.isdigit() for c in 'The answer is 42')

rawDescription: Making sure no integers in a string?

description: Making sure no integers in a string


code:
if __name__=='__main__':
    user = raw_input('GitHub username: ')
    with open('global_var.py','w') as f:
        f.write("user = '%s'" % user)
password = 'blah'
gh = '<login(user, password)>'

rawDescription: Use input variables across Python scripts


code: K = np.array([i for i in xrange(n) if i!=r and i!=s])

rawDescription: How to optimize math operations on matrix in python

description: Optimize math operations on matrix in python


code:
def stringify(func):
    @wraps(func)
    def wrapper(*args):
        return str(func(*args))

    return wrapper

rawDescription: How to unit-test decorated functions?

description: Unit-test decorated functions


code:
import datetime as DT
import pytz
tzone = pytz.timezone('Europe/Budapest')
date1 = tzone.localize(DT.datetime(2014, 3, 29, 0, 0), is_dst=None)
date2 = tzone.localize(DT.datetime(2014, 3, 30, 23, 59), is_dst=None)
print(((date2-date1) + DT.timedelta(minutes=1)).total_seconds()/3600.)
# 47.0

rawDescription: Difference of two timedelta objects, with timezones


code:
import json

r = json.dumps(data)

rawDescription: How can I iterate through a python 2D list and delete/replace characters from a specific column?

description: Iterate through a python 2D list and delete/replace characters from a specific column


code:
import timeit
day = 45
signals = ['aaaa', 'bbbb', 'cccccc', 'dddddddddddd']
timeit.timeit("[('prefix' + str(day) + '_' + s) for s in signals]", 'from __main__ import day, signals')

rawDescription: Python, optimizing a list comprehension for string concatenation


code:
from argparse import ArgumentParser

norse = ArgumentParser()

norse.add_argument('-n', '--norse', required=False, help='Run the Norse IPViking scan.', action='store_true')
norse.add_argument('--threshold', required=False, type=int, choices=range(0,101), metavar="[0-100]", help='Threshold (0-100) denoting at what threat level to provide additional data on an IP address. Default is 49.', default=49)

norse.print_help()

rawDescription: Python argparse value range help message appearance


code:
import prism
def getPrecipData(years=None):
    grid_pnts = prism.getGridPointsFromTxt()
    flrd_pnts = np.array(pd.read_csv(r'D:\truncated\PrismGridPointsFlrd.csv').grid_code)
    mask = prism.makeGridMask(grid_pnts, grid_codes=flrd_pnts)
    for year in years:
        bil = r'/vsizip/G:\truncated\PRISM_ppt_stable_4kmM2_{0}_all_bil.zip\PRISM_ppt_stable_4kmM2_{0}_bil.bil'.format(year)
        b = prism.BilFile(bil)
        nodatavalue, data = b.get_array(mask=mask)
        data *= mm_to_in
        b.write_to_csv(data, 'PrismPrecip_{}.txt'.format(year))
    return

# Get datasets
years = range(1950, 2011, 5)
getPrecipData(years=years)

rawDescription: Reading zipped ESRI BIL files with Python


code:
from pylab import *
import pywt
import scipy.io.wavfile as wavfile

# Find the highest power of two less than or equal to the input.
def lepow2(x):
    return 2 ** floor(log2(x))

# Make a scalogram given an MRA tree.
def scalogram(data):
    bottom = 0

    vmin = min(map(lambda x: min(abs(x)), data))
    vmax = max(map(lambda x: max(abs(x)), data))

    gca().set_autoscale_on(False)

    for row in range(0, len(data)):
        scale = 2.0 ** (row - len(data))

        imshow(
            array([abs(data[row])]),
            interpolation = 'nearest',
            vmin = vmin,
            vmax = vmax,
            extent = [0, 1, bottom, bottom + scale])

        bottom += scale

# Load the signal, take the first channel, limit length to a power of 2 for simplicity.
rate, signal = wavfile.read('kitten.wav')
signal = signal[0:lepow2(len(signal)),0]
tree = pywt.wavedec(signal, 'db5')

# Plotting.
gray()
scalogram(tree)
show()

rawDescription: Basic plotting of wavelet analysis output in matplotlib


rawDescription: python function incrementing variable in list for no obvious reason

description: Python function incrementing variable in list for no obvious reason


rawDescription: separate a string with multiple seperators AND extract single characters only

description: Separate a string with multiple seperators AND extract single characters only


code: z = ','.join(['"{0}"'.format(item) for item in y])

rawDescription: Python - How do I add the backslash characters when formatting a JSON string?


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

rawDescription: Python: nested list comprehension

description: Python: nested list comprehension


code:
class obstacle:
  def __init__(self, offset=1.0):
    self.pos = int((dims[0]-1) * offset)
    self.s = ''
    for x in range(0,dims[1]-1):
      self.s += '='

  def scroll(self):
    self.pos -= 1

screen = curses.initscr()

curses.start_color()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
screen.nodelay(1)
dims = screen.getmaxyx()

q=-1
obs1 = obstacle()
obs2 = obstacle(.5)

while q < 0:
  q = screen.getch()
  if obs1.pos < 0:
    obs1 = obstacle()
  if obs2.pos < 0:
    obs2 = obstacle()
  screen.clear()
  screen.addstr(obs1.pos, 0, obs1.s, curses.color_pair(1))
  screen.addstr(obs2.pos, 0, obs2.s, curses.color_pair(1))
  obs1.scroll()
  obs2.scroll()
  screen.refresh()
  time.sleep(.04)
screen.getch()
curses.endwin()

rawDescription: Dealing with multiple strings in curses

description: Dealing with multiple strings in curses


code:
import dis
dis.dis(compile('[]', '', 'eval'))
dis.dis(compile('{}', '', 'eval'))

rawDescription: Why is [] faster than list()?

description: Why is [] faster than list()


rawDescription: How to add 24 to the %H part of the enter variable, then store it as the same variable?

description: Add 24 to the %H part of the enter variable, then store it as the same variable


code:
class CustomUserLoginView(FormView):
    form_class = AuthenticationForm
    template_name = 'registration/login.html'
    success_url = '/connections/'

    def get(self, request, *args, **kwargs):
        form = self.form_class(initial=self.initial)
        return render(request, self.template_name, {'form':form})

    def post(self, request, *args, **kwargs):
        form = self.form_class(data=request.POST)
        if form.is_valid():
            user = authenticate(
                username=form.cleaned_data['username'],
                password=form.cleaned_data['password'],
                )
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return HttpResponseRedirect(self.success_url)
                else:
                    return HttpResponse('User is not active') # TEMP
            else:
                return HttpResponse('User does not exist') # TEMP
        else:
            return HttpResponse('Form is not valid') # TEMP

rawDescription: Django login form not working for custom user model


code:
b = np.lib.stride_tricks.as_strided(x, [1000,100,100], strides=x.strides + (0, 0))

print(b.flags.owndata)
# False
print(b.base)
# <numpy.lib.stride_tricks.DummyArray object at 0x7fb40c02b0f0>

rawDescription: Size of numpy strided array/broadcast array in memory?


code: self.currvalue = next(self.it)    # Exit on StopIteration

rawDescription: Unexpected Behavior of itertools.groupby


code:
f2=open("CLI.md")
for i in f2:
    print(i)

for i in f2:
    print(i)

rawDescription: Compare two files lines with python

description: Compare two files lines with python


code:
query = select([
    table.c.some_column,
    expression.label(
        'grouped column',
        group_concat(
            table.c.some_oter_column,
            ' separator ',
        ),
    ),
]).group_by(table.c.some_column)

rawDescription: How do I write a group_concat function in sqlalchemy?

description: Write a group_concat function in sqlalchemy


code:
s = u''.join(unichr(c) for c in xrange(sys.maxunicode+1))
import re
re.findall(r'\s', s)

rawDescription: In Python, how to list all characters matched by POSIX extended regex `[:space:]`?


code: dict(line.split(',') for line in WDictionary)

rawDescription: Converting file to key/value dictionary

description: Converting file to key/value dictionary


code:
array([[False,  True],
       [ True,  True]], dtype=bool)

rawDescription: "Less/greater than" comparison of N-dimensional and (N-k)-dimensional numpy arrays


code:
def f(n):
    return '#' * n

rawDescription: Integer to string length in python?

description: Integer to string length in python


code:
import xml.dom.minidom as minidom
dom = minidom.parse("log.xml")
dom

rawDescription: Python xml Trying to show a if statement on a tag


rawDescription: Converting from hexadecimal to binary values


rawDescription: Plot pandas dataframe containing NaNs

description: Plot pandas dataframe containing NaNs


code:
from lxml import etree

tree = etree.parse('xmlfile')
root = tree.getroot()

for elem in root.iter('*'):
    if elem.text is not None:
        elem.text = elem.text.strip()

print(etree.tostring(root))

rawDescription: Python how to strip white-spaces from xml text nodes


code:
index = np.array([0, 2, 3])
X_indexed = X[:, index]
print(X_indexed[:5])

rawDescription: Remove a specific feature in scikit learn


code: get_class = lambda x: globals()[x]

rawDescription: Initialise class object by name

description: Initialise class object by name


code:
from models import User
# Assuming you have user model class in models
user = User.query().filter(User.SESSIONID==1234).get()

rawDescription: Google App Engine Datastore get value of property


rawDescription: How to append an empty list in a for loop in Python and i got "None" when try to append the reversed string.Why its showing None?

description: Append an empty list in a for loop in Python and i got "None" when try to append the reversed string.Why its showing None


code:
import csv

answer = {}
with open('path/to/file') as infile:
    infile = csv.reader(infile, delimiter='\t')
    keys = next(infile)
    values = next(infile)
    answer.update(dict(zip(keys, values)))

rawDescription: Reading key value pair from a tab delimited file in python


code:
teststr = ['1 FirstString', '2x Sec String', '3rd String', 'x forString', '5X fifth']
[i for i in teststr if re.search(r'\d+[xX]', i) ]

rawDescription: Find a specific pattern (regular expression) in a list of strings (Python)


code:
post = SellPost.objects.get(slug=post_name_slug)
context_dict['post'] = post

post_image = SellPostImage.objects.filter(post=post)
context_dict['post_image'] = post_image

rawDescription: Django: Querying to ImageField all images are being rendered


rawDescription: Correctly indexing a multidimensional Numpy array with another array of indices


code:
child_process = subprocess.Popen( ... , stdout=subprocess.PIPE)
child_process.stdout.read("This is the data that comes back")

rawDescription: Printing to stdout in subprocess

description: Printing to stdout in subprocess


code:
def checkbutton_value1():
    if(var1.get()):
       var2.set(0)

def checkbutton_value2():
    if(var2.get()):
       var1.set(0)

var1=IntVar()
checkbox_1 = Checkbutton(root, text='1   ', variable=var1, command=checkbutton_value1)
checkbox_1.pack()
var2=IntVar()
checkbox_2 = Checkbutton(root, text='2   ', variable=var2, command=checkbutton_value2)
checkbox_2.pack()

rawDescription: Python, Tkinter, Checkbutton: Is there a way to check on/off value


code:
ordered_deduped_quals = sorted(set(qualsin))
for quals in ordered_deduped_quals:
    ...

rawDescription: Iterate over a list that contain duplicate elements


code:
xVar = [1, 200, 300, 10]
yVar = [150, 100, 50, 200]
xyVar = zip(xVar, yVar)
xyVar
xyFiltered = [(x, y) for x, y in xyVar if y <= 100]
xyFiltered
xVar, yVar = zip(*xyFiltered)

rawDescription: python: How to remove values from 2 lists based on what's in 1 list

description: Python: How to remove values from 2 lists based on what's in 1 list


code:
connection = op.get_bind()
r = connection.execute(srv_feed_return_type.select().where(...))
for row in r:
    pk_id = r['id']
    """or something more sophisticated"""

rawDescription: alembic and getting the last inserted value

description: Alembic and getting the last inserted value


code:
import numpy as np
def drop_zeros(a_list):
    return [i for i in a_list if i>0]

def log_binning(counter_dict,bin_count=35):

    max_x = log10(max(counter_dict.keys()))
    max_y = log10(max(counter_dict.values()))
    max_base = max([max_x,max_y])

    min_x = log10(min(drop_zeros(counter_dict.keys())))

    bins = np.logspace(min_x,max_base,num=bin_count)

    # Based off of: http://stackoverflow.com/questions/6163334/binning-data-in-python-with-scipy-numpy
    bin_means_y = (np.histogram(counter_dict.keys(),bins,weights=counter_dict.values())[0] / np.histogram(counter_dict.keys(),bins)[0])
    bin_means_x = (np.histogram(counter_dict.keys(),bins,weights=counter_dict.keys())[0] / np.histogram(counter_dict.keys(),bins)[0])

    return bin_means_x,bin_means_y

rawDescription: Plotting log-binned network degree distributions


code: re.match('^(ab|a|)\w*\s*(?:(as|a)\w*|)\w*?\s*(qwe|qw|)',s) #s is your string

rawDescription: Empty string taking precedence in python regex subpatterns with multiple groups


code:
def narrative_list(elements):
    last_clause = " and ".join(elements[-2:])
    return ", ".join(elements[:-2] + [last_clause])

rawDescription: Elegant way for creating a narrative string of a list


code:
from twisted.internet.protocol import Protocol
class SomeProtocol(Protocol):
    def dataReceived(self, data):
        print('Do something with data: {}'.format(data))

    def connectionMade(self):
        self.transport.write("Hello there")

rawDescription: How do Twisted python Factory and Protocol interfaces work?


code:
from timeit import timeit
timeit(lambda:tree.query_ball_point(ecef_cities[:1000], r=euclidean_distance(30)), number=1)

rawDescription: Python - how to speed up calculation of distances between cities


code:
f = open("file.txt", "w")
f.write("a\rb\nc\r\nd\n\re\n")
f.close()
open("file.txt").readlines()
open("file.txt", 'rU').readlines()

rawDescription: Count line no. using python

description: Count line no. using python


code:
def wait_for_bets(self, bettor):
    with self.condition:
        while self.bets[self.bettor] is None:
            self.condition.wait()
        bettor, bet = self.bettor, self.bets[self.bettor]
        self.bets[self.bettor] = None
    # handle the bet

rawDescription: Multiplayer Gaming via python

description: Multiplayer Gaming via python


code: soup.findAll('a', onclick=lambda x: x and 'importantline' in x)

rawDescription: Passing partial attributes into Beautiful Soup findall Function


code:
@ajax_login_required
def ajax_update_module(request, module_slug, action):
    # Etc ...
    return HttpResponse(json, mimetype='application/json')

rawDescription: Django authentication and Ajax - URLs that require login


code: ohell

rawDescription: Python - How to move characters in string by X amount


code:
mutation,percentage = re.sub(r'^.*?\t(.*?)\t(.*?):(.*?)%.*', r'\1\2 \3', s).split()
mutation
percentage

rawDescription: python regex for grabbing specific parts of a line

description: Python regex for grabbing specific parts of a line


rawDescription: How to quickly grab specific indices from a numpy array?

description: Quickly grab specific indices from a numpy array


code: np.digitize(days, bins=b, right=True)

rawDescription: Assigning histogram bin to each data point


code:
def some_obj(object):

    def __init__(self, foo, bar):
        self.foo = foo
        self.bar = bar

rawDescription: Is it possible to make a variable an object of a class by changing its type?


rawDescription: Fastest Way to Sum Individual Columns

description: Fastest Way to Sum Individual Columns


code: d

rawDescription: Choose Variable With Highest Value (Python)


rawDescription: Raspberry Pi is stuck when no char is received on the serial port


code: fig, ax = plt.subplots()

rawDescription: Matplotlib Window Size, Title, etc

description: Matplotlib Window Size, Title, etc


code:
from xml.sax.saxutils import escape as escape
escape(yourText)

rawDescription: Alternatives to Qt.escape in PySide?

description: Alternatives to Qt.escape in PySide


code:
print (Query([
    Parent,
    func.count(Child.id),
    func.count(case(
        [((Child.naughty == True), Child.id)], else_=literal_column("NULL"))).label("naughty")])

    .join(Parent.children).group_by(Parent)
    )

rawDescription: SQLAlchemy: several counts in one query

description: SQLAlchemy: several counts in one query


code:
df
df.sort_values(by='a', ascending=False)
df.sort_values(by='b', ascending=False)
df.sort_values(by=['a','b'], ascending=False)

rawDescription: pandas: Perform multiple commands with a for loop

description: Pandas: Perform multiple commands with a for loop


code: reduce(lambda x, y: x * (y + 1), [1, 2, 3], 1) # initializer is 1

rawDescription: Setting a variable equal to a returned value from a for loop in Python


code:
from sklearn.cluster import KMeans

km = KMeans(n_clusters=num_clusters)
km.fit(some_data)

from sklearn.externals import joblib
# dump to pickle
joblib.dump(km, 'model.pkl')

# and reload from pickle
km = joblib.load('model.pkl')

rawDescription: How to cache in IPython Notebook?

description: Cache in IPython Notebook


code:
soup = BeautifulSoup(t, 'html5lib')
for link in soup.find_all('a'):
    print(link['href'])

rawDescription: beautiful soup parser can't find links

description: Beautiful soup parser can't find links


rawDescription: How to Open, Read, and Write files in Python 2.7 -- Converting code from fortran 90 to Python


code:
partial =  [x for x in a1 if all(x not in y for y in a2)]
partial
a2 += [x + '_111' for x in partial]
a2

rawDescription: partial match between two arrays in python

description: Partial match between two arrays in python


code:
timestamp = 1366831506000
time.strftime("%a %d %b %Y %H:%M:%S GMT", time.gmtime(timestamp / 1000.0))

rawDescription: python linux timestamp to date time and reverse

description: Python linux timestamp to date time and reverse


code:
data
data="c01,a11,b12,a21, b22,c23, c31,b32, a33"
data=[x.strip() for x in data.split(",")]
data=sorted(data,key=operator.itemgetter(0))
data=sorted(data,key=operator.itemgetter(1))
data=sorted(data,key=operator.itemgetter(2))
data

rawDescription: grouping with topology

description: Grouping with topology


code:
result = result.stack(level=0).unstack(level=1)
print(result)

# cols   col1     col2
#           l  n     l  n
# index
# 0        12  0    32  1
# 1        16  1    47  1
# 2        92  0    22  0
# 3        77  0    14  1

# [4 rows x 4 columns]

rawDescription: reshaping data frame in pandas

description: Reshaping data frame in pandas


code:
pat= re.compile( "hi" )
a = { pat : 'eggs' } # native dict, no features.
x=PerlLikeDict( a )
x['b']= 'c'
x
x['b']
x['ji']
x['hi']

rawDescription: How can I read Perl data structures from Python?

description: Read Perl data structures from Python


code:
import numpy as np
import pandas as pd

N, M = 744, 734
index = pd.date_range('2015-01-01', periods=N, freq='H')
idx = np.random.choice(np.arange(N), M, replace=False)
idx.sort()
index = index[idx]

# This creates a toy DataFrame with 734 non-null rows:
df = pd.DataFrame({'values': np.random.randint(10, size=(M,))}, index=index)

# This expands the DataFrame to 744 rows (10 null rows):
df = df.asfreq('H')

# This makes `df` have 744 non-null rows:
df = df.interpolate(method='time')

rawDescription: Python linear interpolation of values in dataframe


code:
stack=[]
shift=0
def Track(func):
    def wrapper(*args, **kwargs):
        global shift
        stack.append([])
        el=stack[-1]
        el.append('%s -- call(name=%s,args=%s,kwargs=%s)' % ('    '*shift,func.__name__,args, kwargs))
        shift+=1
        res = func(*args, **kwargs)
        shift-=1
        el[0]+='return=%s)' % res
        return res
    return wrapper

rawDescription: Can Decorator + Reflection selectively create object trees representing individual function-call chain?


code:
import matplotlib.pyplot as plt
from pandas.tools.plotting import table
import pandas as pd

#My dataframe
df = pd.DataFrame({
    'Weeks' : [201605, 201606, 201607, 201608],
    'Computer1' : [50, 77, 96, 100],
    'Computer2' : [50, 79, 100, 80],
    'Laptop1'   : [75, 77, 96, 95],
    'Laptop2'   : [86, 77, 96, 40],
    'Phone'     : [99, 99, 44, 85],
    'Phone2'    : [93, 77, 96, 25],
    'Phone3'    : [94, 91, 96, 33]
})
df2 = df.set_index('Weeks') #Makes the column 'Weeks' the index.

colors = df2.applymap(lambda x: 'green' if x>= 80 else 'red') \
        .reset_index().drop(['Weeks'], axis=1)

#print(colors)

plt.figure(figsize=(10,5))

ax = plt.subplot(2, 1, 1, frame_on=True) # no visible frame

#ax.xaxis.set_visible(False)    # hide the x axis
#ax.yaxis.set_visible(False)    # hide the y axis

# hide all axises
ax.axis('off')

# http://matplotlib.org/api/pyplot_api.html?highlight=table#matplotlib.pyplot.table
tbl = table(ax, df2,
            loc='center',
            cellLoc='center',
            cellColours=colors.as_matrix(),
            colColours=['black']*len(colors.columns),
            rowColours=['black']*len(colors),
            #fontsize=14
      )

# set color for index (X, -1) and headers (0, X)
for key, cell in tbl.get_celld().items():
    if key[1] == -1 or key[0] == 0:
        cell._text.set_color('white')
    # remove grid lines
    cell.set_linewidth(0)

# refresh table
plt.show()

# save it as an png.
plt.savefig('mytable.png')

rawDescription: Conditionally change background color of specific cells


code:
def check_for_new_file(directory=INCOMING, files={}):
    for file in os.listdir(directory):
        if file in files:
            break
        size = os.stat(file)[stat.ST_SIZE]
        files[file] = (datetime.time.now(), size)
    now = datetime.time.now()
    for file, last_time, last_size in files.items():
        current_size = os.stat(file)[stat.ST_SIZE]
        if current_size != last_size:
            files[file] = (now, current_size)
            continue
        if now - last_time <= TIME_WITH_NO_WRITES:
            return file
    raise NoneReady()

rawDescription: Updating CSV files automatically

description: Updating CSV files automatically


code: keyFileName = getInput()

rawDescription: Write a new function that returns value inputted in another function


code:
def my_filter_func(x):
    return x % 2 == 0

class FilterClass(object):

    filter_func = staticmethod(my_filter_func)

    def filter_data(self, data):
        return filter(self.filter_func, data)

rawDescription: How to store function in class atribute?

description: Store function in class atribute


code: eval(compile('lambda: a', '<stdin>', 'eval'), ...)

rawDescription: Why are closures broken within exec?

description: Why are closures broken within exec


code:
import collections
import itertools
counts = collections.Counter(itertools.chain.from_iterable(words))

threshold = len(words) - allowed_number_of_times_missing
results = {word for word in counts if counts[word] >= threshold}

rawDescription: How do I create something similiar to a "set intersection" that allows for an item to be missing from individual sets n / 1-or-more times?

description: Create something similiar to a "set intersection" that allows for an item to be missing from individual sets n / 1-or-more times


code:
import numpy as np
arr = np.genfromtxt('data', dtype = None)
dct = {tuple(row[:3]):row[5:7] for row in arr}

rawDescription: Using numpy loadtext

description: Using numpy loadtext


code:
from abc import ABC, ABCMeta, abstractmethod

class HandlerRegisterer(ABCMeta):
    # subclass ABCMeta rather than type
    def __init__(self, name, bases, attrs, register=True):
        super().__init__(name, bases, attrs)
        if register:
            HandlerManager.register(self)

    def __new__(metaclass, name, bases, attrs, register=True):
        return super().__new__(metaclass, name, bases, attrs)

class AbstractSubHandler(MyHandler, ABC, register=False):
    # not strictly necessary to subclass ABC, but nice to know it won't
    # screw things up
    @abstractmethod
    def some_method(self):
        pass

try:
    AbstractSubHandler()
except TypeError:
    print("Can not instantiate abstract class")

print(HandlerManager.handlers)
assert AbstractSubHandler not in HandlerManager.handlers

rawDescription: Is there a way to call a function after a class was loaded in python?


code:
import random
help(random)
random.random()

rawDescription: is there any drand48() equivalent in python or a wrapper to it?

description: Is there any drand48() equivalent in python or a wrapper to it


code:
events = Event.objects.filter(system_id=system_id).filter(timestamp__range=[from_date, now])
event_vals = events.values_list('timestamp', 'system.serial_number') # more here
return event_vals

rawDescription: django accessing to models takes so much time

description: Django accessing to models takes so much time


code:
import collections
collections.Counter(x) == collections.Counter(y)

rawDescription: Determine if 2 lists have the same elements, regardless of order?


code:
from selenium import webdriver

options = webdriver.ChromeOptions()
options.add_extension(r'C:\Downloads\Adblock-Plus_v1.11.crx')

capabilities = options.to_capabilities()
capabilities.update({'browser_version': '50.0', 'os': 'Windows', 'os_version': '10', 'resolution': '1366x768'})

driver = webdriver.Remote("http://127.0.0.1:4444/wd/hub", desired_capabilities=capabilities)
driver.get('http://stackoverflow.com/')

rawDescription: Loading .crx into remote chrome Selenium python


rawDescription: Truncated versus floored division in Python


code: data = { "say_hello" : { "name": "World", "times": 4 } }

rawDescription: What is the client for spyne with json document protocol?


rawDescription: How to get enumerated results from a query in Django?

description: Get enumerated results from a query in Django


code:
class MainWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="MyWindowTitle")
        userPassword = get_user_pw(self, "Please enter your password", "Password")

rawDescription: Python/Gtk3 : How to add a Gtk.Entry to a Gtk.MessageDialog?


code: [Tree('root', [Tree('who_player', [Tree('which', ['which']), Tree('team', ['sri', 'lankan']), Tree('player', ['player'])]), Tree('has', ['has']), Tree('the', ['the']), Tree('most', ['most']), Tree('runs', ['runs'])])]

rawDescription: NLTK CFG Grammar with multiple words

description: NLTK CFG Grammar with multiple words


rawDescription: numpy array that is (n,1) and (n,)

description: Numpy array that is (n,1) and (n,)


code:
class FieldsMeta(type):

    def __new__(mcs, name, bases, dict):
        """This controls the creation of each new class."""
        dict.update(fields={'name': ''})
        return type.__new__(mcs, name, bases, dict)

class Parent(object):
    __metaclass__ = FieldsMeta

class Child(Parent):
    pass

rawDescription: How to inherit a class attribute that is a dict and make it unique?

description: Inherit a class attribute that is a dict and make it unique


code: str(input("enter pitch {}".format(i + 1)))

rawDescription: Check variables against a dictionary, and print the dictionary contents remaining


code:
from NumLines import NumLines
from WordsPerLine import WordsPerLine
import sys

intermediate = None

def firstJob(input_file):
    global intermediate
    mr_job = NumLines(args=[input_file])
    with mr_job.make_runner() as runner:
        runner.run()
        intermediate = runner.get_output_dir()

def secondJob(input_file):
    mr_job = WordsPerLine(args=[intermediate,input_file])
    with mr_job.make_runner() as runner:
        runner.run()

if __name__ == '__main__':
    firstJob(sys.argv[1])
    secondJob(sys.argv[1])

rawDescription: How to specifically determine input for each map step in MRJob?

description: Specifically determine input for each map step in MRJob


code:
pattern = r'''(?x)               # set flag to allow verbose regexps
              ([A-Z]\.)+         # abbreviations, e.g. U.S.A.
              | \$?\d+(\.\d+)?%? # numbers, incl. currency and percentages
              | \w+([-']\w+)*    # words w/ optional internal hyphens/apostrophe
              | [+/\-@&*]        # special characters with meanings
            '''

rawDescription: NLTK regexp tokenizer not playing nice with decimal point in regex


code:
import csv

with open(outputfilename, 'wb') as outfh:
    writer = csv.writer(outfh)
    writer.writerow(['Data with , commas and nested " quotes', 'works just fine'])

rawDescription: How do I write a comma as a normal text in a CSV file?(the double quote method doesn't work)

description: Write a comma as a normal text in a CSV file?(the double quote method doesn't work)


code:
image_src = cv2.imread("input.png")
gray = cv2.cvtColor(image_src, cv2.COLOR_BGR2GRAY)
ret, gray = cv2.threshold(gray, 10, 255,0)
image, contours, hierarchy = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
largest_area = sorted(contours, key=cv2.contourArea)[-1]
mask = np.zeros(image_src.shape, np.uint8)
cv2.drawContours(mask, [largest_area], 0, (255,255,255,255), -1)
image_remainder = cv2.bitwise_and(image_src, 255 - mask)

cv2.imwrite("remainder.png", image_remainder)

rawDescription: How to extract white region in an image

description: Extract white region in an image


code:
from collections import defaultdict
mergedData = defaultdict(list)
with open('file1.txt', 'r') as file1, open('file2.txt', 'r') as file2, open('mergedData.txt', 'w') as outfile:
    for line in file1:
        mergedData[line.split()[0].lstrip('0')].append(line)
    for line in file2:
        mergedData[line.split()[0]].append(" ".join(line.split()[:4]))
    ...

rawDescription: Python: Search a text file and then output specific data


code:
d = patt.search(str).groupdict()
g2 = d["optional1"] if d["optional1"] is not None else d["optional2"]

rawDescription: Multiple same name group definitions or if system


code:
import numpy as np
arr = np.array(img)
arr[arr < 10] = 0
img.putdata(arr)

rawDescription: How can i iterate over image pixels in a faster manner in python?

description: Iterate over image pixels in a faster manner in python


code:
customer = Customers.query.filter_by(cat_id=page).first()
data = dict((c.info.get('name', c.name), getattr(customer, c.name))
            for c in customer.__table__.c)
# Or using dict comprehension syntax (Python 2.7+).
data = {c.info.get('name', c.name): getattr(customer, c.name)
        for c in customer.__table__.c}

rawDescription: Flask-sqlalchemy column alias for browser output


code:
x1_arr = np.array(x1)
x2_arr = np.array(x2)
x2_arr - x1_arr

rawDescription: Difference of two tuples containing Long numbers


code:
def get_current_month():
    now = datetime.now()
    return now.strftime("%m")

def get_current_year():
    now = datetime.now()
    return now.strftime("%y")

def tests_by_month(request, id):
    person = Person.objects.get(id=id)
    month = request.GET.get('month', get_current_month()) # if no month take the current as default
    year = request.GET.get('month', get_current_year()) # if no year take the current as default
    test_list = person.tests.filter(date__month=int(month), date__year=int(year))
    context = {'test_list': test_list}
    return render_to_response(request, 'template.html', context)

rawDescription: django sort the query elements in a weekly monthly daily fashion

description: Django sort the query elements in a weekly monthly daily fashion


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

class Class_2(object):
    def __init__(self, class_1_instance):
        self.class_1_instance = class_1_instance

rawDescription: Class management in python

description: Class management in python


code:
while l:
    first = l.pop(0)
    for second in l:
        print(first, second)

rawDescription: Why is my for loop skipping an element in my list?


code:
''' Print text in a scrolling region of the terminal above a fixed line for input

    Written by PM 2Ring 2016.05.29

    Python 2 version
'''

from __future__ import print_function
from time import sleep
import readline

# Some ANSI/VT100 Terminal Control Escape Sequences
CSI = '\x1b['
CLEAR = CSI + '2J'
CLEAR_LINE = CSI + '2K'
SAVE_CURSOR = CSI + 's'
UNSAVE_CURSOR = CSI + 'u'

def emit(*args):
    print(*args, sep='', end='')

def set_scroll(n):
    return CSI + '0;%dr' % n

# Height of scrolling region
height = 40

GOTO_INPUT = CSI + '%d;0H' % (height + 1)

emit(CLEAR, set_scroll(height))

try:
    while True:
        #Get input
        emit(SAVE_CURSOR, GOTO_INPUT, CLEAR_LINE)
        try:
            n = int(raw_input('Number: '))
        except ValueError:
            continue
        finally:
            emit(UNSAVE_CURSOR)

        #Display some output
        for i in range(n):
            print(i)
            sleep(0.1)

except KeyboardInterrupt:
    #Disable scrolling, but leave cursor below the input row
    emit(set_scroll(0), GOTO_INPUT, '\n')

rawDescription: Print text before input() prompt in python


code:
def handle_request(req):
    for i, h in enumerate(handlers):
        if h[1].handles(req):
            h[0] += 1
            for j in reversed(range(i+1)):
                if handlers[j][0] <= h[0]:
                    break
            if j < i:
                handlers[j+1:i+1] = handlers[j:i]
                handlers[j] = h
            break
    else:
        return None
    return h[1]

rawDescription: Efficient way to maintain a sorted list of access counts in Python


code:
app = QApplication(sys.argv)
app.exec_()

rawDescription: Custom signals and multithreading

description: Custom signals and multithreading


code: caps = re.findall(b'data-name="(.*?)"', website.content)

rawDescription: Find all text in HTML web scrape after a specific string (python3)


code:
def keyboard(key):
  values = {
    110: discoMode(),
    27: exit()
  }

rawDescription: Python: Using a dictionary as switch not working


code: p = regex.compile("\p{Lu}", regex.U)

rawDescription: Python regex for unicode capitalized words


rawDescription: Python counting lines in files using exact locations


code:
import numpy as np
a = np.arange(27).reshape(3,3,3)  # 3dimensional array
a[..., 1]  # this returns a slice through the array in the third dimension
a[0, ...]  # This returns a slice through the first dimension

rawDescription: What does Python mean by printing "[...]" for an object reference?


rawDescription: how to change [1,2,3,4] to '1234' using python

description: Change [1,2,3,4] to '1234' using python


code:
def func(a=0,b=10):
 return a+b
args = {'a':15,'b':15}
func(**args)
args={'a':15}
func(**args)
args={'b':6}
func(**args)
args = {}
func(**args)

rawDescription: Pythonic solution for conditional arguments passing


code:
li2=list('012345')    # longer list; completely different construction
li2

rawDescription: Is a variable the name, the value, or the memory location?


code:
if __name__ == "__main__":
    from time import clock

    blocksize = 1000 # Chosen at runtime.
    mixinsize = 100 # Chosen at runtime.
    count = 100000 # Chosen at runtime.
    xs = np.random.randint(0, blocksize + 1, count)
    mixins = np.empty((count, mixinsize))
    acc = np.zeros(blocksize + mixinsize)

    t0 = clock()
    foobar(mixinsize, count, xs, mixins, acc)
    t1 = clock()
    print("elapsed time: %g ms" % (1000*(t1-t0),))

    t2 = clock()
    foobar_jit(mixinsize, count, xs, mixins, acc)
    t3 = clock()
    print("elapsed time with numba jit: %g ms" % (1000*(t3-t2),))

    print("speedup factor: %g" % ((t1-t0)/(t3-t2),))

rawDescription: How to shove this loop into numpy?

description: Shove this loop into numpy


code:
from matplotlib.figure import Figure
fig=Figure()
ax=fig.add_subplot(111)

y = [6.0, 5.0, 4.0, 2.5, 3.0, 2.0, 1.0, 2.0, 2.5, 2.5]

ax.plot(x_normalised, y, 'bo')

rawDescription: How to assign equal scaling on the x-axis in Matplotlib?

description: Assign equal scaling on the x-axis in Matplotlib


code:
specType(0.0)
specType(8.1)
specType(14.5)
specType(22.3)
specType(34.7)

rawDescription: Python one-liner to convert float to string


code:
from os import getpid

from circuits import ipc, Component, Event, Timer

class go(Event):
    """go"""

class produced(Event):
    """produced"""

class Consumer(Component):

    def produced(self, *args):
        print("Consumed {} from {}".format(repr(args), getpid()))

class Producer(Component):

    def init(self):
        self.consumers = [
            Consumer().start(process=True, link=self),
        ]

    def ready(self, *args):
        Timer(1, go(), persist=True).register(self)

    def go(self, *args):
        for process, bridge in self.consumers:
            self.fire(ipc(produced(), bridge.channel))

class App(Component):

    def init(self):
        Producer().register(self)

App().run()

rawDescription: How to use threads with python circuits

description: Use threads with python circuits


code:
colors = [p.get_facecolor().tolist()[0] for p in collec_poly.collections]
gdf['RGBA'] = colors

rawDescription: Contour plot data (lat,lon,value) within boundaries and export GeoJSON


code: [i.replace("_", " ") for i in current_list.split()]

rawDescription: How to add space between strings in Python List without the underscore

description: Add space between strings in Python List without the underscore


code: my_long_string.count(';') + 1

rawDescription: Python, A string split into many pieces. How many pieces are in there?


code:
import pandas as pd
from collections import defaultdict

path_to_file = "/test.csv"
df = pd.read_csv(path_to_file)

groups = df.groupby(['Fruit', 'Origin'])
max_for_fruit = defaultdict(int) #first pass through the groups, store the maximum for each fruit to handle ties

for g in groups:
    fruit, count = g[0][0], len(g[1])
    max_for_fruit[ fruit ] = max( max_for_fruit[fruit], count )

for g in groups:
    fruit, state, count = g[0][0], g[0][1], len(g[1])
    if count == max_for_fruit[ fruit ]:
        print( "{} {} {}".format(fruit, state, count ) )

rawDescription: Python: How best to parse csv and count values for only a subset


code: wchild = parent

rawDescription: Python: Parent and Child responding to child-specific changes in boolean values. Why is this happening?


code:
spams = example.get_spams()
# Access by index returns a proxy.  It does not perform a
# spam-to-python conversion.
spams[0].perform()
# The iterator's value will be returned, requiring a
# spam-to-python conversion.
for spam in spams:
    pass

rawDescription: How to return a std::vector<const T*> to python?

description: Return a std::vector<const T*> to python


rawDescription: Finding and splitting at a character from a string inside a list of lists


code:
def insert_none_for(iterable):
    it = iter(iterable)
    for x in it:
        yield x
        yield next(it)
        yield None

rawDescription: How to insert None into a list?

description: Insert None into a list


code: found = ("0%s@" % element) in item

rawDescription: searching string, exclude certain substrings

description: Searching string, exclude certain substrings


rawDescription: Finding out an average in python

description: Finding out an average in python


code:
if __name__ == '__main__':
    laser = LJV7IF(LASER_DEVICE_ID, LaserData)
    for record in laser.get_storage_data(11):
        print(record.data)

rawDescription: How to interpret a integer as a float

description: Interpret a integer as a float


code: print( fruits['apple'] )

rawDescription: How do I generate a list for each list item

description: Generate a list for each list item


code:
class PluginLoader():
    def __init__(self, path):
        self.path = path

    def __iter__(self):
        for (dirpath, dirs, files) in os.walk(self.path):
            if not dirpath in sys.path:
                sys.path.insert(0, dirpath)
        for file in files:
                (name, ext) = os.path.splitext(file)
                if ext == os.extsep + "py":
                    __import__(name, None, None, [''])
        for plugin in Plugin.__subclasses__():
            yield plugin

rawDescription: Plugin manager in python

description: Plugin manager in python


code:
def month(n):
    a = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split()
    return a[n - 1]

rawDescription: Python: Creating a month() function to take 1 - 12 as input and return corresponding month


code:
words = ['there', 'was', 'a', 'ffff', 'time', 'ssss']
curses = set(['ffff', 'ssss'])
filter(lambda x: x not in curses, words)

rawDescription: Remove list entries that match any entry in another list


rawDescription: Unexpected value of floor() function in python


rawDescription: Plotting for conference posters (increase textsize, linewidth, ... at once)


code: ax.format_coord = lambda x, y: ''

rawDescription: Disable displaying coordinate in matplotlib plot


code:
print("What is " ,num1, operation, num2)
ans = int(input())

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


code:
# For sklearn
bw = 0.15

# For scipy
bw = 0.15/x.std(ddof=1)

rawDescription: Relation between 2D KDE bandwidth in sklearn vs bandwidth in scipy


code:
title = section.split('\n')[0].split('"')[1]
#get first line, get everything between first quotation marks

rawDescription: parse text using bash/python

description: Parse text using bash/python


code: cred = lambda: fill(255, 0, 0)

rawDescription: Declare python variable that stores a function, but not run it


code:
import sqlite3
con = sqlite3.connect(":memory:")
con.isolation_level = None
cur = con.cursor()
cur.execute('''CREATE TABLE ipaddr (ip text, val text )''')

from subprocess import Popen, PIPE

for ip in [ '173.194.43.34', '17.172.224.47', '198.252.206.16' ]:
    pipe = Popen('host {}'.format( ip ), shell=True, stdout=PIPE )
    for line in pipe.stdout:
        cur.execute ( "INSERT INTO ipaddr VALUES ('{}','{}')".format( ip, line.strip( ) ))

rawDescription: Python - writing out the result of Popen into a mysql table


code:
import fileinput
import linecache

file_name = input("Enter the file name: ") #sample.txt

# selecting cells
select1 = int(input("Enter row number: ")) #0
select2 = int(input("Enter column number: ")) #1

# New value
newVal = int(input("Enter row number: ")) #100

# Get nth line from the input file and strip any newline chars
textToSearch = linecache.getline(file_name, select1+1).strip('\n')

# Transform to the line read to a list
tmpList = [int(_) for _ in textToSearch.split(' ')]

# Replace the list with the new value and form the str line back again
tmpList[select2] =  newVal
textToReplace = ' '.join(str(_) for _ in tmpList)

# Modify the sample.txt file inplace
with fileinput.FileInput(file_name, inplace=True, backup='.bak') as file:
    for line in file:
        print(line.replace(textToSearch, textToReplace), end='')

file.close()
linecache.clearcache()

rawDescription: selecting coordinates of an array by user input in python

description: Selecting coordinates of an array by user input in python


code:
import urlparse
parsed_url = urlparse.urlparse('http://councilroom.com/player?player=G%C3%B6rling')
parsed_url
query_dict = dict(urlparse.parse_qsl(parsed_url.query))
query_dict

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

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


code:
class CurrentUserMiddleware(object):
    def process_request(self, request):
        set_current_user(getattr(request, 'user', None))

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


code:
store.get_storer('df').attrs['non_index_axes']
store.get_storer('df').attrs.non_index_axes = [(1, ['new','b','c'])]

rawDescription: Is it possible to directly rename pandas dataframe's columns stored in hdf5 file?


code:
words1 = {'view', 'lower', 'sample','good'}
words2 = {'sold', 'good', 'part', 'view'}
words1 | words2
words1.union(words2)

rawDescription: Extend or Append for String in method

description: Extend or Append for String in method


code: mantis_shrimp = 696969; animal_name(mantis_shrimp)

rawDescription: how to convert variable into string in python

description: Convert variable into string in python


rawDescription: Converting escaped unicode characters to unicode notation


code:
def feetConverter(number):
    tempInchValue, feetValue = math.modf(int(number) * 0.03281)
    inchConverter(tempInchValue) #calls the inch converter function, and ignores the return value
    return feetValue # returns feetValue only.

rawDescription: Centimeter to Feet converter in Python

description: Centimeter to Feet converter in Python


rawDescription: pandas date column subtraction

description: Pandas date column subtraction


code:
def nSplit(lst, delim, count=2):
    output = [[]]
    delimCount = 0
    for item in lst:
        if item == delim:
            delimCount += 1
        elif delimCount >= count:
            output.append([item])
            delimCount = 0
        else:
            output[-1].append(item)
            delimCount = 0
    return output

rawDescription: Most Pythonic Way to Split an Array by Repeating Elements


rawDescription: How does argparse store all of the added argument? Is there a way to access their names, and help strings?


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

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


code:
Mydata_files = [] # List of data files to include
# Get the images from the [script root]/ui/images/ folder
for files in os.listdir(sys.path[0] + '/ui/images/'):
    f1 = sys.path[0] + '/ui/images/' + files
    if os.path.isfile(f1): # This will skip directories
        f2 = 'ui/images', [f1]
        Mydata_files.append(f2)

# Get the config file from the [script root]/Configs folder
Mydata_files.append(('Configs', [sys.path[0] + '/Configs/defaults.cfg']))

rawDescription: py2exe access 'other_resources'

description: Py2exe access 'other_resources'


code:
from itertools import islice

def first_n(iterable, n):
    return islice(iterable, 0, n)

rawDescription: Truncating a python generator

description: Truncating a python generator


rawDescription: initialize python dictionary containing a strings and lists

description: Initialize python dictionary containing a strings and lists


code:
import numpy as np

a = np.arange(4)[:,None]
b = np.arange(2,5)[None,:]
c = np.array(True)
it = np.nditer([a, b, c], flags=['reduce_ok'],
    op_flags = [['readonly'], ['readonly'],['readwrite']])
for x, y, z in it:
    z[...] = x<y
    if not z:
        print('>',x,y)
        break
    else:
        print(x,y)
print(z)

rawDescription: shortcut evalution of numpy's array comparison

description: Shortcut evalution of numpy's array comparison


code: map(lambda x: x[1] if not x[0] else x[0], zip(list1, list2))

rawDescription: Python: merge lists or data frames and overwrite missing values


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

# Make the original line...
x = np.linspace(0, 10, 100)
y = 2 * x
thickness = 0.5 * np.abs(np.sin(x) * np.cos(x))

plt.fill_between(x, y - thickness, y + thickness, color='blue')
plt.show()

rawDescription: How do you make the linewidth of a single line change as a function of x in matplotlib?

description: Make the linewidth of a single line change as a function of x in matplotlib


rawDescription: Creating distinct objects in a function

description: Creating distinct objects in a function


code:
bucket_list = [u'Mossfun ', u'11) ', u'Rtg:103)', u'58.0', u'Atmospherical ', u'8) \n      ', u'Rtg:99)', u'56.5', u'Chloe In Paris ', u'7) \n      ', u'Rtg:97)', u'55.5', u'Bound For Earth ', u'5) ', u'Rtg:92)', u'55.5', u'Fine Bubbles ', u'4) ', u'Rtg:91)', u'55.5', u'Brook Road ', u'9) ', u'Rtg:90)', u'55.5', u'Shamalia ', u'10) ', u'Rtg:89)', u'55.5', u'Tawteen ', u'6) ', u'Rtg:88)', u'55.5', u'Ygritte ', u'2) ', u'Rtg:77)', u'55.5', u'Tahni Dancer ', u'1) ', u'Rtg:76)', u'55.5', u'All Salsa ', u'3) ', u'Rtg:72)', u'55.5']
translation_table = dict.fromkeys(map(ord, ')(@\\n#$'), None)
[x.translate(translation_table) for x in bucket_list ]

rawDescription: Python -unicode- translate table doesn't remove chars


code: (a['href'] if a + '1' else '`NULL`')

rawDescription: TypeError: unsupported operand type(s) for +: 'Tag' and 'str'


code:
countries = []

for e in x:
 countries.append(e[0])

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

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


code:
def set_last_update(details, *args, **kwargs):  # pylint: disable=unused-argument
    """
    Pipeline function to add extra information about when the social auth
    profile has been updated.
    Args:
        details (dict): dictionary of informations about the user
    Returns:
        dict: updated details dictionary
    """
    details['updated_at'] = datetime.utcnow().timestamp()
    return details

rawDescription: Decide when to refresh OAUTH2 token with Python Social Auth


code:
right = range(10, 20)
left = range(10)
sum(x*y for x, y in zip(left, right))
735

rawDescription: Multiply two equal-sized list and sum the result (dot product)


rawDescription: Reshaping a numpy array in python

description: Reshaping a numpy array in python


code:
def conprintNum(counter, lock, num):
    print(num)
    with lock:
        counter.increment()

if __name__ == "__main__":

    example = [1,2,3,4,5]

    m = SyncManager()  # SyncManager, rather than BaseManager
    m.register('Counter', Counter)
    m.start()
    lock = m.Lock()  # SyncManager comes with a shared Lock implementation.
    counter = m.Counter(len(example))
    p = Pool(processes=4)
    func = partial(conprintNum, counter, lock)

    p.map(func, example)

rawDescription: How to write a progress counter/time for concurrency?

description: Write a progress counter/time for concurrency


rawDescription: python - Common lists among lists in a list

description: Python - Common lists among lists in a list


code: arr1[:, np.newaxis, :].shape

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


code:
#ef_UI.py

import wx

''' very simple interface with only a button and a TextCtrl '''
class MyFrame(wx.Frame):
    def __init__(self, efToolObj):
        wx.Frame.__init__(self, None, title="Menu Test")
        self.panel = wx.Panel(self)
        self.efToolObj = efToolObj # save the MyTools object to be used later
        self.MyButton = wx.Button(self.panel, -1, "Button_test", (0, 0))
        self.MyButton.Bind(wx.EVT_BUTTON, self.onClickEvent) # Bind the click event to an event handling method
        self.MyTextCtrl = wx.TextCtrl(self.panel, -1, value="just a test", pos=(100, 0))

    def onClickEvent(self,event): #this is called when a button is clicked
        res = self.efToolObj.OnClickPrinting(self.MyTextCtrl.GetValue()) #Use the mytools object to call its method to apply logic,also get result values
        self.MyTextCtrl.SetValue(res) #use the result values in your UI

rawDescription: binding packages with wxpython

description: Binding packages with wxpython


code: dictionaryNumbers['z'] = dictionaryNumbers.get('z', 0) + 3

rawDescription: What is an efficient way to increase and set the value of a dictionary key using python?


code:
def get_data():
    (...)
    return dict(zip(names, values))

rawDescription: creating variables from external data in python script

description: Creating variables from external data in python script


code: writer = csv.DictWriter(ouf, ['Name'] + months[5:9])

rawDescription: writing csv output python

description: Writing csv output python


code:
gen = []
fam = []
OTUs = []

save_fam = []
save_gen = []
save_OTU = []

FindIT_name = askopenfilename()
ss(FindIT_name)
data = open(FindIT_name, "r").readlines()

#some data manipulation here

def ss(FindIT_name):
    ss_file = asksaveasfile(mode="w", defaultextension=".csv")
    ss_file.write("OTU, Family, Genus")
    ### If you need to do something, you now have FindIT_name available.

rawDescription: Pass file data between functions

description: Pass file data between functions


rawDescription: How to construct a TimeGrouper with a DateOffset?

description: Construct a TimeGrouper with a DateOffset


code:
item = self.listWidget.currentItem()
value = item.text()

rawDescription: return value from listWidget in pyqt4

description: Return value from listWidget in pyqt4


code:
import numpy as np
result = df.groupby(['col1','col2']).agg({'col3':'sum','col4':'sum','col5':np.average})

rawDescription: groupby in pandas with different functions for different columns

description: Groupby in pandas with different functions for different columns


code:
def get_resolved(urllist, generated_urls, layout):
    result = {}
    for url in urllist:
        result[url] = [x for x in generated_urls[url] if x in set(resolved[layout])]
    return result

rawDescription: Make algorithm faster that runs over multiple lists


code: for list3 in ([c[1]/d[1] for c in a for d in b] for a in list1 for b in list2):list3function()

rawDescription: Processing list comprehension output element-by-element


rawDescription: Pivoted pandas dataframe to dictionary

description: Pivoted pandas dataframe to dictionary


code:
from itertools import izip_longest
import csv
with open('a1.txt', 'rb') as csv1,open('b1.txt', 'rb') as csv2,open('c.txt', 'w') as out:
     spam1 = csv.reader(csv1, delimiter=',')
     spam2 = csv.reader(csv2, delimiter=',')
     last_column=list(izip_longest(*spam2))[-1]
     for i,j in  izip_longest(spam1,last_column):
        out.write(','.join([t.strip(';') for t in i])+','+j+'\n')

rawDescription: how to merge some columns data from different txt files in Python?

description: Merge some columns data from different txt files in Python


rawDescription: Euler's pentagonal number function in python


code: driver.execute_script("arguments[0].innerHTML = arguments[1];", element, "testing");

rawDescription: How to add text to a page using python selenium?

description: Add text to a page using python selenium


code: from django.contrib.gis.db import models

rawDescription: Calculating near gps coordinates in django


code:
d = DelayComponents(prop_delay=1, trans_delay=2, proc_delay=3, queue_delay=4)
d.delay

rawDescription: Building variable dependencies

description: Building variable dependencies


code:
config = wx.Config("StackOverflowTest")
config.Write("testdata", "yes it works!")

rawDescription: wxPython - Save Items in ListCtrl

description: WxPython - Save Items in ListCtrl


code: pid_number = subprocess.Popen(['pgrep', sys.argv[1]], stdout=subprocess.PIPE, stderr=subprocess.STDOUT).communicate()[0]

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


code:
cutoffs = np.cumsum(probs)
cutoffs

rawDescription: draw random element in numpy

description: Draw random element in numpy


rawDescription: Accepting top rows in pandas dataframe based on grouping


rawDescription: Item not included in for loop made in Scrapy


code:
def profiler(f, n, *args, **kwargs):
    start = time.time()
    for i in xrange(n):
        f(*args, **kwargs)
    return time.time() - start

rawDescription: Probability distribution in Python

description: Probability distribution in Python


code:
from socket import socket
from ssl import SSLContext
from ssl import PROTOCOL_SSLv23
from ssl import DER_cert_to_PEM_cert

WEAK_CTX = SSLContext(PROTOCOL_SSLv23)
WEAK_CTX.set_ciphers('ALL:!aNULL:!eNULL')

NORMAL_CTX = SSLContext(PROTOCOL_SSLv23)
NORMAL_CTX.set_ciphers(
    'ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:'
    'DH+HIGH:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+HIGH:RSA+3DES:!aNULL:'
    '!eNULL:!MD5'
)

def getCertificate(addr):
    sock = socket()
    sock.connect(addr)
    isWeakCipher = False
    try:
        sslobj = NORMAL_CTX._wrap_socket(sock._sock, server_side=False)
        sslobj.do_handshake()
    except Exception as ex:
        if hasattr(ex, 'reason') and ex.reason == 'SSLV3_ALERT_HANDSHAKE_FAILURE':
            sock.close()
            sock = socket()
            sock.connect(addr)
            sslobj = WEAK_CTX._wrap_socket(sock._sock, server_side=False)
            sslobj.do_handshake()
            isWeakCipher = True
        else:
            raise
    cipher = sslobj.cipher()

    cert = sslobj.peer_certificate(True)
    sock.close()
    return isWeakCipher, cipher, cert

rawDescription: python ssl ssl.SSLError: [SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:590)

description: Python ssl ssl.SSLError: [SSL: UNSUPPORTED_PROTOCOL] unsupported protocol (_ssl.c:590)


code:
import numpy as np;
import seaborn as sns;
import pandas as pd
from scipy import stats
import scipy.cluster.hierarchy as hac
import matplotlib.pyplot as plt

num_samples = 61
group_size = 10

x = np.linspace(0, 15, num_samples)
a = np.sin(x) + np.linspace(0, 5, num_samples)

x = np.linspace(0, 50, num_samples)
b = np.sin(x) + np.linspace(0, -8, num_samples)
c = np.sin(x + 2)

d = np.linspace(0, 14, num_samples)
e = np.random.randn(group_size, 1) + np.linspace(0, -3, num_samples)

x = np.linspace(0, 4, num_samples)
f = np.sin(x)

timeSeries = pd.DataFrame()
ax = None
for arr in [a,b,c,d,e,f]:
    arr = arr + np.random.rand(group_size, num_samples) + (np.random.randn(group_size, 1)*3)
    df = pd.DataFrame(arr)
    timeSeries = timeSeries.append(df)

    # We use seaborn to plot what we have
    #ax = sns.tsplot(ax=ax, data=df.values, ci=[68, 95])
    ax = sns.tsplot(ax=ax, data=df.values, err_style="unit_traces")

rawDescription: Hierarchical clustering of time series in Python scipy/numpy/pandas?


code:
m.T.shape, m.shape
m.dot(C)
C.dot(m)

rawDescription: What is the difference between using matrix multiplication with np.matrix arrays, and dot()/tensor() with np.arrays?


code: [int(''.join(str(x) for x in x)) for x in it.product(*x)]

rawDescription: Print out all combinations of elements in an array of arrays


rawDescription: Concatanating array by While on Python

description: Concatanating array by While on Python


code:
import os

command = "cp %s %s" % (file_location1, file_location2)
os.system(command)

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


code:
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import scipy as sy
import pylab as plb

data = plb.loadtxt('data.dat')
x = data[:,0]
y= data[:,1]

def func(x, a, b, c):
  return a*x**b + c

p0 = sy.array([1,1,1])
coeffs, matcov = curve_fit(func, x, y, p0)

yaj = func(x, coeffs[0], coeffs[1], coeffs[2])
print(coeffs)
print(matcov)

plt.plot(x,y,'x',x,yaj,'r-')
plt.show()

rawDescription: Use of curve_fit to fit data

description: Use of curve_fit to fit data


code:
q = (
    session
    .query(a)
    .filter(a.insert_date == task_date)
    .filter(
        exists(
        # or: ~exists(
            select([b.pk])
            .where(a.pk == b.pk)
            .where(b.insert_date < task_date)
        )
    )
)

rawDescription: SQLAlchemy return model from outerjoin

description: SQLAlchemy return model from outerjoin


code:
def func():
    print(__file__)

rawDescription: Python: How do i get the name of the current archive?


code: request = requests.get(...)

rawDescription: Python Json Parseing for output

description: Python Json Parseing for output


rawDescription: How can I do strtok()-type parsing in Python?

description: Do strtok()-type parsing in Python


code:
import collections

followers_that_friends_follow = collections.defaultdict(list)
for f in friends:
    ff = api.GetFriendsIDs(f)['ids']
    users = followers_not_friends.intersection(ff)
    for user in users:
        followers_that_friends_follow[user].append(f)

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


code:
c = a.size // k # 3
d = (np.arange(k) + c * k) < a.size # [True, False, False]

rawDescription: Way of easily finding the average of every nth element over a window of size k in a pandas.Series? (not the rolling mean)


code:
df['erosion'] = ndimage.binary_erosion(df['dilation'], structure=[1]*(consecutive+1),
                                       border_value=1)
#      RF   mask dilation erosion
# 0   0.3   True     True    True
# 1   1.1   True     True    True
# 2   0.2   True     True    True
# 3   0.0  False     True    True
# 4   0.0  False     True    True
# 5   0.0  False     True    True
# 6   0.0  False     True    True
# 7   0.0  False     True    True
# 8   1.1   True     True    True
# 9   0.6   True     True    True
# 10  0.0  False     True   False  <--,
# 11  0.0  False     True   False     |
# 12  0.0  False    False   False     | The Falses have been expanded
# 13  0.0  False     True   False     | (The Trues eroded)
# 14  0.0  False     True   False     |
# 15  0.0  False     True   False  <--'
# 16  0.8   True     True    True
# 17  1.1   True     True    True
# 18  2.3   True     True    True
# 19  1.4   True     True    True
# 20  0.4   True     True    True
# 21  0.2   True     True    True
# 22  0.0  False     True   False
# 23  0.0  False     True   False
# 24  0.0  False    False   False
# 25  0.0  False    False   False
# 26  0.0  False    False   False
# 27  0.0  False    False   False

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

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


code: 3 in {x for s in l for x in s}

rawDescription: Python: searching within a list

description: Python: searching within a list


rawDescription: Rendering Text Calendar

description: Rendering Text Calendar


code:
lst = [{'restaurant':'one_rest', 'lunch_menu':'one_lunch', 'dinner_menu':'one_din'},
       {'restaurant':'two_rest', 'lunch_menu':'two_lunch', 'dinner_menu':'two_din'}]

rawDescription: Creating a copy of a list of dictionaries with a subset of keys


code:
from datetime import datetime
datetime.fromtimestamp(0)
datetime.fromtimestamp(1268816500)

rawDescription: In Python, if I have a unix timestamp, how do I insert that into a MySQL datetime field?


code:
x = array([-273.0, -176.4, -79.8, 16.9, 113.5, 210.1, 306.8, 403.4, 500.0])
y = array([2.25927498e-53, 2.56028619e-03, 8.64512988e-01, 6.27456769e+00, 1.73894734e+01,
        3.29052124e+01, 5.14612316e+01, 7.20531200e+01, 9.40718450e+01])

x_nodes = array([-273.0, -263.5, -234.8, -187.1, -120.3, -34.4, 70.6, 194.6, 337.8, 500.0])
y_nodes = array([2.25927498e-53, 3.83520726e-46, 8.46685318e-11, 6.10568083e-04, 1.82380809e-01,
                2.66344008e+00, 1.18164677e+01, 3.01811501e+01, 5.78812583e+01, 9.40718450e+01])

## Now get scipy's spline fit.
k = 3
tck = splrep(x_nodes, y_nodes, k=k, s=0)
knots = tck[0]
coeffs = tck[1]
print('knot points=', knots)
print('coefficients=', coeffs)

## Now try scipy's object-oriented version. The result is exactly the same as "tck": the knots are the
## same and the coeffs are the same, they are just queried in a different way.
uspline = UnivariateSpline(x_nodes, y_nodes, s=0)
uspline_knots = uspline.get_knots()
uspline_coeffs = uspline.get_coeffs()

## Here are scipy's native spline evaluation methods. Again, "ytck" and "y_uspline" are exactly equal.
ytck = splev(x, tck)
y_uspline = uspline(x)
y_knots = uspline(knots)

## Now let's try our manually-calculated evaluation function.
y_eval = bspleval(x, knots, coeffs, k, debug=False)

plt.plot(x, ytck, label='tck')
plt.plot(x, y_uspline, label='uspline')
plt.plot(x, y_eval, label='manual')

## Next plot the knots and nodes.
plt.plot(x_nodes, y_nodes, 'ko', markersize=7, label='input nodes')            ## nodes
plt.plot(knots, y_knots, 'mo', markersize=5, label='tck knots')    ## knots
plt.xlim((-300.0,530.0))
plt.legend(loc='best', prop={'size':14})

plt.figure()
plt.title('difference')
plt.plot(x, ytck-y_uspline, label='tck-uspl')
plt.plot(x, ytck-y_eval, label='tck-manual')
plt.legend(loc='best', prop={'size':14})

plt.show()

rawDescription: Getting spline equation from UnivariateSpline object


code:
import os
word="moo"
os.system('cowsay ' + word)

rawDescription: How to use python variable in os.system?

description: Use python variable in os.system


code:
class SongData(object):
    def __init__(self, title="", artist="", directory=""):
        self.title, self.artist, self.directory = title, artist, directory
    def __str__(self):
        desc_str = "Title: %s \nArtist: %s \nDirectory: %s \n" %(self.title,
                                            self.artist, self.directory)
        return desc_str # <------ RETURN HERE

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

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


code:
def remove_numbers(li):
    numberless = []
    for i in li:
        try:
            float(i)
        except:
            numberless.append(i)
    return numberless

res = remove_numbers(["word",'2.134123'])
print(res)
# outputs ['word']

rawDescription: Why doesn't this Python code remove a digit from a list?


rawDescription: How to Split a delimiter string and store it as array in python?


code: [x for x in items if x[2] == 0]

rawDescription: How to check if all elements of a list matches a condition?

description: Check if all elements of a list matches a condition


rawDescription: Python: Generating YYMM string between two dates


code: result

rawDescription: Pythonic way to add a list of vectors

description: Pythonic way to add a list of vectors


code:
#client.py
for line in open("my.csv"):
    s.send(line)

#server.py
def read_line(sock):
    return "".join(iter(lambda:sock.recv(1),"\n"))

rawDescription: Python socket to send rows of data

description: Python socket to send rows of data


code: matching_lines = [line for line in string_list if filter_func(line)]

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