code:
b = []
seen = set()
for t in a:
    s = tuple(sorted(t))
    if s not in seen:
        seen.add(s)
        b.append(t)

rawDescription: Find unique tuples in a list(ignore order) while preserving the others' original order in python?


code: os.environ["NLS_LANG"] = ".AL32UTF8"

rawDescription: Writing accented characters to Oracle

description: Writing accented characters to Oracle


code:
basicList.sort() # sort the list
for line in playerDict: # that's not basicList
    print(playerDict[line]) # still no basicList

rawDescription: Alphabetizing a list through class methods


rawDescription: How can I change the value of a node in a python dictionary by following a list of keys?

description: Change the value of a node in a python dictionary by following a list of keys


code: from tkinter import simpledialog, messagebox

rawDescription: Threading with Twisted with Tkinter

description: Threading with Twisted with Tkinter


rawDescription: Send email confirmation after registration django


code:
def tolist(commaseparated):
    return [s.strip() for s in commaseparated.split(',')] if commaseparated else []

directors = tolist(directors)
producers = tolist(producers)
writers   = tolist(writers)

rawDescription: DRY version of similar string processing


code:
ps_stub = ProspectiveSearchStub('./ps.txt', None)
self.testbed._register_stub(PROSPECTIVE_SEARCH_SERVICE_NAME, ps_stub)

rawDescription: Testbed stub for Google app engine prospective search


code:
def func(id1):
    max = cdfs[id1][-1][0]
    rand = random.random()*max
    for upper,id2 in cdfs[id1]:
        if upper>rand:
            return id2
    return None

rawDescription: Random weighted choice

description: Random weighted choice


code:
# flask_app.py or some such python file

from flask import Flask, request, render_template

app = Flask(__name__)

app.route('/', ['GET', 'POST'])
def root_url():
    if request.method == 'POST':
        # Process the post data
        form_value = request.form.get('we_want_this', None)
        return do_something_with_form_value(form_value)
    else:
        # request.method == 'GET'
        # serve a response to the request
        render_template('templates/mytemplate.html')

rawDescription: How to get an input value and parse it as a variable in a python file

description: Get an input value and parse it as a variable in a python file


code:
from scipy.optimize import fmin
from numpy import array
fmin(func_1,array([1,1]))

rawDescription: Scipy fmin argument passing

description: Scipy fmin argument passing


code:
import copy

new_obj = copy.deepcopy(obj)

rawDescription: Copying class instances

description: Copying class instances


code: df.drop(['col_n'], axis=1)

rawDescription: pandas get all columms without one

description: Pandas get all columms without one


code:
import unittest.mock
import contextlib
from functools import partial

def patched(validation, orig_input, *args):
    while True:
        p = orig_input(*args)
        if validation(p):
            break
    return p

@contextlib.contextmanager
def validator(validate_func):
    func = partial(patched, validate_func, input)  # original input reference saved here
    patch = unittest.mock.patch('builtins.input', func)
    patch.start()
    try:
        yield
    finally:
        patch.stop()

validation = lambda x: len(x) <= 10

rawDescription: Context manager to validate data

description: Context manager to validate data


code:
@app.route('/index',methods=['POST'])
def index():
    token = request.form['token']
    test_message(dict(data=token))
    return '1'

rawDescription: Registering server events with flask SocketIO


code: friend = dict(line.split(":", 1) for line in open("file") if line.strip())

rawDescription: How do I take info from a text file and put it into a dictionary (Python)

description: Take info from a text file and put it into a dictionary (Python)


rawDescription: term by term division in python (division termino a termino en python )

description: Term by term division in python (division termino a termino en python )


code:
a = "some-sample-filename-to-split"
"-".join(a.split("-", 2)[:2])

rawDescription: Python: Split text after the second occurrence of character


code: result(rollno, name, *sub_value)

rawDescription: Pass list of elements to named tuple

description: Pass list of elements to named tuple


code:
def f(module, function, *args):
    return(getattr(__import__(module), function)(*args))

f("math", "sqrt", 2)

rawDescription: subscripting python module

description: Subscripting python module


code:
class ComboParser(object):
    def __init__(self,data):
        self.data=data
    def __getattr__(self,key):
        try:
            return ComboParser(self.data[key])
        except TypeError:
            result=[]
            for item in self.data:
                if key in item:
                    try:
                        result.append(item[key])
                    except TypeError: pass
            return ComboParser(result)
    def __getitem__(self,key):
        return ComboParser(self.data[key])
    def __iter__(self):
        if isinstance(self.data,basestring):
            # self.data might be a str or unicode object
            yield self.data
        else:
            # self.data might be a list or tuple
            try:
                for item in self.data:
                    yield item
            except TypeError:
                # self.data might be an int or float
                yield self.data
    def __length_hint__(self):
        return len(self.data)

rawDescription: Complex transforming nested dictionaries into objects in python


code: numpy.where(df_negAssets.index == 59)[0][0] == 2

rawDescription: Map a row index back to the original indexed dataframe


rawDescription: How to calculate ts1 - ts2.interpolate(index=ts1.index)

description: Calculate ts1 - ts2.interpolate(index=ts1.index)


code:
foo = Foo()
foo.appender(1)
foo.Number1

rawDescription: Create class instance variables using a locals()['some_string'] like approach


code:
a = 1
def foo():
    a = 2
foo()

rawDescription: Hand over global variables to methods in python?


code:
getattr(DoThisOrThat, 'dothis')()
getattr(DoThisOrThat(), 'dothis_instance')()
getattr(DoThisOrThat, 'dothis_classmethod')()

rawDescription: What is an elegant way to call one of several (possibly static) member functions by sending arguments to a Python script?


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

# Generate some data...
data = np.random.random((12, 10))
correlation_mat = np.cov(data)
correlation_mat /= np.diag(correlation_mat)

fig, ax = plt.subplots(figsize=(20,11))
ax.set_title('Correlation Matrix')
ticks = ['$F_{sum}$', '$F_{dif}$', '$x_{sum}$', '$x_{dif}$', '$y_{sum}$',
         '$y_{dif}$', '$HLR_a$', '$e1_a$', '$e2_a$', '$HLR_b$', '$e1_b$',
         '$e2_b$'][::-1]

round_mat = np.round(correlation_mat, 2)
table = ax.table(cellText=round_mat, cellLoc='center', bbox=[0, 0, 1, 1],
                rowLabels=ticks, colLabels=ticks)
table.set_fontsize(25)

ax.axis('off')
for key, cell in table.get_celld().iteritems():
    if key[0] == 0 or key[1] == -1:
       cell.set(facecolor='none', edgecolor='none')
    if key[1] == -1:
       cell._loc = 'right'
    elif key[0] == 0:
       cell._loc = 'center'

plt.show()

rawDescription: Setting x-axis frequency in table for Python


code: df.merge(merge, how='left')

rawDescription: Applying a function to a pandas col

description: Applying a function to a pandas col


code:
import random
def makematrix(x,y,minnum, maxnum):
    matrix = []
    for row in range(x):
        matrix.append([])
        for col in range(y):
            matrix[row].append(random.uniform(minnum,maxnum))
    return matrix

print(makematrix(4,4,0,100))

rawDescription: creating a matrix with randomly generated numbers

description: Creating a matrix with randomly generated numbers


code: ','.join(map(lambda x: '%.7g' % x, long_array1.tolist()))

rawDescription: Python list-like string representation of numpy array


code: reader = csv.DictReader(inf, delimiter='\t')

rawDescription: Find a specific header in a CSV file using python 3 code


code:
class Formatter(argparse.HelpFormatter):
    # use defined argument order to display usage
    def _format_usage(self, usage, actions, groups, prefix):
        if prefix is None:
            prefix = 'usage: '

        # if usage is specified, use that
        if usage is not None:
            usage = usage % dict(prog=self._prog)

        # if no optionals or positionals are available, usage is just prog
        elif usage is None and not actions:
            usage = '%(prog)s' % dict(prog=self._prog)
        elif usage is None:
            prog = '%(prog)s' % dict(prog=self._prog)
            # build full usage string
            action_usage = self._format_actions_usage(actions, groups) # NEW
            usage = ' '.join([s for s in [prog, action_usage] if s])
            # omit the long line wrapping code
        # prefix with 'usage:'
        return '%s%s\n\n' % (prefix, usage)

parser = argparse.ArgumentParser(formatter_class=Formatter)

rawDescription: Argparse - do not catch positional arguments with `nargs`.


code:
urllib.parse.quote(string, safe='/', encoding=None, errors=None)
urllib.parse.quote_plus(string, safe='', encoding=None, errors=None)

rawDescription: using python quote_plus with slashes

description: Using python quote_plus with slashes


code: fname = input("File name? ")  # raw_input in 2.x

rawDescription: How to open a text file with Python GUI?

description: Open a text file with Python GUI


code: sorted(d,key=lambda x: [y.zfill(2) for y in x.split('-')])

rawDescription: Sort python dictionary by Keys formatted as strings


code: [(i,[j,k]) for i,j,k in zip(id,x,y)]

rawDescription: Methods of creating a structured array

description: Methods of creating a structured array


rawDescription: Save image or animation in pyglet

description: Save image or animation in pyglet


code:
def main():
    do_some_stuff()
    from twisted.internet import reactor
    stuff_done = start_some_stuff(reactor)
    stuff_done.addCallback(reactor.stop)

if __name__ == '__main__':
    main()

rawDescription: Python + Twisted ... importing classes from different files?


code:
for func, args in args.funcs:
    func(args)

rawDescription: python argparse set behaviour when no arguments provided

description: Python argparse set behaviour when no arguments provided


code:
for idx, item in enumerate(list1):
    if item == 'test_input_1':
        print ('Found "test_input_1" at index %s' % idx)

rawDescription: regex filtering repeatating items with numerics

description: Regex filtering repeatating items with numerics


code:
from flask import Flask
from flask import render_template
from flask import request

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def calculation():
    result = 0
    error = ''
    # you may want to customize your GET... in this case not applicable
    if request.method=='POST':
        # get the form data
        first = request.form['first']
        second = request.form['second']
        if first and second:
            try:
                # do your validation or logic here...
                if int(first)>10 or int(first)<1:
                    raise ValueError
                result = int(first) + int(second)
            except ValueError:
                # you may pass custom error message as you like
                error = 'Please input integer from 1-10 only.'
    # you render the template and pass the context result & error
    return render_template('calculation.html', result=result, error=error)

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

rawDescription: Web UI for Python function

description: Web UI for Python function


code:
import os
import glob
import pandas as pd

path = 'C:/Users/csvfiles'
#fmask = '*.csv'

def get_merged_csv(path, fmask):
    return pd.concat([pd.read_csv(f, index_col=None, header=0)
                      for f in glob.glob(os.path.join(path, fmask))]
           )

df_list = [get_merged_csv(path, fmask)
           for fmask in ['*nba.csv', '*basketball.csv', '*soccer.csv']]

rawDescription: Reading multiple csv files, concatenate list of file names them into a singe DataFrame


code:
#! /usr/bin/env python3
import argparse
import socketserver

def main():
    parser = argparse.ArgumentParser(description='Execute a UDP server demo.')
    parser.add_argument('port', type=int, help='where the server will listen')
    arguments = parser.parse_args()
    host = socketserver.socket.gethostbyname(socketserver.socket.gethostname())
    address = host, arguments.port
    server = socketserver.ThreadingUDPServer(address, ClientRequestHandler)
    server.serve_forever()

class ClientRequestHandler(socketserver.DatagramRequestHandler):

    def handle(self):
        message = self.rfile.read().decode('ascii')
        print('Received:', message)
        response = 'Received: {}'.format(message).encode('ascii')
        self.wfile.write(response)

if __name__ == '__main__':
    main()

rawDescription: Repeating Communications Over UDP

description: Repeating Communications Over UDP


code:
hasattr("abc","__iter__")
hasattr((0,),"__iter__")
hasattr({},"__iter__")
hasattr(set(),"__iter__")

rawDescription: pythonic way to convert variable to list

description: Pythonic way to convert variable to list


code:
class Profile(models.Model):
  user = models.ForeignKey(User, unique=True)
  gender = models.CharField(max_length=1, blank=True)
  hobbies = models.ManyToManyField(Hobbies)

rawDescription: Defining class relations in django

description: Defining class relations in django


code:
@asyncio.coroutine
def coro1():
   yield from asyncio.sleep(1)
   print("coro1")

@asyncio.coroutine
def coro2():
   yield from asyncio.sleep(2)
   print("coro2")

rawDescription: What should I decorate with @asyncio.coroutine for async operations?


code:
import matplotlib.pyplot as plt

def main():
    lines = ([(0, 0), (1, 0), (0, 1)],
             [(0, 0), (1, 1)],
             [(0.5, 0.5), (1, 0.5)],
             )

    fig, ax = plt.subplots()
    artists = []
    for verts in lines:
        x, y = zip(*verts)
        line, = ax.plot(x, y)
        artists.append(line)

    scalar = StrokeScalar(artists, 0.1)
    ax.callbacks.connect('xlim_changed', scalar)
    ax.callbacks.connect('ylim_changed', scalar)

    # Rescale things to leave a bit of room around the edges...
    ax.margins(0.05)

    plt.show()

class StrokeScalar(object):
    def __init__(self, artists, width):
        self.width = width
        self.artists = artists
        # Assume there's only one axes and one figure, for the moment...
        self.ax = artists[0].axes
        self.fig = self.ax.figure

    def __call__(self, event):
        """Intended to be connected to a draw event callback."""
        for artist in self.artists:
            artist.set_linewidth(self.stroke_width)

    @property
    def stroke_width(self):
        positions = [[0, 0], [self.width, self.width]]
        to_inches = self.fig.dpi_scale_trans.inverted().transform
        pixels = self.ax.transData.transform(positions)
        points = to_inches(pixels) * 72
        return points.ptp(axis=0).mean() # Not quite correct...

main()

rawDescription: matplotlib path linewidth connected to figure zoom

description: Matplotlib path linewidth connected to figure zoom


code:
from django.conf.urls import patterns, include, url

urlpatterns = patterns('', url(r'^foo/$', lambda request: None, name='foo'))

rawDescription: How to make a simple Django URLconf and reverse() on it for a test? (getting TypeError: unhashable type: 'list')

description: Make a simple Django URLconf and reverse() on it for a test? (getting TypeError: unhashable type: 'list')


code:
import lxml.html as LH
import urllib2
url = 'http://espn.go.com/nba/team/roster/_/name/bos/boston-celtics'
doc = LH.parse(urllib2.urlopen(url))
print(doc.xpath('''
    //div[@id="my-players-table"]/div//table[1]//tr/td[2]/a/text()''')[1:])

rawDescription: XPath syntax with Python

description: XPath syntax with Python


code:
import random
import tkinter as tk

def roll(n):
    return random.randint(1, n)

def calc():
    thing = var.get()
    if (thing == 'Barbarian'):
        print ()

root = tk.Tk()
root.geometry("%dx%d+%d+%d" % (500, 80, 200, 150))
root.title("Gold Calculator")

selection = tk.StringVar(root)
selection.set('-Select a class-')

choices = ['Barbarian', 'Bard', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Paladin', 'Ranger', 'Rougue', 'Sorcerer', 'Warlock', 'Wizard']

option = tk.OptionMenu(root, selection, *choices)
option.pack(side='left', padx=10, pady=10)

button = tk.Button(root, text="Calculate Gold", command=calc)
button.pack(side='left', padx=20, pady=10)

label = tk.Label(root, textvariable=selection)
label.pack(side='left', padx=20, pady=10)

root.mainloop()

rawDescription: How do you import two different text widgets into one frame in Python?

description: Import two different text widgets into one frame in Python


rawDescription: Typing Greek letters etc. in Python plots


rawDescription: Make all possible combinations of list elements - using python


rawDescription: How to flat and merge values in a dictionary using python?

description: Flat and merge values in a dictionary using python


code:
from django.core.urlresolvers import reverse
from django.views.generic import RedirectView

class QuerystringRedirect(RedirectView):
    """
    Used to redirect to remove GET parameters from url

    e.g. /permalink/?id=10 to /permalink/10/
    """

    def get_redirect_url(self):
        if 'id' in self.request.GET:
            return reverse('target_view', args=(self.request.GET['id'],))
        else:
            raise Http404()

rawDescription: How to redirect url pattern with variables from urls.py in Django?

description: Redirect url pattern with variables from urls.py in Django


code:
def function1():
    #code here
    def function2():
        return foo
    return function2

def function3():
    x = function1()()  # calls function2, returned by function1()

rawDescription: How to call a function that is nested in python

description: Call a function that is nested in python


code:
import pickle

with open('file.pkl', 'rb') as pickle_load:
    lst = pickle.load(pickle_load)
print(lst) # prints [1,2,3,4,5]

rawDescription: What's the fastest way to save/load a large list in Python 2.7?


code: L.sort(key=lambda t: (t[1],-t[2]))

rawDescription: How to sort similar values in a sorted list (based on second value) of tuples based on another value(third value) in the tuple in descending order

description: Sort similar values in a sorted list (based on second value) of tuples based on another value(third value) in the tuple in descending order


code:
def string_hex(string):
    return ':'.join(format(ord(c), 'x') for c in string)

def hex_string(hexa):
    hexgen = (hexa[i:i+2] for i in range(0, len(hexa), 2))
    return ''.join(chr(eval('0x'+n)) for n in hexgen)

def string_bin(string):
    return ':'.join(format(ord(c), 'b') for c in string)

def bin_string(binary):
    bingen = (binary[i:i+7] for i in range(0, len(binary), 7))
    return ''.join(chr(eval('0b'+n)) for n in bingen)

rawDescription: How to convert a word in string to binary

description: Convert a word in string to binary


code:
from zope.interface import Interface, Attribute
class IA(Interface):
    foo = Attribute("foo")

IA.names()
class IB(IA):
    bar = Attribute("bar")

IB.names()
IB.names(all=True)

rawDescription: Why would a subclass of a subclass of zope.interface.Interface not inherit its parents names?


code: ['Lady Gaga', 'Mogwai', 'AC/DC', 'The Raconteurs', 'The XX']

rawDescription: Build list using only list comprehensions, no function


code: b = [elem * (i+1) for i, elem in enumerate(a)]

rawDescription: calculations using both a value from a list and the index of that value?

description: Calculations using both a value from a list and the index of that value


code:
from __future__ import with_statement
import Tkinter
import tempfile
import base64
import zlib

ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
    'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))

_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
    icon_file.write(ICON)

tk = Tkinter.Tk()
tk.iconbitmap(default=ICON_PATH)
label = Tkinter.Label(tk, text="Window with transparent icon.")
label.pack()

tk.mainloop()

rawDescription: Make the logo transparent in Python tkinter 3.4


rawDescription: Asynchronous Flask functions

description: Asynchronous Flask functions


code: num_lines = sum(1 for line in file)

rawDescription: Why aren't the lists populating in this code?


code:
for item in list:
  fo.write("%s\n" % item)

rawDescription: How do i store data collected by "input" onto a text file in python

description: Store data collected by "input" onto a text file in python


code:
from decimal import Decimal
Decimal("12.234").quantize(Decimal("0.00"))

rawDescription: Django: How should I store a money value?


code:
d = __import__('a.b.c', fromlist=['d']).d
d
d = __import__('a.y.c', fromlist=['d']).d
d

rawDescription: How do I pass in variables into python import path

description: Pass in variables into python import path


code:
result = []
for f in glob.glob('*.sh'):
    item = (os.stat(f).st_size, os.path.realpath(f))
    result.append(item)

rawDescription: Python tuple instead of list

description: Python tuple instead of list


code:
while 1:
    q = status_check_cursor.execute('select status from RunningStatus').fetchone()
    if q[0] == 0:
        break

rawDescription: make python wait for stored procedure to finish executing

description: Make python wait for stored procedure to finish executing


code:
import numpy as np

def hist_match(source, template):
    """
    Adjust the pixel values of a grayscale image such that its histogram
    matches that of a target image

    Arguments:
    -----------
        source: np.ndarray
            Image to transform; the histogram is computed over the flattened
            array
        template: np.ndarray
            Template image; can have different dimensions to source
    Returns:
    -----------
        matched: np.ndarray
            The transformed output image
    """

    oldshape = source.shape
    source = source.ravel()
    template = template.ravel()

    # get the set of unique pixel values and their corresponding indices and
    # counts
    s_values, bin_idx, s_counts = np.unique(source, return_inverse=True,
                                            return_counts=True)
    t_values, t_counts = np.unique(template, return_counts=True)

    # take the cumsum of the counts and normalize by the number of pixels to
    # get the empirical cumulative distribution functions for the source and
    # template images (maps pixel value --> quantile)
    s_quantiles = np.cumsum(s_counts).astype(np.float64)
    s_quantiles /= s_quantiles[-1]
    t_quantiles = np.cumsum(t_counts).astype(np.float64)
    t_quantiles /= t_quantiles[-1]

    # interpolate linearly to find the pixel values in the template image
    # that correspond most closely to the quantiles in the source image
    interp_t_values = np.interp(s_quantiles, t_quantiles, t_values)

    return interp_t_values[bin_idx].reshape(oldshape)

rawDescription: Histogram matching of two images in Python 2.x?


code:
import numpy
import cvxopt
n = C1.shape[1]   # This is for readability only

P = C1
q = C2
G = numpy.vstack([C3, -numpy.eye(n), numpy.eye(n)])
h = numpy.hstack([C4, -C7, C8])
A = C5
b = C6

sol = cvxopt.solvers.qp(cvxopt.matrix(P), cvxopt.matrix(q), cvxopt.matrix(G), cvxopt.matrix(h), cvxopt.matrix(A), cvxopt.matrix(b))
x = sol['x']

rawDescription: Python: porting to cvxopt quadratic programming from MATLAB's quadprog


code:
d = {'mohammed': '123456789', 'john': '1230012'}

found = False
for k,v in d.items():
    if k == 'mohammed' and v == '123456789':
        found = True

if found:
    print("Match")
else:
    print("No Match")

rawDescription: Unexpected Behaviour of if statement inside loop over d.items()


code:
import psyco
psyco.full()

rawDescription: Any way to speed up Python and Pygame?

description: Any way to speed up Python and Pygame


code:
def all_same(items):
    return all(x == items[0] for x in items)

rawDescription: Python: determine if all items of a list are the same item


rawDescription: Python 2.5 - Update folder of text files from values stored in a lookup list


rawDescription: Why is hardcoding this list slower than calculating it?


code:
df = DataFrame(randn(10,2),columns=list('AB')).to_hdf('test.h5','df',data_columns=['B'],mode='w',table=True)
store = pd.HDFStore('test.h5')
store
store.get_storer('df').group.table
store.create_table_index('df',columns=['B'],optlevel=9,kind='full')
store.get_storer('df').group.table

rawDescription: ptrepack sortby needs 'full' index

description: Ptrepack sortby needs 'full' index


code:
weekdays = ['Mon', 'Tues', 'Weds', 'Thurs', 'Fri', 'Sat', 'Sun']
mapping = {day: i for i, day in enumerate(weekdays)}
key = df['day'].map(mapping)

rawDescription: Sorting the order of bars in pandas/matplotlib bar plots


code:
cur = conn.cursor()
query = ("""select student,
    sum(if(grade is not null,1,0)) as `test`,
    sum(if(grade < 7,1,0)) as `bad`,
    sum(if(grade < 7,1,0))/sum(if(grade is not null,1,0))*100 as `Pct Bad Grade`
    from grades_database
    where test_date >= '2014-08-01'
    group by student
    order by `Pct Bad Grade` desc;""")
cur.execute(query)

rawDescription: How to Append "Decimal" from MySQL query result in Python


code:
from itertools import islice, chain

def lines_per_n(f, n):
    for line in f:
        yield ''.join(chain([line], itertools.islice(f, n - 1)))

rawDescription: Python: Trying to Deserialize Multiple JSON objects in a file with each object spanning multiple but consistently spaced number of lines


code: nested_lst_of_tuples = [tuple(l) for l in nested_lst]

rawDescription: How to convert nested list of lists into a list of tuples in python 3.3?

description: Convert nested list of lists into a list of tuples in python 3.3


rawDescription: Comparing List to Dictionary in Python

description: Comparing List to Dictionary in Python


code: for f in self.engine.processMovie(): pass

rawDescription: usage of generators as a progression notifier

description: Usage of generators as a progression notifier


code: prices = [float(item['Price']) for item in data['results']]

rawDescription: storing data retrieved from REST api into variables in python

description: Storing data retrieved from REST api into variables in python


code:
from multiprocessing import Pool

# Note: This many threads may make your system unresponsive for a while
p = Pool(100)

# First argument is the function to call,
# second argument is a list of arguments
# (the function is called on each item in the list)
p.map(extractproducts, pagenumberlist)

rawDescription: Python threading module in loops with parameters?


code:
from BeautifulSoup import BeautifulSoup

soup = BeautifulSoup(htmlSource)

rawDescription: Using urllib and BeautifulSoup to retrieve info from web with Python


rawDescription: Blender 2.6: Select object by name through Python


code:
import collections
with open(p) as f:
    indx = collections.Counter(f.read())

rawDescription: Count every character from file

description: Count every character from file


code:
association_table = db.Table('association', db.Model.metadata,
    db.Column('left_id', db.Integer, db.ForeignKey('left.id')),
    db.Column('right_id', db.Integer, db.ForeignKey('right.id'))
)

class Parent(db.Model):
    __tablename__ = 'left'
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship("Child",
                    secondary=association_table)

class Child(db.Model):
    __tablename__ = 'right'
    id = db.Column(db.Integer, primary_key=True)

p = Parent()
c = Child()
p.children.append(c)
db.session.add(p)
db.session.commit()

rawDescription: Flask sqlalchemy many-to-many insert data


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

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

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


code:
text = nltk.word_tokenize("And now for something completely different")
nltk.pos_tag(text)

rawDescription: Identify all Substantives in a Text

description: Identify all Substantives in a Text


rawDescription: how to access key values in a json files dictionaries with python

description: Access key values in a json files dictionaries with python


code:
import Tkinter as tk
import guiOpMenu2

class Omenu(object):

    def __init__(self):
        self.app = tk.Tk()
        self.app.title('test1')
        self.OpMenu()
        self.btn()

    def OpMenu(self):
        self.op = tk.StringVar()
        self.opt =['1', '2']
        self.men = tk.OptionMenu(self.app, self.op, *self.opt)
        self.men.pack()

    def btn(self):
        self.btn_btn = tk.Button(self.app, text='newGui', command=self.test)
        self.btn_btn.pack()

    def test(self):
        win = guiOpMenu2.Omenu2(self.app)

win = Omenu()
win.app.mainloop()

rawDescription: OptionMenu on multiple windows with single mainloop


code:
import sympy.physics.units as units
from sympy import Rational

my_mile = units.Unit('my_mile', 'mile')
my_hour = units.Unit('my_hour', 'hour')

rawDescription: SymPy: How to return an expression in terms of other expression(s)?


code:
if (line[0] == "A") or (line[0] == "C") or (line[0] == "T") or (line[0] == "G"):
    apt.append(line)
    count = count + 1
else:
    line = line.strip()
    pattern = r"\(\.+\)"
    for match in pattern.finditer(line):
        loop.append(apt[count][match.start():match.end()])

rawDescription: Searching multiple times a pattern in a string via regex in python


code: (uniq[index] == int_array).all()

rawDescription: Convert a 3D array to 2D array based on dictionary


rawDescription: How to get all YouTube comments with Python's gdata module?

description: Get all YouTube comments with Python's gdata module


rawDescription: Resources on Professional Python Programming


code:
from itertools import dropwhile
lines = '''\
foo
bar
def set():
    spam
    ham
    eggs
'''.splitlines(True)
list(dropwhile(lambda l: 'def set' not in l, lines))

rawDescription: Adding extra statements in a python list comprehension


code:
import numpy as np

n=1000
x = np.random.rand(n)
y = np.random.rand(n)
s = np.random.rand(n)

r1, corrfun1, rw1 = twopointcorr(x,y,s,0.1)
r2, corrfun2, rw2 = twopointcorr2(x,y,s,0.1)

assert np.allclose(r1, r2)
assert np.allclose(corrfun1, corrfun2)
assert np.allclose(rw1, rw2)

rawDescription: Vectorizing for loop with repeated indices in python


code:
from sqlalchemy.orm import Query

class PostClassQuery(Query):

    def __getitem__(self, item):

        if isinstance(item, slice) and (isinstance(item.start, date) or isinstance(item.stop, date)):
            query = self
            if isinstance(item.start, date):
                query = query.filter(Post.date >= item.start)
            if isinstance(item.stop, date):
                query = query.filter(Post.date < item.stop)
            return query.all()
        else:
            super(PostClassQuery, self).__getitem__(item)

rawDescription: SQLAlchemy dynamic & customized Collection Class


code:
"""Precendence deep dive"""
'hi' and True #returns true regardless of what the contents of the string
'hi' and False #returns false
b = ('hi','bob')
'hi' and 'bob' in b #returns true BUT not because 'hi' is in b!!!
'hia' and 'bob' in b #returns true as the precedence is 'hia' and ('bob' in b)
result2 = 'bob' in b
'hia' and result2 #returns true and shows the precedence more clearly
'hi' and 'boba' in b #returns false

#each string needs to check in b
'hi' in b and 'bob' in b #return true!!
'hia' in b and 'bob' in b #return false!!
'hi' in b and 'boba' in b #return false!! - same as before but now each string is checked separately

rawDescription: Computing Pointwise Mutual Information of a text document using python


code: data = conn.recv(1024)

rawDescription: Simple Python Socket Server Not Taking Conditional Statement


rawDescription: Database-Independent MAX() Function in SQLAlchemy


code:
pattern = re.compile('">(.+?)

')
# ...
links = pattern.findall(html)

rawDescription: Scrape 160.000 pages - too slow

description: Scrape 160.000 pages - too slow


code:
import psutil
for process in psutil.process_iter():
    if process.name == 'python.exe' and process.pid != mypid:
        process.kill() # or send_signal(signal)

rawDescription: Killing all python interpreters but the one which launches kill command


code:
from collections import Counter
Counter("abracadabra")

rawDescription: Nested for-loops and dictionaries in finding value occurrence in string


rawDescription: Python pandas selection - empty dataframe


code:
from multiprocessing import Pool

if __name__ == '__main__':
    pool = Pool(processes=4)
    pool.map(get_page_data, range(1,101))

rawDescription: Distributing a python workload across multiple processes


code:
import itertools
list(itertools.product(range(3), repeat=2))
list(itertools.product(range(3), repeat=3))

rawDescription: Variable for loops with recursion

description: Variable for loops with recursion


code:
with open('c:/HashFiles/Part1.txt') as inf, open('c:/HashFiles/hashes.txt','w') as outf:
    for line in inf:
        data = line.split(',')
        hash = data[4]
        outf.write(hash + '\n')

rawDescription: parsing/extracting data from a text file. Unable to make it work

description: Parsing/extracting data from a text file. Unable to make it work


code:
from subprocess import Popen, PIPE, STDOUT

shell_command = 'bash -i -c "history -r; history"'
event = Popen(shell_command, shell=True, stdin=PIPE, stdout=PIPE,
           stderr=STDOUT)
print(event.communicate()[0].splitlines()[-1])

rawDescription: Run shell builtin command in Python

description: Run shell builtin command in Python


code:
keyword = input("Please enter a keyword: ")
file = open('myFile.txt', encoding='utf-8')
for currentLine, line in enumerate(file):
    if currentLine % 5 == 2:
        if line.rstrip('\n') == keyword:  # <------
            print("Match!")
        else:
            print("No match!")

rawDescription: Reading the contents of a specific line in Python?


code:
group = arg_parser.add_mutually_exclusive_group()
group.add_argument('-c', '--clean', action='store_true')
group.add_argument('-d', '--dirty', action='store_true')

rawDescription: How to forbid two conflicting options

description: Forbid two conflicting options


code: pd.Series(np.einsum('ij,ij->i', df1.values, df2.values), index=df1.index)

rawDescription: Applying np.dot to each row of two DataFrames


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,"lxml")
print(soup)
soup = BeautifulSoup(html.replace("<!--",""),"lxml")
print(soup)
soup.select(".col-sm-3")
soup.select(".col-sm-3")[0].text

rawDescription: accessing html elements BeautifulSoup Python2.7

description: Accessing html elements BeautifulSoup Python2.7


code:
trusted_proxies = {'127.0.0.1'}  # define your own set
route = request.access_route + [request.remote_addr]

remote_addr = next((addr for addr in reversed(route)
                    if addr not in trusted_proxies), request.remote_addr)

rawDescription: How do I safely get the user's real IP address in Flask (using mod_wsgi)?

description: Safely get the user's real IP address in Flask (using mod_wsgi)


code:
import pexpect # $ pip install pexpect

output, status = pexpect.run('top', timeout=2, withexitstatus=1)

rawDescription: How do I obtain the output from a program that uses screen redrawing for use in a terminal screen scraper?

description: Obtain the output from a program that uses screen redrawing for use in a terminal screen scraper


code:
left  = merge_sort( seq[:mid_index], key )
right = merge_sort( seq[mid_index:], key )

rawDescription: Merge sort a list of words

description: Merge sort a list of words


code:
words = inputstr.split()
secondwords = iter(words)
next(secondwords)

output = [' '.join((first, second))
          for first, second in zip(words, secondwords)]

rawDescription: Split strings by 2nd space

description: Split strings by 2nd space


code:
C = []
for a_item, b_item in zip(A,B):
    for _ in range(a_item):
        C.append(b_item)

rawDescription: How to get one number specific times in an array python

description: Get one number specific times in an array python


rawDescription: Templating with both local and global variables using Python `format`


rawDescription: Numpy: Multiple Outer Products

description: Numpy: Multiple Outer Products


code:
class Tester(QtCore.QThread):
    updateText = QtCore.pyqtSignal(str)
    ...
    def run(self):
        self.updateText.emit("color={} status={}".format(self.color, self.status))

rawDescription: Send signals from multiple worker threads to the GUI thread


code:
cur.execute('''SELECT ?, ?
                    FROM OsuOrgs
                    WHERE ORG_LEVEL= ? ''',
                    [ on_level, parent_level, x ])

rawDescription: String parameters in pyodbc

description: String parameters in pyodbc


rawDescription: enumerate is iterating of letters of strings in a list instead of elements

description: Enumerate is iterating of letters of strings in a list instead of elements


code:
class TestSomeFeature(unittest.TestCase):
    test_data_A = ...

    def test_A(self):
        ... #perform test

rawDescription: A long definition of an object inside a Python unit test


rawDescription: Modifying nested dictionaries

description: Modifying nested dictionaries


code: options = [item.name for item in form.find_control('rdoSelect').items]

rawDescription: How to print Radio Control information

description: Print Radio Control information


code:
df['race'] = df.groupby('Athlete').cumcount()+1
#      Athlete Distance Race  Rank    Time  race
# 0    M.Smith     400m    A     1   48.57     1
# 1    A.Moyet     400m    A     2   49.00     1
# 2  C.Marconi     800m    B     5  104.12     1
# 3    M.Smith     800m    B     3  102.66     2

rawDescription: Pivot Pandas Dataframe with a Mix of Numeric and Text Fields


code:
import io
import pandas as pd

df = '''\
category | subcategory | contract | week1 | week2 | week3
cat1     | sub1         | 11001 |      20 |    20 |    10
cat1     | sub1         | 11001 |      0  |    0  |    30
cat1     | sub2         | 11002 |      10 |    20 |    0
cat1     | sub2         | 11003 |      10 |    20 |    0
cat2     | sub3         | 11004 |      10 |    0  |    50
cat2     | sub3         | 11005 |      10 |    20 |    0 '''

df = pd.read_table(io.BytesIO(df), sep=r'\s*[|]\s*')
result = df.groupby(['category', 'subcategory', 'contract']).sum() > 0
result = result.groupby(level=['category','subcategory']).sum().dropna(axis=0)
print(result)

rawDescription: pandas dataframe count uniques with respect to another column

description: Pandas dataframe count uniques with respect to another column


code: next (iter (dict.values()))

rawDescription: Python access to first element in dictionary


code:
def decorator(func):
    @functools.wraps(func)
    def _check_something(*args, **kwargs):
        # some logic in here
        return func(*args, **kwargs)
    _check_something.__wrapped__ = func   # <== add this
    return _check_something

rawDescription: How to assert that a method is decorated with python unittest?

description: Assert that a method is decorated with python unittest


rawDescription: Why is it true that string.encode('utf-8') != bytes(map(ord, string))?


code:
def inf_log(arr,copy=False):
    mask= (arr<=1)
    notmask= ~mask
    if copy==True:
        out=arr.copy()
        out[notmask]=np.log(out[notmask])
        out[mask]=-np.inf
        return out
    else:
        arr[notmask]=np.log(arr[notmask])
        arr[mask]=-np.inf

rawDescription: numpy: log with -inf not nans

description: Numpy: log with -inf not nans


code:
class MyObject:
    def __init__(self, text, pixbuf):
        self.text = text
        self.pixbuf = pixbuf

def cell1_cb(col, cell, model, iter):
    obj = model.get_value(iter)
    cell.set_property('text', obj.text)

def cell2_cb(col, cell, model, iter):
    obj = model.get_value(iter)
    cell.set_property('pixbuf', obj.pixbuf)

column = gtk.TreeViewColumn('Pixbuf and text')
cell1 = gtk.CellRenderText()
cell2 = gtk.CellRenderPixbuf()
column.pack_start(cell1, True)
column.pack_start(cell2, False)
column.set_cell_data_func(cell1, cell1_cb)
column.set_cell_data_func(cell2, cell2_cb)

rawDescription: How to render custom columns with a GenericTreeModel

description: Render custom columns with a GenericTreeModel


code: r_err = "%.2f" % r_err

rawDescription: Insert calcuated results into LaTeX document


code:
rng = date_range('1/1/2015', periods=365, freq='D')
d = Series(range(0, len(rng)), index=rng)

rawDescription: Pandas Time Grouper: Custom Ranges

description: Pandas Time Grouper: Custom Ranges


code: Foo.aMethod = deco(Foo.aMethod)

rawDescription: Decorate subclass method

description: Decorate subclass method


code:
def to_str(lst):
    if len(lst) == 0:
        return ''  # return empty string
    count = 0
    while count <= len(lst):
        # convert lst[count] to string before concatenating
        result = str(lst[count]) + to_str(lst[1:])
        count += 1
        return result

rawDescription: Covert a list to string

description: Covert a list to string


rawDescription: How to read csv

description: Read csv


code:
thestring = "b c a"
indices = {c: i for i, c in enumerate(thestring.split())}
sorted(['a', 'b', 'c'], key=indices.get)
sorted(['a', 'b', 'c', 'a', 'c', 'b'], key=indices.get)
sorted(['a', 'a', 'a'], key=indices.get)
sorted(['a', 'e', 'b'], key=indices.get)

rawDescription: Python: sort list of strings in order that they appear in another string


code: u = urllib.unquote(s.decode('utf-8'))

rawDescription: Convert multichar %xx escapes to unicode


code:
expected = re.compile(r'session \d+: running task(s)')
str = "session 1234567890: running(s)"
re.match(expected, str)

rawDescription: Match number with variable number of digits


code:
def sub():
    global name_of_array
    return name_of_array[0, 0]

def main():
    name_of_array = np.zeros((2, 2))
    sub()

rawDescription: Numpy array defined in main not recognized in function


code:
import base64

myFile = open("mock.jpg", "wb")
img = base64.b64decode(value)  #value is the returned string
myFile.write(img)
myFile.close()

rawDescription: How to Define Google Endpoints API File Download Message Endpoint


code:
import pika
import threading

class ConsumerThread(threading.Thread):
    def __init__(self, host, *args, **kwargs):
        super(ConsumerThread, self).__init__(*args, **kwargs)

        self._host = host

    # Not necessarily a method.
    def callback_func(self, channel, method, properties, body):
        print("{} received '{}'".format(self.name, body))

    def run(self):
        credentials = pika.PlainCredentials("guest", "guest")

        connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=self._host,
                                      credentials=credentials))

        channel = connection.channel()

        result = channel.queue_declare(exclusive=True)

        channel.queue_bind(result.method.queue,
                           exchange="my-exchange",
                           routing_key="*.*.*.*.*")

        channel.basic_consume(self.callback_func,
                              result.method.queue,
                              no_ack=True)

        channel.start_consuming()

if __name__ == "__main__":
    threads = [ConsumerThread("host1"), ConsumerThread("host2")]
    for thread in threads:
        thread.start()

rawDescription: Python and RabbitMQ - Best way to listen to consume events from multiple channels?


code:
l = []
entries = {}

for id, fid, title in t:
    entries[id] = entry = {'id': id, 'fid': fid, 'title': title}
    if fid == -1:
        l.append(entry)
    else:
        parent = entries[fid]
        parent.setdefault('son', []).append(entry)

rawDescription: how to change a tuple into dictionary tree in python

description: Change a tuple into dictionary tree in python


code:
for tv in vc.findViewsWithAttribute('class', 'android.widget.TextView', root=listview_id):
    text = tv.getText()
    if re.match(re.compile('.*@yahoo.com'), text):
        list_accounts.append(text)

rawDescription: How to use viewclient traverse method in AndroidViewClient

description: Use viewclient traverse method in AndroidViewClient


code:
user = request.user # the doctor, the nurse etc
department = Institution.objects.get(user=user).department

rawDescription: Django: How to filter patients from one department?


code:
def num_digits(n):
    n = abs(n)
    count = 0
    while True:
        count = count + 1
        n = n / 10
        if n == 0:
            break
    return count

rawDescription: How do I get my num_digit function to give me the correct output and how do I get it to return any integer value in python?

description: Get my num_digit function to give me the correct output and how do I get it to return any integer value in python


code: sched = Scheduler(standalone=True)

rawDescription: APscheduler will not stop

description: APscheduler will not stop


code: gems.direction = 'up'

rawDescription: How to use sprite groups in pygame

description: Use sprite groups in pygame


code: l = [object() for i in xrange(100000000)]

rawDescription: Does Python copy references to objects when slicing a list?


code:
folder = '/folder1/folder2/folder3/folder4/folder5'.split('/')
for i in reversed(range(len(folder) + 1)):
    check = '/'.join(folder[:i])
    if lookup.get(check):
        print('{}: {}'.format(check,lookup.get(check)))
        break

#/folder1/folder2/folder3/folder4: include

rawDescription: Best way to work out if a path is in an include or exclude list in python

description: Work out if a path is in an include or exclude list in python


code:
def get_string(node, default=''):
    if node:
        return ', '.join(node.stripped_strings)
    else:
        return default

def get_data(td_princ):
    name = get_string(td_princ.find('span', {'class':'person-link'})).replace('/', '')

    birth = hired = '(missing)'
    for event in td_princ.find('table', {'class': 'events'}).findAll('tr'):
        cnt = [get_string(cell) for cell in event.findAll('td')]
        if len(cnt) == 2:
            if cnt[0] == "event1:":
                birth = cnt[1]
            elif cnt[0] == "event2:":
                hired = cnt[1]
    return (name, birth, hired)

rawDescription: Parsing HTML Python, BeautifulSoup

description: Parsing HTML Python, BeautifulSoup


rawDescription: list index comparison in python

description: List index comparison in python


code: (servqual != "great") or "good" or "lacking" or "poor"

rawDescription: Python: Repeating a function when raw_input is not valid


code:
# Squeezed output array
if A.shape[rem_axis]!=1:
    print('Error: Specified axis is not singleton.')
    A_squeezed = np.array([]) # Set empty as a sign of failure
         # Change it to any other signal value or keep as A itself
else:
    A_squeezed = A.reshape(np.delete(A.shape,rem_axis))

rawDescription: numpy squeeze along an axis wont work

description: Numpy squeeze along an axis wont work


code:
def myfunc(x):
    return x**3 + x

rawDescription: Python find root for non-zero level

description: Python find root for non-zero level


code:
def get_column_sums(matrix):
    result = [0] * len(matrix[0])
    for i in range(len(matrix)):
        for j in range(len(matrix[i])):
            result[j] += matrix[i][j]
    return result

get_column_sums(matrix)

rawDescription: ColSum of matrix from file python

description: ColSum of matrix from file python


code:
from sys import argv
open(argv[1],'w').write(open(argv[2]).read())

rawDescription: In python, how could I make this three line code into a one line code?


code:
import os
import sys
from time import sleep
import os
from subprocess import Popen, CalledProcessError, check_call

p = Popen(["lockfile", "-r", "0", "my.lock"])
p.wait()
if p.returncode == 0:
    with open("my.pid", "w") as f:
        f.write(str(os.getpid()))
else:
    try:
        with open("my.pid") as f:
            # see if process is still running or lockfile
            # is left over from previous run.
            r = f.read()
            check_call(["kill", "-0", "{}".format(r)])
    except CalledProcessError:
        # remove old lock file and create new
        check_call(["rm", "-f", "my.lock"])
        check_call(["lockfile", "-r", "0", "my.lock"])
        # update pid
        with open("my.pid", "w") as out:
            out.write(str(os.getpid()))
        print("Deleted stale lockfile.")
    else:
        print("{} is already running".format(sys.argv[0]))
        print(p.returncode)
        exit(1)
# main body

for i in range(10):
    sleep(1)
    print(1)
check_call(["rm", "-f", "my.lock"])

rawDescription: Checking running python script within the python script


code:
rules = [str(p) for p in ptree.productions()]
rules

rawDescription: Grammar rule extraction from parsed result


code:
with lock:
   print(shared_int)  # Some read operation
# Lock is release once we leave the with block

rawDescription: How to share an integer between threads in python

description: Share an integer between threads in python


rawDescription: Stream results in celery

description: Stream results in celery


code:
class PrereqAdminForm(forms.ModelForm):

    def __init__(self, *args, **kwargs):
        super(PrereqAdminForm, self).__init__(*args, **kwargs)

        self.fields['target_content_type'].queryset = ContentType.objects.filter(your_conditions='something')

class PrereqAdmin(admin.ModelAdmin):
    form = PrereqAdminForm

rawDescription: Django GenericForiegnKey, specify ContentTypes include/exclude for admin


code: map(list, product([1, 0], repeat=2))

rawDescription: How to generate list combinations in Python

description: Generate list combinations in Python


code:
Program
#!/usr/bin/env python
#
# Counts the IP addresses of a log file.
#
# Assumption: the IP address is logged in the first column.
# Example line: 10.1.10.190 http://example.com/t1 404
#

import sys

def extract_ip(line):
    '''Extracts the IP address from the line.
       Currently it is assumed, that the IP address is logged in
       the first column and the columns are space separated.'''
    return line.split()[0]

def increase_count(ip_dict, ip_addr):
    '''Increases the count of the IP address.
       If an IP address is not in the given dictionary,
       it is initially created and the count is set to 1.'''
    if ip_addr in ip_dict:
        ip_dict[ip_addr] += 1
    else:
        ip_dict[ip_addr] = 1

def read_ips(infilename):
    '''Read the IP addresses from the file and store (count)
       them in a dictionary - returns the dictionary.'''
    res_dict = {}
    log_file = file(infilename)
    for line in log_file:
        if line.isspace():
            continue
        ip_addr = extract_ip(line)
        increase_count(res_dict, ip_addr)
    return res_dict

def write_ips(outfilename, ip_dict):
    '''Write out the count and the IP addresses.'''
    out_file = file(outfilename, "w")
    for ip_addr, count in ip_dict.iteritems():
        out_file.write("%5d\t%s\n" % (count, ip_addr))
    out_file.close()

def parse_cmd_line_args():
    '''Return the in and out file name.
       If there are more or less than two parameters,
       an error is logged in the program is exited.'''
    if len(sys.argv)!=3:
        print("Usage: %s [infilename] [outfilename]" % sys.argv[0])
        sys.exit(1)
    return sys.argv[1], sys.argv[2]

def main():
    infilename, outfilename = parse_cmd_line_args()
    ip_dict = read_ips(infilename)
    write_ips(outfilename, ip_dict)

if __name__ == "__main__":
    main()

rawDescription: how to print the output returned from a function in new lines using python?

description: Print the output returned from a function in new lines using python


code:
def random_combinations_sample(lst, element_count, sample_size):
    result = set()
    while len(result) < sample_size:
        indices = random.sample(xrange(len(lst)), element_count)
        sample = tuple(lst[i] for i in sorted(indices))
        result.add(sample)
    return list(result)

rawDescription: Randomize a generator

description: Randomize a generator


code: list = ['Hi my name is joe', 'Hi my name is Sally', 'Hi my name is Bert']

rawDescription: How do I make my list without brackets, commas, and have every element start on a new line?

description: Make my list without brackets, commas, and have every element start on a new line


rawDescription: Scrapy get all children / ignore <br>?

description: Scrapy get all children / ignore <br>


code:
import os
import MySQLdb as mdb
mdb.connect(read_default_file=os.path.realpath(__file__)+"/my.cnf")

rawDescription: crontab unable to MySQLdb query

description: Crontab unable to MySQLdb query


code:
for k, obj in vars().items():
    if k[0] != '_':
       print(type(obj), obj.__doc__)

rawDescription: How do you get a list of objects in a script so that you can print __doc__ on each of them?

description: Get a list of objects in a script so that you can print __doc__ on each of them


rawDescription: beautiful soup get children that are Tags (not Navigable Strings) from a Tag

description: Beautiful soup get children that are Tags (not Navigable Strings) from a Tag


rawDescription: Extract date ("05/Jan/2014") from string


code:
'a' if 1 else 'b' if 1 else 'c'
'a' if 0 else 'b' if 1 else 'c'
'a' if 0 else 'b' if 0 else 'c'

rawDescription: how to add one more elsif into this function?

description: Add one more elsif into this function


code: u'#eeeeee'

rawDescription: Get matplotlib color from style palette

description: Get matplotlib color from style palette


code: canvas = Canvas(tab, borderwidth=0, background=bg)

rawDescription: get default background of ttk.Frame

description: Get default background of ttk.Frame


code:
for filename in files:
    workbook = xlrd.open_workbook(filename )
    for sheet in workbook.sheets():
        Data.append(sheet.cell_value(10,8))

rawDescription: Scraping data from Excel worksheets with different sheet names


rawDescription: How to slice and extend a 2D numpy array?

description: Slice and extend a 2D numpy array


code:
results = []
counter = 0

for iteration in xrange(10 ** 6):  # 1 million times
    counter += 1
    roll = random.randint(1, 6), random.randint(1, 6), random.randint(1, 6)
    if roll == (6, 6, 6):
        results.append(counter)
        counter = 0

rawDescription: Python nested loops issues

description: Python nested loops issues


code:
def map_class(class_):
    table = Table(metadata, class_.__name__, autoload=True)
    mapper(class_, table)

map_class(Person)
map_class(Order)

rawDescription: SQLAlchemy - Models - using dynamic fields - ActiveRecord


code: mx == R

rawDescription: python: Faster local maximum in 2-d matrix

description: Python: Faster local maximum in 2-d matrix


rawDescription: python split string into strings with same language characters

description: Python split string into strings with same language characters


code:
from apt.debfile import DebPackage
from pprint import pprint
pkg = DebPackage('/tmp/wajig_2.7_all.deb')
pprint(pkg.filelist)

rawDescription: Dpkg Python module?

description: Dpkg Python module


code:
list1 = [(12,45,62),(78,35,72),(34,52,75)]
insertionSort(list1)
insertionSort(list1, key=lambda x: x[1])

rawDescription: How do I insertion sort a list of lists?

description: Insertion sort a list of lists


code:
def key(event):
    print ("pressed", repr(event.char))
    if event.char.isdigit():
        append_digit(event.char)
    elif event.char in ('\x08', '\x7f'):
        backspace()

rawDescription: Binding number and operator buttons to calculator buttons


code:
from collections import Counter
f2c = Counter(line.strip('\n') for line in f2)

for line in f1:
    name = line.strip('\n')
    if name in f2c:
        count = f2c[name]

rawDescription: two for loops, second only executes on first iteration python

description: Two for loops, second only executes on first iteration python


code:
import asyncio
import functools
import requests
import time

ts = time.time()
loop = asyncio.get_event_loop()

@asyncio.coroutine
def do_checks():
    futures = []
    for i in range(10):
        futures.append(loop.run_in_executor(None, functools.partial(requests.get, "http://google.com", timeout=3)))

    for req in asyncio.as_completed(futures):
        resp = yield from req
        print(resp.status_code)

loop.run_until_complete(do_checks())
te = time.time()
print("Version A: " + str(te - ts))

ts = time.time()
for i in range(10):
    r = requests.get("http://google.com", timeout=3)
    print(r.status_code)
te = time.time()
print("Version B:  " + str(te - ts))

rawDescription: Asyncio Making HTTP Requests Slower?

description: Asyncio Making HTTP Requests Slower


code:
from django import forms

class SiteAdminForm(forms.ModelForm):

    def clean_domain(self):
        domain = self.cleaned_data.get('domain')
        # Custom validation here

        return domain

class SiteAdmin(admin.ModelAdmin):
    form = SiteAdminForm

admin.site.unregister(Site)
admin.site.register(Site, SiteAdmin)

rawDescription: Validation to site.domain fieald

description: Validation to site.domain fieald


code:
mask = df.applymap(lambda x:isinstance(x, float)).all(1)
df[mask]

rawDescription: Filtering out values in specific columns with Pandas


code:
# Import the sqlite3 module
import sqlite3
# Create a connection and cursor to your database
conn = sqlite3.connect('example.db')
c = conn.cursor()
# Insert bobby
c.execute("""INSERT INTO child VALUES(NULL, 'bobby')""")
# The python module puts the last row id inserted into a variable on the cursor
bobby_id = c.lastrowid
# Insert spot
c.execute("""INSERT INTO dog VALUES(NULL, 'spot')""")
spot_id = c.lastrowid
# Insert the mapping
c.execute("""INSERT INTO child_dog VALUES(?, ?)""", (bobby_id, spot_id));
# Commit
conn.commit()
conn.close()

rawDescription: SQLite foreign key examples

description: SQLite foreign key examples


code:
class Foo: pass

f = Foo()
type(f)
f.__class__
type(f) is Foo
f.__class__ is Foo
isinstance(f, Foo)

rawDescription: using isinstance () gives me True , using type() gives me False in Python

description: Using isinstance () gives me True , using type() gives me False in Python


code:
x1, x2, x3 = symbols('x1 x2 x3')
mat = diag(x1,x2,x3)-eye(3)+ones(3)

rawDescription: How to replace the the diagonal elements of a matrix by a vector in SymPy?

description: Replace the the diagonal elements of a matrix by a vector in SymPy


code:
if "already_selected" in text.tag_names("current"):
    ...

rawDescription: Avoid accessing the same location twice/clicking twice, TkInter Text widget


code:
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_iterable):
    length = w_list.length()
    start, stop = normalize_simple_slice(space, length, w_start, w_stop)
    sequence_w = space.listview(w_iterable)
    w_other = W_ListObject(space, sequence_w)
    w_list.setslice(start, 1, stop-start, w_other)

rawDescription: Why does `mylist[:] = reversed(mylist)` work?


code:
Usage

@set_properties("display background")
class LightDMUser(QObject): pass

rawDescription: Can anyone help condense this Python code?


code:
from django.utils.itercompat import groupby
avatars = Avatar.objects.select_related("user").filter(valid=True).order_by("-user__date_joined")[:32]
grouped_users = groupby(avatars, lambda x: x.user)
user_list = []
for user, avatar_list in grouped_users:
    user.avatar = list(avatar_list)[0]
    user_list.append(user)
# user_list is now what you asked for in the first_place:
# a list of users with their avatars

rawDescription: chaining queries together in Django

description: Chaining queries together in Django


code:
class USBObserverThing(Protocol):
    def connectionMade(self):
        self.factory.usbProto.observers.append(self.emit)

    def connectionLost(self):
        self.factory.usbProto.observers.remove(self.emit)

    def emit(self, output):
        # parse the data, convert to JSON
        output = convertDataToJSON(data)
        self.transport.write(output)

rawDescription: Producing content indefinitely in a separate thread for all connections?


code:
def italic(predecessor):
    x = predecessor
    def successor():
        return "<italic/>" + x() + "</italic>"
    return successor

rawDescription: A better way to write python closures?

description: A better way to write python closures


code:
def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

rawDescription: Elegant way to skip elements in an iterable


code: df['pet1'].str.contains('dog') * 5

rawDescription: Pandas newbie: How do I assign values based on content of columns?


code: out = {f1(keys[i][0], keys[i][1]):(f2(values[i][0]),f2(values[i][1])) for i in range(len(keys))}

rawDescription: Make a dictionary from two lists after applying a function


code:
from multiprocessing import Pool

def test(num):
    if num%2 == 0:
        return True
    else:
        return False

if __name__ == "__main__":
    list_of_datas_to_test = [0, 1, 2, 3, 4, 5, 6, 7, 8]

    p = Pool(4)  # create 4 processes to do our work
    print(p.map(test, list_of_datas_to_test))  # distribute our work

rawDescription: How can I efficiently implement multithreading/multiprocessing in a Python web bot?

description: Efficiently implement multithreading/multiprocessing in a Python web bot


code:
probabilities -= np.amin(probabilities)
probabilities /= np.amax(probabilities)

rawDescription: How to create colormap of confidence estimates for k-Nearest Neighbor Classification

description: Create colormap of confidence estimates for k-Nearest Neighbor Classification


code:
import gmpy2

try:
    range = xrange
except NameError:
    pass

def prime_numbers(limit=1000000):
    '''Prime number generator. Yields the series
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ...
    using Sieve of Eratosthenes.
    '''
    yield 2
    sub_limit = int(limit**0.5)
    oddnums = gmpy2.xmpz(1)
    f_scan0 = oddnums.bit_scan0
    current = 0
    while True:
        current += 1
        current = f_scan0(current)
        prime = 2 * current + 1
        if prime > limit:
            break
        yield prime
        if prime <= sub_limit:
            oddnums.bit_set(iter(range(2*current*(current+1), limit>>1, prime)))

rawDescription: Speed up bitstring/bit operations in Python?


code:
p = re.compile(r'^\s+([\w-]+)\s+.+$', re.MULTILINE)
test_str = "config t\nEnter configuration commands, one per line.  End with CNTL/Z.\nn7k(config)# port-profile demo_ethernet\nn7k(config-port-prof)# ?\n  bandwidth       Set bandwidth informational parameter\n  beacon          Disable/enable the beacon for an interface\n  cdp             Configure CDP interface parameters\n  channel-group   Configure port channel parameters\n  delay           Specify interface throughput delay\n  description     Enter port-profile description of maximum 80 characters"

print(re.findall(p, test_str))

rawDescription: Simple Python Regex to separate the words


code:
def convert_timedelta(duration):
    days, seconds = duration.days, duration.seconds
    hours = seconds // 3600
    minutes = (seconds % 3600) // 60
    seconds = (seconds % 60)
    return days, hours, minutes, seconds

rawDescription: How to convert datetime.timedelta to minutes, hours in Python?

description: Convert datetime.timedelta to minutes, hours in Python


type: Polygon

rawDescription: How to calculate the area of a polygon on the earth's surface using python?

description: Calculate the area of a polygon on the earth's surface using python


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 10)

url = 'http://www.cmegroup.com/trading/energy/crude-oil/light-sweet-crude_quotes_settlements_futures.html'
driver.get(url)

select = Select(driver.find_element_by_id("cmeTradeDate"))
for option in select.options:
    # selecting a value in the dropdown
    select.select_by_value(option.get_attribute("value"))

    # wait for the table to load
    wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, ".cmeProgressPanel")))

    # get the desired price
    selected_price = driver.find_element_by_xpath('//*[@id="settlementsFuturesProductTable"]/tbody/tr[1]/td[6]')
    print(option.text, selected_price.text)

rawDescription: How can I scrape the value/text of an exact <td> using its xpath, after selecting a drop-down using selenium?

description: Scrape the value/text of an exact <td> using its xpath, after selecting a drop-down using selenium


rawDescription: Python append list in loop

description: Python append list in loop


rawDescription: Decoding packed data into a structure

description: Decoding packed data into a structure


code:
obj = MyClass(
    API_KEY,
    API_SECRET,
    OAUTH_TOKEN,
    OAUTH_TOKEN_SECRET,
    **{'data': DATA}
)

rawDescription: best way to pass kwargs arguments to overriden method

description: Pass kwargs arguments to overriden method


code: df.loc[pd.IndexSlice[:, 3, 5], :]

rawDescription: How to filter through pandas pivot table.

description: Filter through pandas pivot table.


code:
class X(object):
    def get_y(self):
        return self.y

    @property
    def y(self):
        return self.get_y()

rawDescription: How to call the property method in a dynamic Django model?

description: Call the property method in a dynamic Django model


code: myThread.setMyAttribute('new value')

rawDescription: Globally-scoped variable: can its value change before it is picked up by the thread?


rawDescription: time.strptime got TypeError: function takes at most 8 arguments (9 given)

description: Time.strptime got TypeError: function takes at most 8 arguments (9 given)


rawDescription: Solving system using linalg with constraints


code:
import numpy as np
infile = file('input_filename.txt')

# Read in the names of the columns from the first row...
names = infile.next().strip().split()

# Make a dtype from these names...
dtype = {'names':names, 'formats':len(names)*[np.float]}

# Read the data in...
data = np.loadtxt(infile, dtype=dtype, delimiter=',')

# Note that data is now effectively 1-dimensional. To access a column,
# index it by name
workers = data['workers']

# Note that this is now one-dimensional... You can't treat it like a 2D array
data[1:10, 3:5] # <-- Raises an error!

data[1:10][['inc1', 'inc2']] # <-- Effectively the same thing, but works..

rawDescription: How to remove 'None' from an Appended Multidimensional Array using numpy

description: Remove 'None' from an Appended Multidimensional Array using numpy


code:
for k, v in sorted(cn.items(), key=lambda x: (-x[1],x[0])):
    print("[{}][{}]".format(k, v))

rawDescription: Counting number of strings in a List with Python


code: v = A[position:][:length]

rawDescription: combining two slicing operations

description: Combining two slicing operations


rawDescription: python numpy array slicing

description: Python numpy array slicing


rawDescription: Choosing between different expression factorizations in SymPy


code:
import threading
import MyClass # This is for you to implement

def RunThreads(outFileName,errFileName):
    # Create a shared object for saving the output of different threads
    outFile = CriticalSection(outFileName)
    # Create a shared object for saving the errors of different threads
    errFile = CriticalSection(errFileName)
    # Run in parallel one thread for each row in the input table
    RunParallelThreads(outFile,errFile)

def RunParallelThreads(outFile,errFile):
    # Create all the parallel threads
    threads = [threading.Thread(target=RunSequentialThreads,args=(outFile,errFile,row)) for row in table]
    # Start all the parallel threads
    for thread in threads: thread.start()
    # Wait for all the parallel threads to complete
    for thread in threads: thread.join()

def RunSequentialThreads(outFile,errFile,row):
    myObject = MyClass()
    for item in row:
        # Create a thread with the arguments given in the current item
        thread = threading.Thread(target=myObject.Run,args=(outFile,errFile,item[0],item[1],item[2]))
        # Start the thread
        thread.start()
        # Wait for the thread to complete, but only up to 600 seconds
        thread.join(600)
        # Terminate the thread if it hasn't completed up to this point
        if thread.isAlive():
            thread._Thread__stop()
            errFile.write('Timeout on arguments: '+item[0]+' '+item[1]+' '+item[2]+'\n')

rawDescription: Threading advice for web crawler - scheduling with single list


code: rest, cf = x>>1, x&1

rawDescription: Carry bit of python bitwise shift

description: Carry bit of python bitwise shift


code:
def split_list(l, sep):
    # occurences
    o = [i for i, x in enumerate(l) if x == sep]
    nl = []
    # first slice
    nl.append(l[:o[0]])
    # middle slices
    for i in range(1, len(o)):
        nl.append(l[o[i-1]+1:o[i]])
    # last slice
    nl.append(l[o[-1]+1:])
    return nl

rawDescription: Splitting a list with a separator

description: Splitting a list with a separator


code:
import socks
import socket
import requests

socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
r = requests.get('http://icanhazip.com')
print(r.text) # check ip

rawDescription: Python3 - Requests with Sock5 proxy

description: Python3 - Requests with Sock5 proxy


code:
class myFactory(WebSocketClientFactory):

    def __init__(self, url, data_processing):
        WebSocketClientFactory.__init__(self, url)
        # here you could have passed whatever parameters
        # later allow you to build up the protocol instance
        self._data_processing = data_processing

    def buildProtocol(self, addr):
        # Use this method to control the creation of the
        # protocol instance.
        # You can either build a protocol here and inject
        # data_func, or use different protocol class type.

        protocol = myWs()
        protocol.data_func = self._data_processing
        return protocol

rawDescription: Using different functions to process messages in a single autobahn WebSocketProtocol


code:
from scrapy.item import Item, Field

class myItems(Item):
    redirect_count = Field()

rawDescription: Count number of redirects in Scrapy

description: Count number of redirects in Scrapy


code:
class OrderDetailForm(forms.ModelForm):
    class Meta:
        model = Order
        fields = ('work_type', 'vendor', 'note')
        widgets = {
            'note': forms.Textarea(),
        }

rawDescription: How do I programmatically set fields in ModelForm in Django?

description: Programmatically set fields in ModelForm in Django


code: exec(dummy_exec_data, {}, {'newsItems': newsItems})

rawDescription: pass list to exec and fill it

description: Pass list to exec and fill it


rawDescription: Python code to access sub-List by index of first element of that sub-List


code:
class MySpider(Spider):

    name = "my_spider"

    def parse(self, response):
        try:
            user_name = Selector(text=response.body).xpath('//*[@id="ft"]/a/@href').extract()[0]
            yield self.make_requests_from_url("https://example.com/" + user_name)
            yield MyItem(user_name)
        except Exception as e:
            pass

rawDescription: Scrapy multiple spiders

description: Scrapy multiple spiders


code:
from itertools import combinations

(a + b for a, b in combinations(xrange(n + 1, 2)))

rawDescription: Python - Sum of numbers

description: Python - Sum of numbers


code:
cdic = default_attribute_dict(lambda: 100)
cdic.hundred
cdic['ten'] = 10
cdic.ten
cdic['ten']

rawDescription: Python 2.7 how to define a super powerful class-style dict object?


code:
while True: # Loop will continue until break is called
    try:
        number = input('Enter') #Asks user to input 7 digit number

        # Checks if the length of the input
            # If not 7 characters long
            # Will then ask for input again due to while loop
        if len(str(number)) != 7:
            print('Incorrect')

            # If is 7 chacters long
        if len(str(number)) == 7:
            print('Okay')
            multiplier = [3,1]
            times = ''
            total = 0
            # example input for number '1234567'
            # list(str(number))
            # '1234567' -> ['1', '2', '3', '4', '5', '6', '7']
            for index, digit in enumerate(list(str(number))):
                # index%2 returns either 0 or 1
                # this is used to get 3 or 1 from multiplier
                # depending on if the index is odd or even

                # for first index and digit from example
                # index = 0, digit = '1'
                # total = total + 1 * 3
                total = total + int(digit)*multiplier[index%2]

                # adds the multiplier value from digit * multiplier[index%2]
                # to times as a string. First value is
                # times = times + str(1 * 3) which is 3
                times = times+str(int(digit)*multiplier[index%2])+', '
            # essentially rounds up to nearest 10
            mof10 = total + (10 - total%10)
            # gets the number it was rounded up by
            checkdigit = mof10 - total
            final = str(number) + str(checkdigit)
            print(times[:-1]) # slice the string to return everything except the last value
            print(total)
            print(mof10)
            print(checkdigit)
            print(final)
            break # exit while loop
        # If the convertion from string to int returns an error
        # meaning a non-number was entered into input eg. !@#$
        # print the following, while loop will continue and prompt again
    except ValueError:
        print('Not a number')

rawDescription: Could someone please go through this code line by line and explain it please? Pseudocode or English


code:
routers.py
from rest_framework import routers
router = routers.SimpleRouter()
router.register('users', MyUserViewSet)

rawDescription: How to implement nested serializer in django rest framework?

description: Implement nested serializer in django rest framework


code: lines = list(reader)

rawDescription: For loop position in csv reader_Python

description: For loop position in csv reader_Python


rawDescription: Calculate conditional means in Pandas

description: Calculate conditional means in Pandas


code:
class A(ModelBase):
    def _infoMethod(self):
        return 'foo'

rawDescription: is it possible to dynamically define a method's name?

description: Is it possible to dynamically define a method's name


code:
print(now_ktm.strftime('%z'))
# +0545

rawDescription: know GMT difference

description: Know GMT difference


code:
import sys
from PyQt4 import QtGui, QtCore

class CustomButton(QtGui.QPushButton):
    custom_clicked = QtCore.pyqtSignal(str, name='customClicked')
    def mousePressEvent(self, event):
        self.custom_clicked.emit("Clicked!")

class SignalsAndSlots(QtGui.QWidget):

    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        layout = QtGui.QHBoxLayout(self)
        self.custom_button = CustomButton("Press Me", self)
        self.custom_button.setObjectName('customButton')
        self.label = QtGui.QLabel("Nothing...", parent=self)
        layout.addWidget(self.custom_button)
        layout.addWidget(self.label)
        QtCore.QMetaObject.connectSlotsByName(self)

    @QtCore.pyqtSlot(str, name='on_customButton_customClicked')
    def autoSlot(self, msg):
        self.label.setText(msg)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    gui = SignalsAndSlots()
    gui.show()
    app.exec_()

rawDescription: pyqt auto connect signal

description: Pyqt auto connect signal


code:
for line in f:
    vowel += sum(ch.isvowel() for ch in line)

rawDescription: How to while loop until the end of a file in Python without checking for empty line?

description: While loop until the end of a file in Python without checking for empty line


rawDescription: timeseries database to use with python

description: Timeseries database to use with python


code:
lis = [[1], [1, 2], [1, 2, 3]]
new_lis = lis[:]                    # creates a shallow copy
[id(x)==id(y) for x,y in zip(lis,new_lis)]
new_lis1 = deepcopy(lis)           # create a deep copy
[id(x)==id(y) for x,y in zip(lis,new_lis1)]

rawDescription: How to copy a list which will not be changed in function call in Python

description: Copy a list which will not be changed in function call in Python


code:
from sqlalchemy import MetaData
m = MetaData()
m.reflect(engine)
for table in m.tables.values():
    print(table.name)
    for column in table.c:
        print(column.name)

rawDescription: List database tables with SQLAlchemy

description: List database tables with SQLAlchemy


code: (new_spin == cluster).all(axis = 1)

rawDescription: Detect ordered pair in numpy array

description: Detect ordered pair in numpy array


code:
import inspect
import importlib
import ast

class Imports(ast.NodeVisitor):
    def visit_Import(self, node):
        print("In Import")
        for imp in node.names:
            if imp.asname is not None:
                print("module name = {}, alias = {}".format(imp.name, imp.asname))
            else:
                print("module name = {}".format(imp.name))
        print()

    def visit_ImportFrom(self, node):
        print("In ImportFrom")
        for imp in node.names:
            if imp.asname is not None:
                print("module = {}\nname = {}\nalias = {}\nlevel = {}\n".
                      format(node.module, imp.name, imp.asname, node.level))
            else:
                print("module = {}\nname = {}\nlevel = {}\n".
                      format(node.module, imp.name, node.level))
        print()

mod = "temp_test"
mod = importlib.import_module(mod)
p = ast.parse(inspect.getsource(mod))
Imports().visit(p)

rawDescription: How to textually find an imported name in a module

description: Textually find an imported name in a module


code:
class profiled(object):
    def __init__(self,f):
        self.__count=0
        self.__f=f
        self.__name__=f.__name__
    def __call__(self,*args,**dargs):
        self.__count+=1
        return self.__f(*args,**dargs)
    def reset(self):
        self.__count=0

rawDescription: Global Variable In a Decorator

description: Global Variable In a Decorator


code:
target_word = 'self'
context_words = ['one', 'hundred', 'context', 'words']
#mine is much shorter than yours! ;)

rawDescription: NLTK Find occurrences of a word within 5 words (left/right) of context words in a corpus


rawDescription: doing xor for binaries in python

description: Doing xor for binaries in python


rawDescription: python s3 get file without knowing exactly the name file

description: Python s3 get file without knowing exactly the name file


code:
max(d['timestamp'], key=int)
max(map(int, d['timestamp']))

rawDescription: How to get max value for a specific key from a python dictionary

description: Get max value for a specific key from a python dictionary


rawDescription: Pandas groupby year object plotting it year over year


rawDescription: Why can I call all these methods that aren't explicitly defined in a class?


code:
counter = tf.Variable(0)
assert counter.dtype == tf.int32_ref

counter_val = tf.identity(counter)
assert counter_val.dtype == tf.int32

rawDescription: How to dereference _ref tensor type in TensorFlow?

description: Dereference _ref tensor type in TensorFlow


code: "Prerequisite: ANT102H5"

rawDescription: XPath: Select Current and Next Node's text by Current Node Attributes


code: encode(self, encoding='utf-8', indent_level=None, formatter='minimal', errors='xmlcharrefreplace')

rawDescription: encode_contents vs encode("utf-8") in Python BeautifulSoup

description: Encode_contents vs encode("utf-8") in Python BeautifulSoup


rawDescription: Is it possible to import class method without instantiating class?


code: df.groupby(['User', 'Resource']).aggregate(lambda _: '+').unstack().fillna('-')

rawDescription: Build access matrix from raw data with python pandas


code:
document = XSCRIPTCONTEXT.getDocument()
search = document.createSearchDescriptor()
search.SearchString = "search for"
search.SearchAll = True
search.SearchWords = True
search.SearchCaseSensitive = False
selsFound = document.findAll(search)
if selsFound.getCount() == 0:
    return
for selIndex in range(0, selsFound.getCount()):
    selFound = selsFound.getByIndex(selIndex)
    selFound.setString("change to")

rawDescription: Python macro for LibreOffice - replace string in text


rawDescription: How to parse text in multiple body tags with BeautifulSoup in Python?

description: Parse text in multiple body tags with BeautifulSoup in Python


code: documents = [f[:-4] for f in os.listdir() if f[-4:] == '.txt']

rawDescription: Python: How to read a directory of texts into a list


rawDescription: convert numpy array to string with delimiter and save to txt file

description: Convert numpy array to string with delimiter and save to txt file


code:
paginator = Paginator(objects, 15)
pr = paginator.page_range

rawDescription: Get record number of total items in ticket paginator


code:
class RSA:
  def __init__(self):
    print("foo")

obj = RSA()
obj is obj

rawDescription: python calling methods of class using different ways

description: Python calling methods of class using different ways


code:
class SonInline(ModelForm):
    model = Son

class FatherForm(ModelForm):
    model = Father

rawDescription: Multiple Forms and Formsets in CreateView


code:
import re

def flat_to_complex(items=None, orig=None):

    if type(items) is dict: # inserts of new objects
        if True: # just to force indentation
            objects = {} # hash-based container for each object
            lists   = {} # hash-based container for each list

            for key,value in items.items():
                has_object_wildcard = re.search(r'^([^_]+)__', key, re.IGNORECASE)
                if bool(has_object_wildcard):
                    objects[has_object_wildcard.group(1)] = None
                elif bool(re.search(r'&&', unicode(value))):
                    lists[key] = str(value).split('&&')

            for list_name, this_list in lists.items():
                items[list_name] = this_list

            for obj_name in objects:

                this_obj = {}
                for key,value in items.items():
                    if key.startswith('{s}__'.format(s=obj_name)):
                        match = re.search(r'__(.+)$', key)
                        this_obj[match.group(1)] = value
                        del(items[key])

                objects[obj_name] = this_obj

            for obj_name, this_obj in objects.items():
                items[obj_name] = this_obj

    elif type(items) is list: # updates to existing objects
        for idx in range(len(items)):
            if type(items[idx]) is dict:
                objects = {} # hash-based container for each object
                lists   = {} # hash-based container for each list

                for key,value in items[idx].items():
                    has_object_wildcard = re.search(r'^([^_]+)__', key, re.IGNORECASE)
                    if bool(has_object_wildcard):
                        objects[has_object_wildcard.group(1)] = None
                    elif bool(re.search(r'&&', unicode(value))):
                        lists[key] = str(value).split('&&')

                for list_name, this_list in lists.items():
                    items[idx][list_name] = this_list

                for obj_name in objects:

                    this_obj = {}
                    for key,value in items[idx].items():
                        if key.startswith('{s}__'.format(s=obj_name)):
                            match = re.search(r'__(.+)$', key)
                            this_obj[match.group(1)] = value
                            del(items[idx][key])

                    objects[obj_name] = this_obj

                for obj_name, this_obj in objects.items():
                    items[idx][obj_name] = this_obj

rawDescription: How can I insert records which have dicts and lists in Flask Eve?

description: Insert records which have dicts and lists in Flask Eve


code:
import pickle
dict = pickle.loads(tcp_recieve())

rawDescription: Python sending dictionary throught TCP

description: Python sending dictionary throught TCP


code: [os.path.join(r, f) for r,ds,fs in os.walk('/start/dir/') for f in fnmatch.filter(fs, '*.html')]

rawDescription: Change directory in python and extract .html filenames through scrapy spider


code:
x=int(input()) #Terminal will stall to allow user to input the first number
y=int(input()) #This will wait for the second number to be inputted

rawDescription: Read in text from STDIN Python

description: Read in text from STDIN Python


code:
from subprocess import Popen, PIPE
p = Popen(['python3', 'echo.py'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
out, err = p.communicate(input="hello!".encode())
assert out.decode().strip() == "hello!"

rawDescription: Python argv and cmd

description: Python argv and cmd


code:
item_dict = {}
for list_index, big_lst in enumerate(item):
    for lst in big_lst:
        if lst[0] in item_dict:
            item_dict[lst[0]] += [lst[1:]+[list_index],] # Append
        else:
            item_dict[lst[0]] = [lst[1:]+[list_index],] # Initialize

rawDescription: Managing big point lists, best approach?

description: Managing big point lists, best approach


rawDescription: Python colorbar ticks are labeled with an offset of +1 and not with specifed values


code:
u = ({k: ','.join(n['like'])} for k, n in df.groupby('max_interest'))
v = ({k: ','.join(n['like'])} for k, n in df.groupby('min_interest'))

rawDescription: pandas get index for a row value

description: Pandas get index for a row value


code:
df = pd.DataFrame(pd.read_clipboard())
df2 = pd.DataFrame(df.set_index('personid').stack(), columns=['location'])
df2.reset_index(inplace=True)
df2.reset_index(inplace=True)
df3 = df2.pivot(index='index', columns='location', values='personid')
df3 = df3.fillna(0)

rawDescription: Pandas series - recording numerical changes


code:
def add_cursor(fig, ax):
    plt.close(fig)

    vline = ax.axvline(1, color='k')
    hline = ax.axhline(0.5, color='k')

    def set_cursor(x, y):
        vline.set_xdata((x, x))
        hline.set_ydata((y, y))
        display(fig)

    interact(set_cursor, x=ax.get_xlim(), y=ax.get_ylim())

rawDescription: IPython Notebook widgets for Matplotlib interactivity


code:
for line in filename:
    fields = line.split(',')
    fields = [x.strip() for x in fields] # Remove whitespace

rawDescription: Parsing CSV to list of tuples without using CSV module


code: b = [np.copy(a) for i in range(3)]

rawDescription: Generating a list of unique numpy arrays


code: result = minimize_scalar(f, bounds=(0, 10.), method='bounded')

rawDescription: How do I solve for x with a known function in equation using numpy/scipy?

description: Solve for x with a known function in equation using numpy/scipy


code: out = abs(z[..., np.newaxis] - z)

rawDescription: Efficiently Calculating a Euclidean Distance Matrix Using Numpy


code:
class Foo(Base):
    __tablename__="foo_tablename"
    bar = Column(String(80))

bar = 'stackoverflow'
session.query(Foo).filter(literal('stackoverflow').like(func.concat(Foo.bar, '%')))

rawDescription: Get all rows where string starts with column value


code:
import re

thing2 = soup("a", text=re.compile(r"\bText\b"))

rawDescription: How to strip characters interfering with Beautiful Soup returning links with specified text?

description: Strip characters interfering with Beautiful Soup returning links with specified text


code: astr = raw_input('Input: ')

rawDescription: Solving a task in cosmology using Python 2.7.5


code:
test1()
test2()

rawDescription: Python concatenation vs append speed on lists


code:
# The default parameters in Matplotlib
with plt.style.context('classic'):
    plt.plot([1, 2, 3, 4])

# Similar to ggplot from R
with plt.style.context('ggplot'):
    plt.plot([1, 2, 3, 4])

rawDescription: Is there a context manager for temporarily changing matplotlib settings?


code:
from bs4 import BeautifulSoup as bs
import urllib
import urllib2
from urlparse import urljoin

baseurl = "http://m-selig.ae.illinois.edu/props/volume-1/data/"

soup = bs(urllib2.urlopen(baseurl), 'lxml')
links = (a["href"] for a in soup.select("a[href*=geom.txt]"))
for link in links:
    urllib.urlretrieve(urljoin(baseurl, link), link)

rawDescription: BeautifulSoup not downloading files as expected


code:
def dot_notation(your_module):
    return your_module.__module__ + "." + your_module.__class__.__name__

rawDescription: How to get a dot notation of a python module?

description: Get a dot notation of a python module


rawDescription: Add percent of total column to Pandas pivot_table


code:
def _get_prop(self):
    return self._prop
def _set_prop(self, value):
    self._prop = value
prop = property(_get_prop, _set_prop)

rawDescription: Future Development in Python 2.4

description: Future Development in Python 2.4


code:
import numpy as np

data = np.loadtxt('yourfile.csv', delimiter=',')
row = data[3] # fourth row e.g.

rawDescription: Extracting certain rows from a csv file using python


code:
import requests
from bs4 import BeautifulSoup

url = "http://www.saksfifthavenue.com/Handbags/shop/_/N-52jzot/Ne-6lvnb5?FOLDER%3C%3Efolder_id=2534374306622829"
response = requests.get(url)

soup = BeautifulSoup(response.content, "html.parser")

for product in soup.select("#product-container [id^=product-]"):
    product_name = product.find(class_="product-designer-name").get_text()
    colors = [elm["data-label"]
              for elm in product.find_all("div", class_="sfa-pa-product-swatches-color")]
    print(product_name, colors)

rawDescription: web scraping using python beautifulsoup getting values after equal sign

description: Web scraping using python beautifulsoup getting values after equal sign


code:
from itertools import islice
next(islice(file_object, times_to_advance, times_to_advance), None)

rawDescription: Advance a file object more than one line as a way of skipping blank lines and lines containing strings


code:
from django.db.models import Q

...
events = Event.objects.filter(Q(event_name__icontains=term)|Q(city__admin1=adminDivision))
...

rawDescription: Django model filter by an empty string

description: Django model filter by an empty string


code:
repr(inf)
float(_)
repr(nan)
float(_)

rawDescription: Float value is equal -1.#IND

description: Float value is equal -1.#IND


rawDescription: Row and Column numbers of selected cells


code:
import re

class Pbm(object):
    """
    Load a Binary Portable Bitmap (PBM) files and provide access to
    its pixels.  See <http://netpbm.sourceforge.net/doc/pbm.html>
    """
    _pbm_re = re.compile(r'''
       (P4)                     # 1. Magic number
       (?:\s+|\#.*\n)*          # Whitespace or comments
       ([0-9]+)                 # 2. Width of image in pixels
       (?:\s+|\#.*\n)*          # Whitespace or comments
       ([0-9]+)                 # 3. Height of image in pixels
       (?:\#.*\n)*              # Possible comments
       \s                       # A single whitespace character
       ([\000-\377]*)           # 4. Raster image data
    ''', re.X)

    def __init__(self, f):
        m = self._pbm_re.match(f.read())
        if not m:
            raise IOError("Can't parse PBM file.")
        self.width = int(m.group(2))             # Width in pixels
        self.height = int(m.group(3))            # Height in pixels
        self.row = (self.width + 7) // 8         # Width in bytes
        self.raster = m.group(4)
        if len(self.raster) != self.height * self.row:
            raise IOError("Size of raster is {} but width x height = {}."
                          .format(len(self.raster), self.height * self.row))

    def getpixel(self, x, y):
        # Negative coordinates are treated as offsets from the end,
        # like Python's slice indexes.
        if x < 0: x += self.width
        if y < 0: y += self.height
        if x < 0 or x >= self.width or y < 0 or y >= self.height:
            raise ValueError("Coords ({},{}) are out of range (0,0)-({},{})."
                             .format(x, y, self.width - 1, self.height - 1))
        return (ord(self.raster[y * self.row + x // 8]) >> (7 - x % 8)) & 1

rawDescription: Calculating pixel in array

description: Calculating pixel in array


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

x = np.random.uniform(-100, 100, 1000)
plt.hist(x, bins=30, range=(-50, 50))
plt.show()

rawDescription: matplotlib hist() autocropping range

description: Matplotlib hist() autocropping range


code:
# Calculate chunksize using same algorithm used internally by pool.map
chunksize, extra = divmod(pcount, CONFIG_POOL_SIZE * 4)
if extra:
   chunksize += 1

pool.imap(dowork, ((p, idx, pcount) for idx,p in enumerate(products)), chunksize=chunksize)
pool.close()
pool.join()

rawDescription: Python MongoDB (PyMongo) Mutliprocessing cursor


code:
def save_single_frame(fig, arts, frame_number, artist_number=-1):
    # make sure everything is hidden
    for frame_arts in arts:
        for art in frame_arts:
            art.set_visible(False)
    # make the one artist we want visible
    arts[frame_number][artist_number].set_visible(True)
    fig.savefig("frame_{}.png".format(frame_number))

rawDescription: Matplotlib save individual "ims" (animation parts) as image


code:
from App.config import getConfiguration
import os

eventlog = getConfiguration().eventlog
logpath = eventlog.handler_factories[0].instance.baseFilename
logfolder = os.path.split(logpath)[0]

rawDescription: How to get the location of a Zope installation from inside an instance?

description: Get the location of a Zope installation from inside an instance


code:
import numpy as np # Import relevant libraries
import cv2
import matplotlib.pyplot as plt

# Read in image
img = cv2.imread('wheat.png')

# Flip the channels as the image is in BGR and cast to float
img = img[:,:,::-1].astype('float32')

# control points for RGB - defined by you
rgb_lookup = np.array([[165,0,38], [222,63,46], [248,142,82],
                      [253,212,129], [254,254,189], [203,232,129],
                      [132,202,102], [42,159,84], [0,104,55]])

# Define number of bins for histogram
num_bins = 64

# Define dummy x keypoint values
x_keypt = np.arange(rgb_lookup.shape[0])

# Define interpolating x values
xp = np.linspace(x_keypt[0], x_keypt[-1], num_bins)

# Define lookup tables for red, green and blue
red_lookup = np.interp(xp, x_keypt, rgb_lookup[:,0])
green_lookup = np.interp(xp, x_keypt, rgb_lookup[:,1])
blue_lookup = np.interp(xp, x_keypt, rgb_lookup[:,2])

# Define final RGB lookup
rgb_final_lookup = np.column_stack([red_lookup, green_lookup, blue_lookup])

# Brute force
# For each pixel we have in our image, find the closest RGB distance
# from this pixel to each pixel in our lookup.  Find the argmin,
# then log into histogram accordingly
hist = np.zeros(num_bins)

# Get the rows and columns of the image
rows = img.shape[0]
cols = img.shape[1]

# For each pixel
for i in np.arange(rows):
  for j in np.arange(cols):
    # Get colour pixel value
    val = img[i,j,:]

    # Find closest distance to lookup
    dists = np.sum((rgb_final_lookup - val)**2.0, axis=1)

    # Get location for histogram
    ind = np.argmin(dists)

    # Increment histogram
    hist[ind] += 1

# Get percentage calculation
hist = 100*hist / (rows*cols)

# Plot histogram
plt.stem(np.arange(num_bins), hist)
plt.title('Histogram of colours')
plt.xlabel('Bin number')
plt.ylabel('Percentage')
plt.show()

rawDescription: How do I create a colour histogram of a custom colour palette in OpenCV?

description: Create a colour histogram of a custom colour palette in OpenCV


rawDescription: Numpy arange over numpy arrays

description: Numpy arange over numpy arrays


code:
import pandas as pd
df = pd.DataFrame(L) #where L is my list of dictionaries
df.index=pd.to_datetime(df.timeline,format='%Y-%m')
df

rawDescription: Appending missing values in list of dictionaries


rawDescription: Build a dictionary with a regular expression


rawDescription: 2D Numpy Array of Strings WITH column access


code:
def double(lst):
    for i in xrange(len(lst)):
        lst[i] *= 2

rawDescription: How to double all the values in a list

description: Double all the values in a list


rawDescription: comparing lists and storing index values if lists match

description: Comparing lists and storing index values if lists match


code:
import inspect
def a():
  return inspect.currentframe()

inspect.getframeinfo(a())
b = inspect.getframeinfo(a())
b.function

rawDescription: Getting the Python function for a code object


code:
#
# Check that the current thread is spawning a child process
#

def assert_spawning(self):
    if not Popen.thread_is_spawning():
        raise RuntimeError(
            '%s objects should only be shared between processes'
            ' through inheritance' % type(self).__name__
            )

rawDescription: Understanding Multiprocessing: Shared Memory Management, Locks and Queues in Python


code:
# views.py
from django.http import HttpResponseForbidden, HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin
from books.models import Author

class RecordInterest(View, SingleObjectMixin):
    """Records the current user's interest in an author."""
    model = Author

    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated():
            return HttpResponseForbidden()

        # Look up the author we're interested in.
        self.object = self.get_object()
        # Actually record interest somehow here!

        return HttpResponseRedirect(reverse('author-detail', kwargs={'pk': self.object.pk}))

rawDescription: What is the difference between Abstract classes and Mixins in Python django