code:
a = input("string1 :")
b = input("string2: ")
list1 = []

for i in a:
    for j in b:
        if i == j and i not in list1:
            list1.append(i)
            break

print(list1)

rawDescription: Finding a substring within a string using an array?


code:
def class2(t, beta):
    return alpha*np.sin(t+beta)*np.sin(3*t), alpha*np.cos(t+beta)*np.sin(3*t)

rawDescription: Parametric equation with numpy

description: Parametric equation with numpy


rawDescription: Pandas Dataframe groupby Display

description: Pandas Dataframe groupby Display


code: return count > self.count

rawDescription: PhantomJS acts differently than Firefox webdriver


rawDescription: Python decorator with optional argument (which is function)


code:
for result in json_data['results']:
    print(result['author']['objectId'])

rawDescription: Parsing the pointers in JSON using python


code:
seen = set()
result = []
for item in l:
    if item not in seen:
        seen.add(item)
        result.append(item)

result

rawDescription: Grab unique tuples in python list, irrespective of order


code:
def sysexec(self, *argv, **popen_opts):
    """ return stdout text from executing a system child process,
        where the 'self' path points to executable.
        The process is directly invoked and not through a system shell.
    """
    from subprocess import Popen, PIPE
    argv = map_as_list(str, argv)
    popen_opts['stdout'] = popen_opts['stderr'] = PIPE
    proc = Popen([str(self)] + argv, **popen_opts)
    stdout, stderr = proc.communicate()
    ret = proc.wait()
    if py.builtin._isbytes(stdout):
        stdout = py.builtin._totext(stdout, sys.getdefaultencoding())
    if ret != 0:
        if py.builtin._isbytes(stderr):
            stderr = py.builtin._totext(stderr, sys.getdefaultencoding())
        raise py.process.cmdexec.Error(ret, ret, str(self),
                                       stdout, stderr,)
    return stdout

rawDescription: Python - How to use py.path.LocalPath.sysexec()


code:
import bisect
class DictRange(dict):

    def __setitem__(self, key, val):
        super(DictRange,self).__setitem__(key, val)
        self.ks = [key[0] for key in self.keys()]

    def __getitem__(self, val):
        return super(DictRange,self).__getitem__(self.keys()[bisect.bisect(self.ks, val) - 1])

rawDescription: Associative array in python with keys as a fragment of array


code:
from setuptools.command.bdist_egg import bdist_egg as _bdist_egg
import os, fnmatch, glob, shutil

def recursive_data_files(treeroot, pattern):
    results = []
    for base, dirs, files in os.walk(treeroot):
        goodfiles = fnmatch.filter(files, pattern)
        if goodfiles:
            results.append((base, [os.path.join(base, f) for f in goodfiles]))
    return results

def make_data_files(output='output'):
    return (
        [('', ['bread.yaml'])]
        + recursive_data_files(output, '*')
    )

class bdist_egg(_bdist_egg):
    def initialize_options(self):
        bake_bread()    # build files to './output'
        self.distribution.data_files = make_data_files()
        _bdist_egg.initialize_options(self)

topping_setup_options = dict(
    cmdclass={
        'bdist_egg': bdist_egg,
    },
    install_requires=[
        'Bread',
    ],
    zip_safe=False,
)

def cleanup_bread_bootstrap(root='.'):
    for f in glob.glob(os.path.join(os.path.abspath(root), '*.egg')):
        if os.path.isdir(f):
            shutil.rmtree(f)  # Egg directory
        else:
            os.remove(f)      # Zipped Egg

rawDescription: Using code from a dependency in setup.py


code:
result = {'value': {}}
result['value']['field1'] = value1
result['value']['field2'] = value2
result['value']['field3'] = value3

rawDescription: Why can't on_change refresh 2 fields is the same time?


rawDescription: Multiply each row of one array with each element of another array in numpy


code:
def invalidate(self):
    return_token = generate_auth_token()
    return {'token':return_token.decode()}, 200

rawDescription: Flask RESTful Destroy User Token

description: Flask RESTful Destroy User Token


rawDescription: String of bytes into an int

description: String of bytes into an int


code:
print(len(list(enumerate_rolls()))) # 36 + 6 * 36 - 6 = 246
A = list(enumerate_rolls(5, 4))
print(len(A)) # 4 ** 5 + 4 * 4 ** 5 - 4 = 5116
print(A[1020:1030]) # some double rolls (of five dice each!) and some single rolls

rawDescription: itertools dice rolls: doubles roll twice

description: Itertools dice rolls: doubles roll twice


code:
paragraphs = [para for para in c.paras()]
for para in paragraphs:
    words = [word for sentence in para for word in sentence]

rawDescription: How to extract words from sample corpus that comes with NLTK?

description: Extract words from sample corpus that comes with NLTK


code:
# once you've parsed the file, you need to **getroot()**
tree = ET.parse(StringIO('<root>' + s + '</root>')).getroot()

rawDescription: Parsing an XML file using ElementTree

description: Parsing an XML file using ElementTree


code:
def pickle_dict(d, filename):
    with open(filename, 'w') as f:
        pickle.dump(d, f)

def unpickle_dict(filename):
    with open(filename) as f:
        return pickle.load(f)

rawDescription: save and update a dictionary

description: Save and update a dictionary


code:
from functools import partial
def foo(bar, baz): print (bar, baz)

p = partial(foo, 1, 2)
p()
p = partial(foo, 1)
p(2)

rawDescription: Is there a way to append a function with arguments into a list?


rawDescription: Changing Vabinary(Max) back to text<MS SQL "2005">


code:
from itertools import islice

with open(filename) as f:
    first10000 = islice(f, 10000)

rawDescription: Is there a way to read 10000 lines from a file in python?


code:
account = splitting[0]
domain = splitting[1]

rawDescription: Filtering string in Python

description: Filtering string in Python


rawDescription: Python: finding the intersection point of two gaussian curves


code:
valgdata\
    .groupby([valgdata.dato_uden_tid.name, valgdata.news_site.name])\
    .mean()\
    .unstack()

rawDescription: Plotting multiple time series after a groupby in pandas


code:
# Consider each row as indexing tuple & get linear indexing value
lid = np.ravel_multi_index(x.T,x.max(0)+1)

# Store sorted indices for lid
sidx = lid.argsort()

# Append 1s at either ends of sorted and differentiated version of lid
mask = np.hstack((True,np.diff(lid[sidx])!=0,True))

# Perform convolution on it. Thus non duplicate elements would have
# consecutive two True elements, which could be caught with convolution
# kernel of [1,1]. Get the corresponding mask.
# Index into sorted indices with it for final output
out = x[sidx[(np.convolve(mask,[1,1])>1)[1:-1]]]

rawDescription: Get non-duplicate rows from numpy array

description: Get non-duplicate rows from numpy array


code:
class User(models.Model):
    ...

class BoardGame(models.Model):
    users = models.ManyToManyField(User, through='Vote')
    ...

class Vote(models.Model):
    user = models.ForeignKey(User)
    boardgame = models.ForeignKey(BoardGame)
    vote = models.IntegerField()

rawDescription: Two way table and django ORM

description: Two way table and django ORM


code: cbar.ax.set_yticklabels(['{:.0f}'.format(x) for x in np.arange(cbar_min, cbar_max+cbar_step, cbar_step)], fontsize=16, weight='bold')

rawDescription: matplotlib colorbar tick label formatting

description: Matplotlib colorbar tick label formatting


code:
# Counts of permutations.
perms = df.apply(tuple, axis=1).value_counts()

rawDescription: Creating and manipulating dataframes with only binary values


rawDescription: Theano element wise maximum

description: Theano element wise maximum


code:
from string import maketrans

flip_table = maketrans('01', '10')
def flip(s):
    return s.translate(flip_table)

rawDescription: Changing binary characters in string

description: Changing binary characters in string


code:
from collections import defaultdict

mapping = defaultdict(set)
for v1, v2 in zip(List1, List2):
    mapping[v2].add(v1)

rawDescription: Python mapping between lists for duplicates


rawDescription: how to apply functions to grouped dataframes in Python pandas?

description: Apply functions to grouped dataframes in Python pandas


code:
l = ['#Trending', '#Trending', '#TrendinG', '#Yax', '#YAX', '#Yax']
{v.lower(): v for v in l}.values()
{v.lower() for v in l}

rawDescription: How to get Case Insensitive Python SET

description: Get Case Insensitive Python SET


code:
def byte_to_char(input):
     return chr(int(input, base=2))

rawDescription: Binary to ASCII in Python

description: Binary to ASCII in Python


code:
a = random.choice(list1)
b = random.choice(list2)

rawDescription: Selecting a random list element in python


code:
from Tkinter import *

class Rectangle():
    def __init__(self, coords, color):
        self.coords = coords
        self.color = color

    def draw(self, canvas):
        """Draw the rectangle on a Tk Canvas."""
        canvas.create_rectangle(*self.coords, fill=self.color)

master = Tk()
w = Canvas(master, width=300, height=300)
w.pack()

rect1 = Rectangle((0, 0, 100, 100), 'blue')
rect1.draw(w)

mainloop()

rawDescription: Help Creating Python Class with Tkinter

description: Help Creating Python Class with Tkinter


code: soup.findAll(True, "post", text=re.compile(".*This post is about.*Google.com.*"))

rawDescription: How can I emulate ":contains" using BeautifulSoup?

description: Emulate ":contains" using BeautifulSoup


rawDescription: Making sinusoids from a wave file rectangular


code:
class Python:

    def __init__(self):
        self.process = subprocess.Popen("python -u", stdin=subprocess.PIPE,
                                         stdout=subprocess.PIPE,
                                         stderr=subprocess.PIPE)
        ...

rawDescription: Open a python process using python's subprocess module


rawDescription: Counting the Number of keywords in a dictionary in python


code:
import requests
import requests.auth

class HTTPProxyDigestAuth(requests.auth.HTTPDigestAuth):
    def handle_407(self, r):
        """Takes the given response and tries digest-auth, if needed."""

        num_407_calls = r.request.hooks['response'].count(self.handle_407)

        s_auth = r.headers.get('Proxy-authenticate', '')

        if 'digest' in s_auth.lower() and num_407_calls < 2:

            self.chal = requests.auth.parse_dict_header(s_auth.replace('Digest ', ''))

            # Consume content and release the original connection
            # to allow our new request to reuse the same one.
            r.content
            r.raw.release_conn()

            r.request.headers['Authorization'] = self.build_digest_header(r.request.method, r.request.url)
            r.request.send(anyway=True)
            _r = r.request.response
            _r.history.append(r)

            return _r

        return r

    def __call__(self, r):
        if self.last_nonce:
            r.headers['Proxy-Authorization'] = self.build_digest_header(r.method, r.url)
        r.register_hook('response', self.handle_407)
        return r

rawDescription: How to pass proxy-authentication (requires digest auth) by using python requests module

description: Pass proxy-authentication (requires digest auth) by using python requests module


code: {i:x for i,x in dup.iteritems() if len(x)>1}

rawDescription: Extract list of duplicate values and locations from array


code:
def diagonalize(A):
    eigenvalues_of_A, eigenvectors_of_A = numpy.linalg.eig(A);
    B = eigenvectors_of_A[:,abs(eigenvalues_of_A).argsort()]
    diagonal_matrix= B.I * A * B
    return diagonal_matrix

rawDescription: create module in python for diagonalizing matrix

description: Create module in python for diagonalizing matrix


code: filename.write("%s=%d" % (name, score))

rawDescription: Making a simple card game in python 2.7

description: Making a simple card game in python 2.7


code:
user_input = 'file write hi.txt {{ "" | RANDOM_INT(1, 10) }}'
template = env.from_string(user_input)
result = template.render()

rawDescription: Python 3 - Replacing part of a string with imformation from the string


code: my_array = my_array.reshape((50, 50))

rawDescription: Convert black and white array into an image in python?


code: f = csv.writer(open("prueba.csv", "a"))

rawDescription: How convert print in table csv beautiful soap


Boston Street Names

label: PRISCILLA ALDEN ROAD

label: VAN BUREN DRIVE

rawDescription: Replace object in json with another json file using python


code:
def place_holder(values):
    return '({})'.format(', '.join('?' * len(values)))

values = ['a', 'b', 'c']
ph = place_holder(values)
# (?, ?, ?)

rawDescription: Dynamically creating a placeholder to insert many column values for a row in SQLite table


code:
def dealer_replace(dealer_dict, text):

    regex = re.compile("(%s)" % "|".join(map(re.escape, dealer_dict.keys())))

    if regex.search(text):
        ret = regex.search(text)
        return dealer_dict[ret.group()]
    else:
        return None

rawDescription: python pandas use map with regular expressions

description: Python pandas use map with regular expressions


rawDescription: How to read a middle size-specified chunk of a file in Python

description: Read a middle size-specified chunk of a file in Python


code:
dat_unq = np.unique(dat)
first = dat_unq['id'].searchsorted(users, side='left')
last =  dat_unq['id'].searchsorted(users, side='right')
info = last - first

rawDescription: Counting number of unique values in subset of sorted array


code:
import json
import urllib
import urllib2

# make query
query = urllib.urlencode(dict(q="blue angel", rpp=5, include_entities=1,
                              result_type="mixed"))
# make request
resp = urllib2.urlopen("http://search.twitter.com/search.json?" + query)

# make dictionary (parse json response)
d = json.load(resp)

rawDescription: python asynchronous httprequest

description: Python asynchronous httprequest


code:
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

    def __str__(self, *args, **kwargs):
        return str(self.data)

def create_list(iterable):
    next_node = current_node = None
    for item in iterable:
        current_node = Node(item)
        current_node.next = next_node
        next_node = current_node
    return current_node

def size(head):
    count = 0
    while head:
        head = head.next
        count += 1
    return count

def print_list(head):
    while head:
        print(head, end="")
        if head.next:
            print(" > ", end="")
        head = head.next
    print(flush=True)
    pass

def value_at(head, index):
    while (head):
        if index < 1:
            return head
        index -= 1
        head = head.next
    return None

def append(head, value):
    while head:
        if not head.next:
            head.next = Node(value)
            return
        head = head.next

def concat(headA, headB):
    while headA:
        if not headA.next:
            headA.next = headB
            return
        headA = headA.next

def split(head):
    center = head
    index = 0
    while head:
        if index % 2:
            center = center.next
        head = head.next
        index += 1
    headB = center.next
    center.next = None
    return headB

def main():
    a = create_list([1, 2, 3, 4, 5, 6, 7, 8, 9])
    print("Print list::")
    print_list(a)
    print("\nSize:")
    print(size(a))
    print("\nValue at:")
    print("a[-1]: %d" % value_at(a, -1).data)
    print("a[0]: %d" % value_at(a, 0).data)
    print("a[1]: %d" % value_at(a, 1).data)
    print("a[5]: %d" % value_at(a, 5).data)
    print("a[8]: %d" % value_at(a, 8).data)
    # print("value @ 9 %d"% value_at(my_head,9).data)
    print("\nAppend (10):")
    print_list(a)
    append(a, 10)
    print_list(a)
    print("\nConcat a, b:")
    print_list(a)
    b = create_list([11, 12, 13])
    print_list(b)
    concat(a, b)
    print_list(a)
    print("\nSplit:")
    print_list(a)
    print("..into..")
    b = split(a)
    print_list(a)
    print("Size a: %d" % size(a))
    print_list(b)
    print("Size b: %d" % size(b))

if __name__ == "__main__":
    main()

rawDescription: Linked List Operations

description: Linked List Operations


rawDescription: Regex conditioning

description: Regex conditioning


code:
#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz  # $ pip install pytz

utc_offset = timedelta(hours=5, minutes=30)  # +5:30
names = set()
now = datetime.now(pytz.utc)
for tz in map(pytz.timezone, pytz.all_timezones_set):
    dt = now.astimezone(tz)
    tzinfos = getattr(tz, '_tzinfos',
                      [(dt.utcoffset(), dt.dst(), dt.tzname())])
    if any(off == utc_offset for off, _, _ in tzinfos):
        names.add(tz.zone)
print("\n".join(sorted(names)))

rawDescription: Getting timezone name from UTC offset

description: Getting timezone name from UTC offset


code:
from System.Diagnostics import Process
p = Process()
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = False
p.StartInfo.FileName = 'C:\\ping.bat'
p.Start()
p.WaitForExit()
print(p.ExitCode)

rawDescription: How to run batch file using IronPython

description: Run batch file using IronPython


code:
x = object()
sys.getrefcount(x)
y = x
sys.getrefcount(x)

rawDescription: Find all references to an object in python


code:
import pysftp
from mock import *
with patch("pysftp.Connection") as mock_connection:
    with pysftp.Connection("1.2.3.4", "user", "pwd", 12345) as sftp:
        sftp.get("filename")
    mock_connection.assert_called_with("1.2.3.4", "user", "pwd", 12345)
    sftp.get.assert_called_with("filename")

rawDescription: Asserting with..as clause in Python

description: Asserting with..as clause in Python


code:
import numpy as np

# Input arrays xyz and mass
xyz = np.array([
   [1, 0, 1],
   [1, 1, 0],
   [0, 1, 1],
   [0, 0, 0],
   [0, 1, 0],
   [1, 1, 0],
   [1, 0, 1],
   [0, 0, 1],
   [1, 0, 1],
   [0, 0, 1]])

mass = np.array([ 0.38668401,  0.44385111,  0.47756182,  0.74896529,  0.20424403,
    0.21828435,  0.98937523,  0.08736635,  0.24790248,  0.67759276])

# Convert each row entry in xyz into equivalent decimal numbers
dec_num = np.dot(xyz,2**np.arange(xyz.shape[1])[:,None])

# Get indices of the first occurrences of the unique values and also label each row
_, unq_idx,row_labels = np.unique(dec_num, return_index=True, return_inverse=True)

# Find unique rows from xyz array
xyz_unique = xyz[unq_idx,:]

# Accumulate the summations from mass based on the row labels
mass_unique = np.bincount(row_labels, weights=mass)

rawDescription: Find indices of unique values of a 3-dim numpy array


code:
point.x, point.y, point.z = (14, 22, 30)
# Tuple unpacking ... is there nothing it cannot do?

rawDescription: A superfluous but cool dynamic python structure and questions about decorators, comprehensions, syntax


code:
import csv
import itertools

csv_dict = {'label1': ['val1', 'val2', 'val3'],
            'label2': ['otherval1', 'otherval2'],
            'label3': ['yetanotherval1']}
keys = csv_dict.keys()
csvrows = itertools.izip_longest(*[csv_dict[k] for k in keys], fillvalue='dummy')

with open('out.csv', 'w') as csvfile:
    csvwriter = csv.writer(csvfile, delimiter=';',
                            quotechar='\\', quoting=csv.QUOTE_MINIMAL)
    csvwriter.writerow(keys)
    for row in csvrows:
        csvwriter.writerow(row)

rawDescription: python - Nested list in dict to csv files

description: Python - Nested list in dict to csv files


code:
def formatted(f): return format(f, '.2f').rstrip('0').rstrip('.')

formatted(0.0)
formatted(4.797)
formatted(4.001)
formatted(13.577)
formatted(0.000000000000000000001)
formatted(10000000000)

rawDescription: Most Pythonic way to print *at most* some number of decimal places


code: subnames = name.split() # split the name on whitespace

rawDescription: I need help on creating a name changing program


code:
class Wrapper (type):
    def __new__(mcls, name, bases, nmspc):
        for attrname, attrval in nmspc.items():
            if callable(attrval) and attrname.startswith('crawl_'):
                nmspc[attrname] = wrapit(attrval)

        return super(Wrapper, mcls).__new__(mcls, name, bases, nmspc)

rawDescription: How to get a method called (decorator?) after every object method

description: Get a method called (decorator?) after every object method


code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty

import time
import threading

class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    health_points = NumericProperty(100)

    def __init__(self, **kwargs):
        super(ScreenTwo, self).__init__(**kwargs)

    def on_health_points(self, instance, value):
        if value < 1:
            self.changeScreen()

    def on_enter(self, *args):
        thread = threading.Thread(target=self.bleed)
        thread.daemon = True
        thread.start()

    def bleed(self, *args):
        while self.health_points > 0:
            self.health_points -= 5
            time.sleep(0.1)

    def displayScreenThenLeave(self):
        self.changeScreen()

    def changeScreen(self):
        if self.manager.current == 'screen1':
            self.manager.current = 'screen2'
        else:
            self.manager.current = 'screen1'

class Manager(ScreenManager):

    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)

class ScreensApp(App):

    def build(self):
        m = Manager(transition=NoTransition())
        return m

if __name__ == "__main__":
    ScreensApp().run()

rawDescription: Kivy: changing screen from python code

description: Kivy: changing screen from python code


rawDescription: Grouping two lists in python

description: Grouping two lists in python


code:
# ...
layout = wx.BoxSizer(wx.VERTICAL)
layout.Add(self.toolBar, 0, wx.EXPAND|wx.BOTTOM, 5)
layout.Add(self.searchBar, 0, wx.EXPAND|wx.LEFT|wx.RIGHT,5)

splitter = wx.SplitterWindow(self,style=wx.SP_LIVE_UPDATE)

tree_panel = wx.Panel(splitter)
tree_panel.SetBackgroundColour(wx.BLACK)
entry_panel = wx.Panel(splitter)
entry_panel.SetBackgroundColour(wx.GREEN)

splitter.SplitVertically(tree_panel,entry_panel)
splitter.SetMinimumPaneSize(50)

layout.Add(splitter,1,wx.EXPAND|wx.ALL,5)
self.SetSizer(layout)

rawDescription: wxPython Nested Panel and Sizers

description: WxPython Nested Panel and Sizers


rawDescription: How the datas are connected to the specific user ID?


code:
def _mul_vector(self, other):
    M,N = self.shape

    # output array
    result = np.zeros(M, dtype=upcast_char(self.dtype.char,
                                           other.dtype.char))

    # csr_matvec or csc_matvec
    fn = getattr(sparsetools,self.format + '_matvec')
    fn(M, N, self.indptr, self.indices, self.data, other, result)

    return result

rawDescription: Numpy/Scipy Sparse vs dense multiplication


code:
try:
    from itertools import izip
except ImportError:  # Python 3
    izip = zip

def pairwise(iterable):
    "s -> (s0,s1), (s2,s3), (s4, s5), ..."
    a = iter(iterable)
    return izip(a, a)

rawDescription: Creating a python dictionary from a line of text


rawDescription: Python3 search value in this array

description: Python3 search value in this array


code:
example_array = [[0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
                [0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1],
                [0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
                [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
                [1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1],
                [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0],
                [1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0],
                [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

def findpairs(mylist, radius = 2.0):
    """
    Find pairs with data within a given radius.
    If we work from the top of the array down, we never
    need to look up (because we already would have found
    those, and we never need to look left on the same line.
    """

    # Create the parameters of a half circle, which is
    # the relative beginning and ending X coordinates to
    # search for each Y line starting at this one and
    # working down.  To avoid duplicates and extra work,
    # not only do we not look up, we never look left on
    # the same line as what we are matching, but we do
    # on subsequent lines.

    semicircle = []
    x = 1
    while x:
        y = len(semicircle)
        x = int(max(0, (radius ** 2 - y ** 2)) ** 0.5)
        # Don't look back on same line...
        semicircle.append((-x if y else 1, x + 1))

    # The maximum number of y lines we will search
    # at a time.
    max_y = len(semicircle)

    for y_start in range(len(mylist)):
        sublists = enumerate(mylist[y_start:y_start + max_y], y_start)
        sublists = zip(semicircle, sublists)
        check = (x for (x, value) in enumerate(mylist[y_start]) if value)
        for x_start in check:
            for (x_lo, x_hi), (y, ylist) in sublists:
                # Deal with left edge problem
                x_lo = max(0, x_lo + x_start)
                xlist = ylist[x_lo: x_start + x_hi]
                for x, value in enumerate(xlist, x_lo):
                    if value:
                        yield (x_start, y_start), (x, y)

print(list(findpairs(example_array, 2.5)))

rawDescription: Identifying pairs of Python array cells separated by maximum distance in a large array?


code:
for key, values in a.items():
    ...

rawDescription: How to efficiently loop through a dictionary when deleting elements from the same (mutated)

description: Efficiently loop through a dictionary when deleting elements from the same (mutated)


code: lines = ax.left_ax.get_lines() + ax.right_ax.get_lines()

rawDescription: Legend only shows one label when plotting with pandas


code:
import csv

with open('file.csv', 'r') as infile, open('reordered.csv', 'a') as outfile:
    # output dict needs a list for new column ordering
    fieldnames = ['A', 'C', 'D', 'E', 'B']
    writer = csv.DictWriter(outfile, fieldnames=fieldnames)
    # reorder the header first
    writer.writeheader()
    for row in csv.DictReader(infile):
        # writes the reordered rows to the new file
        writer.writerow(row)

rawDescription: Python - re-ordering columns in a csv

description: Python - re-ordering columns in a csv


code:
def no_arg_builtins():
  for builtin in dir(__builtins__):
    o = getattr(__builtins__, builtin)
    if callable(o):
      try:
        yield o()
      except TypeError:
        pass # Called incorrectly
    else:
      pass # not callable

rawDescription: Generate a list of objects of python builitins that can be called without arguments


rawDescription: How do I retrieve a list within dictionary within a list?

description: Retrieve a list within dictionary within a list


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import sys

import unittest, time, re

class Sel(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://twitter.com"
        self.verificationErrors = []
        self.accept_next_alert = True
    def test_sel(self):
        driver = self.driver
        delay = 3
        driver.get(self.base_url + "/search?q=stckoverflow&src=typd")
        driver.find_element_by_link_text("All").click()
        for i in range(1,100):
            self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            time.sleep(4)
        html_source = driver.page_source
        data = html_source.encode('utf-8')

if __name__ == "__main__":
    unittest.main()

rawDescription: scrape websites with infinite scrolling

description: Scrape websites with infinite scrolling


code:
class Classic: pass
class NewStyle(object): pass

print(dir(Classic))
# ['__doc__', '__module__']

print(dir(NewStyle))
# ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

rawDescription: Class with Object as a parameter

description: Class with Object as a parameter


code:
from itertools import groupby
from operator import attrgetter

keyfunc = attrgetter('website')
qs = Product.objects.order_by('website__name')
groups = []
uniquekeys = []
for website_name, products in groupby(qs, keyfunc):
    groups.append(list(products))
    uniquekeys.append(website_name)

rawDescription: Group by FK field

description: Group by FK field


code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from dihedrals import *
from time import time

def profileDihedrals(f):
    t0 = time()
    for i in range(20000):
        p = np.random.random( (4,3) )
        f(p)
        p = np.random.randn( 4,3 )
        f(p)
    return(time() - t0)

print("old_dihedral2: ", profileDihedrals(old_dihedral2))
print("wiki_dihedral: ", profileDihedrals(wiki_dihedral))
print("new_dihedral:  ", profileDihedrals(new_dihedral))

rawDescription: Dihedral/Torsion Angle From Four Points in Cartesian Coordinates in Python


code:
cursor = db.cursor()

# execute SQL select statement
cursor.execute("SELECT name FROM Vehicle")

# commit your changes
db.commit()

keywords=[]

rawDescription: calling a mysql and printing data matching text file(python)

description: Calling a mysql and printing data matching text file(python)


code:
plt.subplot(111)
hb = plt.hexbin(x,y, cmap=plt.cm.YlOrRd_r)
plt.cla()
plt.hexbin(x, y,
           C=np.ones_like(y, dtype=np.float) / hb.get_array().max(),
           cmap=plt.cm.YlOrRd_r,
           reduce_C_function=np.sum)
plt.axis([xmin, xmax, ymin, ymax])
cb = plt.colorbar()

rawDescription: matplotlib hexbin normalize

description: Matplotlib hexbin normalize


rawDescription: How to generate regex to match access.log acording to its format config?

description: Generate regex to match access.log acording to its format config


rawDescription: Keeping rows in Pandas where the same ID appears more than n times and convert to list per ID


code:
n = rdd.getNumPartitions()
(rdd_with_keys
    # partition by random key to put data on random partition
    .partitionBy(n)
    # Sort partition by random value to ensure random order on partition
    .mapPartitions(sorted, preservesPartitioning=True)
    # Extract (unique_id, value) pairs
    .values())

rawDescription: Pyspark: shuffle RDD

description: Pyspark: shuffle RDD


code:
def do_stuff(api_func, get_data_func):
    cursor = ''
    while True:
        data = api_func(id, cursor)
        #more code
        for i in data['data']:
            ids_likes += get_data_func(i) + ', '
        #more code

rawDescription: Slightly different loops in python / Minimize code


code:
with open('ascii.txt') as ascii_file:
    ...

rawDescription: Use of 'Open'-command for counting lines and chars from file in Python


code:
_columns = {
        'order_ids': fields.function(_orders, string="Sale Orders", type="many2many", relation='sale.order'),
    }

rawDescription: Odoo. Function field that returns list

description: Odoo. Function field that returns list


code:
z = Button(win, text='')
z['command'] = lambda x=x, y=y, z=z: display(x, y, z)
z.grid(row=x, column=y)

rawDescription: changing buttons within tkinter

description: Changing buttons within tkinter


code: Chloe1.txt, Megan1.txt, Chloe2.txt

rawDescription: Python Splitting a file into many files and adding in extra info


code:
from itertools import islice, count
def bigxrange(start, stop, step=1):
    return islice(count(start, step), (stop - start + step - 1))

list(bigxrange(0, 10))

rawDescription: Extended conditions in a for loop

description: Extended conditions in a for loop


rawDescription: How to override a function call in Python?

description: Override a function call in Python


code:
['hello there', 'foo', 'Watermelons are delicious', 'foobar']
['1', '2', '3', '6']

rawDescription: How to remove an item in a list that has a specific ordering of words

description: Remove an item in a list that has a specific ordering of words


code:
for team in teams:
    team = [1500]

rawDescription: Python: How to have a user reference and access a variable?


code:
def bp():
    from pdb import Pdb
    Pdb().set_trace(sys._getframe().f_back)

rawDescription: Convenience function defined for debugging with ipdb in Python


rawDescription: Splitting a list of numbers from txt file on both spaces and new lines, Python


code:
import array
a = array.array('l', range(20))

rawDescription: Storage size of array.array in a file

description: Storage size of array.array in a file


code:
#
# pendulum_demo.py
#
# Use PyGSL to solve the differential equations for a pendulum with
# friction.  Plot the solution using matplotlib.
#

import numpy as np
import matplotlib.pyplot as plt
from pygsl import odeiv

def pendsys(t, y, args):
    """
    The right-hand side of the first order system of differential equations.
    """
    theta, v = y
    g, b, L, m = args

    f = np.zeros((2,))
    f[0] = v
    f[1] = -b * v / (m * L ** 2) - np.sin(theta) * g / L

    return f

def pendjac(t, y, args):
    """
    The Jacobian of the system.
    """
    theta, v = y
    g, b, L, m = args

    jac = np.zeros((2, 2))
    jac[0, 1] = 1.0
    jac[1, 0] = -g / L * np.cos(theta)
    jac[1, 1] = -b / (m * L ** 2)

    dfdt = np.zeros((2,))

    return jac, dfdt

# Pendulum parameter values
#
# Gravitational constant
g = 9.81
# Friction coefficient
b = 0.5
# Pendulum length
L = 1.0
# Pendulum bob mass
m = 2.0

# Initial conditions
theta0 = np.pi - 0.01
v0 = 0.0
y = [theta0, v0]
t = 0

# Solver control parameters.
abserr = 1e-11
relerr = 1e-9

stoptime = 12.0

# Create the GSL ODE solver
N = 2
step    = odeiv.step_rk8pd(N, pendsys, pendjac, args=(g, b, L, m))
control = odeiv.control_y_new(step, abserr, relerr)
evolve  = odeiv.evolve(step, control, N)

# h is the initial step size.
h = stoptime / 500.0

# The time values and points in the solution are saved in the lists
# tsol and ysol, respectively.  The lists are initialized with
# the initial conditions.
tsol = [t]
ysol = [y]

# Call evolve.apply(...) until the solution reaches stoptime
while t < stoptime:
    t, h, y = evolve.apply(t, stoptime, h, y)
    tsol.append(t)
    ysol.append(y)

tsol = np.array(tsol)
ysol = np.array(ysol)

plt.plot(tsol, ysol[:, 0], label=r'$\theta$')
plt.plot(tsol, ysol[:, 1], label='v')
plt.xlabel('t')
plt.grid(True)
plt.legend()
plt.title('Pendulum with Friction')
plt.show()

rawDescription: Converting GSL ODE solver to Python

description: Converting GSL ODE solver to Python


code:
class AttributeBlock():
    objects = []
    def __init__(self, key, label, isClosed, isRequired, attributes):
        self.key = key
        self.label = label
        self.isClosed = isClosed
        self.isRequired = isRequired
        self.attributes = attributes if attributes is not None else {}
        self.objects.append(self)

rawDescription: creating a new copy of a python class each time it's called

description: Creating a new copy of a python class each time it's called


code:
for rec in range(records):
  currec = data['MainKey'][rec]
  # If each column count can be different
  columns = len(currec['Fields'])
  retval, valrec = self.build_csv_row(currec, columns, fields)

rawDescription: Opening A large JSON file in Python with no newlines for csv conversion Python 2.6.6


code:
arr = []
for data in list:
    elem = next((s for s in selectionsFromTable if data['elements'] in s), None)
    if elem:
        inner = []
        if 'Table' in elem:
            print("table")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['table'],
                          })

        elif ('Bar' in elem) or ('Pie' in elem):
            print("chart")
            inner.append({'id': data['id'],
                          'section_title': data['elements'],
                          'tableorChart': data['chart'],
                          })

        arr.append(inner)

rawDescription: How to retreive particular values from a list: python

description: Retreive particular values from a list: python


code:
import numpy as np
import pandas as pd
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans

tb_existing_url_csv = 'https://docs.google.com/spreadsheets/d/1X5Jp7Q8pTs3KLJ5JBWKhncVACGsg5v4xu6badNs4C7I/pub?gid=0&output=csv'

existing_df = pd.read_csv(
    tb_existing_url_csv,
    index_col = 0,
    thousands  = ',')
existing_df.index.names = ['country']
existing_df.columns.names = ['year']

pca = PCA(n_components=2)
pca.fit(existing_df)
PCA(copy=True, n_components=2, whiten=False)
existing_2d = pca.transform(existing_df)

existing_df_2d = pd.DataFrame(existing_2d)
existing_df_2d.index = existing_df.index
existing_df_2d.columns = ['PC1','PC2']
existing_df_2d.head()

kmeans = KMeans(n_clusters=5)
clusters = kmeans.fit(existing_df)
existing_df_2d['cluster'] = pd.Series(clusters.labels_, index=existing_df_2d.index)

rawDescription: Assigning custom colors to clusters using numpy


rawDescription: Apply and group in the Pandas python

description: Apply and group in the Pandas python


code:
from suds.transport.https import WindowsHttpAuthenticated
ntlm = WindowsHttpAuthenticated(username='xx', password='xx')
client = Client(url, transport=ntlm)

rawDescription: Has anyone combined soap.py or suds with python-ntlm?


code:
for key, grp in df.groupby('Type'):
    plt.plot(grp['Timestamp'], grp['Value'])

rawDescription: Python Panda multi Line graph using time stamp


code:
fd = pickle.load(open("data/fd.txt", 'rb'))

def compute_ave_freq(word_forms):
    l = len(word_forms)
    return sum([fd.freq(form) for form in word_forms]) / l if l > 0 else 0

rawDescription: Faster way to store a NLTK FreqDict?

description: Faster way to store a NLTK FreqDict


rawDescription: How to solve non-linear sets of equations

description: Solve non-linear sets of equations


code:
import re

re.sub(' mytable', '" id="mytable', df.to_html(classes='mytable'))

rawDescription: How to define html id for Pandas Dataframe

description: Define html id for Pandas Dataframe


code: tup_c =  itertools.combinations(list_c, 3)

rawDescription: Optimizing Python script with multiple for loops


code:
from django import forms
from models import Category

class ProductForm(forms.Form):
    # Put all my Categories into a select option
    category = forms.ModelChoiceField(queryset=Category.objects.all())
    product = forms.CharField()
    quantity = forms.IntegerField()
    price = forms.FloatField()

rawDescription: Saving data using Django ModelForms

description: Saving data using Django ModelForms


code:
points = traces[np.arange(n),ttime_inds]
sum_points = points.sum(axis=1)
sqrr_sum_points = (points**2).sum(axis=1)
S = sum_points # **2/(n*sqrr_sum_points)

rawDescription: Vectorizing for loops in python with numpy multidimensional arrays


code:
l = [6, 0, 2, 3, 1, 5, 4]
sorted(l)
sorted(l, reverse=True)

rawDescription: python object list, sort in descending order based on object attribute

description: Python object list, sort in descending order based on object attribute


code:
import collections
import numbers
def type_split(sources):
    types = [basestring, collections.Mapping, numbers.Number]
    return [[item if isinstance(item, itype) else None for item in sources]
            for itype in types]

rawDescription: Python : Split a list of length n into a table or further lists, by type, retaining order


rawDescription: Is there a nicer way to convert a string into a data set in python?


code:
def zippy(x, y):
    return [(x[i], y[i]) for i in range(len(x))]

rawDescription: Trouble creating my own "zip" function in Python


code:
import pandas as pd

store = pd.HDFStore('data.h5')
data = store['mydata']
store.close()

rawDescription: Processing large amount of data in Python


code:
class MyListView(ListView):
    template_name = "transactions/batchfiles/list.html"

rawDescription: Can I change the template search behavior of class based views?

description: Change the template search behavior of class based views


rawDescription: Regular expression which gets the last part after a special character


code:
df  = pd.DataFrame(np.random.random((5,4)))
series = pd.Series(df.values.ravel())
series.describe()

rawDescription: How to describe entire matrix or Pandas dataframe

description: Describe entire matrix or Pandas dataframe


code: c.InteractiveShellApp.exec_lines = ["from __future__ import division", "import something", "another statement"]

rawDescription: is there a way in ipython to always run an import on startup? like 'from __future__ import division'?

description: Is there a way in ipython to always run an import on startup? like 'from __future__ import division'


code:
if any(v > 20 for v in list_1):
    # there is at least one value in list_1 that is greater than 20
    list_1 = [v - 5 if v > 20 else v for v in list_1]

rawDescription: How do you compare a list with an integer in an 'if' statement? Python

description: Compare a list with an integer in an 'if' statement? Python


rawDescription: python list items & convert language codes to names

description: Python list items & convert language codes to names


code:
from dis import dis
dis(f)

rawDescription: Detect all global variables within a python function?


code:
from html.parser import HTMLParser

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.content = []
        self.observation = False
        self.element = None

    def handle_starttag(self, tag, attrs):
        print(tag)
        if tag == 'observation':
            self.content.append({})
            self.observation = True
        elif self.observation:
            self.element = tag
            self.content[-1][self.element] = ""

    def handle_endtag(self, tag):
        if tag == 'observation':
            self.observation = False

        self.element = None

    def handle_data(self, data):
        if self.element:
            self.content[-1][self.element] += data

from pprint import pprint

with open("data.someml", "rt") as infile:
    parser = MyHTMLParser()
    parser.feed(infile.read())

    pprint(parser.content)

rawDescription: HTML Parser on XML page, getting a dictionary of data


code: subreddit_name = getattr(c.subreddit, 'display_name', c.subreddit)

rawDescription: Seeking a better way to check an instance property and assign value depending on property type


rawDescription: Find linked "child/parents" in pythons dictionaries


code:
import numpy as np
df = pd.DataFrame(np.random.binomial(1, 0.2, (2, 10000)))

rawDescription: Finding the common columns when comparing two rows in a datafame in python


code:
def onUpdateUI(self, evt):
    for colNum in xrange(0, self.GetColumnCount()-1):
        if self.GetColumnWidth(colNum) < self.__columnWidth[colNum]:
            self.SetColumnWidth(colNum, self.__columnWidth[colNum])
    evt.Skip()

rawDescription: How to set and preserve minimal width?

description: Set and preserve minimal width


code: '44' + (s[1:] if s.startswith('0') else s)

rawDescription: Python remove first number from string if its 0


code:
def internet_on():
    for timeout in [1,5,10,15]:
        try:
            response=urllib2.urlopen('http://google.com',timeout=timeout)
            return True
        except urllib2.URLError as err: pass
    return False

rawDescription: Checking internet connection with Python


code:
import re
s = "(I (J (K L) (M N)) (O P))"
re.findall('[\w\(\)]',s)

rawDescription: How to split a string by multiple separators and store in a list?

description: Split a string by multiple separators and store in a list


code:
data2 = """Data\tSample\n01-AUG-09\tSample 1\n02-Aug-09\tSample 2\n"""
read_csv(StringIO(data2),sep='\t',parse_dates=True,index_col=0)

rawDescription: Pandas Read CSV with dates as DD-MMM-YY

description: Pandas Read CSV with dates as DD-MMM-YY


rawDescription: How to strip whitespace from before but not after punctuation in python

description: Strip whitespace from before but not after punctuation in python


code: [{ 'cust_no':126, 'lastname': 'smith', 'firstname':'john'}, {'cust_no':127, 'lastname':'smoth', 'firstname':'bob'}]

rawDescription: list of dictionaries to dictionary of lists

description: List of dictionaries to dictionary of lists


code:
def url_allowed(url):
    # check for the pattern /static/dir/dir/dir/ etc
    return True

def process_links(links):
    return [l for l in links if url_allowed(l.url)]

rawDescription: Scrapy - Limiting middle directories (Python)


code:
with open('myfile.txt', 'r') as dataFile:
    for i in range(3):
        next(dataFile)
    the_4th_line = next(dataFile)

rawDescription: How to read one particular line from .txt file in python?

description: Read one particular line from .txt file in python


code:
from functools import partial

myprint = partial(print, end='-', sep='.')

rawDescription: Python 3: Change default values of existing function's parameters?


code:
dynamic_variable1 = request.session['name1']
dynamic_variable2 = request.session['name2']

rawDescription: Passing variables to a function

description: Passing variables to a function


code:
#!/usr/bin/env python
import sys
from functools import partial

print(sum(chunk.count('\n') for chunk in iter(partial(sys.stdin.read, 1 << 15), '')))

rawDescription: estimating number of lines in a file - mismatch between file size and size of all lines

description: Estimating number of lines in a file - mismatch between file size and size of all lines


code:
# Move these outside of the class declaration or make them class variables

stopwords = set(stopwords.words('english'))
grammar = "NP: {}"
cp = nltk.RegexpParser(grammar)

rawDescription: How to stop memory memory leak from Flask and NLTK

description: Stop memory memory leak from Flask and NLTK


code:
#pragma once
#pragma warning (disable: 4244)

#include <tchar.h>
#include <iostream>
#include <boost/tokenizer.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <fstream>
#include <regex>
#include <vector>
#include <streambuf>
#include <sstream>

rawDescription: Script for gzip-inflate of HTTP messages in file


code: mydict = {k:v}

rawDescription: make dictionary from csv file columns

description: Make dictionary from csv file columns


rawDescription: How can I find why is my pickled data so large?

description: Find why is my pickled data so large


code: newlst = [sorted(set(x+y)) for x,y in zip(lstA, lstB)]

rawDescription: Python: Concatenating sublists between different lists


code:
orders_table = Table('orders', metadata,
    Column('item_id', Integer, primary_key=True),
    Column('item_name', String),
    Column('customer_id', Integer, ForeignKey('customers.uid')) # changed
)

# for bi-directional relationship use:
mapper(Customer, customers_table, properties={
    'orders': relationship(Orders, backref='customer')
})
mapper(Orders, orders_table)
# for uni-directional relationship use:
mapper(Customer, customers_table)
mapper(Orders, orders_table, properties={
    'customer': relationship(Customer)
})
#...

rawDescription: Use a foreign key mapping to get data from the other table using Python and SQLAlchemy


code:
for line in open('my_file.txt'):
    row = line.split()
    if row[0] == 'SOLU' and row[1] == '6DIM' and row[4] == 'EULER' and row[8] == 'FRAC':
        euler = map(float, row[5:8])
        frac = map(float, row[9:12])
        do_something_with(euler, frac)

rawDescription: How to I extract floats from a file in Python?

description: I extract floats from a file in Python


code:
for key in tree_dict.keys():

    parent_id = key.strip('0')[:-1].ljust(len(key), '0')

    # If it's all zeros, set it to None so we know the parent is root
    if int(parent_id) == 0:
        parent_id = None

    tree_dict[key].update({'parent':parent_id})

rawDescription: make a tree based on the key of each element in list

description: Make a tree based on the key of each element in list


code:
def guess_extension(self, type, strict=True):
    # ...
    extensions = self.guess_all_extensions(type, strict)
    if not extensions:
        return None
    return extensions[0]

rawDescription: Why the various JPEG Extensions?

description: Why the various JPEG Extensions


code:
import matplotlib
import matplotlib.pyplot as plt

rawDescription: What is the preferred way to import pylab at a function level in Python 2.7?


code:
re.findall(r',?([^,]+)(?:,|\r\n)', string)
# this regex captures anything between `,` and/or a newline

rawDescription: Python regex: exclude \r\n

description: Python regex: exclude \r\n


code: from settings import *

rawDescription: Django settings based on IP or hostname

description: Django settings based on IP or hostname


code: y[::REPLACE_EVERY_Nth] = REPLACE_WITH

rawDescription: how to replace every n-th value of an array in python most efficiently?

description: Replace every n-th value of an array in python most efficiently


code:
def applyIncrease(increaseFn, m):
    return increaseFn(m)

def makeIncrease(n):
    def _innerFn(arg):
        return arg + n
    return _innerFn

rawDescription: How to define a function that output another function?

description: Define a function that output another function


code: dk[:,0] = np.array([sqrt((PC_t-pc)**2/np.var(PC_l)) for pc in PC_l])

rawDescription: Optimize Cython code for numpy variance calculation


code:
import inspect
import numpy as np
inspect.getmodule(np.dot)

rawDescription: Numpy dot product very slow using ints

description: Numpy dot product very slow using ints


rawDescription: Fibonacci numbers integer taken

description: Fibonacci numbers integer taken


code:
import cv # Load in relevant packages
import cv2
import numpy as np

img = cv2.imread(...,0) # Load in image here - Ensure 8-bit grayscale
final_circles = [] # Stores the final circles that don't go out of bounds
circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,20,param1=50,param2=30,minRadius=0,maxRadius=0) # Your code
rows = img.shape[0] # Obtain rows and columns
cols = img.shape[1]
circles = np.round(circles[0, :]).astype("int") # Convert to integer
for (x, y, r) in circles: # For each circle we have detected...
    if (r <= x <= cols-1-r) and (r <= y <= rows-1-r): # Check if circle is within boundary
        final_circles.append([x, y, r]) # If it is, add this to our final list

final_circles = np.asarray(final_circles).astype("int") # Convert to numpy array for compatability

rawDescription: OpenCV Python HoughCircles: Circles detected outside of image boundary


code:
import itertools
years = range(1996, 2000)
numbers = range(1,10)
result = ""
for year in years:
    for y, n in itertools.product([year], numbers):
        result += "%s:%s, "%(y, n)

result

rawDescription: Python: Create two ranges and join to one expression


rawDescription: ctypes reimplementation of rshift for c_ulong

description: Ctypes reimplementation of rshift for c_ulong


code:
def __new__(cls, i):
    ...
    try:
        return _intcache[ival]   # <-- return the cached Integer if seen before
    except KeyError:
        obj = Expr.__new__(cls)  # <-- create a new Integer if ival not in _intcache
        obj.p = ival

        _intcache[ival] = obj
        return obj

rawDescription: Sympy classes Zero, One and NegativeOne, why they exist?


code:
f = open('out.csv','a') #open in append mode if you're already written some of the file.
header = "location\ttemperature\tweather\date\n" #only write this if you haven't already.
date = str(now.month) + "/" + str(now.day) +  "/" + str(now.year) + " " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
f.write(header)
f.write('\t'.join([locations,temp_f,weather,date]))
f.write('\n')
f.close()

rawDescription: print with row for CSV in python respecting column(header) and row

description: Print with row for CSV in python respecting column(header) and row


code: os.system("open -a path/blender.app Import_mhx.blend")

rawDescription: Open a file from a specific program from python


rawDescription: Parsing xml with "not well-formed" characters in python


code: some_global_thing = {}

rawDescription: Meteor modularity and access protection — is everything 'global'?


code:
df = pd.DataFrame(np.random.randint(low=0, high=10, size=(100, 50)))
df_colcount = float(len(df.columns))
df['zero_count'] = df.mask(df!=0).count(axis=1)
df['zero_share'] = df.mask(df!=0).count(axis=1).div(df_colcount)

rawDescription: Filter by row sum and value

description: Filter by row sum and value


rawDescription: Confused about Python’s id()

description: Confused about Python’s id()


code:
class Site(object):
    pass
site = Site()
site.__dict__.update(dict)
site.a

rawDescription: Adding dynamic property to a python object


code: ''.join(['{0:{1}}'.format(c, width + extra) for c, width in zip(line, maxLength)])

rawDescription: dynamically building strings 'string'.format()

description: Dynamically building strings 'string'.format()


code:
def isrespondingPID(PID):
    os.system('tasklist /FI "PID eq %d" /FI "STATUS eq running" > tmp.txt' % PID)
    tmp = open('tmp.txt', 'r')
    a = tmp.readlines()
    tmp.close()
    if int(a[-1].split()[1]) == PID:
        return True
    else:
        return False

rawDescription: How to tell if process is responding in Python on Windows

description: Tell if process is responding in Python on Windows


rawDescription: Python Pandas Identify Duplicated rows with Additional Column


code:
dist = np.sum(np.abs(xx - yy), axis=0)
dist

rawDescription: calculating distance between two numpy arrays

description: Calculating distance between two numpy arrays


code: logging.basicConfig(filename='example.log', filemode='w', format='%(created)f %(message)s', level=logging.INFO)

rawDescription: Time formatting using logging module in python


rawDescription: Django Form Wizard to Edit Model

description: Django Form Wizard to Edit Model


code:
names = [item[0] for item in months]
numbers = [item[1] for item in months]

rawDescription: Creating a sum from a list within a list in Python?


code:
result = []
for x in a:
   if isinstance(x, list) and len(x) == 1: # check item type and length
       result.append(x[0])
   else:
       result.append(x)

rawDescription: Python: Extract one value from a list of integers and lists


code:
from collections import Counter
def remove_dups(d):
    c = Counter(d.values())
    return {k:v for k, v in d.items() if c[v] == 1}

rawDescription: Removing certain keys in a dictionary

description: Removing certain keys in a dictionary


code:
from collections import defaultdict
import string

# Only process words at least a minimum number of letters long.
MIN_WORD_LEN = 3

def find_words(line, min_word_len=MIN_WORD_LEN):
    # Remove punctuation and all whitespace characters other than spaces.
    line = line.translate(None, string.punctuation + '\t\r\n')
    return (word for word in line.split(' ') if len(word) >= min_word_len)

def make_index(input_file):
    index = defaultdict(set)

    for line_num, line in enumerate(input_file, start=1):
        for word in find_words(line.lower()):
            index[word].add(line_num)  # Ensure line number is in word's set.

    # Convert result into a regular dictionary of simple sequence values.
    return {word:tuple(line_nums) for word, line_nums in index.iteritems()}

rawDescription: Check Values of A Dictionary for Repeating Numbers


code: "You have %d cheeses!" % 10

rawDescription: Explain why variables in my function are not related to variables in my script in Python 2


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

filename = '/tmp/graph.png'
G = nx.complete_graph(10)
pos = nx.spring_layout(G)
xy = np.row_stack([point for key, point in pos.iteritems()])
x, y = np.median(xy, axis=0)
fig, ax = plt.subplots()
nx.draw(G, pos, with_labels=False, node_size=1)
ax.set_xlim(x-0.25, x+0.25)
ax.set_ylim(y-0.25, y+0.25)
plt.savefig(filename, bbox_inches=0, orientation='landscape', pad_inches=0.1)

rawDescription: Matplotlib savefig() on a zoomed in graph


code:
class Quitter(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return 'Use %s() or %s to exit' % (self.name, eof)
    def __call__(self, code=None):
        # Shells like IDLE catch the SystemExit, but listen when their
        # stdin wrapper is closed.
        try:
            sys.stdin.close()
        except:
            pass
        raise SystemExit(code)
__builtin__.quit = Quitter('quit')
__builtin__.exit = Quitter('exit')

rawDescription: The difference between exit() and sys.exit() in Python?


code: from django.contrib.auth import logout as auth_logout

rawDescription: Django User Logout Fails to Redirect Homepage


code: [(lambda x: x*10)(i) for i in range(10,21)]

rawDescription: Python lambda function declaration and call for list generation


code:
class ThreadedFetch(threading.Thread):
    def __init__(self, queue, out_queue):
        super(ThreadedFetch, self).__init__()
        self.queue = queue
        self.outQueue = out_queue

    def run(self):
        url, save_to = self.queue.get()
        # ...

rawDescription: why is queue empty for the file to be downloaded

description: Why is queue empty for the file to be downloaded


code:
import numpy as np
data = [1,2,3]
a = np.empty([len(data),2], dtype=object)
a
a[:,0]='a'
a
a[:,1]=data
a
data2=np.array([[1,2],[3,4],[5,6]])
data2
b = np.empty([len(data2),3],dtype=object)
b
b[:,0]='a'
b
b[:,1:]=data2
b

rawDescription: How can I quickly convert to a list of lists, insert a string at the start of each element?

description: Quickly convert to a list of lists, insert a string at the start of each element


code:
df2
slice_consc(df2,5)

rawDescription: slice pandas df based on n consecutive instances of element

description: Slice pandas df based on n consecutive instances of element


code:
import os.path

def parse_pdf(filename):
    "Parse a pdf into text"
    content = getPDFContent(filename)
    encoded = content.encode("utf-8")
    ## split of the pdf extension to add .txt instead.
    (root, _) = os.path.splitext(filename)
    text_file = open(root + ".txt", "w")
    text_file.write(encoded)
    text_file.close()

rawDescription: How do I apply my python code to all of the files in a folder at once, and how do I create a new name for each subsequent output file?

description: Apply my python code to all of the files in a folder at once, and how do I create a new name for each subsequent output file


code:
['Hello, World',
 'Hello, World',
 'Hello, World',
 'Hello, World',
 'Hello, World',
 'Hello, World',
 'Hello, World',
 'Hello, World']

rawDescription: can ipython parallel operate across different sets of nodes

description: Can ipython parallel operate across different sets of nodes


code:
with open('path/to/input') as infile:
    curr = None
    for line in infile:
        if line == curr:
            continue
        print(line)
        curr = line

rawDescription: Keep the first line from a block of related lines


code:
from pulp import *

objs = [2,3,2,5,3]
weights = [1,2,2,1,3]
knapweight = 5

prob = LpProblem('Knapsack', LpMaximize)
xs = [LpVariable("x{}".format(i+1), cat="Binary") for i in range(len(objs))]

# add objective
total_prof = sum(x * obj for x,obj in zip(xs, objs))
prob += total_prof

# add constraint
total_weight = sum(x * w for x,w in zip(xs, weights))
prob += total_weight <= knapweight

status = prob.solve()
print(LpStatus[status])
print("Objective value:", value(prob.objective))
print ('\nThe values of the variables : \n')
for v in prob.variables():
    print(v.name, "=", v.varValue)

rawDescription: How do I generate PuLP variables and constrains without using exec?

description: Generate PuLP variables and constrains without using exec


rawDescription: Set the property of a class using decorators on a class method in python


code:
from datetime import datetime

t0 = datetime(1, 1, 1)
now = datetime.utcnow()
seconds = (now - t0).total_seconds()
ticks = seconds * 10**7

rawDescription: What is python equivalent of C#'s system.datetime.Ticks()?


code:
class NoProps(object):
    def __init__(self, spam=None):
        if spam is None:
            spam = 0  # Pick a sensible default
        self.spam = spam

    @classmethod
    def from_other_obj(cls, other_obj):
        """Factory method to copy other_obj's values"""
        # The call to cls() is where the "magic" happens
        obj = cls()
        obj.spam = other_obj.spam
        # Copy any other properties here
        return obj

class Props(NoProps):
    @property
    def spam(self):
        """Docstring for the spam property"""
        return self._spam

    @spam.setter
    def spam(self, value):
        # Do whatever calculations are needed here
        import random
        self._spam = value + random.randint(0,20)

    @spam.deleter
    def spam(self):
        del self._spam

rawDescription: Turning an attribute into a property on an object-by-object basis?


code: words = { e.lower() for e in words }

rawDescription: Cancel capitalization in python

description: Cancel capitalization in python


code: self.Bands = {name: Band(self.Meta, name) for name in ('R', 'G', 'B')}

rawDescription: Can an attribute access another attribute?


rawDescription: Pythonic way to generate random uniformly distributed points withing hollow square lamina


code:
from scipy import *
set_printoptions(30)
x = randn(10)
cum_x = cumsum(x)

#The objective is to recover x using cum_x and the diff function.
y = append(cum_x[0], diff(cum_x))
print(x)
print("\n")
print(y)
#Now, y should be equal to x but this is not confirmed by the function in1d
test = in1d(x, y)
print(test)

rawDescription: Test of equality between arrays using in1d function


code: actions = [chain.from_iterable((d[event], d[data]) for d in value)]

rawDescription: Iterator inside DictWriter.writerow

description: Iterator inside DictWriter.writerow


code:
s = "aaabcaaa"
re.search(r'(.+)bc\1',s).group(1)

rawDescription: Repeat a substring in a nonconsecutive position


code:
search_str = "01catdog"
test1 = re.compile("cat|catdog")
match1 = test1.search(search_str)
match1.group()
match1.start()
test2 = re.compile("catdog|cat")  # reverse order
match2 = test2.search(search_str)
match2.group()
match2.start()

rawDescription: What's the most efficient way to find one of several substrings in Python?


code:
if display_graph:
    init_something()

functions_to_call = []

if show_timer:
    functions_to_call.append(do_something)

if other_option:
    functions_to_call.append(do_other_thing)

while True:
    for function in functions_to_call:
        function()

rawDescription: Python code readablity for boolean variable driven code


code:
s="""dcenter: code=d1, loc=San Jose, x=100, y=100, ctype=ct1
dcenter: code=d2, loc=San Ramon, x=300, y=200, ctype=ct2
storage: code=s1, locFrom=d1, x=50, y=50, rtype=rt1
storage: code=s2, locFrom=d1, x=-50,y=100, rtype=rt1"""

rawDescription: Please help me with this program to parse a file into an XML file


code:
dates = df[df['bool']].groupby("Group")['date'].first()
dates

rawDescription: How can I use transform to extract a feature based on multiple conditions, using pandas

description: Use transform to extract a feature based on multiple conditions, using pandas


rawDescription: I need some help Write random test data to csv file using faker


code:
y = [D['A'] for D in x]
y

rawDescription: filter the values of dictionary items in a list by their key in python

description: Filter the values of dictionary items in a list by their key in python


code: id = user.userid

rawDescription: Google App Engine Datastore get value of property


code:
from fabric.api import env, roles, run

env.roledefs['webservers'] = ['www1', 'www2', 'www3']

@roles('webservers')
def my_task():
    run('ls -l')

rawDescription: Retrieve current host from within an executed task


code: gallery = [verical[0]] + horizontal[0:3] + vertical[1:4] + horizontal[3:] + vertical[4:]

rawDescription: Custom filter for Django querysets

description: Custom filter for Django querysets


code: ds = xr.open_dataset(path_file, chunks={'year': 10})

rawDescription: Speeding up reading of very large netcdf file in python


code:
r = r"(?:Who|What) is the (?P<RelationVar>.+) of (?P<ConceptVar>.+)[?]"
l = ['Who is the director of The Dark Knight?', 'What is the capital of China?', 'Who is the president of USA?']
[re.match(r, i).groupdict() for i in l]

rawDescription: Python regular expressions for simple questions


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

tbl = table(ax, df2, loc='center',
            cellColours=colors.as_matrix(),
            colColours=['black']*len(colors.columns),
            rowColours=['black']*len(colors))

rawDescription: Conditionally change background color of specific cells


code:
# /home/your_user_name/PROJECT_DIR
# PROJECT_DIR/setup.py
# PROJECT_DIR/README.md
# PROJECT_DIR/your_app
# PROJECT_DIR/your_app/__init__.py # mark this folder as a python package named your_app
# PROJECT_DIR/your_app/main.py # your main codes
# PROJECT_DIR/your_app/utils.py # put stuff like help in here

rawDescription: Python, globally accessable function

description: Python, globally accessable function


code: sorted(range(len(a)), key=lambda i: a[i], reverse=True)[:2]

rawDescription: Get index of the top n values of a list in python


code:
import re
s = 'start TT end'
re.sub(r'([A-Z])\1', lambda pat: pat.group(1).lower(), s) # Inline

rawDescription: Using a regular expression to replace upper case repeated letters in python with a single lowercase letter


code:
from math import log, floor
def expansion(x, b):
    k = int(floor(log(x) / log(b)))
    d, r = divmod(x / float(b ** k), 1)
    digits = [int(d)]
    for _ in range(k):
        d, r = divmod(b * r, 1)
        digits.append(int(d))
    def rest(b, d, r):
        while r:
            d, r = divmod(b * r, 1)
            yield int(d)
    return digits, rest(b, d, r)

rawDescription: Looking for a different kind of divmod function


code:
s = df.stack()
s[s % 3 == 0].index.tolist()

rawDescription: Inverse lookup in pandas

description: Inverse lookup in pandas


code:
def checkplayerhealth (playerhealth):
    if playerhealth < 0:
        input ("Looks like you died.  Better luck next time! Press any key to exit")
        sys.exit()

rawDescription: Trouble with a playerhealth function

description: Trouble with a playerhealth function


code:
vol = Scale(
    sound_box,
    from_ = 0,
    to = 1,
    orient = HORIZONTAL ,
    resolution = .1,
    ####################
    command=change_vol
    ####################
)

rawDescription: Creating a volume slider in tkinter

description: Creating a volume slider in tkinter


rawDescription: Group time ranges by boolean values in pandas


rawDescription: When oracle virtualbox are set, How to get local ip address in python


rawDescription: How can I convert character like "\u0096" in python?

description: Convert character like "\u0096" in python


code:
get_start_of_previous_month(datetime.datetime.now())
get_start_of_previous_month(datetime.datetime(2013, 1, 21, 12, 23))

rawDescription: Handling months in python datetimes

description: Handling months in python datetimes


code:
numbers = list(range(100))
counter = -1

def go():
    global counter
    counter += 1
    if counter == len(numbers):
        counter = 0
    textbox.delete(1.0, END)
    textbox.insert('insert', str(numbers[counter]))

rawDescription: Stepping through function with button instead of input()


code:
from flask import session
from dropbox import session

rawDescription: Conflicting imports

description: Conflicting imports


code:
for i in fa:
    s=fa[i]+fb[3]

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

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


code:
def reduct(length, n=5):
    return [i for i in \
            itertools.combinations_with_replacement(range(1, 10), length) \
            if ((sum(i) - 1) % 9 + 1) == n]

rawDescription: Python Itertools: Complex "product"

description: Python Itertools: Complex "product"


code: plt.plot(Z['Time'], Z['Profit'])

rawDescription: Extract columns using "genfromtxt"

description: Extract columns using "genfromtxt"


code:
for col in u_response:
    df[col] = 0 # initialize the col
    df.loc[df.MultipleCheckboxResponse.str.contains(col), col] = 1 # adjust as necessary

df
#  ID NormalResponse       MultipleCheckboxResponse  Response7  Response4  \
#0  1        Answer3                      Response3          0          0
#1  2        Answer1            Response1|Response7          1          0
#2  3        Answer2  Response2|Response4|Response7          1          1
#3  4        Answer2            Response2|Response3          0          0

#   Response2  Response3  Response1
#0          0          1          0
#1          0          0          1
#2          1          0          0
#3          1          1          0

rawDescription: Pandas- Create new columns by splitting cells on | n times


code:
db.define_table('reviews',
            Field('title',requires=IS_NOT_EMPTY()),
            Field('review','text',requires=IS_NOT_EMPTY()),
            Field('game_id', 'reference games'))

rawDescription: Web2Py getting/posting values

description: Web2Py getting/posting values


code:
outputList(['"GRE"', '"KIN"', '"ERD"', '"KIN"'],
           '/path/to/datafile',
           '/path/to/outputfile')

rawDescription: Return lines from file in order of a list


code: max_revenue = max(revenue(i) for i in range(4*L))

rawDescription: Algorithm (prob. solving) achieving fastest runtime


code:
from tkinter import *
from ship import *

class Game:
    def __init__(self, gameWidth, gameHeight):
        self.root = Tk()
        self.gameWidth = gameWidth
        self.gameHeight = gameHeight
        self.gameWindow()

        self.ship = Ship(self.canvas, x=self.gameWidth / 2,y=self.gameHeight / 2, width=50, height=50, turnspeed=10, acceleration=5)
        self.root.bind('<Left>', self.ship.rotate)
        self.root.bind('<Right>', self.ship.rotate)
        self.root.bind('<Up>', self.ship.accel)
        self.root.bind('<Down>', self.ship.accel)

        self.root.mainloop()

    def gameWindow(self):
        self.frame = Frame(self.root)
        self.frame.pack(fill=BOTH, expand=YES)

        self.canvas = Canvas(self.frame,width=self.gameWidth, height=self.gameHeight, bg="black", takefocus=1)
        self.canvas.pack(fill=BOTH, expand=YES)

asteroids = Game(600,600)

rawDescription: How do I rotate a polygon in python on a Tkinter Canvas?

description: Rotate a polygon in python on a Tkinter Canvas


code:
input_string = "this is a sample test"
words = input_string.split()
output_string = ""
for word in words:
    for c in reversed(word):
        output_string += c
    output_string += " "
print(output_string)

rawDescription: How to preserve and reverse a string?

description: Preserve and reverse a string


code:
proc = subprocess.Popen(cmd)
proc.wait()

rawDescription: Are pipes in each process independent?

description: Are pipes in each process independent


code:
keys = ['Open', 'High', 'Low', 'Close', 'Volume', 'Adj Clos']
histodict = dict((item[0], dict(zip(keys, item[1:]))) for item in history)

rawDescription: How to sort a structured list of stock data for later access in Python?

description: Sort a structured list of stock data for later access in Python


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

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


code: any('Gabe' in s for s in x)

rawDescription: How do I check if a sub string exist in a list nested in a dictionary?

description: Check if a sub string exist in a list nested in a dictionary


code:
# Or using xparth or something
element = driver.find_element_by_css_selector(css_selector)

rawDescription: How do you click on an element which is hidden using Selenium WebDriver?

description: Click on an element which is hidden using Selenium WebDriver


code:
import numpy
import pickle
import pygame

surface = pygame.Surface((100, 100))

rawDescription: How can I tranform this (100, 100) numpy array into a grayscale sprite in pygame?

description: Tranform this (100, 100) numpy array into a grayscale sprite in pygame


code:
_, contours, hierarchy = cv2.findContours(opening.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
  #will give an error if there are less than 5 points
  if len(c) > 4:
    elip = cv2.fitEllipse(c)
    if check_ellipse(elip):
      cv2.ellipse(img, elip, (255, 0, 0))

rawDescription: Uniquely identifying clustered ellipses which are about the same shape, but different sizes


rawDescription: Replacing a string by a string with backslashes


rawDescription: pandas dataframe by index and integer

description: Pandas dataframe by index and integer


code:
import traceback
traceback.print_exc()

rawDescription: Large try except block in Python - how to understand where was an exception?


rawDescription: Writing value on the current textbox - Selenium


rawDescription: pexpect regex not working

description: Pexpect regex not working


code: {word: tuple(k for k, v in dict1.items() if word in v) for word in all_words}

rawDescription: python- searching dictionary sublists; converting dictionary keys to values

description: Python- searching dictionary sublists; converting dictionary keys to values


code:
number = "20"
number
m = re.sub(r'(?<=number )\d+', number, "hello number 10, Agosto 19")
m

rawDescription: python re.sub : replace substring with string

description: Python re.sub : replace substring with string


code:
import working_code
import mock

@mock.patch('working_code.simplejson')
def test_my_fn(mock_simplejson):
    working_code.my_fn('test-args')
    mock_simplejson.dumps.assert_called_with({'args': 'test-args'})

rawDescription: Python mocking- understand the concept and necessity


code:
g = ((t[1], t[0]) for t in mylist)
#call when you need a value
g.next()

rawDescription: swap values in a tuple/list inside a list in python?

description: Swap values in a tuple/list inside a list in python


code:
from hashids import Hashids
hashids = Hashids()

hashid = hashids.encode(123) # 'Mj3'
intid = hashids.decode('Mj3') # 123

rawDescription: How to use encrypted parameters in url python flask?

description: Use encrypted parameters in url python flask


code: [c in range(k[0], k[1]+1) for k in b for c in a]

rawDescription: Python - Apply an conditional function (intersection) to every element of a list


code:
a = A()
method = getattr(a, methodname)
method(1, 2)

rawDescription: Call Class Method from another Class

description: Call Class Method from another Class


code:
class TestAlarm(unittest.TestCase):
    def test_foo(self):
        clock = unittest.mock.Mock()
        clock.t = 0
        with unittest.mock.patch('time.perf_counter', lambda: clock.t):
            alarm = Alarm()
            alarm.setAlarm()
            clock.t += 999.0
            alarm.runStep()
            self.assertFalse(alarm.alarmed)
            clock.t += 1.1
            alarm.runStep()
            self.assertTrue(alarm.alarmed)

rawDescription: Mocking tool for python-3.3

description: Mocking tool for python-3.3


rawDescription: Getting top 3 rows that have biggest sum of columns in `pandas.DataFrame`?


code:
if 'rows' in listReply:
    for row in listReply['rows']:
        ...

rawDescription: Checking if a json response row exists or not in python


rawDescription: Regex re.sub list in a file

description: Regex re.sub list in a file


code: ids = [item['Data']['id'] for item in data['level1']['level2']]

rawDescription: How to iterate a multilevel dict in python

description: Iterate a multilevel dict in python


rawDescription: Passing private variables from class isntance into method outside class?


code:
s = " $25 Sep 5 Porcelain Baby Deer $25 (sunnyvale) pic household items - by owner "
L = s.strip().split(' ') #remove whitespace at ends and break string apart by spaces

rawDescription: Python Web Scrape Write Output to File

description: Python Web Scrape Write Output to File


code:
dim_x = np.linspace(0, 2, 5)
dim_y = np.linspace(0, 2, 5)
X * Y

rawDescription: List comprehension for two variable loop in Python and numpy


rawDescription: Easiest way to store a single timestamp in appengine


code:
#! /usr/bin/python2.7
import sys

for s in (s.strip () for s in open ('test', 'r') ):
    p = int (s.replace ('sys.maxint', str (sys.maxint) ) )
    print (p)

rawDescription: Reading sys.maxint from file

description: Reading sys.maxint from file


code:
D = {int:c_int,float:c_double}
pyarr = [1.2,2.4,3.6]
arr = (D[type(pyarr[0])] * len(pyarr))(*pyarr)
arr
arr[0]
arr[1]
arr[2]

rawDescription: How to get ctypes type object from an ctypes array

description: Get ctypes type object from an ctypes array


code: df.apply(crawl, axis=1)

rawDescription: Loop through csv with Pandas, specific column


rawDescription: Specifying an exit function for an abstract function


code: gameMap = [list(row.rstrip('\n')) for row in open('Room 1.txt')]

rawDescription: Storing 2D arrays within a 2D array - Python


code: lines = (line.strip() for line in f_in)

rawDescription: Iterate over a portion of a list in a list comprehension


code:
import header

header.module1.method() #make all the function calls via header module.

rawDescription: Python "header.py" module

description: Python "header.py" module


code:
f = open("test.yaml")
d = yaml.safe_load(f)

# d is now a dict - {'host': 'example.com', 'work': [{'processes': 1, 'users': [{'username': 'me'}, {'username': 'notme'}]}], 'timeout': 60}

def yaml_count(d, s):
    c = 0
    if isinstance(d, dict):
        for k, v in d.iteritems():
            if k == s: c += 1
            c += yaml_count(v, s)
    elif isinstance(d, list):
        for l in d:
            c += yaml_count(l, s)
    return c

yaml_count(d, "username") # returns 2

rawDescription: Counting how many specific items exist in a YAML file?


code:
from distutils.version import StrictVersion
s = ['1.04','1.6','1.14', '2.02.04', '1.14.01']
max(s, key=StrictVersion)

rawDescription: Python - Returning max number in an array. Errors


code:
while True:
    line = raw_input()
    if line == '0 0 0':
        break
    # do something with `line`

rawDescription: How to read in lines until a certain line?

description: Read in lines until a certain line


code:
import random
SD = 7

nominalList = [1,2,3,4,5]
randomList = [random.normalvariate(0, SD) for n in nominalList]

for i,n in enumerate(nominalList):
    while n + randomList[i] < 0:
        randomList[i] = random.normalvariate(0, SD)

rawDescription: Replacing list values in a while loop

description: Replacing list values in a while loop


rawDescription: More numpy way of iterating through the 'orthogonal' diagonals of a 2D array


code:
tmp = p2[['A', 'B']].merge(p1.reset_index())
tmp

rawDescription: How to get DataFrame indices for multiple rows configurations using Pandas in Python?

description: Get DataFrame indices for multiple rows configurations using Pandas in Python


code:
s = 'xxxxxxxx,yyyyyyyyyyyy,zzzzzzzzz'
parts = s.split(',')
parts

rawDescription: How can i change the position of substring within a string in Python?

description: Change the position of substring within a string in Python


code:
nsmap = {
    'dct': 'http://purl.org/dc/terms/',
    'doc': 'http://www.witbd.org/xmlns/common/document/',
    'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
}

tree.find('.//dct:title', namespaces=nsmap)

rawDescription: Parsing a specific element from XML using Python/ET


code:
filename = 'test.csv'
with open(filename, 'rb') as f, tempfile.NamedTemporaryFile(mode='wb', delete=False) as g:
    writer = csv.writer(g, delimiter = ',')
    for row in csv.reader(f):
        row = [math.sqrt(float(num)) for num in row]
        writer.writerow(row)

rawDescription: Python CSV take square root of data in the field


code:
import sys
src = sys.argv[1]

rawDescription: How do I accept user input on command line for python script instead of prompt

description: Accept user input on command line for python script instead of prompt


code:
import os
import mimetypes

for dirpath, dirnames, filenames in os.walk('/path/to/directory'):
    for filename in filenames:
        if mimetypes.guess_type(filename)[0] == 'text/plain':
            print(os.path.join(dirpath, filename))

rawDescription: How to use os.walk to only list text files

description: Use os.walk to only list text files


code:
f = eval('lambda x: float(x)', {'__builtins__': None})
f('1.1')

rawDescription: Is there a recommended way to load arbitrary functions from text (json) files?


code:
@app.before_request
def _check_user_login():
    if not request.path.startswith('/static/') and \
            not request.path in ['/urls', '/that', '/do', '/not', '/require', '/login']:
        if not 'key' in session:
            return redirect(url_for('your_login_page'))

rawDescription: Flask: Login without authentication

description: Flask: Login without authentication


code:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'C:/users/you/my.db'

rawDescription: Trouble setting up sqlite3 with django! :/


rawDescription: Selecting and sorting on unique foreign key with Django


code:
all_numbers = [int(x) for x in all_numbers]
# This will return [2, 1]

rawDescription: Read a number in a word from a file in python


code:
extent = xedges[0], xedges[-1], yedges[0], yedges[-1]
imshow(wcounts, extent=extent, alpha=0.5)

rawDescription: Generate Weight Map in Color with MatPlotLib?


code:
#kill pid
os.kill(pid, signal.SIGTERM)

#wait for pid to release it back to the system
#this will block until the child is dead
os.waitpid(pid,0)

rawDescription: Call a shell script on a button press & kill it after pressed again


code: tot_items_b = sum(1 for x in b for y in x) #total items in b

rawDescription: Finding common elements from two lists of lists


code:
class BaseRequestHandler(webapp.RequestHandler):
    def __init__(self):
        self.user = None

    def setAuthUser(self):
        user = users.get_current_user()
        if not user:
            self.redirect(users.create_login_url(request.uri))
            return False
        self.user = user
        return True

class AnswerHandler(BaseRequestHandler):
    def get(self):
        if not self.setAuthUser():
            return
        answer = Answer.next_answer_by_user(self.user)
        self.write_template({"answer" : answer})

rawDescription: How to stop execution after self.redirect

description: Stop execution after self.redirect


code:
def sum_spots_inplace(bright, spread, x0, y0, z0):

    # Create x and y indices
    x = np.linspace(-50, 50, 200)
    y = np.linspace(-50, 50, 200)
    z = np.linspace(-50, 50, 200)

    dx = x[None, None, :, None] - x0[:, None, None, None]
    dy = y[None, :, None, None] - y0[:, None, None, None]
    dz = z[None, None, None, :] - z0[:, None, None, None]
    spread = spread[:, None, None, None]
    bright = bright[:, None, None, None]

    s2 = spread * spread
    a = np.exp(-(dx * dx) / s2)
    b = np.exp(-(dy * dy) / s2)
    c = np.exp(-(dz * dz) / s2)

    out = np.zeros((200, 200, 200), dtype=np.uint16)

    for ii in xrange(bright.shape[0]):
        out += bright[ii] * a[ii] * b[ii] * c[ii]

    return out

rawDescription: faster way to make a 3D blob with python?

description: Faster way to make a 3D blob with python


code:
block_list = pygame.sprite.Group()
for i in xrange(10):
    block = Block([i,i])
    block_list.add(block)

rawDescription: How do you make an image move through the screen and then make it disappear in python?

description: Make an image move through the screen and then make it disappear in python


code:
with open(filename) as f:
    data = f.read().splitlines()

rawDescription: Compare two text files to find differences and output them to a new text file


code:
with open('/path/to/file', 'w') as f:
    f.writelines('%s\n' % s for s in a_list_of_strings)

rawDescription: Save list of strings to file

description: Save list of strings to file


code: sys.stderr = sys.stdout = logFile = open( tmpLogFileName, 'a', 0 )

rawDescription: Ouput file redirection in Python

description: Ouput file redirection in Python


code:
class EmailChecker():
    def is_email_correct(self, email):
        pass

def my_side_effect(*args):
    if args[0] == '1':
        return True
    else:
        return False

with mock.patch.object(EmailChecker, 'is_email_correct', side_effect=my_side_effect):
    checker = EmailChecker()
    print(checker.is_email_correct('1'))
    print(checker.is_email_correct('2'))

rawDescription: Python patch object with a side_effect

description: Python patch object with a side_effect


rawDescription: Counting occurence of words in CSV column and writing to another CSV?


code:
@app.task(base=APITask, bind=True)
def call_api(self, url):
    # self will refer to the task instance (because we're using bind=True)
    self.session.get(url)

rawDescription: Initializing a worker with arguments using Celery


rawDescription: What is a "slug" in Django?

description: What is a "slug" in Django


code:
from setuptools import setup

setup(setup_requires=["pbr"], pbr=True)

rawDescription: python setup.py sdist and custom setup keywords don't play together

description: Python setup.py sdist and custom setup keywords don't play together


code: import falcondup.falconpeer

rawDescription: How to import and create my class from my module?

description: Import and create my class from my module


code:
import csv
with open(r'C:\Users\Gord\Desktop\broken.csv', 'rb') as csv_in:
    inpTweets = csv.reader(csv_in, skipinitialspace=True)
    with open(r'C:\Users\Gord\Desktop\output.csv', 'wb') as csv_out:
        writer = csv.writer(csv_out, quoting=csv.QUOTE_NONNUMERIC)
        header_row = True
        for row in inpTweets:
            if header_row:
                header_row = False
            else:
                out_row = [', '.join([row[x] for x in range(1, len(row) - 1)]), 0]
                writer.writerow(out_row)

rawDescription: A single CSV column includes commas - Python


code:
from scipy.optimize import fmin_slsqp

f = lambda x : - (x[0]**0.5 * x[1]**(0.5))

def f_prime(x):
    ddx1 = 0.5 * x[0]**-0.5 * x[1]**0.5
    ddx2 = 0.5 * x[1]**-0.5 * x[0]**0.5
    return [ddx1, ddx2]

f_eq = lambda x: x[0] + x[1] - 10

fmin_slsqp(f, [0.01,0.01], fprime=f_prime, f_eqcons=f_eq)

rawDescription: Constrained Optimization with Scipy for a nonlinear fucntion


code:
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
api = tweepy.API(auth)

tweet = api.get_status(id_of_tweet)
print(tweet.text)

rawDescription: Twitter API - get tweets with specifc id


code:
from django.core.cache import cache

class PostList(generics.ListAPIView):

    def get_queryset(self):
        qs_key = str(self.request.user.id) + '_key'
        if 'refresh' in self.request.QUERY_PARAMS:
            # get a new query set
            qs = Post.objects.all()
            cache.set(qs_key, qs)
        return cache.get(qs_key)

rawDescription: Django Rest Framework Pagination Since ID


code: z = y.split(",")

rawDescription: How to convert JavaScript dictionary into Python syntax

description: Convert JavaScript dictionary into Python syntax


code:
def func(**kwargs):
    retval = {}
    if 'a' in kwargs:
        retval['a'] = funcA(kwargs['a'])
    if 'b' in kwargs:
        retval['b'] = funcB(kwargs['b'])
    return retval

a = func(a = a)['a']
b = func(b = b)['b']
vals = func(a = a, b = b)
a = vals['a']
b = vals['b']

rawDescription: Is there a simple way to have optional output arguments in a python function?


code:
import re
soup.find_all(text='Title')
soup.find_all(text=re.compile('Title'))

rawDescription: BeautifulSoup not finding parents

description: BeautifulSoup not finding parents


code:
import sys
import ConfigParser

class ConfigParserWithComments(ConfigParser.ConfigParser):
    def add_comment(self, section, comment):
        self.set(section, '; %s' % (comment,), None)

    def write(self, fp):
        """Write an .ini-format representation of the configuration state."""
        if self._defaults:
            fp.write("[%s]\n" % ConfigParser.DEFAULTSECT)
            for (key, value) in self._defaults.items():
                self._write_item(fp, key, value)
            fp.write("\n")
        for section in self._sections:
            fp.write("[%s]\n" % section)
            for (key, value) in self._sections[section].items():
                self._write_item(fp, key, value)
            fp.write("\n")

    def _write_item(self, fp, key, value):
        if key.startswith(';') and value is None:
            fp.write("%s\n" % (key,))
        else:
            fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))

config = ConfigParserWithComments()
config.add_section('Section')
config.set('Section', 'key', 'value')
config.add_comment('Section', 'this is the comment')
config.write(sys.stdout)

rawDescription: Adding comment with configparser

description: Adding comment with configparser


code: set(frozenset(s) for s in A)

rawDescription: Convert a list of sets to a set of sets (to find the unique elements)


code:
type(None)
type(None)()

rawDescription: Why does None lack a docstring?

description: Why does None lack a docstring


code: file1_fields.join(file2_fields.map(lambda x: (x[0], x[1:])))

rawDescription: Python Spark How to Map Fields of one rdd to another rdd


code: re.sub(your_regex, lambda m: your_dict[m.group(1)] if m.group(1) in your_dict else m.group(1), your_string)

rawDescription: Python regex. Match and replace roman numerals


rawDescription: Is is possible to run knnMatch for different images on different threads?


code:
df1[1].dtype
df2[1].dtype
df3[1].dtype

rawDescription: Mixed types of elements in DataFrame's column


code: df.A.str.isnumeric().isnull() & (df.A > 0)

rawDescription: how to create dummy variables in Pandas when columns can have mixed types?

description: Create dummy variables in Pandas when columns can have mixed types


code:
from itertools import permutations
list(permutations([2, 3, 4]))

rawDescription: How to find all possible sequences of elements in a list?

description: Find all possible sequences of elements in a list


title: Sample Konfabulator Widget

rawDescription: Adding a '\n' to the end of my dictionary when writing it to file


code:
from math import log10
[reduce(lambda a, x: a * 10**(int(log10(x))+1) + x, t) for t in tuples]

rawDescription: How to turn a tuple into an integer in Python (examples inside)?

description: Turn a tuple into an integer in Python (examples inside)


rawDescription: Parse resulting webpage Python Selenium

description: Parse resulting webpage Python Selenium


code:
class Product(scrapy.Item):
    name = scrapy.Field()
    price = scrapy.Field()
    stock = scrapy.Field()

    def __init__(self, *args, **kwargs):
        super(Product, self).__init__(*args, **kwargs)
        self.fields['@type'] = Field()

rawDescription: Custom field with unconventional character in key for scrappy.Item


code: print(', '.join(yourdict))

rawDescription: Pick a key from a dictionary python

description: Pick a key from a dictionary python


code:
import pickle
from datetime import timedelta
from uuid import uuid4
from redis import Redis
from werkzeug.datastructures import CallbackDict
from flask.sessions import SessionInterface, SessionMixin

class RedisSession(CallbackDict, SessionMixin):

    def __init__(self, initial=None, sid=None, new=False):
        def on_update(self):
            self.modified = True
        CallbackDict.__init__(self, initial, on_update)
        self.sid = sid
        self.new = new
        self.modified = False

class RedisSessionInterface(SessionInterface):
    serializer = pickle
    session_class = RedisSession

    def __init__(self, redis=None, prefix='session:'):
        if redis is None:
            redis = Redis()
        self.redis = redis
        self.prefix = prefix

    def generate_sid(self):
        return str(uuid4())

    def get_redis_expiration_time(self, app, session):
        if session.permanent:
            return app.permanent_session_lifetime
        return timedelta(days=1)

    def open_session(self, app, request):
        sid = request.cookies.get(app.session_cookie_name)
        if not sid:
            sid = self.generate_sid()
            return self.session_class(sid=sid, new=True)
        val = self.redis.get(self.prefix + sid)
        if val is not None:
            data = self.serializer.loads(val)
            return self.session_class(data, sid=sid)
        return self.session_class(sid=sid, new=True)

    def save_session(self, app, session, response):
        domain = self.get_cookie_domain(app)
        if not session:
            self.redis.delete(self.prefix + session.sid)
            if session.modified:
                response.delete_cookie(app.session_cookie_name,
                                       domain=domain)
            return
        redis_exp = self.get_redis_expiration_time(app, session)
        cookie_exp = self.get_expiration_time(app, session)
        val = self.serializer.dumps(dict(session))
        self.redis.setex(self.prefix + session.sid, val,
                         int(redis_exp.total_seconds()))
        response.set_cookie(app.session_cookie_name, session.sid,
                            expires=cookie_exp, httponly=True,
                            domain=domain)

rawDescription: Use database to store session instead of Cookie with Flask


code: pylab.close()     # closes the current figure

rawDescription: Python matplotlib: memory not being released when specifying figure size


code:
from scipy import integrate
integrate.simps([0,0,1,2,3,1,1,1,0,0],dx=6)

rawDescription: How convert [x] into x?

description: How convert [x] into x


code:
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, -z, zdir='z', c= 'red')
plt.savefig("demo.png")

rawDescription: Creating a 3D plot from a 3D numpy array


code: b = Delegate(B, c)

rawDescription: python: super()-like proxy object that starts the MRO search at a specified class

description: Python: super()-like proxy object that starts the MRO search at a specified class


code:
df1.set_index("SubID", inplace=True)
df2.set_index("Subject Number", inplace=True)
df3 = df1.join(df2, how="left")

rawDescription: Add a new column to Pandas DataFrame with coding data from a separate DataFrame without using a Loop?


code: import foo as bar

rawDescription: Where does __import__ get alias names from?


code:
from django.template.defaultfilters import linebreaksbr
text_with_br = linebreaksbr(instance.fieldname)

rawDescription: django + mysql text field format with breakline

description: Django + mysql text field format with breakline


code:
with open('Supercars', 'rb') as f:
    data = pickle.load(f)

rawDescription: Iterating over particular attributes of instances of objects in python


rawDescription: Update selected columns in 2D numpy array with 1D array


code:
for spell in self.spellTimers:
            self.spellTimers[spell]['tick'] += 1

rawDescription: Python too slow - Can I rewrite engine in lower level and cp scripts?


code:
def hidePasswords(L, password):
    for elem in L:
        if elem == password:
            elem = "*"*len(password)
    return L

rawDescription: List as a function argument - modifications discarded


code:
def bar():
    return foobar()

def foobar():
    return "foobar is a cool word, don't you think?"

rawDescription: Python: have a user-defined function as an input while keeping the source-code inaccessible?


rawDescription: numpy pad array with nan, getting strange float instead

description: Numpy pad array with nan, getting strange float instead


code:
import getpass
getpass.getuser()

rawDescription: python: Python script knows user that started this script last time?

description: Python: Python script knows user that started this script last time


rawDescription: Create list out of a particular element of sublists


code:
try:
    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="myElem" and text() != ""]')))
except TimeoutException:
    raise Exception('Unable to find text in this element after waiting 10 seconds')

rawDescription: How to verify an element contains ANY text?

description: Verify an element contains ANY text


code: [{key: dict(value)} for key,value in B.iteritems()]

rawDescription: Aggregate items in dict

description: Aggregate items in dict


rawDescription: Django REST Framework Specify Some Fields Before calling serializer.isValid()


code:
#for python3
import time

timestamp1 = 'Fri Mar 27 15:25:24 NZDT 2015'
timestamp2 = 'Fri Mar 27 15:23:01 NZDT 2015'

pattern = '%a %b %d %H:%M:%S NZDT %Y'

epoch_timestamp1 = int(time.mktime(time.strptime(timestamp1, pattern)))
epoch_timestamp2 = int(time.mktime(time.strptime(timestamp2, pattern)))

if epoch_timestamp1 > epoch_timestamp2:
  print ("Timestamp1 is the latest")
else:
  print ("Timestamp2 is the latest")

rawDescription: Comparing two timestamp strings in Python