code:
class Emitter(object):
    def __init__(self, pos=(0, 0)):
        self.particles = []
        self.pos = pos
        self.factories = []

    def add_factory(self, factory, pre_fill=300):
        self.factories.append(factory)
        tmp = []
        for _ in xrange(pre_fill):
            n = next(factory)
            tmp.extend(n)
            for p in tmp:
                p.move()
        self.particles.extend(tmp)

    def update(self):
        for f in self.factories:
            self.particles.extend(next(f))

        for p in self.particles[:]:
            p.move()
            if p.alive == -1:
                self.particles.remove(p)

    def draw(self, screen, position_translater_func):
        for p in self.particles:
            target_pos = position_translater_func(map(sum, zip((p.x, p.y), self.pos)))
            pygame.draw.circle(screen, p.col, target_pos, int(p.size))

rawDescription: How to implement a particle engine

description: Implement a particle engine


code:
def test_iter(A, B):
    m,n1 = A.shape
    n2 = B.shape[1]
    Cshape = (m, n1*n2)
    data = np.empty((m,),dtype=object)
    col =  np.empty((m,),dtype=object)
    row =  np.empty((m,),dtype=object)
    for i,(a,b) in enumerate(zip(A, B)):
        data[i] = np.outer(a.data, b.data).flatten()
        #col1 = a.indices * np.arange(1,a.nnz+1) # wrong when a isn't dense
        col1 = a.indices * n2   # correction
        col[i] = (col1[:,None]+b.indices).flatten()
        row[i] = np.full((a.nnz*b.nnz,), i)
    data = np.concatenate(data)
    col = np.concatenate(col)
    row = np.concatenate(row)
    return sparse.coo_matrix((data,(row,col)),shape=Cshape)

rawDescription: Special kind of row-by-row multiplication of 2 sparse matrices in Python


code: list(s)

rawDescription: Get list of all second-order keys in nested dictionary


code:
short_data = {}
for key, value in data.items():
    short_key = (key[0], key[1], key[3])
    if short_key in short_data:
        short_data[short_key].extend(value)
    else:
        short_data[short_key] = value

rawDescription: Python Dictionary Trouble: Grouping by elements in tuple key


code:
def foo(x):
 return x or 8
foo(4)
foo([])
foo(0)

rawDescription: How can i omit the redundant words in python condition loop

description: Omit the redundant words in python condition loop


code:
def corect_change(nickles, pennies, summ):
    for i in xrange(nickles):
        if summ - (i * 5) < 5:
            break
    for j in xrange(pennies):
        if summ == (i * 5) + j:
            break
    return summ == (i * 5) + j

rawDescription: Change calculator nickels pennies

description: Change calculator nickels pennies


code:
from functools import reduce
from operator import or_
reduce(or_, [x, y, z])

rawDescription: How do I use the union and intersection on a list of Counters of unknown length?

description: Use the union and intersection on a list of Counters of unknown length


code:
def homeaway(self):
    MONTH_COUNT = 6
    display = Display(visible=0, size=(1024, 768))
    display.start()
    driver = webdriver.Firefox()
    driver.maximize_window()

    wait = WebDriverWait(driver, 10)

    url = 'https://www.homeaway.pt/arrendamento-ferias/p1418427a?uni_id=1590648'
    driver.get(url)

    count = 0
    for month in range(MONTH_COUNT):
        # pick start date
        start_date = wait.until(EC.visibility_of_element_located((
            By.CSS_SELECTOR,
            ".quotebar-container input[name=startDateInput]")))
        start_date.click()

        for x in range(count):
            driver.execute_script('$( "a.ui-datepicker-next" ).click()')

        current_month = driver.find_element_by_css_selector(
            ".ui-datepicker-month").text

        print("current_month:", current_month)

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))

        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # pick end date (TODO: violates DRY principle, refactor!)
        end_date = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR,
             ".quotebar-container input[name=endDateInput]")))

        end_date.click()

        first_available_date = wait.until(EC.element_to_be_clickable(
            (By.CSS_SELECTOR, "#ui-datepicker-div td.full-changeover > a")))

        ActionChains(driver).move_to_element(first_available_date).perform()
        driver.find_element_by_css_selector(
            "#ui-datepicker-div td.full-selected.full-changeover > a").click()

        # get the calculated price
        price = wait.until(EC.visibility_of_element_located(
            (By.CSS_SELECTOR, ".price-quote .price-total")))

        print(price.text.encode('ascii', 'ignore'))

        driver.execute_script('$( "button.ui-datepicker-clear" ).click()')

        count += 1

    driver.close()

rawDescription: Getting availability from datepicker

description: Getting availability from datepicker


rawDescription: What is an alternative to execfile in Python 3.0?


code:
# test values for the bw_method option ('None' is the default value)
bw_values =  [None, 0.1, 0.01]

# generate a list of kde estimators for each bw
kde = [scipy.stats.gaussian_kde(data,bw_method=bw) for bw in bw_values]

# plot (normalized) histogram of the data
import matplotlib.pyplot as plt
plt.hist(data, 50, normed=1, facecolor='green', alpha=0.5);

# plot density estimates
t_range = np.linspace(-2,8,200)
for i, bw in enumerate(bw_values):
    plt.plot(t_range,kde[i](t_range),lw=2, label='bw = '+str(bw))
plt.xlim(-1,6)
plt.legend(loc='best')

rawDescription: how to plot Probability density Function (PDF) of inter-arrival time of events?

description: Plot Probability density Function (PDF) of inter-arrival time of events


code:
class C(object):
    a = 17

    @staticmethod
    def b(x):
      return x+1

rawDescription: Can't have a function as a class attribute in Python


code:
response = self.connection.retr(i+1)
raw_message = response[1]

rawDescription: Save email attachment (python3, pop3_ssl, gmail)


code:
def find_image(im, tpl):
    im = np.atleast_3d(im)
    tpl = np.atleast_3d(tpl)
    H, W, D = im.shape[:3]
    h, w = tpl.shape[:2]

    # Integral image and template sum per channel
    sat = im.cumsum(1).cumsum(0)
    tplsum = np.array([tpl[:, :, i].sum() for i in range(D)])

    # Calculate lookup table for all the possible windows
    iA, iB, iC, iD = sat[:-h, :-w], sat[:-h, w:], sat[h:, :-w], sat[h:, w:]
    lookup = iD - iB - iC + iA
    # Possible matches
    possible_match = np.where(np.logical_and(*[lookup[..., i] == tplsum[i] for i in range(D)]))

    # Find exact match
    for y, x in zip(*possible_match):
        if np.all(im[y+1:y+h+1, x+1:x+w+1] == tpl):
            return (y+1, x+1)

    raise Exception("Image not found")

rawDescription: Determine if an image exists within a larger image, and if so, find it, using Python


rawDescription: Slicing strings of a sentence with a scale


code: divisibleBySeven = [num for num in inputList if num != 0 and num % 7 == 0]

rawDescription: Return Subset of List that Matches Condition


code:
from itertools import imap
sum(imap(f, xlist))

rawDescription: Quickest way to sum(f(x)) in Python

description: Quickest way to sum(f(x)) in Python


code:
re.search(r'(TIMES\d)', line).groups()
re.findall(r'TIMES\d', line)

rawDescription: Extracting a specific PIECE of a line from a textfile(Python)


code:
#set up frame
df =pd.DataFrame({'Team Name':['x','y'], 'Nb of teammates':[2,1], 'Team Leader One':['Joe','Jack'],'Team Leader Two':['Joey',None]})

rawDescription: Python Pandas inserting new rows with colums values


code:
with file("...", 'r') as f:
    g = generate_tokens(f.readline())

rawDescription: USing a Scanner Import

description: USing a Scanner Import


code: print("Ahoy Captain ", pirateInput)

rawDescription: Checking if correct value is outputed Python unit test


code: [x for x in words if x not in curses]

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


code: vars()

rawDescription: naming collision in namespaces in modules of python

description: Naming collision in namespaces in modules of python


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

rawDescription: Python: Comparing all elements of two arrays and modifying 2nd array


code:
# top level menu
menu1 = wx.Menu()
menu1.Append(11,"11")
menu1.Append(12, "12")

# sub menu 1
menu2 = wx.Menu()
menu2.Append(131, "131")
menu2.Append(132, "132")
menu1.AppendMenu(13,"13",menu2)

# sub menu 2
menu3 = wx.Menu()
menu3.Append(1321,"1321")
menu3.Append(1322,"1322")
menu3.Append(1323,"1323")
menu2.AppendMenu(132, "132", menu3)

# add top to menubar
menubar.Append(menu1, "&Top")

rawDescription: Figure out nested submenu selections in wxPython?


rawDescription: extracting IP values

description: Extracting IP values


rawDescription: python beautifulsoup4 parsing google finance data

description: Python beautifulsoup4 parsing google finance data


code:
d.search(*d.sample_search(1,1,1,5))
d.search(*d.sample_search(1,1,1,1))
d.search(*d.sample_search(10,None,1,1,1,1))
d.search(*d.sample_search(10,None,1,1,1,1))
d.search(*d.sample_search(10,None,1,1,1,1))
import timeit
timeit.Timer('d.search(*d.sample_search(10,None,1,1,1,1))','from __main__ import d').timeit(100)

rawDescription: What's the fastest way to filter a table of integers in Python?


code:
class UserRegistrationForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('first_name', 'last_name', 'username', 'password1', 'password2')

class RegistrationFormTeacher(forms.ModelForm):
    class Meta:
        model = Teacher
        fields = (modules, confirmed)

rawDescription: Django, register extended user model

description: Django, register extended user model


rawDescription: NumPy slice notation in a dictionary

description: NumPy slice notation in a dictionary


rawDescription: python pandas - creating a column which keeps a running count of consecutive values

description: Python pandas - creating a column which keeps a running count of consecutive values


rawDescription: Extract sequences from a FASTA file to multiple files, file based on header_IDs in a separate file


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

cmap = plt.get_cmap('jet')
names = ["bob", "joe", "andrew", "pete"]
colors = cmap(np.linspace(0, 1, len(names)))
print(colors)
# [[ 0.          0.          0.5         1.        ]
#  [ 0.          0.83333333  1.          1.        ]
#  [ 1.          0.90123457  0.          1.        ]
#  [ 0.5         0.          0.          1.        ]]

x = np.linspace(0, np.pi*2, 100)
for i, (name, color) in enumerate(zip(names, colors), 1):
    plt.plot(x, np.sin(x)/i, label=name, c=color)
plt.legend()
plt.show()

rawDescription: Elegant way to match a string to a random color matplotlib


code: anded = b[selectedcolumns].apply(lambda x, y: x*y, args=[b['J']])

rawDescription: Efficient boolean operation of a pandas series object on a pandas dataframe object


rawDescription: How do I escape closing '/' in HTML tags in JSON with Python?

description: Escape closing '/' in HTML tags in JSON with Python


code:
mask = int(pattern.replace('0', '1').replace('?', '0'), 2)
test = int(pattern.replace('?', '0'), 2)

def is_match(n):
    return (n&mask)==test

rawDescription: Find the perfect square in the string

description: Find the perfect square in the string


code: soup.select("div.container a[href]")

rawDescription: Navigation with BeautifulSoup

description: Navigation with BeautifulSoup


code:
df.reindex(ind & ind2).join(df2.reindex(ind & ind2))  # equivalent to inner
df.reindex(ind - ind2).join(df2.reindex(ind - ind2))  # join on A set minus B

rawDescription: DataFrame algebra in Pandas

description: DataFrame algebra in Pandas


rawDescription: All possible combinations of numbers of length 5 in a 4X4 matrix


code:
list(multiletters("abc", 2))
list(multiletters("abcd", 4))[:8]

rawDescription: All strings with list of characters?

description: All strings with list of characters


code:
import binascii
s=''.join(chr(i^j) for i,j in zip(s1,s2))
s4=binascii.a2b_qp(s)
''.join(chr(i^j) for i,j in zip(s1,s4))

rawDescription: Xor bits in python

description: Xor bits in python


code: range(1, 1)

rawDescription: List index out of range even though I'm asking for [0]?


code: handler.stream.seek(0, 2)

rawDescription: Python logging and subprocess ouput and error stream


code:
import datetime
a = datetime.date(2011, 2, 8)
b = datetime.date(2010, 5, 14)
months(a, b)

rawDescription: Subtract dates in Django?

description: Subtract dates in Django


code:
@fk_processed
def add (x, y): return x + y
add (1, 2)
add (1, 3)
add (1, 2)
add ('1', '3') # Here is the problem if you can have various types from your arguments

rawDescription: Design for application able to calculate a function or load the result from a file


code:
def sizeVH(vSegHeight,hSegWidth,vSep,hSep):

    hSepStr= " " *hSep
    hSegN= " "* hSegWidth
    hSegY= "-"* hSegWidth

    phyLineN= []
    phyLineN.append([])
    phyLineN[0]= []
    phyLineN.append([])
    phyLineN[1]= []
    phyLineN[1].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[2]= []
    for i in range(vSegHeight) :
        phyLineN[2].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[3]= []
    phyLineN[3].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[4]= []
    for i in range(vSegHeight) :
        phyLineN[4].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[5]= []
    phyLineN[5].append([ "", " ", hSegN," ", hSepStr ])
    phyLineN.append([])
    phyLineN[6]= []
    for i in range(vSep) :
        phyLineN[6].append([ "", " ", hSegN," ", hSepStr ])

    phyLineY= []
    phyLineY.append([])
    phyLineY[0]= []
    phyLineY.append([])
    phyLineY[1]= []
    phyLineY[1].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[2]= []
    for i in range(vSegHeight) :
        phyLineY[2].append([ "", "|", hSegN,"|", hSepStr ])
    phyLineY.append([])
    phyLineY[3]= []
    phyLineY[3].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[4]= []
    for i in range(vSegHeight) :
        phyLineY[4].append([ "", "|", hSegN,"|", hSepStr ])
    phyLineY.append([])
    phyLineY[5]= []
    phyLineY[5].append([ "", " ", hSegY," ", hSepStr ])
    phyLineY.append([])
    phyLineY[6]= []
    for i in range(vSep) :
        phyLineY[6].append([ "", " ", hSegN," ", hSepStr ])

    return (phyLineN,phyLineY)

phyLineN, phyLineY= sizeVH(4,6,1,3)
printNumberNY(3475649560458,phyLineN,phyLineY)

rawDescription: Calculator 7 Segment Display w/ Width

description: Calculator 7 Segment Display w/ Width


code:
def grouped_boxplots(data_groups, ax=None, max_width=0.8, pad=0.05, **kwargs):
    if ax is None:
        ax = plt.gca()

    max_group_size = max(len(item) for item in data_groups)
    total_padding = pad * (max_group_size - 1)
    width = (max_width - total_padding) / max_group_size
    kwargs['widths'] = width

    def positions(group, i):
        span = width * len(group) + pad * (len(group) - 1)
        ends = (span - width) / 2
        x = np.linspace(-ends, ends, len(group))
        return x + i

    artists = []
    for i, group in enumerate(data_groups, start=1):
        artist = ax.boxplot(group, positions=positions(group, i), **kwargs)
        artists.append(artist)

    ax.margins(0.05)
    ax.set(xticks=np.arange(len(data_groups)) + 1)
    ax.autoscale()
    return artists

rawDescription: Pyplot boxplots centered around xticks

description: Pyplot boxplots centered around xticks


code:
_list = [i for i in range(3, 5000, 2)]
_list.append(2)

for i in _list:
   popped = [_list.pop(j) for j,x in enumerate(_list) if x%i==0 and x!=i]
   # above list compression means remove all list items that are divisible by i and not equal to i
   print(len(_list), i)

rawDescription: Updating list with a for loop

description: Updating list with a for loop


code:
from scipy.stats import linregress
X = pd.Series(np.arange(10))
Y = pd.Series(np.arange(10))

In [4]: linregress(X, Y)
Out[4]: (1.0, 0.0, 1.0, 4.3749999999999517e-80, 0.0)

rawDescription: How to apply linregress in Pandas bygroup

description: Apply linregress in Pandas bygroup


rawDescription: Regex, how to remove all non-alphanumeric except colon in a 12/24 hour timestamp?


code: a > c

rawDescription: Python 3.2.2 custom object comparation fails


code:
with a() as b:
    # data won't store
    for i in xrange(100):
        # don't store data
        b[i] = i + 5

pass
# data store while leave context

rawDescription: Python disable while iterating

description: Python disable while iterating


code:
def generate():
    """
    CSV generator.
    """
    yield CSV_HEADERS

    for row in database.get_all_rows():
        csv_res = ','.join(row) + '\n'
        yield csv_res

rawDescription: python flask generate csv without losing first row

description: Python flask generate csv without losing first row


code:
form = UserForm()
form.job.choices = [(a.id, a.name) for a in Job.query.order_by(Job.name)]

rawDescription: Dynamic select field using WTForms not updating


code:
def save_user_friends(user, friends):
    user_friends = []
    for friend in friends:

        create_fields = ['id', 'user_id', 'first_name', 'last_name', 'city', 'province', 'country']
        update_fields = ['first_name', 'last_name', 'city', 'province', 'country']

        user_friends.append(
            [
                str(user.id),
                str(friend['id']),
                friend['first_name'],
                friend['last_name'],
                friend['city'],
                friend['province'],
                friend['country'],
            ]
        )

    User_Friend.objects._bulk_insert_or_update(create_fields, update_fields, user_friends)

rawDescription: MySQL INSERT ... ON DUPLICATE KEY UPDATE with django 1.4 for bulk insert


code:
import numpy as np
from StringIO import StringIO # for example file

a = StringIO("""family  id  mum  dad  rs1  rs2  rs3  rs4  rs5  rs6  rs7  rs8  rs9  rs10  rs11  rs12
1  1   4   6   A T  A A  T T  C C  G G  A T  A G  A A  G A  T A  G G  C C
2  2   7   9   T A  G A  C T  C T  G A  T T  A A  A C  G G  T A  C C  C T
3  3   2   8   T T  G G  C T  C T  G G  A T  A G  A C  G G  T T  C C  C C
4  4   5   1   A A  A A  T T  C C  G A  T T  A A  A A  G A  T A  G C  C T """)

nrs = 12        # number of `rs` columns, for dtype
dt = 'int,'*4 + 'S10,'*nrs

A = np.genfromtxt(a, delimiter='  ', names=True, dtype=dt)

rawDescription: Combine two columns under one header in Numpy array


rawDescription: Zipping specific elements of a list in Python


code:
new = 'this is a bunch of words'
new2 = 'this is another bunch of words'

unique_words = set(new.split())
unique_words.update(new2.split())
sorted_unique_words = sorted(list(unique_words))
print('\n'.join(sorted_unique_words))

rawDescription: How do I find all unique words(no duplicates)?

description: Find all unique words(no duplicates)


rawDescription: How to change ticklabels within figure created with axisartist

description: Change ticklabels within figure created with axisartist


code: import attr

rawDescription: Python: preferred way to handle large number of attributes that might not exists?


code:
HEIGHT = 10
WIDTH = 40
MARKER = '*'
FILL_CHARACTER = ' '

coords = [(ch[0], ch[3]) for ch in band1.values()]

# Convert to coordinates of a desired ASCII area

xmin = min(c[0] for c in coords)
xmax = max(c[0] for c in coords)
kx = (WIDTH - 1)  / (xmax - xmin)

ymin = min(c[1] for c in coords)
ymax = max(c[1] for c in coords)
ky = (HEIGHT - 1) / (ymax - ymin)

acoords = [(round((c[0] - xmin) * kx),
            round((c[1] - ymin) * ky)) for c in coords]

# Actually draw the graph

for y in range(HEIGHT, -1, -1):
    chars = []
    for x in range(WIDTH):
        if (x, y) in acoords:
            chars.append(MARKER)
        else:
            chars.append(FILL_CHARACTER)
    print(''.join(chars))

rawDescription: Ascii graph in python

description: Ascii graph in python


code:
from lxml import html
import requests

base_url = '?'.join(['http://www.fashionguide.com.tw/Beauty/08/MsgL.asp',
                     'LinkTo=TopicL&TopicNum={topic}&Absolute=1'])
topics = [13976, 13996, ]

xp1 = "//*[@itemprop='brand']/text()"
xp2 = "//*[@itemprop='brand']/following-sibling::span[1]/text()"

for t in topics:
    url = base_url.format(topic=t)
    page_content = requests.get(url).content
    tree = html.fromstring(page_content)
    title1 = tree.xpath(xp1)  # returns a list!
    title2 = tree.xpath(xp2)
    title = " ".join(title1 + title2)
    print(title)

rawDescription: How to scrape Traditional Chinese text with beautifulsoup?

description: Scrape Traditional Chinese text with beautifulsoup


code:
from django.db import models

from .fields import CMSImageField

class CMSDocument(models.Model):
    title = models.CharField(max_length=50)

class CMSMediaDocument(CMSDocument):
    thumb = CMSImageField(upload_to='./media/', blank=True)

rawDescription: Python Django: How to upload a file with a filename based on instance pk


rawDescription: How to pass csrf_token to the post params of editurl of jqgrid?

description: Pass csrf_token to the post params of editurl of jqgrid


code:
for filenames in os.listdir(r'C:\Users\output'):
    for filename in filenames:
        print(filenames)

rawDescription: Confused about how lists or os.listdir() work in Python 3


code:
mapping = {'ABSTRACT': 'Abstract', 'Keywords': 'Keywords', 'REFERENCES': 'References'}
for div in my_file['div'] :
    for span in div.find_all('span'):
        text = span.get_text(strip=True)

        if text in mapping:
            my_file[mapping[text]] = div

rawDescription: how to get a parent of an <span> tag containing a specific text

description: Get a parent of an <span> tag containing a specific text


code:
def bbb(s):
    return s + 1

rawDescription: Optimal file structure organization of Python module unittests?


code:
sitemaps = {
    'blog': BlogSitemap,
    'static': StaticViewSiteMap,
}

rawDescription: how do I add homepage to Django sitemap?

description: Add homepage to Django sitemap


rawDescription: Django templatetag return True or False

description: Django templatetag return True or False


code:
import csv
split_on_commas = lambda s: csv.reader([s]).next()

rawDescription: Conditional regular expression to split on commas


rawDescription: Python/Pandas subtracting numbers in a column


code:
for lines in rawcode:
    output = [] #empty list for output
    line = rawcode.readline()

rawDescription: Iterating over lines in a file yields wrong number of lines


code:
class TestDiscoverSomeType(unittest.TestCase):
    @patch('sometype.discover_some_type')
    def test_discover_some_type(self, mock_discover_some_type):
        import discovery_channel

        file_to_discover = SomeType()

        datashape.discover(file_to_discover)

        mock_discover_some_type.assert_called_with(file_to_discover)

rawDescription: How do I patch a method registered by a decorator in Python's datashape?

description: Patch a method registered by a decorator in Python's datashape


code:
from instagram.client import InstagramAPI
from django.conf import settings
from bson.json_util import default

import json
import logging
import instagram.models
import time
import datetime

rawDescription: python instagram api user_recent_media returns None

description: Python instagram api user_recent_media returns None


code:
for sprite in group: #Replace group with your sprite group
    if sprite.type == 1: #Or whatever type you want
        sprite.draw(screen)
    elif sprite.type == 2:
        sprite.draw(screen)
    #Etc...

rawDescription: pygame: draw() currently draws all sprites in a group. How can I just draw a specific one

description: Pygame: draw() currently draws all sprites in a group. How can I just draw a specific one


code:
import os
import shutil

def dir_copy(srcpath, dstdir):
    dirname = os.path.basename(srcpath)
    dstpath = os.path.join(dstdir, dirname)
    shutil.copytree(srcpath, dstpath)

rawDescription: Python: folder creation when copying files


code:
import  pandas as pd

spl = d["visualLogs"][0].split("\r")

df = pd.DataFrame([v for v in map(lambda x: x.split(","), spl[1:]) if v[0]], columns=spl[0].split(","))

rawDescription: how to extract python data frame from JSON string

description: Extract python data frame from JSON string


rawDescription: Python cosine-similarity on all possible pairs in list


code:
for col in row:
    total += int(col)

rawDescription: Looping through columns in a .csv files in Python


code: df_age = df.sort('Age', ascending=False)

rawDescription: Pandas - Create new dataframes based on ranked values in select columns


code:
def f():
    call_some_function_A()
    call_some_function_B()
#   [...]
    call_some_function_Z()

rawDescription: Executing a function in reverse in Python


rawDescription: How does NOAA convert City,St to lat long?


code:
# [...] generate dynamic data [...]
html = html + template.render (templatepath + 'outputpage_start.html', {})
html = html + template.render (templatepath + 'outputpage_js.html', {})
html = html + """<table width="500" class='out' border="1" data-dynamic="%s">""" % json.dumps(your_generated_data_dict)
#tr/td elements and templating as needet
self.response.out.write(html)

rawDescription: Send data from Python to Javascript (JSON)


code:
['brown', 'log', 'jumped', 'over', 'fox', 'quick', 'the']
['jumped', 'fox', 'over', 'brown', 'log', 'the', 'quick']

rawDescription: creating a heap in python

description: Creating a heap in python


code:
import pickle
s = pickle.dumps({'username': 'admin', 'password': '123'})
s

rawDescription: Python - How do I make a dictionary inside of a text file?


code:
import getpass

user = raw_input("Username:")
passwd = getpass.getpass("Password for " + user + ":")

print("Got", user, passwd)

rawDescription: Python prompt for user with echo and password without echo


code:
def file_loader(f):
    file_dict = {}
    current_set = None
    for line in f:
        if line.startswith('d.complex'):
            file_dict[line] = current_set = {}
            continue

        if current_set is not None:
            current_set[line] = current_set.get(line, 0)

    return file_dict

rawDescription: Comparing two lists items in python

description: Comparing two lists items in python


rawDescription: Average Date Array Calculation

description: Average Date Array Calculation


code:
def render_notifications(request):
    user_notices = notification.Notice.objects.filter(recipient=request.user).order_by(-'added')
    unseen_notices = user_notices.filter(unseen=True)
    notices = user_notices.filter(unseen=False)

    context = RequestContext(request,{'notices':notices,
            'unseen_notices':unseen_notices})
    response = render_to_response('my_notifications.html',context_instance=context)

    # update the seen flag
    unseen_notices.update(unseen=False)

    return response

rawDescription: What's the best way excluding a query set from a queryset


code: selection = [node for node in selection if is_group(node)]

rawDescription: How to isolate group nodes in maya with python

description: Isolate group nodes in maya with python


code:
conversions[FIELD_TYPE.DECIMAL] = float
conversions[FIELD_TYPE.NEWDECIMAL] = float

rawDescription: Fastest way to load numeric data into python/pandas/numpy array from MySQL


code:
class ThreadPool(object):
    def __init__(self, max_workers):
        self.queue = queue.Queue()
        self.workers = [threading.Thread(target=self._worker) for _ in range(max_workers)]
    def start(self):
        for worker in self.workers:
            worker.start()
    def stop(self):
        for _ in range(self.workers):
            self.queue.put(None)
        for worker in self.workers:
            worker.join()
    def submit(self, job):
        self.queue.put(job)
    def _worker(self):
        while True:
            job = self.queue.get()
            if job is None:
                break
            job()

rawDescription: how to download multiple file simultaneously and join them in python?

description: Download multiple file simultaneously and join them in python


code:
List = [['Server1','Server2','Server4','Server6','EnvName1','become_a'],
        ['Server2','Server3','Server1','Server5','EnvName2','become_a'],
        ['Server4','Server3','Server7','Server6','EnvName3','become_b'],
        ['Server5','Server1','Server2','Server3','EnvName4','become_a'],
        ['Server4','Server2','Server1','Server7','EnvName5','become_b']]
grouped = {}
for entry in List:
    env, account = entry[-2:]
    for server in entry[:-2]:
        key = server, account
        grouped.setdefault(key, []).append(env)

[[server] + environments + [account]
              for (server, account), environments in sorted(grouped.items())]
[[server, ','.join(environments), account]
              for (server, account), environments in sorted(grouped.items())]

rawDescription: Grouping of list of list for elments of inner list in python


code:
def ascii_encode_dict(data):
    ascii_encode = lambda x: x.encode('ascii')
    return dict(map(ascii_encode, pair) for pair in data.items())

json.loads(json_data, object_hook=ascii_encode_dict)

rawDescription: Forcing Python json module to work with ASCII


code:
filt = resonator(freq=pi/4, bandwidth=pi/8)
filt
type(filt)

rawDescription: Applying time-variant filter in Python

description: Applying time-variant filter in Python


code: m = re.findall('\[[^\[\]]*\]', '[Honda] Japanese manufacturer [VTEC] Name of electronic lift control')

rawDescription: Capture the text inside square brackets using a regex


code:
def to_camel_case(snake_str):
    components = snake_str.split('_')
    # We capitalize the first letter of each component except the first one
    # with the 'title' method and join them together.
    return components[0] + "".join(x.title() for x in components[1:])

rawDescription: Converting Snake Case to Lower Camel Case (lowerCamelCase)


code:
lock = threading.Lock()

def mainWork():
    # do stuff here
    lock.acquire()
    if 'di_unread_inbox' in inbox_page:
        try:
            # something in here changes inbox_page so that 'di_unread_inbox' isn't there anymore
            inboxmessagelabel.set("some label")
        finally:
            lock.release()
    else:
        lock.release()
        # do other stuff

rawDescription: lock all threads but one in python

description: Lock all threads but one in python


rawDescription: Trying use a .pth file to add a path in documents folder on Mac


code: new_date = use_date+relativedelta(months=+1, day=1, weekday=TU(2))

rawDescription: Is there a simple way to increment a datetime object one month in Python?


code: return render_to_response('ui/search.html', {"result":result},  context)

rawDescription: Displaying view variables in template with Django?


code:
import re

e2 = etree.fromstring(xml2)
ns_re = dict(re="http://exslt.org/regular-expressions")
re_id = "|".join(map(re.escape, ids))
nodes = e2.xpath("//id[re:test(.,'^(?:%s)$')]/parent::node" % re_id,
                 namespaces=ns_re)

rawDescription: XML Cross Reference

description: XML Cross Reference


code: sheets = {}

rawDescription: Switching between worksheets

description: Switching between worksheets


code:
print('Format {} example {}'.format(stringA, stringB))
print('Old: %s example %s of string formatting' % (stringA, stringB))

rawDescription: Simple adding spaces between variables in python3


rawDescription: convert lists to tuples converting also the lists inside the list

description: Convert lists to tuples converting also the lists inside the list


code:
import math

data = [[1.565888, 2.073744], [2.073744, 2.962492], [2.962492, 4.52838], [4.52838, 5.417127], [5.417127, 6.390517], [7.025337, 7.871763], [11.1, 12.1]]

def int_split(data):
    last_b = data[0][1]

    for item in data:
        a, b = item

        # First, make sure we haven't missed anything from the last loop
        if math.floor(a) - math.ceil(last_b) > 1.0:
            for x in int_split([[last_b, a]]):
                yield x

        # Did we cross an integer boundary, i.e.
        # Is ceil(b) - floor(a) > 1.0?
        if math.ceil(b) - math.floor(a) > 1.0:
            # Yes, so split, making sure to include all integers between a and b

            # Find any integers in the range (and convert to floats)
            ints = [float(x) for x in range(int(math.ceil(a)), int(b))]
            for c in ints:
                # output those values
                yield [a, c]
                a = c
            else:
                yield [a, math.floor(b)]
                yield [math.floor(b), b]
        else:
            yield item

        # remember where we are
        last_b = b

rawDescription: Need help thinking through splitting lists at integer divisions in Python


code: total_appr = appr.sum( axis=1 )

rawDescription: Calculating previous chance figure for use in chi square test


code:
import matplotlib.pyplot as plt
fig,ax = plt.subplots(1)
l,=ax.plot(range(5))
l.set_label('line 1')
ax.legend()
plt.show()

rawDescription: How do I set label for an already plotted line in matplotlib?

description: Set label for an already plotted line in matplotlib


code:
from turtle import *

listening = False

def listen(listen_original=listen):
    global listening
    listen_original()
    listening = True

def unlisten():
    global listening
    listening = False

def setChar1():
    if not listening:
        return

    reset()
    for i in range(36):
        forward(200)
        left(170)

def setChar2():
    unlisten()

def setChar3():
    listen()

onkey(setChar1, '1')
onkey(setChar2, '2')
onkey(setChar3, '3')
listen()

done()

rawDescription: Python - Turtle Stop Listening

description: Python - Turtle Stop Listening


rawDescription: python how to create list of interchangeable values?

description: Python how to create list of interchangeable values


code: df.pivot_table(values='val', index=df.index, columns='key', aggfunc='first')

rawDescription: Pandas column values to columns?

description: Pandas column values to columns


code: vertices = [rand_xy() for _ in range(N)]

rawDescription: Python List Multiplication on Function Call


code:
let2num('unmilione')
let2num('unmilionemilleduecento')
let2num('unmilionemilleduecentotre')
let2num('ventiquattro')
let2num(normalize_text('Dieci milioni e CentoQuarantaTreMila miliardi di miliardi di miliardi Otto cento e quattro'))
let2num('ventiquattromiliardicentotrentatremilionitredicimiladuecentouno')

rawDescription: Convert number to Italian and Italian to number in python


code:
def get_uniq_by_value(data, val_name):
    return {val for value in data.values() for val in value.get(val_name,[])}

rawDescription: Get unique list items by nested dictionary key


code:
zeros_count = len([i for i in myNum if i == '0'])
print("number of zeros: %d" % zeros_count)

rawDescription: How to calculate numbers from loop?

description: Calculate numbers from loop


rawDescription: Built-in method to Generate Random Strings of Fixed Length From Given Characters


code:
from collections import namedtuple

Point = namedtuple('Point', 'x y')

def calculate_coordinates(foo, bar, baz):
    return Point(42, 81)

rawDescription: Assign attribute to function

description: Assign attribute to function


code:
import operator

f_dict = {"e"+str(i):f for i,f in enumerate([lambda x: op(x, n) for n in range(10) for op in [operator.lt ,operator.le , operator.eq]])}

locals().update(f_dict)

rawDescription: How to transform a arithmetic expression to a Python arithmetic function?

description: Transform a arithmetic expression to a Python arithmetic function


code: [[elem.strip('\n') for elem in lst] for lst in listoflists]

rawDescription: Editing strings in a list of lists

description: Editing strings in a list of lists


code:
import pandas as pd
f.seek(0)
pd.read_csv(f, delimiter=' ', index_col=0, names=['a', 'b'],
            converters={1: parse_pair, 2: parse_pair})

rawDescription: How to read complex numbers from file with numpy?

description: Read complex numbers from file with numpy


code:
root = HelloWorld()
root.onepage = OnePage()
root.otherpage = OtherPage()

rawDescription: Running more than one class in Cherrypy

description: Running more than one class in Cherrypy


code:
import random
def obfuscate(word):
    return ''.join([random.choice(["!", "%", "*", "#", "@"]) for char in word])

rawDescription: Python, obfuscating letters in a list

description: Python, obfuscating letters in a list


code:
def counter(val=0):
    while True:
        yield val
        val += 1

rawDescription: How would a pythonista code the equivalent of the ++ increment operator in Python 3?


code:
import requests
from bs4 import BeautifulSoup
soup = BeautifulSoup(requests.get('http://deals.whotels.com/W-Guangzhou-3126/tnc/1680/24900/en').content)
terms_elements = soup.find(class_='popBodyText')
[t.lstrip('\u2022 ') for t in terms_elements.stripped_strings if t.startswith('\u2022')]

rawDescription: Scrape text after every <br> - BeautifulSoap


code: zip(*args)

rawDescription: Arbitrary number of assignments (lists (of numbers)) converted into single list


code:
import example
def fun():
    assert('xyz' not in locals())
    x = example.X()
    assert('xyz' in locals())
    assert('xyz' not in globals())

assert('xyz' not in globals())
fun()
assert('xyz' not in globals())
x = example.X()
assert('xyz' in globals())
del xyz
fun()
assert('xyz' not in globals())

rawDescription: PyEval_GetLocals returns globals?

description: PyEval_GetLocals returns globals


code:
import numpy as np
cump = np.cumsum(np.random.rand(11))
cump -= cump[0]
cump /= cump[-1]
cump

rawDescription: Make Numpy.where stop after first true encountered


code:
def evalpage(request, user):
    ...

rawDescription: Retrieve clicked list item in Django

description: Retrieve clicked list item in Django


rawDescription: create csv file on each run

description: Create csv file on each run


code:
from decimal import Decimal
RATES = {
    'JPY': Decimal("0.010589"),
    'EUR': Decimal("1.2983"),
    'GBP': Decimal("1.5228"),
    'USD': Decimal("1.0000"),
}
def conversion_rate(cur1, cur2):
    """Get the conversion rate from currency 1 to currency 2."""
    return RATES[cur1] / RATES[cur2]

rawDescription: Make sure input is limited to variables

description: Make sure input is limited to variables


rawDescription: How to read csv file into Python 2d array?

description: Read csv file into Python 2d array


code: [ k  for k,v in Counter(lst2).iteritems() if v == Counter(lst)[k] ]

rawDescription: Compare tuple values in Two lists

description: Compare tuple values in Two lists


code:
import sys

# your cool code here

sys.exit(0) # stops the program without a pop-up

rawDescription: How to remove exit(0) pop-up in Python IDLE?

description: Remove exit(0) pop-up in Python IDLE


code:
import flask

app = flask.Flask(__name__)

footext = """f
o
<script>alert('oops')</script>
o"""

@app.route("/foo")
def foo():
    text = ""
    for line in footext.split('\n'):
        text += flask.Markup.escape(line) + flask.Markup('<br />')
    return flask.render_template("bar.html", text=text)

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

rawDescription: disabling autoescape in flask

description: Disabling autoescape in flask


code:
def function (parameter_1, parameter_2, action = None, var = None):
    if parameter_1 == 1:
        return action(var)

variable = function(1, 2, secondary_function, variable)

rawDescription: Function within a function to alter a variable


rawDescription: Print a list of space-separated elements in Python 3


code: cal = vobject.iCalendar()

rawDescription: Create "downloadable" vCalendar object for Outlook with vObject (Python)


code:
#It looks like you are writing the word yes to all of the lines, then spliting
#each word into letters and listing those word's letters on their own line?
source = open('myfile','r')
output = open('out','w')
for line in source:
    for word in line.split():
        new = ",".join(word)
        print >>output, new
    print >>output, 'y,e,s'

rawDescription: Edit and save file

description: Edit and save file


code:
[
 (split1_train_idxs, split1_test_idxs),
 (split2_train_idxs, split2_test_idxs),
 (split3_train_idxs, split3_test_idxs),
 ...
]

rawDescription: scikit-learn cross validation custom splits for time series data

description: Scikit-learn cross validation custom splits for time series data


code:
res[0].currency
res[0].price
res[0].destination
res[0].flight_segment[0].origin
len(res[0].flight_segment) # gives how many segments

rawDescription: Pandas semi structured JSON data frame to simple Pandas dataframe


code:
class Person(Document):
    @classmethod
    def by_name(cls, db, **kw):
        return cls.view(db, **kw)

rawDescription: Meaning of the map function in couchdb-pythons ViewField


code:
namelist = ["Robinson, David", "Roberts, Tim"]
# split each item into a list, around the commas:
[n.split(", ") for n in namelist]
# [['Robinson', 'David'], ['Roberts', 'Tim']]

# reverse the split up list:
[n.split(", ")[::-1] for n in namelist]
# [['David', 'Robinson'], ['Tim', 'Roberts']]

# join it back together with a space:
[" ".join(n.split(", ")[::-1]) for n in namelist]
# ['David Robinson', 'Tim Roberts']

rawDescription: Switch Lastname, Firstname to Firstname Lastname inside List


code: result = [((int(item[0]), item[1], int(item[2]), item[3], item[4])) for item in data]

rawDescription: Converting items in a tuple Python

description: Converting items in a tuple Python


code:
df['c'] = df['c'].ffill().fillna(0)
df

rawDescription: pandas get last value of column x when column y is equal to z

description: Pandas get last value of column x when column y is equal to z


rawDescription: using dictionaries and linked list python

description: Using dictionaries and linked list python


code:
list_name = [('ABC', 'DEF', 2), ('GHI', 'JKL', 6), ('MNO', 'PQR', 22), ('ABC', 'STU', 2)]
list_name.sort(key=lambda x: (-x[2],) + x[:2])
list_name

rawDescription: Organize Numerically Descending then Alphabetically Ascending by Different Elements in a List Python


code:
cardmonitor = CardMonitor()
cardobserver = printout()
cardmonitor.addObserver(cardobserver)
#If no card in 20secs kill program (put in for testing)
sleep(20)
cardmonitor.deleteObserver(cardobserver)

rawDescription: Python ACR122U Poll

description: Python ACR122U Poll


rawDescription: Python - most efficient way to select a random message according to a type


rawDescription: OpenCV built in Debug or Release mode?

description: OpenCV built in Debug or Release mode


code: mpl.rcParams['axes.color_cycle'] = ['m', 'r', 'y']

rawDescription: Matplotlib & Python: assign a color when doing iterateive plotting


code:
def line_to_rainfall(line):
    cols = line.split(",")
    month, num_days = int(cols[0]), int(cols[1])
    daily_rainfalls = [float(x) for x in cols[2:2+num_days]]
    return month, daily_rainfalls

def print_monthly_totals (input_csv_filename):
    data = open(input_csv_filename).readlines()
    print('Rainfall totals for each month')
    for line in data:
        month, daily_rainfalls = line_to_rainfall(line)
        total_rainfall = sum(daily_rainfalls)
        print('Month {:2}: {:.1f}'.format(month, total_rainfall))

rawDescription: Python more functions

description: Python more functions


rawDescription: How to apply a function to only one item in a sublist across all sublists?

description: Apply a function to only one item in a sublist across all sublists


rawDescription: Generate random numbers without using the last n values in Python


code:
for item in result:
    print('{} - {}, {}'.format(*(part.replace(';', ' ') for part in item)))

rawDescription: Python Complicated Splitting of a String


code: topicResult = " ".join([t['TopicArn'] for t in topicsList if topic in t['TopicArn']])

rawDescription: Variable that returns complete string inside list


rawDescription: Selecting some elements based on conditions


code:
elem = soup.find(class_='special_tag')
elem.clear()
elem.append('inner')
print(elem)

rawDescription: Replacing the inner HTML with BeautifulSoup?


code: # empty

rawDescription: Import local testing version of a Python namespace package


code:
import itertools as it

def addVecs(a, b):
    return [e1 + e2 for e1, e2 in zip(a, b)]

def scaleVec(a, s):
    return [e*s for e in a]

# first 7 rows add 1 to a and 1 to c
add1 = [1, 0, 1, 0]

# next 5 rows add 1 to b and 1 to d
add2 = [0, 1, 0, 1]

# stack them accordingly
upper = list(it.chain(it.repeat(add1, 7),
                      it.repeat(add2, 5)))

# lower is the negated version of upper
lower = list(it.starmap(scaleVec, zip(upper, it.repeat(-1))))

# stack them
both = upper + lower

# create cumsum to get for each row the relative distance to the first row
# (istead of distance to previous)
sums = [[0, 0, 0, 0]]
for row in both:
    sums.append(addVecs(sums[-1], row))

# the first row
l = [0, 0, 2137, 1419]

# now for each row in sums, add it to l
result2 = list(it.starmap(addVecs, zip(it.repeat(l), sums)))
for row in result2:
    print(row)

rawDescription: Python array: formulas to populate array with data


code:
import requests
import lxml.html as LH
url = ("https://www.goodeggs.com/sfbay/missionheirloom/"
       "seasonal-chicken-stew-16oz/53c68de974e06f020000073f")
page = requests.get(url, verify=False)
root = LH.fromstring(page.text)

path = '//section[@class="product-description"]/div[@class="description-body"]'
for div in root.xpath(path):
    print(div.text_content())

rawDescription: What am I doing wrong? Parsing HTML using lxml


rawDescription: How to store a one-to-one relationship table for fast lookups in Python?

description: Store a one-to-one relationship table for fast lookups in Python


code: [{} for _ in range(7)]

rawDescription: Different Dictionaries sharing the same instance


code:
from Tkinter import *

master = Tk()

listbox = Listbox(master)
listbox.pack()

listbox2 = Listbox(master)

def moveDown():

    move_text = listbox.selection_get()
    curindex = int(listbox.curselection()[0])
    listbox.delete(curindex)
    listbox2.insert(END, move_text)

moveBtn = Button(master, text="Move Down", command=moveDown)
moveBtn.pack()

listbox2.pack()

for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)

mainloop()

rawDescription: creating an interactive Python GUI that generates a new list

description: Creating an interactive Python GUI that generates a new list


code:
class MyDateTimeField(forms.Field):

    def to_python(self, value):
        # Return an empty list if no input was given.
        if not value:
            return []
        import datetime
        today = datetime.date.today()
        hhmm = value.split(':')
        val= datetime.datetime.combine(datetime.date.today(), datetime.time(int(hhmm[0]),int(hhmm[1])))
        return val

rawDescription: allow HH:MM kind of input in a django DateTimeField instead of long format

description: Allow HH:MM kind of input in a django DateTimeField instead of long format


code: list_display = ('trucks', 'multiplier', 'capacity')

rawDescription: django, show fields on admin site that are not in model

description: Django, show fields on admin site that are not in model


code:
right_table = soup.find('table', class_='table table-bordered table-striped table-condensed movers-table')

table_dict = {}

for row in right_table.select("tr"):
    # increase class are where increments are
    increments = [s.text for s in row.select('span.increase')]
    # make sure we have some data in tr
    if increments:
        # rank/place is first text in td, could also use find("td",{"class":"first-right"})
        place = int(row.td.text)
        # text/character name is in a tag text
        title = row.find("a").text
        increments.append(title)
       # get alt attribute from img tag
        increments.append(row.find("img")["alt"])
        table_dict[place] = increments

from pprint import pprint as pp

pp(table_dict)

rawDescription: BeautifulSoup: Extract "img alt" content Web Scraping in Python


rawDescription: How can i use the forloop counter in django without resetting

description: Use the forloop counter in django without resetting


code:
class ParentlessChildlessVisitor(DictionaryVisitor):
    def visit(self, node, parents, result):
        """ Collect the nodes that have no parents or children """
        # parent filtering is performed in accept
        if 'childs' not in node:
            result.append(node)

    def accept(self, nodes, parents, result):
        """ Reject all nodes with parents """
        return not parents

rawDescription: create different list of dicts from master list of dict

description: Create different list of dicts from master list of dict


rawDescription: How to programmatically generate an event in wxPython

description: Programmatically generate an event in wxPython


code:
with open("file") as fh:
    for line in fh:
        if not sync(int(line)) == 0:
            break

rawDescription: One-liner for calling a function for each line in a file


code:
for element in iterable:
    k = key(element)
    if k not in seen:
        seen.add(k)
        yield element

rawDescription: Custom comparison functions for built-in types in Python


code:
letters = ['a', 'b', 'c']
cn = letters.count("c")
letters = ["c"] * letters.count("c") + letters

print(letters)

rawDescription: Why can't I duplicate selected items in for loop?


code: list(generator)

rawDescription: Better way to execute nested for loops to generate a dict?


rawDescription: python, deleting characters from a line, but leaving numbers and special symbols

description: Python, deleting characters from a line, but leaving numbers and special symbols


code:
import re
s = ' Luca Bercovici (characters) '
re.sub('^\s|\s$','',s)

rawDescription: Strip space on either side of word

description: Strip space on either side of word


code:
def int_int_doSomething(a, b):
    return (a + b * 2) > 5

def int_int_doSomethingElse(a, b):
    return a - b * 4

Example = {}
Example[(int, int)] = (int_int_doSomething, int_int_doSomethingElse)

def doSomething(a, b):
    types = type(a), type(b)
    return Example[types][0](a, b)

def doSomethingElse(a, b):
    types = type(a), type(b)
    return Example[types][1](a, b)

rawDescription: From Haskell to functional Python

description: From Haskell to functional Python


code:
import matplotlib.pyplot as plt
import scipy

x = np.random.normal(0,1,15000)
y = np.random.normal(0,1,15000)
tol = 0.01

xy = np.hstack((x[:,np.newaxis],y[:,np.newaxis]))
d = scipy.spatial.distance.cdist(xy,xy)
b = np.ones(x.shape,dtype='bool')
for i in range(d.shape[0]-1):
    if d[i,i+1:].min() < tol and b[i]:
        b[i+1+d[i,i+1:].argmin()] = False

x2 = x[b]
y2 = y[b]

f, (ax1, ax2) = plt.subplots(1, 2)

ax1.scatter(x,y,s=90)
ax1.set_xlim(-6,6)
ax1.set_ylim(-6,6)
ax2.scatter(x2,y2,s=90)
ax2.set_xlim(-6,6)
ax2.set_ylim(-6,6)

print('Before: ', x.shape,'\nNow: ',x2.shape)
plt.show()

rawDescription: Remove occluded scatter plot points

description: Remove occluded scatter plot points


code:
def highscore(filename):
    try:
        with open(filename, 'r') as f:
            return int(f.read())
    except:
        return 0

rawDescription: Python / Function parameters

description: Python / Function parameters


code:
class Color(object):
    def __init__(self, red, green, blue, tint):
        self.red = red
        self.green = green
        self.blue = blue
        self.tint = tint

    def somemethod(self):
        pass #you can add some method.

rawDescription: How to create and fill 2d list in python. Basically trying to create a list of rgb values

description: Create and fill 2d list in python. Basically trying to create a list of rgb values


code:
from sympy import *
from sympy.solvers.solveset import linsolve
x, y, z = symbols('x, y, z')

rawDescription: How can I solve system of linear equations in SymPy?

description: Solve system of linear equations in SymPy


code:
running_time = current_time - launch_time.total_seconds()
running_time = running_time.seconds / 60 + running_time.days / 1440

rawDescription: Python doesn't compare date?

description: Python doesn't compare date


code:
from datetime import timedelta
x = 'hours'
timedelta(x=3)
timedelta(**{x:3})

rawDescription: Passing Arguments to Inner Function

description: Passing Arguments to Inner Function


code:
response = client.create_policy(
    PolicyName='string',
    Path='string',
    PolicyDocument='string',
    Description='string'
)

rawDescription: How do you create roles and policies with Boto 3?

description: Create roles and policies with Boto 3


code:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.transforms import BlendedGenericTransform

delays = [0.5, 2.3, 'L', 0.9, 'L', 2]
delays = [item if item != 'L' else np.nan for item in delays]
delays = np.ma.masked_invalid(delays)

fig, ax = plt.subplots()

ax.plot(delays, 'bo')

flags = delays.mask.nonzero()
ax.plot(flags, np.zeros_like(flags), 'rx', clip_on=False, mew=2,
        transform=BlendedGenericTransform(ax.transData, ax.transAxes))

# Expand axis limits by 0.5 in all directions for easier viewing
limits = np.array(ax.axis())
ax.axis(limits + [-0.5, 0.5, -0.5, 0.5])

plt.show()

rawDescription: matplotlib: draw special symbol when a value appears

description: Matplotlib: draw special symbol when a value appears


code:
iterates = input("How many iterates: ")
trials = input("How many trials: ")

def function():
    recordList = []

    for i in range(iterates):
        # math goes here
        recordList.append()

    return recordList

aggregateList = []
for i in range(trials):
    aggregateList.append(function())

rawDescription: Python Overwriting List Element

description: Python Overwriting List Element


code: data[np.arange(start1, end1, step1), np.arange(start2, end2, step2)]

rawDescription: Multidimensional arrays, using range, while simultaneously having a set start, stop, and step?


rawDescription: What method can I use just before deserialization in Django REST?


code:
import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Options")

        radio1 = wx.RadioButton( self, -1, " Radio1 ", style = wx.RB_GROUP )
        radio2 = wx.RadioButton( self, -1, " Radio2 " )
        radio3 = wx.RadioButton( self, -1, " Radio3 " )

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(radio1, 0, wx.ALL, 5)
        sizer.Add(radio2, 0, wx.ALL, 5)
        sizer.Add(radio3, 0, wx.ALL, 5)

        for i in range(3):
            chk = wx.CheckBox(self, label="Checkbox #%s" % (i+1))
            sizer.Add(chk, 0, wx.ALL, 5)
        self.SetSizer(sizer)

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "wx.Menu Tutorial")

        # Add a panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()

        optionsItem = fileMenu.Append(wx.NewId(), "Options",
                                      "Show an Options Dialog")
        self.Bind(wx.EVT_MENU, self.onOptions, optionsItem)

        exitMenuItem = fileMenu.Append(wx.NewId(), "Exit",
                                       "Exit the application")
        self.Bind(wx.EVT_MENU, self.onExit, exitMenuItem)

        menuBar.Append(fileMenu, "&File")
        self.SetMenuBar(menuBar)

    #----------------------------------------------------------------------
    def onExit(self, event):
        """"""
        self.Close()

    #----------------------------------------------------------------------
    def onOptions(self, event):
        """"""
        dlg = MyDialog()
        dlg.ShowModal()
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.PySimpleApp()
    frame = MyForm().Show()
    app.MainLoop()

rawDescription: wxPython and add Options Menu when select in Menu Bar

description: WxPython and add Options Menu when select in Menu Bar


code:
class MovieSerializer(serializers.ModelSerializer):
    """
    Serialiazing all the Movies.
    """
    genre = MyPrimaryKeyRelatedField(many=True, queryset=Genre.objects.all())
    directorName = MyPrimaryKeyRelatedField(queryset=Director.objects.all())
    owner = serializers.ReadOnlyField(source='owner.username')
    class Meta:
        model = Movie
        fields = ('popularity',"directorName",'genre','imdbScore','name','owner')

rawDescription: How to get string representation of PrimaryKeyRelatedField in JSON

description: Get string representation of PrimaryKeyRelatedField in JSON


AFN: Afghan Afghani

ZWL: Zimbabwean Dollar

AED: united Arab Emirates Dirham

rawDescription: Start a new line after every comma

description: Start a new line after every comma


rawDescription: Converting numpy string array to float: Bizarre?


code:
IndexDict = {}
for key, value in  dct.iteritems():
    for i, v in enumerate(value):
       IndexDict.setdefault(i, {})[key] = v

rawDescription: How to map string id, index and value in a nested python dictionary

description: Map string id, index and value in a nested python dictionary


rawDescription: Adding to a specific variable in a list

description: Adding to a specific variable in a list


rawDescription: Store and retreive coordinates using python and yaml


rawDescription: Python -- sympy solve() returning another equation instead of value


rawDescription: Numpy: Indexing of arrays

description: Numpy: Indexing of arrays


code:
def f_log(x, b, c_ln):
    return b * x - c_ln

rawDescription: How to fit the following function using curve_fit

description: Fit the following function using curve_fit


code:
strel = ndimage.generate_binary_structure(2, 1)
mask = ndimage.binary_closing(A==100, structure=strel)

rawDescription: Replace values in an 2d array according to neighboring values


code:
with open(os.path.join(dirname, filename), "r") as assetfile:
    assetdata = assetfile.read().replace("\n", "")

rawDescription: Minify all CSS and Javascript in a directory


code:
it = iter(myorderList)
sorted(myList, key=lambda x: next(it))

rawDescription: Python ordering the list according to another list


code:
df['SecondName'] = df['first'].shift(-1).combine_first(df['last'].shift(1))
df = df.drop(['first', 'l1'], axis=1)

rawDescription: Modify pandas dataframe in python based on multiple rows


code:
sorted_top = []
while top:
    max_top = max(top)
    sorted_top.append(max_top)
    top.remove(max_top)

rawDescription: Code: Code isn't working to sort through a list of 1 million integers, printing top 10


rawDescription: Pandas - Conditional Probability of a given specific b


code:
s
import re
re.findall(r'TEST_COMMAND\.(\d+)', s)

rawDescription: Best way to parse/find text with Python

description: Parse/find text with Python


code:
import sys
row=[]
with open("file.txt","r") as infile:
    while True:
        for line in infile:
            if line.startswith("Sequuo:"):
                line1=line.replace("\t","")
                line1=line.split(":")
                row.append(line1[1].strip())
                #line1=line.replace("\n","\t")
            if line.startswith("saeer on werarms23e:"):
                line1=line.replace("\t","")
                line1=line.split(":")
                row.append(line1[1].strip())

            if line.startswith("//"):
                break
        if line=="\n":
            break

print ('Sequuo  saeer on werarms23e')

count = 0
for i in row:
    count +=1
    sys.stdout.write (i + '\t')
    if not count%2:
        print

rawDescription: Re-formatting data file content using python


code:
m = np.zeros_like(mask).astype(np.int)
m[:] = arr
res = np.ma.masked_where(~mask,m)

rawDescription: Fastest way to filter values in np array based on changing threshold


code:
from rpy2.robjects import r, numpy2ri
numpy2ri.activate()
import numpy as np
A = np.random.rand(50,10)
fit = r.factanal(A, 5, rotation='promax')
corr = fit.rx2('correlation')
r["[["](fit,'correlation')

rawDescription: Extract correlation matrix from R's factanal() via RPy


code: obj = Person(**mapy)

rawDescription: How to convert Python dict to a specifically-typed object?

description: Convert Python dict to a specifically-typed object


code:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
np.random.seed(2015)

N = 16
df = pd.DataFrame({'Opportunities': np.random.randint(0, 30, size=N),
                   'Percentage': np.random.randint(0, 100, size=N)},
                  index=pd.date_range('2015-3-15', periods=N, freq='B').date)
fig, ax = plt.subplots()

df[['Opportunities']].plot(kind='bar', stacked=False, title='get_title',
                           color='grey', ax=ax, grid=False)
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), df[['Percentage']].values, linestyle='-', marker='o',
        color='k', linewidth=1.0, label='percentage')

lines, labels = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax.legend(lines + lines2, labels + labels2, loc='best')
ax.yaxis.set_ticks_position("right")
ax2.yaxis.set_ticks_position("left")

fig.autofmt_xdate()
plt.show()

rawDescription: Multi-Axis Graph with Line on top. Matplotlib


code: re.sub(r'(--\S+)\s+(\w\S+)', r'\1=\2', ' '.join(commandOptionsAndArguments))

rawDescription: how to combine the elements of a list representing the elements of a Bash command to a new list featuring the equals symbol

description: Combine the elements of a list representing the elements of a Bash command to a new list featuring the equals symbol


code:
prefs, _, log = subj.partition('|')
for group in prefs.split(', '):
    pref, _, id5 = group.partition('-')
    if id5.isdigit() and len(id5) <= 5 and pref.isalpha():
         print(pref, id5)

rawDescription: Verify if provided string corresponds to pattern on python


code:
query = 'SELECT * FROM table WHERE column IN ({})'.format(','.join(['%s'] * len(lst)))
cursor.execute(query, lst)

rawDescription: Join a list of strings such that each string is within quotes and comma separated


code:
class NotifyException(Exception):
    '''
    This exception would be handled by send_emails decorator, the decorator will
    catch it and return its value to outer.
    '''
    def __init__(self, value):
        self.value = value
        super(NotifyException, self).__init__(value)

rawDescription: Send mail on Celery task success/failure reliably


rawDescription: Avoiding typing redundancy when generating strings with elements separated by a token


code:
def squared_distances(X, Y):
    """Return a distance matrix for each pair of rows i, j in X, Y."""
    # http://stackoverflow.com/a/19094808/166749
    X_row_norms = np.einsum('ij,ij->i', X, X)
    Y_row_norms = np.einsum('ij,ij->i', Y, Y)
    distances = np.dot(X, Y)
    distances *= -2
    distances += X_row_norms
    distances += Y_row_norms

    np.maximum(distances, 0, distances)  # get rid of negatives; optional
    return distances

rawDescription: Possible optimizations for calculating squared euclidean distance


code:
total_numbers = sum(numbers)

total_numbers
#>>> 9

rawDescription: Count number of elements in each row in 2D list


code:
list_input = [('a', 'b'), ('a', 'c'), ('a', 'd'), ('z', 'b'), ('z', 'e')]
[second for first, second in list_input if first == 'a']

rawDescription: List comprehension python and specific item


code:
x = u'\xf9\xec\xe5\xed'
x.encode('latin-1').decode('cp1255')

rawDescription: Fixing faulty unicode strings

description: Fixing faulty unicode strings


code:
# Create minimal Spark context.
sc = SparkContext(appName="ScriptWithIntegratedConfig")

# It's critical to initialize configurator so any
# new org.apach.hadoop.Configuration object loads our resources.
sc._jvm.com.wellcentive.nosql.Configurator.init()

rawDescription: Custom Hadoop Configuration for Spark from Python (PySpark)?


code:
from google.appengine.api import urlfetch

file_name = 'image.jpg'
url = 'http://example.com/%s' % file_name
result = urlfetch.fetch(url)
if result.status_code == 200:
    doSomethingWithResult(result.content)

rawDescription: Uploading an Image from an external link to google cloud storage using google app engine python


code:
DF_dict = {s_id :
          {c_id : df[(df.s_id == s_id) & (df.c_id == c_id)] for c_id in df[df.s_id == s_id]['c_id'].unique()}
          for s_id in df.s_id.unique()}

rawDescription: Split Pandas Dataframe into separate pieces based on column values


code:
from django.views.generic import FormView
from django.core.urlresolvers import reverse_lazy

class PackOrdersView(FormView):
    form_class = PackOrders

    success_url = reverse_lazy('orders') # we can't use normal reverse here, because it will be evaluated before urlconf is parsed

    def form_valid(self, form):

        # your code here

        return super(PackOrdersView, self).form_valid(form)

rawDescription: django return same page on clicking button

description: Django return same page on clicking button


code:
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        do_the_login()
    else:
        show_the_login_form()

rawDescription: RESTful Python for Java (Jersey) developer


rawDescription: Merging two dictionaries while keeping the original


code:
veggie = Pizza()
veggie.toppings.add(Topping.objects.create(name='Olives'))
veggie.toppings.add(Topping.objects.create(name='Peppers'))
veggie.save()

rawDescription: How does a model relate to another model via Foreignkey?


code: import wx.lib.mixins.listctrl as listmix

rawDescription: WxPython ListCtrl index 0 item non editable


code:
KRB_AP_REQ = '\x01\x00'
KRB_AP_REP = '\x02\x00'
KRB_ERROR = '\x03\x00'
class Example(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('spam', univ.Integer()),
        namedtype.NamedType('eggs', univ.Any()),
        namedtype.NamedType('ham', univ.Any()),
)

example = Example()
example['spam'] = 42
example['eggs'] = KRB_AP_REQ
example['ham'] = encoder.encode(univ.Integer(24))
print(example.prettyPrint())
substrate = encoder.encode(example)
data, tail = decoder.decode(substrate, asn1Spec=Example())
print(data.prettyPrint())
data['eggs'].asOctets()
data['eggs'].asNumbers()
example['eggs'] == KRB_AP_REQ

rawDescription: Decode using ASN.1 where substrate contains some opaque data


rawDescription: Endpoints Proto Datastore - Path {requirement} variable used for keys randomly breaks


code:
setup(...,
      scripts=['scripts/mrbob']
      )

rawDescription: How can one enable a shell command line argument for a python package installed for pip?


code:
item = 5
for elem in dis:
    dis[elem].append(item)

rawDescription: PYTHON - creating multiple lists in a loop


code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Users\\chromedriver.exe", chrome_options=options)

rawDescription: How do you use credentials saved by the browser in auto login script in python 2.7?

description: Use credentials saved by the browser in auto login script in python 2.7


code:
#write to excel, omit index of DataFrame
df.to_excel('test.xlsx', index=False)

rawDescription: using pandas.to_csv(df) only displays a subset of numpy.array in Excel

description: Using pandas.to_csv(df) only displays a subset of numpy.array in Excel


code:
from scipy.sparse import coo_matrix

def reshape(a, shape):
    """Reshape the sparse matrix `a`.

    Returns a coo_matrix with shape `shape`.
    """
    if not hasattr(shape, '__len__') or len(shape) != 2:
        raise ValueError('`shape` must be a sequence of two integers')

    c = a.tocoo()
    nrows, ncols = c.shape
    size = nrows * ncols

    new_size =  shape[0] * shape[1]
    if new_size != size:
        raise ValueError('total size of new array must be unchanged')

    flat_indices = ncols * c.row + c.col
    new_row, new_col = divmod(flat_indices, shape[1])

    b = coo_matrix((c.data, (new_row, new_col)), shape=shape)
    return b

rawDescription: Reshape sparse matrix efficiently, Python, SciPy 0.12


code: self.after(5, lambda y=y: self.move_in_from_bottom(y))

rawDescription: Python Tkinter - Toplevel MoveInAnimation


code: set(main_array) - set(second_array)

rawDescription: Python, remove all occurrences of string in list


code:
stocks['AIG'][0]
stocks['AIG'][1]

rawDescription: What is the correct way to store data N with associated properties if I've 100s of N to track?


code:
def is_printable(c):
    return not unicodedata.category(c).startswith('C')

rawDescription: Python, "filtered" line editing, read stdin by char with no echo


code:
def flatten(aList):
    t = []
    for i in aList:
        if not isinstance(i, list):
             t.append(i)
        else:
             t.extend(flatten(i))
    return t

rawDescription: Flatten an irregular list of lists in Python recursively


code: from blist import sortedsed,sortedlist

rawDescription: Python can't import blist classes

description: Python can't import blist classes


code:
def limit_health(health):
    if health < 0:
        health = 0
    if health > 100:
        health = 100
    return health

health = limit_health(150)

rawDescription: Stuck on a simple if statement Python 3

description: Stuck on a simple if statement Python 3


code:
from operator import add
numpy.fromfunction(add, (5, 5))

rawDescription: Are there alternative way to manage value assignment of n-dim array/matrix/list in Python?


code: foo("I'm passing this string literal to a function")

rawDescription: String literal with triple quotes in function definitions


rawDescription: Writing lists of data to a text file column by column


rawDescription: Iterate over all possible combinations of list elements and use as indices in Python


code:
import numpy as np
import pyximport
pyximport.install(setup_args={'include_dirs': [np.get_include()]})

open('_tmp.pyx', 'w').write('''
cimport numpy as np
cpdef function(np.ndarray[np.float32_t, ndim=2] response, max_loc):
    cdef unsigned int p_one, q_one
    p_one = int(max_loc[0])
    q_one = int(max_loc[1])
    p_two = int(max_loc[0])
    q_two = int(max_loc[1])
    r_one = response[q_one, p_one]
    r_two = response[q_two, p_two]
''')

import _tmp
assert(hasattr(_tmp, 'function'))

rawDescription: Cython: unsigned int indices for numpy arrays gives different result


code: [i for i, v in sorted(enumerate(A), key=lambda iv: iv[1])]

rawDescription: Swapping indices with values in a Python list?


code:
reg  = PanelOLS(y=df['y'],x=df[['x']],time_effects=True)
reg

rawDescription: Fixed effect in Pandas or Statsmodels

description: Fixed effect in Pandas or Statsmodels


code:
def slice_indices(l, n):
    q, r = divmod(l, n)
    step = q + 1 if r else q
    return [(i, min(i+step, l)) for i in range(0, l, step)]

rawDescription: Splitting a list into a list of block indices


code:
B = nx.to_numpy_matrix(G)
plt.matshow(B)
plt.colorbar()
plt.show()

rawDescription: how to create weighted 2D array from networkx object in python

description: Create weighted 2D array from networkx object in python


rawDescription: Dividing a string by type of data inside each line of file in python


code:
import os
import numpy as np

def create_data(N):
    data = np.random.random(size=N)*86400
    hours, remainder = data.__divmod__(3600)
    minutes, seconds = remainder.__divmod__(60)
    mag = np.arange(N)
    filename = os.path.expanduser('~/tmp/data')
    with open(filename, 'w') as f:
        for h,m,s,a in np.column_stack([hours, minutes, seconds, mag]):
            f.write('{h:d}:{m:d}:{s:.6f} {a}\n'.format(h=int(h), m=int(m), s=s, a=a))

def parse_date(datestr):
    return sum([multiplier*val for multiplier, val in
                zip((3600, 60, 1), map(float, datestr.split(':')))])

def using_converter():
    x = np.loadtxt('data', dtype={'names': ('time', 'magnitude'),
                                  'formats': ('f8', 'f8')},
                   converters={0:parse_date})
    return x

def using_fromiter():
    x = np.loadtxt('data', dtype={'names': ('time', 'magnitude'), 'formats': ('S12', 'f8')})
    arr = np.char.split(x['time'], ':')
    newarr = np.fromiter((tuple(row) for row in arr), dtype=[('', np.float)]*3,
                         count=len(arr)).view('float').reshape(-1, 3)
    times = (newarr * [3600,60,1]).sum(axis=1)

    y = np.empty_like(x, dtype={'names': ('time', 'magnitude'), 'formats': ('f8', 'f8')})
    y['time'] = times
    y['magnitude'] = x['magnitude']
    return y

create_data(10**6)

rawDescription: Parsing a data matrix containing HH:MM:SS.mmm times using numpy.loadtxt


code: [a for a in list1+list2 if (a not in list1) or (a not in list2)]

rawDescription: Comparing two lists and only printing the differences? (XORing two lists)


rawDescription: Assign array to key dictionary in python


code:
charset = 'abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY'
all(c in charset for c in word)

rawDescription: Has a better way to check word?

description: Has a better way to check word


code:
with open("jobs_done.txt") as inp:
    job_done_count = int(inp.read().strip())

rawDescription: implement a global configure in Python

description: Implement a global configure in Python


code:
class PhotoUrls(models.Model):
    class Meta:
         managed = False
         db_table = 'photo_urls'
    gallery_id = models.IntegerField()
    photo_url = models.CharField()

rawDescription: How can I improve this many-to-many Django ORM query and model set?

description: Improve this many-to-many Django ORM query and model set


code:
if success:
    return HttpResponse('
SUCCESS
')

rawDescription: Load document returned by AJAX Call

description: Load document returned by AJAX Call


code:
import json

outpath = 'out.json'
with open(outpath, "w") as f:
    f.write(json.dumps(data))

rawDescription: Python save arbitrarily nested list to CSV


code: correct = sum(x == y for x, y in zip(exam_solutions, student_answers))

rawDescription: compare the contents of two lists

description: Compare the contents of two lists


code:
sl1 = set(s1)
l1 + [el2 for el2 in l2 if el2 not in sl1]
sl2 = set(s2)
[el1 for el1 in l1 if el1 not in sl2]

rawDescription: Subtract and add two lists without changing their order in Python


rawDescription: Access matrix by list of matrix indices in Numpy


rawDescription: Python: Project Euler 145

description: Python: Project Euler 145


code:
# english.py
def greet(): return 'Hello!'

# italian.py
def greet(): return 'Ciao!'

# french.py
def greet(): return 'Salut!'

# config.py
langname = 'italian'

# startit.py
import config
import sys
lang = __import__(config.langname)
sys.modules['lang'] = lang

rawDescription: How could you swap out a particular database implementation in python?


code:
value = info.findNext("b")
value = value if value else "Oompa Loompa"

rawDescription: Compressing `x if x else y` statement in Python


code:
member = session.query(Member).filter(Member.name == membername).one()
session.delete(member)

rawDescription: How to do a paged query with empty collections in SQLAlchemy?

description: Do a paged query with empty collections in SQLAlchemy


code:
with open('infile') as f, open('outfile') as w:
    for line in f:
        for i in line.split(','):
            if 'Blue' in i:
                break
        else:
            w.write(line)

rawDescription: Python extract certain rows

description: Python extract certain rows


code:
def original_app(arr,df):
    for index, row in df.iterrows():
        arr[arr == row['country_codes']] = row['continent_codes']

def vectorized_app1(arr,df):
    oldval = np.array(df['country_codes'])
    newval = np.array(df['continent_codes'])
    mask = np.in1d(arr,oldval)
    idx = np.searchsorted(oldval,arr.ravel()[mask])
    arr.ravel()[mask] = newval[idx]

def vectorized_app2(arr,df):
    oldval = np.array(df['country_codes'])
    newval = np.array(df['continent_codes'])
    left_idx = np.searchsorted(oldval,arr,'left')
    right_idx = np.searchsorted(oldval,arr,'right')
    mask = left_idx!=right_idx
    arr[mask] = newval[left_idx[mask]]

rawDescription: Replace values in numpy 2D array based on pandas dataframe


code: sum(myList[0], [])

rawDescription: How to print list of list into one single list in python without using any for or while loop?

description: Print list of list into one single list in python without using any for or while loop


rawDescription: How to execute two loop simultaneously in python?

description: Execute two loop simultaneously in python


code: isinstance(fn, collections.Callable)

rawDescription: python 3: how to check if an object is a function?

description: Python 3: how to check if an object is a function


code:
def json_to_series(text):
    keys, values = zip(*[(dct['label'], dct['value']) for dct in json.loads(text)])
    return pd.Series(values, index=keys)

rawDescription: Pandas expand json field across records

description: Pandas expand json field across records


code:
import ast

data = ast.literal_eval(r.text[19:-1])

rawDescription: Parse JSON from response with python requests


code:
from pyspark.sql.functions import max

(df
    .groupBy("Year", "CountryCode", "IndicatorCode")
    .agg(max("Value").alias("Value"))
    .groupBy("Year", "CountryCode")
    .pivot("IndicatorCode", columns)
    .max("Value"))

rawDescription: Spark 1.6 DataFrame pivot funtion

description: Spark 1.6 DataFrame pivot funtion


rawDescription: Python: how to revise tuples in an list?

description: Python: how to revise tuples in an list


rawDescription: Unable to remove additional double quotes in json string added by python


code:
while i < len(sc):
    curr = sc[i]
    i += 1
    if not(curr in string.digits):
        num = 1 if start + 1 == i else int(sc[start:i-1])
        chars.append("".join(curr * num))
        start = i

rawDescription: How can I analyze or improve my niece's simple compression algorithm that is based on Morse code?

description: Analyze or improve my niece's simple compression algorithm that is based on Morse code


code: print("Your new health is %s" % dmgCheck())

rawDescription: Python - Get a function to overwrite a variable


code:
one = [1, 2, 3]
two = None
len(one or [])
len(two or [])

rawDescription: Is there a Django function to count objects even on None objects?


code: zip(zip(*list1)[0], list2, list3)

rawDescription: Python, Write new line to CSV using columns from multiple lists


code: my_func('hello', c='world')

rawDescription: Python newbie: What does this code do?

description: Python newbie: What does this code do


code: your_array[..., np.newaxis]

rawDescription: forcing genfromtxt output to no-vector

description: Forcing genfromtxt output to no-vector


rawDescription: Pygtk StatusIcon not loading?

description: Pygtk StatusIcon not loading


code:
cells_and_enzymes = cells & enzymes
cells_and_biochemicals = cells & biochemicals

rawDescription: Improve efficiency in Python matching

description: Improve efficiency in Python matching


code:
if x in {1, 2, 3}:
    pass # do something

rawDescription: Using lists and tuples in Python if statements


rawDescription: Python regex to avoid matching word followed by multiple conditions


code:
while True:
    option = input('What Units Are You Using? (Q to Quit) ').lower()
    if option in ['millimeters' , 'mm']:
        a = int(input('Enter Your number: '))
        b, c = a / 10, a / 1000
        print('Okay- Heres what we got: {} centimeters(cm) and {} meters (m)'.format(b, c))

rawDescription: How to Get Multiple 'Keys' For a Command

description: Get Multiple 'Keys' For a Command


code:
_file = re.sub(r'''(?x)BEGIN COPYRIGHT
                   (?:(?!(?:END|BEGIN) COPYRIGHT)[\w\s])*
                   END COPYRIGHT''',_copyright, _file)

rawDescription: Replacing a string between two strings on more then one occation with regex, python


code:
try:
   title = foo.bar.baz.qux
except AttributeError:
   title = None

rawDescription: How to chain attribute lookups that might return None in Python?

description: Chain attribute lookups that might return None in Python


code:
def __init__(self, **flags):
    self.flags = flags

rawDescription: Is there a shorthand to create member variables from function arguments in python?


rawDescription: Python 3.5.0 decimal

description: Python 3.5.0 decimal


code:
X.sum_duplicate() # make sure, not sure if this happens with normal usage.
old_data = X.data.copy()
X.data **= 2
squared_sum = X.sum(1)
X.data = old_data

rawDescription: element-wise operations on a sparse matrix

description: Element-wise operations on a sparse matrix


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

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


rawDescription: pandas merging based on a timestamp which do not match exactly

description: Pandas merging based on a timestamp which do not match exactly


code: # [' with text ']

rawDescription: Get pattern text that is within search string


code:
def set_value(self, member, value):
    if member == "a":
        self.a = value
    elif member == "b":
        self.b = value

rawDescription: Passing an Python Class Attribute Given a String


code:
a = UserInput.create_new_user("foo", listc=...) # all others are left empty
b = UserInput("bar") # all are left empty
c = UserInput.create_new_user("ham", lista=..., listd=...) # all others are left empty

rawDescription: Applying the @staticmethod, python3

description: Applying the @staticmethod, python3


rawDescription: how to append to a dictionary values from another dictionary if keys match

description: Append to a dictionary values from another dictionary if keys match


code:
class Block:
    def __init__(self):
        col1 = randint(1, 255)
        col2 = randint(1, 255)
        col3 = randint(1, 255)
        self.block_color = (col1, col2, col3)

rawDescription: Implementing an ever changing color system in pygame


code:
myvars = {}
with open("namelist.txt") as myfile:
    for line in myfile:
        name, var = line.partition("=")[::2]
        myvars[name.strip()] = float(var)

rawDescription: Parse key value pairs in a text file

description: Parse key value pairs in a text file


rawDescription: Show week 53 as yearweek in Python

description: Show week 53 as yearweek in Python


code:
def as_python_object(dct):
    if '_python_object' in dct:
        return pickle.loads(str(dct['_python_object']))
    return dct

pyobj = json.loads(json_str, object_hook=as_python_object)

rawDescription: Making object JSON serializable with regular encoder


code:
df1 = df.set_index(['col1', 'col2', 'session_number'])
df1

rawDescription: Pandas Dataframe split in to sessions

description: Pandas Dataframe split in to sessions


code:
lst = [2, 5, 7, 3, 8]
indexes = 1, 3, 4
indexes = set(indexes)  # `x in set` is faster than `x in tuple/list`
[x for i, x in enumerate(lst) if i not in indexes]

rawDescription: Removing indexes from a list

description: Removing indexes from a list


code: img[img < 100]

rawDescription: Making Pixel Histogram.ValueError: The truth value of an array with more than one element is ambiguous


rawDescription: Numpy savetxt heterogenous data

description: Numpy savetxt heterogenous data


code: closest_match = by_closeness[0]

rawDescription: Finding a string in a list and returning a secondary value with python


rawDescription: counting the number of samples in a numpy array

description: Counting the number of samples in a numpy array


code:
A_revsum = cumsum2_reverse(A)
A_revsum

rawDescription: Inverse cumsum for numpy

description: Inverse cumsum for numpy


rawDescription: Getting XML attribute value with lxml module


code:
import pandas as pd
import numpy as np
import csv
import time
import glob
import datetime
import re
import sys
import os
from dateutil import relativedelta
from xlsxwriter.utility import xl_rowcol_to_cell

'''This is where I find the file with the compiled data and add the needed columns to the df'''
###  File Finding Stuff
file_names = sorted(glob.glob(r'T:\Tom\Scripts\\' + '*_fillssideclient.csv'), reverse=True)
file = file_names[0]
date = os.path.basename(file)[0:8]
#file = "20151215_fillssideclient.csv"  ### For manual file pulls
df = pd.read_csv(file)

###  Column Renaming, NaN Replacing & DataFrame Column Additions
df.rename(columns={'provider':'Providers'}, inplace=True)
df = df.replace(np.nan,'All Tags', regex=True)
df['five_avg'] =    df.iloc[:, 30:40].sum(axis=1).astype('int64') / 10          #Added column at end of df for 5s avg
df['ten_avg'] =     df.iloc[:, 30:50].sum(axis=1).astype('int64') / 20          #Added column at end of df for 10s avg
df['twenty_avg'] =  df.iloc[:, 30:70].sum(axis=1).astype('int64') / 40          #Added column at end of df for 20s avg

col = [['customer_account', 'CounterPartyID'],
       ['customer_account', 'CounterPartyID', 'symbol'],
       ['customer_account', 'CounterPartyID', 'Providers', 'symbol'],
       ['Providers', 'customer_account'],
       ['Providers', 'symbol']]

#   This is where I find the output location, declare my 'groupby' variables and execute the script
writer = pd.ExcelWriter(date + '_counterparty_monthly.xlsx', engine='xlsxwriter')

for i, col in enumerate(col):
    #print col
    #print i
    grouped = df.groupby(col)

    sheetname = 'All Trades-' + str(i)
    #print sheetname

    #This is the primary function that I need to have my 5 'groupby' variables loop through and create 5 sheets'''
    ###  Primary DataFrame Calculations
    filled_total =      df['filled'].sum()
    order_total =       grouped['filled'].count()
    total_tickets =     grouped['filled'].sum()
    share =             total_tickets / filled_total
    fill_rate =         total_tickets / order_total
    total_size =        grouped['fill_size'].sum()
    avg_size =          total_size / total_tickets

    ###  One Second Calculations
    one_toxicity =      grouped.apply(lambda x: x['filled'][x['1000'] < -25].sum()) / total_tickets
    one_average =       grouped.apply(lambda x: x[x['filled'] == 1]['1000'].mean())
    one_low =           grouped.apply(lambda x: x[x['filled'] == 1]['1000'].quantile(.25))
    one_med =           grouped.apply(lambda x: x[x['filled'] == 1]['1000'].quantile(.50))
    one_high =          grouped.apply(lambda x: x[x['filled'] == 1]['1000'].quantile(.75))

    ###  Five Second Calculations
    #five_toxicity =    grouped.apply(lambda x: x['filled'][x['5000'] < -25].sum()) / total_tickets
    five_average =      grouped.apply(lambda x: x[x['filled'] == 1]['five_avg'].mean())
    five_low =          grouped.apply(lambda x: x[x['filled'] == 1]['five_avg'].quantile(.25))
    five_med =          grouped.apply(lambda x: x[x['filled'] == 1]['five_avg'].quantile(.50))
    five_high =         grouped.apply(lambda x: x[x['filled'] == 1]['five_avg'].quantile(.75))
    #five_std =         grouped.apply(lambda x: x[x['filled'] == 1]['five_avg'].std())

    ###  Ten Second Calculations
    #ten_toxicity =     grouped.apply(lambda x: x['filled'][x['10000'] < -25].sum()) / total_tickets
    ten_average =       grouped.apply(lambda x: x[x['filled'] == 1]['ten_avg'].mean())
    ten_low =           grouped.apply(lambda x: x[x['filled'] == 1]['ten_avg'].quantile(.25))
    ten_med =           grouped.apply(lambda x: x[x['filled'] == 1]['ten_avg'].quantile(.50))
    ten_high =          grouped.apply(lambda x: x[x['filled'] == 1]['ten_avg'].quantile(.75))
    #ten_std =          grouped.apply(lambda x: x[x['filled'] == 1]['ten_avg'].std())

    ###  Twenty Second Calculations
    #twenty_toxicity =  grouped.apply(lambda x: x['filled'][x['20000'] < -50].sum()) / total_tickets
    twenty_avg =        grouped.apply(lambda x: x[x['filled'] == 1]['twenty_avg'].mean())
    twenty_low =        grouped.apply(lambda x: x[x['filled'] == 1]['twenty_avg'].quantile(.25))
    twenty_med =        grouped.apply(lambda x: x[x['filled'] == 1]['twenty_avg'].quantile(.50))
    twenty_high =       grouped.apply(lambda x: x[x['filled'] == 1]['twenty_avg'].quantile(.75))
    #twenty_std =       grouped.apply(lambda x: x[x['filled'] == 1]['twenty_avg'].std())

    ###  Column Formatting
    #comma_fmt =        workbook.add_format({'num_format': '#,##0'})
    #money_fmt =        workbook.add_format({'num_format': '$#,##0.000'})
    #percent_fmt =      workbook.add_format({'num_format': '0.0%'})
    #Still need to figure out how to customize column width, column format and conditional formatting'''
    list_of_lists = [
        ['Trades',      total_tickets],
        ['Share %',     share],
        ['Fill Rate',   fill_rate],
        ['Total Size',  total_size],
        ['Avg Size',    avg_size],
        ['1s Toxic',    one_toxicity],
        ['1s Avg',      one_average],
        ['1s 25th',     one_low],
        ['1s 50th',     one_med],
        ['1s 75th',     one_high],
        ['5s Avg',      five_average],
        ['5s 25th',     five_low],
        ['5s 50th',     five_med],
        ['5s 75th',     five_high],
        ['10s Avg',     ten_average],
        ['10s 25th',    ten_low],
        ['10s 50th',    ten_med],
        ['10s 75th',    ten_high],
        ['20s Avg',     twenty_avg],
        ['20s 25th',    twenty_low],
        ['20s 50th',    twenty_med],
        ['20s 75th',    twenty_high]
    ]
    result =            pd.concat([lst[1] for lst in list_of_lists], axis=1)
    result.columns =    [lst[0] for lst in list_of_lists]
    result =            result[result.Trades > 0]           # Removes results that are less than 1...use '!= 0' to remove only 0 trades

    result.to_excel(writer, sheet_name=sheetname)
    workbook = writer.book

    #add sheet name
    worksheet = writer.sheets[sheetname]
    worksheet.set_zoom(80)

    #Worksheet and Print Options
    worksheet.hide_gridlines(2)
    worksheet.fit_to_pages(1, 1)

writer.save()

rawDescription: Loop through a pandas dataframe with multiple groupby functions and write to excel


code:
commandOptionsAndArguments = ['myBigTool', '--num-callers', '30', '--leak-check', 'full', '--tool', 'memcheck', '--suppressions', 'etc/valgrind-root.supp', '--suppressions', 'Gaudi.supp/Gaudi.supp', '--suppressions', 'oracleDB.supp', '--suppressions', 'valgrindRTT.supp', '--suppressions', 'root.supp/root.supp', '--mySpecialFlag', '$(which python)', '$(which athena.py)', 'athenaConf.pkl']
re.split(r'\s+(?![^()]*\))', re.sub(r'(--\S+)\s+(\w\S+)', r'\1=\2', ' '.join(commandOptionsAndArguments)))

rawDescription: how to combine the elements of a list representing the elements of a Bash command to a new list featuring the equals symbol

description: Combine the elements of a list representing the elements of a Bash command to a new list featuring the equals symbol


code: date_b = date_a - datetime.timedelta(days=2, minutes=30)

rawDescription: How to subtract datetime with multiple choice values

description: Subtract datetime with multiple choice values


rawDescription: how to write regular expression replaces zero with empty string and make sure that there is not leading digits

description: Write regular expression replaces zero with empty string and make sure that there is not leading digits


rawDescription: Shift elements in a numpy array

description: Shift elements in a numpy array


code:
foo = type(42)
foo

rawDescription: Understanding Python Attributes and Methods


code:
def create_room(room):
    global map
    #set all tiles inside the room to passable

    # limit room coordinates. This is free of additional handlings but
    # might create odd rooms in the corner of the map.
    max_x, max_y = len(map) - 1, len(map[0]) - 1
    for x in range(room.x1 + 1, room.x2):
        for y in range(room.y1 + 1, room.y2):
            map[min(max_x, x)][min(max_y, y)].blocked = False
            map[min(max_x, x)][min(max_y, y)].block_sight = False

rawDescription: IndexError: list index out of range

description: IndexError: list index out of range


code:
# in e.g. myapp/forms.py
from django.contrib.auth.forms import PasswordResetForm

class CaptchaPasswordResetForm(PasswordResetForm):
    captcha = ReCaptchaField()
    ...

rawDescription: django customize reset password form

description: Django customize reset password form


code: gridTL = [gridlist[m[0]][m[1]]for m in section]

rawDescription: Turning a list of coordinates into a set of list/grid references


code:
import datetime
datetime.datetime(*ast.literal_eval('(2011, 12, 11, 15, 45, 20)'))

rawDescription: Python datetime formatted string to datetime object


rawDescription: Regular Expressions in data parsing Python


code:
zerovalue = np.where(mock_data[dummy] == 0)[0]
# array([  0, 189, 470, 491])

rawDescription: np.where() not returning expected indices in numpy python

description: Np.where() not returning expected indices in numpy python


code:
df
df.reset_index(['Cat2'])

rawDescription: convert sublevel into a single column

description: Convert sublevel into a single column


rawDescription: Find and append each reference to a html link - Python


code:
profile = webdriver.FirefoxProfile(path_to_profile_in_your_pc)
webdriver.Firefox(profile)

rawDescription: Setting default setting to 'no proxy' in Selenium Firefox


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

rawDescription: writing csv output python

description: Writing csv output python


rawDescription: moving average of 3 elements by C or Python

description: Moving average of 3 elements by C or Python


rawDescription: Python PANDAS write to csv: how to set decimal point (".", or ",")?


rawDescription: Dividing a list into two lists

description: Dividing a list into two lists


code: return 'window.location = "%s"' % URL('default', 'index')

rawDescription: How to dynamically check logged-in state from View in web2py

description: Dynamically check logged-in state from View in web2py


code:
def to_dict(self):
     retval = dict(identification=self.identifier,
                   information=self.information)
     if self.children:
         retval['nodes'] = {e.position: e.to_dict for e in self.children}
     return retval

rawDescription: optional dictionary items in dict creation

description: Optional dictionary items in dict creation


code: agreement = np.sum(a == b) / a.size

rawDescription: percentage difference between two images in python using correlation coefficiant

description: Percentage difference between two images in python using correlation coefficiant


rawDescription: Variable value is determined by function call


rawDescription: Sending Hex data

description: Sending Hex data


code:
import clr
clr.AddReferenceToFile('ConfigurationProxy.dll')

from IronPythonUtilities import ConfigurationProxy

def override(filename):
    proxy = ConfigurationProxy(filename)
    return proxy.InjectToConfigurationManager()

rawDescription: How to use IronPython with App.Config?

description: Use IronPython with App.Config


code:
for a_byte in se:
    print (bin(a_byte))

rawDescription: How to see the bytes of a Python3 <class 'bytes'> object?

description: See the bytes of a Python3 <class 'bytes'> object


rawDescription: Search patern to include square brackets


code:
def private_only(route):
    def wrapper(*args, **kwargs):
        if IPy.IP(bottle.request.remote_addr).iptype() == 'PRIVATE':
            return route(*args, **kwargs)
        else:
            return "Not allowed!"
    return wrapper

rawDescription: How to filter IP addresses by route in bottle?

description: Filter IP addresses by route in bottle


code:
import itertools

itertools.bongos = 'BONGOS'

rawDescription: What is a simple, yet proper, way to add a container to a pre-exisiting object in python?


code:
import subprocess

reply = int(raw_input("Enter 1, 2 or 3: "))
for item in [itroom, second, csupport][reply - 1]:
    subprocess.check_call([r'c:\delprof2\DelProf2.exe', '/l', '/c:' + item])

rawDescription: How do you add mutiple arguments to a subprocess line?

description: Add mutiple arguments to a subprocess line


code:
class MyZipFile(zipfile.ZipFile): # Create class based on zipfile.ZipFile
  def __init__(file, mode='r'): # Initial part of our module
    zipfile.ZipFile.__init__(file, mode) # Create ZipFile object

  def __enter__(self): # On entering...
    return(self) # Return object created in __init__ part
  def __exit__(self, exc_type, exc_val, exc_tb): # On exiting...
    self.close() # Use close method of zipfile.ZipFile

rawDescription: where is the __enter__ and __exit__ defined for zipfile?

description: Where is the __enter__ and __exit__ defined for zipfile


rawDescription: Python code for counting number of zero crossings in an array


code:
def addtrans():
    ...
    # check that both users exist
    if not services.user_exists(to_id) or not services.user_exists(from_id):
        return "no such users"
    ...

rawDescription: How to mock the service layer in a python (flask) webapp for unit testing?

description: Mock the service layer in a python (flask) webapp for unit testing


code:
arr2 = np.arange(4).reshape(2,2) # shape (2, 2)
np.atleast_2d(arr2)
_.shape

rawDescription: Python: How can I force 1-element NumPy arrays to be two-dimensional?


code:
def start_requests(self):
    requests = []

    for i, account in enumerate(self.crawler.settings['MY_ACCOUNTS']):
        request = FormRequest('http://example.com/login',
            formdata={'form_login_name': account['login'], 'form_pwd_name': account['pwd']},
            callback=self.parse,
            dont_filter=True)

        request.meta['cookiejar'] = i
        requests.append(request)

    return requests

rawDescription: Scrap multiple accounts aka multiple logins


code:
arr_x = arr[:, :, np.newaxis, np.newaxis, :] # shape (200, 600, 1, 1, 20)
arr_y = arr[np.newaxis, np.newaxis, :, :, :] # shape (1, 1, 200, 600, 20)

rawDescription: Iterate over all pairwise combinations of numpy array columns