code:
a = {
    k.replace(u'\u2019', '@'): v.replace(u'\u2019', '@')
    for k, v in a.iteritems()
}

rawDescription: how to replace one character in a dictionary with a for loop

description: Replace one character in a dictionary with a for loop


code:
def twosum(nums=(6, 7, 11, 15, 3, 6, 5, 3), target=6):
    lookup = dict(((v, i) for i, v in enumerate(nums)))
    return next(( (i+1, lookup.get(target-v)+1)
            for i, v in enumerate(nums)
                if lookup.get(target-v, i) != i), None)

rawDescription: Two Sum on LeetCode

description: Two Sum on LeetCode


code:
import mimetypes
mimetypes.guess_type('hello.txt.gz')
mimetypes.guess_type('hello.pdf.gz')
mimetypes.guess_type('hello.pdf')

rawDescription: Django: allowing download of various media files from S3 without creating a model (while hiding s3 storage)


code:
import subprocess
process = subprocess.Popen(["yourcommand"], stdout=subprocess.PIPE)
result = process.communicate()[0]

rawDescription: How can I capture the stdout output of a child process?

description: Capture the stdout output of a child process


code: getaddresses(['info@test.org, Blahblah <blah@test.com>, <another@blah.org>, "Hey" <last@one.com>'])

rawDescription: Parse email fields

description: Parse email fields


code:
dict_a = {row: 0 for row in list_a}
for row in list_b:
    if row in dict_a:
        dict_a[row] += 1

result = [row + (dict_a[row],) for row in list_a]

rawDescription: Comparing Python nested lists and count duplicates


code:
def resize_and_crop(im, mask_width=1000, mask_height=1000):
    width, height = im.size
    aspect = 1.0*width/height
    mask_aspect = 1.0*mask_width/mask_height
    if width != mask_width or height != mask_height:
        if aspect > mask_aspect:
            ratio = 1.0*height/mask_height
            imt = im.transform((mask_width, mask_height),
                                Image.AFFINE,
                               (ratio, 0, (width-mask_width*ratio)/2, 0, ratio, 0),
                               Image.CUBIC)
        else:
            ratio = 1.0*width/mask_width
            imt = im.transform((mask_width, mask_height),
                               Image.AFFINE,
                               (ratio, 0, 0, 0, ratio, (height-mask_height*ratio)/2),
                               Image.CUBIC)
    else:
        imt = im
    return imt

rawDescription: django-photologue: How to scale image to box and fill background

description: Django-photologue: How to scale image to box and fill background


code:
import csv
import sys

if sys.hexversion >= 0x3000000:
    inp = input
else:
    inp = raw_input

def main():
    input_file  = inp('Name of bank statement file? ')
    output_file = inp('Save YNAB output file as? ')

    with open(input_file, 'rb') as inf, open(output_file, 'wb') as outf:
        incsv  = csv.reader(inf)
        outcsv = csv.write(outf)
        # deal with headers
        header = next(incsv)
        outcsv.write(['Date', 'Payee', 'Category', 'Memo', 'Outflow', 'Inflow'])
        # translate data
        for row in incsv:
            cad = float(row[6])
            ynab = [row[2], row[4], '', row[5], -cad if cad < 0. else '', cad if cad > 0. else '']
            outcsv.writerow(ynab)

if __name__=="__main__":
    main()

rawDescription: Restructing a CSV file?

description: Restructing a CSV file


rawDescription: Sum all values of a counter in Python

description: Sum all values of a counter in Python


code:
def enumerate_month_dates(start_date, end_date):
    current = start_date
    while current <= end_date:
        if current.month >= 12:
            next = datetime.date(current.year + 1, 1, 1)
        else:
            next = datetime.date(current.year, current.month + 1, 1)
        last = min(next - datetime.timedelta(1), end_date)
        yield current, last
        current = next

rawDescription: loop through python months since start time

description: Loop through python months since start time


code:
action = menu.addAction(linktype)
...
action.trigger();

rawDescription: Simulate a triggered(QAction *) in a menu


code:
import operator

def your_view(self, request, *args, **kwargs):
    # Here you list all your filter names
    filter_names = ('filter_one', 'filter_two', 'another_one', )

    queryset = Books.objects.all();
    filter_clauses = [Q(filter=request.GET[filter])
                      for filter in filter_names
                      if request.GET.get(filter)]
    if filter_clauses:
        queryset = queryset.filter(reduce(operator.and_, filter_clauses))

    # rest of your view

rawDescription: How to add filters to a query dynamically in Django?

description: Add filters to a query dynamically in Django


code:
# generate sample data
import pandas as pd
df = pd.DataFrame(np.random.randint(0,10,(10,3)))
df.columns = ['A','B','C']

# sort by C, group by A, take last row of each group
df.sort('C').groupby('A').nth(-1)

rawDescription: Finding the maximum entry based on another column in a data frame


code:
NO_KEY = object()

def f(ch):
    chap = {
        1: 'foo',
    }
    return chap.get(ch, NO_KEY)

chapter = f(1)
print(chapter if chapter is not NO_KEY else 'There was no chapter')

rawDescription: python function that contains a dict, takes a key and returns a value from that dict

description: Python function that contains a dict, takes a key and returns a value from that dict


code:
import re

html = """

"""

ids = re.findall(r'id="\w+_(\d+)"', html)

rawDescription: Find number based on text right before Python 2.7


code:
with open('filename', 'rb') as f:
    block = f.read(512 * 2**10)
    while block != "":
        # Do stuff with a block
        block = f.read(512 * 2**10)

rawDescription: How to write a python program to read data from disk image file (.raw file)?

description: Write a python program to read data from disk image file (.raw file)


code:
#!/usr/bin/env python
from ecmwfapi import ECMWFDataServer
server = ECMWFDataServer()
for year in range(1998, 2000):
    server.retrieve({
        "class": "e2",
        "dataset": "era20c",
        "date": '%d-07-01/%d-07-02' % (year,year),
        "domain": "g",
        "area" : "12/-72/-67/22",
        "grid" : "1.0/1.0",
        "expver": "1",
        "param": "214.140/233.140",
        "step": "3/9/15/21",
        "format" : "netcdf",
        "stream": "wave",
        "target": '/home/nicolas/hycom/hycom_data/ECMWF/ERA20C/forecast/%d/test_era20c_wave_set1.nc' % (year),
        "time": "06",
        "type": "fc",
    })

rawDescription: Python command to wait for shell (ubuntu csh) finishing action / return code


rawDescription: How to ensure unique values in a multidimensional array?

description: Ensure unique values in a multidimensional array


code:
#!/usr/bin/env python
import os
import pty
import sys

with open(dump_file_path, 'wb') as file:
    def read(fd):
        data = os.read(fd, 1024)
        file.write(data)
        file.flush()
        return data

    pty.spawn(['/bin/sh', '-c', cmd], read)

rawDescription: How to stop subprocess.Popen() write desc in terminal

description: Stop subprocess.Popen() write desc in terminal


code:
import numpy as np
from scipy.spatial.distance import cdist

# some starter data
dim = (1000,1000)
values = np.random.randint(0, 10, dim)

# cdist will want 'samples' as a 2-d array
samples = np.array([1, 2, 3]).reshape(-1, 1)

# this could be a one-liner
# 'values' must have the same number of columns as 'samples'
mins = cdist(values.reshape(-1, 1), samples)
outvalues = mins.argmin(axis=1).reshape(dim)

rawDescription: Minimum Distance Algorithm using GDAL and Python


code:
def _cam_loop(self):
    while self.cam_live:
        img = self.cam.get_image()
        cv2.imshow("cam", img)
        cv2.waitKey(1)
        cv2.destroyAllWindows()

rawDescription: OpenCV imshow window cannot be reused when called within a thread


code:
def pregen(request, elements, kw):
    kw['_app_url'] = 'http://sub.example.com'
    return elements, kw

def req_sub(info, request):
    return request.host.startswith('sub')

config.add_route('sub_only', '/',
                 custom_predicates=(req_sub,),
                 pregenerator=pregen)
config.add_route('foo', '/foo')
config.add_view(view, route_name-'foo')

def view(request):
    # redirect the user to "http://sub.example.com", regardless of whether
    # request.host is "example.com" or "sub.example.com"
    return HTTPFound(request.route_url('sub_only'))

rawDescription: Multiple domains and subdomains on a single Pyramid instance


code:
def read_poetry_form_description(poetry_forms_file):
    """ (file open for reading) -> poetry pattern

    Precondition: we have just read a poetry form name from poetry_forms_file.

    Return the next poetry pattern from poetry_forms_file.
    """
    # Create three empty lists
    syllables_list = []
    rhyme_list = []
    pattern_list = []
    # Read the first line of the pattern
    line = poetry_forms_file.readline()
    # Read until the end the the pattern
    while line != '\n' and line != '':
        # Clean the \n's
        pattern_list.append(line.replace('\n', '').split(' '))
        line = poetry_forms_file.readline()
    # Add elements to lists
    for i in pattern_list:
        syllables_list.append(int(i[0]))
        rhyme_list.append(i[1])
    # Add two lists into a tuple
    pattern = (syllables_list, rhyme_list)
    return pattern

def read_poetry_form_descriptions(poetry_forms_file):
    """ (file open for reading) -> dict of {str: poetry pattern}

    Return a dictionary of poetry form name to poetry pattern for the
    poetry forms in poetry_forms_file.
    """
    # Initiate variables
    forms_dict = {}
    keys = []
    values = []
    # Get the first line
    line = poetry_forms_file.readline()
    while line != '':
        # Check if the line is the beginning of a form
        if line != '\n':
            keys.append(line.replace('\n', ''))
            values.append(read_poetry_form_description(poetry_forms_file))
        line = poetry_forms_file.readline()
    # Add key-value pairs to the dictionary
    for i in range(len(keys)):
        forms_dict[keys[i]] = values[i]
    return forms_dict

rawDescription: Program skips reading text in Python

description: Program skips reading text in Python


code:
while number >= 0:
    if number == 0:
        print("Sum of odds: ", odd_sum)
        print("Sum of evens: ", even_sum)
        print("Odd count: ", count_odd)
        print("Even count: ", count_even)
        print("Total positive int count:", even_sum + odd_sum)
        break
    elif number%2 == 0:
        count_even+=1
        even_sum= even_sum + number
        number = int(input("Input an integer (0 terminates the program): "))
    elif number%2 != 0:
        count_odd+=1
        odd_sum = odd_sum + number
        number = int(input("Input an integer (0 terminates the program): "))

rawDescription: how to make a python program using "while" to print at 0?

description: Make a python program using "while" to print at 0


code:
def my_gen(x=None):
    while True:
        if x is None:
            x = yield X()
        else:
            x = yield f(x)

rawDescription: Can I express this as a generator / coroutine?

description: Express this as a generator / coroutine


code:
# views.py
class UserFilter(django_filters.FilterSet):
    nickname = django_filters.CharFilter(name='first_name')
    class Meta:
        model = User

rawDescription: django-filter - map query parameter to field name

description: Django-filter - map query parameter to field name


code:
is_menu = False

def menu():
    pass
    #right here you want the necessary code to maintain the main menu. be sure not to put the code that starts up the menu here, only the code required to run over and over again to keep the menu running

def change_to_menu():
    is_menu = True
    #right here you want the things to do to create a menu, as well as the above line of code that will be used to mark the fact that the menu is to be maintained, not gameplay

def gameplay():
    pass
    #here is where you put code to maintain gameplay. remember this will be called over and over again, multiple times a second

def change_to_gameplay():
    is_menu = False
    #insert the code to resume or initialize gameplay here, along with the above line. you may want to make different functions to initialize and resume gameplay, though it is not nescessary

while not salir:          #this is your mainloop
    if is_menu:
        menu()
    else:
        gameplay()

rawDescription: how to return to the main menu from the game

description: Return to the main menu from the game


code:
from django.core.mail import EmailMessage

msg = EmailMessage(
                       'Test',
                       'asdfasdfasdf
asdfasfasdfasdf
asdfasdfasdf',
                       'sender@example.com',
                       ['receiver@example.com', ]
                  )
msg.content_subtype = "html"
msg.send()

rawDescription: Python Django send_mail newlines?

description: Python Django send_mail newlines


code:
combined = {}
for x in ml_dict:
    try:
        if tb_dict[x] < ml_dict[x]: combined[x] = ml_dict[x]
    except KeyError: continue

rawDescription: Comparing Values of 2 dictionaries in Python and Constructing New dictionary From Comparison


code:
from dateutil import parser
parser.parse('2012-06-14 20:38:24.213-7:00')

rawDescription: How do I convert a string to a python datetime object with a relevant timezone?

description: Convert a string to a python datetime object with a relevant timezone


code:
class SomeClass(object):
    def __init__(self):
        self._cached_func = {}

    def call(self, **kwargs):
        function_name = '{}_{}'.format(self.parms['prefix'], self.parms['name'])

        try:
            func = self._cached_func[function_name]
        except KeyError:
            file_name =  self.parms['prefix']
            mod_name = 'PACKAGE.{}'.format(file_name)
            module = __import__(mod_name, globals(), locals(), [file_name,])
            func = getattr(module, function_name)
            self._cached_func[function_name] = func

        return func(**kwargs)

rawDescription: Is there a way to use getattr to call dynamic functions without using __import__?


code:
import win32com.client as w32c

Word = w32c.Dispatch("Word.Application")
Word.Visible=1
doc=Word.Documents.Open("C:\\docx_with_a_table.docx")
tables=doc.Tables
for t_cnt in range(tables.Count):
    table=tables[t_cnt]
    for r_cnt in range(table.Rows.Count):
        row=table.Rows[r_cnt]
        for c_cnt in range(row.Cells.Count):
            cell=row.Cells[c_cnt]
            print(cell.Range.Text)

rawDescription: extracting data from docx files in python

description: Extracting data from docx files in python


rawDescription: Extract a part of URL - python

description: Extract a part of URL - python


code:
import turtle

geoff = turtle.Turtle()

geoff.pencolor(my_color)
geoff.setpos(my_x, my_y)
geoff.circle(my_radius)

rawDescription: Python random Circle

description: Python random Circle


code:
class parent(object):
  def method(self):
    blah
    self.helper()
    blah
  def helper(self):
    print("parent running")

class child(parent):
  def helper(self):
    print("child running")

rawDescription: overwrite parent method partially in python

description: Overwrite parent method partially in python


code:
import pylab as plt
a = [[1,0,1],[1,1,1]]
plt.matshow(a)
plt.show()

rawDescription: How to make plot of m x n matrix in python

description: Make plot of m x n matrix in python


code:
AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'social_auth.backends.twitter.TwitterBackend',
    'social_auth.backends.facebook.FacebookBackend',
)

rawDescription: Unable to log in to the django admin page due to django-social-auth backends enabled


code:
url(
    r'^login/$',
    auth_views.login,
    {
        'template_name': 'admin/login.html',
        'extra_context': {'site_header': 'Log In'},
    },
    name='login'
)

rawDescription: Django - change site_header when using `admin/login.html` template as a normal login template


rawDescription: How to make axes transparent in matplotlib?

description: Make axes transparent in matplotlib


code: size_of_filtered_list = sum(1 for item in my_list if item % 2 == 0)

rawDescription: Efficiently computing size of filtered list


code:
import re

# Compile the regular expression
# r'' is to give a raw string and avoid escaping \ characters
prog = re.compile(r'\d{4}-\d{1,2}-\d{1,2}$')

#Search the regular expression in filename
res = prog.search(filename)

#This gives you the start position of the date
#(assuming all filenames end with a date)
date_start = res.start()
new_name = 'Bonus_file_%s.csv' % filename[date_start:]

rawDescription: Python: how to keep and/or modify partial file name?


rawDescription: Parse url like requets.Request.url which prepared


code:
def __hash__(self):
    return hash(frozenset(self.iteritems()))

rawDescription: Lookup table for unhashable in Python

description: Lookup table for unhashable in Python


code:
f = open("file.txt",'r+')
lines = f.readlines()
f.writelines(lines)
f.close()

rawDescription: How to open a text file in python?

description: Open a text file in python


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

from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
x = []
y = []
t = []
fig = plt.figure()
rect = fig.patch
rect.set_facecolor('#31312e')
readFile = open('C:\\Temp\\001.txt', 'r')
sepFile = readFile.read().split('\n')
readFile.close()

startTime = raw_input('please enter start time in format like 21/7/2014 0:00 :')
endTime   = raw_input('please enter end time in format like 22/7/2014 23:57 :')

# startTime = '21/7/2014 0:02'
# endTime = '22/7/2014 23:58'
startTime = datetime.strptime(startTime, '%d/%m/%Y %H:%M')
endTime = datetime.strptime(endTime, '%d/%m/%Y %H:%M')

for idx, plotPair in enumerate(sepFile):
    if plotPair in '. ':
        # skip. or space
        continue
    if idx > 1:  # to skip the first line
        xAndY = plotPair.split(',')
        time_string = xAndY[0]
        time_string1 = datetime.strptime(time_string, '%d/%m/%Y %H:%M')
        if startTime<=time_string1 <=endTime:
            t.append(time_string1)
            y.append(float(xAndY[1]))
ax1 = fig.add_subplot(1, 1, 1, axisbg='white')
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%Y %H:%M'))
ax1.plot(t, y, 'c', linewidth=3.3)
plt.title('IRRADIANCE')
plt.xlabel('TIME')
fig.autofmt_xdate(rotation=45)
fig.tight_layout()
plt.show()

rawDescription: Asking User For Datetime Before Plotting Selected Points


code:
custid_true = set(dfm[dfm['Condition']==True].CustID)
custid_false = set(dfm[dfm['Condition']==False].CustID)
custid_both = custid_true.intersection(custid_false)
n_custid_both = len(custid_both)

rawDescription: pandas: groupby two columns nunique

description: Pandas: groupby two columns nunique


code:
import os
root = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(root, 'img/open_dir.png')

rawDescription: Why MS Windows "Open with" option does not realize image path?


code:
from tkinter import *

def func(value):
    print(value)

root = Tk()
options = ["1", "2", "3"]
var = StringVar()
drop = OptionMenu(root, var, *options, command=func)
drop.place(x=10, y=10)

rawDescription: Adding command to a python Tkinter Optionmenu


code:
if self.request.get('key'):
    # Key specified, get field values from db store
    key = self.request.get('key')
    sort = db.get(key)
    for field in ItemDB.properties():
        self.formdata[field] = getattr(sort, field)

rawDescription: Iterate over GQL query fields

description: Iterate over GQL query fields


code:
df.index = pd.MultiIndex.from_tuples(df.index.map(lambda x: (x[0:2], x[2:4])))
df

rawDescription: Convert indexed DataFrame to multi-indexed using pattern in indices


code:
import time
if os.path.exists('result.png'):
    plt.savefig('result_{}.png'.format(int(time.time())))
else:
    plt.savefig('result.png')

rawDescription: How to save the file with different name and not overwriting existing one

description: Save the file with different name and not overwriting existing one


code:
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied

@receiver(pre_delete, sender=User)
def delete_user(sender, instance, **kwargs):
    if instance.is_superuser:
        raise PermissionDenied

rawDescription: How to prevent super user deletion in Django admin?

description: Prevent super user deletion in Django admin


code:
LAST_NUMBER = re.compile(r'(\d+)(?!.*\d)')

def getNext(path):
    return LAST_NUMBER.sub(lambda match: str(int(match.group(1))+1), path)

rawDescription: How to get the last number in a string and +1?

description: Get the last number in a string and +1


code:
from scipy.stats import ks_2samp
import numpy as np

np.random.seed(12345678);
x = np.random.normal(0,1,1000)
y = np.random.normal(0,1,1000)
z = np.random.normal(1.1,0.9, 1000)

ks_2samp(x, y)
ks_2samp(x, z)

rawDescription: Two-sample Kolmogorov-Smirnov Test in Python Scipy


code: tuple(int(x.strip()) for x in raw_input().split(','))

rawDescription: How to input an integer tuple from user?

description: Input an integer tuple from user


code:
def runner():
    # _check_esc searches for Escape in our queue
    def _check_esc():
        oldpos = len(queue)
        while gtk.events_pending():
            gtk.main_iteration()
        new = itertools.islice(queue, oldpos, None)
        return any(event.type == gtk.gdk.KEY_PRESS \
                       and gtk.gdk.keyval_name(event.keyval) == 'Escape'
                   for event in new)

    queue = []
    # temporarily set the global event handler to queue
    # the events
    gtk.gdk.event_handler_set(queue.append)
    try:
        code(_check_esc)
    finally:
        # restore the handler and replay the events
        handler = gtk.main_do_event
        gtk.gdk.event_handler_set(gtk.main_do_event)
        for event in queue:
            handler(event)
    gtk.main_quit()

rawDescription: Peeking a GTK event; gtk.gdk.event_peek always returns None


code:
# -*- coding: utf-8 -*-
import scrapy

# item class included here
class DmozItem(scrapy.Item):
    # define the fields for your item here like:
    link = scrapy.Field()
    attr = scrapy.Field()

class DmozSpider(scrapy.Spider):
    name = "dmoz"
    allowed_domains = ["craigslist.org"]
    start_urls = [
    "http://chicago.craigslist.org/search/emd?"
    ]

    BASE_URL = 'http://chicago.craigslist.org/'

    def parse(self, response):
        links = response.xpath('//a[@class="hdrlnk"]/@href').extract()
        for link in links:
            absolute_url = self.BASE_URL + link
            yield scrapy.Request(absolute_url, callback=self.parse_attr)

    def parse_attr(self, response):
        item = DmozItem()
        item["link"] = response.url
        item["attr"] = "".join(response.xpath("//p[@class='attrgroup']//text()").extract())
        return item

rawDescription: Make Scrapy follow links and collect data


code:
def drop(iterable,start,end):

    for i,x in enumerate(iterable):
        if x==start :
            for y in iterable[i:]:
                if y!=end and 'sub_string' in y: #or 'sub_string' not in y
                    yield y
                else:
                    #based on your problem do stuff
                    #yield end
                    break

rawDescription: Slicing a python list

description: Slicing a python list


code:
def get_num(lst):
    found = set()
    for element in lst:
        if not any(item.startswith(element[0]) for item in found):
            found.add(element)
            yield element

x = ['512','345','321','345','674','132','231','145','214','576']
list(get_num(x))

rawDescription: Find the 1st occurence of elements in a list of strings, based on first character in the string


code:
sql_insert = 'INSERT INTO "tempHoods" (nbhd_id, nbhd_name, typology, notes, geom) values '
sql_values = ['(%s, %s, %s, %s, %s)']

data_values = []
# you can make this larger if you want
# ...try experimenting to see what works best
batch_size = 100
sql_stmt = sql_insert + ','.join(sql_values*batch_size) + ';'
for i, row in enumerate(rows, 1):
    data_values += row[:5]
    if i % batch_size == 0:
        curQa.execute (sql_stmt , data_values )
        data_values = []
if (i % batch_size != 0):
    sql_stmt = sql_insert + ','.join(sql_values*(i % batch_size)) + ';'
    curQa.execute (sql_stmt , data_values )

rawDescription: How to use python to ETL between databases?

description: Use python to ETL between databases


code:
import operator

lambda map: operator.setitem(map, 'x', 'y')

rawDescription: How to perform an assignment inside a lambda function

description: Perform an assignment inside a lambda function


code:
output_path = os.path.join(os.path.expanduser("~"), 'output')
if not os.path.isdir(output_path):
    os.makedirs(output_path)
output_file = os.path.join(output_path, '{}.json'.format(facultyID))
if os.path.isfile(output_file):
    with open(output_file) as data_file:
        old = json.load(data_file)
    if old['date'] == date or old['message'] == msg:
        print('email already sent')
        return None
result = {'name': name, 'subject': subject, 'message': msg, 'date': sent}
with open(output_file, 'w') as data_file:
    json.dump(result, data_file, indent=4)
return result

rawDescription: check if a json file already has the data i am overriding in python

description: Check if a json file already has the data i am overriding in python


rawDescription: Passing list to array

description: Passing list to array


rawDescription: How to create a sliding window generator python 3.3

description: Create a sliding window generator python 3.3


code:
for x in os.walk('.', topdown=True):
    dirpath, dirnames, dirfiles = x
    print(dirpath, dirnames)
    dirnames[:] = filter(lambda x : not x.startswith('Load'), dirnames)

rawDescription: Python walker that does NOT go into specific folders


code: mapper(Something, select([sometable], sometable.c.deleted == False))

rawDescription: How to add an automatic filter to a relation with SQLAlchemy?

description: Add an automatic filter to a relation with SQLAlchemy


rawDescription: Small python quirk: Finding the index of first and last number within boundaries in a list


code:
async def main_async():
    try:
        with NamedTimeout('outer', 0.5) as t1:
            with NamedTimeout('inner', 0.3) as t2:
                await asyncio.sleep(2)
    except asyncio.TimeoutError as e:
        print(e.timeout_name)

class NamedTimeout(Timeout):
    def __init__(self, name, timeout, *, loop=None):
        super().__init__(timeout, loop=loop)
        self.name = name

    def __exit__(self, exc_type, exc_val, exc_tb):
        try:
            super().__exit__(exc_type, exc_val, exc_tb)
        except asyncio.TimeoutError as e:
            e.timeout_name = self.name
            raise

rawDescription: Is there any way to find which one of multiple timers raised exception?


code:
import wx
import wx.html2

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Scroll", size=(500,500))
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self._on_timer, self.timer)
        self.wv=wx.html2.WebView.New(self)
        self.wv.SetPage(content, "")
        self.timer.Start(100) # adjust the timer interval
    def _on_timer(self, event):
        self.wv.Find("Some string in the text")

app = wx.App()
frame = MyFrame()
frame.Show()
app.MainLoop()

rawDescription: wxpython, to scroll a webView

description: Wxpython, to scroll a webView


code:
restype = ctypes.c_int
argtypes = [ctypes.c_int, ctypes.c_double]        # or whatever
functype = ctypes.CFUNCTYPE(restype, *argtypes)
wrapper = functype(address_of_static_function_as_an_int)

rawDescription: Can I use ctypes to call back C function from python embedded in C?

description: Use ctypes to call back C function from python embedded in C


code: t = tuple(x[0] for x in s)

rawDescription: Getting the first elements per row in an array in Python?


code:
a = "Some string" * 10 ** 7
b = a[10000]
del a

rawDescription: Python's immutable strings and their slices


code:
def add(self, name):
    if self.head == None:
        self.head = PersonList(name)
    else:
         tmp = self.head
         while tmp._next is not None: #while we are not at the end of the line
             tmp = tmp._next
         #end of line
         tmp._next = PersonList(name) #get in line at the end

rawDescription: Using a linked list in python to simulate standing in line


code:
obj = l
for index in indices[:-1]:
    obj = obj[index]
del obj[indices[-1]]

rawDescription: del Operator in Arbitrarily Nested List

description: Del Operator in Arbitrarily Nested List


code:
logfile = r'C:\logfile.txt'
y = open(r'C:\extract.txt', 'a')
with open(logfile, 'r') as read:
  for line in read:
    if "solver.cpp:189" in line:
        loss = line.split(',')[1]
        y.write(loss)
    if "solver.cpp:470" in line:
        lr = line.split(',')[1]
        y.write(lr)

rawDescription: how can I extract the lines of a big log file in python

description: Extract the lines of a big log file in python


code:
import calendar
import datetime
import pytz

d = datetime.datetime.now(pytz.timezone('America/Los_Angeles'))
# d == datetime.datetime(2012, 7, 10, 1, 6, 36, 37542, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)

ts = calendar.timegm(d.utctimetuple())
# ts == 1341907596

# test with UTC epoch:

d = datetime.datetime(1969, 12, 31, 16, 0, 0, 0, pytz.timezone('America/Los_Angeles'))
# d == datetime.datetime(1969, 12, 31, 16, 0, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)

ts = calendar.timegm(d.utctimetuple())
# ts == 0

rawDescription: Get UNIX timestamp (ie UTC timestamp) from a Python timezone aware date


code:
import subprocess

with open("inputfile", "rb") as f:
    subprocess.call(f.read())

rawDescription: How to open a program from a text file in Python

description: Open a program from a text file in Python


code:
rounds = input()
for i in range(rounds):
    ...

rawDescription: How do you determine the number of inputs from an integer in a prior input? Dice game

description: Determine the number of inputs from an integer in a prior input? Dice game


code:
import csv

PASSWORDS = "passwords.csv"

def in_csv(fname, row, **kwargs):
    with open(fname) as inf:
        incsv = csv.reader(inf, **kwargs)
        return any(r==row for r in incsv)

def login():
    ltext1 = lment1.get()
    ltext2 = lment2.get()

    if in_csv(PASSWORDS, [ltext1, ltext2]):
        # login is correct
        nothing()
    else:
        # login failed
        something()

rawDescription: How can I make a login function that checks what the user inputted to what is on a .csv file? Python

description: Make a login function that checks what the user inputted to what is on a .csv file? Python


code:
df1['c_len'] = df1.c.apply(len)
df1['d_len'] = df1.d.apply(len)
df1[(df1.c_len == 0) | (df1.d_len == 0)]

rawDescription: Pandas: How to select rows with [ ] in any of 2 selected column


rawDescription: Sorting a Dictionary in Python by two keys (frequency and lexicographically)


code:
def work(arg):
    i, n = arg
    mask = var1['halo_id'] == n
    newtbdata = tbdata1[mask]
    hdu = pyfits.BinTableHDU(newtbdata)
    hdu.writeto(('/home/Documments/file_{0}.fits').format(i))

if __name__ == '__main__':
    pool = Pool(processes=4)
    pool.map(work, enumerate(halo_param.strip()))
    pool.close()
    pool.join()

rawDescription: Use multiprocessing for a for loop, Python


code:
import csv
array = csv.reader(open('1.csv','r'))
a = list(array)[:] # for a[0] - save first line
s_array = [a[0]] + sorted(a[1:], key=lambda x: x[-1])
csv.writer(open('2.csv', 'wb'), dialect='excel').writerows(s_array)

rawDescription: Python how can i sort a csv file by the column id


code:
someList = [8, 9, 7, 3, 1]
someDict = {1:2, 2:3, 3:4, 4:5, 5:6}
intersection = [i for i in someList if i in someDict]
print(intersection)

rawDescription: Find out if no items in a list are keys in a dictionary


code: iskDropped, iskDestroyed, iskTotal = [condense_value(int(value[:-7].replace(',',''))) for value in [iskDropped, iskDestroyed, iskTotal]]

rawDescription: Can I condense these values with a loop?

description: Condense these values with a loop


code:
with open("path/to/my/file/test.txt", 'rb') as f:
    # Strip will remove the newlines, spaces, etc...
    lines = [line.strip() for line in f.readlines()]
numbers = [21, 22, 23]
itr = iter(numbers)
with open("path/to/my/file/test.txt", 'wb') as f:
    for line in lines:
        if '/' in line:
            f.write('%s %s\n' % (line, itr.next()))
        else:
            f.write('%s\n' % line)

rawDescription: Add numbers from a list to an existing file using python


code:
import re
foo = "jessica\n\n\nhello\n\n\n\nhow are you doing?"
re.sub('(\n{1,2})\n*', '\g<1>', foo)

rawDescription: In Python regex, how do I make the maximum number of line breaks to be 2?


code:
df = pd.DataFrame(data)
fix = df.groupby(level=0)['data'].apply(lambda x:pd.DataFrame(x.iloc[0],columns = ['Year','Value']))
fix = fix.reset_index(level=1,drop=True)
df = pd.merge(fix,df.drop(['data'],1),how='inner',left_index=True,right_index=True)

rawDescription: Creating pandas dataframe from list of dictionaries containing lists of data


code:
def pytest_configure(config):
    terminal = config.pluginmanager.getplugin('terminal')
    BaseReporter = terminal.TerminalReporter
    class QuietReporter(BaseReporter):
        def __init__(self, *args, **kwargs):
            BaseReporter.__init__(self, *args, **kwargs)
            self.verbosity = 0
            self.showlongtestinfo = self.showfspath = False

    terminal.TerminalReporter = QuietReporter

rawDescription: How can I show verbose py.test diffs without verbose test progress?

description: Show verbose py.test diffs without verbose test progress


code:
from xml.dom.minidom import parse

doc = parse('in.tcx')

trackpoints = doc.getElementsByTagName("Trackpoint")
result = []
elements = ('Time', 'LatitudeDegrees', 'LongitudeDegrees')
for tp in trackpoints:
    obj = {}
    for el in elements:
        obj[el] = tp.getElementsByTagName(el)[0].firstChild.data
    result.append(obj)

print(result)

rawDescription: python xml extraction for loop

description: Python xml extraction for loop


code:
from rauth import OAuth1Service
import webbrowser

def getSession():
    # Create a session
    # Use actual consumer secret and key in place of 'foo' and 'bar'
    service = OAuth1Service(
              name = 'etrade',
              consumer_key = 'foo',
              consumer_secret = 'bar',
              request_token_url = 'https://etws.etrade.com/oauth/request_token',
              access_token_url = 'https://etws.etrade.com/oauth/access_token',
              authorize_url = 'https://us.etrade.com/e/t/etws/authorize?key={}&token={}',
              base_url = 'https://etws.etrade.com')

    # Get request token and secret
    oauth_token, oauth_token_secret = service.get_request_token(params =
                                  {'oauth_callback': 'oob',
                                   'format': 'json'})

    auth_url = service.authorize_url.format(consumer_key, oauth_token)

    # Get verifier (direct input in console, still working on callback)
    webbrowser.open(auth_url)
    verifier = input('Please input the verifier: ')

    return service.get_auth_session(oauth_token, oauth_token_secret, params = {'oauth_verifier': verifier})

# Create a session
session = getSession()

# After authenticating a session, use sandbox urls
url = 'https://etwssandbox.etrade.com/accounts/sandbox/rest/accountlist.json'

resp = session.get(url, params = {'format': 'json'})

print(resp)

rawDescription: Getting an oauth request token from etrade in Python


rawDescription: Python organzing default values as objects


code:
import httplib2
h = httplib2.Http(".cache")
resp, content = h.request("http://example.org/", "GET")

rawDescription: Downloading compressed content over HTTP using Python


code:
import cgi

data = cgi.FieldStorage()

make = data.getvalue('make')
model = data.getvalue('model')

print ( 'Content-type:text/html\r\n\r\n' )
print ( '''<!DOCTYPE HTML>

            <meta charset="UTF-8">
            <title>Python Response</title>

, ''' + make + ', ' + model + ''',

            Back

        ''' )

rawDescription: Issue with HTTP GET Parameters in Python CGI


code:
import xmlrpclib, httplib

class TimeoutTransport(xmlrpclib.Transport):
    timeout = 10.0
    def set_timeout(self, timeout):
        self.timeout = timeout
    def make_connection(self, host):
        h = httplib.HTTPConnection(host, timeout=self.timeout)
        return h

t = TimeoutTransport()
t.set_timeout(20.0)
server = xmlrpclib.Server('http://time.xmlrpc.com/RPC2', transport=t)

rawDescription: Timeout for xmlrpclib client requests

description: Timeout for xmlrpclib client requests


code:
domain = response.url.replace("http://","").replace("https://","").split("/")[0]
links = [k for k in links if domain in k.url]

rawDescription: Only Allow Internal Links in Broad Scrapy Web Crawl


code:
from os.path import dirname
dirname("/tmp/test/helloworld.cpp")

rawDescription: truncate the filname part of a string that is a path.

description: Truncate the filname part of a string that is a path.


code:
import ast

playerSpells_list = ast.literal_eval(playerSpells)

rawDescription: Import a list from a file, into a list

description: Import a list from a file, into a list


code:
#!/usr/bin/python
import threading
import sys
from PyQt4 import QtGui, QtCore
import time
import os

class Class(QtGui.QWidget):

    display_update = QtCore.pyqtSignal() # ADDED

    def __init__(self):
        #Some init variables
        self.initUI()

    def initUI(self):
        #some UI
        self.display_update.connect(self.guiUpdate) # ADDED
        self.show()

    def update(self):
        while True and not self.stop_event.isSet():
            self.updateSpeed()
            self.updateDistance()
            self.printLogs()
            self.saveCSV()

            # self.guiUpdate()
            self.display_update.emit() # ADDED

            time.sleep(1)

    #gui button function
    def initiate(self):
        self.stop_event = threading.Event()
        self.c_thread = threading.Thread(target = self.update)
        self.c_thread.start()

    #Also gui button function
    def cancelTracking(self):
        self.stop_event.set()
        self.close()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Class()
    sys.exit(app.exec_())
    # ex.update() # - this does nothing

if __name__ == '__main__':
    main()

rawDescription: pyQt and threading application crash

description: PyQt and threading application crash


code:
y = int(form['year'].value)
m = int(form['month'].value)
d = int(form['day'].value)

rawDescription: Python (2.7.5.) need assistance with calendar module


code:
from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get(app_label='Apps', model='app_users')
ct.delete()

rawDescription: Django : Syncdb incorrectly warns that many-to-many field is stale


rawDescription: How to bind scrollbar bottom in wxPython ListCtrl

description: Bind scrollbar bottom in wxPython ListCtrl


code:
temp = lb.E.entityset(
        lb.E.entity(
            lb.E.temp(code='{0}'.format(temp_code)),
            lb.E.attr(lb.E.value('{0}'.format(value)), code='2ndCode'),
            lb.E.attr(lb.E.value('{0}'.format(value2)), code='3rdCode'),
        )
    )

rawDescription: Python3 lxml builder

description: Python3 lxml builder


rawDescription: Finding indices given condition in numpy matrix


code:
for line in open(os.path.join(dirname, filename), "r").readlines():
    if 'string' in line:
        print("found "+line)
        break

rawDescription: only print the first found string in textfile with python

description: Only print the first found string in textfile with python


code:
from PyQt4.QtGui import QPainter

yourQWebView.setRenderHints(QPainter.TextAntialiasing | QPainter.SmoothPixmapTransform | QPainter.HighQualityAntialiasing)

rawDescription: Controlling how QtWebKit resizes images

description: Controlling how QtWebKit resizes images


code:
import os

path = r"C:\Drawings"

for root, dirs, files in os.walk( path ): # parse through file list in the current directory
    for filename in files:
        if "~" in filename:
            source_filename = os.path.join(root, filename)
            target_filename = os.path.join(root, filename.replace("~","_")) # convert spaces to _'s
            os.rename(source_filename, target_filename) # rename the file

rawDescription: Using os.walk in Python

description: Using os.walk in Python


code:
for row in reader:
    writer.writerow([row['User Name'],row['Address'].split(',')[0],row['Last Login DateTime'],row['Address'].split(',')[7]])

rawDescription: Python CSV read file and select columns and write to new CSV file


code:
from igraph import Graph
from random import randint
g = Graph.GRG(100, 0.2)        # generate a geometric random graph
g.es["capacity"] = [randint(0, 1000) for i in xrange(g.ecount())]
cut = g.maxflow(0, 99, "capacity")

rawDescription: Can python-igraph be used for min s-t cut?


code:
def getReversedList(aList, end, start, step):
    return aList[end:start if start!=-1 else None:step]

rawDescription: Extended slice that goes to beginning of sequence with negative stride


code:
entries = set()

for row in reader:
   key = (row[0], row[1]) # instead of just the last name

   if key not in entries:
      writer.writerow(row)
      entries.add(key)

rawDescription: How to remove duplicates in a csv file based on two columns?

description: Remove duplicates in a csv file based on two columns


code:
child.send("\033[A")  # up
child.send("\033[B")  # down

rawDescription: pexpect send cursor movement

description: Pexpect send cursor movement


code:
import StringIO
fh = StringIO.StringIO()
f.send_flowing_data(s)
print(fh.getvalue())
fh.close()

rawDescription: Can the formatter class be used without relying on an intermediate stream/file?


code:
import re
re.split(r'\b/\b', 'https://example.com/about/hello/')

rawDescription: Deleting all occurances of '/' after its 2nd occurance in python


code:
persons = []
for person in driver.find_elements_by_class_name('person'):
    title = person.find_element_by_xpath('.//div[@class="title"]/a').text
    company = person.find_element_by_xpath('.//div[@class="company"]/a').text

    persons.append({'title': title, 'company': company})

rawDescription: Selenium: Iterating through groups of elements


code:
import re

yourrule = re.compile(r"@£$¥èéùìòÇ\fØø\nÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\"#¤%&'()*+,-./[0-9]:;<=>\?¡[A-Z]ÄÖÑÜ§¿[a-z]äöñüà\^\{\}\[~\]\|€")

def validate_GSM_characters(value):
    """ Raise a ValidationError if the value contains any values not within
        the GSM character set.
    """
    if not yourrule.search(value):
        msg = u"Invalid characters."

        raise ValidationError(msg)

rawDescription: Creating a regular expression in django

description: Creating a regular expression in django


rawDescription: How to print dict sorted by keys?

description: Print dict sorted by keys


code:
class MyClass:
    ...

    @property
    def length(self):
        return self.x.size

rawDescription: Updating instance attributes of class

description: Updating instance attributes of class


code:
import wx

MIN_ALPHA = 64
MAX_ALPHA = 255

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.alpha = MAX_ALPHA
        self.SetTitle('Mouse Alpha')
        self.on_timer()
    def on_timer(self):
        x, y, w, h = self.GetRect()
        mx, my = wx.GetMousePosition()
        d1 = max(x - mx, mx - (x + w))
        d2 = max(y - my, my - (y + h))
        alpha = MAX_ALPHA - max(d1, d2)
        alpha = max(alpha, MIN_ALPHA)
        alpha = min(alpha, MAX_ALPHA)
        if alpha != self.alpha:
            self.SetTransparent(alpha)
            self.alpha = alpha
        wx.CallLater(20, self.on_timer)

if __name__ == '__main__':
    app = wx.App(None)
    frame = Frame()
    frame.Show()
    app.MainLoop()

rawDescription: Capturing mouse events outside wx.Frame in Python


code:
import timeit
import sched
import time
import logging
import sys

logger = logging.getLogger(__name__)
logging.basicConfig(level = logging.DEBUG,
                    format = '%(threadName)s: %(asctime)s: %(message)s',
                    datefmt = '%H:%M:%S')
schedule = sched.scheduler(timeit.default_timer, time.sleep)
delay = 5 # change to 3600 to schedule event in 1 hour

def interval():
    logger.info('All work and no play makes Jack a dull boy.')
    schedule.enter(delay = delay, priority = 1, action = interval, argument = ())
    # Uncomment this to see how scheduled events are delayed if interval takes a
    # long time.
    # time.sleep(10)

schedule.enter(delay = 0, priority = 1, action = interval, argument = ())
try:
    schedule.run()
except (KeyboardInterrupt, SystemExit):
    print('Exiting')
    sys.exit()

rawDescription: Stopping a timed thread in python

description: Stopping a timed thread in python


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

br = webdriver.Firefox()
br.get('http://www.google.com/')

save_me = ActionChains(br).key_down(Keys.CONTROL)\
         .key_down('s').key_up(Keys.CONTROL).key_up('s')
save_me.perform()

rawDescription: How to save "complete webpage" not just basic html using Python

description: Save "complete webpage" not just basic html using Python


code:
def lineKey (line):
    keyStr, rest = line.split(' ', 1)
    a, b = keyStr.split('/', 1)
    return (a, int(b))

sorted(lines, key=lineKey)

rawDescription: compound sorting in python

description: Compound sorting in python


code:
import re

text= '''Q2 2016 Apple Inc Earnings Call - Final I am feeling lucky today     Event Brief of Q1 2016 Apple Inc Earnings Call - Final'''
text2= 'YouHolaHey'
pattern = '</(?:SPAN|span)>(?P<content>[\w\d\s]*)<(?:SPAN|span)'

print(re.findall(pattern, text))
print(re.findall(pattern, text2))

rawDescription: Content between two span class

description: Content between two span class


code:
from openpyxl import load_workbook
wb = load_workbook("Données.xlsm")
for n in wb.sheetnames:
    ws = wb[n]
    print(n, ws.sheet_properties.codeName)

rawDescription: How get a excel sheet with its code name property with "python"


code:
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
import scipy.optimize

def main():
    # Actual parameters
    A0, K0, C0 = 2.5, -4.0, 2.0

    # Generate some data based on these
    tmin, tmax = 0, 0.5
    num = 20
    t = np.linspace(tmin, tmax, num)
    y = model_func(t, A0, K0, C0)

    # Add noise
    noisy_y = y + 0.5 * (np.random.random(num) - 0.5)

    fig = plt.figure()
    ax1 = fig.add_subplot(2,1,1)
    ax2 = fig.add_subplot(2,1,2)

    # Non-linear Fit
    A, K, C = fit_exp_nonlinear(t, noisy_y)
    fit_y = model_func(t, A, K, C)
    plot(ax1, t, y, noisy_y, fit_y, (A0, K0, C0), (A, K, C0))
    ax1.set_title('Non-linear Fit')

    # Linear Fit (Note that we have to provide the y-offset ("C") value!!
    A, K = fit_exp_linear(t, y, C0)
    fit_y = model_func(t, A, K, C0)
    plot(ax2, t, y, noisy_y, fit_y, (A0, K0, C0), (A, K, 0))
    ax2.set_title('Linear Fit')

    plt.show()

def model_func(t, A, K, C):
    return A * np.exp(K * t) + C

def fit_exp_linear(t, y, C=0):
    y = y - C
    y = np.log(y)
    K, A_log = np.polyfit(t, y, 1)
    A = np.exp(A_log)
    return A, K

def fit_exp_nonlinear(t, y):
    opt_parms, parm_cov = sp.optimize.curve_fit(model_func, t, y, maxfev=1000)
    A, K, C = opt_parms
    return A, K, C

def plot(ax, t, y, noisy_y, fit_y, orig_parms, fit_parms):
    A0, K0, C0 = orig_parms
    A, K, C = fit_parms

    ax.plot(t, y, 'k--',
      label='Actual Function:\n $y = %0.2f e^{%0.2f t} + %0.2f$' % (A0, K0, C0))
    ax.plot(t, fit_y, 'b-',
      label='Fitted Function:\n $y = %0.2f e^{%0.2f t} + %0.2f$' % (A, K, C))
    ax.plot(t, noisy_y, 'ro')
    ax.legend(bbox_to_anchor=(1.05, 1.1), fancybox=True, shadow=True)

if __name__ == '__main__':
    main()

rawDescription: fitting exponential decay with no initial guessing

description: Fitting exponential decay with no initial guessing


code:
my_dir = os.path.dirname(sys.argv[0])
data_file_path = os.path.join(my_dir, data_filename)

rawDescription: Access external program data files from script in Python


code:
def unscramble(A_shuf,ind_shuf):
    order = np.lexsort(ind_shuf[::-1])
    return A_shuf.flat[order].reshape(A_shuf.shape)

rawDescription: Fastest way to reorder a numpy array across multiple axes using index arrays


code:
import csv
with open("blank.csv", "rb") as fp:
    reader = csv.reader(fp)
    rows = [line for line in reader if line]
    blob = zip(*rows)

blob

rawDescription: problems reading a csv by column in python

description: Problems reading a csv by column in python


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

q_range = np.linspace(0.01,1,num=100)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(q_range, q_range**(-0.4), label = r"$q^{-0.4}$")
ax.legend()
plt.show()

rawDescription: Using LaTex in Matplotlib legend

description: Using LaTex in Matplotlib legend


code:
from django.forms.forms import Form
from django.forms.fields import CharField
class FunkyForm(Form):
    def __init__(self, *args, **kwargs):
        super(FunkyForm, self).__init__(*args, **kwargs)
        for item in range(5):
            self.fields['test_field_%s' % item] = CharField(max_length=255)

rawDescription: building dynamic forms in django

description: Building dynamic forms in django


rawDescription: How can I select which multi-index axis splits the data in a groupby object across different subplots?

description: Select which multi-index axis splits the data in a groupby object across different subplots


code:
from __future__ import print_function
import urllib2

baseURL = "http://www.website.com/page/"

for n in xrange(100):
    fullURL = baseURL + str(n)
    #print fullURL
    try:
        req = urllib2.Request(fullURL)
        resp = urllib2.urlopen(req)
        if resp.getcode() == 404:
            #Do whatever you want if 404 is found
            print ("404 Found!")
        else:
            #Do your normal stuff here if page is found.
            print ("URL: {0} Response: {1}".format(fullURL, resp.getcode()))
    except:
        print ("Could not connect to URL: {0} ".format(fullURL))

rawDescription: Checking a url for a 404 error scrapy

description: Checking a url for a 404 error scrapy


code:
import numpy
a = numpy.zeros(shape=(5,2))
a
a[0] = [1,2]
a[1] = [2,3]
a

rawDescription: How do I create an empty array/matrix in NumPy?

description: Create an empty array/matrix in NumPy


code:
url = 'http://waterservices.usgs.gov/nwis/iv/'
params = {'site': 11527000, 'format': 'json', 'parameterCd': '00060', 'period': 'P52W'}
r = requests.get(url, headers={'Accept-Encoding': 'gzip,deflate'})

rawDescription: Unable to GET entire page with Python request


rawDescription: why product of range(1,35) is equal to zero?

description: Why product of range(1,35) is equal to zero


code:
# prints out "uk"
{{ request.LANGUAGE_CODE }}

# prints out "Січ"
{{ month_verbose|date:"M" }}

rawDescription: How to translate dates in django template?

description: Translate dates in django template


code:
P = Property
myList = [P(0), P(2.3), P(4), P(0,2)]

# or...

def createPropertyList(*args):
    return [Property(x) for x in args]

myList = createPropertyList(0,2.3,4,(0,2))

rawDescription: Polymorphism with built-in types in Python


code:
with open('yourfile.pdf', 'w') as f:
    f.write(xdata)

rawDescription: download file from web service in python 3

description: Download file from web service in python 3


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

import textwrap
fname='/tmp/tmp.dat'
with open(fname,'w') as f:
    f.write(textwrap.dedent('''\
        id col1 col2 col3
        2010 1 2 3 4
        # Foo

        2011 5 6 7 8
        # Bar
        # Baz
        2012 8 7 6 5
        '''))

data = np.genfromtxt(fname,
                     comments='#',    # skip comment lines
                     dtype = None,    # guess dtype of each column
                     names=True)      # use first line as column names
print(data)
plt.plot(data['id'],data['col2'])
plt.show()

rawDescription: Make pyplot faster than gnuplot

description: Make pyplot faster than gnuplot


code:
errors = []
growing_rf = RandomForestClassifier(n_estimators=10, n_jobs=-1,
                                    warm_start=True, random_state=1514)
for i in range(40):
    growing_rf.fit(X_train, y_train)
    growing_rf.n_estimators += 10
    errors.append(log_loss(y_valid, growing_rf.predict_proba(X_valid)))

_ = plt.plot(errors, '-r')

rawDescription: Python - Random Forest - Iteratively adding trees


code:
import pyglet, random

window = pyglet.window.Window()

label = pyglet.text.Label('Hello, world',
                          font_name='Times New Roman',
                          font_size=36,
                          x=window.width//2, y=window.height//2,
                          anchor_x='center', anchor_y='center')
@window.event
def on_draw():
    window.clear()
    label.draw()

@window.event
def on_key_press(symbol, modifiers):
    pyglet.image.get_buffer_manager().get_color_buffer().save('screenshot.png')

def update(dt):
    label.x += random.randint(-10, 10)
    label.y += random.randint(-10, 10)

pyglet.clock.schedule_interval(update, 0.1)
pyglet.app.run()

rawDescription: Taking a screenshot with Pyglet [Fix'd]

description: Taking a screenshot with Pyglet [Fix'd]


code: sorted(plots, key = lambda plot :(plot._linestyle, plot._y[-1]))

rawDescription: Sort arrays by two criteria

description: Sort arrays by two criteria


code:
from  numpy.lib.recfunctions import append_fields
a = numpy.rec.array(a, dtype=[('a', numpy.float64)])
new_a = append_fields(a, 'b', b, usemask=False, dtypes=[numpy.int64])
new_a
new_a['a']
new_a['b']

rawDescription: numpy: is it possible to preserve the dtype of columns when using column_stack

description: Numpy: is it possible to preserve the dtype of columns when using column_stack


code:
s="""<root>
   ...
"""
import xml.etree.ElementTree as ET
catalog = list( ET.fromstring(s) )[0]  #<--- here
ET.tostring(catalog, encoding='utf8', method='xml')

rawDescription: How to remove root element from xml file using python?

description: Remove root element from xml file using python


code:
import sys
try:
    fileobj = open(sys.argv[1], 'r')
except IndexError:
    fileobj = sys.stdin

with fileobj:
    data = fileobj.read()

rawDescription: How to read a whole file in Python? To work universally in command line

description: Read a whole file in Python? To work universally in command line


code:
columns = soup.findAll('td')
for col in columns:
    try:
        part = col.find("font", {"color": "#7b1010"}).contents[0]
        print(part)
    except:
        pass

rawDescription: Beautifulsoup parse through poor tags

description: Beautifulsoup parse through poor tags


code:
# make a Flask app
from flask import Flask, render_template, g
app = Flask(__name__)
@app.route("/")
def index():
    return render_template("index.html")

# run in under twisted through wsgi
from twisted.web.wsgi import WSGIResource
from twisted.web.server import Site

resource = WSGIResource(reactor, reactor.getThreadPool(), app)
site = Site(resource)

# bind it etc
# ...

rawDescription: Web interface for a twisted application

description: Web interface for a twisted application


code:
#assuming a fixed number of investments dates, build a list
investment_dates = pd.date_range('2014-1-1', periods=12, freq='MS')

#build a table, by month, which contains the cumulative MTD
#return for each invesment date.  Still have to loop over the investment dates,
#but don't need to loop over each daily value
running_mtd = []
for date in investment_dates:
    curr_mo = (df[df.index >= date].groupby('Month')['MTD'].last() + 1.).cumprod()
    curr_mo.name = date
    running_mtd.append(curr_mo)
running_mtd_df = pd.concat(running_mtd, axis=1)
running_mtd_df = running_mtd_df.shift(1).fillna(1.)

#merge running mtd returns with base dataframe
df = df.merge(running_mtd_df, left_on='Month', right_index=True)

#calculate ytd return for each column / day, by multipling the running
#monthly return with the current MTD value
for date in investment_dates:
    df[date] = np.where(df.index < date, np.nan, df[date] *  (1. + df['MTD']) - 1.)

rawDescription: Speeding up pandas array calculation

description: Speeding up pandas array calculation


code:
# set up a scoped_session
from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker

session_factory = sessionmaker(bind=some_engine)
Session = scoped_session(session_factory)

# now all calls to Session() will create a thread-local session
some_session = Session()

# you can now use some_session to run multiple queries, etc.
# remember to close it when you're finished!
Session.remove()

rawDescription: Multi-threaded use of SQLAlchemy

description: Multi-threaded use of SQLAlchemy


code:
def formfield_for_foreignkey(self, db_field, request, **kwargs):
    if db_field.name == 'node':
        kwargs['queryset'] = Node.objects.order_by('name')
    return super(DeviceAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

rawDescription: Django admin select ordering different than default model ordering


code:
import numpy
original_ball = cv2.imread("Aqua-Ball-Red-icon.png")
ball = cv2.resize(bola_original, (64,64), fx=1, fy=1)
mask_original = cv2.imread("input-mask.png",0)
mask = cv2.resize(mask_original, (64,64), fx=1, fy=1)

max_value= numpy.max(mask)
mask/=max_value
res= ball*mask

rawDescription: opencv/python: mask in module cv2 while streaming webcam video

description: Opencv/python: mask in module cv2 while streaming webcam video


code:
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
...

rawDescription: import input_data MNIST tensorflow not working

description: Import input_data MNIST tensorflow not working


code:
places = ['New York', 'Texas', 'California']
zips = ['01010', '70707', '90909']
place_zip_map = dict(zip(places, zips))
place_zip_map['New York']

rawDescription: Pythonic way of exchanging between lists?


code:
import urllib2
urllib2._opener.handlers[1].set_http_debuglevel(100)
urllib2.urlopen('http://mit.edu').read()[:10]

rawDescription: Can urllib2 make HTTP/1.1 requests?

description: Can urllib2 make HTTP/1.1 requests


code:
from matplotlib import pyplot as plt
import math
tempList = [20.3, 20.4, 20.4, 20.4, 20.4, 20.4, 20.4, 20.4, 20.4, 20.4]
plt.plot(tempList)
plt.ylim(math.floor(min(tempList)), math.ceil(max(tempList)))
plt.show()

rawDescription: matplotlib screws up float axis

description: Matplotlib screws up float axis


code:
my_list = ['text1', 'text1', 'text1', 'text2', 'text2', 'text2', 'text3', 'text3', 'text3']
outfiles = ['file1', 'file2', 'file3']

i = 0 # to keep count of which words will be written
for f in outfiles:
    with open ('{}.txt'.format(f), 'w') as fo:
        for word in my_list[i:i + 3]:
            fo.write(word + "\n")
    i += 3

rawDescription: Split a list in python into different text files


code:
data = bytearray()
while True:
    chunk = yield from reader.read(100)
    if not chunk:
        break
    data += chunk

rawDescription: Reading from asyncio StreamReader

description: Reading from asyncio StreamReader


code:
while True:
    num = input
    if num > 0.0:
        do_stuff()
    elif num < 0.0:
        do_neg_stuff()
    else:
        break

rawDescription: traced my code and it seems to be in a continous loop

description: Traced my code and it seems to be in a continous loop


code:
from tkinter import *

def curseur(bidon):
    root.after(50, lambda: mytext.mark_set(INSERT, END))

root = Tk()
mytext = Text(root)
mytext.pack()
mytext.insert(INSERT, "Clic in other window, then clic back here (line one)\n")
mytext.insert(INSERT, "Type something on the keyboard\n")
mytext.insert(INSERT, "The typed text must go to the end of the widget\n")
mytext.bind("<FocusIn>", curseur)
root.mainloop()

rawDescription: Move insert position in Text widget

description: Move insert position in Text widget


code:
class AuthorSchema(Schema):
    # Make sure to use the 'only' or 'exclude' params
    # to avoid infinite recursion
    books = fields.Nested('BookSchema', many=True, exclude=('author', ))
    class Meta:
        fields = ('id', 'name', 'books')

class BookSchema(Schema):
    author = fields.Nested(AuthorSchema, only=('id', 'name'))
    class Meta:
        fields = ('id', 'title', 'author')

rawDescription: Circular import in marshmallow

description: Circular import in marshmallow


code:
import string
''.join(i for i in 'cats\xe2\x80\x99 faces' if i in string.printable)

rawDescription: regex unicode characters

description: Regex unicode characters


rawDescription: Pandas dropna does not work as expected on a MultiIndex


code: os.path.join(DB_NAME, 'sqlite3DB.db')

rawDescription: Is there a pythonic way to add a file name to absolute path?


code:
with open (output1, "wb") as compdata:
    for row in range(height):
        bytes = bytearray(cobs(delta_rows[row].getByte_List()))
        compdata.write(bytes)

rawDescription: How to write a list of integers to a binary file in python

description: Write a list of integers to a binary file in python


code:
from PyQt4 import QtCore, QtGui
from PyQt4.QtWebKit import QWebPage
from PyQt4.QtGui import QApplication

class TextBrowser(QtGui.QDialog):

    def __init__(self, url):
        self.some_url = url

        QtCore.QObject.__init__(self)
        self.page = QWebPage()
        self.page.loadFinished.connect(self.get_html)
        self.page.mainFrame().load(self.some_url)

    def get_html(self):
        frame = self.page.mainFrame()
        self.html = frame.toHtml()
        self.close()

def get_html_source():
    app = QApplication([])
    urls = ['http://www.google.com', 'http://www.yahoo.com/']
    out = []
    for u in urls:
        t = TextBrowser(QtCore.QUrl(u))
        t.exec_()
        out.append(t.html)
    print(out)

if __name__ == "__main__":
    get_html_source()

rawDescription: QWebpage only fetches HTML once, and cannot be invoked again


code:
import csv

trials = ['trial134', 'trial1', 'trial56']
data = csv.reader(open('completedataset.txt'))

with open('smalldataset.txt','w') as outf:
    csv.writer(outf).writerows(l for l in data if l[0] in trials)

rawDescription: Question about nested loops

description: Question about nested loops


rawDescription: How to properly import a function for use in timeit

description: Properly import a function for use in timeit


code:
from Tkinter import Tk, Button
import ttk

root = Tk()

tree = ttk.Treeview(root)

tree["columns"]=("one","two")
tree.column("one", width=100 )
tree.column("two", width=100)
tree.heading("one", text="coulmn A")
tree.heading("two", text="column B")

tree.insert("" , 0,    text="Line 1", values=("1A","1b"))

id2 = tree.insert("", 1, "dir2", text="Dir 2")
tree.insert(id2, "end", "dir 2", text="sub dir 2", values=("2A","2B"))

##alternatively:
tree.insert("", 3, "dir3", text="Dir 3")
tree.insert("dir3", 3, text=" sub dir 3",values=("3A"," 3B"))

def edit():
    x = tree.get_children()
    for item in x: ## Changing all children from root item
        tree.item(item, text="blub", values=("foo", "bar"))

def delete():
    selected_item = tree.selection()[0] ## get selected item
    tree.delete(selected_item)

tree.pack()
button_del = Button(root, text="del", command=delete)
button_del.pack()
button_del = Button(root, text="edit", command=edit)
button_del.pack()

root.mainloop()

rawDescription: Delete and Edit items in TreeView Tkinter


code:
buildOptions = dict(include_files = [(absolute_path_to_your_file,'final_filename')]) #single file, absolute path.

buildOptions = dict(include_files = ['your_folder/']) #folder,relative path. Use tuple like in the single file to set a absolute path.

setup(
         name = "appname",
         version = "1.0",
         description = "description",
         author = "your name",
         options = dict(build_exe = buildOptions),
         executables = executables)

rawDescription: How can I include a folder with cx_freeze?

description: Include a folder with cx_freeze


code:
a = (1, 2)
b = (3, 2)
sum(x == y for x, y in zip(a, b)) / float(len(a))

rawDescription: How do you find the similarity of tuples, as a fraction, in python?

description: Find the similarity of tuples, as a fraction, in python


code:
import django
from django.conf import settings
from django.core.mail import send_mail

send_mail('Subject here', 'Here is the message.', settings.EMAIL_HOST_USER,
    ['to@example.com'], fail_silently=False)

rawDescription: Python Django Gmail SMTP setup

description: Python Django Gmail SMTP setup


code:
import matplotlib.pyplot as plt

class Workaround(object):
    def __init__(self, artists):
        self.artists = artists
        artists[0].figure.canvas.mpl_connect('button_press_event', self)

    def __call__(self, event):
        for artist in self.artists:
            artist.pick(event)

fig1, ax1 = plt.subplots()
ax2 = ax1.twinx()

ax1.plot([1,2,3],[0.1,0.82,0.3],'y*', label="one")
ax2.plot([1,2,3],[5,6,7],'ro', label="two")

leg1 = ax1.legend()
leg2 = ax2.legend(loc='upper left')

leg1.draggable(True)
leg2.draggable(True)

draggable_workaround = Workaround([leg1, leg2])

plt.show()

rawDescription: Matplotlib: Draggable is not working on multiple legends of plots which are generated with twinx()


code:
import array
from System import Array, Char

x = array.array('c', 'abc')

y = Array[Char](x)

rawDescription: Convert between python array and .NET Array


code:
for tick in fig.findobj(matplotlib.axis.Tick):
    tick.label1.set_visible(tick.label1On)
    tick.label2.set_visible(tick.label2On)

for text in fig.findobj(match=Text, include_self=False):
    s = text.get_text()
    if not s or not text.get_visible(): continue
    # do something with the texts

rawDescription: Duplicate Tick Labels in Matplotlib

description: Duplicate Tick Labels in Matplotlib


code:
import os
os.environ['MKL_NUM_THREADS'] = '1'

rawDescription: python multiprocessing module: strange behaviour and processor load when using Pool

description: Python multiprocessing module: strange behaviour and processor load when using Pool


rawDescription: In python Sorting a list of tuples?

description: In python Sorting a list of tuples


code:
class GoldorSilverFilter(DateFieldFilterSpec):
    def __init__(self, f, request, params, model, model_admin):
        super(GoldorSilverFilter, self).__init__(f, request, params, model, model_admin)

        self.links = (
            (_('All'), {}),
            (_('Gold'), {'%s__regex' % self.field.name: '^[^J]',}),
            (_('Silver'), {'%s__istartswith' % self.field.name: 'J',}),
        )

    def title(self):
        return _("Metal")
        #return self.field.name

FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'goldorsilverfilter', False), GoldorSilverFilter))

rawDescription: Django Exclude in list_filter with custom filter


code:
key=registry.OpenKey(registry.HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections",0,registry.KEY_ALL_ACCESS)
(value, regtype) = registry.QueryValueEx(key, "DefaultConnectionSettings")
if regtype == registry.REG_BINARY:
     value = value[:8] + chr(0x03) + value[9:]
registry.SetValueEx(key, "DefaultConnectionSettings", None, regtype, value)

rawDescription: Proxy setting in windows via python

description: Proxy setting in windows via python


code:
text = "Bob and Anna are meeting at noon"
words = {}
last_p = None
for word in text.split():
    word2 = word.lower()
    if word2 == word2[::-1]:
        words[word] = []
        last_p = word
    elif last_p:
        words[last_p].append(word)
print(words)

rawDescription: dictionary with palindromes to non palindromes?

description: Dictionary with palindromes to non palindromes


code:
def addEvent(calendar_service):
    event = gdata.calendar.CalendarEventEntry()
    event.content = atom.Content(text='Tennis with John 30.12.2009 15:00-16:00')
    event.quick_add = gdata.calendar.QuickAdd(value='true')
    feed = calendar_service.GetOwnCalendarsFeed()
    calurl=[a_calendar.content.src for i, a_calendar in enumerate(feed.entry)]
    new_event = calendar_service.InsertEvent(event, calurl[1]) #calurl[1] select the 2. cal of own's cals

rawDescription: How can I insert a new Event for non primary Calendar? Using python gdata

description: Insert a new Event for non primary Calendar? Using python gdata


rawDescription: python tarfile to ignore directory structure while creating tarball

description: Python tarfile to ignore directory structure while creating tarball


code:
import matplotlib
matplotlib.use('TkAgg')
import pylab
import matplotlib.pyplot as plt
from matplotlib import rc

plt.clf()
plt.rc('text', usetex=True)
plt.rcParams["figure.figsize"] = (8, 5)
plt.rc('font', **{'family':'serif', 'serif':['Computer Modern Roman'], 'size': 16})
plt.axis("off")
plt.text(0.5, 0.5, "Maths $e = mc^2$")

rawDescription: Can I integrate MathJax into a Python program?

description: Integrate MathJax into a Python program


code: json.loads('{"foo": 42, "bar": "baz"}')[u'bar']

rawDescription: How to extract data from JSON Object in Python?

description: Extract data from JSON Object in Python


code: [f + '/' if isdir(f) else f for f in files]

rawDescription: What is the [][] meaning in Python? is it pythonic?


code:
df3, _ = df2.align(df1)
df3.loc[df1.index, df1.columns] = df1
df3

rawDescription: Combine_first and null values in Pandas

description: Combine_first and null values in Pandas


code:
with open(...) as f:
    rows = csv.reader(f)
    for row in rows:
        t = row[1].split('_')
        if len(t) >= 4 and t[3] == '001':
            print (row[1], row[13])

rawDescription: Selecting rows in csv file by splitting string on specific charater in Python


code:
class UploadForm(forms.Form):
    ...
    def __init__(self, *args, **kwargs):
        super(UploadForm, self).__init__(*args, **kwargs)
        src_lang = self.initial.get('src_lang')
        if src_lang:
            choices = [(code, lang) for (code, lang) in settings.LANGUAGES
                                    if code != src_lang]
            self.fields['tgt_lang'].choices = choices
            self.fields['tgt_lang'].widget.choices = choices

rawDescription: Django form - initial value filter

description: Django form - initial value filter


code:
df = pd.DataFrame({"body": ["ball", "red BALL", "round sphere"]})
df[df["body"].str.contains("ball")]
df[df["body"].str.lower().str.contains("ball")]
df[df["body"].str.contains("ball", case=False)]
df[df["body"].str.contains("ball", case=True)]

rawDescription: Filtering pandas dataframe rows by contains str


code:
from socket import socket, AF_INET, SOCK_STREAM

sock = socket(AF_INET, SOCK_STREAM)
sock.bind(("localhost", 7777))
sock.listen(1)
while True:
    connection = None # <---
    try:
        connection, address = sock.accept()
        print("connected from ", address)
        received_message = connection.recv(300)
        if not received_message:
            break
        connection.sendall(b"hello")
    except KeyboardInterrupt:
        if connection:  # <---
            connection.close()
        break  # <---

rawDescription: can't close socket on KeyboardInterrupt [Python]

description: Can't close socket on KeyboardInterrupt [Python]


code:
import urllib2
search = urllib2.quote("@skirani")

rawDescription: How to get hastahg, username tweets using tweepy

description: Get hastahg, username tweets using tweepy


code:
from tkinter import *

def show_entry_fields():
    straal = int(e1.get())
    area = 3.143 * (straal * straal)
    calculation_label['text'] = str(area)    #using new label

master = Tk()
Label(master, text="Straal").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)

area_label = Label(master, text="Oppervlakte")
area_label.grid(row=2)
calculation_label = Label(master, text="")    #new label
calculation_label.grid(row=2, column=1)

Button(master, text='Sluit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Bereken oppervlakte', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

mainloop()

rawDescription: How can I let the Tkinter GUI not replace a string?

description: Let the Tkinter GUI not replace a string


code:
import StringIO,psycopg2,json

json_to_write = json.dumps(myobject).replace('\\','\\\\')
buffer = StringIO.StringIO(json_to_write)
cursor.copy_from(buffer,'my_table')
connection.commit()

rawDescription: psycopg2: Writing JSON objects using copy_from. How to format the json string?

description: Psycopg2: Writing JSON objects using copy_from. How to format the json string


code:
C = input("Choose your charecter to insert. ")
P = int(input("Choose your character's position. "))
S = input("Choose your string. ")

if P > len(S):
    print(S)
else:
    st = S[:P] + C + S[P:]

    print(st)
    print(C, P, S)

rawDescription: inserting characters in strings in python

description: Inserting characters in strings in python


code:
self.center[0]-self.size[0] * 0.45, self.center[1]-self.size[1] * 0.45, \
self.center[0]+self.size[0] * 0.45, self.center[1]-self.size[1] * 0.45, \
self.center[0]+self.size[0] * 0.45, self.center[1]+self.size[1] * 0.45, \
self.center[0]-self.size[0] * 0.45, self.center[1]+self.size[1] * 0.45

rawDescription: kivy - issues with canvas coordinate system (global vs. local)

description: Kivy - issues with canvas coordinate system (global vs. local)


code: data.append([int(x) for x in input().split()])

rawDescription: User input to store in nested lists with int conversion - Python


code:
class IAmAReallyComplicatedBeast(object):
    def something(self, other_stuff):
        data = self.obtain_data()
        dispatcher = {
            'something_mundane': self.do_foo,
            'something_special': self.do_bar,
            'something_really_special': self.do_baz
        }
        self.categorizer.handleData(data, dispatcher)

class Categorizer(object):
    '''Determines which category data is in.
       There may be multiple variants of this class'''
    def handleData(self, data, callback):
        if self.somethingReallySpecial(data):
            dispatcher['something_really_special'](data)

rawDescription: Python equivalent of Java inner class for delegation


code:
import urllib
unescaped_url = '%5BB%40463336a0'
escaped_url = urllib.quote(unescaped_url)
unescaped_url == urllib.unquote(escaped_url)

rawDescription: flask route variable alters uriencoded string

description: Flask route variable alters uriencoded string


code:
my_long_string = (
    "foo"
    "bar"
    "baz"
)

print(my_long_string)

rawDescription: Proper way to break up large strings

description: Proper way to break up large strings


code:
class BarForm(ModelForm):
  FOO_CHOICES = (
    (0, 'Choice1'),
    (1, 'Choice2'),
    (2, 'Choice3'),
  )
  fooempty = forms.ChoiceField(required=False, label='lbl', choices=FOO_CHOICES)
  class Meta:
    model = Bar

  def __init__(self, *args, **kwargs):
    super(BarForm, self).__init__(*args, **kwargs)
    if self.fields['foo'].queryset.count(): # Foo empty?
      self.fields['fooempty'].widget = forms.HiddenInput()
    else:
      self.emptyflag
      self.fields['foo'].widget = forms.HiddenInput()

  def save(self, commit=True):
    bar = super(BarForm, self).save(commit=False)
    if self.emptyflag:
      bar.foo_id = self.cleaned_data['fooempty']
    bar.save()
    return bar

rawDescription: django modelchoicefield with unsaved models

description: Django modelchoicefield with unsaved models


code:
import numpy as np
import matplotlib.pyplot as py
from matplotlib import animation

x = np.random.rand(10)
y = np.random.rand(10)

# animation line plot example

fig = py.figure(4)
ax = py.axes(xlim=(0, 1), ylim=(0, 1))
line, = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,

def animate(i):
    line.set_data(x[:i], y[:i])
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(x)+1,
                               interval=200, blit=False)

rawDescription: Matplotlib FuncAnimation not animating line plot


code:
# In all of the following, the list of intervals must be sorted and
# non-overlapping. We also assume that the intervals are half-open, so
# that x is in tp(start, end) iff start <= x and x < end.

def flatten(list_of_tps):
  """Convert a list of intervals to a list of endpoints"""
  return reduce(lambda ls, ival: ls + [ival.start, ival.end],
                list_of_tps,
                [])

def unflatten(list_of_endpoints):
  """Convert a list of endpoints, with an optional terminating sentinel,
     into a list of intervals"""
  return [tp(list_of_endpoints[i], list_of_endpoints[i + 1])
          for i in range(0, len(list_of_endpoints) - 1, 2)]

def merge(a_tps, b_tps, op):
  """Merge two lists of intervals according to the boolean function op"""
  a_endpoints = flatten(a_tps)
  b_endpoints = flatten(b_tps)

  sentinel = max(a_endpoints[-1], b_endpoints[-1]) + 1
  a_endpoints += [sentinel]
  b_endpoints += [sentinel]

  a_index = 0
  b_index = 0

  res = []

  scan = min(a_endpoints[0], b_endpoints[0])
  while scan < sentinel:
    in_a = not ((scan < a_endpoints[a_index]) ^ (a_index % 2))
    in_b = not ((scan < b_endpoints[b_index]) ^ (b_index % 2))
    in_res = op(in_a, in_b)

    if in_res ^ (len(res) % 2): res += [scan]
    if scan == a_endpoints[a_index]: a_index += 1
    if scan == b_endpoints[b_index]: b_index += 1
    scan = min(a_endpoints[a_index], b_endpoints[b_index])

  return unflatten(res)

def interval_diff(a, b):
  return merge(a, b, lambda in_a, in_b: in_a and not in_b)

def interval_union(a, b):
  return merge(a, b, lambda in_a, in_b: in_a or in_b)

def interval_intersect(a, b):
  return merge(a, b, lambda in_a, in_b: in_a and in_b)

rawDescription: difference of two sets of intervals

description: Difference of two sets of intervals


code: pd.merge(stops, st, on='stop_id')

rawDescription: Trouble matching and setting values from multiple dataframes


code:
h_i=input()
h=int(h_i)
s=0
q=0
total=s**2
while total<h:
     q+=total
     print(total)
     s+=1
     total=s**2
print(q)

rawDescription: Python Homework - sum of perfect squares


code:
class Empty(ndb.Model):
    pass

e = Empty(id="somestring")
e.put()

rawDescription: Is it possible to have an entity kind with no property?


code:
def power(x, y):
    if y == 0:
        return 1

    if y >= 1:
        return x * power(x, y - 1)

rawDescription: Recursive Power Function Step

description: Recursive Power Function Step


code:
def update_probabilities(self, attribute_name, decision):
    i = 0
    attr_value = getattr(self, attribute_name)
    for action, current_probability in attr_value:
        #SOME CODE HERE
        attr_value[i] = (action, new_probability)
        i = i + 1
    setattr(self, attribute_name, attr_value)

rawDescription: updating values of class attributes

description: Updating values of class attributes


code:
from matplotlib import pyplot as plt
from matplotlib_venn import venn2
plt.figure(figsize=(4,4))
v = venn2(subsets = (3, 2, 1))

plt.axhline(0, linestyle='--')
plt.axvline(0, linestyle='--')

plt.plot(-0.5,0.2,'bo')
plt.text(-0.6,0.2, 'A')

plt.show()

rawDescription: adding a labeled point to a Venn diagram in matplotlib-venn

description: Adding a labeled point to a Venn diagram in matplotlib-venn


code: dt = pandas.read_csv('file.csv', index_col=1, skiprows=1).T.to_dict()

rawDescription: Reading csv file as dictionary using pandas


code:
f = open('test124.txt', 'a') # use the append flag so that it creates the file.
f.write('testing')
f.close()

rawDescription: String won't be written to file

description: String won't be written to file


rawDescription: Multidimensional (sublist to list) assignment


rawDescription: Python: Efficient way to iterate over a list and a dictionary simultaneously containing similar values


code:
for edge in {(u,v,data) for u,v,data in G.edges_iter(data=True) if data['edge_type']=='foo'}:
    ...

rawDescription: networkx best practice getting edge attribute value while iterating over edges

description: Networkx best practice getting edge attribute value while iterating over edges


code:
from random import randint

dice = [0]*11
for i in range(100000):
    dice[randint(1,6)+randint(1,6)-2] += 1

total = sum(dice) #it is 100000, of course
for i, v in enumerate(dice, 2):
    print('{0}: {1}%'.format(i, v*100.0/total))

rawDescription: How can I simplify this code

description: Simplify this code


code:
from threading import Thread

def timer(name):
    count = 0
    while True():
        time.sleep(60)
        count += 1
        print("Hi " + name + "This program has now been running for " + str(count) + " minutes.")

print("Hello! Welcome to the program timer!")
name = raw_input("What is your name?")
print("Nice to meet you, " + name + "!")
background_thread = Thread(target=timer, args=(name,))
background_thread.start()

rawDescription: Python functions run as background processes


code:
cmd = ['convert', local_file, '-crop', '50%x50%+10+10', '-']
new_image = subprocess.check_output( cmd )
response = HttpResponse( new_image, mimetype=mt )

rawDescription: return dynamic generated file without creating tmp version?

description: Return dynamic generated file without creating tmp version


code:
Request.query.join(Request.requested_by) \
             .join(Request.requested_role) \
             .filter(or_(User.name.contains(search_query),
                         User.username.contains(search_query),
                         Role.name.contains(search_query)))

rawDescription: Checking to see if the fields of multiple ORM relationships contain a value


code:
import re
r = r"(\d+),\s*(\d+),\s*(\d+)"
re.match(r, "0, 254,10").groups()
all(0 <= int(group) <= 255 for group in re.match(r, "0, 254,10").groups())
all(0 <= int(group) <= 255 for group in re.match(r, "0,256, 190").groups())

rawDescription: Regular expression help in Python. Looking for an expression for RGB values


code:
canvas.coords
canvas.delete
canvas.itemconfigure
canvas.scale
canvas.type
...

rawDescription: Remove canvas widgets in Tkinter

description: Remove canvas widgets in Tkinter


code: myHandles = []

rawDescription: How to create array of the certain type in python

description: Create array of the certain type in python


code:
import cv2
import numpy as np

# original image
# -1 loads as-is so if it will be 3 or 4 channel as the original
image = cv2.imread('image.png', -1)
# mask defaulting to black for 3-channel and transparent for 4-channel
# (of course replace corners with yours)
mask = np.zeros(image.shape, dtype=np.uint8)
roi_corners = np.array([[(10,10), (300,300), (10,300)]], dtype=np.int32)
# fill the ROI so it doesn't get wiped out when the mask is applied
channel_count = image.shape[2]  # i.e. 3 or 4 depending on your image
ignore_mask_color = (255,)*channel_count
cv2.fillPoly(mask, roi_corners, ignore_mask_color)

# apply the mask
masked_image = cv2.bitwise_and(image, mask)

# save the result
cv2.imwrite('image_masked.png', masked_image)

rawDescription: NumPy/OpenCV 2: how do I crop non-rectangular region?


code:
from collections import Counter
Counter('abracadbra').most_common(2)
Counter([1,2,1,3,3,4]).most_common(2)

rawDescription: Is there a "freq" function in numpy/python?


code:
for i in range(n):
    cmd_1 = subprocess.Popen("./p1",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    cmd_2 = subprocess.Popen("./p2",shell = True,stdin = subprocess.PIPE,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    cmd_1.stdin.write(cmd_input[n])
    cmd_2.stdin.write(cmd_input[n])
    cmd1_stdout, cmd1_stderr = cmd_1.communicate()
    cmd2_stdout, cmd2_stderr = cmd_2.communicate()
    for line in cmd1_stdout:
        print(line.decode('ascii'))
    for line in cmd2_stdout:
        print(line.decode('ascii'))

rawDescription: How to give input multiple times to another program on python

description: Give input multiple times to another program on python


code: newlist = oldlist[i:]

rawDescription: Exporting one list into another

description: Exporting one list into another


rawDescription: What is the best way to convert a zope DateTime object into Python datetime object?


rawDescription: Devise an experiment that compares the performance of the del operator on lists and dictionaries


code:
import cloudfiles
import multiprocessing

USERNAME = '---'
API_KEY = '---'

def get_container():
    conn = cloudfiles.get_connection(USERNAME, API_KEY)
    testcontainer = conn.create_container('test')
    return testcontainer

def uploader(filenames):
    '''Worker process to upload the given files'''
    container = get_container()

    # Keep going till you reach STOP
    for filename in iter(filenames.get, 'STOP'):
        # Create the object and upload
        obj = container.create_object(filename)
        obj.load_from_filename(filename)

def main():
    NUMBER_OF_PROCESSES = 16

    # Add your filenames to this queue
    filenames = multiprocessing.Queue()

    # Start worker processes
    for i in range(NUMBER_OF_PROCESSES):
        multiprocessing.Process(target=uploader, args=(filenames,)).start()

    # You can keep adding tasks until you add STOP
    filenames.put('some filename')

    # Stop all child processes
    for i in range(NUMBER_OF_PROCESSES):
        filenames.put('STOP')

if __name__ == '__main__':
    multiprocessing.freeze_support()
    main()

rawDescription: How do I upload many files at the time to cloud files with Python?

description: Upload many files at the time to cloud files with Python


rawDescription: python regex get first part of an email address

description: Python regex get first part of an email address


code:
while playOn != "Yes":
   playOn = easygui.buttonbox ("Do you want to play again?", choices = ["Yes", "No"])
   if playOn == "Yes":
      Score = 0 #resets score count, if player wants to play again

   elif playOn == "No":
     easygui.msgbox ("Bye for now. Hope you'll play the game again soon!")

rawDescription: How to make a python program loop/repeat

description: Make a python program loop/repeat


code: {k: v for d in agents.values() for k, v in d.items()}

rawDescription: What is the best way to merge multiple dictionaries?


code:
import bpy
import bmesh

myvertexlist = [[2,2,2],[4,4,4],[6,6,6],[8,8,8]]

# Get the active mesh
me = bpy.context.object.data

# Get a BMesh representation
bm = bmesh.new()   # create an empty BMesh
bm.from_mesh(me)   # fill it in from a Mesh

# Modify the BMesh, can do anything here...
for newvert in myvertexlist:
    bm.verts.new(newvert)

# also add bm.edges and bm.faces

# Finish up, write the bmesh back to the mesh
bm.to_mesh(me)
bm.free()  # free and prevent further access

rawDescription: How to create a bmesh from a list of vertices

description: Create a bmesh from a list of vertices


code:
yourmongocoll.find({"vals":1100})
yourmongocoll.find({"$or":[ {"vals":1700}, {"vals":100}]})
yourmongocoll.find({"$and":[ {"vals":100}, {"vals":1100}]})

rawDescription: Pymongo, query on list field, and/or

description: Pymongo, query on list field, and/or


code:
df.head()
morning = df[df.index.hour < 12]
afternoon = df[df.index.hour >= 12]
morning.head()
afternoon.head()

rawDescription: python: compare two timestamp in different dates

description: Python: compare two timestamp in different dates


code:
import sys
import os
from PyQt4 import QtGui, QtCore

class TestListView(QtGui.QListWidget):
    def __init__(self, type, parent=None):
        super(TestListView, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setIconSize(QtCore.QSize(72, 72))

    def dragEnterEvent(self, event):
        if event.mimeData().hasUrls:
            event.accept()
        else:
            event.ignore()

    def dragMoveEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
        else:
            event.ignore()

    def dropEvent(self, event):
        if event.mimeData().hasUrls:
            event.setDropAction(QtCore.Qt.CopyAction)
            event.accept()
            links = []
            for url in event.mimeData().urls():
                links.append(str(url.toLocalFile()))
            self.emit(QtCore.SIGNAL("dropped"), links)
        else:
            event.ignore()

class MainForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainForm, self).__init__(parent)

        self.view = TestListView(self)
        self.connect(self.view, QtCore.SIGNAL("dropped"), self.pictureDropped)
        self.setCentralWidget(self.view)

    def pictureDropped(self, l):
        for url in l:
            if os.path.exists(url):
                print(url)
                icon = QtGui.QIcon(url)
                pixmap = icon.pixmap(72, 72)
                icon = QtGui.QIcon(pixmap)
                item = QtGui.QListWidgetItem(url, self.view)
                item.setIcon(icon)
                item.setStatusTip(url)

def main():
    app = QtGui.QApplication(sys.argv)
    form = MainForm()
    form.show()
    app.exec_()

if __name__ == '__main__':
    main()

rawDescription: PyQT4: Drag and drop files into QListWidget


code:
import httplib

images = ['img1.png', 'img2.png', 'img3.png']

conn = httplib.HTTPConnection('www.example.com')

for image in images:
    conn.request('GET', '/images/%s' % image)
    resp = conn.getresponse()
    data = resp.read()
    with open(image, 'wb') as f:
        f.write(data)

conn.close()

rawDescription: Python: Downloading files from HTTP server


code:
import os
os.path.abspath(__file__)

rawDescription: How to find the "current" source file in Python 3?

description: Find the "current" source file in Python 3


code:
text = 'Rappel de paimenet' if object.lang == 'fr_FR' else 'Payment reminder'
# Now your template doesn't need to use an if-statement
template = Template("""Status: {{text}}""").render(text=text)

rawDescription: Mako, use if / else control structure in one line


code:
from matplotlib.colors import LogNorm
from matplotlib.pyplot import *

x = np.random.normal(5,10,100000)
y = np.random.normal(5,10,100000)
counts,ybins,xbins,image = hist2d(x,y,bins=100,norm=LogNorm())
contour(counts,extent=[xbins.min(),xbins.max(),ybins.min(),ybins.max()],linewidths=3)

rawDescription: Turn hist2d output into contours in matplotlib


code:
fig = plt.figure()
with sns.axes_style("whitegrid"):
    ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

rawDescription: Changing seaborn style in subplots

description: Changing seaborn style in subplots


code:
import rpy2.robjects.numpy2ri as rpyn
vector=rpyn.ri2numpy(vector_R)

rawDescription: rpy2: Convert FloatVector or Matrix back to a Python array or list?

description: Rpy2: Convert FloatVector or Matrix back to a Python array or list


code:
class Parent: pass
class Child (Parent): pass

Parent.Child = Child
del Child

rawDescription: Python: How can I subclass a class I'm nesting in?


code:
date = '01.01.2011'

ffilter = "%s > CAST('%s' AS DATE)" %(str(ft), date)
q = q.filter(ffilter)

rawDescription: sqlalchemy dates filter in raw sql

description: Sqlalchemy dates filter in raw sql


code:
a_key = ndb.Key(A, 'mykey')
aext_key = ndb.Key(AEXT, 'mykey')

rawDescription: GOOGLE APP ENGINE - NDB - KEY

description: GOOGLE APP ENGINE - NDB - KEY


rawDescription: how to use variables in os.rename

description: Use variables in os.rename


code: urllib.quote("Tabula Rasa")

rawDescription: Prepare a string for Google Ajax Search?

description: Prepare a string for Google Ajax Search


code:
from collections import defaultdict

def partition(seq, key):
    d = defaultdict(list)
    for x in seq:
        d[key(x)].append(x)
    return d

rawDescription: Simple way to group items into buckets

description: Simple way to group items into buckets


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

# Generate some data
x = np.linspace(0, 20, 200)
y = np.cos(x) + 0.5 * np.sin(3 * x)

fig, ax = plt.subplots()
ax.plot(x, y)

ax.annotate(r'$y = cos(x) + \frac{sin(3x)}{2}$', xy=(x[70], y[70]),
            xytext=(20, 10), textcoords='offset points', va='center',
            arrowprops=dict(arrowstyle='->'))
plt.show()

rawDescription: matplotlib/pandas: put line label along the plotted lines in time series plot

description: Matplotlib/pandas: put line label along the plotted lines in time series plot


code:
class_method = A.calc
class_method.__class__
class_method.__func__
class_method.im_class
class_method.im_class == A
class_method.im_class.__module__

rawDescription: Identify class module from a class function


rawDescription: Matplotlib graph placement

description: Matplotlib graph placement


code: passthru('/usr/python/bin/python3 ../cgi-bin/tabular.py 1 2>&1');

rawDescription: When calling a Python script from a PHP script, temporary file that is created on a console run, is not created via the PHP invocation


code: [y for x in range(3) for y in [x, x]]

rawDescription: Repeating elements in list comprehension


code:
from datetime import datetime
l = [['28/02/2014: apples'], ['09/07/2014: oranges'], ['22/04/2014: bananas'], ['14/03/2014: tomatoes']]
sorted(l, key=lambda x: datetime.strptime(x[0].split(':')[0], '%d/%m/%Y'))

rawDescription: Ordering a list of tuples in python

description: Ordering a list of tuples in python


code:
# binary-indexed tree, allows point updates and prefix sum queries
class Fenwick:
  def __init__(self, n):
    self.tree = [0]*(n+1)
    self.n = n
  def update_point(self, i, val):  # O(log n)
    i += 1
    while i <= self.n:
      self.tree[i] += val
      i += i & -i
  def read_prefix(self, i):        # O(log n)
    i += 1
    sum = 0
    while i > 0:
      sum += self.tree[i]
      i -= i & -i
    return sum

def solve(a):
  rank = { v : i for i, v in enumerate(sorted(a)) }
  res = []
  counts, sums = Fenwick(len(a)), Fenwick(len(a))
  total_sum = 0
  for i, x in enumerate(a):
    r = rank[x]
    num_smaller = counts.read_prefix(r)
    sum_smaller = sums.read_prefix(r)
    res.append(total_sum - 2*sum_smaller + x * (2*num_smaller - i))
    counts.update_point(r, 1)
    sums.update_point(r, x)
    total_sum += x
  return res

print(solve([3,5,6,7,1]))  # [0, 2, 4, 7, 17]
print(solve([2,0,1]))      # [0, 2, 2]

rawDescription: sum of absolute differences of a number in an array

description: Sum of absolute differences of a number in an array


code:
# configure logging
# log a message
# delete log file

logging.shutdown()

# configure logging
# log a message (log file will be recreated)

rawDescription: reset python interpreter for logging

description: Reset python interpreter for logging


code:
# Python list
a = [1, 2, 3]
a.append(1)
# => [1, 2, 3, 1]

# Numpy array
import numpy as np
a = np.array([1, 2, 3])
a = np.append(a, 1)
# => [1, 2, 3, 1]

rawDescription: Python/Numpy: How do you assign the end+1 element of an array similar to how it's done in Matlab?


code:
import Tkinter as tk
class MyDialog(object):
    def __init__(self, parent):
        self.toplevel = tk.Toplevel(parent)
        self.var = tk.StringVar()
        label = tk.Label(self.toplevel, text="Pick something:")
        om = tk.OptionMenu(self.toplevel, self.var, "one", "two","three")
        button = tk.Button(self.toplevel, text="OK", command=self.toplevel.destroy)
        label.pack(side="top", fill="x")
        om.pack(side="top", fill="x")
        button.pack()

    def show(self):
        self.toplevel.deiconify()
        self.toplevel.wait_window()
        value = self.var.get()
        return value

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.button = tk.Button(self, text="Click me!", command=self.on_click)
        self.label = tk.Label(self, width=80)
        self.label.pack(side="top", fill="x")
        self.button.pack(pady=20)

    def on_click(self):
        result = MyDialog(self).show()
        self.label.configure(text="your result: %s" % result)

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

rawDescription: Creating a Tkinter class and waiting for a return value


code:
from xml.dom import minidom
content = """
<xml>
    <Item>
          <Cantidad>1</Cantidad>
          <UniMed>L</UniMed>
    </Item>
</xml>
"""
root = minidom.parseString(content)
insert_tag = root.createElement("tag2")
htext = root.createTextNode('test')
insert_tag.appendChild(htext)

items = root.getElementsByTagName("Item")
item = items[0]
item_chidren = item.childNodes
item.insertBefore(insert_tag, item_chidren[2])
root.toxml()

rawDescription: Insert after x node minidom xml python

description: Insert after x node minidom xml python


code:
from matplotlib import rc
rc('text', usetex=True)
rc('font', size=20)

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

fig = plt.figure(figsize=(10, 6))
ax = fig.add_subplot(111)
ax.semilogx(range(100))
ax.xaxis.set_major_formatter(ScalarFormatter())
plt.show()

rawDescription: Matplotlib: disable powers of ten in log plot


code:
from scipy.sparse import csr_matrix
X = csr_matrix([[0, 0, 1], [2, 3, 0]])
X
X.toarray()
print(X)

rawDescription: Understanding format of data in scikit-learn


code:
i_indices, j_indices = zip(*adj_list)
adj_matrix = scipy.sparse.coo_matrix((np.ones(number_of_nodes),
                                     (i_indices, j_indices)))

rawDescription: Connected components in a graph with 100 million nodes


code:
import klepto
@klepto.inf_cache(cache=klepto.archives.dir_archive(cached=False))
def func1(x, y, name='func1'):
  return x+y

@klepto.inf_cache(cache=klepto.archives.dir_archive(cached=False))
def func2(x, y, name='func2'):
  return x-y

func1(1,2)
func1(1,3)
func2(2,4)
func2(1,2)
func1.__cache__()

func1(1,2)
func1(1,2)
func2(1,2)

func1.__cache__() == func2.__cache__()

rawDescription: use klepto to cache by function name and args

description: Use klepto to cache by function name and args


code:
from itertools import cycle
word = cycle('gcse')
message = 'Hello, my name is Jake'
''.join([next(word) for c in message])

rawDescription: Offset key WORD for cipher in Python

description: Offset key WORD for cipher in Python