code:
def SECdownload(year, month):
    import os
    from urllib.request import urlopen
    root = None
    feedFile = None
    feedData = None
    good_read = False
    itemIndex = 0
    edgarFilingsFeed = 'http://www.sec.gov/Archives/edgar/monthly/xbrlrss-' + str(year) + '-' + str(month).zfill(2) + '.xml'
    print(edgarFilingsFeed)
    #print( edgarFilingsFeed ) #from the slides
    if not os.path.exists( "sec/" + str(year) ):
        os.makedirs( "sec/" + str(year) )
    if not os.path.exists( "sec/" + str(year) + '/' + str(month).zfill(2) ):
        os.makedirs( "sec/" + str(year) + '/' + str(month).zfill(2) )
    target_dir = "sec/" + str(year) + '/' + str(month).zfill(2) + '/'
    try:
        feedFile = urlopen( edgarFilingsFeed ) # urlopen will not work (python 3) needs from urllib.request import urlopen
        try:
            feedData = feedFile.read()
            good_read = True
        finally:
            feedFile.close()
    except HTTPError as e:
        print( "HTTP Error:", e.code )
    return target_dir

rawDescription: Python GUI cannot see global variable

description: Python GUI cannot see global variable


code:
from itertools import tee
class Aggreegator:
    def __init__(self, all_obj):
        self.obj = all_obj
        self.used_user, self.mean = self.getTotalBytesAndMean()
        self.total_elements = len(self.all_obj)
        self.aligned = self.getEligned()

    def getTotalBytesAndMean(self):
        iter_1, iter_2 = tee((obj.usedBytes for obj in self.all_obj))
        return sum(iter_1), statistics.mean(iter_2)

    def getTotal(self):
        return sum(obj.total for obj in self.all_obj)

    def getEligned(self):
        return sum(obj.aligned for obj in self.all_obj)

    def getEncoding(self):
        return (obj.encoding for obj in self.all_obj)

rawDescription: Pythonic way to aggregate object properties in memory efficient way?


code: Engine(1) in [eng1, eng2]

rawDescription: are user defined classes mutable

description: Are user defined classes mutable


rawDescription: Most efficient way to loop through multiple csv files and calculate NYSE tick


code:
def interpolate_delta(df, inplace=False):
    if not inplace:
        df = df.copy()
    ind = df.index
    df.index = df.index.total_seconds()
    df.interpolate(method="index", inplace=True)
    df.index = ind
    return df

rawDescription: Cannot interpolate in pandas dataframe

description: Cannot interpolate in pandas dataframe


code:
import pandas as pd
rng = pd.Series([11969, 12132001, 1022013, 1022013])
test_str = str(rng)
flags = re.MULTILINE
p = r'\d{4}$'
result = re.findall(p, test_str, flags)
print(result)
# ['1969', '2001', '2013', '2013'] # not just unique values

rawDescription: Python - Retrieve and replace based on a regex


code: images = np.dstack((img1, img2, img3))

rawDescription: Temporal median image of multiple images


rawDescription: Zipping lists within a list in Python

description: Zipping lists within a list in Python


code: inds = np.argsort(rec['f0'])

rawDescription: sorting numpy structured and record arrays is very slow

description: Sorting numpy structured and record arrays is very slow


code:
def func(input):
    # do work
    return length, something_else         # one might be an integer another string, etc.

s = "hello goodbye"
length, something = func(s)

rawDescription: Python Newbie: Returning Multiple Int/String Results in Python


code:
img2 = PhotoImage("entryBorder", data="""
        R0lGODlhHQAdAOMNAAAAAAQGCAgLERkfLR0mODBFZTFFZTNIajtTezxTez1XgD5XgU
        Fch////////////ywAAAAAHQAdAAAEbHCQg5i9OGt0iFRaKGLKxBgCoK5s6woGc4Cp
        a9+AwFQM7ruYn1AVHP6KRhwyaVsyW87nKioFUKXXZ5a5TXaN32FYOD5eqsAzmlX2tZ
        XqNZGxYATkgAD9wCjUqgIFMgR1I4YZCx4TCYeGCR0DEQA7""")

oestyle = ttk.Style()
oestyle.element_create("blueborder", "image", "entryBorder",
                                   border=3, sticky="nsew")
oestyle.layout("OEntryStyle.TEntry",
               [('Entry.blueborder', {'children': [(
                   'Entry.padding', {'children': [(
                     'Entry.textarea', {'sticky': 'nswe'})],
                      'sticky': 'nswe'})], 'sticky': 'nswe'})])
oestyle.configure("OEntryStyle.TEntry",
                 background="black",
                  foreground="grey")
oentry_v = StringVar()
oentry = ttk.Entry(root_window, style="OEntryStyle.TEntry", textvariable=oentry_v)
oentry.pack(padx=10, pady=10)

rawDescription: ttk Entry background colour

description: Ttk Entry background colour


code:
from datetime import datetime
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')

rawDescription: in Python, How can I convert a string into a date object and get year, month and day separately?

description: In Python, How can I convert a string into a date object and get year, month and day separately


code:
from collections import defaultdict
from itertools import chain

merged = defaultdict(int)

for key, value in chain(list1, list2):
    merged[key] += value

rawDescription: What is the Pythonic way to merge 2 lists of tuples and add the nonunique values in the tuples?


rawDescription: How to load IPython shell with PySpark

description: Load IPython shell with PySpark


code:
find_element_by_xpath(".//a[contains(text(), 'Columbia')][10]") # one-based index, one element only
find_elements_by_xpath(".//a[contains(text(), 'Columbia')]")[9] #  find_elements_* gives you zero-base index list

rawDescription: Using Selenium to select an anchor with specific content


code:
import gtk, gobject

tree_store = gtk.TreeStore(gobject.TYPE_STRING,
                           gobject.TYPE_BOOLEAN,
                           gobject.TYPE_BOOLEAN )
root = tree_store.append(None, ("Computer 1", False, False))
tree_store.append(root, ("C drive", False, True))
tree_store.append(root, ("D drive", True, True))

view = gtk.TreeView(tree_store)
renderer1 = gtk.CellRendererText()
renderer2 = gtk.CellRendererToggle()

view.append_column(gtk.TreeViewColumn("Computer Name",
                                      renderer1, text=0))
view.append_column(gtk.TreeViewColumn("Select Drive",
                                      renderer2, active=1, visible=2))
w = gtk.Window()
w.add(view)
w.show_all()
gtk.main()

rawDescription: How to remove Parent checkbox in pygtk

description: Remove Parent checkbox in pygtk


code:
b = [[[h for h in x if h in y] for y in k] for x in j]
b

rawDescription: Create a list of items after every iteration in python


rawDescription: Decoding JSON and printing some specific part?


code:
l1 = [] # This makes an empty list
l2 = raw_input("> ") # Do this statement in a loop if you like
l1.append(l2) # This appends l2 to the list

rawDescription: how to compare a string with each element of a list in python

description: Compare a string with each element of a list in python


code:
d.clear()
d.update({k: v for k,v in d.iteritems() if v > 20})

rawDescription: Fastest way to remove a lot of keys from a dictionary


code:
p = Parallel(args)
result = []
for val in self.list_objects:
    result.append(p(val))

rawDescription: Parallel processing loop using multiprocessing Pool


code:
import numpy as np
from scipy.spatial.distance import pdist

def dfun(u, v):
    return np.sqrt(((u-v)**2).sum())

dm = pdist(X, dfun)

rawDescription: Use pdist() in python with a custom distance function defined by you


code:
with open('data.txt', 'rb') as f:
    rows = list(map(float, row) for row in csv.reader(f, delimiter=','))

rows = zip(*[col for col in zip(*rows) if any(col)])

rawDescription: Removing columns in python

description: Removing columns in python


code:
x
x - np.array(frame['seconds'], dtype='timedelta64[s]')

rawDescription: Apply time shift on Pandas DataFrame from another column


code:
class TestBase:
    def __init__(self):
        self.foo = 1

    def foo_printer(self):
        print(self.foo)

class TestChild(TestBase):
    def __init__(self):
        super().__init__()
        print(hasattr(super(), 'foo_printer'))

test = TestChild()

rawDescription: How to test if python class parent has method defined?

description: Test if python class parent has method defined


code:
import logging
# create console handler
ch = logging.StreamHandler()
# create formatter
formatter = logging.Formatter('\x1b[80D\x1b[1A\x1b[K%(message)s')
# add formatter to console handler
ch.setFormatter(formatter)
# add console handler to logger
logger.addHandler(ch)

rawDescription: display only one logging line

description: Display only one logging line


code:
for prop in self._properties.itervalues():
  name = prop._code_name
  values[name] = prop._get_for_dict(self)

rawDescription: Get properties of ndb model instance where Python name != datastore name


code:
a='51545'
obj = []
for i in range(len(a)):
    obj.append({a[i]: i})

rawDescription: Finding Index in string with recurring chars


code:
import myScript
myScript.main(arg1, arg2, arg3)

rawDescription: Efficient way to run many instances of python script with a few changes each time


code:
df['prices'] = [1.0, np.nan, 2.0]
df.prices.apply(lambda x: type(x) == float)

rawDescription: How to create a new derived column in a pandas dataframe new column using only notnull vaues

description: Create a new derived column in a pandas dataframe new column using only notnull vaues


code: out = np.mean(np.sum(pattern[:,None,:] == matrix[None,:,:],2),1)

rawDescription: Numpy matrix row comparison

description: Numpy matrix row comparison


code:
import subprocess
with open('CONTENT','w') as f1:
    p1 = subprocess.Popen(["sort", "CONTENT1"], stdout=subprocess.PIPE)
    p2 = subprocess.Popen(["uniq"], stdin=subprocess.PIPE, stdout=f1)
    out_line = p1.stdout.readline()
    while out_line:
        p2.stdin.write(out_line)
        out_line = p1.stdout.readline()

rawDescription: Redirecting Output of PIPE to a FILE in python


code: avg = y.mean(axis=(1, 3))

rawDescription: How can I vectorize the averaging of 2x2 sub-arrays of numpy array?

description: Vectorize the averaging of 2x2 sub-arrays of numpy array


code:
class SummaryHistory(models.Model):
    version = models.IntegerField(editable=False)
    book = models.ForeignKey('Book')
    text = models.TextField()

    class Meta:
        unique_together = ('version', 'book',)

    def save(self, *args, **kwargs):
        # start with version 1 and increment it for each book
        current_version = SummaryHistory.objects.filter(book=self.book).order_by('-version')[:1]
        self.version = current_version[0].version + 1 if current_version else 1
        super(SummaryHistory, self).save(*args, **kwargs)

rawDescription: Store versioned history of Field in a Django model


rawDescription: multi variable for loops python

description: Multi variable for loops python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('
asdfasdf

foo
')
soup.find_all(['a', 'div'])

rawDescription: Beautiful Soup Using Regex to Find Tags?

description: Beautiful Soup Using Regex to Find Tags


code:
from django.db.models import signals

def send_email_on_new_order(instance, created, raw, **kwargs):
    # Fixtures or updates are not interesting.
    if not created or raw:
        return

    # `instance` is your PurchaseOrder instance.
    email = EmailMessage('Purchase Order System', 'your message here', to=['youremail@gmail.com'])
    email.send()

signals.post_save.connect(send_email_on_new_order, sender=PurchaseOrder, dispatch_uid='send_email_on_new_order')

rawDescription: How can I send an email based on a new primary key being generated through Django?

description: Send an email based on a new primary key being generated through Django


rawDescription: Replacing html script element sources with regular expression in Python


code:
correct_filename = u"торт.txt"
mojibake = correct_filename.encode('utf-8').decode('cp1251') # WRONG
print(mojibake) # if you see this;
print(mojibake.encode('cp1251').decode('utf-8')) # recode

rawDescription: Convert windows-1251 directory names to unicode (using Python)


code:
'    def find_nodes(graph):'
'        # get the distinct nodes from the edges'
'        nodes = []'
'        l = len(graph)'
'        for i in range(l):'
'        \tedge = graph[i]'
'        \tn1 = edge[0]'
'        \tn2 = edge[1]'
'        \tif n1 not in nodes:'
'        \t\tnodes.append(n1)'
'        \tif n2 not in nodes:'
'        \t\tnodes.append(n2)'
'    \treturn nodes'

rawDescription: Python: Why is one element in this list not being printed?


code:
def g(n):
    if n <= 3:
        return n
    a, b, c = 1, 2, 3
    for i in range(n - 3):
        a, b, c = b, c, c + 2 * b + 3 * a
    return c

rawDescription: Python: Convert this recursive function to iterative


code:
df = pd.DataFrame(np.random.randn(1000000, 3),
                  columns=list('ABC'),
                  index=pd.DatetimeIndex(start='2015-1-1', freq='10s', periods=1000000))

rawDescription: Does Indexing makes Slice of pandas dataframe faster?


code:
subprocess.Popen( ["-c", "kill -USR2 %s" % master],
    stdout=subprocess.PIPE, shell=True).wait()

rawDescription: How to properly pass dash parameters to subprocessPopen

description: Properly pass dash parameters to subprocessPopen


rawDescription: python binding of variable to particular expression

description: Python binding of variable to particular expression


code:
data = list(filter(lambda x: "flag" in x.lower(), data))
print(data)  # => ['The flag is: 917035HrQ0PODo#']

rawDescription: connecting to server and sending data to it in python

description: Connecting to server and sending data to it in python


code:
[
    'You are experiencing a decrease in sales and customers',
    'If your brand design does not reflect what you deliver',
    'If you want to attract a new target audience',
    'Management change',
    '19 Questions to Ask Yourself Before You Start Rebranding'
]

rawDescription: How to filter out unwanted tags within tags

description: Filter out unwanted tags within tags


code:
@pytest.mark.parametrize('data', [1,2,3,4])
def test_data(data):
    assert data > 0

rawDescription: Paramaterize unit tests in python

description: Paramaterize unit tests in python


rawDescription: Concatenate generator and item

description: Concatenate generator and item


code:
import datetime
start = datetime.date(2015, 1, 1)
dts = [ start + datetime.timedelta(days=int(ea)-1) for ea in time ]
time = [ ea.strftime('%d-%m') for ea in dts ]

rawDescription: Plotting Days of the year Python

description: Plotting Days of the year Python


rawDescription: specifying start and end positions from python list

description: Specifying start and end positions from python list


code:
s = '''
<counter name="abcb">70324360</counter>
<counter name="efghij">1094</counter>
<counter name="klm">0</counter>'''

import xml.etree.ElementTree as ET

tree = ET.fromstring('<root>' + s + '</root>')

def get_counter(name):
    for node in tree.iter('counter'):
        if node.attrib.get('name') == name:
            return node.text

rawDescription: Creating a python table from key/counters text file


code:
b=np.arange(0,20,1)
c=b*0.5
d=b*2

plt.plot(b,d,color='r',marker='o',label='set 1')
plt.plot(b,c,color='g',marker='*',label='set 2')

plt.legend(loc='upper left')

rawDescription: Multiple legends and multiple colors/shapes matplotlib


code:
df
df.index

rawDescription: Pandas: read_csv with only HH:MM:SS in the index


rawDescription: Using python 3.x how can I pass a Tree object from ete3 to DendroPy without writing to file


rawDescription: Python Image Uploading with AjaxUpload

description: Python Image Uploading with AjaxUpload


code:
time, timeStr, person
1, 2, 3
4, 5, 6

rawDescription: Using python dtype as headers for compressed csv


code:
from Tkinter import Tk, Button,Label,StringVar,LEFT,RIGHT,BOTTOM,TOP
import tkMessageBox
import random
class MyButton:
    def __init__(self,root,label,**config):
        self.text_var = StringVar()
        self.text_var.set(label)
        self.btn = Button(root,textvariable=self.text_var,**config)
    def SetLabel(self,label,**config):
        self.text_var.set(label)
        self.btn.configure(**config)
    def GetLabel(self):
        return self.text_var.get()
    def __getattr__(self,attr):
        return getattr(self.btn,attr)
class MyQuiz:
    def check_button(self,btn):
        if btn.cget("bg") == "green":
            tkMessageBox.showinfo("WINNER!!!","You picked the right one!")
            return True
        else:
            tkMessageBox.showinfo("Sorry no Luck","You picked the WRONG one!")

    def on_button(self,button_index):
        self.root.after_cancel(self.timer)
        if self.check_button(self.buttons[button_index]):
            self.buttons[button_index].configure(bg="red")
            self.buttons[random.randint(0,2)].configure(bg="green")
            self.timer_text.set("30")
        self.timer = self.root.after(1000,self.on_timer)

    def on_timer(self):
        time_left = int(self.timer_text.get()) - 1
        if time_left <= 0:
            tkMessageBox.showinfo("Out of Time!","You Ran Out Of Time!!!")
        else:
            self.timer_text.set(str(time_left))
            self.timer = self.root.after(1000,self.on_timer)
    def __init__(self):
        self.root = root = Tk()
        self.timer_text=StringVar()
        self.timer_text.set("30")
        self.label1 = Label(self.root,text="Pick The Right One")
        self.label2 = Label(self.root,textvariable=self.timer_text)
        self.label1.pack(side=TOP)
        self.label2.pack(side=RIGHT)
        self.buttons = [MyButton(root,"Button1",bg="green",command=lambda:self.on_button(0)),
                        MyButton(root,"Button2",bg="red",command=lambda:self.on_button(1)),
                        MyButton(root,"Button3",bg="red",command=lambda:self.on_button(2)),]
        self.buttons[0].pack(side=TOP)
        self.buttons[1].pack(side=TOP)
        self.buttons[2].pack(side=TOP)
        self.timer = root.after(1000,self.on_timer)
        self.root.mainloop()

MyQuiz()

rawDescription: Timer or stopwatch in python

description: Timer or stopwatch in python


rawDescription: How to let user pick colormap?

description: Let user pick colormap


code:
import tkinter as tk

class Text(tk.Text):
    def destroy(self):
        global ending_text
        ending_text = self.get("1.0","end-1c")
        super(Text,self).destroy()

rawDescription: Integrating file and text widget python/tkinter


code:
timeit.timeit('primes_list(10000)', 'from __main__ import primes_list', number=1000)
timeit.timeit('erat(10000)', 'from __main__ import erat', number=1000)

rawDescription: Is this more efficient than Sieve of Eratosthenes?


code:
class NameDetailsForm(forms.ModelForm):
    ...
    def clean_language_version(self):
        lang = self.cleaned_data.get('language_version')
        if LanguageVersion.objects.filter(language_code=lang, language_code_disabled=True).exists():
            raise forms.ValidationError("Language code disabled")
        return lang

rawDescription: django - prevent form submission if submitted form value is X

description: Django - prevent form submission if submitted form value is X


rawDescription: Splitting a string with multiple separators


code:
homework = work.objects.get(pk=1)
math = Class.objects.get(pk=1)
homework.subject = math
homework.save()
quizzes = work.objects.get(pk=2)
math = Class.objects.get(pk=1)
quizzes.subject = math
quizzes.save()

rawDescription: Django foreign key starting with nothing


code:
df = pd.DataFrame(np.random.randn(3,3))
df
df2 = df.stack()
df2

rawDescription: Pandas stacking DataFrame and concatenating name of columns with index


code:
a = myclass()
a.function().params

rawDescription: How can I extend a local class in a function?

description: Extend a local class in a function


code:
import numpy as np
np.random.rand(10)
np.random.randn(10)

rawDescription: The random number generator in numpy

description: The random number generator in numpy


rawDescription: Unpacking a list / tuple of pairs into two lists / tuples


code:
from PyQt4 import QtCore, QtGui, Qt
import cPickle
import pickle

rawDescription: Multiple drag and drop in PyQt4

description: Multiple drag and drop in PyQt4


rawDescription: Can I sort text by its numeric value in Python?

description: Sort text by its numeric value in Python


code:
prices = [float(item[2]) for item in world_dict if item[1] == 'House']
print(prices)

rawDescription: Python - Store a value in an array inside one for loop


code:
import decimal
decimal.getcontext().rounding = decimal.ROUND_DOWN
decimal.getcontext().prec = 28
a = decimal.Decimal('15.80') / decimal.Decimal('483.49870000')
b = a * decimal.Decimal('483.49870000')
print(b)

rawDescription: PHP bcmath versus Python Decimal

description: PHP bcmath versus Python Decimal


code:
def update(all_marks, stud_num, mark, column):
    tmp = all_marks
    for i in range(len(tmp)):
        if stud_num in tmp[i]:
            tmp[i][column] = mark
    return tmp

rawDescription: Replacing values in csv by using python

description: Replacing values in csv by using python


rawDescription: Killing thread in python

description: Killing thread in python


code:
def lcsubstring_length(a, b):
    table = [[0] * (len(b) + 1) for _ in xrange(len(a) + 1)]
    l = 0
    for i, ca in enumerate(a, 1):
        for j, cb in enumerate(b, 1):
            if ca == cb:
                table[i][j] = table[i - 1][j - 1] + 1
                if table[i][j] > l:
                    l = table[i][j]
    return l

rawDescription: Python: Length of longest common subsequence of lists


rawDescription: Using django 1.8 duration field to annotate a set of django objects


code:
self.numButton.append(
    Button(root,text=num[i],command=lambda i=i: self.do(num[i])))

rawDescription: Creating buttons inside loop command failing


code:
def loadTreeFrom(self, source, sourcePath):
    if source not in ('CSV', 'DB'):
        raise AttributeError('Unknown source : supported sources are CSV or DB')

    dao = None
    if source == 'CSV':
        dao = CSV            # dao just a reference to the class now
    elif source == "DB":
        dao = SQLite

    with dao() as tasks:
        #populate the tasks first
        for task in tasks:
            self.tasks[unicode(task.id)] = task

        # then populate the dependencies
        for item in self.tasks.iteritems():
            ancestorsID = dao.loadPredecessorsID(item[1])
            self.addDependencies(item[1], ancestorsID)

rawDescription: How to consume a File / Cursor line by line from another object that has no access to them

description: Consume a File / Cursor line by line from another object that has no access to them


code:
import csv
with open('large.csv','w') as f1:
    writer=csv.writer(f1, delimiter='\t',lineterminator='\n',)
    for i in range(1000000):
        row = [i + j*0.2 for j in range(i+1)]
        writer.writerow(row)

rawDescription: csv writing within loop

description: Csv writing within loop


code:
import mock # import unittest.mock (Python 3.3+)
import pytest

from some_module import function_to_test

@pytest.fixture(scope='function')
def mock_open(request):
    m = mock.patch('__builtin__.open', mock.mock_open(read_data='file content'))
    m.start()
    request.addfinalizer(m.stop)

def test_something(mock_open):
    ret = function_to_test()
    assert ret[1]() == 'file content'

rawDescription: Mocking not working with pytest and flexmock


code:
#!/usr/bin/env python

import os
import subprocess

RTMPDUMP="./rtmpdump"
assert os.path.isfile(RTMPDUMP)
command = [RTMPDUMP,'-r','rtmp://oxy.videolectures.net/video/',
        '-y','2007/pascal/bootcamp07_vilanova/keller_mikaela/bootcamp07_keller_bss_01',
        '-a','video','-s',
        'http://media.videolectures.net/jw-player/player.swf',
        '-w','ffa4f0c469cfbe1f449ec42462e8c3ba16600f5a4b311980bb626893ca81f388'
        ,'-x','53910','-o','test.flv']

stdout = open("stdout.txt","wb")
stderr = open("stderr.txt","wb")
subprocess.call(command,stdout=stdout,stderr=stderr)

rawDescription: Python: How do I redirect this output?

description: Python: How do I redirect this output


rawDescription: How do I get the path and name of the file that is currently executing?

description: Get the path and name of the file that is currently executing


code: dict([i.strip() for i in item.split("=")] for item in PARAMS.split(";"))

rawDescription: Python split twice with strip in one line


code:
for i in range(0,n):
    j = i / 3
    k = i % 3
    lists[i][j][k] = 2

rawDescription: Replace index in list

description: Replace index in list


code: people = [{'name':'Tom', 'uid':'asdlfkj223'}, {'name':'Jerry', 'uid':'alksd32'},]

rawDescription: Convert One Key Value from List of Dictionaries to List


code: 1 - residual / sum((y - y.mean())**2)

rawDescription: Converting Numpy Lstsq residual value to R^2


rawDescription: Python regex for a complex string

description: Python regex for a complex string


code:
class Namespace(object):
    pass

a = Namespace()
a.x = 1     # same as as.__dict__['a'] = 1
a.__dict__

rawDescription: How can I create a separate namespace within a Python script?

description: Create a separate namespace within a Python script


code:
import networkx as nx
import matplotlib.pyplot as plt

data= open("test.txt", "r") # replace with the path to your edge file
G = nx.DiGraph()
edge_labels = dict()
for line in data:
    node1, node2, weight1 = line.strip().split("; ")
    length = float(weight1.replace(",", ".")) # the length should be a float
    G.add_edge(node1, node2, label=str(weight1), length=length)
    edge_labels[(node1, node2)] = weight1 # store the string version as a label

# Draw the graph
pos = nx.spring_layout(G) # set the positions of the nodes/edges/labels
nx.draw_networkx(G, pos=pos) # draw everything but the edge labels
nx.draw_networkx_edge_labels(G, pos=pos, edge_labels=edge_labels)
plt.show()

rawDescription: python 3 directed graph with labels from file

description: Python 3 directed graph with labels from file


code:
import glob
import pandas as pd

path = './file[0-9]*'
filenames = glob.glob(path)

df_list = []
# Read in all files and concatenate to a single data frame:
for file in filenames:
    df_list.append(pd.read_csv(file, header = None, sep = '\s+'))
df = pd.concat(df_list)

# changing type for convenience:
df[2] = df[2].astype(str)
# sorting here is not necessary:
# df = df.sort_values(by = 1)

df2 = df.groupby(by = 1).aggregate({0:'first', 2: lambda x: ' '.join(x)})
df2.to_csv('output_file', header = None)
# (Columns in 'output_file' are separated by commas. )

rawDescription: merge multiple files by reading them simultaneously line by line?

description: Merge multiple files by reading them simultaneously line by line


rawDescription: What does list[x::y] do?

description: What does list[x::y] do


code:
def log_message(self, format, *args):
    sys.stderr.write("%s - - [%s] %s\n" %
                     (self.client_address[0],
                      self.log_date_time_string(),
                      format%args))

rawDescription: Control wsgiref simple_server log

description: Control wsgiref simple_server log


rawDescription: python: how to sort a list which is returned from a sql query?

description: Python: how to sort a list which is returned from a sql query


rawDescription: Fetching the pattern for input for the string using re.sub


code:
import posix_ipc # POSIX-specific IPC
import mmap      # From Python stdlib

class SharedMemory(object):
    """Python interface to shared memory.
    The create argument tells the object to create a new SHM object,
    rather than attaching to an existing one.
    """

    def __init__(self, name, size=posix_ipc.PAGE_SIZE, create=True):
        self.name = name
        self.size = size
        if create:
            memory = posix_ipc.SharedMemory(self.name, posix_ipc.O_CREX,
                                            size=self.size)
        else:
            memory = posix_ipc.SharedMemory(self.name)
        self.mapfile = mmap.mmap(memory.fd, memory.size)
        os.close(memory.fd)
        return

    def put(self, item):
        """Put item in shared memory.
        """
        # TODO: Deal with the case where len(item) > size(self.mapfile)
        # TODO: Guard this method with a named semaphore
        self.mapfile.seek(0)
        pickle.dump(item, self.mapfile, protocol=2)
        return

    def get(self):
        """Get a Python object from shared memory.
        """
        # TODO: Deal with the case where len(item) > size(self.mapfile)
        # TODO: Guard this method with a named semaphore
        self.mapfile.seek(0)
        return pickle.load(self.mapfile)

    def __del__(self):
        try:
            self.mapfile.close()
            memory = posix_ipc.SharedMemory(self.name)
            memory.unlink()
        except:
            pass
        return

rawDescription: Fast IPC/Socket communication in Java/Python


code:
def permutations_with_length_key(lst, length_key):
    tuples_by_length = {}
    for t in L:
        tuples_by_length.setdefault(len(t), []).append(t)
    positions = {k: i for i, k in enumerate(tuples_by_length.iterkeys())}
    for x in product(*[permutations(v) for v in tuples_by_length.itervalues()]):
        x = map(iter, x)
        yield [next(x[positions[y]]) for y in length_key]

rawDescription: Iterating through permutations that fit a specific key


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

class Model(QAbstractTableModel):
    def __init__(self, parent=None, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.items = ['Item_A_001','Item_A_002','Item_B_001','Item_B_002']

    def rowCount(self, parent=QModelIndex()):
        return len(self.items)
    def columnCount(self, parent=QModelIndex()):
        return 1

    def data(self, index, role):
        if not index.isValid(): return QVariant()
        elif role != Qt.DisplayRole:
            return QVariant()

        row=index.row()
        if row<len(self.items):
            return QVariant(self.items[row])
        else:
            return QVariant()

class Proxy(QSortFilterProxyModel):
    def __init__(self):
        super(Proxy, self).__init__()
        self.filterActive = False

    def setView(self, view):
        self._view = view

    def filterAcceptsRow(self, row, parent):
        if self.filterActive and '_B_' in self.sourceModel().data(self.sourceModel().index(row, 0), Qt.DisplayRole).toPyObject():
            self._view.selectRow(row)
        return True

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        tableModel=Model(self)

        proxyModel=Proxy()
        proxyModel.setSourceModel(tableModel)

        self.tableview=QTableView(self)
        self.tableview.setModel(proxyModel)
        self.tableview.horizontalHeader().setStretchLastSection(True)
        self.tableview.setSelectionMode(QAbstractItemView.MultiSelection)

        proxyModel.setView(self.tableview)

        button=QPushButton(self)
        button.setText('Select Items with B')
        button.clicked.connect(self.clicked)

        layout = QVBoxLayout(self)
        layout.addWidget(self.tableview)
        layout.addWidget(button)
        self.setLayout(layout)

    def clicked(self, arg):
        proxyModel=self.tableview.model()
        self.tableview.clearSelection()
        proxyModel.filterActive = True
        proxyModel.invalidateFilter()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

rawDescription: How to select QTableView index or row from inside of Model

description: Select QTableView index or row from inside of Model


code:
a
print(a)

rawDescription: Print tree with indentations

description: Print tree with indentations


code: nanmean_reduceat(a, [0, 2, 3])

rawDescription: numpy.nanmean of subsets of elements

description: Numpy.nanmean of subsets of elements


code:
django_app/monkey/init.py
def patch_tastypie():
    from tastypie.fields import ToManyField, RelatedField
    from tastypie.resources import Resource, ResourceOptions
    from monkey.tastypie import dehydrate, _build_reverse_url, get_via_uri, build_related_resource

    setattr(ToManyField, 'dehydrate', dehydrate)
    setattr(Resource, '_build_reverse_url', _build_reverse_url)
    setattr(Resource, 'get_via_uri', get_via_uri)
    setattr(ResourceOptions, 'include_resource_uri', False)
    setattr(RelatedField, 'build_related_resource', build_related_resource)

rawDescription: Django Tastypie Postgres slow server response


rawDescription: Regular expression: Match string between two slashes if the string itself contains escaped slashes


rawDescription: Create model for MySQL view and join on it


code: all_bs = [b.text for b in soup.findAll("b", {"class":"whb"})]

rawDescription: Find section of a string from keyword to keyword


code:
phone = models.CharField(max_length=20, blank=False)
is_mobile = models.BoleanField()

rawDescription: Multiple User types with Django 1.5, custom fields and shared auth


code: np.put(b, np.arange(len(a)), a)

rawDescription: inserting numpy data into a bigger array of zeros

description: Inserting numpy data into a bigger array of zeros


code:
lines = """assign (resid 3 and name H ) (resid 18 and name H ) 2.5 2.5 2.5 ! note string 1
assign (resid 16 and name H ) (resid 5 and name H ) 2.5 2.5 2.5 ! note string 2
assign (resid 42 and name H ) (resid 55 and name H ) 2.5 2.5 2.5 ! note string 3
assign (resid 44 and name H ) (resid 53 and name H ) 2.5 2.5 2.5 ! note string 4
assign (resid 53 and name H ) (resid 44 and name H ) 2.5 2.5 2.5 ! note string 5"""

import re

d = {}

r1 = re.compile(r"(?<=\))\s")
r2 = re.compile(r"\(.*\)")

for line in lines.splitlines():
    key = tuple(r1.split(r2.findall(line)[0]))
    # ("foo","bar") == ("bar","foo") , also check current key is not in d
    if tuple(reversed(key)) not in d and key not in d:
        d[key] = line

pp(list(d.values()))

rawDescription: Python Find Duplicate with Note String Preservation


code:
def toSimpleXml(data, wrap = False):
    result = list()
    input_data_type = type(data)
    if input_data_type is list:
        for sub_element in data:
            result.append(toSimpleXml(sub_element))
        data = ''.join(result)
    elif input_data_type is dict:
        for tag_name in data:
            sub_element = data[tag_name]
            result.append('<%s>' % tag_name)
            result.append(toSimpleXml(sub_element))
            result.append('</%s>' % tag_name)
        data = ''.join(result)

    if wrap:
        return '<%s>%s</%s>' % (wrap, data, wrap)

    return data

rawDescription: How to handle recursion in python?

description: Handle recursion in python


code:
import subprocess

child = subprocess.Popen(['python2', 'simple.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE)

for line in iter(child.stdout.readline, ""):
    print(line)
    if "name" in line:
        child.stdin.write("foo\n")
    elif "age" in line:
        child.stdin.write("100\n")

rawDescription: python subprocess: check to see if the executed script is asking for user input

description: Python subprocess: check to see if the executed script is asking for user input


rawDescription: find neighbours in np array in python

description: Find neighbours in np array in python


code:
while True:
    data = f.read(8192)
    if not data:
        break
    process(data)

rawDescription: Process large data in python

description: Process large data in python


rawDescription: PyGTK: When filtering keyboard input to an Entry widget, can't correctly set the cursor position while opening a large buffer


code: foo = bar = qaxx = spat = "1Value4All"

rawDescription: Python: Set many variables to the same value – with automatic count of number of variables


code:
productList = sc.parallelize([
    {u'name': u'product_id', u'price': 12, u'quantity': 1}])

productList.map(
    lambda x: {'name': x['name'],  'total': x['price'] * x['quantity']}
).first()

## {'name': 'product_id', 'total': 12}

rawDescription: How can I map a spark RDD of json objects to another RDD which contains objects with only selected set of attributes

description: Map a spark RDD of json objects to another RDD which contains objects with only selected set of attributes


rawDescription: How to parse a time

description: Parse a time


code:
round(c.next(), 4)
round(c.next(), 4)
round(c.next(), 4)
round(c.next(), 4)
round(c.next(), 4)

rawDescription: Rounding values for normalised variables


rawDescription: Get list of XML attribute values in Python


code:
value = int.from_bytes(your_bytes_value, byteorder='big')
bits_21_to_8 = (value & 0x1fffff) >> 8

rawDescription: How to shift bits in a 2-5 byte long bytes object in python?

description: Shift bits in a 2-5 byte long bytes object in python


code:
plt.figure()
plt.scatter(X,Y,c=Z,cmap=cm.jet)
plt.colorbar(ticks=np.unique(Z))
#or
#plt.colorbar(ticks=range(Z.min(),Z.max()+1))

rawDescription: Discrete pyplot scatter colobar

description: Discrete pyplot scatter colobar


code:
P.figure()
P.hist(x, range(100,300,10))
P.savefig('b.png')

rawDescription: Bar charts with xaxis bins using Plotly or Matplotlib?


code:
expected = 'https:%2F%2Fdl.dropbox.com%2Fu%2F94943007%2Ffile.kml'
url = 'https://dl.dropbox.com/u/94943007/file.kml'
urllib.quote(url, safe=':') == expected

rawDescription: percent encoding URL with python

description: Percent encoding URL with python


code: x, y, z = np.genfromtxt(r'dataa.txt', unpack=True)

rawDescription: Contour plot in Python importing txt table file


code:
output = []
for cat, counts in totals.iteritems():
    item = {'cat': cat}
    item.update(counts)
    output.append(item)

rawDescription: python dictionary with two running totals

description: Python dictionary with two running totals


code:
import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import QApplication, QDialog, QListWidgetItem, QListWidget, QIcon

def main():

    app = QtGui.QApplication(sys.argv)
    window = QDialog()

    list = QListWidget( window )

    itm = QListWidgetItem( "Tick" );
    itm.setIcon(QIcon(r"tick.png"));
    list.addItem(itm);

    window.show( )
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: how to add image with text in qlistwidget pyqt4 python?

description: Add image with text in qlistwidget pyqt4 python


code:
from itertools import tee
veggies_query_dict, fruits_query_dict = tee(query_dict)
veggies = [ x for x in veggies_query_dict if re.search('veggies',x['url']) ]
fruits = [ x for x in fruits_query_dict if re.search('fruits',x['url']) ]

rawDescription: Why can't I split this python list?

description: Why can't I split this python list


code:
from datetime import datetime
datetime.strptime('02/11/2010', '%d/%m/%Y').strftime('%Y-%m-%d')

rawDescription: Perl or Python: Convert date from dd/mm/yyyy to yyyy-mm-dd


code: totals = [match.groups for match in filter(None, map(pattern.match, cgi_keys))]

rawDescription: Can this python code be expressed with list comprehension?


code:
import socket
gce_name = socket.gethostname()

rawDescription: Get the name or ID of the current Google Compute Instance


rawDescription: Merge dicts from a list of dicts based on some key/value pair


code:
loclength = 1
kT = 1
cutoff = 0.25
a = np.random.rand(3, 5)
b = np.random.rand(4, 5)

rawDescription: vectorize a function for numpy, that correlates each element of one vector with each element of another

description: Vectorize a function for numpy, that correlates each element of one vector with each element of another


code:
b=set(B)
[x in b for x in A]

rawDescription: Python: How to return list of booleans to see if elements of one list in another list


code:
f1 = compile_theano_version1(N, S, np.float32)
f2 = compile_theano_version2(N, S)

rawDescription: Speedup for the reduce operation in Theano


rawDescription: Python elements retrieved from container (list, dict, tuple etc) passed by reference or value?


code:
Main_Window = Tk()
Main_Window.title("Main Window")
Main_Window.geometry("500x500+25+25")

Label_1 = Label(Main_Window, text="Label 1", width=8, bg=Label_1_Color, fg="black", bd=2, relief=RIDGE, padx=2, pady=2)
Label_1.pack(side=TOP, anchor=W)
...

Radio_Group = IntVar()

def setColor():
    if Radio_Group.get() == 1:
        Label_1.configure(background="white")
        Label_2.configure(background="white")
        Label_3.configure(background="white")
        Label_4.configure(background="white")
    ...

Radio_1 = Radiobutton(Main_Window, text="Option 1", bg="white", variable=Radio_Group, value=1, command=setColor)
Radio_1.pack(side=BOTTOM, anchor=W)
Radio_1.invoke()
...

rawDescription: Python Label change upon radiobutton selection


code:
def print_obj(obj, prefix=''):
    sys.stdout.write('%s%s' % (prefix, obj))
    if obj.child:
        print_obj(obj, '.')

rawDescription: Parent child relationship in django

description: Parent child relationship in django


code:
from random import random

list1 = []

def funct1():
    list1.append(random())
    print (list1)

funct1()
funct1()

rawDescription: Python 3.4 Import a list to function

description: Python 3.4 Import a list to function


code:
# Example of just one line of the data
line = """x1=45 y1=74 z1=55 col1=[255, 255, 255] x2=46 y2=74 z2=55 col2=[255, 255, 255] """

# Extract the relevant x1, y1, z1 values, stored as a list of strings
p1 = re.findall(r"[x-z][1]=([\d]*)", line)

# Extract the relevant x2, y2, z2 values, stored as a list of strings
p2 = re.findall(r"[x-z][2]=([\d]*)", line)

# Convert the elements in each list from strings to integers
p1 = [int(x) for x in p1]
p2 = [int(x) for x in p2]

# Calculate difference vector (Im assuming this is what you're trying to do)
diff = [p2[i] - p1[i] for i in range(len(p2))]

rawDescription: Extracting xyz coordinate values from a string into lists


rawDescription: Most pythonic way to delete text between two delimiters


code:
a = []
a.append(4)
a.append(2)
a.append(5)

rawDescription: How to correctly call variables from a txt file into my python code?

description: Correctly call variables from a txt file into my python code


rawDescription: iterating over lists finding standard deviation of each point

description: Iterating over lists finding standard deviation of each point


rawDescription: Wait until user stopped typing in Tkinter


code:
def chunks(l, n, step):
    """ Yield successive n-sized chunks from l with a given step
    """
    for i in range(0, len(l)-n+1, step):
        yield l[i:i+n]

string = 'Thequickbrownfox.'

for c in chunks(string, 3, 1):
    print(c)

rawDescription: Convert string into substrings ngrams - Python


code:
if credentials.access_token_expired:
    credentials.refresh(httplib2.Http())

rawDescription: How can I refresh a stored Google oAuth credential

description: Refresh a stored Google oAuth credential


code:
[
    ("'goal','fastbreak','leftfoot'", '1'),
    ("'goal','openplay','leftfoot'", '2'),
    ...
    ("'miss','openplay','rightfoot'", '27')
]

rawDescription: How to parse this JSON array of arrays (I think)

description: Parse this JSON array of arrays (I think)


code:
for k in dct.keys():
    d = euc(int(dct[k][0]), int(dct[k][0]))
    dis.append((k, d))

dis = sorted(dis, key=lambda x: x[1])
dis

rawDescription: Find closest location from a list of locations in csv file Python


code:
for start in ('567...'):
    for subcombo in product('01234567...', repeat=9):
        yield (start,) + subcombo

rawDescription: Python itertools.combinations continue from certain value?


rawDescription: trying to mask 2D numpy arrays based on values in one column

description: Trying to mask 2D numpy arrays based on values in one column


rawDescription: Importing file as list. \n after every item


code: subprocess.call(cmd, shell=True)

rawDescription: python 3: subprocessing a python script that uses argparse

description: Python 3: subprocessing a python script that uses argparse


rawDescription: Python regex negative lookbehind not failing match


code: db.mytable.myfield.label = CAT('My', BR(), 'Label')

rawDescription: multi line headers with web2py's SQLFORM.grid

description: Multi line headers with web2py's SQLFORM.grid


code:
import datetime as dt
try:
    import email.utils as eu
except ImportError:
    import email.Utils as eu  # for Python 2.3

date_string="Fri, 08 Aug 2011 14:15:10 -0400"
ttuple=eu.parsedate(date_string)
date=dt.datetime(*ttuple[:6])
print(date.isoformat()+'Z')

rawDescription: converting a date to string for manipulation in python

description: Converting a date to string for manipulation in python


code:
from django.conf import settings
import socket

def test_connection_to_db(database_name):
    try:
        db_definition = getattr(settings, 'DATABASES')[database_name]
        s = socket.create_connection(
            (db_definition['HOST'], db_definition['PORT']), 5)
        s.close()
        return True
    except:
        return False

class MasterSlaveRouter(object):
    def db_for_read(self, model, **hints):
        """
        Reads go to a randomly-chosen slave.
        """
        if test_connection_to_db('master'):
            return 'master'
        return 'slave'

    def db_for_write(self, model, **hints):
        """
        Writes always go to master.
        """
        if test_connection_to_db('master'):
            return 'master'
        return 'slave'

    def allow_relation(self, obj1, obj2, **hints):
        """
        Relations between objects are allowed if both objects are
        in the master/slave pool.
        """
        db_list = ('master', 'slave')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, model):
        """
        All non-auth models end up in this pool.
        """
        return True

rawDescription: Django multiple DB how to write router

description: Django multiple DB how to write router


code:
def print_hello_world(hello, world, how, are, you):
    print(hello, world, how, are, you)

rawDescription: How can I insert all the variable in a function automatically in python?

description: Insert all the variable in a function automatically in python


code:
with open('my_file.html', 'w') as fo:
    fo.write(tsod.to_html())

rawDescription: save pandas' to_html' as a file

description: Save pandas' to_html' as a file


code: elt.toprettyxml(indent = '  ')

rawDescription: Pretty print Python xml.dom.Element object


code:
if tn is not None:
    tn.close()
    tn = None
else:
    tn = telnetlib.Telnet(HOST)

rawDescription: Find and remove a Python object from locals() or globals()


code:
for each_line in results_f:
    (name,score) = each_line.split()
    scores.append(float(score))

results_f.seek(0)

for line in results_f:
    (name,score) = line.split()
    surfers.append(name)

rawDescription: Python : The second for loop is not running


rawDescription: How to get the mean over time value

description: Get the mean over time value


code:
def mask2(rgb, threshold=80, color=[255,20,147]):
    res = rgb.copy()
    lab = skimage.color.rgb2lab(rgb)
    mask = lab[:,:,0] > threshold
    res[mask] = np.array(color).reshape(1,-1).repeat(mask.sum(),axis=0)
    return res

rawDescription: python code slow skin detection

description: Python code slow skin detection


code: my_signal = pyqtSignal([int], [str])

rawDescription: PyQt proper use of emit() and pyqtSignal()


code:
big_regex = re.compile('|'.join(map(re.escape, prohibitedWords)))
the_message = big_regex.sub("repl-string", str(word[1]))

rawDescription: Replace all words from word list with another string in python


code: data.ix[(data.OUT.str.contains('nan')), 'OUT'] = np.nan

rawDescription: Cleaning date and time records in python pandas


code: df.groupby('Site').EnteroCount.apply(lambda group: group.product() ** (1 / float(len(group))))

rawDescription: Create complicated conditional column (geometric mean) Python


code:
from sqlalchemy.orm import validates

class Obs(DeclarativeBase):
    __tablename__ = 'obs'
    id = Column(Integer, primary_key=True)
    state = Column(Integer, default=0)
    @validates('state')
    def update_state(self, key, value):
        self.task.state = value
        return value

rawDescription: Trigger in sqlachemy

description: Trigger in sqlachemy


code:
while True:
    try:
        num = int(input("Give me an integer between 1 and 100:"))
        if num > 100 or num < 1:
            raise ValueError
        print ("Thank you for your input")
        break
    except ValueError:
        print ("Sorry, that is not an integer between 1 and 100. Try again.")

rawDescription: python try except format

description: Python try except format


code: image_array[0,0] = 100

rawDescription: Change a pixel value

description: Change a pixel value


code: norm = math.sqrt(u ** 2 + v ** 2 + w ** 2)

rawDescription: How to update 3D arrow animation in matplotlib

description: Update 3D arrow animation in matplotlib


code: proc = subprocess.Popen(['./program_name', 'arg1', 'arg2', 'etc'], env=environ)

rawDescription: How to get PID via subprocess.Popen with custom environment variable?

description: Get PID via subprocess.Popen with custom environment variable


code: os.system("xfce4-terminal -e 'bash -c \"echo hello ; /bin/bash ; exit 0\"'")

rawDescription: How to kill a terminal after completion of process

description: Kill a terminal after completion of process


code:
df['Short Date']= pd.to_datetime(df['Created Date'])
df
df['Short Date'] = df['Short Date'].apply(lambda x:x.date().strftime('%m%d%y'))
df

rawDescription: Strip time from an object date in pandas


code:
@api.onchange('product_id')
def onchange_product_id(self):
    res = {}
    if self.product_id:
        ids = self.product_id.categ_id.lot_fields_category.lot_additional_fields.mapped('id')
        res['domain'] = {'lot_lot_additional_fields': [('lot_additional_fields', 'in', ids)]}
    return res

rawDescription: Odoo 8 domain filter on Many2one field

description: Odoo 8 domain filter on Many2one field


code:
class Foo(object):
    CONST_NAME = "Name"

rawDescription: How to create a constant in Python

description: Create a constant in Python


code:
#determinant of matrix a
def det(a):
    return a[0][0]*a[1][1]*a[2][2] + a[0][1]*a[1][2]*a[2][0] + a[0][2]*a[1][0]*a[2][1] - a[0][2]*a[1][1]*a[2][0] - a[0][1]*a[1][0]*a[2][2] - a[0][0]*a[1][2]*a[2][1]

#unit normal vector of plane defined by points a, b, and c
def unit_normal(a, b, c):
    x = det([[1,a[1],a[2]],
             [1,b[1],b[2]],
             [1,c[1],c[2]]])
    y = det([[a[0],1,a[2]],
             [b[0],1,b[2]],
             [c[0],1,c[2]]])
    z = det([[a[0],a[1],1],
             [b[0],b[1],1],
             [c[0],c[1],1]])
    magnitude = (x**2 + y**2 + z**2)**.5
    return (x/magnitude, y/magnitude, z/magnitude)

#dot product of vectors a and b
def dot(a, b):
    return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]

#cross product of vectors a and b
def cross(a, b):
    x = a[1] * b[2] - a[2] * b[1]
    y = a[2] * b[0] - a[0] * b[2]
    z = a[0] * b[1] - a[1] * b[0]
    return (x, y, z)

#area of polygon poly
def area(poly):
    if len(poly) < 3: # not a plane - no area
        return 0

    total = [0, 0, 0]
    for i in range(len(poly)):
        vi1 = poly[i]
        if i is len(poly)-1:
            vi2 = poly[0]
        else:
            vi2 = poly[i+1]
        prod = cross(vi1, vi2)
        total[0] += prod[0]
        total[1] += prod[1]
        total[2] += prod[2]
    result = dot(total, unit_normal(poly[0], poly[1], poly[2]))
    return abs(result/2)

rawDescription: Python: find area of polygon from xyz coordinates


code: fact = lambda x: x == 0 and 1 or x * fact(x-1)

rawDescription: Can a lambda function call itself recursively in Python?


code:
import csv
with open('a.csv','r') as f:
    reader = csv.reader(f,skipinitialspace=True)
    words = next(reader)
words

rawDescription: Reading comma-separated words into pandas


code:
b1=b.astype('datetime64[M]')
b1

rawDescription: Select specified month date from numpy array (datetime object)


code: list(zip(*[(k, v) for k, vs in d.items() for v in vs]))

rawDescription: From dictionary of lists to two lists of same length


rawDescription: Counting bigrams (pair of two words) in a file using python


code:
import threading

def worker():
    for share in shares.split(', '):
        ...
        rsyncRun = subprocess.Popen(...)
        out, err = rsyncRun.communicate()

t = threading.Thread(target = worker)
t.daemon = True
t.start()
t.join()

rawDescription: Run multiple subprocesses in foreach loop? One at the time?


code:
def a():
    print('a')
def b():
    print('b')
l = [a,b]
for i in l:
    i()

rawDescription: Can I add function references to a list, and iterate over this list calling each in succession?

description: Add function references to a list, and iterate over this list calling each in succession


code:
def meetup_day(year, month, day_name, week_num):
    year_int = int(year)
    month_int = int(month)
    day_int = list(calendar.day_name).index(day_name)
    relevant_mdays = [i for i in zip(*calendar.monthcalendar(year_int, month_int))[day_int] if i != 0]
    if week_num == 'teenth':
        mday = next((i for i in relevant_mdays if i in range(13, 20)), None)
    else:
        if week_num == 'last':
            week_int = -1
        else:
            week_int = int(week_num[0]) - 1
        mday = relevant_mdays[week_int]
    return datetime.date(year_int, month_int, mday)

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


code:
dic = {}
seen = set()
for k,v in dicts.iteritems():
    if v[11] not in seen:
        dic[k] = v
        seen.add(v[11])

dic

rawDescription: python delete those keys from dictionary who has same value

description: Python delete those keys from dictionary who has same value


code:
send_mail('Subject', 'Content', 'sender@example.com', ['nobody@example.com'],
          html_message='This is <b>HTML</b> Content')

rawDescription: Django Forms : Make some text sent by form to email appear bold


code:
__import__(module_name)
class_obj = getattr(sys.modules[module_name], class_name)

rawDescription: getting the class path or name space of a class in python even if it is nested

description: Getting the class path or name space of a class in python even if it is nested


code:
fig, ax = plt.subplots()

df1_99.plot(x='date', y='units', ylim=[0,11], figsize=[12,12], ax=ax)

ax.axvspan(*mdates.datestr2num(['10/27/2011', '11/2/2011']), color='red', alpha=0.5)
plt.show()

rawDescription: How to use ax with Pandas and Matplotlib

description: Use ax with Pandas and Matplotlib


code:
df["C"] = df["A"].replace(group_ids(zip(df.A, df.B)))
df

rawDescription: Grouping Data into Clusters Based on DataFrame Columns


rawDescription: Pandas - Get first row value of a given column


code:
def split_string(source,separators):
    return re.split('(?:'+'|'.join(re.escape(x) for x in separators)+')+',source)

rawDescription: Splitting Strings in Python with Separator variable


code:
ToD=({'name': 'Blah1', 'age': 'x'}, {'name': 'Blah2', 'age': 'y'}, {'name': None, 'age': None})
[e for e in ToD if None not in e.values()]

rawDescription: How should I remove all dicts from a list that have None as one of their values?


rawDescription: How should I serialize game objects containing pygame surfaces?


code:
def sort_df(df, column_idx, key):
    '''Takes a dataframe, a column index and a custom function for sorting,
    returns a dataframe sorted by that column using that function'''

    col = df.ix[:,column_idx]
    df = df.ix[[i[1] for i in sorted(zip(col,range(len(col))), key=key)]]
    return df

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


code:
def bytearray_test_opt():
    tStart = time.clock()
    ba = bytearray()
    for i in range(COUNT):
        ba.extend(MSG)
    print('array opt time:', time.clock() - tStart)

rawDescription: Python bytearray verses list of bytes

description: Python bytearray verses list of bytes


code:
class MyEncoder(json.JSONEncoder):
    """ json.JSONEncoder extension: handle querysets """
    def default(self, obj):
        if isinstance(obj, QuerySet) or isinstance(obj, RawQuerySet):
            return serializers.serialize('python', obj, ensure_ascii=False)
        return json.JSONEncoder.default(self, obj)

rawDescription: Django: Serialize to JSON a dictionary with items that can include querysets


code:
b,b,b,b
d,d,d,d
e,e,e,e

rawDescription: Select row to be deleted in csv file in python


code:
coupon_customers = df.groupby(['cohort_group', 'customer_id']).apply(lambda x: x.sort_values('cohort_period').iloc[0]).dropna(subset=['coupon_id']).customer_id.tolist()

[2]

rawDescription: Cohort group condition

description: Cohort group condition


code:
monlist = ['dragon', 'angry gnome vigilante', 'Donald Trump', 'lambhorn rider', 'bone cancer']
while check == 'y':
    check = input('Do you want to create a monster? (y/n): ')
    if check == 'n':
        break
    new_monster = input('New monster name: ')
    monlist.append(new_monster)

rawDescription: Dictionary creator (Python 3.x)

description: Dictionary creator (Python 3.x)


code: myDict["Objective A"] = MyClass.FuncA()

rawDescription: How can I associate dictionary keys with functions?

description: Associate dictionary keys with functions


code:
jvmData = "..." #the string holding the data
jvmLines = jvmData.split("\n") #a list of the lines in the string
lines = filter(lambda x: "name=" in x, jvmLines) #filter all data lines
result = {}
for line in lines:
    data = dict(map(lambda x: x.split("="), line.split(", ")))
    if data["name"] == "HeapSize":
        result[data["name"]] = data["current"]
    else:
        result[data["name"]] = data["count"]

rawDescription: Create name value pairs in python

description: Create name value pairs in python


rawDescription: Detect errant data over consecutive rows of a Python Pandas DataFrame


code:
class ClientList(Resource):
    def get(self):
       clients = []
       for client in model.Client.query.all():
           if client.email:
               clients.append(marshal(client_fields))
           else:
               clients.append(marshal(client_fields_no_email))
       return clients

rawDescription: Flask-RESTful - don't return object property instead of returning null


code:
n = 5
x = [my_array[i:i+n] for i in range(0,len(my_array),n)]

rawDescription: Splitting strings on an nth character in Python


code: df = pd.read_pickle(file_name)

rawDescription: How to store data frame using PANDAS, Python

description: Store data frame using PANDAS, Python


code:
process = Popen(['/bin/sh'], stdin=PIPE, stdout=PIPE)
out,err =process.communicate('ls -al\n')

rawDescription: Read stdout from subprocess until there is nothing left


code:
def start_tcp_client(cond):
    ls_output = subprocess.Popen(['tcpclient.exe','192.168.4.110','8000','1000','1400'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,bufsize=3)
    with cond:
        cond.wait()
        ls_output.communicate(input = '3')
        ls_output.communicate()

def TCPSettings(cond):
    with cond:
        RunSer2Command(lines2[22])
        RunSer2Command(lines2[23])
        cond.notify()

    condition = threading.Condition()
    condition1 = threading.Condition()
    Client_thread=threading.Thread(name='Client_thread', target=start_tcp_client, args=(condition,))
    TCP_thread=threading.Thread(name='TCP_thread', target=TCPSettings, args=(condition,))
    RunSer2Command(lines2[21])
    time.sleep(2)
    Client_thread.start()
    time.sleep(2)
    TCP_thread.start()
    time.sleep(1)
    Client_thread.join()
    time.sleep(10)
    RunSer2Command(lines2[24])

rawDescription: Using multiple programs simultaneously in Python


rawDescription: pandas count true values in multi-index frame

description: Pandas count true values in multi-index frame


code:
from operator import itemgetter
{k:map(itemgetter(0), foo[k]) for k in foo}

rawDescription: Accessing nested tupels in dictionary values


code:
from matplotlib import pyplot as plt

fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.plot(x, y, 'ob', label='data')
ax.plot(x, A.dot(p), '-k', lw=2, label='linear fit')
ax.legend()

rawDescription: Scipy.optimization linear function approximation


rawDescription: Python: searching through html file grabbing <a> tags with the href and text content


code:
import mock

patcher = mock.patch.multiple(
    '__main__.MyClass',
    foo=mock.Mock(return_value='mocked foo!'),
    bar=mock.Mock(return_value='bar')
)

rawDescription: Using the Python mock module, how can I patch a class so that it stubs only the methods I want to stub and leaves other properties and methods alone?


code:
import numpy as np
from ast import literal_eval
pt1 = np.array(map(literal_eval,open("in.txt")))

rawDescription: Read several lists from the text file properly python


code:
cols = list('xacybz')
df = DataFrame(randn(10, len(cols)), columns=cols)
preordered = list('xyz')
new_order = preordered + list(df.columns - preordered)
new_order
df.reindex(columns=new_order)

rawDescription: how to sort only some of the columns in a data frame in pandas?

description: Sort only some of the columns in a data frame in pandas


rawDescription: Array building in Python

description: Array building in Python


code:
class Photo(models.Model):
    name = models.CharField(max_length=64)

class Album(models.Model):
    name = models.CharField(max_length=64)
    photoItems = models.ManyToManyField(Photo, related_name='albums')

rawDescription: Connect one model's ManyToManyField with another model's ForeignKey


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

rawDescription: Find adjacent elements in a 2D numpy grid


code:
class raquete(pygame.sprite.Sprite):

    def __init__(self, x, y, l_raquete, a_raquete):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((l_raquete, a_raquete))
        # I guess branco means color
        self.image.fill(branco)
        # no need for the x and y members,
        # since we store the position in self.rect already
        self.rect = self.image.get_rect(x=x, y=y)

rawDescription: Pygame - Getting a rectangle for a dynamically drawn object


code:
import itertools

def generate_combinations(thing, positive="valid", negative=None):

    """ Generate all possible combinations, walking and mimicking structure of "thing" """

    if isinstance(thing, dict):  # if dictionary, distinguish between two types of dictionary
        if positive in thing:
            return thing[positive] if negative is None else [thing[positive][0]] + thing[negative]
        else:
            results = []
            for key, value in thing.items():  # generate all possible key: value combinations
                subresults = []
                for result in generate_combinations(value, positive, negative):
                    subresults.append((key, result))
                results.append(subresults)
            return [dict(result) for result in itertools.product(*results)]

    elif isinstance(thing, list) or isinstance(thing, tuple):  # added tuple just to be safe
        results = []
        for element in thing:  # generate recursive result sets for each element of list
            for result in generate_combinations(element, positive, negative):
                results.append(result)
        return results

    else:  # not a type we know how to handle
        raise TypeError("Unexpected type")

def generate_invalid_combinations(thing):

    """ Generate all possible combinations and weed out the valid ones """

    valid = generate_combinations(thing)

    return [result for result in generate_combinations(thing, negative='negative') if result not in valid]

def generate_boundary_combinations(thing):

    """ Generate all possible boundary combinations """

    return generate_combinations(thing, positive="boundary")

def generate_explore_combinations(thing):

    """ Generate all possible explore combinations and weed out the valid ones """

    valid = generate_combinations(thing)

    return [result for result in generate_combinations(thing, negative='explore') if result not in valid]

rawDescription: Generate all combinations from a nested python dictionary and segregate them


code:
def compose(stuff):
    return stuff['dbtype'] + '@' + stuff['host']

temp = map(compose, stuffs)
result = reduce(lambda x, y: x + ', ' + y, temp)

rawDescription: how to join an django model object response

description: Join an django model object response


code: x.b = x.b.shift(n-1)

rawDescription: How to shift entire groups in pandas groupby

description: Shift entire groups in pandas groupby


code:
def on_models_committed(sender, changes):
    """Handler for model change signals"""
    for model, change in changes:
        if change == 'insert' and hasattr(model, '__commit_insert__'):
            model.__commit_insert__()
        if change == 'update' and hasattr(model, '__commit_update__'):
            model.__commit_update__()
        if change == 'delete' and hasattr(model, '__commit_delete__'):
            model.__commit_delete__()

rawDescription: SQLALchemy "after_insert" doesn't update target object fields


code:
column_names = ['col1', 'col2', 'col3']
columns = (Column(name, Integer) for name in column_names)
table = Table('table_name', metadata, *columns) # unpacks Column instances

rawDescription: Creating MySql Table from a python list

description: Creating MySql Table from a python list


code:
import itertools

def generate_arrangements(iterable, size=4):
    iterator = iter(iterable)
    while True:
        yield next(iterator)
        yield list(list(row) for row in itertools.islice(iterator, size))

rawDescription: Unpack a given number of items in Python?


code:
l = [{1:2},{2:3},{3:4}]

def func1(d):
        for k in d:
                d[k] += 1

def func2(l):
        for d in l:
                for k in d:
                        d[k] += 1

rawDescription: What is more effective iterate a list and call a function or pass the list to the function


code:
q = session.query(Base)
q = q.outerjoin(Mine)
q = q.filter(
    or_(Base.type == 'Base',
        Mine.id == 1))
result = q.all()

rawDescription: query on super class and inherited class

description: Query on super class and inherited class


code:
t=lambda x:eval(compile(x,'None','single'))
strs="print 'hello'"
t(strs)

rawDescription: Can I write a lambda function in python of more than one line, and call other functions?

description: Write a lambda function in python of more than one line, and call other functions


rawDescription: Python integer formatting

description: Python integer formatting


code:
class B(object):
    def __init___(self, clazz=A):
        self.__clazz = clazz
    def method(self):
        a = self.__clazz()
        a.test()

class C(B):
    def __init__(self):
        super(C, self).__init__(CustomA)

rawDescription: Python super override object name

description: Python super override object name


code:
l2_set = set(l2)
# or use
#   l2_set = {(13,'h'),(13,'d'),(14,'c'),(14,'s'),(14,'h')}
# directly

l1 = [hand for hand in l1 if not (hand[0] in l2_set or hand[1] in l2_set)]

rawDescription: Python, removing whole list if it contains a matching tuple


rawDescription: Python itertools.combinations' results

description: Python itertools.combinations' results


code:
import StringIO
content = 'big\nugly\ncontents\nof\nmultiple\npdf files'
buf = StringIO.StringIO(content)
buf.readline()
buf.readline()
buf.readline()
buf.readline()
buf.readline()
buf.readline()
buf.seek(0)
buf.readline()

rawDescription: How to read line by line in pdf file using PyPdf?

description: Read line by line in pdf file using PyPdf


code:
mobhelp_list = []
wifi.add(h1, out_mobilityhelper=mobhelp_list)
mobilityhelper = mobhelp_list[0]

rawDescription: How to add a variable of a method of a class in aonther program in python?

description: Add a variable of a method of a class in aonther program in python


code: pattern6 = re.compile(r"Time: .*$", flags=re.M)

rawDescription: Regex pattern to stop only accept what's left on that line


code:
from collection import defaultdict
grammar = {'AB':('S', 'B'), 'BB':'A', 'a':'A', 'b':'B'}
tmp_result = defaultdict(list)
def tuplify(val):
    if not isinstance(val, tuple):
        val = (val,)
    return val

for key, value in grammar.items():
    values = tuplify(value)
    for val in values:
        tmp_result[val].append(key)

tmp_result
result = {key:tuple(val) for key, val in tmp_result.items()}
result

rawDescription: How to read python dictionary and tuples

description: Read python dictionary and tuples


rawDescription: Python - sort lists based on their sum

description: Python - sort lists based on their sum


code:
for client in dict_list:
    currentClient = Label(text='Client: ' + client['Client']).grid(row=[i], column=1)
    ...

rawDescription: Checkbox to determine if an action is completed or not


rawDescription: Python regex does not search with group numbering


code:
def filter_factory(lookup_type):
    class ProductFilter(django_filters.FilterSet):
         price = django_filters.NumberFilter(lookup_type=lookup_type)

    return ProductFilter

rawDescription: Taking unrelated code out of views.py

description: Taking unrelated code out of views.py


code:
params = {
    "A":    ["A part of",      "#comment 1"],
    "B":    [1.2,              "#comment 2"],
    "C":    ["the test run.",  "#comment 3"]
}

rawDescription: Replace dictionary value in a separate file


code:
import pygame,sys,time,Blockm
from pygame.locals import *
black,white=(0,0,0),(255,255,255)
size=w,h=1400,800
screen=pygame.display.set_mode(size)
pygame.init()

class Block(object):

    sprite = None

    def __init__(self, x, y):
        if not Block.sprite:
            Block.sprite = pygame.image.load("Block.png").convert()
            # ^  MODIFIED: .convert_alpha() won't recognize the effects of .set_alpha(), so use regular .convert() instead.
        self.rect = Block.sprite.get_rect(top=y, left=x)
        self.ticks = 255 # NEW: Set a local tick count.
        # REMOVED `self.fade()`: This runs on main now.

    def fade(self):
        ## REPURPOSED METHOD: Runs each frame that the Block is active. Called on main loop.
        # MODIFIED BLOCK: Update local tick count before setting the class's sprite alpha.
        self.ticks -= 1
        Block.sprite.set_alpha(self.ticks) # MOVED, MODIFIED: uses local tick count for alpha.
        print(self.ticks) # UPDATED: Using local ticks.

blocks = [] # MOVED: Make a list for the Blocks, but don't clear it on frame.
while True:
    mmraw=pygame.mouse.get_pos()
    mmx,mmy=mmraw[0]-(pygame.image.load("Block.png").get_width())/2,mmraw[1]-(pygame.image.load("Block.png").get_width())/2
    # ^  There may be a tidier way of doing this. Described in solution body...
    for event in pygame.event.get():
        if event.type== pygame.QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            print('click')
            blocks.append(Block(mmx,mmy))

    screen.fill((22,22,222))
    # ^  NEW: Fill the screen with some backdrop so that the erasure is obvious!
    #         If you don't do this, you'll be blitting faded images over brighter ones and there will be no real change!!
    for block in blocks:
        block.fade()    # NEW: Update this block.
        if block.ticks < 1: # NEW BLOCK: If the block has become invisible...
            blocks.remove(block) # NEW: Expunge this invisible block.
            continue # NEW: Immediately move on to the next block.
        screen.blit(Block.sprite, block.rect)
        print('trying to blit')
        print(Block.sprite.get_offset())
    pygame.display.update()

rawDescription: How to use a while loop within a class in pygame?

description: Use a while loop within a class in pygame


code:
#!/usr/bin/env python3
from datetime import datetime
import pytz # $ pip install pytz

pacific = pytz.timezone('America/Los_Angeles')
naive  = datetime(2015, 2, 2)
pacific_dt = pacific.localize(naive, is_dst=None)
print(pacific_dt.timestamp())

utc_dt = naive.replace(tzinfo=pytz.utc)
print(utc_dt.timestamp())

print(naive.timestamp()) #NOTE: interpret as local time

rawDescription: Is time.mktime timezone free in python?

description: Is time.mktime timezone free in python


code:
from itertools import chain, zip_longest
def lister(l1, l2, l3):
    print('List1   List2   List3')
    for a,b,c in zip_longest(chain(*l1), chain(*l2), chain(*l3), fillvalue=''):
       print('{:7s} {:7s} {:7s}'.format(str(a),str(b),str(c)))

rawDescription: How do you make a table out of multiple list in list?

description: Make a table out of multiple list in list


code:
a[:, 1]  # get the entire 2nd (index 1) column
a[1:3, -1]  # get the second and third elements from the last column
a[1:3, 1:3]  # get the data in the second and third rows and columns

rawDescription: Numpy array slicing using commas

description: Numpy array slicing using commas


rawDescription: Summing similar elements within a tuple-of-tuples


code:
# create a false product
twodaysinthefuture = datetime.datetime.now() + datetime.timedelta(hours=48)
p1 = Product(name="Product X")
p2 = Product(name = "Product Y")
p3 = Product(name = "Product Z")
x1 = Product(name = "Product older than 24 hours 1", last_time_parsed=twodaysinthefuture)
x2 = Product(name = "Product older than 24 hours 2", last_time_parsed=twodaysinthefuture)
x3 = Product(name = "Product older than 24 hours 3", last_time_parsed=twodaysinthefuture)

rawDescription: SqlAlchemy filter by time difference

description: SqlAlchemy filter by time difference


code: counts = np.bincount(id[mask1]-1)

rawDescription: Summation of only consecutive values in a python array


code:
class A_meta(type):
    def __init__(cls, *args):
        type.__init__(cls, *args)
        if hasattr(cls, 'VAL'):
            cls.big_attr = heavy_func(cls.VAL)

class A(object):
    __metaclass__ = A_meta

class B(A):
        VAL = 'b'

class C(A):
        VAL = 'c'

rawDescription: pythonic way of computing a value to a class object only once

description: Pythonic way of computing a value to a class object only once


code:
dist = np.minimum(np.arange(100), np.arange(100, 0, -1))
dist *= dist
dist_3d = np.sqrt(dist[:, None, None] + dist[:, None] + dist)
distances, _ = np.unique(dist_3d, return_inverse=True)
values = np.bincount(_, weights=space_ac.ravel()) / np.bincount(_)

plt.plot(distances[1:], values[1:])

rawDescription: Power Spectrum and Autocorrelation of Data in Numpy


code:
from timeit import timeit

def select_items_old(temp_dict, n):
  res = []
  sort_keys = sorted(temp_dict.keys())
  count = 0
  for key in sort_keys:
    for pair in temp_dict[key]:
      if count < n:
        res.append(pair)
        count += 1
      else:
        return res
  return res

def select_items_new(data, limit):
    count = 0
    result = []
    extend = result.extend
    for key in sorted(data.keys()):
        value = data[key]
        extend(value)
        count += len(value)
        if count >= limit:
            break
    return result[:limit]

data = {x:range(10) for x in range(1000)}

def compare(*args):
    number = 1000
    for func in args:
        name = func.__name__
        print ('test: %s(data, 12): %r' % (name, func(data, 12)))
        code = '%s(data, %d)' % (name, 300)
        duration = timeit(
            code, 'from __main__ import %s, data' % name, number=number)
        print ('time: %s: %.2f usec/pass\n' % (code, 1000000 * duration/number))

compare(select_items_old, select_items_new)

rawDescription: Python 2.6 optimize nested loops

description: Python 2.6 optimize nested loops


code:
from pyspark.sql.functions import broadcast

like_with_tokenizer_and_bd = (broadcast(tokenized)
    .join(keywords_df, col("token") == col("keyword"))
    .drop("token"))

like_with_tokenizer.explain()

## TungstenProject [document_id#3L,keyword#5]
##  BroadcastHashJoin [token#10], [keyword#5], BuildLeft
##   TungstenProject [document_id#3L,token#10]
##    !Generate explode(words#8), true, false, ...
##     ConvertToSafe
##      TungstenProject [document_id#3L,UDF(document_text#4) AS words#8]
##       Scan PhysicalRDD[document_id#3L,document_text#4]
##   ConvertToUnsafe
##    Scan PhysicalRDD[keyword#5]

rawDescription: How can we JOIN two Spark SQL dataframes using a SQL-esque "LIKE" criterion?


rawDescription: Regex unicode in python 2.x vs 3.x

description: Regex unicode in python 2.x vs 3.x


code:
df2 = pd.get_dummies(df.set_index('ID')['DATE'])
df2.reset_index().groupby('ID').sum()

rawDescription: Counting Occurrences Across Dataframe Rows


code:
'scipy.special._ufuncs_cxx'
'scipy.linalg.cython_blas'
'scipy.linalg.cython_lapack'
'scipy.integrate'
'scipy.integrate.quadrature'
'scipy.integrate.odepack'
'scipy.integrate._odepack'
'scipy.integrate.quadpack'
'scipy.integrate._quadpack'
'scipy.integrate._ode'
'scipy.integrate.vode'
'scipy.integrate._dop'
'scipy.integrate.lsoda'

rawDescription: How to use pyinstaller with hidden imports for scipy.optimize leastsq

description: Use pyinstaller with hidden imports for scipy.optimize leastsq


code:
import json
with open('Database.txt') as f:
    print(json.load(f)['user2123'])

rawDescription: How to read a dictionary from a file in Python 2.7?

description: Read a dictionary from a file in Python 2.7


rawDescription: Write a simple list into a new CSV file

description: Write a simple list into a new CSV file


code:
def gen_group(L):
    DELIMETER = "A"
    out = [DELIMETER]
    while L:
        for ind, elem in enumerate(L[1:]):
            if elem == DELIMETER :
                break
            else:
                out.append(elem)

        for i in range(ind + 1):
            L.pop(0)

        yield out
        out = [DELIMETER ]

rawDescription: How can I group a list of objects by continuity?

description: Group a list of objects by continuity


code: lst = [[] for _ in xrange(a)]

rawDescription: How to create a number of empty nested lists in python

description: Create a number of empty nested lists in python


code:
import constellations

for key, const in constellations.list_of_constellations.items():
    setattr(constellations, constellation[0], v)  # set "And" to 0, etc.

rawDescription: Runtime injection of attributes into module namespace


code:
def fill_from_left(a, x=0):
    to_fill = (a == x)
    if a[0] == x:
        raise ValueError("cannot have {} as first element".format(x))
    if to_fill.any():
        lefts = ~to_fill & (np.roll(a, -1) == x)
        fill_from = lefts.cumsum()
        fill_with = a[np.where(lefts)[0]][fill_from - 1]
        a[to_fill] = fill_with[to_fill]

rawDescription: In Python and numpy, How do I replace missing values in an array with the previous element? (Masked array?)


code: re.search(r'\{cite web(.*?)([\r\n]+)(.*?)\}\}', inputtext, flags=re.DOTALL)

rawDescription: Python regex on wikitext template

description: Python regex on wikitext template


code:
# load modules
import time

from PySide import QtCore, QtGui

# APPLICATION STUFF
# -----------------

APP = QtGui.QApplication([])

# THREADS
# -------

class WorkerThread(QtCore.QThread):
    '''Does the work'''

    def __init__(self):
        super(WorkerThread, self).__init__()

        self.running = True

    def run(self):
        '''This starts the thread on the start() call'''

        # this goes over 1000 numbers, at 10 a second, will take
        # 100 seconds to complete, over a minute
        for i in range(1000):
            print(i)
            time.sleep(0.1)

        self.running = False

class BackgroundThread(QtCore.QThread):
    '''Keeps the main loop responsive'''

    def __init__(self, worker):
        super(BackgroundThread, self).__init__()

        self.worker = worker

    def run(self):
        '''This starts the thread on the start() call'''

        while self.worker.running:
            APP.processEvents()
            print("Updating the main loop")
            time.sleep(0.1)

# MAIN
# ----

def main():
    # make threads
    worker = WorkerThread()
    background = BackgroundThread(worker)

    # start the threads
    worker.start()
    background.start()
    # wait until done
    worker.wait()

if __name__ == '__main__':
    main()

rawDescription: How to make QtGui window process events whenever it is brought forward on the screen?

description: Make QtGui window process events whenever it is brought forward on the screen


code:
10, 20 if True else 10
10, 20 if False else 10
(10, 20) if False else 10
(10, 20) if True else 10
10, (20 if False else 10)
10, (20 if True else 10)

rawDescription: Unexpected python function return behavior


code:
class Demo:
     STARTED, LOADING, ACTIVE, ENDED = range(4)

rawDescription: python define a state object

description: Python define a state object


code: sorted(alarm_list, key=lambda alarm: getattr(alarm, 'severity'))

rawDescription: Custom sort by property

description: Custom sort by property


code:
dicts = [
 {'point': '-2.43896484341047, 53.4369463378926',
  'time': '2015-06-17 12:51:16+01:00',
  'title': 'Queen and Royal Family Members Visit Manchester'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-20 12:52:29+01:00',
  'title': 'Price Harry Quits the Army to Concentrate on Charity Work'},
 {'point': '-0.150032043436181, 51.5402430395087',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'},
 {'point': '-77.1075439345789, 35.456615048032',
  'time': '2015-06-26 17:01:19+01:00',
  'title': 'A true friend: Ukraine president asks Tony Blair to take on advisory role'}]

ans = []
for time, title in set((d['time'], d['title']) for d in dicts):
    points = [d['point'] for d in dicts if (d['time'], d['title']) == (time, title)]
    ans.append({
        'point' : points if len(points) > 1 else points[0],
        'time'  : time,
        'title' : title })

rawDescription: Merge dictionaries with the same values to a single dictionary


code:
b = bytearray('abc0efg')
b[3] = 'd'
b

rawDescription: Alternative to python string item assignment


code:
a = itertools.starmap(lambda x,y: x+y, zip(itertools.repeat(x), y))
a = list(a)

rawDescription: map lambda x,y with a constant x

description: Map lambda x,y with a constant x


code:
from collections import Counter

sources = []
for edge in edges:
    source.append(edge['source'])
    source.append(edge['target'])

sources_count = Counter(sources)
sources_count = sorted(source_count.items(), key=lambda s: s[1], reverse=True)

sources_count.pop(2)
valid_nodes = filter(lambda s: s[1] <= 5, sources_count)

link_list = filter(
    lambda e: e['source'] not in valid_nodes and e['target'] not in valid_nodes,
    edges
)

rawDescription: removing number of edges in python

description: Removing number of edges in python


code:
Modifiers
sxg

rawDescription: Pythonic Regular Expression in a large text file for a tabular data


code:
[[u'wasnt', u'right', u'part', u'say', u'things', u'shes',
  u'hurt', u'terribly', u'shes', u'speaking']]

rawDescription: Why is re not working on my file?

description: Why is re not working on my file


code:
import shelve

name = input("What's your name? ")
score = int(input("What's your high score? "))

with shelve.open("bin-data") as b:
    b[name] = score # b is dict-like

rawDescription: Deleting File Lines in Python

description: Deleting File Lines in Python


rawDescription: Finding Date difference in list of dates [Pandas]


rawDescription: Python - how to write strings to file without quotes and spaces?


rawDescription: Method to ravel an object numpy array

description: Method to ravel an object numpy array


code:
def process(df):
    L = []
    for index, row in table.iterrows():
        L += [Toy(row) for _ in range(row['number'])]
    return L

table = pd.DataFrame({
    'type': ['a', 'b', 'c']*10,
    'number': [5000, 4000, 30000]*10
    })

p = mp.Pool(processes=8)
split_dfs = np.array_split(table,8)
pool_results = p.map(process, split_dfs)
p.close()
p.join()

# merging parts processed by different processes
result = [a for L in pool_results for a in L]

rawDescription: Parallel python iteration

description: Parallel python iteration


code:
def parse_product(self, response):
    loader = FlipkartItemLoader(response=response)
    loader.add_value('pid', 'value of pid')
    loader.add_xpath('name', 'xpath to name')
    yield loader.load_item()

    # CrawlSpider defines this method to return all scraped urls.
    yield from self.parse(response)

rawDescription: Multiple inheritance in scrapy spiders

description: Multiple inheritance in scrapy spiders


rawDescription: python multidimensional list.. how to grab one dimension?

description: Python multidimensional list.. how to grab one dimension


code:
from sklearn.externals import joblib
joblib.dump(clf, 'my_model.pkl', compress=9)

rawDescription: Python scikit-learn: exporting trained classifier


rawDescription: Python modulo result differs from wolfram alpha?


code: id_produto = str(ID+'[!0-9]*')

rawDescription: Python Find script

description: Python Find script


code:
def linear_merge(*args):
  return list(iter_linear_merge(*args))

rawDescription: Question on a solution from Google python class day


rawDescription: How to turn this simple 5 bytes back into 4 bytes ? (algorithm to convert 4 bytes into 5 bytes is known)

description: Turn this simple 5 bytes back into 4 bytes ? (algorithm to convert 4 bytes into 5 bytes is known)


code: variable =  1 if not True else 2

rawDescription: Python inline if when setting variable

description: Python inline if when setting variable


code: listOfCRs = []

rawDescription: Identify and extract number from text files in Python


code:
example = [[1, 1, 20], [1, 1, 11], [2, 3, 12.1], [2, 5, 13], [5,4,10], [3,6,15]]
x = [el[0] for el in example]
y = [el[1] for el in example]
z = [el[2] for el in example]

# indices for x,y to get them in sorted order later
# duplicates in both dimensions are preserved
x_idx = sorted(range(len(x)), key=lambda k:x[k])
y_idx = sorted(range(len(y)), key=lambda k:y[k])

# initialize A with 0
A = [[0 for _ in range(len(y)+1)] for _ in range(len(x)+1)]

# and fill it with values
for k, val in enumerate(z):
    A[x_idx[k]+1][y_idx[k]+1] = val
    A[k+1][0] = x[x_idx[k]]
    A[0][k+1] = y[y_idx[k]]

rawDescription: python sorting array of XYZ coordinates in table matrix

description: Python sorting array of XYZ coordinates in table matrix


code: curve_fit(user_func, x, y, [initial_guess_param1, param2, ...])

rawDescription: Python: Passing function with arguments to a function


code:
urlpatterns = patterns('',
    ('^$', 'django.views.generic.simple.redirect_to', {'url': '/first-page'}),
    ...
)

rawDescription: django redirecting '/' to a flat page

description: Django redirecting '/' to a flat page


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

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


code:
date.today()
for i in range(13):
    print(months_ago(i))

rawDescription: Python 2.7 Get month from 2 months ago (i.e get '05' from todays date)


code:
def num_spaths(G):
    n_spaths = dict.fromkeys(G, 0.0)
    spaths = nx.all_pairs_shortest_path(G)

    for source in G:
        for path in spaths[source].values():
            for node in path[1:]: # ignore firs element (source == node)
                n_spaths[node] += 1 # this path passes through `node`

    return n_spaths

rawDescription: Python: how to compute the number of shortest paths passing through one node?


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

plt.ion()

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

for i,(_,_,idx) in enumerate(local_minima):
    dat = dst_data[idx-24:idx+25]
    dates,values = zip(*dat)
    if i == 0:
        assert(len(dates) == len(values))
        lines2d, = ax.plot_date(mdate.date2num(dates), np.array(values), linestyle='-')
    else:
        assert(len(dates) == len(values))
        lines2d.set_ydata(np.array(values))
        lines2d.set_xdata(mdate.date2num(dates))  #This line causes problems.
        ax.relim()
        ax.autoscale_view(True,True,True)
        fig.canvas.draw()
    raw_input()

rawDescription: update matplotlib plot

description: Update matplotlib plot


code:
@validate(0, int)
@validate(1, str, logMessage='second argument must respond to str()')
@validate(2, customValidationFunction)
def some_func(int_arg, str_arg, other_arg):
    # the control gets here only after args are validated correctly
    return int_arg * str_arg

rawDescription: Python input validity and using asserts

description: Python input validity and using asserts


code:
import operator
reduce(operator.add, lis, '')

rawDescription: How To Add List Elements and Convert Them To A String


code:
def create_session():
  config = tf.ConfigProto(log_device_placement=True)
  config.gpu_options.per_process_gpu_memory_fraction=0.3 # don't hog all vRAM
  config.operation_timeout_in_ms=60000   # terminate on long hangs
  # create interactive session to register a default session
  sess = tf.InteractiveSession("", config=config)
  return sess

rawDescription: How to *actually* read CSV data in TensorFlow?

description: *actually* read CSV data in TensorFlow


code:
vocab = ["foo", "bar", "baz"]
s = "foo bar baz bar quux foo bla bla"

wordcount = dict((x,0) for x in vocab)
for w in re.findall(r"\w+", s):
    if w in wordcount:
        wordcount[w] += 1

rawDescription: Count occurrences of a couple of specific words


code:
sr, x = wavfile.read("input.wav")
x = x / np.abs(x).max() # x scale between -1 and 1

x2 = limiter(x)
x2 = np.int16(x2 * 32767)
wavfile.write("output_limit.wav", sr, x2)

x3 = arctan_compressor(x)
x3 = np.int16(x3 * 32767)
wavfile.write("output_comp.wav", sr, x3)

rawDescription: How to amplify sounds without distortion in Python

description: Amplify sounds without distortion in Python


code:
cog = []
for i in range(20201, 20705):
    cog.append('{"money": str(i), "last": "0.001"}')
params = tuple(cog)
...
p_c['params'] = json.dumps(params)

rawDescription: Convert curl command into python code

description: Convert curl command into python code


rawDescription: What are the languages available for graphlab stopwords?


code:
from os.path import normpath
from posixpath import normpath as normpath2
normpath is normpath2

rawDescription: Inspect import path string from object

description: Inspect import path string from object


code:
from tkinter import *
from tkinter.ttk import *
import time
import threading

class Interface:
    def __init__(self, master):
        self.master = master
        self.browse_button= Button (master, text="Browse", command=self.browser)
        self.browse_button.pack()
        # Create an indeterminate progressbar here but don't pack it.
        # Change the maximum to change speed. Smaller == faster.
        self.progressbar = Progressbar(mode="indeterminate", maximum=20)

    def browser (self):
        # set up thread to do work in
        self.thread = threading.Thread(target=self.read_file, args=("filename",))
        # disable the button
        self.browse_button.config(state="disabled")
        # show the progress bar
        self.progressbar.pack()
        # change the cursor
        self.master.config(cursor="wait")
        # force Tk to update
        self.master.update()

        # start the thread and progress bar
        self.thread.start()
        self.progressbar.start()
        # check in 50 milliseconds if the thread has finished
        self.master.after(50, self.check_completed)

    def check_completed(self):
        if self.thread.is_alive():
            # if the thread is still alive check again in 50 milliseconds
            self.master.after(50, self.check_completed)
        else:
            # if thread has finished stop and reset everything
            self.progressbar.stop()
            self.progressbar.pack_forget()
            self.browse_button.config(state="enabled")
            self.master.config(cursor="")
            self.master.update()

            # Call method to do rest of work, like displaying the info.
            self.display_file()

    def read_file (self, filename):
        time.sleep(7)  # actually do the read here

    def display_file(self):
        pass  # actually display the info here

window = Tk()
starter = Interface(window)
window.mainloop()

rawDescription: Blocking Tkinter interface until thread finishes its task


code:
with open('log.txt') as f:
    dico = dict()
    total_value = 0
    for line in f:
        #Feeding the dictionary
        split_array = line.split()
        possible_key = split_array[0]
        value = int(split_array[1])

        #If url has already been processed, update the value
        #Else, initialize the entry in the dictionary => default=0
        dico[possible_key] = dico.get(possible_key, default=0) + value

        #Updating the global sum
        total_value = total_value + value

rawDescription: python log parse create dict of repetitive url and size with count

description: Python log parse create dict of repetitive url and size with count


rawDescription: sort integers positive then negative

description: Sort integers positive then negative


code: return len(L) < 2 or (L[0] <= L[1] and isSorted(L[1:]))

rawDescription: check if sorted using recursion in python

description: Check if sorted using recursion in python


rawDescription: Python regex: finding sequence of chars inside a string


code:
class ServerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Server

class DatacenterSerializer(serializers.ModelSerializer):
    servers = ServerSerializer(many=True)
    class Meta:
        model = Datacenter
        fields = ('id', 'name','status','servers')

rawDescription: Clean way to implement views or methods with Django Rest Framework


code: scale = [notes[(y+notes.index(root))%len(notes)] for y in [0,2,4,5,7,9,11]]

rawDescription: python looping index

description: Python looping index


rawDescription: Reshaping/Pivoting data in Spark RDD and/or Spark DataFrames


rawDescription: counting values and adding reference in a new dataframe column

description: Counting values and adding reference in a new dataframe column


code:
if counter == 100:
    break

rawDescription: Python - Save user inputs in new variables


rawDescription: Plotting surface of implicitly defined volume


code:
import glob
import re
import os
patt = re.compile(r"File \d+\.csv")
for f in os.listdir("."):
    if patt.match(f):
        print(f)

rawDescription: Finding files that have a numeric suffix in Python


code:
f = open('test.txt','r')

string = f.read()
words = string.split('\t')

desired = [words[ind+1] for ind, word in enumerate(words) if word == "seven"]

rawDescription: Scan through txt, append certain data to an empty list in Python


code: path = json.loads(path_string)

rawDescription: How to Create a Dynamic Path for Grabbing Variables from JSON in Python


rawDescription: Double loops for "concatenation of subarrays in bigger array"


code:
CONTENT_TYPES = {
  'exposed-contenttype': 'app_label.model'
}

class ContentTypeField(...):
    def from_native(self, data):
        if data not in CONTENT_TYPES:
            raise serializers.ValidationError(...)
        app_label, model = CONTENT_TYPES[data].split('.')
        # ...

rawDescription: set contenttype by name in generic relation in django rest framework

description: Set contenttype by name in generic relation in django rest framework


code:
from collections import defaultdict

d = defaultdict(list)

with open("test.txt") as fin:
    for line in fin:
        if ":" in line:
            k, v = line.rstrip().split(":")
            d[k].extend(map(str.strip,v.split(","))  if v.strip() else [])
        else:
            d[k].append(line.rstrip())
    print(d)

rawDescription: Convert text file to dictionary with one key and multiple values


code:
import timeit

setup = '''
import numpy, pandas
df = pandas.DataFrame(numpy.zeros(shape=[10, 1000]))
dictionary = df.to_dict()
'''

# f = ['value = dictionary[5][5]', 'value = df.loc[5, 5]', 'value = df.iloc[5, 5]']
f = ['value = [val[5] for col,val in dictionary.items()]', 'value = df.loc[5]', 'value = df.iloc[5]']

for func in f:
    print(func)
    print(min(timeit.Timer(func, setup).repeat(3, 100000)))

rawDescription: Pandas DataFrame performance

description: Pandas DataFrame performance


code:
class MyExecutor(Executor):
    def before_execute(self):
        result = super(MyExecutor, self).before_execute()
        return result and self.my_other_function()

rawDescription: Call functions that have an attribute/characteristic


code:
userInput = raw_input("Enter Something: ")
testDict = Counter(userInput.split(" "))
testDict.most_common()

rawDescription: Determining the most common word from a user's input. [Python]


code:
data = 'AAACTGACACCATCGATCAGAACCTGA'
head, sep, tail = data.partition('ACTGAC')
tail[:10]

rawDescription: Finding various string repeats in python in next 10 characters


code:
0               [love, car]
1           [view, amazing]
2    [feel, great, morning]
3        [excited, concert]
4            [best, friend]

rawDescription: Python remove stop words from pandas dataframe


code: plt.savefig('my_fig.png', dpi=my_dpi * 10)

rawDescription: Specifying and saving a figure with exact size in pixels


code: f_sorted = sorted((first, (first,second)) for first,second in sa)

rawDescription: Python: how to search for a substring in a set the fast way?


rawDescription: From 1D graph to 2D mask

description: From 1D graph to 2D mask


code:
import numpy as np

d = 5
hpoints = np.random.normal(size=(100, d))
bits = np.zeros((2, 100))
bits[0, :50] = 2.0 ** np.arange(50)
bits[1, 50:] = 2.0 ** np.arange(50)
N = 100000
uniques = set()
for i in xrange(0, N, 1000):
    tpoints = np.random.normal(size=(1000, d))
    signatures = np.inner(np.inner(tpoints, hpoints) < 0.0, bits)
    uniques.update(map(tuple, signatures))
print(len(uniques))

rawDescription: A fast way to count non-empty regions

description: A fast way to count non-empty regions


rawDescription: pandas save date in ISO format?

description: Pandas save date in ISO format


code:
from subprocess import Popen

cmd = 'vlc.exe --started-from-file --playlist-enqueue "2.wmv"'
p = Popen(cmd) # start and forget
assert not p.poll() # assert that it is started successfully

rawDescription: How to add to VLC playlist queue using Python subprocess

description: Add to VLC playlist queue using Python subprocess