rawDescription: Convert a complex array of array to a list


rawDescription: Average from two list of dictionaries

description: Average from two list of dictionaries


rawDescription: How do I change strings based on some rules?

description: Change strings based on some rules


code:
rows, cols = np.where(adjacency_matrix == 1)
edges = zip(rows.tolist(), cols.tolist())
gr = nx.Graph()
gr.add_edges_from(edges)

rawDescription: How to find the number of edges bewteen any two nodes using networkx?

description: Find the number of edges bewteen any two nodes using networkx


code:
y = [(k,v) for k,v in x if d[v]==k]
y

rawDescription: Python: conditionally delete elements from list


code:
from social.pipeline.user import get_username as social_get_username
from random import randrange

def get_username(strategy, details, user=None, *args, **kwargs):
    result = social_get_username(strategy, details, user=user, *args, **kwargs)
    result['username'] = '-'.join([
        result['username'], strategy.backend.name, str(randrange(0, 1000))
    ])
    return result

rawDescription: Python Social Auth: How to define a different username scheme?


code:
json_loads_byteified('{"Hello": "World"}')
json_loads_byteified('"I am a top-level string"')
json_loads_byteified('7')
json_loads_byteified('["I am inside a list"]')
json_loads_byteified('[[[[[[[["I am inside a big nest of lists"]]]]]]]]')
json_loads_byteified('{"foo": "bar", "things": [7, {"qux": "baz", "moo": {"cow": ["milk"]}}]}')
json_load_byteified(open('somefile.json'))

rawDescription: How to get string objects instead of Unicode ones from JSON in Python?

description: Get string objects instead of Unicode ones from JSON in Python


code:
metadata = df[['transcript_id', 'gene_id', 'gene_name']].copy()
# .copy() is important!

rawDescription: Retaining nuisence coliumns in pandas groupby


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

class User(models.Model):
    location = models.PointField()
    objects = models.GeoManager()

rawDescription: How would I write this query in GeoDjango? (It's a library for geographic calculations in Django)


code: block = Block([20, 20])

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

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


code:
q=arange(1,1+n*m).reshape(n,m) #example input
r=q.T.reshape(-1,k,n)
s=r.swapaxes(1,2)
t=s.reshape(-1,k)

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

description: Slice and extend a 2D numpy array


code:
class A:
    def meth(self):
        foo=1
        bar=2
        def func():
            nonlocal foo,bar
            print (foo,bar)
            foo=2               #changes meth's foo to 2
        func()
        print (foo)
a=A()
a.meth()

rawDescription: Can a function within a class's method access all the local variables within the method?


code: df

rawDescription: Finding hour of daily max using Pandas in Python


code:
usbDevice = ...
usbProto = SomeUSBProtocol()
thingy = USBThingy(reactor, usbDevice, usbProto)
thingy.start()

factory = ServerFactory()
factory.protocol = USBObserverThing
factory.usbProto = usbProto
reactor.listenTCP(12345, factory)
reactor.run()

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


code: SITE_URL = 'http://www.mydomain.com'

rawDescription: a way to have django SITEURL constant in settings.py

description: A way to have django SITEURL constant in settings.py


code: sums3 = np.sum(x, axis=1)

rawDescription: Why does numpy.apply_along_axis seem to be slower than Python loop?


code:
import re

s = "Date>>January 2. 2012 News 122"
m = re.match("^Date>>(\S+)\s+(\d+)\.\s+(\d+)", s)
if m:
   month, day, year = m.groups()
   print("{} {} {}").format(month, day, year)

rawDescription: Python: split string at word

description: Python: split string at word


rawDescription: Running a background process on Windows 7/XP


code: np.where(x > x[0,0], 1, 0).flatten()

rawDescription: array statement in python

description: Array statement in python


code: '{:=^79}'.format(' Thread: {} starting '.format(42))

rawDescription: centering text using .format the right way?

description: Centering text using .format the right way


rawDescription: Splicing lists within a list

description: Splicing lists within a list


code:
kw = {field_name:change} # you're not explicit as to which is the field_name
                         # value you would like to search for.
old_image = model.objects.get(**kw)

rawDescription: django - get field which name is dynamic

description: Django - get field which name is dynamic


code: [item[0].get_internal_type() for item in raw_list._meta.get_fields_with_model()]

rawDescription: How to get a list of datatypes on Django model?

description: Get a list of datatypes on Django model


code:
from __future__ import print_function
import csv
import functools
try:
    from itertools import izip, imap
except ImportError:  # Python 3
    izip = zip
    imap = map
from itertools import permutations, repeat

# Create a dictance dictionary from csv data file with entries like this:
#     (city_a, city_b): float(distance-between-city_a-and-city_b)
# for all pairs of city names in the file.
data_filename = 'european_cities.csv'
dist_dict = {}
with open(data_filename, 'r') as file:
    reader = csv.reader(file, delimiter=';')
    cities = next(reader)  # header row
    num_cities = len(cities)
    for city in cities:  # should be a row of distances for each city
        from_city_iter = repeat(city, num_cities)
        dist_dict.update((pair for pair in izip(izip(from_city_iter, cities),
                                                imap(float, next(reader)))
                            if pair[1])) # skip 0 distances (city_a == city_b)

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2,s3), ..."
    a, b = iter(iterable), iter(iterable)
    next(b)  # advance second iterator one iteration
    return izip(a, b)

for tour in permutations(cities, len(cities)):
    tour += (tour[0],)  # make round trip by appending starting city
    route = '->'.join(tour)
    dist = sum(dist_dict[city_a, city_b] for city_a, city_b in pairwise(tour))
    print('{:^49}: {:,}'.format(route, dist))

rawDescription: I want to calculate the difference between two values in a "distance_table = []" with permutations, how can I use permutations correctly in this case?


rawDescription: Complex Sorting of a List

description: Complex Sorting of a List


code:
class ProductFilter(django_filters.FilterSet):
    def __init__(self, attribute=None, **kwargs):
        super(ProductFilter, self).__init__(**kwargs)

rawDescription: django pass extra arguments to filterset_class

description: Django pass extra arguments to filterset_class


code:
spacereplace = re.compile(b'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I | re.DEBUG)
spacereplace = re.compile(r'(?<!\band)(?<!\bor)\s(?!or\b)(?!and\b)', re.I | re.DEBUG)

rawDescription: Lookarounds in python

description: Lookarounds in python


code: json_dict = defaultdict(lambda: defaultdict(dict))

rawDescription: pythonic way to create 3d dict

description: Pythonic way to create 3d dict


code:
overall_total = 0
for item in items:
    total_price = item[1] * item[2]
    overall_total += total_price

rawDescription: In Python, how do I create a new variable each time a loop goes around, and then send them all to a function to be processed?


code:
def parse(self, response):
    sel = Selector(response)
    sites = sel.xpath('//div[@class="item paid-featured-item"]/div[@class="listing-item"]')
    cars = []
    for site in sites:
        car = CarItem()
        car['ATitle']=site.xpath('.//div[@class="block item-title"]/h3/span[@class="title"]/a/text()').extract()
        cars.append(car)
    return cars

rawDescription: python can't specific xpath for <a> tag

description: Python can't specific xpath for <a> tag


rawDescription: Generate lottery numbers - Convert R statement to Python


code: # locust --clients=20 --hatch-rate=2 --num-request=500

rawDescription: Locust : How to make locust run for a specific amount of time


code:
import requests
from time import sleep

def get_data_count(url):
    try:
        req = requests.get(url)
    except requests.ConnectionError:
        return 'could not get page'

    try:
        return int(req.json['statistics']['dataCount'])
    except TypeError:
        return 'field not found'
    except ValueError:
        return 'not an integer'

def main():
    urls = [
        ('hostnameA', 'http://hostnameA:1234/Service/statistics?%24format=json'),
        ('hostnameA', 'http://hostnameB:1234/Service/statistics?%24format=json'),
        ('hostnameA', 'http://hostnameC:1234/Service/statistics?%24format=json'),
        ('hostnameA', 'http://hostnameD:1234/Service/statistics?%24format=json'),
        ('hostnameA', 'http://hostnameE:1234/Service/statistics?%24format=json')
    ]

    while True:
        print('')
        for name, url in urls:
            res = get_data_count(url)
            print('{name} - {res}'.format(name=name, res=res))
        sleep(10.)

if __name__=="__main__":
    main()

rawDescription: How to parse the JSON coming from all the URL's using Python?

description: Parse the JSON coming from all the URL's using Python


code:
class sub_class(datetime.date):

    def __new__(cls, year, month, day, lst):
        inst = super(sub_class, cls).__new__(cls, year, month, day)
        inst.lst = lst
        return inst

rawDescription: __init__ argument mismatch between super and subclass


rawDescription: How to implement this Depth-First-Search in a Sudoku Solver

description: Implement this Depth-First-Search in a Sudoku Solver


code: my_dict2 = dict((y,x) for x,y in my_dict.iteritems())

rawDescription: switching keys and values in a dictionary in python

description: Switching keys and values in a dictionary in python


rawDescription: Iterating through list of lists of lists and list of tuples


code:
sorted(range(len(a)), key=lambda i: a[i]['name'])
sorted(range(len(a)), key=lambda i: a[i]['age'])

rawDescription: Get sorted list of indices, for a list of dictionaries sorted by a given key


code:
from subprocess import PIPE, Popen
x = Popen(['ls', '-l'], stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=False)
while 1:
    if x.poll() != None:
        break
    _queue.put(x.stdout.readline())
    sleep(0.2)

rawDescription: Popen mixed data stream?

description: Popen mixed data stream


rawDescription: Python for loop doesn't execute last iteration


code:
import scipy.io
from numpy import *
#every row in the *.mat file is 256*256 numbers representing gray scale values
#for each pixel in an image. i.e. if XTrain.mat has 1000 lines than each line
#will be made up of 256*256 numbers and there would be 1000 images in the file.
#The following loads the image into a sciPy matrix where each row is a vector
#of length 256*256, representing an image. This code will need to be switched
#out if you have a different method of storing images.
Xtrain = scipy.io.loadmat('Xtrain.mat')["Xtrain"]
Ytrain = scipy.io.loadmat('Ytrain.mat')["Ytrain"]
Xtest = scipy.io.loadmat('Xtest.mat')["Xtest"]
Ytest = scipy.io.loadmat('Ytest.mat')["Ytest"]
learn(Xtest,Xtrain,Ytest,Ytrain,5) #this lowers the dimension from 256*256 to 5

def learn(testX,trainX,testY,trainY,n):
    pcmat = PCA(trainX,n)
    lowdimtrain=mat(trainX)*pcmat #lower the dimension of trainX
    lowdimtest=mat(testX)*pcmat #lower the dimension of testX
    #run some learning algorithm here using the low dimension matrices for example
    trainset = []

    knnres = KNN(lowdimtrain, trainY, lowdimtest ,k)
    numloss=0
    for i in range(len(knnres)):
        if knnres[i]!=testY[i]:
            numloss+=1
    return numloss

def PCA(Xparam, n):
    X = mat(Xparam)
    Xtranspose = X.transpose()
    A=Xtranspose*X
    return eigs(A,n)

def eigs(M,k):
    [vals,vecs]=LA.eig(M)
    return LM2ML(vecs[:k])

def LM2ML(lm):
    U=[[]]
    temp = []
    for i in lm:
       for j in range(size(i)):
           temp.append(i[0,j])
       U.append(temp)
       temp = []
    U=U[1:]
    return U

rawDescription: Classifiying a set of Images into Classes


code: return Py_BuildValue("(O)", Py_None);

rawDescription: How to return a tuple containing a None value from the C API?

description: Return a tuple containing a None value from the C API


rawDescription: Nested django templates

description: Nested django templates


rawDescription: python: fast and easy way to compare these lists?

description: Python: fast and easy way to compare these lists


code:
from django.contrib.auth.signals import user_logged_in
user_logged_in.connect(do_stuff)

rawDescription: Django Middleware, single action after login


rawDescription: Best way of getting the longest sequence of even digits in an integer


rawDescription: passing variables to a template on a redirect in python

description: Passing variables to a template on a redirect in python


code:
grouped = df.groupby(df.columns.tolist())
index = [gp_keys[0] for gp_keys in grouped.groups.values()]
unique_df = df.reindex(index)
unique_df

rawDescription: Dataframe how to keep only unique rows and plot curve


code:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'mydb',
        'PASSWORD': 'mydb',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    },
    'alias': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'mydb',
        'PASSWORD': 'mydb',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}

rawDescription: Multiple concurrent database transactions with Django?


rawDescription: Arranging keys and values from a dictionary in a csv file - Python


code:
a
b
c

rawDescription: Print a list in python that I'm passing in


code:
urlpatterns = patterns('',

    ...

    (r'^(?P<path>.+)$', 'django.views.generic.simple.redirect_to', {
        'url': '/login/?next=/%(path)s',
        'permanent': False
    }),
)

rawDescription: Django: information leakage problem when using @login_required and setting LOGIN_URL


code:
from collections import Counter
from itertools import groupby
from operator import itemgetter

def ModeList(lst):
    counts = Counter(lst)
    grouped = groupby(counts.most_common(), itemgetter(1))
    return [i[0] for i in next(grouped)[1]]

rawDescription: Mode of a List - Python

description: Mode of a List - Python


code:
from Tkinter import Tk
from twisted.internet import tksupport
root_window = Tk()
tksupport.install(root_window)

rawDescription: Initiating TCP Client after running reactor.run()


code:
#derive 1st column width from longest term
width = 0
for s in column1:
    if len(s) > width :
        width = len(s) + 1

rawDescription: Printing columns of an equal custom set spacing while not letting long strings flow over other columns. (using clint perhaps?)


rawDescription: One Way to Grab both text and percentages regex python


code:
import data
import model

directory = data.load_data()
threshold, fft_size = 10, 1000

data.prescreen_fn(directory, threshold)
data.plot_prescreen_hits(directory)
data.extract_features(directory, fft_size)
model.generate_train_test(directory)
model.SVM_train_test(directory)

rawDescription: Pythonian structure?

description: Pythonian structure


code: sorted((i,k) for i,j in mydict.items() for k in ([j] if type(j) in (int, str) else j))

rawDescription: Python dictionary to sorted tuples, can this be done better?


rawDescription: Changing values in list: Python

description: Changing values in list: Python


code:
def get_first_duplicate(l):
    s = set()
    for x in l:
        if x not in s:
            s.add(x)
        else:
            return x

get_first_duplicate([3, 5, 3])
# returns 3

rawDescription: Python: Compare more numbers

description: Python: Compare more numbers


rawDescription: python regex find/match one or more in a string

description: Python regex find/match one or more in a string


code:
from parse import parse
d_approx_copy = parse(template, out_string).named

rawDescription: Reversing C-style format strings in Python (`%`)


code:
from mymodule.database.dbclasses import MyDBClass
from mymodule.api.apiclasses import MyAPIClass

rawDescription: Proper way to include files

description: Proper way to include files


code:
df2.set_index("ID", inplace=True)  # index df2 in place so you can use join, which merges by index by default
result = df2.join(df1, how="inner") # join df1 by index

rawDescription: Combining DataFrames without Nans

description: Combining DataFrames without Nans


code:
import selenium.webdriver as webdriver
import contextlib

@contextlib.contextmanager
def quitting(thing):
    yield thing
    thing.close()
    thing.quit()

with quitting(webdriver.Firefox()) as driver:
    driver.get(url)
    data = []
    for tr in driver.find_elements_by_xpath('//table[@id="table_id_10"]//tr'):
        tds = tr.find_elements_by_tag_name('td')
        if tds:
            data.append([td.text for td in tds])
print(data)
# [[u'10/15/2011', u'92', u'37'], [u'7/15/2011', u'91', u'27'], [u'4/15/2011', u'90', u'25']]

rawDescription: Using Selenium and python to save a table


code:
projects_and_nodes = {}
for project in projects:
     projects_and_nodes[project] = [node for node in nodes if node.project == project]

rawDescription: Select all of django foreign key model if condition or part if another condition


code:
B = np.arange(5)
np.einsum('i,j->ji', np.ones(1), B)

rawDescription: Is it possible to use einsum to transpose everything?


code: CameraAnimation(TestAction)

rawDescription: How to pass a lambda expression to a C# constructor from an IronPython script?

description: Pass a lambda expression to a C# constructor from an IronPython script


code:
from datetime import datetime, timedelta

def get_weekday(year, dayOfTheYear):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

    userWeekDay = (datetime(year, 1, 1) + timedelta(days=dayOfTheYear)).weekday()
    return days[userWeekDay]

rawDescription: How to calculate day of the week provided the year and the day?

description: Calculate day of the week provided the year and the day


code:
import pandas as pd
columns = pd.MultiIndex.from_tuples(
    zip(['date', 'x', 'y', 'z'],
        ['(yyyy-mm-dd)', '(s)', '(m)', '(kg)']))
data = [['2014-03-12', 1, 2, 3],
        ['2014-03-13', 4, 5, 6]]
df = pd.DataFrame(data, columns=columns)
df

rawDescription: How can I write a csv file with multiple header lines with pandas to_csv()?

description: Write a csv file with multiple header lines with pandas to_csv()


code:
import numpy as np
import pylab as plt
import itertools

import seaborn as sns
n = 13
colors = sns.color_palette("hls", n)
roots = np.roots( [1,] + [0,]*(n-1) + [-1,] )

# Sorted by angle
idx = np.argsort([np.angle(x) for x in roots])
roots = roots[idx]

plt.figure(figsize=(6,6))

for root,c in zip(roots,colors):
    plt.arrow(0,0,root.real,root.imag,ec=c,lw=3)

plt.xlim(-1.25,1.25)
plt.ylim(-1.25,1.25)
plt.show()

rawDescription: MatPlotLib: Plot complex roots unity as arrow-vectors on complex plane


code:
from collections import defaultdict
by_name = defaultdict(list)    # create empty lists automatically
for name, team in teams:
    by_name[name].append(team)

rawDescription: finding unique combinations of elements in a list of lists

description: Finding unique combinations of elements in a list of lists


rawDescription: Heat Map Python

description: Heat Map Python


rawDescription: Efficient counting of related objects in Django


code:
dis.dis(lambda: count(*map(float, x), three=x.pop()))
dis.dis(lambda: count(*[float(y) for y in x], three=x.pop()))

rawDescription: Evaluation order of positional and keyword arguments


rawDescription: Identifying a function call in a python script line in runtime


code:
import copy
board=[[1,2],[3,4]]
temp=copy.deepcopy(board)
temp[1][0]=100
temp
board

rawDescription: Python - Function has a list as argument. How to return another list without changing the first?


code:
def create_dict(x):
    d = defaultdict(int)
    for c in x:
        d[c] += 1
    return d
c = df["path"].str.split(",").apply(create_dict)
pd.DataFrame({n: c.apply(lambda x: x[n]) for n in wordlist})

rawDescription: pandas dataframe count row values

description: Pandas dataframe count row values


code:
result = ', '.join(result)

if len(result) > 0:
    print ( u"Filnament är:\n %s \noch sökresultatet är:\n %s" % (d, result.decode('utf-8')))

rawDescription: Python re.findall fails at UTF-8 while rest of script succeeds


code:
mask = b <= history[..., -1:]
index = argmax(mask, axis=-1)

rawDescription: Gridwise application of the bisection method


code: get_lerp_factor(2, range(8), 6)

rawDescription: How to code a Python function that accepts float, list or numpy.array?

description: Code a Python function that accepts float, list or numpy.array


code:
result=[]
for perm_type,permission_value in permissions_bytype.iteritems():
        permission_value["type"] = perm_type
        result.append(permission_value)

rawDescription: Python dictionary group by a field

description: Python dictionary group by a field


code:
numpy.random.normal(xx, 10)
numpy.random.normal(xx, 100)

rawDescription: Does calling random.normal on an array of values add noise?


code: age + (height - (weight * (iq / 2)))

rawDescription: Learn Python The Hard Way Exercise 21 Extra Credit


rawDescription: Challenge: Creating a list of tuples of different combinations with different value 'options' in each location of the tuple.


rawDescription: regex - how to recognise a pattern until a second one is found

description: Regex - how to recognise a pattern until a second one is found


code:
# Reshape it

array_.reshpe(89,444,5)

# Change it's dtype to str so you can replace NaN by white spaces

array_.astype(str)

# Replace nan by white spaces

array_[array_ == 'nan'] = ''

# Finaly, save it SEE EDIT

rawDescription: Write numpy array to csv file with a specific number of columns


code:
ax.tick_params(axis='x', pad=10)
ax.xaxis.labelpad = 10
plt.xlabel("your labels go here", labelpad=10)

rawDescription: Python: how to reduce spacing between plot and xticks using matplotlib


code:
def flash(event):
    b.config(bg='orange')
    b.after(1000, lambda: b.config(bg='green'))
    # OR  b.after(1000, b.config, {'bg': 'green'})

rawDescription: Button Colour Doesn't Change in Function (Python tkinter)


code:
# Sometimes blobKey is None
self.blobKey = files.blobstore.get_blob_key(file_name)

# We have to make it wait til it works!
for i in range(1,3):
    if(self.blobKey):
         break
    else:
        logging.info("blobKey is still None")
        time.sleep(0.05)
        self.blobKey = files.blobstore.get_blob_key(file_name)

logging.info("Blobkey: "+str(self.blobKey))

rawDescription: Erratic problem with App Engine when writing files directly to the blobstore


code: x = input('Prompt: ')

rawDescription: Python- how do I write user input to an Excel file?


code: df[df.columns[df.var() == 0.0]]

rawDescription: How can you check if a column in a DataFrame is stale?

description: Check if a column in a DataFrame is stale


rawDescription: Finding consecutive segments in a pandas data frame


code:
import operator
filter(operator.isNumberType, list_1)

rawDescription: Is there a way to output the numbers only from a python list?


code:
from collections import Counter

urls = [{"url": key, "nbr": value} for key, value in Counter(list_of_urls).items()]

rawDescription: Python : List of dict, if exists increment a dict value, if not append a new dict


code:
import urlparse

def starPassword(route):
    parsed = urlparse.urlsplit(route)
    if '@' not in parsed.netloc:
        return route

    userinfo, _, location = parsed.netloc.partition('@')
    username, _, password = userinfo.partition(':')
    if not password:
        return route

    userinfo = ':'.join([username, '*****'])
    netloc = '@'.join([userinfo, location])
    parsed = parsed._replace(netloc=netloc)
    return urlparse.urlunsplit(parsed)

rawDescription: Regex-Match password in mail URL

description: Regex-Match password in mail URL


code:
app = Flask(__name__)

manager = Manager(app)
db = SQLAlchemy()
redis = Redis()

def attach_extensions(app):
    db.init_app(app)
    redis.init_app(app)

rawDescription: Flask extensions & its configuration

description: Flask extensions & its configuration


code:
def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

rawDescription: Method to get the max distance (step) between values in python?


code:
import parser, token, symbol, pprint

_map = dict(token.tok_name.items() + symbol.sym_name.items())

def clean_ast(ast):
    if not isinstance(ast, list):
        return ast
    elif len(ast) == 2: # Elide single-child nodes.
        return clean_ast(ast[1])
    else:
        return [_map[ast[0]]] + [clean_ast(a) for a in ast[1:]]

ast = parser.expr('''{

'*': {     # Applies to all data
  isYummy: _,    # Everything must be yummy

  chocolate: {        # To validate, say `validate("chocolate", object)`
    sweet: _,        # chocolate must be sweet (but not necessarily chocolate.*)

    lindt: {          # To validate, say `validate("chocolate.lindt", object)`
      tasty: _,        # Applies only to chocolate.lindt (and not to chocolate.lindt.dark, for e.g.)

      '*': {            # Applies to all data under chocolate.lindt
        smooth: _,  # Could also be written smooth()
        creamy: 1   # Level 1 creamy
      }
# ...
    }
  }
}

}''').tolist()
pprint.pprint(clean_ast(ast))

rawDescription: Tips on how to parse custom file format

description: Tips on how to parse custom file format


rawDescription: Extract sub-lists recursively from main list


code:
from django import template
import bleach
from html5lib.tokenizer import HTMLTokenizer

register = template.Library()

@register.filter(is_safe=True)
def linkify(value):
    return bleach.linkify(value, skip_pre=True, parse_email=True, tokenizer=HTMLTokenizer)

rawDescription: Using Django's urlize with CommonMark

description: Using Django's urlize with CommonMark


code:
import json

json_string = json.dumps(query_string_as_dict)

rawDescription: Parse the following query string into json using python


code: segdists = np.sqrt((d ** 2).sum(axis=1))

rawDescription: Calculating euclidean distance between consecutive points of an array with numpy


code:
table = sub_axes.table(cellText=table_vals,
                       colWidths = [0.15, 0.25],
                       rowLabels=row_labels,
                       loc='right')
table.set_fontsize(14)
the_table.scale(1.5, 1.5)  # may help

rawDescription: How to change the table's fontsize with matplotlib.pyplot?

description: Change the table's fontsize with matplotlib.pyplot


code: reservations = conn.get_all_instances(filters={'name': ['somevalue']})

rawDescription: AWS BOTO - find volume ID of an ec2 instance using tag


code: keys = [x for x,y in posValueD.items() if y == posValueD[aiMove2]]

rawDescription: Returning all keys that have the same corresponding value in a dictionary with python


code: elements = {'2': 'Hydrogen', '8': 'Oxygen'}

rawDescription: How to access part of a dictionary in python?

description: Access part of a dictionary in python


rawDescription: Making a python dictionary from a huge textfile?


code:
na
str(na)
na.reshape((-1,1))
na.reshape((-1,1)).shape

rawDescription: Transform an array of shape (n,) to a numpy array of shape (n,1)


rawDescription: Any way to extract the sign of a number in Python?


code:
import random
from matplotlib import pyplot as plt
from matplotlib import animation

def data_generator(t):
    if t<100:
        return random.sample(range(100), 20)

def init():
    return plt.plot()

def animate(i):
    data = data_generator(i)
    return plt.plot(data, c='k')

fig = plt.figure()
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=1000, interval=1000, blit=True)
plt.show()

rawDescription: Overplotting dynamically

description: Overplotting dynamically


code: (pd.read_csv(f, sep=',', usecols=['Month','Day','AirTemperature']) for f in glob.glob('*.csv'))

rawDescription: Create empty csv file with pandas

description: Create empty csv file with pandas


rawDescription: numpy tile without memory allocation

description: Numpy tile without memory allocation


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

import signal
import logging
from functools import partial
from multiprocessing import Process, Queue
from threading import Thread
from time import sleep

logger = logging.getLogger("mepy-client")

class SocketClientProtocol(object):

    def __init__(self, q_in, q_out, q_binary):
        self.q_in = q_in
        self.q_out = q_out
        self.q_binary = q_binary
        self.running = True
        t = Thread(target=self._loop)
        t.start()
        t = Thread(target=self._loop_binary)
        t.start()

    def _loop(self):
        print("start of loop 2")
        for res in iter(self.q_in.get, None):
            self._handle_msg(res)
        print("end of loop 2")

    def _loop_binary(self):
        print("start of loop 3")
        for res in iter(self.q_binary.get, None):
            self._handle_binary(res)
        print("end of loop 3")

    def _handle_msg(self, msg):
        msg_type = msg[0]
        if msg_type == "stop2":
            self.q_in.put(None)
            self.q_binary.put(None)

    def _put_msg(self, msg):
        self.q_out.put(msg)

    def stop(self):
        print("STOP RECEIVED")
        self.running = False
        self.q_in.put(None)
        self.q_binary.put(None)

    def _handle_binary(self, data):
        pass

    def handle_element(self):
        self._put_msg(["something"])

def run_twisted(q_in, q_out, q_binary):
    s = SocketClientProtocol(q_in, q_out, q_binary)
    signal.signal(signal.SIGINT, partial(signal_handler_child, s))
    while s.running:
        sleep(2)
        s.handle_element()

class MediatorSender(object):

    def __init__(self):
        self.q_in = None
        self.q_out = None
        self.q_binary = None
        self.p = None
        self.running = False

    def start(self):
        if self.running:
            return
        self.running = True
        self.q_in = Queue()
        self.q_out = Queue()
        self.q_binary = Queue()
        print("!!!!START")
        self.p = Process(target=run_twisted,
                         args=(self.q_in, self.q_out, self.q_binary))
        self.p.start()
        self.loop = Thread(target=self._loop)
        self.loop.start()

    def stop(self):
        print("!!!!STOP")
        if not self.running:
            return
        print("STOP2")
        self.running = False
        self.q_out.put(None)

    def _loop(self):
        print("start of loop 1")
        for res in iter(self.q_out.get, None):
            self._handle_msg(res)
        print("end of loop 1")

    def _handle_msg(self, msg):
        self._put_msg(msg)

    def _put_msg(self, msg):
        self.q_in.put(msg)

    def _put_binary(self, msg):
        self.q_binary.put(msg)

    def send_chunk(self, chunk):
        self._put_binary(chunk)

def signal_handler_main(ms, *args):
    ms.stop()

def signal_handler_child(s, *args):
    s.stop()

if __name__ == "__main__":
    ms = MediatorSender()
    signal.signal(signal.SIGINT, partial(signal_handler_main, ms))
    ms.start()
    for i in range(100):
        ms.send_chunk("some chunk of data")
    while ms.loop.is_alive():
        ms.loop.join(9999999)
    print('done main')

description: Python multiprocessing/threading cleanup


code:
from dateutil.parser import _timelex, parser, parserinfo
info = parserinfo(dayfirst=True)
p = parser(info)

rawDescription: How to parse multiple dates from a block of text in Python (or another language)

description: Parse multiple dates from a block of text in Python (or another language)


code:
class UserIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    ingredient = indexes.MultiValueField()

    def prepare_ingredient(self, obj):
        return [i.name for i in obj.ingredient.all()]

    def get_model(self):
        return user

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

rawDescription: Django manytomany field, how to get check for 80% subset/ match?


rawDescription: numpy.loadtxt and tab separated values: data type not understood

description: Numpy.loadtxt and tab separated values: data type not understood


code:
root = Tk()
frame = ttk.Frame(root)
frame.pack(expand=1, fill=BOTH)

maxtick = 1000

inner_frame = ttk.Frame(frame)
inner_frame.pack(expand=1, fill=BOTH, side=LEFT)

# Primary canvas (inner_frame)
canvas = Canvas(inner_frame, bg='#FFF', width=maxtick,
                scrollregion=(0, 0, maxtick, 16 * (128 + 1 + 1)) )
canvas.pack(expand=1, fill=BOTH)

for y in range(1, 128 + 1 + 1):
    canvas.create_line(0, 16 * y - 1, maxtick, 16 * y - 1)

# Vertical scrollbar
vbar = ttk.Scrollbar(frame, orient=VERTICAL, command=canvas.yview)
vbar.pack(fill=Y, side=LEFT)
canvas.configure(yscrollcommand=vbar.set)

root.mainloop()

rawDescription: Tkinter, Canvas refuses to expand or contract when window resized


code:
import win32gui, win32ui, win32con, win32api
hwin = win32gui.GetDesktopWindow()
width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
hwindc = win32gui.GetWindowDC(hwin)
srcdc = win32ui.CreateDCFromHandle(hwindc)
memdc = srcdc.CreateCompatibleDC()
bmp = win32ui.CreateBitmap()
bmp.CreateCompatibleBitmap(srcdc, width, height)
memdc.SelectObject(bmp)
memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
bmp.SaveBitmapFile(memdc, 'screenshot.bmp')

rawDescription: Python windows 7 screenshot without PIL

description: Python windows 7 screenshot without PIL


rawDescription: Google Chart API - Line Chart: Averaging data


code: subset = df.loc[df['condition'] > 5, :].copy()

rawDescription: Avoiding pandas chained selection

description: Avoiding pandas chained selection


code: [int(x[2]) for x in l]

rawDescription: Why use the map() function?

description: Why use the map() function


code:
pygame.gfxdraw.aapolygon(window, (UL, UR, BR, BL), color_L1)
pygame.gfxdraw.filled_polygon(window, (UL, UR, BR, BL), color_L1)

rawDescription: Pygame draw anti-aliased thick line

description: Pygame draw anti-aliased thick line


code:
def foo(l, dtype=long):
    return map(dtype, l)

rawDescription: Python change type of whole list?

description: Python change type of whole list


code: print('\n'.join(['\n'.join(['{}:{}'.format(i,j) for i,j in d.items()]) for d in storeItems]))

rawDescription: Python OrderedDict with dictionary within list?


rawDescription: Which is the best way to label a csv file for being converted into json file in python


code:
import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt

# make up three datasets for testing
x1 = np.linspace(0, 10, num=11, endpoint=True)
x2 = np.linspace(0, 10, num=13, endpoint=True)
x3 = np.linspace(0, 10, num=23, endpoint=True)

y1 = np.cos(-x1**2/9.0) + 0.2*np.random.rand((len(x1)))
y2 = np.cos(-x2**2/9.0) + 0.2*np.random.rand((len(x2)))
y3 = np.cos(-x3**2/9.0) + 0.2*np.random.rand((len(x3)))

# interpolate data
f1 = interp1d(x1, y1,'cubic')
f2 = interp1d(x2, y2,'cubic')
f3 = interp1d(x3, y3,'cubic')

# define common carrier for calculation of average curve
x_all = np.linspace(0, 10, num=101, endpoint=True)

# evaluation of fits on common carrier
f1_int = f1(x_all)
f2_int = f2(x_all)
f3_int = f3(x_all)

# put all fits to one matrix for fast mean calculation
data_collection = np.vstack((f1_int,f2_int,f3_int))

# calculating mean value
f_avg = np.average(data_collection, axis=0)

# plot this example
plt.figure()
plt.hold('on')
plt.plot(x1,y1,'ro',label='row1')
plt.plot(x2,y2,'bo',label='row2')
plt.plot(x3,y3,'go',label='row3')

plt.plot(x_all,f1_int,'r-',label='fit1')
plt.plot(x_all,f2_int,'b-',label='fit2')
plt.plot(x_all,f3_int,'g-',label='fit3')

plt.plot(x_all, f_avg,'k--',label='fit average')
plt.legend(loc=3)

plt.hold('off')
plt.show()

rawDescription: Calculate mean over discrete functions with different amount of sampling points


code:
listWords = ["go", "make", "do", "some", "lot"]
listOccurrences = [2, 4, 8, 1, 5]
listOccurrences, listWords = zip(*sorted(zip(listOccurrences, listWords), reverse=True))
listWords
listOccurrences

rawDescription: Sort two lists in python?

description: Sort two lists in python


code:
import json
import urllib2
url = "http://httpbin.org/get"
response = urllib2.urlopen(url)
data = response.read()
values = json.loads(data)

rawDescription: Reading JSON files from curl in Python

description: Reading JSON files from curl in Python


code:
ds = gdal.Open(path)
...
_myfunc(C.c_void_p(long(ds.this)))

rawDescription: GDAL: Get pointer/handle of underlying C object


code:
proc.stdin.write(bytes(my_str_input + "\n", "ascii"))
proc.stdin.close()  # Ensures the process knows nothing else is coming

rawDescription: Use subprocess.communicate() to pipe stdin without waiting for process


code: df['filter'].astype(object).replace({False: 'fail', True: 'pass'})

rawDescription: making new column in pandas dataframes based on filter

description: Making new column in pandas dataframes based on filter


code:
body=Call(
    func=Name(id='and_', ctx=Load()),
    args=[
        Compare(
            left=Name(id='x', ctx=Load()),
            ops=[Lt()],
            comparators=[Num(n=5)]
        ),
        Compare(
            left=Name(id='x', ctx=Load()),
            ops=[Gt()],
            comparators=[Num(n=-5)]
        )
    ],
    keywords=[],
    starargs=None,
    kwargs=None
)

rawDescription: Defining new semantics for expressions in Python


code: line.set_data([x1, x2], [y1, y2])

rawDescription: Plotting two objects using a 4-item list


code:
# Just 1 indent
from blqblq.lqlqlqlq.bla import (
    fobarbazbarbarbazar
    as foo
) # end at the next line so it's always clear where what ends

from matplotlib.backends.backend_qt4agg import (
    FigureCanvasQTAgg as FigureCanvas
)

rawDescription: Long imports in Python

description: Long imports in Python


code:
with open('ttext.txt') as text,  open('filterlist.txt') as filter_words:
    st = set(map(str.rstrip,filter_words))
    txt = next(text).split()
    out = [word  for word in txt if word not in st]

rawDescription: filter words from one text file in another text file?

description: Filter words from one text file in another text file


code:
import enchant
d = enchant.Dict('de_DE')
d.check("Hello") # False
d.check("Guten") # True

rawDescription: PyEnchant without German dictionary

description: PyEnchant without German dictionary


code:
from py2neo import neo4j

graph_db = neo4j.GraphDatabaseService()
cypher_merge_user = neo4j.CypherQuery(graph_db,
    "MERGE (user:User {screen_name:{name}}) RETURN user")

def get_or_create_user(screen_name):
    """Return the user if exists, create one if not"""
    return cypher_merge_user.execute_one(name=screen_name)

def get_or_create_users(screen_names):
    """Apply the get or create user cypher query to many usernames in a
    batch transaction"""

    batch = neo4j.WriteBatch(graph_db)

    for screen_name in screen_names:
        batch.append_cypher(cypher_merge_user, params=dict(name=screen_name))

    return batch.submit()

root = get_or_create_user("Root")
users = get_or_create_users(["alice", "bob", "charlie"])

rawDescription: Failed WriteBatch Operation with py2neo

description: Failed WriteBatch Operation with py2neo


code:
from django.db.models import Q

b_models = modelB.objects.values('email', 'name')
exclude = Q()
for model in b_models:
    exclude |= (Q(email=model['email']) & Q(name=model['name']))

a_models = modelA.objects.exclude(exclude)

rawDescription: Django ORM - partial match of rows in different tables


code:
if option == 'f':
    x = (s.strip() for s in input().split(','))
    y = (sorted(x, key=lambda z: float(z)))   # sort float ascending
    print(y)

rawDescription: Sort an Input List of Floating Numbers.

description: Sort an Input List of Floating Numbers.


rawDescription: Trying to return numbers in a list

description: Trying to return numbers in a list


code:
def foo(full, inclusive, first_call = True):
    if first_call:
        full, inclusive = map(sorted, (full, inclusive))
    if not full and inclusive:
        return False
    if not inclusive:
        return True
    if inclusive[0] == full[0]:
        inclusive = inclusive[1:]
    return foo(full[1:], inclusive, False)

assert not foo('','kkkcccjjj')
assert not  foo('sun','xun')
assert not  foo('jack','kkkcccjjj')
assert foo('s', 's')
assert foo('jckackjkjc','kkkcccjjj')
assert foo('','')
assert foo('a','')

rawDescription: How to use recursion to determine if characters are in a string?

description: Use recursion to determine if characters are in a string


code:
for customer in seller.feedbacks.all():
    do_something(customer)

rawDescription: Django: Efficient model for ordered list of objects


code:
card = random.choice(Deck)
Deck.remove(card)
MyHand.append(card)

rawDescription: Python 3: How to move an item from list x to list y and remove it from list x?


code:
n = 3         # Number of elements to keep per row
A[np.arange(A.shape[0])[:,None],A.argsort(1)[:,:A.shape[1]-n]] = 0

rawDescription: argpartsort/partsort of 2D array keeping original array dimensions

description: Argpartsort/partsort of 2D array keeping original array dimensions


code: [next(x) for x in map(iter,lis)]

rawDescription: Convert list of single element tuples to list of elements


code:
from itertools import groupby

is_hashtag = lambda w: w.startswith('#') and len(w)>1

tests = """\
    Hello #world I am in #London and it is #sunny today it is #2016
    Hello #London I am in #London and it is #sunny today it is #2016
    Hello #Austin I am at #SXSW and this is just a # hashtag symbol"""

for t in tests.splitlines():
    t = t.strip()
    groups = groupby(t.split(), key=is_hashtag)

    tags = []
    keywords = []

    # get words up to first hashtag
    ishash, words = next(groups)
    keywords.extend(list(words))

    # get first hashtag
    ishash, tag = next(groups)
    tags.extend(list(tag))

    # get words up to next hashtag
    ishash, words = next(groups)
    keywords.extend(list(words))

    # get all remaining hashtags
    for ishash, tag in groups:
        if ishash:
            newtags = list(tag)
            tags.extend(newtags)

    print (t)
    print (tags)
    print (keywords)
    print ('')

rawDescription: Splitting string data into keywords in Python in customized order and store specific keywords seprately


code:
total_pay = 0
hours = int(hours)
wage = float(wage)

for i in range(1,hours+1):
    if i <= 40:
        total_pay += wage
    elif i <= 45:
        total_pay += wage * 1.5
    else:
        total_pay += wage * 2

rawDescription: How to select a range of numbers to multiply an hourly wage by

description: Select a range of numbers to multiply an hourly wage by


rawDescription: Displaying frames from 2 separate videos according to timestamps stored in a text file


code:
import csv
with open('file.txt') as f:
    writer = csv.writer(f, delimiter=' ')
    writer.writerow(list(gen(items)))

rawDescription: Small difference between print in Python 2 and Python 3


code:
def main(global_config, **settings):
    # ...
    from pyramid.settings import asbool
    production_deployment = asbool(settings.get(
               'production_deployment', 'false'))
    settings['production_deployment'] = production_deployment
    config = Configurator(settings=settings)

rawDescription: Is there an easy way to find the runtime environment in Pyramid


code: 'Comp1' and 'Employee'

rawDescription: Multi-Group rules in view to enforce redirect?


rawDescription: Pandas filter rows

description: Pandas filter rows


code:
i = 0
t = (i,)
t[0] is i  # both of them point to the same immutable object
i += 1  # We can't modify an immutable object, changing `i` simply
i
t       # t still points to the same 0
x = y = 1
id(x),id(y)
x += 1
id(x),id(y)       #y still points to the same object

rawDescription: Assignment rules

description: Assignment rules


code:
flowmap = np.array([...]) # your flow map
listOfFlowMaps.append(flowmap) # add the flow map to the list

rawDescription: How to create a vector of Matrices in python

description: Create a vector of Matrices in python


code:
data1
data2

rawDescription: Replacing data to different file

description: Replacing data to different file


code:
import a
x = a.Something()
a.run()

rawDescription: python eval in "library" file

description: Python eval in "library" file


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


code:
class things1tab(wx.Panel):
    def newAddImage(self,parent):
        things2_listbox.append('hi');
        self.listBox.Set(things2_listbox)
        self.listBox.SetSelection(len(things2_listbox)-1)
    def reName(self,parent):
        sel = self.listBox.GetSelection()
        text = self.listBox.GetString(sel)
        renamed = wx.GetTextFromUser('Rename item', 'Rename dialog', text)
        if renamed != '':
            things2_listbox.pop(sel)
            things2_listbox.insert(sel,renamed)
            self.listBox.Set(things2_listbox)
            self.listBox.SetSelection(sel)
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.listBox = wx.ListBox(self, size=(200, -1), choices=things2_listbox, style=wx.LB_SINGLE)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizerMain = wx.BoxSizer()
        self.listBox.Bind(wx.EVT_LISTBOX_DCLICK, self.reName)
        btn = wx.Button(self, label="Test")
        btnTwo = wx.Button(self, label="Test 2")

        self.sizer.Add(self.listBox, proportion=1, flag=wx.TOP | wx.EXPAND | wx.LEFT, border=5)
        btnSizer.Add(btn, 0, wx.ALL, 5)
        btnSizer.Add(btnTwo, 0, wx.ALL, 5)
        self.sizer.Add(btnSizer)
        self.sizerMain.Add(self.sizer, proportion=0, flag=wx.BOTTOM | wx.EXPAND, border=70)
        self.SetSizer(self.sizerMain)

rawDescription: Putting Buttons underneath listbox in wxpython?


rawDescription: Python Time Problem

description: Python Time Problem


code: grouped = ((i, ' '.join(df.text[df.sender_id == i])) for i in uni)

rawDescription: These 8 lines of python + pandas would take ~1 month to execute, help me speed them up?


rawDescription: adding item to dictionary python with a variable name key

description: Adding item to dictionary python with a variable name key


rawDescription: Stream/string/bytearray transformations in Python 3


rawDescription: How do I format code to print with the same width every time?

description: Format code to print with the same width every time


code:
#!/usr/bin/env python

def strip_output(nb):
    for ws in nb.worksheets:
        for cell in ws.cells:
            if hasattr(cell, "outputs"):
                cell.outputs = []
            if hasattr(cell, "prompt_number"):
                del cell["prompt_number"]

if __name__ == "__main__":
    from sys import stdin, stdout
    from IPython.nbformat.current import read, write

    nb = read(stdin, "ipynb")
    strip_output(nb)
    write(nb, stdout, "ipynb")
    stdout.write("\n")

rawDescription: How can I open an IPython notebook without the output?

description: Open an IPython notebook without the output


code:
from decimal import Decimal
from pprint import pprint
meses_ord = {'January': 1, 'February': 2, 'March': 3, 'April': 4}
fechas_ = [{'anyo': 2010, 'horas': Decimal('52.5'), 'month': 'March', 'importe': Decimal('4200.000')},
{'anyo': 2010, 'horas': Decimal('40.0'), 'month': 'February', 'importe':Decimal('3200.000')},
{'anyo': 2010, 'horas': Decimal('42.5'), 'month': 'April', 'importe': Decimal('3400.000')},
{'anyo': 2010, 'horas': Decimal('20.0'), 'month': 'January', 'importe': Decimal('1600.000')}]
pprint(sorted(fechas_, key=lambda entry: meses_ord[entry['month']]))

rawDescription: Order a list of dictionaries based on another dictionary


code: dates, df = df[0], df.iloc[:, 1:]

rawDescription: Python pandas.read_csv split column into multiple new columns using comma to separate


code:
try:
    with open(filename, "r") as f:
        for ln, k in enumerate(map(float, reductions)):
            for key, group in groupby(f, key=getkey):
                outfile[ln].writelines(choose_random(group, fraction=k))
            f.seek(0)

finally:
    for f in outfile:
        f.close()

rawDescription: Python File Reduction - part 2

description: Python File Reduction - part 2


code:
import numpy as np
a = np.arange(12).reshape(3,4)
a
a[:, [1,2,3]]
a[:, [1,3]]

rawDescription: Python Slice Notation with Comma/List

description: Python Slice Notation with Comma/List


rawDescription: Creating a file to lookup paths and file names?


rawDescription: Replacing strings in specific positions into a text and then rewriting all the text


code:
import xml.etree.ElementTree as ET

tree = ET.fromstring(xml_str)

def getkey(elem):
    # Used for sorting elements by @LIN.
    # returns a tuple of ints from the exploded @LIN value
    # '1.0' -> (1,0)
    # '1.0.1' -> (1,0,1)
    return float(elem.get('LIN'))

root = etree.fromstring(xml_str)
lines = root.find("PurchaseOrder/LineItems")
lines[:] = sorted(lines, key=getkey)

rawDescription: Sorting child elements with lxml based on attribute value


code: df['FLAG'] = np.where(first_condition & second_condition, 1, 0)

rawDescription: Iterate and compare values through a dataframe


code:
import itertools

def break_down(text):
    words = text.split()
    ns = range(1, len(words)) # n = 1..(n-1)
    for n in ns: # split into 2, 3, 4, ..., n parts.
        for idxs in itertools.combinations(ns, n):
            yield [' '.join(words[i:j]) for i, j in zip((0,) + idxs, idxs + (None,))]

rawDescription: Split a string into all possible ordered phrases


code:
if len(a) == len(b):
    for i in range(len(a)):
        if not same_structure(a[i], b[i]):
            return False
    return True
else:
    return False

rawDescription: How to find out two lists with same structure in python?

description: Find out two lists with same structure in python


code: "SELECT * FROM your_table WHERE " + ' '.join(['%s = 1' % name for name in columns])

rawDescription: Wildcards in column name for MySQL

description: Wildcards in column name for MySQL


code:
import csv
import requests
from bs4 import BeautifulSoup
for i in range(907):      # Number of pages plus one
    url = "http://www.pga.com/golf-courses/search?page={}&searchbox=Course+Name&searchbox_zip=ZIP&distance=50&price_range=0&course_type=both&has_events=0".format(i)
    r = requests.get(url)
    soup = BeautifulSoup(r.content)

    # Your code for each individual page here

rawDescription: How can I loop scraping data for multiple pages in a website using python and beautifulsoup4

description: Loop scraping data for multiple pages in a website using python and beautifulsoup4


rawDescription: IPython "Column not found error"

description: IPython "Column not found error"


code:
template = "Player # likes @."
subs = ("{n}", "{w}") if numbers_first else ("{w}", "{n}")
format = make_format(template, subs) # escape {}, replace # and @

# ...
for n, w in product(*data):
    print(format.format(n=n, w=w))

rawDescription: Refactoring code/consolidating functions (e.g. nested for-loop order)


code: df2.columns = [l0 + str(l1+1) for l0, l1 in df2.columns]

rawDescription: Pivot Pandas Dataframe and calculate 'columns' parameter


rawDescription: Flatten doubly nested lists

description: Flatten doubly nested lists


rawDescription: Python: Replace dataframe values row by row


code:
f = a.f
id(f) == id(a.g)

rawDescription: Why do different methods of same object have the same `id`?


code: findNextSibling('div')

rawDescription: Attempting a Nested Scrape Using BeautifulSoup


code:
with open("pathtomyfile", "w") as f:
    for item in r.json or []:
        try:
            f.write(item['repository']['name'] + "\n")
        except KeyError:  # you might have to adjust what you are writing accordingly
            pass  # or sth ..

rawDescription: Write Python dictionary obtained from JSON in a file


code:
import numpy as np

myList = ['a','b','c','d']
toRemove = [0,2]

new_list = np.delete(myList, toRemove)

rawDescription: Concise way to remove elements from list by index in Python


rawDescription: dictionary in python problem

description: Dictionary in python problem


code:
def print_counter(counter):
    """Pretty print the result of the counter."""

    print(LINE_FORMAT.format('prot', 'source', 'destination',  'port', 'hitcnt'))
    print(LINE_FORMAT.format(*tuple(('------------------',) * 5)))
    for re_match, count in counter.most_common():

        re_match_length = len(re_match)

        # Pick elements based on match from DENY_PATTERN
        if re_match_length == 8:
            protocol, src, src_port, dst, dst_port, icmp_spec, icmp_type, icmp_code =  re_match
            if icmp_code or icmp_type:
                dst_port = '{}, {}'.format(icmp_code, icmp_type)

        # Pick elements based on match from DENIED_PATTERN
        if re_match_length == 4:
            protocol, src, dst, dst_port = re_match

        print(LINE_FORMAT.format(protocol.lower(), src, dst, dst_port, count))

rawDescription: Python program and regex

description: Python program and regex


code: row = get_row(copy.deepcopy(d))

rawDescription: How not to change outer values?

description: How not to change outer values


code:
def _reduce_method_descriptor(m):
    return getattr, (m.__objclass__, m.__name__)

import copy_reg
copy_reg.pickle(type(set.union), _reduce_method_descriptor)
pickle.dumps(set.union)

rawDescription: Pickling method descriptor objects in python


code:
if rule.endpoint != 'static':
    docstring = app.view_functions[rule.endpoint].__doc__
    if docstring is not None and docstring.strip():
        func_list[docstring.strip()] = rule.rule

rawDescription: How to interchange the key value output of app.url_map.iter_rules function

description: Interchange the key value output of app.url_map.iter_rules function


code: dictB = dictA.copy()

rawDescription: Setting dictionarys equal in Python

description: Setting dictionarys equal in Python


code:
Alpha, 42
Alpha, 10
Beta, 30
Beta, 10
Gamma, 81

rawDescription: How to sort two columns at once using sorted()

description: Sort two columns at once using sorted()


code:
date_index = pd.DatetimeIndex([pd.datetime(2003,6,24), pd.datetime(2003,8,13),
        pd.datetime(2003,8,19), pd.datetime(2003,8,22), pd.datetime(2003,8,24)])

ts = pd.Series([2,1,2,1,5], index=date_index)

rawDescription: Filling continuous pandas dataframe from sparse dataframe


code:
def getSubdigits2(n, a, b):
    l = int(math.ceil(math.log10(n)))
    n %= 10 ** (l - a)
    n //= 10 ** (l - b)
    return n

rawDescription: Is using `str` the correct idiom for working with digits in Python


code: re.sub('<a href="/name/[^>]*>',  '', string)

rawDescription: python string.replace() a string + 12 characters following

description: Python string.replace() a string + 12 characters following


code:
from collections import Counter

pair_counts = Counter(zip(List, List[1:]))

rawDescription: Find the letters in a python list that follow each other most frequently


code:
from appscript import *
safari = app("Safari")
safari.make(new=k.document,with_properties={k.URL:"http://stackoverflow.com"})
safari.windows.first.current_tab.close()

rawDescription: Opening and using Safari

description: Opening and using Safari


code:
import socket

def http_banner_grabber(ip, port=80, method="HEAD",
                        timeout=60, http_type="HTTP/1.1"):
    assert method in ['GET', 'HEAD']
    # @see: http://stackoverflow.com/q/246859/538284
    assert http_type in ['HTTP/0.9', "HTTP/1.0", 'HTTP/1.1']
    cr_lf = '\r\n'
    lf_lf = '\n\n'
    crlf_crlf = cr_lf + cr_lf
    res_sep = ''
    # how much read from buffer socket in every read
    rec_chunk = 4096
    s = socket.socket()
    s.settimeout(timeout)
    s.connect((ip, port))
    # the req_data is like 'HEAD HTTP/1.1 \r\n'
    req_data = "{} / {}{}".format(method, http_type, cr_lf)
    # if is a HTTP 1.1 protocol request,
    if http_type == "HTTP/1.1":
        # then we need to send Host header (we send ip instead of host here!)
        # adding host header to req_data like 'Host: google.com:80\r\n'
        req_data += 'Host: {}:{}{}'.format(ip, port, cr_lf)
        # set connection header to close for HTTP 1.1
        # adding connection header to req_data like 'Connection: close\r\n'
        req_data += "Connection: close{}".format(cr_lf)
    # headers join together with `\r\n` and ends with `\r\n\r\n`
    # adding '\r\n' to end of req_data
    req_data += cr_lf
    # the s.send() method may send only partial content.
    # so we used s.sendall()
    s.sendall(req_data.encode())
    res_data = b''
    # default maximum header response is different in web servers: 4k, 8k, 16k
    # @see: http://stackoverflow.com/a/8623061/538284
    # the s.recv(n) method may receive less than n bytes,
    # so we used it in while.
    while 1:
        try:
            chunk = s.recv(rec_chunk)
            res_data += chunk
        except socket.error:
            break
        if not chunk:
            break
    if res_data:
        # decode `res_data` after reading all content of data buffer
        res_data = res_data.decode()
    else:
        return '', ''
    # detect header and body separated that is '\r\n\r\n' or '\n\n'
    if crlf_crlf in res_data:
        res_sep = crlf_crlf
    elif lf_lf in res_data:
        res_sep = lf_lf
    # for under HTTP/1.0 request type for servers doesn't support it
    #  and servers send just send body without header !
    if res_sep not in [crlf_crlf, lf_lf] or res_data.startswith('<'):
        return '', res_data
    # split header and data section from
    # `HEADER\r\n\r\nBODY` response or `HEADER\n\nBODY` response
    content = res_data.split(res_sep)
    banner, body = "".join(content[:1]), "".join(content[1:])
    return banner, body

rawDescription: Banner grabbing HTTP

description: Banner grabbing HTTP


code:
parameters = {'api_token': 'e27e901c196b8f0399bc79'}
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1', params=parameters)

rawDescription: Using Python Variables in one Instance

description: Using Python Variables in one Instance


code:
from pyspark.sql.types import StructType, StructField, DoubleType

meta = {"ml_attr": {
    "name": "type",
    "type": "nominal",
    "vals": ["0.0", "1.0", "3.0"]
}}

schema = StructType([StructField("type", DoubleType(), False, meta)])

training = sc.parallelize([(0., ), (1., ), (1., ), (3., )]).toDF(schema)
testing  = sc.parallelize([(0., ), (1., ), (1., ), (1., )]).toDF(schema)

assert (
    encoder.setInputCol("type").transform(training).first()[-1].size ==
    encoder.setInputCol("type").transform(testing).first()[-1].size
)

rawDescription: Spark: Use same OneHotEncoder on multiple dataframes


code:
a = the_list
a += [0] * (-len(a) % 5)
result = [a[i:i + 5] for i in range(0, len(a), 5)]

rawDescription: How to return every 5 items from a list in python?

description: Return every 5 items from a list in python


code:
import time
to = time.strptime("12/03/2015", "%m/%d/%Y")
to.tm_mon
to.tm_year

rawDescription: Iterate through CSV and match lines with a specific date


code: 'update, index, set, where, and'.split(',')

rawDescription: Python list is not being appended to if value found in another list


code:
def overlapping_substrings(string, tree, solved=None):
    if solved is None:
        solved = PrefixTree()
    i = 1
    last = 0
    matching = True
    solutions = []
    while i < len(string) + 1:
        if string[last:i] in tree:
            if not matching:
                matching = True
            else:
                i += 1
                continue
        else:
            if matching:
                matching = False
                solutions.append(string[last:i - 1])
                last = i - 1
                i -= 1
        i += 1
    if matching:
        solutions.append(string[last:i])
    for solution in solutions:
        if solution in solved:
            continue
        else:
            solved.add_ngram(solution)
            yield solution

def slide_start(string):
    for i in range(len(string)):
        yield string[i:]

def seek_subtree(tree, sequence):
    # Find the node of the search tree which
    # is found by this sequence of items
    node = tree
    for i in sequence:
        if i in node:
            node = node[i]
        else:
            raise KeyError(i)
    return node

def find_all_common_spans(string, tree):
    # We can keep track of solutions to avoid duplicates
    # and incomplete prefixes using a Prefix Tree
    seen = PrefixTree()
    for substring in slide_start(string):
        # Drive generator forward
        list(overlapping_substrings(substring, tree, seen))
    # Some substrings are suffixes of other substrings which you do not
    # want
    compress = SuffixTree()
    for solution in sorted(seen.labels, key=len, reverse=True):
        # A substrings may be a suffix of another substrings, but that substrings
        # is actually a repeating pattern. If a solution is
        # a repeating pattern, `not solution in seek_subtree(tree, solution)` will tell us.
        # Otherwise, discard the solution
        if solution in compress and not solution in seek_subtree(tree, solution):
            continue
        else:
            compress.add_ngram(solution)
    return compress.labels

def search(query, corpus):
    tree = SuffixTree()
    if isinstance(corpus, SuffixTree):
        tree = corpus
    else:
        for elem in corpus:
            tree.add_ngram(elem)
    return list(find_all_common_spans(query, tree))

rawDescription: Finding all common, non-overlapping substrings


code:
from __future__ import print_function

d = {}
with open('descr2semtype_short.txt') as f:
    for line in f:
        line = line.rstrip()
        a, b = line.split()
        if a not in d:
            d[a] = []
        d[a].append(b)

for k,v in d.iteritems():
    print(k + ' ' + ' '.join(v))

rawDescription: Print lines with constraint in Python

description: Print lines with constraint in Python


code:
class Ship(Entity):
    def __init__(self, container):
        Entity.__init__(self)
        self.speed = 5
        self.image = pygame.Surface((SHIP_WIDTH, SHIP_HEIGHT)).convert()
        self.rect = self.image.get_rect()
        self.rect.centerx = container.centerx
        self.rect.y = container.bottom - self.rect.height * 2
        self.container = container

    def update(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_w]:
            self.rect.centery -= self.speed
        if key[pygame.K_s]:
            self.rect.centery += self.speed
        if key[pygame.K_d]:
            self.rect.centerx += self.speed
        if key[pygame.K_a]:
            self.rect.centerx -= self.speed

        self.rect.clamp_ip(self.container)

rawDescription: Adding scrolling to a top/down shooter

description: Adding scrolling to a top/down shooter


code:
L = [2, 1, 4, 3, 5, 1, 2, 1, 1, 6, 5]
S = set()
M = []
for e in L:
    if e in S:
        continue
    S.add(e)
    M.append(e)

M

rawDescription: Algorithm - How to delete duplicate elements in a list efficiently?


code: ax.plot_wireframe(T, z, abs(U), cstride=1000)

rawDescription: Matplotlib plot pulse propagation in 3d

description: Matplotlib plot pulse propagation in 3d


code:
import sys

nvl = lambda value, other: other if value is None else value

def exec_module(module, globals=None, locals=None):
    frame = sys._getframe(1)
    globals = nvl(globals, {})
    globals.update(frame.f_globals)
    locals = nvl(locals, {})
    locals.update(frame.f_locals)
    exec(compile(module.__loader__.get_source(module.__name__),
                 module.__file__, 'exec'), globals, locals)

# this is how you would use the code up above

import http.server

exec_module(http.server, dict(__name__='__main__'))

rawDescription: Start SimpleHTTPServer in python

description: Start SimpleHTTPServer in python


code:
fig, axes = plt.subplots(nrows=4)
for y, ax in zip(ydata, axes):
    ax.plot(x, y, color='salmon')
    ax.locator_params(axis='y', nbins=5)
plt.show()

rawDescription: Python Numpy 2D plot set total number of y-tics with autoscaling


code:
def msg_generator(self):
    ''' Provides messages until bot dies '''
    while self.alive:
        for msg in self.irc.recv(self.buffer).split(('\r\n').encode()):

            if len(msg) <= 3:
                continue

            try:
                yield Message(msg.decode())
            except Exception as e:
                self.log('%s %s\n' % (except_str, str(e)))

rawDescription: My code nests too deep. Is there a better way?


code:
import copy

def main(matrix,direction):
    matrixcopy = copy.deepcopy(matrix)
    index16 = indexOf(16,matrixcopy)
    matrixcopy[index16[0]][index16[1]],matrixcopy[index16[0]-1][index16[1]]=matrixcopy[index16[0]-1][index16[1]],matrixcopy[index16[0]][index16[1]]
    return matrixcopy

rawDescription: Python: Function with matrix in argument


code: "{}{}{}".format(subdomain, '.' if subdomain else '', fullDomain)

rawDescription: Conditionally parsing a string

description: Conditionally parsing a string


code:
import urlparse
urlparse.urljoin('http://www.example.com/foo/bar/../../baz/bux/', '.')

rawDescription: Python: How to resolve URLs containing '..'


code:
# Make sure to include the appropriate ouputSchema
def join_bags(BAG1, BAG2):
    return BAG1 + BAG2

rawDescription: PIG how to merge two groups?

description: PIG how to merge two groups


code:
parent = htmlFile.find_all("div", "product")[0].p
lines = newContent.splitlines()
parent.append(htmlFile.new_string(lines[0]))
for line in lines[1:]:
    parent.append(htmlFile.new_tag('br'))
    parent.append(htmlFile.new_string(line))

rawDescription: Modifying a BeautifulSoup .string with line breaks


code:
a = {'foo':''}
a.get('foo','bar') or 'bar'
a['foo'] = 'baz'
a.get('foo','bar') or 'bar'
a.get('qux','bar') or 'bar'

rawDescription: Checking if a key exists and its value is not an empty string in a Python dictionary


code:
dates = Dating.objects.filter((Q(creator=user) | Q(assignee=user)) & Q(accepted=True)) \
                  .exclude(id__in=alreadyRated)

rawDescription: Discard older elements in Django queryset


code:
global x
x = 0

def increment_x():
    x += 1
    return x

rawDescription: Why aren't my globals global?

description: Why aren't my globals global


rawDescription: Detect and extract changing numbers in list of strings


code:
def foo(indict: dict(apple=None, pear=None)):
    print(indict)

foo(dict())

rawDescription: Dictionary with some mandatory keys as function input


code: getattr(server, "do_" + vars["fnc"])(vars)

rawDescription: python variable function definition

description: Python variable function definition


code:
with open('test.txt') as f:
    d = {}
    for line in f:
        k,v = line.split(':')
        for x in v.split(','):
            x = x.strip()
            d.setdefault(k,[]).append(x)

rawDescription: Creating a dictionary from a text file on python


code:
import itertools

def union(it1, it2):
    it1, it2 = iter(it1), iter(it2)
    for item in (item for pair in itertools.izip(it1, it2) for item in pair):
        yield item
    for it in (it1, it2):
        for item in it:
            yield item

rawDescription: Union of two iterators

description: Union of two iterators


code:
import tastyfood
choco = tastyfood.Chocolate()

from tastyfood import Chocolate
choco = Chocolate()

from tastyfood.chocolate import Chocolate  # still works too
choco = Chocolate()

rawDescription: Package up Python files

description: Package up Python files


code:
features = vectorizer.get_feature_names()
weights = np.abs(weights)
for i in range(km.n_clusters):
    top5 = np.argsort(weights[i])[-5:]
    print(zip([features[j] for j in top5], weights[i, top5]))

rawDescription: sklearn decomposition top terms

description: Sklearn decomposition top terms


code:
@add_metaclass(ABCMeta)
class MyABC(SomeParentABC):
    def __init__(self, important_attr):
        self.important_attr = important_attr
    def gamma(self):
         self.important_attr += ' gamma'

rawDescription: How to write a Python ABC with a concrete initializer in python 2.6 to 3.5?

description: Write a Python ABC with a concrete initializer in python 2.6 to 3.5


code:
class InstrumentBase(object):
    def external_method(self, arg1, arg2):
        if self.flag:
            ...

rawDescription: Importing methods for a Python class

description: Importing methods for a Python class


code: np.any(np.in1d(a1, a2))

rawDescription: python: check if an numpy array contains any element of another array

description: Python: check if an numpy array contains any element of another array


code:
import itertools, numpy

cube = numpy.array(list(itertools.product((0,1), (0,1), (0,1))))

x_dim = y_dim = z_dim = 10

for offset in itertools.product(*map(xrange, (x_dim, y_dim, z_dim))):
    work_with_cube(cube+offset)

rawDescription: Pythonic way of iterating over 3D array

description: Pythonic way of iterating over 3D array


rawDescription: Python print " character

description: Python print " character


code:
def inorder(self, file):
    if self._left is not None:
        file.write(str(self) + ' ')
        self._left.inorder(file)
    file.write(str(self) + ' ')
    if self._right is not None:
        file.write(str(self) + ' ')
        self._right.inorder(file)

rawDescription: Writing traversals to a file in python3.x issue


code:
skills[skills_dict[pts2]] += pts
points -= pts

rawDescription: Programming Python for absolute beginners: Chapter 5 Challenge #2


code:
sorter = amplitudes.argsort()
modes_sorted = modes[:,sorter]

rawDescription: Sort a 1D array and a 2D array(by column) simultaneously


code:
def __init__(self, text='', parent=None):
    super(Button, self).__init__('Text', parent=parent)
    ...

rawDescription: Custom button PyQT4

description: Custom button PyQT4


code:
def __add__(self, g):
    summed = sum((self.whole_number, g.whole_number, self.fraction, g.fraction))
    whole = int(summed)
    remainder = summed - whole
    return '{} and {}'.format(whole, remainder)

rawDescription: adding fractions class python

description: Adding fractions class python


code: p2 = plt.bar(ind, womenMeans, width, color='y', bottom=menMeans, yerr=womenStd)

rawDescription: How to draw 100% stacked bars with mixed +ve and -ve values in matplotlib?

description: Draw 100% stacked bars with mixed +ve and -ve values in matplotlib


code:
import csv

with open('thefile.csv', 'rb') as f:
  data = list(csv.reader(f))

rawDescription: python csv: getting subset

description: Python csv: getting subset


code:
def combo1_changed (combo, path, iter):
    main_iter = store.get_iter (path)
    selected  = store.get_value (main_iter, X)
    if selected == 'Alphabet':
        store.set_value (main_iter, M, alphabet_list_store)
    elif selected == 'Numbers':
        store.set_value (main_iter, M, number_list_store)
    ...

rawDescription: Conditional CellRenderCombo in pyGTK TreeView


code:
story = []
# ...
# doc.addPageTemplates([...])

story.append(NextPageTemplate(['pageLeft', 'pageRight'])) # this will cycle through left/right/left/right/...

story.append(NextPageTemplate(['firstPage', 'secondPage', '*', 'pageLeft', 'pageRight'])) # this will cycle through first/second/left/right/left/right/...

rawDescription: reportlab different next page

description: Reportlab different next page


code:
class Art(models.Model):
    title = models.CharField()
    artist = models.CharField()
    link = models.URLField()

    def __unicode__(self):
        return u'%s, %s, %s' % (self.title, self.artist, self.link)

rawDescription: Adding json to new Django database

description: Adding json to new Django database


rawDescription: Multiple page PDF with multiple charts per page from matplotlib


code:
my_svc = svm.SVC(probability=True, C=1000)
my_svc.fit(x_training,y_trainc)

p_classification = my_svc.predict(x_test)

rawDescription: Sklearn SVM: SVR and SVC, getting the same prediction for every input


code:
G = nx.DiGraph()

csv_F = csv.reader(open("test.txt"),delimiter=';')
for row in csv_F:
    attributes=row[1].split(',')
    G.add_node(row[0], attr = attributes)

rawDescription: Add Nodes with attributes from a .txt file with multiple delimiters networkx/pandas


code:
filter_namespace = argparse.Namespace(filter1=None, filter2=None)
namespace = argparse.Namespace(filter=filter_namespace)
namespace = main_parser.parse_args(namespace=namespace)

rawDescription: argparse subcommands with nested namespaces

description: Argparse subcommands with nested namespaces


code:
def GvizFromCsv(filename):
  """ creates a gviz data table from a CSV file """

  data_table = gviz_api.DataTable([('col1','string','SURNAME'),
                                   ('col2','number','ONE'    ),
                                   ('col3','number','TWO'    ) ])

  #  --- with such a table schema , lines in the file must be like that: ---
  #  blah, number, number, ...anything else...\n
  #  SMITH,1.006,1.006, ...anything else...\n
  #  JOHNSON,0.810,1.816, ...anything else...\n
  #  WILLIAMS,0.699,2.515, ...anything else...\n

  with open(filename) as f:
    data_table.LoadData( [el if n==0 else float(el) for n,el in enumerate(line.split(',')[0:3])]
                         for line in f )
  return data_table

rawDescription: Fastest way to do data type conversion using csv.DictReader in python


code:
def sphere(x,y,z):
    return x**2 + y**2 + z**2 - 2.0**2

def translate(fn,x,y,z):
    return lambda a,b,c: fn(x-a,y-b,z-c)

def union(*fns):
    return lambda x,y,z: np.min(
        [fn(x,y,z) for fn in fns], 0)

def intersect(*fns):
    return lambda x,y,z: np.max(
        [fn(x,y,z) for fn in fns], 0)

def subtract(fn1, fn2):
    return intersect(fn1, lambda *args:-fn2(*args))

plot_implicit(union(sphere,translate(sphere, 1.,1.,1.)), (-2.,3.))

rawDescription: Plotting implicit equations in 3d

description: Plotting implicit equations in 3d


code:
string_input = input('Question?')
try:
    integer_input = int(string_input)
except ValueError:
    print('Please enter a valid number')

rawDescription: How does my input not equal the answer?

description: How does my input not equal the answer


code:
g = df1.groupby('A')
df = pd.DataFrame({"AA": g.mean()['C'] + g.mean()['D'], "BB": g.std()['D']})
df

rawDescription: Python Pandas: Groupby and Apply multi-column operation


code:
def sortTable(table, col):
    return sorted(table, key=itemgetter(col))

rawDescription: Generate ranks based on data table

description: Generate ranks based on data table


code:
from caching.base import CachingManager, CachingMixin
from django.contrib.auth.models import AbstractUser, UserManager

class CachedUserManager(CachingManager, UserManager):
    pass

class CachedUser(CachingMixin, AbstractUser):
    objects = CachedUserManager()

rawDescription: How to get Django Cache Machine to work on django.contrib.auth.models.User?

description: Get Django Cache Machine to work on django.contrib.auth.models.User


code:
p = subprocess.Popen([sys.executable, 'sub.py'], stdin=subprocess.PIPE)
_, _ = p.communicate(input='foo')

rawDescription: program input redirected via python subprocess

description: Program input redirected via python subprocess


code:
def parse(self, response):
    items = []

    hxs = HtmlXPathSelector(response)
    print ('hxs:', hxs)

    movie_rows = hxs.select('//div[@id="body"]/div/table/tr/td/table/tr[2]/td/table[2]/tr')
    for m in movie_rows:
        movie = BoxofficemojoItem()

        movie['title'] = m.select('td[1]/font/a/@href').extract()[0]
        movie['link'] = urlparse.urljoin(
            response.url, m.select('td[1]/font/a/b/text()').extract()[0])
        movie['gross'] = m.select('td[3]/font/text()').extract()[0]
        movie['release_date'] = m.select('td[7]/font//text()').extract()[0]

        items.append(movie)
    return items

rawDescription: Scrapy MySQL Pipeline -- All DB Entries Are the Same


rawDescription: Removing decimal place from int or string in Python


code: x, y = m(latitude,longitude)

rawDescription: Lines not plotting on graph using Python/Basemap


rawDescription: python: how to have a property and with a setter function that detects all changes that happen to the value

description: Python: how to have a property and with a setter function that detects all changes that happen to the value


code: # arr = cellArray[i*ncols+j].image

rawDescription: Faster way than concatenate of combining multiple cv2 images (numpy arrays) in python?


code:
positive = ['Fully Paid', 'Current']
negative = ['Charged Off', 'Default']

rawDescription: Python way to convert categorical values into binary in the same column


code:
sigtermreceived = False

def sigterm(signum, frame):
  global sigtermreceived
  sigtermreceived = True

signal.signal(signal.SIGTERM, sigterm)

while not sigtermreceived:
  signal.pause()

rawDescription: signal.signal receives sigint/sigterm when child process ends

description: Signal.signal receives sigint/sigterm when child process ends


code: value = value.zfill(8)

rawDescription: Add leading 0's to value until length is 8 characters long


code:
response.view = '%s_%s.%s' % (request.controller, request.function,
                              request.extension)

rawDescription: Web2py mapping controller to view

description: Web2py mapping controller to view


code:
#!/usr/bin/env python
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox() # Get local session of firefox
browser.get("http://www.yahoo.com") # Load page
assert "Yahoo" in browser.title
elem = browser.find_element_by_name("p") # Find the query box
elem.send_keys("seleniumhq" + Keys.RETURN)
time.sleep(0.2) # Let the page load, will be added to the API
try:
    browser.find_element_by_xpath("//a[contains(@href,'http://docs.seleniumhq.org')]")
except NoSuchElementException:
    assert 0, "can't find seleniumhq"
browser.close()

rawDescription: Remote executing of program via xterm run using paramiko python ssh library


code: reduce(lambda res, cur: {cur: res}, reversed("foo/bar/baz".split("/")), 1)

rawDescription: How can I split a string and form a multi-level nested dictionary?

description: Split a string and form a multi-level nested dictionary


rawDescription: Convert a black or white only rgb array into a 1 or 0 array in Python


code:
import kivy
from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.uix.relativelayout import RelativeLayout
from kivy.graphics import *
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from random import random
from kivy.properties import ListProperty

kv = '''
<ColoredLabel>:
    size: (100,100)
    pos: (10,10) # no effect
    background_color:
    canvas.before:
        Color:
            rgba: self.background_color
        Rectangle:
            pos: self.pos
            size: self.size
    '''

Builder.load_string(kv)

class ColoredLabel(Label):
    background_color = ListProperty((0,0,0,1))

class MyApp(App):
    def build(self):
        f = AnchorLayout()
        g = GridLayout(cols=2, rows=2)
        layout = BoxLayout(size_hint=(1, None), height=50)
#        v = Video(source='driver.mp4', state='play', options={'eos':'loop'})
        l1 = Label(text="jenkins", font_size=32)
        l2 = Label(text="git", font_size=32)
#        f.add_widget(v)

        label = ColoredLabel(text="Hello World!", size_hint=(None, None), background_color=(random(), random(), random(), 1))
        g.add_widget(label)

        f.add_widget(g)

        return f

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

rawDescription: python kivy add text inside rectangle

description: Python kivy add text inside rectangle


code:
from inspect import iscode

for x in complex_func.__code__.co_consts:
    if iscode(x):
        # Skip <setcomp>, <dictcomp>, <listcomp> or <genexp>
        if x.co_name.startswith('<') and x.co_name != '<lambda>':
            continue
        flags = x.co_flags
        # CO_OPTIMIZED = 0x0001, CO_NEWLOCALS = 0x0002
        if flags & 0x0001 and flags & 0x0002:
            print(x, 'represents a function {!r}'.format(x.co_name))
        else:
            print(x, 'represents a class {!r}'.format(x.co_name))

rawDescription: Is it possible to access inner functions and classes via code objects?


code:
def callme(*args):
    if CheckVar1.get() or CheckVar2.get():
        b1['state'] = 'normal'
    else:
        b1['state'] = 'disabled'

rawDescription: How to change the state of a button in Tkinter?

description: Change the state of a button in Tkinter


rawDescription: Copy key value pair from one dictionary to another if the same key exists in both the dictionaries in Python


code:
eVec
old_eVec

rawDescription: Python OpenCV PCACompute Eigenvalue

description: Python OpenCV PCACompute Eigenvalue


code:
def expire_view_cache(view_name, args=[], namespace=None, key_prefix=None):
    """
    This function allows you to invalidate any view-level cache.
        view_name: view function you wish to invalidate or it's named url pattern
        args: any arguments passed to the view function
        namepace: optioal, if an application namespace is needed
        key prefix: for the @cache_page decorator for the function (if any)
    """
    from django.core.urlresolvers import reverse
    from django.http import HttpRequest
    from django.utils.cache import get_cache_key
    from django.core.cache import cache
    # create a fake request object
    request = HttpRequest()
    # Loookup the request path:
    if namespace:
        view_name = namespace + ":" + view_name
    request.path = reverse(view_name, args=args)
    # get cache key, expire if the cached item exists:
    key = get_cache_key(request, key_prefix=key_prefix)
    if key:
        if cache.get(key):
            # Delete the cache entry.
            #
            # Note that there is a possible race condition here, as another
            # process / thread may have refreshed the cache between
            # the call to cache.get() above, and the cache.set(key, None)
            # below.  This may lead to unexpected performance problems under
            # severe load.
            cache.set(key, None, 0)
        return True
    return False

rawDescription: Expire a view-cache in Django?

description: Expire a view-cache in Django


code: parser.get('Some Section', 'spam')

rawDescription: Python's ConfigParser unique keys per section


code:
lines = in_file.readlines()
segment_tab = [line.split('\t') for line in lines]

rawDescription: How to not include '\n' and the next index entry while appending values from a certain index to a list

description: Not include '\n' and the next index entry while appending values from a certain index to a list


code:
max_number_of_nas = 3000
df = df.loc[:, (df.isnull().sum(axis=0) <= max_number_of_nas)]

rawDescription: How to delete a column in pandas dataframe based on a condition?

description: Delete a column in pandas dataframe based on a condition


code:
result = (A.shift() * A).dropna()
result /= result.abs()
print(result)

rawDescription: Pythonic way of obtaining serial correlation of elements in pandas dataframe


code:
Output
America/Puerto_Rico
America/New_York
America/Anchorage

rawDescription: pytz: return Olson Timezone name from only a GMT Offset

description: Pytz: return Olson Timezone name from only a GMT Offset


code:
a = 'ahdhkhkahfkahafafkh'
"".join(i.upper() if i == 'a' else i for i in a)

rawDescription: Conversion of a specific character in a string to Upper Case


code:
for row in world_map:
    for column in row:
        print(column, end="")
    print()

rawDescription: Multidimensional array for a text based game


rawDescription: pandas: how to aggregate a subset of groupby rows into a single row?

description: Pandas: how to aggregate a subset of groupby rows into a single row


code:
class MultipleChoices(web.HTTPError):
    def __init__(self, choices):
        status = '300 Multiple Choices'
        headers = {'Content-Type': 'text/html'}
        data = '
Multiple Choices
\n
\n'
        data += ''.join('
{0}
\n'.format(c)
                        for c in choices)
        data += '
'
        web.HTTPError.__init__(self, status, headers, data)

rawDescription: Webpy: how to set http status code to 300


code:
def implicit(a):
    n = a.shape[0]
    m = np.zeros_like(a)
    for k in range(n):
        for i in range(n):
            m[k,i] = (a[i,:] - a[k,:]).sum() - n*a[i,k] + n*a[k,k]
    return m

rawDescription: Numpy matrix operations

description: Numpy matrix operations


code: main_list[indexes,:] = substitutions

rawDescription: How do I substitute a row in a two dimensional numpy array with a list of substitutions?

description: Substitute a row in a two dimensional numpy array with a list of substitutions


code:
__version__ = '0.1.6'

def version():
    return __version__

rawDescription: Get pip/setuptools version of package from within the package?


code:
from django.conf import settings

def network_url(request):
    return redirect(settings.MAIN_DOMAIN_LINK)

rawDescription: Get main domain url without hardcoding

description: Get main domain url without hardcoding


rawDescription: Searching from Bottom to Top in Python

description: Searching from Bottom to Top in Python


code:
import ansible.playbook
from ansible import callbacks
from ansible import utils
ANSIBLE_HOSTS = 'hosts'
PLAYBOOK = 'foo.yml'
def deploy():
    stats = callbacks.AggregateStats()
    playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY)
    inventory = ansible.inventory.Inventory(ANSIBLE_HOSTS)
    runner_cb = callbacks.PlaybookRunnerCallbacks(stats,verbose=utils.VERBOSITY)

    pb = ansible.playbook.PlayBook(playbook=PLAYBOOK,
                                   callbacks=playbook_cb,
                                   runner_callbacks=runner_cb,
                                   stats=stats,
                                   inventory=inventory,
                                   extra_vars={'name': "AAA"})
    pb.run()

if __name__ == "__main__":
    deploy()

rawDescription: I have a ansible file . I just want to run ansible file in flask API


rawDescription: Counting sublists in a list

description: Counting sublists in a list


code:
from Tkinter import *
import tkFont

class Application:
    def __init__(self, top):
        self.top=top
        # initializer for gui
        self.gui()

    def gui(self):
        var="Application"
        helv16=tkFont.Font(self.top,family="Helvetica",size=16,slant="italic")
        self.label1=Label(self.top,text=var,font=helv16,bd=5,fg='#2ddef9',bg='#aaecaa',relief=RIDGE,padx=17,pady=4,justify="center")
        self.label1.pack(side=TOP)

        label2=Label(self.top,text='Proxy City', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=18,pady=4,justify="center").pack()
        # setting default proxy city to Bangalore
        self.v=IntVar()
        # initiallize to Bangalore
        self.v.set(1)

        list_city=[("Bangalore", 1), ("Chennai", 2), ("USA", 3), ("Chaina", 4)]
        for city, num in list_city:
            #print city, num
            Radiobutton(self.top, text=city, padx=10,pady=5, variable=self.v, value=num,command=self.sel, bd=3,fg='red',bg='#FFFFFF',relief=GROOVE).pack(anchor='w')

        label3=Label(self.top,text='Add URL', bd=3,fg='red',bg='#FFFFFF',relief=GROOVE,padx=17,pady=4,justify="center").pack()
        entry3=Entry(self.top,relief=SUNKEN,justify=CENTER,bd=3,fg='#0000FF',font='arial').pack()

    def sel(self):
        selection = "You selected the option " + str(self.v.get())
        self.label1.config(text = selection)

def main():
    top=Tk()
    top.geometry("680x600+400+240")
    top.title("Application")
    Application(top)
    top.mainloop()

if __name__=='__main__':
    main()

rawDescription: How to get radio button output in Tkinter?

description: Get radio button output in Tkinter


code: [{"param331": tab - 5.0, "param332": tab + 5.0} for tab in myTab]

rawDescription: How to assign floats to some fields of json dumps using a for loop

description: Assign floats to some fields of json dumps using a for loop


code:
def check(s):
    return all(ss.count('0')>=2 for ss in (s[i:i+5] for i in xrange(len(s)-4)))

rawDescription: Deterministic Finite Automata for {0,1} Every5-Two0


code:
import x
dir(x)

rawDescription: Parse Python file to retrieve variables, values, function definitions


code:
import tkinter as tk

# --- functions ---

def nxt(count):
    print("Forward:", count)

def bck(count):
    print("Back:", count-1)

def run(count, root):

    count += 0.1
    print(count)
    if count < 100:
        # run again after 100ms
        root.after(100, run, count, root)

# --- main ---

root = tk.Tk()
root.geometry("100x100-0+0")
count = 0

fwd_btn = tk.Button(root, text="Next", command=lambda:nxt(count))
fwd_btn.pack()
bck_btn=tk.Button(root, text="Previous", command=lambda:bck(count))
bck_btn.pack()

# first time run immediately
run(count, root)

# or after 100ms
#root.after(100, run, count, root)

root.mainloop()

rawDescription: Is it possible to run a while loop and check for tkinter input in python 3


code: group = models.AutoField(primary_key=True, db_column='group_id')

rawDescription: Django OneToOneField and ForeignKeyField add "_id" suffix to the field


rawDescription: Multidimensional array in Python

description: Multidimensional array in Python


rawDescription: Why can't we access an object using its address in Python?


code:
class X:
   def __init__(self):
     self._value = 90

   def _get(self):
     return self._value

   def _set(self, value):
     self._value = value

   value = property(_get, _set)

rawDescription: calling methods on an instance with getattr [ python ]

description: Calling methods on an instance with getattr [ python ]


code:
# the first row
In [206]: a[0]
Out[206]: array([1, 2])

# the second row
In [207]: a[1]
Out[207]: array([3, 4])

# the second column
In [208]: a[:,1]         # the : gives all rows here, the 1 gets second column
Out[208]: array([2, 4])

# the first row again, using a `:` even though it's not required
In [209]: a[0,:]         # here the : gives all columns (it can be left off as in line 206)
Out[209]: array([1, 2])

rawDescription: I am stuck at the process of retrieving a value from a matrix


code:
months =['January','February','March','April','May','June','July','August','September','October','November','December']

d_months = {}

for i, month in enumerate(months):
    d_months[i+1] = month

print(d_months)

rawDescription: How can I create dictionary in python3?

description: Create dictionary in python3


rawDescription: Sort a list of Class Instances Python

description: Sort a list of Class Instances Python


code:
def my_function(x):
    return not callable(x)

rawDescription: Is there any method of getting detailed object information when using pdb to debug python script?


rawDescription: Reshape 1-D Numpy Array to 2-D

description: Reshape 1-D Numpy Array to 2-D


code:
from pymake import task, main

@task
def print_foo():
    '''Prints foo'''
    print('foo')

@task
def print_hello_world():
    '''Prints hello world'''
    print('Hello World!')

@task
def print_both():
    '''Prints both'''
    print_foo()
    print_hello_world()

if __name__ == '__main__':
    main()

rawDescription: Is there a Cake equivalent for Python?

description: Is there a Cake equivalent for Python


code:
from django.template import Context, loader
template = loader.get_template(template_name)
context = Context(extra_context)
html_message = template.render(context)

rawDescription: django-post_office - Using file-based instead of database-based email templates

description: Django-post_office - Using file-based instead of database-based email templates


rawDescription: Python Django - Accessing foreignkey data


code:
class EditButtonsWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(EditButtonsWidget,self).__init__(parent)

        # add your buttons
        layout = QtGui.QHBoxLayout()

        # adjust spacings to your needs
        layout.setContentsMargins(0,0,0,0)
        layout.setSpacing(0)

        # add your buttons
        layout.addWidget(QtGui.QPushButton('Save'))
        layout.addWidget(QtGui.QPushButton('Edit'))
        layout.addWidget(QtGui.QPushButton('Delete'))

        self.setLayout(layout)

rawDescription: Adding Multiple Widgets to QTableWidget Cell in PyQt


code:
import tornado.httpserver
import tornado.ioloop
import tornado.web
import tornado.platform.twisted

tornado.platform.twisted.install()

from twisted.web.xmlrpc import Proxy
from twisted.internet import reactor

proxy = Proxy('http://advogato.org/XMLRPC')

from tornado.options import define, options
define("port", default=8000, help="run on the given port", type=int)

class IndexHandler(tornado.web.RequestHandler):
    def printValue(self, value):
        self.write(repr(value))
        self.finish()

    def printError(self, error):
        self.write('error: %s' % error)
        self.finish()

    @tornado.web.asynchronous
    def get(self):
        proxy.callRemote('test.sumprod', 3, 5).addCallbacks(self.printValue, self.printError)

if __name__ == "__main__":
    tornado.options.parse_command_line()
    app = tornado.web.Application(handlers=[(r"/", IndexHandler)])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()

rawDescription: Make asynchronous xmlrpc (client call) in Tornado


code:
from itertools import izip_longest

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

# use a empty dictionary as filler at the end
for group in grouper(table, 4, dict.fromkeys(labels, '')):
    for label in labels:
        print(table_row.format(label=label, cwidth=cwidth, lwidth=lwidth,
            row=[entry[label] for entry in group]))
    print()

rawDescription: Formatting output python

description: Formatting output python


code:
subq1 = session.query(
    func.array_to_string(Company.groups, ',').label('group_name')
).filter(
    (Company.status == 'active') &
    (func.array_to_string(Company.groups, ',').like(term))
).limit(limit).subquery()

subq2 = session.query(
    func.regexp_split_to_table(subq1.c.group_name, ',')
        .distinct()
        .label('group')
).subquery()

q = session.query(subq2.c.group).\
    filter(subq2.c.group.like(term)).\
    order_by(subq2.c.group).\
    limit(limit)

rawDescription: SQLAlchemy subquery in from clause without join


code: print(getattr(decoded, attr)).value()

rawDescription: Call a method on getattr() function

description: Call a method on getattr() function


code:
string = 'abcdefghijklmnopqrstuvwxyz0123456789'
sum(zip(string[0::10], string[9::10]), ())

rawDescription: how to pick chars from a string in defined but different sized steps?

description: Pick chars from a string in defined but different sized steps


code:
def symmetrized_item_selection(listA, listB, listC):
    best_results = []
    for ordering in itertools.permutations([listA, listB, listC]):
        best_results.extend(item_selection(*ordering))
    return min(best_results, key = distance)

rawDescription: Finding the most similar numbers across multiple lists in Python


rawDescription: Perl unpack to Python Conversion

description: Perl unpack to Python Conversion


code: lis.append((istart, iend, ipeople, itype))

rawDescription: Adding things into a list in the same order


rawDescription: Why do pyplot methods apply instantly and subplot axes methods do not?


code:
def get_value():
   return 5

def print_value(x):
   print(x)

print_value(get_value())

rawDescription: Python/Pygame: How do you take output from a function and use it as input for another?


code:
def fan_out(modifier):
    def _fan_out(particle):
        d = particle.alive / modifier
        d += 1
        particle.x += random.randint(-d, d)
    return _fan_out

rawDescription: How to implement a particle engine

description: Implement a particle engine


code: myCmpItem = map(int, myCmpItem)

rawDescription: Order a list by all item's digits in Python


code:
while 1:
    product = queue.get()
    if product is None:
        break
    consume(product)

rawDescription: How to terminate Producer-Consumer threads from main thread in Python?

description: Terminate Producer-Consumer threads from main thread in Python


code:
newDict = {}
for k, v in itemList:
    newDict.setdefault(k, []).append(v)

rawDescription: Is there a more compact way to convert a sequence of (key,value) tuples into a {key:[value,..],..} dict?


code:
import random
def sample_gen(n, forbid):
    state = dict()
    track = dict()
    for (i, o) in enumerate(forbid):
        x = track.get(o, o)
        t = state.get(n-i-1, n-i-1)
        state[x] = t
        track[t] = x
        state.pop(n-i-1, None)
        track.pop(o, None)
    del track
    for remaining in xrange(n-len(forbid), 0, -1):
        i = random.randrange(remaining)
        yield state.get(i, i)
        state[i] = state.get(remaining - 1, remaining - 1)
        state.pop(remaining - 1, None)

rawDescription: How to incrementally sample without replacement?

description: Incrementally sample without replacement


rawDescription: Python: Make last item of array become the first


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

t = np.linspace(0,10,100)
x = np.random.rand(100,3)
y = np.random.rand(100,3)
colmaps = ['Blues', 'Greys', 'Reds']

for i in range(3):
    plt.scatter(x[:,i], y[:,i], c=t, cmap=colmaps[i], label=i)

plt.legend()
ax = plt.gca()
legend = ax.get_legend()
legend.legendHandles[0].set_color(plt.cm.Blues(.8))
legend.legendHandles[1].set_color(plt.cm.Greys(.8))
legend.legendHandles[2].set_color(plt.cm.Reds(.8))
plt.show()

rawDescription: Python Scatter Plot with Colorbar and Legend Issues


rawDescription: creating proper tsv output from dictionary of dictionaries

description: Creating proper tsv output from dictionary of dictionaries