code:
_pattern = r"""(?x)       # enable verbose mode (which ignores whitespace and comments)
    ^                     # start of the input
    [^\d+-\.]*            # prefixed junk
    (?P<number>           # capturing group for the whole number
        (?P<sign>[+-])?       # sign group (optional)
        (?P<integer_part>         # capturing group for the integer part
            \d{1,3}               # leading digits in an int with a thousands separator
            (?P<sep>              # capturing group for the thousands separator
                [ ,.]                 # the allowed separator characters
            )
            \d{3}                 # exactly three digits after the separator
            (?:                   # non-capturing group
                (?P=sep)              # the same separator again (a backreference)
                \d{3}                 # exactly three more digits
            )*                    # repeated 0 or more times
        |                     # or
            \d+                   # simple integer (just digits with no separator)
        )?                    # integer part is optional, to allow numbers like ".5"
        (?P<decimal_part>     # capturing group for the decimal part of the number
            (?P<point>            # capturing group for the decimal point
                (?(sep)               # conditional pattern, only tested if sep matched
                    (?!                   # a negative lookahead
                        (?P=sep)              # backreference to the separator
                    )
                )
                [.,]                  # the accepted decimal point characters
            )
            \d+                   # one or more digits after the decimal point
        )?                    # the whole decimal part is optional
    )
    [^\d]*                # suffixed junk
    $                     # end of the input
"""

rawDescription: Fuzzy smart number parsing in Python

description: Fuzzy smart number parsing in Python


code:
with open("puzzle.txt") as f:
    puzzle = [list(line.strip()) for line in f]

rawDescription: 2d array structure python

description: 2d array structure python


code: root.option_add("*Font", bigfont)

rawDescription: Combobox fontsize in tkinter

description: Combobox fontsize in tkinter


code:
def game(phrase_to_guess):
    return input("Guess the phrase: ") == phrase_to_guess

def main():
    phrase = "hello, world"
    while (not(game(phrase))):
        print("Incorrect.")
    print("Correct")

main()

rawDescription: How do I re-run code in Python?

description: Re-run code in Python


rawDescription: How to shift a list nth time in Left or Right and fill it up with 0 in python

description: Shift a list nth time in Left or Right and fill it up with 0 in python


code:
end = line.index('"', start)
end
line[start:end]

rawDescription: Writing from one file to another python

description: Writing from one file to another python


code:
deques = [deque1, deque2, deque3]
for item in itertools.chain(*deques):
    some_action(item)

rawDescription: An elegant and fast way to consecutively iterate over two or more containers in Python?


code:
from datetime import datetime

class GetClimate(PeriodicThread):
    def main(self):
       timestamp = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
       temperature, humidity = Adafruit_DHT.read_retry(Adafruit_DHT.DHT22, 4)
       if temperature is not None and humidity is not None:
           print(timestamp + ' - Temperature: {:.1f} C'.format(temperature))
           print(timestamp + ' - Humidity:    {:.1f} %'.format(humidity))
           self.temperature = temperature
           self.humidity = humidity

rawDescription: Python Threading not stopping

description: Python Threading not stopping


rawDescription: creating multiple dataframes which contain subsets of an orginal pandas dataframe

description: Creating multiple dataframes which contain subsets of an orginal pandas dataframe


code: re.findall('^T', ..., flags=re.M)  # M is an alias for MULTILINE

rawDescription: Regex - Print the lines starting with a T


code:
import re

testcases = ['TUW1TH', 'JUWRG', 'BUIUW1', 'ATWKO', 'EWRG', 'AGDTWSD', 'W1EF', 'EW1RG']

# The `(W1?)` part matches a 'W' with an optional '1'. The `(?<!U)` part
#     matches the current position only if it wasn't a preceded by a 'U'
pattern = re.compile(r'(?<!U)(W1?)')

for s in testcases:
    print(pattern.sub(r'\1 ', s))

rawDescription: Python regex with negated pattern

description: Python regex with negated pattern


rawDescription: How do you write one element of multiple lists to a file before moving on to the next element?

description: Write one element of multiple lists to a file before moving on to the next element


code:
start_list[::2] = len(start_list[::2])*[1]
start_list

rawDescription: Can't figure out how to increment certain values in a list


code:
import time
try:
    # python 2.x
    import Tkinter as tk
except ImportError:
    # python 3.x
    import tkinter as tk

class Example(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)

        self.text = tk.Text(self, height=6, width=40)
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.text.yview)
        self.text.configure(yscrollcommand=self.vsb.set)
        self.vsb.pack(side="right", fill="y")
        self.text.pack(side="left", fill="both", expand=True)

        self.add_timestamp()

    def add_timestamp(self):
        self.text.insert("end", time.ctime() + "\n")
        self.text.see("end")
        self.after(1000, self.add_timestamp)

if __name__ == "__main__":
    root =tk.Tk()
    frame = Example(root)
    frame.pack(fill="both", expand=True)
    root.mainloop()

rawDescription: autoscroll of text and scrollbar in python text box

description: Autoscroll of text and scrollbar in python text box


code:
def is_regen(s):
    return s[-5:] == '_regen'

rawDescription: Python Pandas: Changing One column based on another dynamically


rawDescription: Calling a function on every column of a pandas DataFrame


code:
def get_searchdata():
    spec_list = Speciality.objects.order_by('name')
    ins_list = Insurance.objects.order_by('name')
    dict_data = {'Specialties': spec_list,
            'Insurances' : ins_list}
    return dict_data

rawDescription: Django context dictionary DRY

description: Django context dictionary DRY


code: print(rc2xy(0, 1))

rawDescription: How to I get the coordinates of a cell in a geotif?


code:
concordance_index = ConcordanceIndex2(tokens, key=lambda s: s.lower())
concordance_index.print_concordance('\xce\x9a\xce\x91\xce\x99\xce\xa3\xce\x91\xce\xa1\xce\x95\xce\x99\xce\x91\xce\xa3')

rawDescription: Concordance Unicode characters in Unicode corpus in nltk


code:
last = lines[-1]
last[11:].split()
numbers = last[11:].split()
print('avg / total & ' + ' & '.join(numbers) + r'\\')

rawDescription: convert string to latex table format in Python

description: Convert string to latex table format in Python


rawDescription: Save a subdomain name to String

description: Save a subdomain name to String


code: all( d1.get(x) == d2.get(x) for x in l )

rawDescription: Equality of dictionaries

description: Equality of dictionaries


code:
A # A is (2,2,3)
unq_out

rawDescription: Efficiently count the number of occurrences of unique subarrays in NumPy?


code:
...
ser = serial.Serial('/dev/tty.usbserial', 9600)
ser.write('5')
ser.readline()
...

rawDescription: Communication from arduino to a browser extension all run locally


code:
import glob
import os
import matplotlib.pyplot as plt

os.chdir('X:/stormdays')
for file in glob.glob("*.oax"):
    spc_file = open(file, 'r').read()
    try:
        newName = file[6:8] + '-' + file[4:6] + '-' + file[0:4] + ' ' + file[8:12] +  ' (Observed) - Sea Breeze Day'
        plt.title(newName, fontsize=12, loc='left')
        plt.savefig('X:/' + newName + '.png')

    except Exception:
        pass

rawDescription: Incorporate elements from input file name into figure title and figure file name in Python


code:
import sunrise as sr
for lat in range(0, 65, 5):
    sr.lsr(lat, -110, 2013, 4)

rawDescription: Best strategy for storing precomputed sunrise/sunset data?


code:
def decorator_factory(value):
    def msg_decorator(f):
        def inner_dec(*args, **kwargs):
            g = f.func_globals
            sentinel = object()

            oldvalue = g.get('var', sentinel)
            g['var'] = value

            try:
                res = f(*args, **kwargs)
            finally:
                if oldvalue is sentinel:
                    del g['var']
                else:
                    g['var'] = oldvalue

            return res
        return inner_dec
    return msg_decorator

rawDescription: How to inject variable into scope with a decorator in python

description: Inject variable into scope with a decorator in python


code:
hot_str = {
    0: "You guessed it!",
    1: "Extremely hot!",
    10: "Frozen cold!",
    }

print(hot_str[heat(diff)])

rawDescription: Checking if number is within more or less range in Python


code:
from django.db import models

class Example(models.Model):
    VALUES = {
        'AA': 1,
        'AB': 3,
        'AC': 2,
        'BA': 1,
        'BB': 3,
        'BC': 2,
    }

    field_a = models.CharField(default='AA', max_length=2,)
    field_b = models.CharField(default='BA', max_length=2,)

    # The value in this field is derived from the first two.
    # Updated every time the model instance is saved.
    field_sum = models.PositiveIntegerField(
        default=0,  # This value will be overwritten during save()
        editable=False,  # Hides this field in the admin interface.
    )

    def save(self, *args, **kwargs):
        # calculate sum before saving.
        self.field_sum = self.calculate_sum()
        super(Example, self).save(*args, **kwargs)

    def calculate_sum(self):
        """ Calculate a numeric value for the model instance. """
        try:
            value_a = self.VALUES[self.field_a]
            value_b = self.VALUES[self.field_b]
            return value_a + value_b
        except KeyError:
            # Value_a or value_b is not in the VALUES dictionary.
            # Do something to handle this exception.
            # Just returning the value 0 will avoid crashes, but could
            # also hide some underlying problem with your data.
            return 0

rawDescription: Django ORM to sum two columns to fill a third one


code:
lst = list(gen)
lst

rawDescription: Convert generator object to list for debugging


rawDescription: Subclass super returning self should return subclass


rawDescription: Verifying arithmetic sequence python

description: Verifying arithmetic sequence python


code:
first  = {"one":"un", "two":"deux", "three":"trois"}
second = {"one":"un", "two":"deux", "three":"trois", "foo":"bar"}
first.items() <= second.items()
first['four'] =  'quatre'
first.items() <= second.items()

rawDescription: Test if dict contained in dict

description: Test if dict contained in dict


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

def polar_heat(values, thetas=None, radii=None, ax=None, fraction=0.3,
               **kwargs):

    values = np.atleast_2d(values)
    if thetas is None:
        thetas = np.linspace(0, 2*np.pi, values.shape[1]).reshape(1, -1)
    if radii is None:
        radii = np.linspace(0, 1, values.shape[0] + 1).reshape(-1, 1)
    if ax is None:
        fig, ax = plt.subplots(1, 1, subplot_kw={'polar':True})

    mesh = ax.pcolormesh(thetas, radii, values, **kwargs)

    radrange = radii.ptp()
    ax.set_rlim(radrange * (1 - 1. / fraction), radrange)
    ax.set_axis_off()

    return mesh

rawDescription: Python plot data as a function of angle in a ring


rawDescription: How to select all data from Data Frame?

description: Select all data from Data Frame


code:
def EncryptCode():
    print ("This text below will be encrypted:")
    printMessage(encrypt)
    eightNumKey = 0
    ASCIINumber = []
    ASCIIKey = []
    while eightNumKey !=8:
        eightNumKey = eightNumKey + 1
        RandomNumber = random.randint(33,126)
        ASCIINumber.append(RandomNumber)
        RandomCharacter = chr(RandomNumber)
        ASCIIKey.append(RandomCharacter)
    print ("This is your eight character key. \n",ASCIIKey)

rawDescription: How to print only the result of the encryption and not the intermediate steps?

description: Print only the result of the encryption and not the intermediate steps


code:
rules = [
    Rule(LinkExtractor(allow=r"/q/hp\?s=\w+\.\w+&a=\d+&b=\d+&c=\d+&d=\d+&e=\d+&f=\d+&g=m"), follow=True)
]

rawDescription: Scrapy: Scraping very select URLs

description: Scrapy: Scraping very select URLs


rawDescription: Sqlalchemy explicit locking of Postgresql table


code:
headers = {'content-type': 'application/json'}
response = requests.get('http://10.xx.xx.xx:1234/virtual-networks', headers=headers)

rawDescription: How would you use a cURL "GET" with a Python based REST API for network programming?


code:
a = np.random.random(20)
a = a.reshape(4,5,1)
a
b = b.reshape(y, x, z)
x,y,z = a.shape
b = a.T
b = b.reshape(y,x,z)
b

rawDescription: Reshape/resize(pivot?) N-dimensional numpy array column-wise


code: columns[0] = [int(n) for n in columns[0]]

rawDescription: Convert a column in a list to number, keeping other columns unchanged


rawDescription: Same observation in multiple columns

description: Same observation in multiple columns


code:
plt.bar(x, 100 + y, bottom = -100)
# or, more general:
# plt.bar(x, -m + y, bottom = m)
# where m is the minimum value of your value array, m = np.min(y)

rawDescription: How can I properly plot -dBc values in matplotlib?

description: Properly plot -dBc values in matplotlib


rawDescription: How to determine if a string is between two characters w/o Regex?

description: Determine if a string is between two characters w/o Regex


rawDescription: python getting weekday from an input date

description: Python getting weekday from an input date


code:
def recur(l,call=1):
    if not l:
        return 0
    else:
        print("l = {} and  l[0] = {} on recursive call {}".format(l,l[0],call))
        call+=1
        return recur(l[1:],call) + len(l[0])

rawDescription: How to count items in list recursively

description: Count items in list recursively


code:
from django.http import HttpResponse
from django.shortcuts import get_object_or_404

def xml_view(func):
  def wrapper(*args, **kwargs):
    result = func(*args, **kwargs)
    return HttpResponse(serializers.serialize("xml", result),
        mimetype="text/xml")
  return wrapper

@xml_view
def index(request):
  return Books.objects.all()

@xml_view
def get(request, id):
  return get_object_or_404(Book, pk=id)

rawDescription: Django ease of building a RESTful interface


code: map(lambda k: add_x_to_input(some_value_for_x, k), myList)

rawDescription: Map function and input function parameters


code:
import collections
def intermix(items, nconsecutive=2):
    counter = collections.Counter(items)
    # sort from most common to least common
    items = sorted(items, key=counter.get, reverse=True)
    N = len(items)
    count = 0
    # remember the last two values
    last = []
    for i in range(N):
        val = items[i]
        if len(last) < nconsecutive:
            if last and val == last[-1]:
                last.append(val)
            else:
                last = [val]
            counter[val] -= 1
            yield val
        else:
            # last is full; find a different value
            for j in range(i, N):
                if items[j] != last[-1]:
                    items[i], items[j] = items[j], items[i]
                    val = items[i]
                    last = [val]
                    counter[val] -= 1
                    # as items are yielded, the meaning of "most common" can change.
                    items[i+1:] = sorted(items[i+1:], key=counter.get, reverse=True)
                    yield val
                    break
            else:
                raise ValueError('No solution possible')

rawDescription: Mix two lists python

description: Mix two lists python


code:
c.execute("""SELECT spam, eggs, sausage FROM breakfast
          WHERE price < %s""" % (max_price,))

rawDescription: Python best practice and securest to connect to MySQL and execute queries


rawDescription: Why my python and objective-c code get different hmac-sha1 result?


code: [sum(ai) for ai in zip(*arra)]

rawDescription: Iterating and selecting a specific array from a multidimensional array in Python


code:
def a():
    b = c = d

dis.dis(a)

rawDescription: Why is assigning to multiple targets (identifier/attribute) producing strange results?


code:
from pprint import pprint
import ruamel.yaml as yaml

yaml_str = """\
-
  - &base_value_structure
    path_to_value: 'path to element'
    selector_type: 'XPATH'
    required: false
  - &base_other_structure
    key1: val1
    key2: val2
- Default_profile: &Default_profile
    title:
        <<: *base_value_structure
        path_to_value: "//div[@id='ctitle']/text()"
"""

data = yaml.load(yaml_str)[1]
pprint(data)

rawDescription: Use key/value inheritence but leave it out of the result in pyyaml


code:
str = raw_input('please enter string: ')
# pressing enter didn't work...
str
str.replace(' ', '')

rawDescription: Remove all line breaks from a long string of text


rawDescription: How to modify each item in the dictionary with the value from the rest of others items in the same dictionary combined exhaustedly?

description: Modify each item in the dictionary with the value from the rest of others items in the same dictionary combined exhaustedly


code:
quad_generator = yield_next_two(yield_next_two(original))
list(quad_generator)
nearly_eights_generator = yield_next_two(yield_next_two(yield_next_two(original)))
list(nearly_eights_generator)

rawDescription: create sublists within sublists in python

description: Create sublists within sublists in python


code: firstbyte & 0b111111

rawDescription: Read 14 bit number from 2 bytes

description: Read 14 bit number from 2 bytes


code:
# indexing: list of x and y coordinates, and functions that map them to index
x  = np.unique(data['x']).tolist()
y  = np.unique(data['y']).tolist()
ix = np.vectorize(lambda i: x.index(i), otypes='i')
iy = np.vectorize(lambda j: y.index(j), otypes='i')

# create output array
r  = np.zeros((x.size,y.size), float)   # default value is 0
r[ix(data['x']), iy(data['y'])] = data['value']

rawDescription: Nx3 column data to 2d matrix for image processing


code: json.dump([requests.get(url).json() for url in URLs], fp)

rawDescription: Python 3 - Parse JSON from multiple API requests into a list and output to a file


code:
cs, rs, vs = zip(*data)
print ('\n'.join(['  '+' '.join(cs)] + [r+' '+'  '*i+v for i,(r,v) in enumerate(zip(rs,vs))]))

rawDescription: Formatting output as table

description: Formatting output as table


code: res2 = res_1 / divide_by

rawDescription: Python: How to perform division according to its representation?


code:
max('zara', 'marco')
max('zara', 'marco', key=len)
max('apples', 'oranges', 'cherries', 'banana')
max('apples', 'oranges', 'cherries', 'banana', key=len)

rawDescription: Max() function does not return largest value in Python 3.4


code:
from urllib.request import urlopen
html = urlopen("https://accounts.google.com/NewAccount").read().decode('utf-8')

rawDescription: How do I submit a request for a new Google account?

description: Submit a request for a new Google account


code:
import random
a = [random.random() - 0.5 for _ in range (10)]
a = [None if i < 0 else i for i in a]

rawDescription: Matlab's Logicals for Array Indexing - Python Equivalent


rawDescription: Simulate static variables in python with closures


rawDescription: comparing column values based on other column values in pandas

description: Comparing column values based on other column values in pandas


code:
def process():
    # Do the measurement
    # Do the calculation
    Buffer.onNewReading(data1, data2, etc)

rawDescription: Python thread-safe access without blocking or uncontrolled queue growth?


code: Matrix = [[0 for x in range(5)] for x in range(5)]

rawDescription: Append Numpy Array post Creation

description: Append Numpy Array post Creation


code:
def iter_data(path):
    with open(path) as data_file:
        for line in data_file:
            yield from line.split()

arr = np.fromiter(iter_data('data.txt'), int)

rawDescription: Using numpy's genfromtxt to load a triangular matrix with python


code:
iFile = "foo"
"Successfully created the file: {0}.txt".format(iFile)

rawDescription: Python - How to modify text output using %s?


rawDescription: Generate permutations of a list of lists with variable lengths


rawDescription: How do I compare two arrays element-wise in Python using specific rules?

description: Compare two arrays element-wise in Python using specific rules


code:
pages = ('alliancejjsandiego',)
objects = graph.get_objects(pages, fields='emails,name')
for page in objects.values():
    print('{}: {}'.format(page['name'], ', '.join(page['emails'])))

rawDescription: Using Selenium to scrape email addresses by checking for characters


code:
g('set terminal pngcairo size 800,400')
g.set_string('output', 'aaa.png')
g.refresh()
g.set_string('output')

rawDescription: hardcopy of gnuplot.py plot

description: Hardcopy of gnuplot.py plot


code:
class MyDictSubclass(dict):
    pass

rawDescription: Python builtin functions aren't really functions, right?


code:
for q, (a, b, c) in enumerate(zip(abc, xyz, mnl)):
    print(a, b, c)
    if int(a) < int(b) and int(a) < int(c):
        print("a", "\t", a)
    elif int(b) < int(a) and int(b) < int(c):
        print("b", "\t", b)
    elif int(c) < int(a) and int(c) < int(b):
        print("c", "\t", c)

rawDescription: Python skipping negative values

description: Python skipping negative values


code: result = dict(alist[0], c=[d['c'] for d in alist])

rawDescription: concatenate dictionaries over a key

description: Concatenate dictionaries over a key


code:
def pre_save_checkout(sender, instance, **kwargs):
    if isinstance(instance,Checkout) and instance.id and instance.department:
         substract_checkout(instance.department)

def post_save_checkout(sender, instance, **kwargs):
    if isinstance(instance,Checkout) and instance.department:
         add_checkout(instance.department)

def  substract_checkout(organizational_unit):
    organizational_unit.checkout_number-=1
    organizational_unit.save()
    if organizational_unit.parent:
        substract_checkout(organizational_unit.parent)

def  add_checkout(organizational_unit):
    organizational_unit.checkout_number+=1
    organizational_unit.save()
    if organizational_unit.parent:
        add_checkout(organizational_unit.parent)

rawDescription: How can I get a total count of a model's related objects and the model's children's related objects?

description: Get a total count of a model's related objects and the model's children's related objects


code:
import logging

def summarize():
    console_handler.setFormatter(logging.Formatter('%(message)s'))
    logger.info('Here is my report')
    console_handler.setFormatter(console_formatter)

numlevel = logging.DEBUG
logfile = 's2.log'
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

console_handler = logging.StreamHandler()
console_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
console_handler.setFormatter(console_formatter)
logger.addHandler(console_handler)

file_handler = logging.FileHandler(filename=logfile, mode='w')
file_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s:%(funcName)s - %(message)s')
file_handler.setFormatter(file_formatter)
logger.addHandler(file_handler)

logger.info('Before summary')
summarize()
logger.info('After summary')

rawDescription: Suspend the formating of the logger, then go back to it


rawDescription: Finding intersection between straight line and contour


code: soup.select("div > div.column")[1]

rawDescription: soup.select css nth of type?

description: Soup.select css nth of type


code:
from pymongo import MongoClient
connection = MongoClient("mongodb://127.0.0.1:3001/meteor")
db = connection.meteor.tasks

rawDescription: Meteor / python / mongoDB - How do I update the mongoDB from a backround running process?


rawDescription: Inconsistency in string parsing of python


code:
def count_list(l):
    c = {1:0, 2:0, 3:0, 4:0, 5:0, 6:0}  # initialize dictionary
    for key in c:
        c[key] = l.count(key)
    return c

rawDescription: I need to get my main function working

description: I need to get my main function working


code: ['CN=First Last,OU=ex,OU=am,DC=ple,DC=com', {'department': ['1234']}, 'CN=Another Person,OU=ex,OU=am,DC=ple,DC=com', {'department': ['1234']}]

rawDescription: Extract string from tuple embedded in a list, Python 3


code:
items = [[1, 'A'], [2, 'A'], [6, 'B'], [3, 'B'], [4, 'C'], [5, 'C'], [7, 'F']]

import itertools, operator, random

groups = [list(g) for _, g in itertools.groupby(items, operator.itemgetter(1))]
random.shuffle(groups)
shuffled = [item for group in groups for item in group]

print(shuffled)

rawDescription: Shuffle groups of sublists in Python

description: Shuffle groups of sublists in Python


code:
def second_card():
    Number2 = random.choice(Numbers)
    Type2 = random.choice(Types)
    Card2 = Number2 + " of " + Type2
    if Card2 == Card:
        return second_card()
    return (Number2, Type2, Card2)

Number2, Type2, Card2 = second_card()

rawDescription: using global variables in a python game

description: Using global variables in a python game


code: {{ "It will take place in the year %(year) and the month %(month)." % {'year': objects.when.year, 'month': objects.when.month} }}

rawDescription: How can I get a Django template to use string formatting?

description: Get a Django template to use string formatting


code:
# Don't do this - will crash Python with out-of-memory error
list(itertools.product(my_list, repeat = 100000))

# Iterate over the list instead
for one_combination in itertools.product(my_list, repeat = 100000):
    print (one_combination)

rawDescription: How to create a list or every permutation in a group of objects N-deep

description: Create a list or every permutation in a group of objects N-deep


rawDescription: iterating through list of list and dictionary python

description: Iterating through list of list and dictionary python


code: a = 'this\\is\\a\\path\\to\\file'

rawDescription: Get characters of a string (from right)

description: Get characters of a string (from right)


rawDescription: Difference between np.random.seed() and np.random.RandomState()


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

def main():
    data = generate_data()
    xbar, ybar, cov = intertial_axis(data)

    fig, ax = plt.subplots()
    ax.imshow(data)
    plot_bars(xbar, ybar, cov, ax)
    plt.show()

def generate_data():
    data = np.zeros((200, 200), dtype=np.float)
    cov = np.array([[200, 100], [100, 200]])
    ij = np.random.multivariate_normal((100,100), cov, int(1e5))
    for i,j in ij:
        data[int(i), int(j)] += 1
    return data

def raw_moment(data, iord, jord):
    nrows, ncols = data.shape
    y, x = np.mgrid[:nrows, :ncols]
    data = data * x**iord * y**jord
    return data.sum()

def intertial_axis(data):
    """Calculate the x-mean, y-mean, and cov matrix of an image."""
    data_sum = data.sum()
    m10 = raw_moment(data, 1, 0)
    m01 = raw_moment(data, 0, 1)
    x_bar = m10 / data_sum
    y_bar = m01 / data_sum
    u11 = (raw_moment(data, 1, 1) - x_bar * m01) / data_sum
    u20 = (raw_moment(data, 2, 0) - x_bar * m10) / data_sum
    u02 = (raw_moment(data, 0, 2) - y_bar * m01) / data_sum
    cov = np.array([[u20, u11], [u11, u02]])
    return x_bar, y_bar, cov

def plot_bars(x_bar, y_bar, cov, ax):
    """Plot bars with a length of 2 stddev along the principal axes."""
    def make_lines(eigvals, eigvecs, mean, i):
        """Make lines a length of 2 stddev."""
        std = np.sqrt(eigvals[i])
        vec = 2 * std * eigvecs[:,i] / np.hypot(*eigvecs[:,i])
        x, y = np.vstack((mean-vec, mean, mean+vec)).T
        return x, y
    mean = np.array([x_bar, y_bar])
    eigvals, eigvecs = np.linalg.eigh(cov)
    ax.plot(*make_lines(eigvals, eigvecs, mean, 0), marker='o', color='white')
    ax.plot(*make_lines(eigvals, eigvecs, mean, -1), marker='o', color='red')
    ax.axis('image')

if __name__ == '__main__':
    main()

rawDescription: Compute eigenvectors of image in python

description: Compute eigenvectors of image in python


rawDescription: django: how to get a substring?

description: Django: how to get a substring


code:
(i,), data = struct.unpack("I", data[:4]), data[4:]
s, data = data[:i], data[i:]

rawDescription: packing and unpacking variable length array/string using the struct module in python

description: Packing and unpacking variable length array/string using the struct module in python


code:
result = []
for classifier in classifier_list:
    result.append(classifier(data))

rawDescription: Fudging the line between classes and functions


code: df = df.loc[df["x.count"].shift() != df["x.count"]]

rawDescription: Delete the entire row if the a value in value is equal to previous row in Python


code: x = numpy.delete(x, numpy.where(~indices)[0], axis=0)

rawDescription: Numpy: choose, where, partition or something else?


code:
from urlparse import urljoin

base = "http://www.hltv.org/"
soup = BeautifulSoup(requests.get("http://www.hltv.org/?pageid=188&offset=1").content)
cont = soup.select("div.covMainBoxContent a[href*=matchid=]")

print([urljoin(base, a["href"]) for a in cont])

rawDescription: Getting TypeError when trying to concatenate 'str' and 'NoneType' objects


rawDescription: Plotting pandas timedelta

description: Plotting pandas timedelta


rawDescription: prevent unexpected stdin reads and lock in subprocess

description: Prevent unexpected stdin reads and lock in subprocess


rawDescription: Skipping rows, and saving a row into several arrays during file readings using Python


code:
import pandas as pd
import numpy as np

df = pd.DataFrame([{'a': 15, 'b': 15, 'c': 5}, {'a': 20, 'b': 10, 'c': 7}, {'a': 25, 'b': 30, 'c': 9}])
ext = {0: 'st', 1: 'nd', 2: 'rd'}
cols = df.columns

def make_ranking(row, rank=0, is_value=False):
    values = list(row[cols])
    sorted_values = sorted(set(values), reverse=True)
    value = sorted_values[rank] if len(sorted_values) > rank else np.nan
    if not is_value:
        items = [k for k, v in enumerate(values) if v == value]
        value = '/'.join([cols[item] for item in items]) or np.nan
    return value

for i in range(len(cols)):
    df[str(i+1)+ext[i]] = df.apply(make_ranking, args=(i, False, ), axis=1)
    df[str(i+1)+ext[i]+'_value'] = df.apply(make_ranking, args=(i, True, ), axis=1)

print(df)

rawDescription: Ranking columns and selecting column names


code:
guesses = ['H','A']
secret = 'HANGMAN'
state = ''.join(x if x in guesses else '_' for x in secret)
state

rawDescription: Python: hangman program

description: Python: hangman program


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


code:
s = 'hello there'

word1, word2 = s.split()

rawDescription: How can I separate two separated words in a variable?

description: Separate two separated words in a variable


code:
if value & self.PINPUT: # Value has a 1 in the PINPUT bit (bit 3)
    do_something()

rawDescription: Pythonic way of checking bit values

description: Pythonic way of checking bit values


code:
#write vbscript to file
vbscript="""if WScript.Arguments.Count < 3 Then
    WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file> <worksheet number (starts at 1)>"
    Wscript.Quit
End If

csv_format = 6

Set objFSO = CreateObject("Scripting.FileSystemObject")

src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0))
dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1))
worksheet_number = CInt(WScript.Arguments.Item(2))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Worksheets(worksheet_number).Activate

oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit
""";

f = open('ExcelToCsv.vbs','w')
f.write(vbscript.encode('utf-8'))
f.close()

rawDescription: Faster way to read Excel files to pandas dataframe


code:
for (a, b) in grid_objects(alist, blist):
    if a.is_whatever():
        b.do_something()

rawDescription: How can I, in python, iterate over multiple 2d lists at once, cleanly?


code:
result = add_together.delay(23, 42)
result.wait()

rawDescription: How do I run a long-running job in the background in Python

description: Run a long-running job in the background in Python


code: cursor.execute("INSERT INTO table VALUES(" +values+ ");")

rawDescription: Only inserting specific rows from csv file into Sqlite3 database using python


code:
img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)
# scale the image, preserving the aspect ratio
W = img.GetWidth()
H = img.GetHeight()
if W > H:
    NewW = self.PhotoMaxSize
    NewH = self.PhotoMaxSize * H / W
else:
    NewH = self.PhotoMaxSize
    NewW = self.PhotoMaxSize * W / H
img = img.Scale(NewW,NewH)

rawDescription: wxpython image doesn't fit on screen

description: Wxpython image doesn't fit on screen


code:
string_list = [set(raw_input()) for _ in range(t)]
common = set.intersection(*string_list)

rawDescription: How to approach this loop?

description: Approach this loop


code:
def autoproperty(attribute_name, can_get=True, can_set=True, allow_null=False, default_value=0):
    def getter(self):
        return getattr(self, attribute_name, default_value)
    def setter(self, value):
        if not allow_null and value is None:
            raise ValueError('Cannot set {} to None'.format(name))
        setattr(self, attribute_name, value)
    return property(getter if can_get else None, setter if can_set else None)

rawDescription: Create a class level decorator to automatically add a property


rawDescription: How to click a specific row in tablewidget using a function in Pyqt?

description: Click a specific row in tablewidget using a function in Pyqt


code:
import json

data = json.loads(json_data)
html = data['results_html']

rawDescription: BS4 get info from class with weird name

description: BS4 get info from class with weird name


code:
exercise = EuropeanExercise(Date(3,August,2011))
payoff = PlainVanillaPayoff(Option.Call, 100.0)
option = EuropeanOption(payoff,exercise)

rawDescription: Calculating EuropeanOptionImpliedVolatility in quantlib-python


code: oval_id = canvas.create_oval(x1, y1, x2, y2)

rawDescription: Binding a key press event on a Canvas widget


code:
def myFunction():
   try:
       QtCore.QCoreApplication.processEvents()
   finally:
       QTimer.singleShot(1000, myFunction())

rawDescription: QLCDNumber updating with processEvents() - Python 3


code:
with open(...) as fin:
    fin.read()   # read first time
    fin.seek(0)  # offset of 0
    fin.read()   # read again

rawDescription: Re-read an open file Python

description: Re-read an open file Python


code:
classes = dict((c.__name__, c) for c in [Iteration,Observation,DataPoint])
# ...
obj = classes[txtobj]()

rawDescription: Generating python data structures from parsed text without eval()


rawDescription: OpenMaya API (python): print more useful info from MObjects? E.g. MFloatVector(x, y, z)


code:
from endpoints_proto_datastore.ndb import EndpointsModel

class Greeting(EndpointsModel):
    message = ndb.StringProperty()

class GreetingCollection(EndpointsModel):
    items = ndb.StructuredProperty(Greeting, repeated=True)

rawDescription: POST List of Objects w/ endpoints-proto-datastore


code:
from django.contrib.contenttypes.models import ContentType

contenttype_obj = ContentType.objects.get_for_model(person_object)

Conversation.objects.filter(object_id_1=person_object.id, content_type=contenttype_obj)

rawDescription: Reference two foreign keys in Django data model


code:
from django.core import management
from YourProject import settings
management.setup_environ(settings)

from YourApp.models import YourModel

rawDescription: Uploading Data from a Python File into a Django Database


code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

driver = webdriver.Chrome()
driver.get("http://www.kegg.jp/kegg-bin/get_htext?ban00001.keg")

# locating the 4th arrow
arrow4 = driver.find_element_by_xpath("//img[@alt='4th Level']")
arrow4.click()

# wait for the grid to become visible
wait = WebDriverWait(driver, 10)
wait.until(expected_conditions.visibility_of_element_located((By.ID, "grid")))

print(driver.page_source)

driver.quit()

rawDescription: Python selenium- Clicking a javascript link doesn't load the new page but shows the url


code:
df1
df2
ind = df2.user_id.isin(df1.user_id) & df1.user_id.isin(df2.user_id)
ind
df1[ind].append(df2[ind])

rawDescription: Finding common rows (intersection) in two Pandas dataframes


code: my_resample = lambda x: x.sort_index().tail(1)

rawDescription: Resample while keeping last date from input file (and not last day computed by `resample`)


code:
# add at the beginning of settings.py
import os

# ...

TEMPLATE_DIRS = (
    os.path.join(PROJECT_ROOT, 'templates/'),
)

rawDescription: What is the path for TEMPLATE_DIRS in django settings.py when using virtualenv


code:
from itertools import groupby

for key, group in groupby(line.split(';'), key=lambda i: int(i) > 4):
    if key:
        for i in group:
            print(i)
    else:
        print(';'.join(group))

rawDescription: How does one splice a list and create a new list with the spliced section?

description: Splice a list and create a new list with the spliced section


rawDescription: What is the most efficient method to parse this line of text?


code:
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
d['a'].append(2)
d['b'].append(3)
d['c'].append(4)
d['b'].append(5)
print(d['a'])
print(d)

rawDescription: How to address a dictionary in a list of ordered dicts by unique key value?

description: Address a dictionary in a list of ordered dicts by unique key value


code:
import re

def parse(s):
    if 'day' in s:
        m = re.match(r'(?P<days>[-\d]+) day[s]*, (?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)', s)
    else:
        m = re.match(r'(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d[\.\d+]*)', s)
    return {key: float(val) for key, val in m.groupdict().iteritems()}

rawDescription: How to convert standard timedelta string to timedelta object

description: Convert standard timedelta string to timedelta object


code:
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
file_handler = logging.FileHandler('mylog.log')
file_handler.setFormatter(logging.Formatter('%(message)s'))
log.addHandler(file_handler)

log.debug('this is awesome!')

rawDescription: how do I create logging / loggers on a per thread basis?

description: Create logging / loggers on a per thread basis


code:
{"voucher_id": "5735", "code": ["MUM10"]}
{"voucher_id": "3634", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "5446", "code": ["APP20"]}
{"voucher_id": "5558", "code": ["No code for this deal"]}
{"voucher_id": "1673", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "3963", "code": ["CNATION150"]}
{"voucher_id": "5515", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4313", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4309", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "1540", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4310", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "1539", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4312", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4311", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "2785", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "3631", "code": ["Deal Activated. Enjoy Shopping"]}
{"voucher_id": "4496", "code": ["Deal Activated. Enjoy Shopping"]}

rawDescription: How to scrape coupon code of coupon site (coupon code comes on clicking button)

description: Scrape coupon code of coupon site (coupon code comes on clicking button)


rawDescription: concise and fast zip operation on lists of lists in Python?

description: Concise and fast zip operation on lists of lists in Python


code:
df1.join(df2, df1.k == df2.k, joinType='left_outer')
sqlContext.sql('SELECT * FROM df1 LEFT OUTER JOIN df2 ON df1.k = df2.k')

rawDescription: How do you perform basic joins of two RDD tables in Spark using Python?

description: Perform basic joins of two RDD tables in Spark using Python


code:
for p in res:
    if not p[-1]:
        p.pop()

rawDescription: Merge nested list items based on a repeating value


code: imageSrc = self.request.get('imageSrc')

rawDescription: python gae Render an image returned from ajax post json format

description: Python gae Render an image returned from ajax post json format


code:
import re

a = ["Britney spears", "red dog", "\xa2xe3"]
b = ["cat","dog",
     "red dog is stupid",
     "good stuff \xa2xe3",
     "awesome Britney spears",
     "transferred dogcatcher"]

#This version leaves whitespace and will match between words.
regex = re.compile('|'.join(re.escape(x) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', ' is stupid', 'good stuff ', 'awesome ', 'transfercatcher']

#This version strips whitespace from either end
# of the returned string
regex = re.compile('|'.join(r'\s*{}\s*'.format(re.escape(x)) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transfercatcher']

#This version will only match at word boundaries,
# but you lose the match with \xa2xe3 since it isn't a word
regex = re.compile('|'.join(r'\s*\b{}\b\s*'.format(re.escape(x)) for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff \xa2xe3', 'awesome', 'transferred dogcatcher']

#This version finally seems to get it right.  It matches whitespace (or the start
# of the string) and then the "word" and then more whitespace (or the end of the
# string).  It then replaces that match with nothing -- i.e. it removes the match
# from the string.
regex = re.compile('|'.join(r'(?:\s+|^)'+re.escape(x)+r'(?:\s+|$)' for x in a))
c = [regex.sub("",x) for x in b ]
print (c) #['cat', 'dog', 'is stupid', 'good stuff', 'awesome', 'transferred dogcatcher']

rawDescription: check if element of list is present in elements of another list

description: Check if element of list is present in elements of another list


code:
from collections import namedtuple

# use namedtuple to define some simple classes (requires Py2.6 or later)
FooA = namedtuple('FooA', 'id name type foo')
FooB = namedtuple('FooB', 'idfoo namefoo spam')

# create a wrapper class for FooB's to look like a FooA
class FooAMimic(object):
    attrMap = dict(zip(FooA._fields, FooB._fields))
    # or if the fields aren't nicely ordered, declare this mapping explicitly
    #~ attrMap = { 'id' : 'idfoo', 'name' : 'namefoo', 'foo' : 'spam' }
    def __init__(self, obj):
        self.obj = obj
    def __getattr__(self, aname):
        ob = self.obj
        if aname in self.attrMap:
            return getattr(ob, self.attrMap[aname])
        elif  hasattr(ob, aname):
            return getattr(ob, aname)
        else:
            raise AttributeError("no such attribute " + aname)
    def __dir__(self):
        return sorted(set(dir(super(FooAMimic,self))
                          + dir(self.obj)
                          + list(FooA._fields)))

rawDescription: Best way to construct a "complex" data structure in Python

description: Construct a "complex" data structure in Python


code:
w = Weather.objects.get(pk=1)
w.region.plan.name
w.district.plan.name
w.temp_max
w.temp_min

rawDescription: How to join the results of two tables in django python

description: Join the results of two tables in django python


code:
def partition(lst, n):
    division = len(lst) / float(n)
    return [ lst[int(round(division * i)): int(round(division * (i + 1)))] for i in xrange(n) ]

rawDescription: python list average over bin sizes

description: Python list average over bin sizes


code:
p = re.compile(r'(?!\bbadger\b|\bmushroom\b)\b[^\s]+\b')
test_str = "badger randomword badger mushroom mushroom somethingelse"
subst = "mushroom"
result = re.sub(p, subst, test_str)

rawDescription: Python regex - Replace all but a few words


rawDescription: Space between words in matplotlib plot axis label


code:
def get_model_metadata(model_class, meta_whitelist=[]):
  field_list = model_class._meta.fields
  return_data = {}
  for field in field_list:
    field_name = field.name
    field_meta = field.__dict__
    return_meta = {}
    for meta_name in field_meta:
      if meta_name in meta_whitelist:
        return_meta[meta_name] = field_meta[meta_name]
    if len(return_meta) > 0:
      return_data[field_name] = return_meta
  return return_data

rawDescription: How can i extract metdata from django models

description: Extract metdata from django models


code:
indexes = [defaultdict(list) for _ in range(3)]

for i, values in enumerate(yourstructure):
    for index, val in zip(indexes, values):
        index[val].append(i)

rawDescription: How to assign a collection of 3 pieces of data

description: Assign a collection of 3 pieces of data


code:
def foo():
    ...

def bar():
    ...

funcs = {"foo": foo, "bar": bar}

funcs["foo"]()

rawDescription: Call a function using getattr in Python

description: Call a function using getattr in Python


code:
values = { 'a': 1, 'b': 2 }
s = sum(**values)

rawDescription: What does the Star operator mean in Python?


code: {{ title }}

rawDescription: Returning Data from Python Function for use with Jinja2 Template (Flask)


code:
df    #Before naming the column
df.columns = ['ColA']
df    #Renamed
df['ColA'][0]    #Now you can access the column using the new name
df[0][0]         #... but trying the old name will not work

rawDescription: No Column Names in pandas python

description: No Column Names in pandas python


rawDescription: How do I strip patterns or words from the end of the string backwards?

description: Strip patterns or words from the end of the string backwards


rawDescription: Plot different DataFrames in the same figure


code:
for i in mylist:
   print("student: %s" % i.student)

rawDescription: How to display object properties in python

description: Display object properties in python


rawDescription: Python parse ip regex

description: Python parse ip regex


code:
return max(
    sqrt( abs(x - cx) + abs(y - cy) )
    for cx, cy in chests)

rawDescription: returning the largest or smallest result from a for loop in python

description: Returning the largest or smallest result from a for loop in python


code: df

rawDescription: Widening Pandas Data Frame, Similar to Pivot or Stack/Unstack


rawDescription: Merge two dictionaries in python with lists as values


code:
from blessings import Terminal
term = Terminal()

question = "{t.red}{}{t.normal}{t.bold}".format
answer = "{t.normal}{t.move_up}{t.green}{}{t.normal}{t.clear_eol}".format

input(question("Question: ", t=term))
print(answer("Correct!", t=term))

rawDescription: How to print over raw_input's line in Python?

description: Print over raw_input's line in Python


code: L[::] = 'potato'

rawDescription: How does python do list slices and modification on the LHS?


rawDescription: Python NumPy Arrays

description: Python NumPy Arrays


code: datetime.now() - datetime.now()

rawDescription: Get seconds since midnight in python

description: Get seconds since midnight in python


code:
import argparse, sys

parser = argparse.ArgumentParser(description='Process src to dst')
parser.add_argument('src', type=argparse.FileType('r'),
                    default=sys.stdin)
parser.add_argument('dst', type=argparse.FileType('w'),
                    default=sys.stdout)
options = parser.parse_args()

rawDescription: How to convert sys.argv to argparse in python

description: Convert sys.argv to argparse in python


rawDescription: Add vertex attributes to a weighted igraph Graph in python


code:
r = requests.get(settings.STATICMAP_URL.format(**data), stream=True)
if r.status_code == 200:
    with open(path, 'wb') as f:
        for chunk in r.iter_content(1024):
            f.write(chunk)

rawDescription: How to download image using requests

description: Download image using requests


code:
from astropy.io import fits
h = fits.getheader('SN1415_F625W_1_drz.fits')
f = fits.open('SN1415_F625W_1_drz.fits')
f
from astropy import wcs
w = wcs.WCS(f[0].header)
w.wcs.naxis
f[0].data
w = wcs.WCS(f[1].header)
w.wcs.naxis
f[1].data
w.wcs_pix2world(100., 100., 1)

rawDescription: Find physical coordinates of a pixel in a fits file with python


code:
with open(filename,'r') as f:
    for key,group in it.groupby(f,lambda line: line.startswith(':Entry')):
        if not key:
            for line in group:
                ...

rawDescription: Splitting large text file by a delimiter in Python


code:
# Python 3
[x**2 for x in range(10)] == list(map(lambda x: x**2, range(10)))
[2**i for i in range(13)] == list(map(lambda x: 2**x, range(13)))
S = [x**2 for x in range(10)]
[x for x in S if x % 2 == 0] == list(filter(lambda x: x % 2 == 0, S))

rawDescription: Are Python list comprehensions the same thing as map/grep in Perl?


code: np.diff(C, axis = -1)

rawDescription: In place difference between elements of a Numpy array


code:
# command01.py
from ._private import *

class Command(SharedCommand):
    def handle(self, *args, **options):
        ...

rawDescription: How to abstract away command code in custom django commands

description: Abstract away command code in custom django commands


code:
def name_gen():
    with open('C:/python/namessample.txt') as nameFile:
        return [name.strip('"') for name in nameFile.read().split(',')]

rawDescription: generating strings from a file

description: Generating strings from a file


code: towns = list(d.keys())

rawDescription: python print a variable as a string from a list

description: Python print a variable as a string from a list


code:
self.frame2 = Frame(self, relief=FLAT, borderwidth=2)
self.frame2.pack(side=TOP, fill=BOTH, expand=False)

rawDescription: How do I access a label to change its text in Tkinter

description: Access a label to change its text in Tkinter


code:
def handle_csrf():
    """The general recommendation by people in the know [OWASP]_, is
       'to implement the Synchronizer Token Pattern (STP_)'.

       .. [OWASP] The Open Web Application Security Project
          (https://www.owasp.org/index.php/Cross-\
Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet)
       .. _STP: http://www.corej2eepatterns.com/Design/PresoDesign.htm

    """

rawDescription: How do I break a link in a rst docstring to satisfy pep8?

description: Break a link in a rst docstring to satisfy pep8


code:
import json

json_string = json.dumps(d)

rawDescription: django - aggregate data according to field in model

description: Django - aggregate data according to field in model


rawDescription: Keep function namespace alive for debugging in IPython


rawDescription: Performing if inside lambda

description: Performing if inside lambda


code:
is_text = True
d = dict(is_text=is_text)

rawDescription: Is it possible to convert a variable as a string?


code:
import re
r = re.compile("[/!/?,:.;-]")

t = r.sub(" ",instance.title) # clear punctuation
instance.hashtag = "".join([s.capitalize() for s in t.rsplit()])

rawDescription: Convert post title to CamelCase

description: Convert post title to CamelCase


code:
from lxml import html
from lxml.html import etree
import requests
from bs4 import UnicodeDammit
import chardet

try:
    self.log.debug("Try to get content from page {}".format(url))
    r = requests.get(url)
except requests.exceptions.RequestException as e:
    self.log.warn("Unable to get page content of the url: {url}. "
                  "The reason: {exc!r}".format(url=url, exc=e))
    raise ParsingError(e.message)

ud = UnicodeDammit(r.content, is_html=True)

enc = ud.original_encoding.lower()
declared_enc = ud.declared_html_encoding
if declared_enc:
    declared_enc = declared_enc.lower()
# possible misregocnition of an encoding
if (declared_enc and enc != declared_enc):
    detect_dict = chardet.detect(r.content)
    det_conf = detect_dict["confidence"]
    det_enc = detect_dict["encoding"].lower()
    if enc == det_enc and det_conf < THRESHOLD_OF_CHARDETECT:
        enc = declared_enc
# if page contains any characters that differ from the main
# encodin we will ignore them
content = r.content.decode(enc, "ignore").encode(enc)
htmlparser = etree.HTMLParser(encoding=enc)
root = etree.HTML(content, parser=htmlparser)
etree.strip_elements(root, html.etree.Comment, "script", "style")
text = html.tostring(root, method="text", encoding=unicode)

rawDescription: Problems with encoding while parsing html document with lxml


rawDescription: numpy, named columns

description: Numpy, named columns


code:
import subprocess

def create_pdf(input_filename, output_filename):
    process = subprocess.Popen([
        'latex',   # Or maybe 'C:\\Program Files\\MikTex\\miktex\\bin\\latex.exe
        '-output-format=pdf',
        '-job-name=' + output_filename,
        input_filename])
    process.wait()

rawDescription: How to generate pdf with call to MiKTeX from python?

description: Generate pdf with call to MiKTeX from python


code:
set(['this', 'test', 'is', 'my'])
True

rawDescription: Extract Words from a file

description: Extract Words from a file


code: lst = [line[6:17] for line in open(fname)]

rawDescription: How to read, in a line, all characters from column A to B

description: Read, in a line, all characters from column A to B


code:
my_list[0] #what you were trying to assign to 'a'
my_list[0][0] #the first element in that sub-list

rawDescription: Building multiple lists from single list with multiple lists within


code:
import calendar, datetime

# Convert a unix time u to a datetime object d, and vice versa
def dt(u): return datetime.datetime.utcfromtimestamp(u)
def ut(d): return calendar.timegm(d.timetuple())

rawDescription: Convert a unixtime to a datetime object and back again (pair of time conversion functions that are inverses)


code:
# Enforce UTF-8 encoding
import sys
stdin, stdout = sys.stdin, sys.stdout
reload(sys)
sys.stdin, sys.stdout = stdin, stdout
sys.setdefaultencoding('UTF-8')

# SQLite3 database
import sqlite3
# Pandas: Data structures and data analysis tools
import pandas as pd

# Read database, attach as Pandas dataframe
db = sqlite3.connect("Applications.db")
df = pd.read_sql_query("SELECT path, language, date, shortest_sentence, longest_sentence, number_words, readability_consensus FROM applications ORDER BY date(date) DESC", db)
db.close()
df.columns = ['Path', 'Language', 'Date', 'Shortest Sentence', 'Longest Sentence', 'Words', 'Readability Consensus']

# Parse Dataframe and apply Markdown, then save as 'table.md'
cols = df.columns
df2 = pd.DataFrame([['---','---','---','---','---','---','---']], columns=cols)
df3 = pd.concat([df2, df])
df3.to_csv("table.md", sep="|", index=False)

rawDescription: Programmatically convert pandas dataframe to markdown table


code:
g = {1:{2:.5, 3:.2}, 2:{4:.7}, 4:{5:.6, 3:.3}}
hubs = g.items() # list of nodes and outgoing vertices
size=max(map(lambda hub: max(hub[0], max(hub[1].keys())), hubs))+1 # matrix dimension is highest known node index + 1
matrix=[[None]*size for row in range(size)] # set up a matrix of the appropriate size

for node, vertices in hubs: # loop through every node in dictionary
    for vertice, weight in vertices.items(): # loop through vertices
        matrix[vertice][node] = weight # define adjacency of both nodes by assigning the vertice's weight

rawDescription: Creating Adjacency Lists from Dicts in Python


code:
store.get_storer('df').levels
store

rawDescription: Pandas HDFStore of MultiIndex DataFrames: how to efficiently get all indexes


code:
for lt, lo in (map(float, pair.split(',')) for pair in input.split()):
    ...

rawDescription: extract latitude and longitude from given input

description: Extract latitude and longitude from given input


rawDescription: How can I search a file for all text lines prefaced with numbers in a certain format and move them to a new line

description: Search a file for all text lines prefaced with numbers in a certain format and move them to a new line


code:
m = re.search(p,'http://www.abc.com/test')
m.group('host') # 'www.abc.com'
m.group('port') # '' i.e. you'll have to treat this as '80'

rawDescription: Parsing hostname and port from string or url


code:
#random test data:
x_data = random_sample(100)
y_data = random_integers(10,50,(100))

#GOOD PLOT:
fig2 = plt.figure()
ax2 = fig2.add_subplot(111)
ax2.bar(x_data, y_data,width=0.00001)
#set the bbox for the text. Increase txt_width for wider text.
txt_height = 0.04*(plt.ylim()[1] - plt.ylim()[0])
txt_width = 0.02*(plt.xlim()[1] - plt.xlim()[0])
#Get the corrected text positions, then write the text.
text_positions = get_text_positions(x_data, y_data, txt_width, txt_height)
text_plotter(x_data, y_data, text_positions, ax2, txt_width, txt_height)

plt.ylim(0,max(text_positions)+2*txt_height)
plt.xlim(-0.1,1.1)

#BAD PLOT:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(x_data, y_data, width=0.0001)
#write the text:
for x,y in zip(x_data, y_data):
    ax.text(x - txt_width, 1.01*y, '%d'%int(y),rotation=0)
plt.ylim(0,max(text_positions)+2*txt_height)
plt.xlim(-0.1,1.1)

plt.show()

rawDescription: Matplotlib overlapping annotations

description: Matplotlib overlapping annotations


code:
matrix = 3*[3*[0]]
matrix

rawDescription: standard python library to construct matrix

description: Standard python library to construct matrix


code:
from functools import wraps

def positive(x):
    'must be positive'
    return x > 0

def negative(x):
    'must be negative'
    return x < 0

def ensure(func):
    'Decorator that enforces contracts on function arguments (if present)'
    return_check = func.__annotations__.get('return',None)
    arg_checks = [(name,func.__annotations__.get(name))
                   for name in func.__code__.co_varnames]

    @wraps(func)
    def assert_call(*args):
        for (name,check),value in zip(arg_checks,args):
            if check:
                assert check(value),"%s %s" % (name, check.__doc__)
        result = func(*args)
        if return_check:
            assert return_check(result),"return %s" % (return_check.__doc__)
        return result
    return assert_call

# Example use
@ensure
def foo(a:positive, b:negative) -> positive:
    return a-b

rawDescription: Examples of open-source projects using Python 3 function annotations


code:
def pointDistFunc(p1, p2):
    return sqrt((p1.x-p2.x)**2 + (p1.y-p2.y)**2)

rawDescription: Python Effecient Wrappers

description: Python Effecient Wrappers


code: Months = [x.strftime("%B") for x in dates]

rawDescription: getting day of years for each month in python

description: Getting day of years for each month in python


rawDescription: Index duplicates in a multidimensional array


code: {'resource': {'DisplayName': 'VM', 'type': 'vm', 'name': 'charlie', 'baseHost': 'tango'}, 'DisplayName': 'Host', 'type': 'h', 'name': 'tango'}

rawDescription: XML Parsing issue in python using xml.etree.ElementTree


code: M = np.column_stack((x**2, x, np.ones_like(x)))

rawDescription: numpy.polyfit with adapted parameters

description: Numpy.polyfit with adapted parameters


code:
urlpatterns = patterns('whatever.views',
    url(r'^browser/$', 'theview', name="browser"),
    url(r'^browser/(?P<category_id>[0-9]+)/$', 'theview', name="browser"),
    url(r'^browser/(?P<category_id>[0-9]+)/(?P<post_id>[0-9]+)/$', 'theview', name="browser"),
)

rawDescription: Multiple Django Model Queries Changing Independently in One View


rawDescription: Selecting List Elements Periodically in Python 3


code:
userList = UserDetails[0].split(',')     # turn your comma separated string into a list
userList[0] = usernamechange             # assuming the user name is the first change the user name
UserDetails[0] = ','.join(userList)      # turn the list back into a comma separated string and place it back in UserDetails

rawDescription: Editing a string in an Array

description: Editing a string in an Array


code:
class AnotherGUI(QtGui.QMainWindow):
    def __init__(self):
        super(AnotherGUI, self).__init__()
        self.notepad = Notepad(self)
        self.notepad.setWindowFlags(
            self.notepad.windowFlags() & ~QtCore.Qt.Window)
        self.button = QtGui.QPushButton('Submit', self)
        ...

rawDescription: embedding a text editor into a widget

description: Embedding a text editor into a widget


rawDescription: faster way to append value

description: Faster way to append value


code: Counter({'doleful': 1, 'curious': 1, 'delightfully': 1, 'careful': 1, 'uncommon': 1, 'mean': 1, 'perilous': 1, 'fearless': 1, 'imperial': 1, 'christian': 1, 'trustworthy': 1, 'untoward': 1, 'maddens': 1, 'true': 1, 'contemptible': 1, 'subtly': 1, 'wise': 1, 'lamentable': 1, 'tyrannical': 1, 'puzzled': 1, 'vexatious': 1, 'part': 1, 'gamesome': 1, 'determined': 1, 'reliable': 1, 'lazy': 1, 'passing': 1, 'modifies': 1, 'few': 1, 'horrible': 1, 'candid': 1, 'exasperate': 1, 'pitiable': 1, 'abundant': 1, 'mystifying': 1, 'mouldy': 1, 'loving': 1, 'domineering': 1, 'impalpable': 1, 'singular': 1})

rawDescription: The similar method from the nltk module produces different results on different machines. Why?


code:
p = "\((?:\S*,\s+)?(\S*)\)"
s = "The cert pegol (Cmzia, CZP) some words (6-mp) and (5-ASA)"
re.findall(p,s)

rawDescription: Python: Omitting Content inside Capturing Regexes


code:
cons = (
        {'type': 'ineq', 'fun': lambda x:  int(dfA.max()) - x[0] - x[1]},   # sum of b and p not greater than max amount of dfA
        {'type': 'ineq', 'fun': lambda x:  x[0]},                           # x[0] 0 or higher
        {'type': 'ineq', 'fun': lambda x:  x[1]},                           # x[1] 0 or higher
        {'type': 'ineq', 'fun': lambda x:  100 - x[0]},                           # x[0] < 100
        {'type': 'ineq', 'fun': lambda x:  100 - x[1]},                           # x[1] < 100
        )

rawDescription: Using a pandas dataframe with timeseries data as basis for optimization with SciPy


rawDescription: Reset List When Condition Met

description: Reset List When Condition Met


code:
seq = iter([None, 1, 2, 3, None])        #  [None, 1, 2, 3, None]
                                         # next^
all(x is None for x in seq)
                                         #        next^
any(x is None for x in seq)
                                         #                    next^ (off the end)
return all(x is None for x in seq)       # all returns True for the empty sequence

rawDescription: Pythonic way to determine whether not null list entries are 'continuous'


code:
lB = []
for i in range(len(l)):
    lStuffAbove = l[0:i+1]
    lB.append(sum(lStuffAbove))

rawDescription: Sum up previous values of a column

description: Sum up previous values of a column


code:
class HeaderStruct(Structure):
    _fields_ = [("param1", EBoolean),
                ("param2", c_uint)]

rawDescription: How to define C-Enumeration types in python

description: Define C-Enumeration types in python


code: fft(ifft(uhat)) == n*uhat  and  ifft(fft(u))==n*u

rawDescription: Error in RK4 algorithm in Python

description: Error in RK4 algorithm in Python


code:
from ..Level1 import t_Generic
from t_Generic import A as GenericA
from t_Generic import B as GenericB
from t_Generic import C as GenericC

A = GenericA # Just re-export the generic A class
B = GenericB # I'll subclass when I know what I want to do to it

#I really want to inherit from C and change it's behavior
class C(GenericC):
    def changeHello(self):
        self.hello = "Hello! I am Mr.C modified in t_Custom"

__all__ = ["A","B","C"]

rawDescription: pythonic way to customize classes

description: Pythonic way to customize classes


rawDescription: Removing characters from a tuple

description: Removing characters from a tuple


code:
solutions = []
for f in f_array:
    ces = odeint(rhs, init_cond, t, args=(f,))
    solutions.append(ces)

rawDescription: Solve a ODE for arrays of parameters (Python)


code:
if line in my_dictionary.keys():
    delimiter = my_dictionary[line]

rawDescription: Text file to csv with glob. Need to change delimiter depending on section of file being read


rawDescription: Querying MongoDB (via pymongo) in case insensitive efficiently


code:
import lib
lib.somthing_from_lib7

rawDescription: Python : Import modules once then share with several files


code:
df = df.sort(columns='number_of_purchase', ascending=False)
df

rawDescription: Python, use "order by" inside a "group concat" with pandas DataFrame


code:
class Child(Base):
  __tablename__ = "children"
  dbid = Column(Integer, Sequence("child_seq"), primary_key=True)
  parent_dbid = Column(Integer, ForeignKey("parents.dbid"))

rawDescription: SQLAlchemy retrieve one-to-many entities by subquery


code:
def main():
    total_circles = input("Enter the number of circles: ")
    circles = []
    for number in range(1, int(total_circles) + 1):
        circles.append(float(input("Enter the radius of circle {}: ".format(number))))
    print(circles)

rawDescription: How can you store user input within a loop without overwriting?

description: Store user input within a loop without overwriting


rawDescription: Mutable object is not changed by a function. How to debug it?


code:
pprint(65)
pprint(1)
pprint(60)
pprint(61)

rawDescription: What is the best way to write a function that converts an integer of minutes into a string formatted in "X hour(s) Y minute(s)"?


code:
import subprocess
data = ["list", "of", "stuff"]
proc = subprocess.Popen(["sql", "-c", "COPY table FROM stdin"], stdin=subprocess.PIPE)
proc.communicate("\n".join(data))

rawDescription: using a python list as input for linux command that uses stdin as input

description: Using a python list as input for linux command that uses stdin as input


code:
def show(*args, **kw):
    """
    Display a figure.

    When running in ipython with its pylab mode, display all
    figures and return to the ipython prompt.

    In non-interactive mode, display all figures and block until
    the figures have been closed; in interactive mode it has no
    effect unless figures were created prior to a change from
    non-interactive to interactive mode (not recommended).  In
    that case it displays the figures but does not block.

    A single experimental keyword argument, *block*, may be
    set to True or False to override the blocking behavior
    described above.
    """

rawDescription: in NetworkX cannot save a graph as jpg or png file

description: In NetworkX cannot save a graph as jpg or png file


code:
Paul
James
Victor
Jane

rawDescription: Python 2.7 print strings

description: Python 2.7 print strings


code: file_contents = fh.read().replace('\u2028',' ').splitlines()

rawDescription: How to omit \u2028 line separator in Python?

description: Omit \u2028 line separator in Python


code:
import json
list_to_send = []
listx=[1,2,3]
listy=[4,5,6]
list_to_send.append(listx)
list_to_send.append(listy)
json.dumps(list_to_send)
msg = json.dumps(list_to_send)
sendOverSocket(msg)

rawDescription: Sending multiple lists (list of lists) over a network


rawDescription: append list of values to sublists

description: Append list of values to sublists


code:
sql = "SELECT ... WHERE 1"
parameters = []
if name_input != "":
    sql += " AND name = ?"
    parameters += [name_input]
if id_input != "":
    sql += " AND id = ?"
    parameters += [id_input]
...

rawDescription: Python sqlite3 build where part with dynamic placeholders


code:
from html.parser import HTMLParser

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.strict = False
        self.convert_charrefs= True
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

rawDescription: Strip HTML from strings in Python

description: Strip HTML from strings in Python


code: finstr = y * x

rawDescription: How do you get x amounts of y in one string and put it in another string in python?

description: Get x amounts of y in one string and put it in another string in python


code:
l = MagicList()
l
isinstance(l, list)
bool(l)

rawDescription: Python, how to implement a list subclass that evaluates as False if its items evaluate False?


code:
from scrapy.selector import Selector
selector = Selector(response)
selector.css('td:contains("Phone") + td').re(r'(\d[\d ]+\d)')

rawDescription: XPATH: If there is element with certain value assume "phone" then get it's sibling value


rawDescription: Make row value into column and sum for DataFrame


code: np.abs(A[:,np.newaxis] - B)

rawDescription: Populate numpy matrix from the difference of two vectors


rawDescription: Django Memcache : Compare and Set

description: Django Memcache : Compare and Set


code:
filled_df = filled_df.drop('patient_id', axis=1)
filled_df = filled_df.reset_index()
filled_df = filled_df.set_index('visit_date')

final_df = filled_df.groupby(filled_df.index).size()

rawDescription: Counting the number of concurrent entities in a panel data set in pandas


code:
def min_distance(point, lines):
    return lines.distance(point).min()

df_points['min_dist_to_lines'] = df_points.geometry.apply(min_distance, df_lines)

rawDescription: Calculate Distance to Nearest Feature with Geopandas


code:
def _match_abbrev(s, wordmap):
    """_match_abbrev(s : string, wordmap : {string : Option}) -> string

    Return the string key in 'wordmap' for which 's' is an unambiguous
    abbreviation.  If 's' is found to be ambiguous or doesn't match any of
    'words', raise BadOptionError.
    """
    # Is there an exact match?
    if s in wordmap:
        return s
    else:
        # Isolate all words with s as a prefix.
        possibilities = [word for word in wordmap.keys()
                         if word.startswith(s)]
        # No exact match, so there had better be just one possibility.
        if len(possibilities) == 1:
            return possibilities[0]
        elif not possibilities:
            raise BadOptionError(s)
        else:
            # More than one possible completion: ambiguous prefix.
            possibilities.sort()
            raise AmbiguousOptionError(s, possibilities)

rawDescription: optparse - why the last char of the option can be ignored? With `--file` it behaves same as `--fil`

description: Optparse - why the last char of the option can be ignored? With `--file` it behaves same as `--fil`


code:
import requests

url_t = "http://localhost:8000/records/%i"

def process_id(id):
    """process a single ID"""
    # fetch the data
    r = requests.get(url_t % id)
    # parse the JSON reply
    data = r.json()
    # and update some data with PUT
    requests.put(url_t % id, data=data)
    return data

rawDescription: Making multiple API calls in parallel using Python (IPython)


code:
from somewhere import something

@app.context_processor
def inject_things():
    return {'something': something}

rawDescription: Get a reference to a list in a python module and iterate over it


code: PyObject_CallObject(PyObject* function, PyObject* arguments, PyObject* keywords)

rawDescription: How does CPython match variables to values?


code:
seen = set(string2)

''.join([c for c in string1 if c in seen])

rawDescription: Characters in Strings in Python

description: Characters in Strings in Python


code:
freq = 261.6

sin(2*math.pi* freq *t)

rawDescription: Placeholder to set different frequencies of sin wave output to each key press


rawDescription: Overhead in networkx reverse function?

description: Overhead in networkx reverse function


code: funcs["add_3"] = partial(myadd, 3)

rawDescription: Function in Class __init__ Dict | Functions Evaluated on Call of Class


code:
@register.assignment_tag
def getgenre():
    genre = ["Test1", "Test2"]
    return genre

rawDescription: Django custom template-tag not rendered in loop


code:
res = requests.post(URL, formdata, headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36',
    'Referer':URL,
})

rawDescription: How to start a query from a static website?

description: Start a query from a static website


code: u = Union(a, b)

rawDescription: Is there code out there to subclass set in Python for big xranges?


rawDescription: Filter pandas dataframe based on values in multiple columns


code:
from django.shortcuts import get_object_or_404
from myapp.models import PurchaseOrder

def confirm(request, itemnum):
    item = get_object_or_404(PurchaseOrder, item_number=itemnum)
    print("Got item #: {0.item_number}".format(item)) # a test
    return HttpResponse('test successful')

rawDescription: Importing a class' primary key in to another file's definition using Django


code:
def my_view(request):
   if not request.user.is_authenticated():
        return redirect('/login/')

rawDescription: Android app front-end with Django back-end auto login session


code:
def f(number, length):
    s = '{:0{}x}'.format(number, length*2)
    return ''.join(s[i-2:i] for i in range(len(s), 0, -2))

for i in range(20):
    print(f(i, 6))

rawDescription: How to print little endian hex number of varying sizes?

description: Print little endian hex number of varying sizes


code: matches = [s for s in mytext if s.startswith('foobar')]

rawDescription: Pythonic way of searching for a substring in a list


code: [[[ [0,0,0,0] for _ in range(4)] for _ in range(15)] for _ in range(5)]

rawDescription: Setting a Value in a Nested Python List Automatically Iterates


rawDescription: Numpy using where with varying array sizes


code:
class Object(object):
    def __init__(self, real_obj):
        self.real_obj = real_obj

    def __getattr__(self, name):
        return getattr(self.real_obj, name)

rawDescription: See what method was called on an object

description: See what method was called on an object


rawDescription: Python support sorted dictionary -- similar to C++ map?


rawDescription: Python: unpacking string of integers to complex numbers


rawDescription: Hashing in Python dictionaries

description: Hashing in Python dictionaries


code:
user_scores = [] # stores in this format[[id, game1, game2]]
while i< number:
    user_input = map(int, raw_input().split())
    user_scores.append(user_input)
    i += 1

rawDescription: User inputs values; need to store them in array without numpy and sort based on columns-Python


rawDescription: How to change a string to hex

description: Change a string to hex


rawDescription: Fastest way to replace elements of a file's path and extension?


code:
u = [[math.sin(i) + math.cos(j) for j in range(40)] for i in range(1000)]
y = [[math.sin(i) + math.cos(j) for j in range(40)] for i in range(1000)]

u_np = np.array(u)
y_np = np.array(y)

rawDescription: Numba : cell vars are not supported

description: Numba : cell vars are not supported


rawDescription: Setting yahoo finance date as dataframe index


rawDescription: What is subplot_adjust() doing to pyplot axes?


code:
class Investor(Profile):
  ROLE = (
    ('AN', 'Angel Investor'),
    ('VC', 'Venture Capital'),
    ('SC', 'Seed Capital')
  )
  role = models.CharField(max_length=2, default='AN', choices=ROLE)
  min_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2,
    verbose_name='Minimum Investments per year')
  max_inv = models.DecimalField(
    default=0, max_digits=20, decimal_places=2,
    verbose_name='Maximum investments per year')
  no_startups = models.IntegerField(
    default=0, verbose_name='Number of investments per year')
  rating_sum = models.FloatField(default=0)
  no_raters = models.IntegerField(default=0)
  categories = models.ManyToMany(Category)
  stages = models.ManyToMany(Stage)
  fundings = models.ManyToMany(Funding)

rawDescription: Adding multiple tags to model

description: Adding multiple tags to model


code:
current = pd.DataFrame({'starts': starts, 'ends': -ends}, p)
current
current.fillna(0)

rawDescription: Create a Pandas dataframe with counts of items spanning a date range


code:
df = pd.DataFrame([['A','B'],['C','D'],['E','F']] , columns=['Sheet','row'])
df
df['title'] = df.apply(lambda row: "Sheet{}_row{}".format(row['Sheet'],row['row']) , axis=1)
df

rawDescription: New column from previous columns in Pandas


rawDescription: How to to get the pid of a daemon created by a double fork?

description: To get the pid of a daemon created by a double fork


code: ll = [get_value(x) for x in (1, 2, 3)]

rawDescription: Working with tuples in python?

description: Working with tuples in python


code:
import string

VALID_NAME_CHARACTERS = string.lowercase + string.uppercase + string.digits
PLACEHOLDER = "_"

def to_name(s):
    if s[0] in string.digits:
        s = PLACEHOLDER + s[1:]
    return ''.join(c.lower() if c in VALID_NAME_CHARACTERS else PLACEHOLDER for c in s)

rawDescription: Arbitrary string to valid Python name

description: Arbitrary string to valid Python name


code:
import contextlib
with contextlib.nested(open('input.csv', 'rb'), open('output.csv', 'wb')) as (inf, outf):
    reader = ...

rawDescription: Strip all trailing occurrences of a character


rawDescription: Does KNeighborsClassifier compare lists with different sizes?


code:
import pylab as pl

palette = pl.array([[180, 0 , 0], [255, 150, 0], [255, 200, 0], [0, 128, 0]])

img = pl.imread('lena.jpg')[:, :, :3]
pl.clf()
pl.subplot(1, 2, 1)
pl.imshow(img)

IMG = img.reshape((512, 512, 3, 1))
PAL = palette.transpose().reshape((1, 1, 3, -1))
idx = pl.argmin(pl.sum((IMG - PAL)**2, axis=2), axis=2)
img = palette[idx, :]
pl.subplot(1, 2, 2)
pl.imshow(img)

rawDescription: Working on multidimensional arrays

description: Working on multidimensional arrays


rawDescription: Pythonic way to eval all octal values in a string as integers


code: from rpy import *

rawDescription: Embed R code in python

description: Embed R code in python


code:
label_to_number = {label: i for i, label in enumerate(set(labels), 1)}
[(label_to_number[label], label) for label in labels]

rawDescription: Python: Enumerate a list of string 'keys' into ints


code:
number = request.form.get('widget_number')
try:
    number = int(number)
except ValueError:
    abort(404)
policy = request.form.get('widget_policy')
if policy not in ('foo', 'bar'):
    abort(404)

form = UpdateWidgetForm(widget_number=number, widget_policy=policy)

rawDescription: Multiple WTForm fields from same form class in HTML table?


code:
import string

def allsimilar(word, valid):
  wl = list(word)
  for i, c in enumerate(wl):
    for x in string.ascii_lowercase:
      if x == c: continue
      wl[i] = x
      nw = ''.join(wl)
      if valid(nw): yield nw
    wl[i] = c

rawDescription: word ladder in python

description: Word ladder in python


code:
datain = (
    (1,'Root',1,16,-1),
    (2,'Articles',2,11,1),
    (5,'Fiction',3,8,2),
    (7,'Fantasy',4,5,5),
    (8,'Sci-fi',6,7,5),
    (6,'Reference',9,10,2),
    (3,'Portfolio',12,13,1),
    (4,'Contact',14,15,1),
    )

def convert_to_json(data):
    node_index = dict()
    parent_index = dict()
    for node in data:
        node_index[node[0]] = node
        parent_index.setdefault(node[4],[]).append(node)

    def process_node(index):
        result = { 'data' : node_index[index][1] }
        for node in parent_index.get(index,[]):
            result.setdefault('children',[]).append(process_node(node[0]))
        return result

    node = process_node(1)
    return [node]

rawDescription: Modified Preorder Tree Traversal to Python structure


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

rawDescription: XOR on two lists in Python

description: XOR on two lists in Python


code:
#...... your code

# adjustment so the origin is aligned width left group2
ax2.set_ylim((0, 2.7))
align_yaxis(ax, np.mean(grp2), ax2, 0)
plt.show()

rawDescription: Matplotlib: align origin of right axis with specific left axis value


code:
import clr
clr.AddReference('StdLib') #from the compilation of non-package std libraries

clr.AddReference('logging')
import logging

rawDescription: Compiling IronPython into an exe that uses standard library packages


code: FILE = open('report.pdf', "w")

rawDescription: Write PDF file from URL using urllib2

description: Write PDF file from URL using urllib2


code:
import numpy as np
import pandas as pd

bin_edges = np.linspace(low, high, nbins + 1)
total = np.zeros(nbins, np.uint)

# iterate over your dataset in chunks of 1000 lines (increase or decrease this
# according to how much you can hold in memory)
for chunk in pd.read_table('/path/to/my/dataset.txt', header=None, chunksize=1000):

    # compute bin counts over the 3rd column
    subtotal, e = np.histogram(chunk.iloc[:, 2], bins=bin_edges)

    # accumulate bin counts over chunks
    total += subtotal.astype(np.uint)

rawDescription: How to build (or precompute) a histogram from a file too large for memory?

description: Build (or precompute) a histogram from a file too large for memory


code:
data.loc[data.Lang.str.match(pat[0]),'Type'] = 'B'
data.loc[data.Lang.str.match(pat[1]),'Type'] = 'A'
data
data.fillna('')

rawDescription: Python iterating through pandas more efficiently without for loop


code: df['Timestamp'].apply(lambda x: dt.datetime.strptime(x, '%Y%m%d %H:%M:%S.%f'))

rawDescription: Python Reading in Tick Data Pandas

description: Python Reading in Tick Data Pandas


code:
from pprint import pprint
mydict = {'a':1, 'b':2, 'c':3, 'd':4, 'e':5}
pprint(mydict)
d = dict(zip("kjihgfedcba",range(11)))
pprint(d)

rawDescription: Readably print out a python dict() sorted by key


code:
nested_tables = soup.select('table.main table.main')
tables = [t for t in soup.select('table.main') if t not in nested_tables]

rawDescription: Select tag which are not already selected as descendants of the same elements


code:
a = [[0] * (rows)] * (cols)
[id(x) for x in a]

rawDescription: Python two dimensional list add value to element


code: hex_string = hex(struct.unpack('<I', struct.pack('>I', int(val, 16)))[0])

rawDescription: Print Decimal value from long string of little endian hex values


code:
import re
f = open('a.txt','r')
a = f.read()
f.close()
a_new = open('a_new.txt','w')
a_new.write('\n'.join(re.findall('(^.*ACC.*$)',a,re.M)))
a_new.close()
f = open('b.txt','r')
b = f.read()
f.close()
with open('b_new.txt','w') as b_new,open('a_new.txt','r') as a_new:
    inds = [x.replace(' ','') for x in re.findall('^\s*(\d\s*\d\s*\d)',a_new.read(),re.M)]
    for ind in inds:
        reg = '(#indexes:\s*{0}\s*{1}\s*{2}[\s\S]*?(?=#indexes|$))'.format(*list(ind))
        matches = re.findall(reg,b)
        b_new.write('\n'.join(matches))

rawDescription: Reading different text file and exctract same index lines


rawDescription: Multiply Values of two dictionaries, while appending the Keys in Python


code:
lis = [('one', 'a'), ('two', 'b'), ('three', 'a')]
seen = set()
[item for item in lis if item[1] not in seen and not seen.add(item[1])]

rawDescription: Get Unique Tuples from List , Python

description: Get Unique Tuples from List , Python


code:
def kl(line,pat = pat):
    return time.mktime(time.strptime(line.split('\t')[-2],'%d/%m/%y %H:%M:%S'))

rawDescription: How to merge only the unique lines from file_a to file_b?

description: Merge only the unique lines from file_a to file_b


code:
def original_app(masked_img):
    meanblue = 0
    meangreen = 0
    meanred = 0
    count = 0
    H, W, b = masked_img.shape
    for i in range(0,H,1):
       for j in range(0,W,1):
        if masked_img[i,j,0] > 1 or masked_img[i,j,1] > 1 or masked_img[i,j,2] > 1:
            meanblue = meanblue + masked_img[i,j,0]
            meangreen = meangreen + masked_img[i,j,1]
            meanred = meanred + masked_img[i,j,2]
            count = count+1
    return [meanblue, meangreen, meanred], count

def vectorized_app1(masked_img):
    mask = (masked_img > 1).any(2)
    count = mask.sum()
    mean_bgr = (masked_img*(mask[...,None])).sum(axis=(0,1))
    return mean_bgr, count

def vectorized_app2(masked_img):
    mask = (masked_img > 1).any(2)
    count = mask.sum()
    mean_bgr = np.einsum('ijk,ij->k',masked_img.astype(int),mask)
    return mean_bgr, count

rawDescription: Python nested for loop in RGB average

description: Python nested for loop in RGB average


code:
def help_wrapper(func):
    def inner(*args):
        results = func(*args)
        return your_cleanup_function_here(results)
help = help_wrapper(help)

rawDescription: restructuredText, docstring and python interactive shell

description: RestructuredText, docstring and python interactive shell


code:
def make_recursive_func(coefficients, baseval):
    def f(n):
        if n < len(coefficients): return baseval[n]
        return sum(coefficient * f(n-i-1) for i, coefficient in enumerate(coefficients))
    return f

rawDescription: Is there a way to write a Python script that creates and executes code?


code: out = np.dot(arr_one,arr_two.T)

rawDescription: Computing the correlation coefficient between two multi-dimensional arrays


code: import io, optparse, tarfile

rawDescription: Get Python without (selected) batteries

description: Get Python without (selected) batteries


code: ['TABITHA', 'OLLIE', 'JAIME', 'WINIFRED']

rawDescription: Importing .txt data in a particular format


code:
[u'LADY MAKFI, showed vastly improved form to shed her maiden tag on this seasonal debut for a new yard. The filly offered little for Tony Martin last year, but did show some ability on her debut and is evidently capable when fresh. She saw it out well and it\xc2\xb4ll be interesting to see how she copes with a rise.',
 'Weardiditallgorong, went down fighting over this longer trip and probably improved again on her last-time-out second at Bath. This was her best effort yet on the AW.',
 'Chauvelin, in second-time blinkers, turned in his most encouraging effort for some time and is certainly well treated on his best form.',
 'Happy Jack, not for the first time travelled easily until making heavy weather of it when asked for his effort. [David Orton]']

rawDescription: Python: pulling bold text and the text that follows


code:
while True:
    y = producer()
    print(y)
    y = producer()
    # ignore this value

rawDescription: What is offered by coroutines in python that improve a naive consumer/producer setup?


code:
from datetime import datetime, timezone

t = datetime(2015, 2, 1, 15, 16, 17, 345, tzinfo=timezone.utc)

rawDescription: How do I construct a UTC `datetime` object in Python?

description: Construct a UTC `datetime` object in Python


code:
from random import randint

guessesTaken = 0
randomNumber = [str(randint(1, 9))  for _ in range(4)] # create list of random nums

while guessesTaken < 10:
    guesses = list(raw_input("Guess Number: ")) # create list of four digits
    check = "".join(["Y" if a==b else "N" for a,b in zip(guesses,randomNumber)])
    if check == "YYYY": # if check has four Y's we have a correct guess
        print("Congratulations, you are correct")
        break
    else:
        guessesTaken += 1 # else increment guess count and ask again
        print(check)

rawDescription: Python multiple number guessing game

description: Python multiple number guessing game


code:
if node.attributes['type'] == 'command':
    value = os.popen(node.firstChild.text).read()
elif node.attributes['type'] == 'string':
    value = node.firstChild.text
os.environ[node.attributes['name']] = value

rawDescription: Python, change current shell environment variables while using Popen?


code: ''.join(s for s in string if ord(s)>31 and ord(s)<126)

rawDescription: Remove formatting from strings

description: Remove formatting from strings


code:
s=3
eval('s==3')
eval('s + 1')
eval('s')
eval('str(s)+"test"')

rawDescription: eval SyntaxError: invalid syntax in python

description: Eval SyntaxError: invalid syntax in python


rawDescription: Easiest way to get a specific section of a file path


code:
import itertools as it
a = [1, 2, 3, 4, 5]
list(it.accumulate(a))

rawDescription: Discrete Integration if List of Values

description: Discrete Integration if List of Values


code:
class Z(X):
    z = 1

z = Z
z.test_x()

rawDescription: unbound method when using type builtin to create a dynamic object

description: Unbound method when using type builtin to create a dynamic object


rawDescription: PyQt4 application: buttons suddenly no longer works


code:
a="# -- coding: utf-8 --"
a= map(str.strip,a.split())
a
a = " ".join(sorted(set(a),key=lambda x:a.index(x)))
a

rawDescription: Remove duplicated string(s) in strings in a list


code:
class decorated_by_class(object):
    def __init__(self, *some_decorator_args):
        self.args = some_decorator_args

    def compute_something(self):
        return self.args

    def __call__(self, cls):
        # do something with the class
        cls.decorated_with_args = self.compute_something()
        return cls

@decorated_by_class("hello")
class C(object):
    pass

print(C.decorated_with_args)    # => ('hello',)

class decorated_by_subclass(decorated_by_class):
    def compute_something(self):
        return self.args * 2

@decorated_by_subclass("hello")
class D(object):
    pass

print(D.decorated_with_args)    # => ('hello', 'hello')

rawDescription: Subclass from a class decorated by a class


code:
class MyUsefulClass(object):

    a = ValidatedAttribute(int)

rawDescription: Using Python descriptors: How best to write a general validation getter-setter


code: cache

rawDescription: Combine dictionary

description: Combine dictionary


rawDescription: Splitting a url into a list in python

description: Splitting a url into a list in python


code: 'Hello world!'

rawDescription: Adding spaces on word boundaries in text extraction with lxml


code:
"DATABASE\\names.txt" # double \
r"DATABASE\names.txt" # raw string
"DATABASE/names.txt" # use forward slashes

rawDescription: IO ERROR(ERRNO 20) while Accessing a file inside a folder in python


code:
output = []
for z,l1 in enumerate(l[::-1]):
    for y, l2 in enumerate(l1):
        for x, v in enumerate(l2.split(',')):
            output.append((x, y, z, v))

rawDescription: Python: converting a nested list into a simple list with coord positions


code:
matrix = []
numberOfRows = eval(input("Enter the number of rows: "))
numberOfColumns = eval(input("Enter number of columns: "))

rawDescription: Python 3- matrix input

description: Python 3- matrix input


code: '--bass_cut_off=0 --out=something'

rawDescription: How to replace a words near the specific word in a text file using python

description: Replace a words near the specific word in a text file using python


code: b - a

rawDescription: python time difference in second

description: Python time difference in second


rawDescription: Django dynamic model fields

description: Django dynamic model fields


code:
from distutils.core import setup

# set dependencies, we need sphinx to build doc
dependencies = ['sphinx']

setup(
    name='my_package'
    # rest of your setup
)

# auto-generate documentation
import sphinx

# automatically build html documentation
# For example:
#   format = 'html'
#   sphinx-src-dir = './doc'
#   sphinx-build-dir = './doc/build'
sphinx.build_main(['setup.py', '-b', '<format>', '<sphinx-src-dir>', '<sphinx-build-dir>'])

rawDescription: Install sphinx compilated doc from setup.py


code:
gen = ((x[0],x[1],x[2:]) for x in data)
for base, argtype, args in gen:
   print(base, argtype, args)

rawDescription: How can I easily unpack extra elements of array into single variable?

description: Easily unpack extra elements of array into single variable


rawDescription: How do I efficiently web-scrape these abnormal values?

description: Efficiently web-scrape these abnormal values


code:
from time import sleep
from flask import Flask, render_template
from math import sqrt

app = Flask(__name__)

@app.route('/')
def index():
    # render the template (below) that will use JavaScript to read the stream
    return render_template('index.html')

@app.route('/stream_sqrt')
def stream():
    def generate():
        for i in range(500):
            yield '{}\n'.format(sqrt(i))
            sleep(1)

    return app.response_class(generate(), mimetype='text/plain')

app.run()

rawDescription: Display data streamed from a Flask view as it updates


code:
import concurrent.futures
import itertools

def process_stuff(img_part):
    Process1 = Do_something(img_part)
    Process2 = Do_something(Process1)

with concurrent.futures.ProcessPoolExecutor() as executor:
    i_iterator = arange(0, shape(img)[0] - window_size[0], 10)
    j_iterator = arange(0, shape(img)[1] - window_size[1], 10)
    product = itertools.product(i_iterator, j_iterator)
    parts_generator = (img[i: i + winSize[0], j: j + winSize[1]]
                       for i, j in product)

    executor.map(process_stuff, parts_generator)

rawDescription: How can I parallelize this Python for-loop?

description: Parallelize this Python for-loop


code:
cache = {0: (), 1: ((set([1]),),)}

def bell(x):
    # Change these lines to alter memoization.
    if x in cache:
        return cache[x]
    previous = bell(x - 1)
    new = []
    for sets in previous:
        r = []
        for mark in range(len(sets)):
            l = [s | set([x]) if i == mark else s for i, s in enumerate(sets)]
            r.append(tuple(l))
        new.extend(r)
        new.append(sets + (set([x]),))
    cache[x] = tuple(new)
    return new

rawDescription: Why is my code slower in pypy than the default python interpreter?


code: from __future__ import with_statement

rawDescription: How to save an xml file to disk?

description: Save an xml file to disk


code:
from PIL import Image
from PIL import ImageFilter
im = Image.open(r'c:\temp\temp.png')
white = im.filter(ImageFilter.BLUR).filter(ImageFilter.MaxFilter(15))

rawDescription: What processing steps should I use to clean photos of line drawings?


rawDescription: New Python Gmail API - Only Retrieve Messages from Yesterday


code:
plt.xticks([])
plt.yticks([])
plt.tight_layout(pad=0.0, w_pad=0.0, h_pad=0)

rawDescription: Python Tkinter Crashing, every run but program continues? Final polishing of Tkinter


rawDescription: Arbitrary number of nested loops dependent on the previous loop in Python


code: my_dict = {'a{}'.format(i):f(j) for i,j in enumerate([3,4,5,1,2])}

rawDescription: Declaring multiple variables using the same function in Python


code: options = []

rawDescription: How to write a recursion function for all the possible parameter combinations in python

description: Write a recursion function for all the possible parameter combinations in python


code:
from selenium.webdriver.common.action_chains import ActionChains

actions = ActionChains(driver)
actions.move_to_element(size_button).perform()

rawDescription: What is the class of this javascript button for use with selenium?


code:
class Node(object):
    __slots__ = ('x', 'y')
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return '{0.__class__.__name__}({0.x!r}, {0.y!r})'.format(self)

n1 = Node(10, 20)
n2 = n1
n2
n1.x = 42
n1.y = 81
n2

rawDescription: Use _replace method of namedtuple to update the fields of other data structures containing the nt


code:
def joinadj(iterable, join=' '.join):
    adj = []
    for item in iterable:
        if isinstance(item, str):
            adj.append(item) # save for later
        else:
            if adj: # yield items accumulated so far
                yield join(adj)
                del adj[:] # remove yielded items
            yield item # not a string, yield as is

    if adj: # yield the rest
        yield join(adj)

print(list(joinadj(itertext(html.fromstring(
                "text0text1

item1
"
                "
item2

text2sib")))))

rawDescription: Python, lxml - access text

description: Python, lxml - access text


rawDescription: Group DataFrame by entries X amount of time from each other


code:
def wxVTKRenderWindowInteractorConeExample():
 """Like it says, just a simple example
 """
 # every wx app needs an app
 app = wx.PySimpleApp()

 # create the top-level frame, sizer and wxVTKRWI
 frame = wx.Frame(None, -1, "wxVTKRenderWindowInteractor", size=(400,400))
 widget = wxVTKRenderWindowInteractor(frame, -1)
 sizer = wx.BoxSizer(wx.VERTICAL)
 sizer.Add(widget, 1, wx.EXPAND)
 frame.SetSizer(sizer)
 frame.Layout()

 # It would be more correct (API-wise) to call widget.Initialize() and
 # widget.Start() here, but Initialize() calls RenderWindow.Render().
 # That Render() call will get through before we can setup the
 # RenderWindow() to render via the wxWidgets-created context; this
 # causes flashing on some platforms and downright breaks things on
 # other platforms.  Instead, we call widget.Enable().  This means
 # that the RWI::Initialized ivar is not set, but in THIS SPECIFIC CASE,
 # that doesn't matter.
 widget.Enable(1)

 widget.AddObserver("ExitEvent", lambda o,e,f=frame: f.Close())

 ren = vtk.vtkRenderer()
 widget.GetRenderWindow().AddRenderer(ren)

 cone = vtk.vtkConeSource()
 cone.SetResolution(8)

 coneMapper = vtk.vtkPolyDataMapper()
 coneMapper.SetInput(cone.GetOutput())

 coneActor = vtk.vtkActor()
 coneActor.SetMapper(coneMapper)

 ren.AddActor(coneActor)

 # show the window
 frame.Show()

 app.MainLoop()

description: VtkRenderWindowInteractor stalling program


code:
def f():
  g()
  h()

def g():
  h()
  do_something1()

def h():
  do_something2()

rawDescription: What is the difference between Lock and RLock


rawDescription: Output pandas grouped dataframe without aggregation


code:
if something == 0:
    pass
else:
    do_stuff()

rawDescription: PYTHON - Restart same for loop iteration