code:
from collections import deque
from itertools import *
import pygame
from random import randrange
import sys
from pygame.locals import *

class Vector(tuple):
    """A tuple that supports some vector operations.

    >>> v, w = Vector((1, 2)), Vector((3, 4))
    >>> v + w, w - v, v * 10, 100 * v, -v
    ((4, 6), (2, 2), (10, 20), (100, 200), (-1, -2))
    """
    def __add__(self, other): return Vector(v + w for v, w in zip(self, other))
    def __radd__(self, other): return Vector(w + v for v, w in zip(self, other))
    def __sub__(self, other): return Vector(v - w for v, w in zip(self, other))
    def __rsub__(self, other): return Vector(w - v for v, w in zip(self, other))
    def __mul__(self, s): return Vector(v * s for v in self)
    def __rmul__(self, s): return Vector(v * s for v in self)
    def __neg__(self): return -1 * self

FPS = 60                        # Game frames per second
SEGMENT_SCORE = 50              # Score per segment

SNAKE_SPEED_INITIAL = 4.0       # Initial snake speed (squares per second)
SNAKE_SPEED_INCREMENT = 0.25    # Snake speeds up this much each time it grows
SNAKE_START_LENGTH = 4          # Initial snake length in segments

WORLD_SIZE = Vector((20, 20))   # World size, in blocks
BLOCK_SIZE = 24                 # Block size, in pixels
WORLD_BOUNDARIES = False

BACKGROUND_COLOR = 45, 45, 45
SNAKE_COLOR = 0, 255, 0
FOOD_COLOR = 255, 0, 0
DEATH_COLOR = 255, 0, 0
TEXT_COLOR = 255, 255, 255

DIRECTION_UP    = Vector(( 0, -1))
DIRECTION_DOWN  = Vector(( 0,  1))
DIRECTION_LEFT  = Vector((-1,  0))
DIRECTION_RIGHT = Vector(( 1,  0))
DIRECTION_DR    = DIRECTION_DOWN + DIRECTION_RIGHT

# Map from PyGame key event to the corresponding direction.
KEY_DIRECTION = {
    K_w: DIRECTION_UP,    K_UP:    DIRECTION_UP,
    K_s: DIRECTION_DOWN,  K_DOWN:  DIRECTION_DOWN,
    K_a: DIRECTION_LEFT,  K_LEFT:  DIRECTION_LEFT,
    K_d: DIRECTION_RIGHT, K_RIGHT: DIRECTION_RIGHT,
}

class Snake(object):
    def __init__(self, start, start_length):
        self.speed = SNAKE_SPEED_INITIAL # Speed in squares per second.
        self.timer = 1.0 / self.speed    # Time remaining to next movement.
        self.growth_pending = 0          # Number of segments still to grow.
        self.direction = DIRECTION_UP    # Current movement direction.
        self.segments = deque([start - self.direction * i
                             for i in xrange(start_length)])

    def __iter__(self):
        return iter(self.segments)

    def __len__(self):
        return len(self.segments)

    def change_direction(self, direction):
        """Update the direction of the snake."""
        # Moving in the opposite direction of current movement is not allowed.
        if self.direction != -direction:
            self.direction = direction

    def head(self):
        """Return the position of the snake's head."""
        return self.segments[0]

    def tail(self):
        """Return the tail of the snake."""
        return deque(islice(self.segments, 1, None))

    def update(self, dt, direction):
        """Update the snake by dt seconds and possibly set direction."""
        self.timer -= dt
        if self.timer > 0:
            # Nothing to do yet.
            return
        # Moving in the opposite direction of current movement is not allowed.
        if self.direction != -direction:
            self.direction = direction
        self.timer += 1 / self.speed

        '''************************************************************************************
        ***************************************************************************************
        ***************************************************************************************'''
        # Add a new head.
        if not WORLD_BOUNDARIES:
            head = self.head()
            if self.direction == DIRECTION_DOWN and head[1] == WORLD_SIZE[1]-1:
                self.segments.appendleft(Vector((head[0], 0)))

            elif self.direction == DIRECTION_UP and head[1] == 0:
                self.segments.appendleft(Vector((head[0], WORLD_SIZE[1]-1)))

            elif self.direction == DIRECTION_RIGHT and head[0] == WORLD_SIZE[0]-1:
                self.segments.appendleft(Vector((0, head[1])))

            elif self.direction == DIRECTION_LEFT and head[0] == 0:
                self.segments.appendleft(Vector((WORLD_SIZE[0]-1, head[1])))

            else: self.segments.appendleft(self.head() + self.direction)
        else: self.segments.appendleft(self.head() + self.direction)

        '''************************************************************************************
        ***************************************************************************************
        ***************************************************************************************'''

        if self.growth_pending > 0:
            self.growth_pending -= 1
        else:
            # Remove tail.
            self.segments.pop()

    def grow(self):
        """Grow snake by one segment and speed up."""
        self.growth_pending += 1
        self.speed += SNAKE_SPEED_INCREMENT

    def self_intersecting(self):
        """Is the snake currently self-intersecting?"""
        it = iter(self)
        head = next(it)
        return head in it

class SnakeGame(object):
    def __init__(self):
        pygame.display.set_caption('PyGame Snake')
        self.block_size = BLOCK_SIZE
        self.window = pygame.display.set_mode(WORLD_SIZE * self.block_size)
        self.screen = pygame.display.get_surface()
        self.clock = pygame.time.Clock()
        self.font = pygame.font.Font('freesansbold.ttf', 20)
        self.world = Rect((0, 0), WORLD_SIZE)
        self.reset()

    def reset(self):
        """Start a new game."""
        self.playing = True
        self.next_direction = DIRECTION_UP
        self.score = 0
        self.snake = Snake(self.world.center, SNAKE_START_LENGTH)
        self.food = set()
        self.add_food()

    def add_food(self):
        """Ensure that there is at least one piece of food.
        (And, with small probability, more than one.)
        """
        while not (self.food and randrange(4)):
            food = Vector(map(randrange, self.world.bottomright))
            if food not in self.food and food not in self.snake:
                self.food.add(food)

    def input(self, e):
        """Process keyboard event e."""
        if e.key in KEY_DIRECTION:
            self.next_direction = KEY_DIRECTION[e.key]
        elif e.key == K_SPACE and not self.playing:
            self.reset()

    def update(self, dt):
        """Update the game by dt seconds."""
        self.snake.update(dt, self.next_direction)

        # If snake hits a food block, then consume the food, add new
        # food and grow the snake.
        head = self.snake.head()
        if head in self.food:
            self.food.remove(head)
            self.add_food()
            self.snake.grow()
            self.score += len(self.snake) * SEGMENT_SCORE

        # If snake collides with self or the screen boundaries, then
        # it's game over.
        if self.snake.self_intersecting() or not self.world.collidepoint(self.snake.head()):
            self.playing = False

    def block(self, p):
        """Return the screen rectangle corresponding to the position p."""
        return Rect(p * self.block_size, DIRECTION_DR * self.block_size)

    def draw_text(self, text, p):
        """Draw text at position p."""
        self.screen.blit(self.font.render(text, 1, TEXT_COLOR), p)

    def draw(self):
        """Draw game (while playing)."""
        self.screen.fill(BACKGROUND_COLOR)
        for p in self.snake:
            pygame.draw.rect(self.screen, SNAKE_COLOR, self.block(p))
        for f in self.food:
            pygame.draw.rect(self.screen, FOOD_COLOR, self.block(f))
        self.draw_text("Score: {}".format(self.score), (20, 20))

    def draw_death(self):
        """Draw game (after game over)."""
        self.screen.fill(DEATH_COLOR)
        self.draw_text("Game over! Press Space to start a new game", (20, 150))
        self.draw_text("Your score is: {}".format(self.score), (140, 180))

    def play(self):
        """Play game until the QUIT event is received."""
        while True:
            dt = self.clock.tick(FPS) / 1000.0 # convert to seconds
            for e in pygame.event.get():
                if e.type == QUIT:
                    return
                elif e.type == KEYUP:
                    self.input(e)
            if self.playing:
                self.update(dt)
                self.draw()
            else:
                self.draw_death()
            pygame.display.flip()

if __name__ == '__main__':

    pygame.init()
    SnakeGame().play()
    pygame.quit()

rawDescription: Dissable boundaries in a python snake game


code:
import numpy as np
from bokeh.plotting import output_file, figure, gridplot, show
from bokeh.models import ColumnDataSource, Circle

N = 100
max = 100
x1 = np.random.random(size = N) * max
y1 = np.random.random(size = N) * max
a1 = np.random.choice(a = [0.2, 0.5, 1], size = N)
x2 = np.random.random(size = N) * max
y2 = np.random.random(size = N) * max
a2 = np.random.choice(a = [0.2, 0.5, 1], size = N)

source = ColumnDataSource(data = dict(x1 = x1, y1 = y1, x2 = x2, y2 = y2,
                                      a1 = a1, a2 = a2, a1n = a1 * 0.5, a2n=a2*0.5))

left = figure(tools = 'box_select, tap', width = 400, height = 400,
              x_range = (0,100), y_range = (0,100))
right = figure(tools = 'box_select, tap', width = 400, height = 400,
               x_range = (0,100), y_range = (0,100))

points1 = left.circle('x1', 'y1', source = source, size = 10,
                      fill_color = 'blue', line_color = None, alpha = 'a1')
points2 = right.circle('x2', 'y2', source = source, size = 10,
                       fill_color = 'blue', line_color = None, alpha = 'a2')
points1.selection_glyph = Circle(fill_color = 'red', line_color = None)
points2.selection_glyph = Circle(fill_color = 'red', line_color = None)
points1.nonselection_glyph.fill_alpha = "a1n"
points2.nonselection_glyph.fill_alpha = "a2n"
p = gridplot([[left, right]])
show(p)

rawDescription: Is there a way to have multiple opacities for nonselection glyph in bokeh?


code:
import os
os._exit(3)

rawDescription: Killing Python webservers

description: Killing Python webservers


code:
from datetime import datetime

query = '2008-10-07'
nbOfDays = datetime.strptime(query, '%Y-%m-%d').toordinal() + 365

rawDescription: Handling dates prior to 1970 in a repeatable way in MySQL and Python


code:
class ViewPerso (QtGui.QTableView):
    def __init__(self, parent = None):
        super(ViewPerso, self).__init__(parent)
        self.parent = parent

    def currentChanged (self, current, previous):
        index = current.sibling(current.row(), 4)
        try:
            if not index.data() or len(self.parent.vidsSelected()) > 1:
                return
        except AttributeError:
            pass
        self.clicked.emit(current)
        self.scrollTo(current)

    def keyboardSearch (self, search):
        pass

    def keyPressEvent (self, e):
        super(ViewPerso, self).keyPressEvent(e)
        e.ignore()

rawDescription: QTableView intercepts Key_Down and Key_Up events


code: matches = filter(None, map(findit, out))

rawDescription: filter and map in one? (Python)

description: Filter and map in one? (Python)


code:
Meta = type("Meta", (), {"model": Consumer})
TestForm = type("TestForm", (djangoforms.ModelForm,), {"Meta": Meta})

rawDescription: python creating a class at runtime containing another class

description: Python creating a class at runtime containing another class


code:
def proc_init():
    random.seed(str(os.getpid()) + str(time.time()))

pool = Pool(num_procs, initializer=proc_init)

rawDescription: Multiprocessing, pooling and randomness

description: Multiprocessing, pooling and randomness


code: root.bind('<Return>', (lambda e, b=b: b.invoke())) # b is your button

rawDescription: tkinter default button in a widget

description: Tkinter default button in a widget


code:
wind = np.random.randn(100) + np.random.randn(100).cumsum() * 0.5
x = np.arange(len(wind))
y = wind
t = y

fig, ax = plt.subplots(2,1,figsize=(10,6))

ax[0].plot(x,y)

ax[1].plot(x, 100- y * 10, lw=2, c='black')

ymin, ymax = ax[1].get_ybound()
xmin, xmax = ax[1].get_xbound()

im = ax[1].imshow(y.reshape(1, y.size), extent=[xmin,xmax,ymin,ymax], interpolation='none', alpha=.5, cmap=plt.cm.RdYlGn_r)
ax[1].set_aspect(ax[0].get_aspect())

cax = fig.add_axes([.95,0.3,0.01,0.4])
cb = plt.colorbar(im, cax=cax)
cb.set_label('Y parameter [-]')

rawDescription: Colormap entire subplot

description: Colormap entire subplot


code:
my_list = [11, 22, 33]
my_keys = ['var1', 'var2', 'var3']

my_dict = dict(zip(my_keys, my_list))

rawDescription: Convert a list of variables to a dictionary of varName / varValue pairs


code:
instance = form_data.save(commit=False)
# do some stuff with instance
instance.save()

rawDescription: django: create object from constructor or model form

description: Django: create object from constructor or model form


code:
from bs4 import BeautifulSoup
import urllib2

url = "http://example.com/releases"
html = urllib2.urlopen(url).read()

soup = BeautifulSoup(html)

last_link = soup.find_all('a', href=True)[-1]

latest_content = urllib2.urlopen(last_link['href']).read()
# do stuff

rawDescription: How to get latest file using "urllib2" by reading html directory in python

description: Get latest file using "urllib2" by reading html directory in python


code:
def sample_given_size(population, population_size, sample_size):
    for item in population:
        if random.random() < sample_size / population_size:
            yield item
            sample_size -= 1
        population_size -= 1

rawDescription: Iterative or Lazy Reservoir Sampling

description: Iterative or Lazy Reservoir Sampling


code:
def getLineNumber(fileName, searchString):
    with open(fileName) as f:
        for i,line in enumerate(f, start=1):
           if searchString in line:
               return i
    raise Exception('string not found')

rawDescription: Python Search file for a string and get the string's line number


code:
from google.appengine.api import search
...
def get_all_in_index(index_name):
    """Get all the docs in the given index."""
    results = []
    doc_index = search.Index(name=index_name)

    # looping because get_range by default returns up to 100 documents at a time
    while True:
        # Get a list of documents populating only the doc_id field and extract the ids.
        document_ids = [document.doc_id
                        for document in doc_index.get_range(ids_only=True)]
        if not document_ids:
            break
        # Get the documents for the given ids from the Index.
        results.append(doc_index.get_range(document_ids))

rawDescription: Python App Engine Full Text Search > empty search return all documents of a search.Index


code:
cost = input("What is the cost in dollars? ")
money = input("Money given: ")
change = int(round(money - cost) * 100)

if change < 0:
    print("You didn't pay enough!")
    exit()

dollars = change / 100
change %= 100

quarters = change / 25
change %= 25

dimes = change / 10
change %= 10

nickels = change / 5
change %= 5

pennies = change

print("""Your change is:
    %i dollars
    %i quarters
    %i dimes
    %i nickels
    %i pennies
""" % (dollars, quarters, dimes, nickels, pennies))

rawDescription: Odd behavior of int(x) in python

description: Odd behavior of int(x) in python


code:
import pymongo
pymongo.has_c()
import bson
bson.has_c()

rawDescription: checking pymongo driver details

description: Checking pymongo driver details


code:
def after(u, v):
    # return sign of cross product
    return u[0]*v[1]<u[1]*v[0]

rawDescription: Fastest way to find the rotation of a vector


code:
from Tkinter import *
import random
root = Tk()

letters = [random.choice('BCDFGHJKMPQRTVWXYYZ')  for x in range(19)]
numbers = [random.choice('2346789') for x in range(6)]
s = letters + numbers
random.shuffle(s)
s = ''.join(s)

# Rather than use global variables, which is generally a bad idea, we can make a callback creator function, which takes the formerly global variables as arguments, and simply uses them to create the callback function.
def makeCallback(sVariable):
    def callback():
# This will set the text of the entry
        sVar.set(s)
    return callback

# Use a StringVar to alter the text in the Entry
sVar = StringVar(root)
# You can use an Entry for this, but it seems like a Label is more what you're looking for.
Code = Entry(root, state='readonly', textvariable=sVar)

# Create a callback function
callback = makeCallback(sVar)

Code.grid(row=0, pady=20)
generate=PhotoImage(file='generate.gif')
G = Button(root, image=None , command=callback, compound=CENTER)
G.grid(row=1, padx=206.5, pady=20)

rawDescription: Connect a message box to a button

description: Connect a message box to a button


code:
class FixtureSuite(unittest.TestSuite):
    def run(self, result, debug=False):
        socket.setdefaulttimeout(30)
        super().run(result, debug)
        socket.setdefaulttimeout(None)
...
suite1 = unittest.TestSuite(testCases)
suite2 = FixtureSuite(testCases)
fullSuite = unittest.TestSuite([suite1,suite2])
unittest.TextTestRunner(verbosity=2).run(fullSuite)

rawDescription: python unittests with multiple setups?

description: Python unittests with multiple setups


code:
ids_l1 = set(x.id for x in l1)  # All ids in list 1
intersection = [item for item in l2 if item.id in ids_l1]  # Only those elements of l2 with an id in l1

rawDescription: python set intersection with object sets

description: Python set intersection with object sets


code:
import cgi
cgi.escape('<&>')

rawDescription: Convert & to & in Python

description: Convert & to & in Python


code:
import re

with open('book.txt', 'r') as file:
    txt = file.readlines()

f = False

for line in txt:
    if re.match("^[A-Z]+$", line):
        if f: f.close()
        f = open(line + '.txt', 'w')

    else:
        f.write(line + "\n")

rawDescription: Parsing a book into chapters – Python

description: Parsing a book into chapters – Python


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

np.random.seed(101)
g = np.floor(np.random.random((100, 100)) + .5)

plt.subplot(211)
plt.imshow(g)
plt.subplot(212)
plt.imshow(g, cmap='Greys',  interpolation='nearest')
plt.savefig('blkwht.png')

plt.show()

rawDescription: Plot a black-and-white binary map in matplotlib


code: links = table.xpath(".//a[contains(@href, 'http://www.example.com/filter/')]")

rawDescription: Python: Using xpath locally / on a specific element


code:
import numpy as np
import sympy as sp

# preparation
n = sp.symbols("n")
F = sp.factorial

# create the equation
f = (F(n) + F(n + 1)) / F(n - 2)
print(f)               # => (factorial(n) + factorial(n + 1))/factorial(n - 2)

# reduce it
f = f.simplify()
print(f)               # => n*(n - 1)*(n + 2)

# evaluate it in SymPy
# Note: very slow!
print(f.subs(n, 6))    # => 240

# turn it into a numpy function
# Note: much faster!
f = sp.lambdify(n, f, "numpy")
a = np.arange(2, 10)
print(f(a))            # => [  8  30  72 140 240 378 560 792]

rawDescription: Computing factorials efficiently with Python and Numpy


code:
dates = pd.to_datetime(data['Dates'])
date_df = pd.DataFrame(dict(
    year=dates.dt.year,
    month=dates.dt.month,
    day=dates.dt.day,
    # etc.
))
xtrn[:, :5] = date_df.values  # use date[['year', 'month', 'day', etc.]] if the order comes out wrong

rawDescription: Map pandas dataframe column to a matrix

description: Map pandas dataframe column to a matrix


code:
def get_new_failures(list1, list2):
    check = set([(d['classname'], d['testname']) for d in list2])
    return [d for d in list1 if (d['classname'], d['testname']) not in check]

rawDescription: Comparing lists of dictionaries

description: Comparing lists of dictionaries


code:
Bulk = dict()
for i in range(LNew-2):
    for j in range(LNew-2):
        if i == j:
            Bulk[(i,j)]=H2
        else:
            Bulk[(i,j)]=idm

rawDescription: numpy fill an array with arrays

description: Numpy fill an array with arrays


code:
from math import *
import numpy as np
import matplotlib.pyplot as plt

k = range(11)

yvals = [[] for i in range(len(k))] #there should be a more pythonic way to
x = np.arange(0.4,5,0.1)               #create nested lists i think, ALM I added np.

for i in k:
    for j in x:
        i = float(i) #this conversion from int to float was to debug,
        j = float(j) #it should be made better
        y=1/(2*j**2) + i/(2*j**2)
        i = int(i)
        yvals[i].append(y)

for i in k:
    plt.plot(x,yvals[i])

plt.show()

rawDescription: learning python multipe plots on the same set of axis

description: Learning python multipe plots on the same set of axis


code: import base.dir2.b as b

rawDescription: Python cyclical import between peer nodes in a graph


code:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

pd.set_option('display.expand_frame_repr', False)

cols = ['ID', 'address', 'used_at']
df = pd.read_csv(r'D:\data\gDrive\data\.stack.overflow\data\avito_trend.csv.gz',
                 parse_dates=['used_at'], usecols=cols)

df.sort_values(['ID','used_at','address'], inplace=True)

df['prev_address'] = df['address'].shift()
df['time_diff'] = df['used_at'] - df['used_at'].shift()

df = df[df['address'] != df['prev_address']]
df = df[df['time_diff'] <= pd.Timedelta('10min')]

tmp = df[['ID','address','prev_address']] \
      .groupby(['address','prev_address', df.used_at.dt.year]) \
      .count() \
      .reset_index()

# remove `df` from memory
del df

tmp['visit_from'] = tmp['prev_address'] + ' -> ' + tmp['address']

# keep only 'interesting' columns
tmp = tmp[['visit_from','used_at','ID']]
tmp.columns = ['visit_from','year','visits']

# save temporary groupped CSV file
#fn = r'D:\data\gDrive\data\.stack.overflow\data\avito_grp.csv'
#tmp.to_csv(fn, index=False)

# show all
#df = tmp

# show only those sites with visits >= 100 (within both years)
df = tmp[tmp.groupby('visit_from')['visits'].transform('sum') >= 100].reset_index()

# prepare sorted index
idx = df.groupby('visit_from')['visits'].transform('sum').sort_values(ascending=False).index

# 'apply' index
df = df.reindex(idx)

# add 'total' column (sum of visits for all years)
#df['total'] = df.groupby('visit_from')['visits'].transform('sum')

################################################
#
# SeaBorn plotting
#
sns.set(style="darkgrid")
sns.set_color_codes("pastel")

f, ax = plt.subplots(figsize=(16, 12))
ax = sns.barplot(x='visits', y='visit_from', hue='year', data=df, saturation=0.8)
plt.xlabel('Visits')

# add annotations
[ax.text(p.get_width() + 3, p.get_y() + p.get_height(),
         int(p.get_width()), fontsize=8)
 for p in ax.patches]

plt.show()

rawDescription: Barchart horizontal using matplotlib

description: Barchart horizontal using matplotlib


code:
def func(list_name):
    for i in list_name:
        if isinstance(i, list):
            print(i)

if __name__ == "__main__":
    a = [1, 2, "asdf", [1, 2, 3], {"q": 1}]
    func(a)

rawDescription: Check for nested lists

description: Check for nested lists


code:
from selenium import webdriver
driver = webdriver.Firefox()
print (driver.current_url)

rawDescription: get current url from browser python

description: Get current url from browser python


code:
assert str is not bytes

from lib_socks_proxy_2013_10_03 import monkey_patch as socks_proxy_monkey_patch

socks_proxy_monkey_patch.monkey_patch()

import io
import re

import time

import stem.connection
import stem.socket
import stem.process
from stem import Signal
from stem.control import Controller
from stem.util import term

from urllib import request as req
from lib_socks_proxy_2013_10_03 import socks_proxy_context

import config

tor_process = []
_ = config.variables

def startTor(i):
    print(" - Starting TOR Process...")

    tor_process.extend([stem.process.launch_tor_with_config(
        config={
            'SocksPort':str(9050+i),
            'ControlPort':str(9070+i),
            'CookieAuthentication':str(1),
            'HashedControlPassword':str(_.TOR_HASHED_PASS_),
            'DataDirectory':'data/tor'+str(i),
            'PidFile':'tor'+str(i)+'.pid',
                        },
        init_msg_handler = print_bootstrap_lines,
        )])

def print_bootstrap_lines(line):
    if("Bootstrapped " in line):
        print(line)

def checkIP(i):
    print("[+] Checking IP Address...")
    try:
        opener = req.build_opener()

        with socks_proxy_context.socks_proxy_context(proxy_address=('127.0.0.1', 9050+i)):
           res = opener.open('https://internet.yandex.com/get_full_info/', timeout=20.0)

        data = res.read(10000).decode()
        print(data)
    except:
        print(" - Failed.")

def changeIP(i):
    print("[+] Changing IP Address... ["+str(i)+"]")
    with Controller.from_port(port = 9070+i) as controller:
        controller.authenticate(_.TOR_PASS_)
        controller.signal(Signal.NEWNYM)

## Test 3 nodes.

if(__name__=="__main__"):
    for x in range(0,3):
        startTor(x)
        checkIP(x)
    for x in range(0,3):
        changeIP(x)
    for x in range(0,3):
        checkIP(x)
        tor_process[x].kill()

rawDescription: Multiple TOR Exit Nodes - Controller Not Working?


code:
from urlparse import urlsplit, parse_qs

s = '/?user=hello&user=man&user='
parse_qs(urlsplit(s).query, keep_blank_values=True)
{'user': ['hello', 'man', '']}

rawDescription: Find 'hello', 'man' and '' in '/?user=hello&user=man&user=' using regex


code:
import csv

data = open("murtuz.txt").readlines()
outfile = open("myfile.csv","w")
out = csv.writer(outfile)
for row in data:
    row = row.strip().split(';')
    if row:
        for subrow in row:
            subrow = subrow.strip().split()
            if subrow:
                out.writerow(subrow)
outfile.close()

rawDescription: how to write in next row using python?

description: Write in next row using python


code:
ifile_name = "hello/input.txt"
ofile_name = "hello/output.txt"
ifile_name = open(ifile_name, 'r')
lines = ifile_name.readlines()
ofile_name = open(ofile_name, "w")

lines[-1] = lines[-1].rstrip() + '\n'
start = 0
end = len(lines) - 1
while start < end:
    ofile_name.write(lines[start])
    ofile_name.write(lines[end])
    start += 1
    end -= 1
if start == end:
    ofile_name.write(lines[start])
ifile_name.close()
ofile_name.close()

rawDescription: Re-ordering text file - Python

description: Re-ordering text file - Python


code:
string = [('limited', 1), ('all', 16), ('concept', 1), ('secondly', 1)]
my_dict = dict(string)
my_dict

rawDescription: Python convert pairs list to dictionary

description: Python convert pairs list to dictionary


code:
import random
import numpy
from matplotlib import pyplot

x = [random.gauss(3,1) for _ in range(400)]
y = [random.gauss(4,2) for _ in range(400)]

bins = numpy.linspace(-10, 10, 100)

pyplot.hist(x, bins, alpha=0.5, label='x')
pyplot.hist(y, bins, alpha=0.5, label='y')
pyplot.legend(loc='upper right')
pyplot.show()

rawDescription: Plot two histograms at the same time with matplotlib


code:
class Thread(QThread):
  dataReady = Signal(object)

  def run(self):
    while True:
      self.data = slowly_produce_data()
      # this will add a ref to self.data and avoid the destruction
      self.dataReady.emit(self.data)

class Widget(QWidget):
  def __init__(self):
    self.thread = Thread()
    self.thread.dataReady.connect(self.get_data, Qt.QueuedConnection)
    self.thread.start()

  def get_data(self, data):
    self.data = data

  def paintEvent(self, event):
    paint_somehow(self.data)

rawDescription: Communication between threads in PySide

description: Communication between threads in PySide


code:
alphabet = u'AĄBCĆDEĘFGHIJKLŁMNŃOÓPRSŚTUWYZŹŻaąbcćdeęfghijklłmnńoóprsśtuwyzźż'
dsort = dict((let, i) for i, let in enumerate(alphabet))

def key_utf8(utf8_string):
  s = utf8_string.decode('utf8')
  return map(dsort.get, s)

some_list_of_utf8_strings.sort(key=key_utf8)

rawDescription: How to sort utf-8 string on Google App Engine in Python?

description: Sort utf-8 string on Google App Engine in Python


code:
a = legendre(3,np.linspace(0,1,4))
np.vstack(a)

rawDescription: How to use numpy.frompyfunc to return an array of elements instead of array of arrays?

description: Use numpy.frompyfunc to return an array of elements instead of array of arrays


code:
def set_hour_for_date(customer, hour_str, transaction_date):
    meridian = hour_str[-2:]
    hour = int(hour_str[:2])
    if meridian == 'PM':
        hour = hour + 12
    relative_time = customer.time_zone.localize(datetime(
                                                        transaction_date.year,
                                                        transaction_date.month,
                                                        transaction_date.day,
                                                        hour,
                                                        0,
                                                        0,
                                                        294757))

    return relative_time

rawDescription: how to get hour integer from python datetime.time field

description: Get hour integer from python datetime.time field


code:
import pandas as pd # use recent version which has dt.quarter attr for time
import json
value = 'value' # to be able to read your Data string as json
Data1 = json.dumps(Data) # need it to use read_json() method.
a = pd.read_json(Data1)
a[a['date'].dt.quarter == 1].mean() # 1st quarter
a[a['date'].dt.quarter == 2].mean() # 2nd quarter
a[a['date'].dt.quarter == 3].mean() # 3rd quarter

rawDescription: How to group by quarter and calculate average from an array using numpy?

description: Group by quarter and calculate average from an array using numpy


code:
OPTIONS=('ON', 'OFF', )
"OPTIONS="+repr(OPTIONS)

rawDescription: Python: how to write a data struct to a file as text (not pickled)


code:
from pyspark.sql.functions import expr
from pyspark import HiveContext

sqlContext = HiveContext(sc)
df = sqlContext.createDataFrame(rdd)

df.groupBy("x").agg(expr("collect_list(y) AS y"))

rawDescription: In PySpark 1.5.0, how do you list all items of column `y` based on the values of column `x`?


code:
class A(object): ...
class B(A): ...
b = B()

super(B, b).get_value = Something()  # no
b.get_value = Something()  # ok
B.get_value = Something()  # ok
A.get_value = Something()  # ok

rawDescription: python - unittesting super() calls

description: Python - unittesting super() calls


code:
data = {'myid': '123'}
form = MyModelForm(data=data, instance=None)
form.is_valid()
if form.instance.pk:
     print('This has a pk! It was saved before!')
else:
     print('This has no pk! It was never saved!')

rawDescription: Test if Django ModelForm has instance on customized model


code:
fig, axes = plt.subplots(nrows=2, ncols=3)
spfvL = [6, 11, 22, 33, 44, 55]
for j, i in enumerate(spfvL):
    df['spfv' + str(i)] = pd.rolling_std(df['r VIX'], i) * np.sqrt(252)
    res = smf.ols(formula='spfv'+ str(i)+' ~ Q(\'VIX Index\')', data=df).fit()
    df['pred'+ str(i)] = better_predict(res, df)
    df.loc[:, ['pred' + str(i), 'spfv' + str(i)]].plot(ax=axes.flat[j])

rawDescription: pandas subplots in a loop

description: Pandas subplots in a loop


code:
import numpy as np
arr = np.random.random([10, 5])
sp = np.random.binomial(1, arr)
sp

rawDescription: Generate samples from a random matrix

description: Generate samples from a random matrix


code:
import atexit
import sys
import logging

log = logging.getLogger('test')
log.setLevel(logging.DEBUG)

console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter("%(message)s"))
log.addHandler(console_handler)

def shutdown_logging():
    logging.shutdown()

atexit.register(shutdown_logging)

def main(argv=None):
    log.info('Starting something...')
    log.info('Doing something...')
    log.info('Finished something.')

if __name__=='__main__':
    sys.exit(main(sys.argv[1:]))

rawDescription: Python logging in interactive sessions

description: Python logging in interactive sessions


code:
from django.core.management import setup_environ
from django.core.mail import EmailMultiAlternatives, send_mail
from django.contrib.auth.models import User

import settings
from my.app.models import *

setup_environ(settings)

rawDescription: Easiest way to write a Python program with access to Django database functionality


code:
s = ['status', 'value', 'place', 'tag']
s_sorted = sorted(s, key=lambda x:(x!='tag', x))

rawDescription: Sorting a list of string in python such that a specific string, if present appears first


code:
import re
s = '<td casd2" aasdeft" class="satyle3"><b><a asddidasd?ct=Peasds&fasdaao=Monsdar&pID=19635"...'
match = re.search(".*pID=(\d+).*",s)
if match:
  match.group(1)

rawDescription: Python Divide a string

description: Python Divide a string


code:
[i.find('ul', {'class': "definitionList"}).find('li').text
 for i in soup.find_all('div', {'class': "contentBlurb"})
 if 'Description Synonyms' in str(i.text)][0]

rawDescription: How to extracts contents of a div tag containing a particular text using BeautifulSoup

description: Extracts contents of a div tag containing a particular text using BeautifulSoup


code:
def OnShowPopup(self, event):
   """ Obtain right-click selection """
   pos = event.GetPosition()
   pos = self.panel.ScreenToClient(pos)
   self.rightClickSelected = event.GetEventObject()
   self.panel.PopupMenu(self.rightClickMenu, pos)

def OnPaste(self, event = None):
   """ Paste content from clipboard """
   self.rightClickSelected.SetFocus()
   if isinstance(self.rightClickSelected, wx.TextCtrl):
      self.rightClickSelected.Paste()

rawDescription: wxPython: Right-click with mouse does not set focus

description: WxPython: Right-click with mouse does not set focus


code:
from decimal import Decimal, Context
ctx = Context(prec=20)
two = Decimal(2)
ctx.divide(ctx.power(two, Decimal(31)).ln(ctx), two.ln(ctx))

rawDescription: Inaccurate Logarithm in Python

description: Inaccurate Logarithm in Python


code: int_val_list = [int(value) for value in frames[f]]

rawDescription: INT function not working and I'm trying to remove quotes from a list of numbers


code:
def flatten(iterable):
    """Recursively iterate lists and tuples.
    """
    for elm in iterable:
        if isinstance(elm, (list, tuple)):
            for relm in flatten(elm):
                yield relm
        else:
            yield elm

rawDescription: Making a flat list out of a multi-type nested list


code:
promotion_items = {
    'one': {'remaining_pcs': 100, 'discount_ratio': 10},
    'two': {'remaining_pcs': 200, 'discount_ratio': 20},
}
def bypcs(k):
  return promotion_items[k]['remaining_pcs']
byrempcs = sorted(promotion_items, key=bypcs)
def bydra(k):
  return promotion_items[k]['discount_ratio']
bydiscra = sorted(promotion_items, key=bydra)

rawDescription: Sort by key of dictionary inside a dictionary in Python


code:
root.xpath('//html:div[@class="countdown closed"]/@data-utime',
  namespaces={'html': 'http://www.w3.org/1999/xhtml'})

rawDescription: Finding attribute value using lxml without using a for loop


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

img_A = np.ones((10,10))
img_B = np.ones((10,10))

plot_image = np.concatenate((img_A, img_B), axis=1)

plt.imshow(plot_image)

rawDescription: Plotting two images side by side in python


code:
def myfunc(p1, p2, p3):
    """myfunc does something interesting.

    some more detail. See :meth:`my_other_func` for more information.

    :param p1: The first parameter.
    :type p1: string
    :param p2: The second parameter.
    :param p3: The third parameter.
    :returns: True if successful, False if not.
    """

    my_code(p1)
    more_code(p2)
    return third_part(p1,p2,p3)

rawDescription: Documenting my Django code

description: Documenting my Django code


code:
from tkinter import *
root = Tk()
text = Text(root)
text.insert("end", "Hello world, how are you?")
text.pack(fill="both", expand=1)
text.get("1.0", "1.4")
text.get("1.4", "end")
text.get("2.0", "end")  # because there's just one line
text.get("1.7", "end-1c")  # without the newline at the end

rawDescription: How to slice and access text with TkInter's indices?

description: Slice and access text with TkInter's indices


code:
class C0(object):

  def func0(self, arg): # added self
    ...

  def func1(self, rdd): # added self
    func = self.func0
    result = rdd.map(lambda x: func(x))

rawDescription: PySpark serializing the 'self' referenced object in map lambdas?


code:
mdr = fix.Message()
mdr.getHeader().setField(fix.BeginString(fix.BeginString_FIX44))
mdr.getHeader().setField(fix.MsgType(fix.MsgType_MarketDataRequest))

mdr.setField(fix.MDReqID('1'))
mdr.setField(fix.SubscriptionRequestType(fix.SubscriptionRequestType_SNAPSHOT_PLUS_UPDATES))
mdr.setField(fix.MarketDepth(0))
mdr.setField(fix.NoMDEntryTypes(2))
mdr.setField(fix.MDUpdateType(fix.MDUpdateType_INCREMENTAL_REFRESH))

group = fixnn.MarketDataRequest().NoMDEntryTypes()
group.setField(fix.MDEntryType(fix.MDEntryType_BID))
mdr.addGroup(group)
group.setField(fix.MDEntryType(fix.MDEntryType_OFFER))
mdr.addGroup(group)

mdr.setField(fix.NoRelatedSym(len(new_pairs)))

symbol = fixnn.MarketDataRequest().NoRelatedSym()
for pair in new_pairs:
    symbol.setField(fix.Symbol(pair))
    mdr.addGroup(symbol)

fix.Session.sendToTarget(mdr, sessionID)

rawDescription: quickfix python marketdatarequest

description: Quickfix python marketdatarequest


code:
lst = [{"id": 14, "name": "cat14", "parent_id": 13, "path_info": [8, 10, 12, 13, 14]}, {"id": 15, "name": "cat15", "parent_id": 13, "path_info": [8, 10, 12, 13, 15]}]

master_dct = { d['id'] : d for d in lst}
for d in lst:
    d['subcats'] = [{field : master_dct[i][field] for field in ['id', 'name', 'parent_id']} \
        for i in d['path_info'] if i in master_dct]

import json
with open('out.json', 'w') as f:
    json.dump(lst, f)

rawDescription: Create hierarchical json dump from list of dictionary in python


code: data[0]['f'] = var

rawDescription: Add element to a json in python

description: Add element to a json in python


rawDescription: How to remove duplicating character from beginning of a string

description: Remove duplicating character from beginning of a string


code:
import requests
from bs4 import BeautifulSoup

s = requests.session()
r = s.get('http://www.jstor.org/stable/pdf/10.1086/512825.pdf')
soup = BeautifulSoup(r.content)
pdfurl = 'http://www.jstor.org' + soup.find('a', id='acptTC')['href']
with open('export.pdf', 'wb') as handle:
    response = s.get(pdfurl, stream=True)
    for block in response.iter_content(1024):
        if not block:
            break
        handle.write(block)

rawDescription: python-requests does not grab JSESSIONID and SessionData cookies

description: Python-requests does not grab JSESSIONID and SessionData cookies


code:
seen = set()
result = []
for d in dicts:
    h = d.copy()
    h.pop('date')
    h = tuple(h.items())
    if h not in seen:
        result.append(d)
        seen.add(h)
pprint(result)

rawDescription: Remove duplicates from a list of dictionaries when only one of the key values is different


code:
# coding: utf-8

import random

import pygame

SIZE = 800,600

def tile_texture(texture, size):
    result = pygame.Surface(size, depth=32)
    for x in range(0, size[0], texture.get_width()):
        for y in range(0, size[1], texture.get_height()):
            result.blit(texture,(x,y))
    return result

def apply_alpha(texture, mask):
    """
    Image should be  a 24 or 32bit image,
    mask should be an 8 bit image with the alpha
    channel to be applied
    """
    texture = texture.convert_alpha()
    target = pygame.surfarray.pixels_alpha(texture)
    target[:] = pygame.surfarray.array2d(mask)
    # surfarray objets usually lock the Surface.
    # it is a good idea to dispose of them explicitly
    # as soon as the work is done.
    del target
    return texture

def stamp(image, texture, mask):
    image.blit(apply_alpha(texture, mask), (0,0))

def main():
    screen = pygame.display.set_mode(SIZE)
    screen.fill((255,255,255))
    texture = tile_texture(pygame.image.load("texture.png"), SIZE)
    mask = pygame.Surface(SIZE, depth=8)
    # Create sample mask:
    pygame.draw.polygon(mask, 255,
                        [(random.randrange(SIZE[0]), random.randrange(SIZE[1]) )
                         for _ in range(5)] , 0)

    stamp(screen, texture, mask)
    pygame.display.flip()
    while not any(pygame.key.get_pressed()):
        pygame.event.pump()
        pygame.time.delay(30)

if __name__ == "__main__":
    pygame.init()
    try:
        main()
    finally:
        pygame.quit()

rawDescription: Texturing drawn shapes - python pygame

description: Texturing drawn shapes - python pygame


code:
INSTALLED_APPS += [
    'djcelery',
]
try:
    import flower.command
    import celery.bin.celery
except Exception as e:
    logging.error("Flower will not be available: %s"%e)
else:
    celery.bin.celery.CeleryCommand.register_command(
          flower.command.FlowerCommand, name='flower')

rawDescription: Adding a Python Entry Point without Running setuptools


code:
def solution(n):
    q = [(1,)]
    visited = set()
    for seq in q:
        s = sum(seq)
        if s == n:
            return seq
        elif s > n:
            continue
        key = (seq[-1], s)
        if key in visited:
            continue
        visited.add(key)
        q.append(seq + (seq[-1] * 2,))
        q.append(seq + (seq[-1] + 1,))
    return None

rawDescription: Shortest subset for given sum and fastest solution in Python


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

def plot_matrix(rm, title='Robot World', cmap=plt.cm.Blues):
    plt.imshow(rm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.tight_layout()
    plt.show()

cmap = colors.ListedColormap(['k','b','y','g','r'])
bounds=[0,1,2,3,4]
norm = colors.BoundaryNorm(bounds, cmap.N)

rm = np.random.randint(0,4,(5,5))
plot_matrix(rm,cmap=cmap)

rawDescription: How to map integers to colors in matplotlib?

description: Map integers to colors in matplotlib


rawDescription: Extracting part of a string with regex

description: Extracting part of a string with regex


code:
from bisect import bisect_left
secondData[bisect_left(secondData, "8:33:00"):bisect_left(secondData, "8:34:00")]

rawDescription: Best method for intersecting a time list with a time range list


code:
from ast import literal_eval

s = "[1, 'house', 3]"
l = literal_eval(s)
print(l)
# [1, 'house', 3]

rawDescription: How to convert a list into string and this string back to the initial list?

description: Convert a list into string and this string back to the initial list


code:
import threading
import Queue

QUEUE_SIZE = 1000
sentinel = object()

def read_file(name, queue):
    with open(name) as f:
        for line in f:
            queue.put(line)
    queue.put(sentinel)

def process(inqueue, outqueue):
    for line in iter(inqueue.get, sentinel):
        outqueue.put(do_something(line))
    outqueue.put(sentinel)

def write_file(name, queue):
    with open(name, "w") as f:
        for line in iter(queue.get, sentinel):
            f.write(line)

inq = Queue.Queue(maxsize=QUEUE_SIZE)
outq = Queue.Queue(maxsize=QUEUE_SIZE)

threading.Thread(target=read_file, args=(source_filename, inq)).start()
threading.Thread(target=process, args=(inq, outq)).start()
write_file(target_filename, outq)

rawDescription: How to parse a large file taking advantage of threading in Python?

description: Parse a large file taking advantage of threading in Python


code:
from itertools import count
zip(count(idx_start), list_a, list_b)

rawDescription: Merge items of two lists into list of tuples that also contains index


code:
import numpy
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable

data = numpy.random.random((10, 10))

fig = plt.figure()

ax1 = fig.add_subplot(1,2,1, aspect = "equal")
ax2 = fig.add_subplot(1,2,2, aspect = "equal")

im1 = ax1.pcolormesh(data)
im2 = ax2.pcolormesh(data)

divider1 = make_axes_locatable(ax1)
cax1 = divider1.append_axes("right", size="5%", pad=0.05)

divider2 = make_axes_locatable(ax2)
cax2 = divider2.append_axes("right", size="5%", pad=0.05)

#Create and remove the colorbar for the first subplot
cbar1 = fig.colorbar(im1, cax = cax1)
fig.delaxes(fig.axes[2])

#Create second colorbar
cbar2 = fig.colorbar(im2, cax = cax2)

plt.tight_layout()

plt.show()

rawDescription: Adding a colorbar to two subplots with equal aspect ratios


code:
# we'll use this to collate partial data
data = ""

while 1:
    # receive the next batch of data
    data += s.recv(BUFFER_SIZE).decode('utf-8')

    # split the data into lines
    lines = data.splitlines(keepends=True)

    # the last of these may be a part line
    full_lines, last_line = lines[:-1], lines[-1]

    # print (or do something else!) with the full lines
    for l in full_lines:
        print(l, end="")

    # was the last line received a full line, or just half a line?
    if last_line.endswith("\n"):
        # print it (or do something else!)
        print(last_line, end="")

        # and reset our partial data to nothing
        data = ""
    else:
        # reset our partial data to this part line
        data = last_line

rawDescription: Python Socket is receiving inconsistent messages from Server


code:
import requests
from lxml import etree

url = 'http://www.websitetoaccess.com'
res1 = requests.get(url)
# do something with res1.content
# you could try parsing the html page with lxml
root = etree(res1.content, etree.HTMLParser())
# do something with root, find question and calc answer?
post_params = {'answer': str(42), 'submit': 'Submit'}
res2 = requests.post(url, data=post_params)
# check res2 for success or content?

rawDescription: Sending POST request on an already open url in python


code:
def rmblock(path, block):
    lines = open(path).readlines()
    blockstart = lines.index(block + "\n")
    blockend = lines.index(r"\n" + "\n", blockstart)
    del(lines[blockstart:blockend+1])
    open(path, 'w+').writelines(lines)

rawDescription: Removing a block of lines from a txt file


code:
from PIL import Image
import numpy as np, time

im_array = np.random.rand(10,10,3)
pad = 4
padded_array  = np.pad(im_array, ((pad,pad+1),(pad,pad+1),(0,0)), 'constant')
for i in xrange(im_array.shape[0]):
    for j in xrange(im_array.shape[1] ):
       temp_im = padded_array[i:i+10, j:j+10]
       # print temp_im.shape
       if i == 0 and j == 0:
        new_im = temp_im[np.newaxis,...]
       else:
        new_im = np.vstack([new_im, temp_im[np.newaxis,...]])

rawDescription: how can I efficiently pad a RGB pixel to make it the center pixel of a resulting image using numpy

description: Efficiently pad a RGB pixel to make it the center pixel of a resulting image using numpy


code:
from multiprocessing.dummy import Pool # use threads

def func1(url):
    # download url here
    return url, "result", None

def func2(url):
    # do something with the url unsuccessfully
    return url, None, "describe the error"

pool = Pool(20) # no more than 20 concurrent connections
results_for_func1 = pool.map(func1, urls_func1) # it blocks until done
results_for_func2 = pool.map(func2, urls_func2)

rawDescription: execute multiple functions in parallel

description: Execute multiple functions in parallel


code:
def rotateAndScale(img, scaleFactor = 0.5, degreesCCW = 30):
    (oldY,oldX) = img.shape #note: numpy uses (y,x) convention but most OpenCV functions use (x,y)
    M = cv2.getRotationMatrix2D(center=(oldX/2,oldY/2), angle=degreesCCW, scale=scaleFactor) #rotate about center of image.

    #choose a new image size.
    newX,newY = oldX*scaleFactor,oldY*scaleFactor
    #include this if you want to prevent corners being cut off
    r = np.deg2rad(degreesCCW)
    newX,newY = (abs(np.sin(r)*newY) + abs(np.cos(r)*newX),abs(np.sin(r)*newX) + abs(np.cos(r)*newY))

    #the warpAffine function call, below, basically works like this:
    # 1. apply the M transformation on each pixel of the original image
    # 2. save everything that falls within the upper-left "dsize" portion of the resulting image.

    #So I will find the translation that moves the result to the center of that region.
    (tx,ty) = ((newX-oldX)/2,(newY-oldY)/2)
    M[0,2] += tx #third column of matrix holds translation, which takes effect after rotation.
    M[1,2] += ty

    rotatedImg = cv2.warpAffine(img, M, dsize=(int(newX),int(newY)))
    return rotatedImg

rawDescription: Python 2.7.3 + OpenCV 2.4 after rotation window doesn't fit Image


code:
import re

s = "some string ['foo'] [b a r] [baz] [] extra stuff"

pat0 = re.compile(r'\[([^]]*)\]')

lst0 = pat0.findall(s)

lst1 = [s.replace(' ', '') for s in lst0]
lst2 = [s.replace("'", '') for s in lst1]

print(lst2) # prints: ['foo', 'bar', 'baz', '']

rawDescription: Convert sed with multiple expressions to python


code:
class Node(db.Model):
   # some properties and stuff

   @classmethod
   def active_nodes(cls):
       return cls.all().filter('active = ',True)

rawDescription: Transparent logical filter on entities - App Engine


code:
def get_query(self):
    role = current_user.role
    if role == 'contributor':
        return # filtered query
    elif role == 'admin':
        return # unfiltered query

rawDescription: Pre-filter readable data based on user permissions with Flask-Admin


code:
with open('file.csv') as csvfile:
        reader = csv.reader(csvfile,delimiter=' ')
        reader.next()
        reader.next()
        for row in reader:
                a = [float(i) for i in row if i!='']
        if a[1]>=-13 and a[1]<=-1:
            print (a[0],a[1])

rawDescription: reading in certain rows from a csv file using python

description: Reading in certain rows from a csv file using python


code:
from bs4 import BeautifulSoup
from urllib import request

page = request.urlopen('http://www.federalreserve.gov/econresdata/researchdata/feds200628_1.html').read()
soup = BeautifulSoup(page)

desired_table = soup.findAll('table')[2]

# Find the columns you want data from
headers = desired_table.findAll('th')
desired_columns = []
for th in headers:
    if 'SVENY' in th.string:
        desired_columns.append([headers.index(th), th.getText()])

# Iterate through each row grabbing the data from the desired columns
rows = desired_table.findAll('tr')

for row in rows[1:]:
    cells = row.findAll('td')
    row_name = row.findNext('th').getText()
    for column in desired_columns:
        print(cells[column[0]].text, row_name, column[1])

rawDescription: Scraping with BeautifulSoup: want to scrape entire column including header and title rows


rawDescription: BeautifulSoup: Can't convert NavigableString to string


code:
g2 = g.copy()
g2.contract_vertices(membership)
g2.simplify(combine_edges={"weight": sum})

rawDescription: collapsing graphs according to a common parameter in iGraph [Python]

description: Collapsing graphs according to a common parameter in iGraph [Python]


code: stmt = "SELECT * from myTable where id=:idVar AND pct LIKE :pctVar ESCAPE '\\'"

rawDescription: escape character for sqlalchemy using execute()

description: Escape character for sqlalchemy using execute()


code:
import matplotlib as plt

f = plt.figure()
r = f.canvas.get_renderer()
t = plt.text(0.5, 0.5, 'test')

bb = t.get_window_extent(renderer=r)
width = bb.width
height = bb.height

rawDescription: Matplotlib text dimensions

description: Matplotlib text dimensions


code:
def wraps_with_first_line(f):
    def wrap(wrapper):
        wrapper = wraps(f)(wrapper)
        if not hasattr(wrapper, '_first_line'):
            wrapper._first_line = inspect.getsourcelines(f)[1]
        return wrapper
    return wrap

rawDescription: extending the functionality of the functools.wraps decorator

description: Extending the functionality of the functools.wraps decorator


code:
from google.appengine.ext import ndb

ndb.delete_multi(
    Game.query().fetch(keys_only=True)
)

rawDescription: How to delete all entities for NDB Model in Google App Engine for python?

description: Delete all entities for NDB Model in Google App Engine for python


rawDescription: Python str.strip() with regex filtering unexpected characters


code:
from PIL import Image
from PIL import ImageFilter

im = Image.open("plot.png")
im = im.filter(ImageFilter.BLUR)
im.save("saved.jpg")

rawDescription: How do I change my image format?

description: Change my image format


code:
class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        http = tornado.httpclient.AsyncHTTPClient()
        http.fetch("http://friendfeed-api.com/v2/feed/bret",
                   callback=self.async_callback(self.on_response))

    def on_response(self, response):
        if response.error: raise tornado.web.HTTPError(500)
        json = tornado.escape.json_decode(response.body)
        self.write("Fetched " + str(len(json["entries"])) + " entries "
                   "from the FriendFeed API")
        self.finish()

rawDescription: Best (or appropriate) WSGI server for this Python script? - Python


code:
# myapp/apps.py
from django import VERSION as DJANGO_VERSION
if DJANGO_VERSION >= (1, 7):
    from django.apps import AppConfig
    class MyAppConfig(AppConfig):
        ...

# myapp/__init__.py
from django import VERSION as DJANGO_VERSION
if DJANGO_VERSION >= (1, 7):
    default_app_config = 'myapp.apps.MyAppConfig'

rawDescription: Is there a recommended approach for handling AppConfig when designing an app for Django 1.6 and 1.7?


rawDescription: Parsing a string representing a float *with an exponent* in Python


code: plt.scatter(xs[:,0], xs[:,1], c=list('rk')) #r is red, k is black

rawDescription: Matplotlib: how to plot colored points without looping?


code:
reobj = re.compile(r"""
    # Match (unreliably) Python function with self reference.
    ^                        # Anchor to start of line.
    ([ \t]*)                 # $1: Indentation of DEF statement.
    def[ \t]+                # Function definition.
    ([^\s(]+)                # $2: Name of function to find.
    .*\r?\n                  # Remainder of function def line.
    (?:                      # Zero or more lines w/o funcname.
      (?:                    # Function block lines alternatives.
        \1[ \t]+             # Func block lines have extra indentation.
        (?:(?!\b\2\s*\().)*  # Optional non-funcname stuff on line
      | [ \t]*\#.*           # Allow comment lines to defy indent rules.
      )?                     # Allow blank lines in function block.
      \r?\n                  # End of line not containing funcname.
    )*                       # Zero or more lines w/o funcname
    \1[ \t]+                 # Now match the line having funcname.
    (?:(?!\b\2\s*\().)*      # Optional non-funcname stuff on line
    \b\2\s*\(                # Match the function self reference.
    """, re.MULTILINE | re.VERBOSE)

rawDescription: How to use regex to find functions call themself?

description: Use regex to find functions call themself


code:
def f(x=None):
    if x is None:
        x = {}

rawDescription: What is the most Pythonic way to use an empty dictionary as a default argument?


code: list1 = [i for i in list1 if not(i%3==0)]

rawDescription: How to filter a list with relevant elements left only

description: Filter a list with relevant elements left only


code:
object_instance = Object.objects.get(pk=pk)
object_instance.special_value = SomeBusinessLogic(data_to_aggregate)

rawDescription: Adding a value to an Object retrieved via a QuerySet


code:
from paste.deploy.loadwsgi import appconfig
config = appconfig('config:development.ini', 'myapp', relative_to='.')

if config['env'] == 'production':
    api_endpoint_uri = 'http://api.external.com/?{0}'
    timezone = timezone('US/Eastern')
# .. and so on ...

rawDescription: How to get Registry().settings during Pyramid app startup time?

description: Get Registry().settings during Pyramid app startup time


code:
x = numpy.random.rand(10,10)
numpy.savetxt('test.out', x, delimiter=',')
dataa = numpy.genfromtxt('test.out',delimiter=",", dtype=None)
if dataa.dtype.names is None:#then dataa is homogenous?
  l1 = map(lambda z:('f%d'%(z),dataa.dtype),range(0,dataa.shape[1]))
  dataa.dtype = dtype(l1)

dataa.dtype
dataa.dtype.names

rawDescription: genfromtxt - Force column name generation for unknown number of columns

description: Genfromtxt - Force column name generation for unknown number of columns


code:
for grp in sys.argv[1:]:
  for i in range(len(sh.col_values(8))):
   if sh.cell(i, 1).value == grp:
      hlo.append(sh.cell(i, 8).value)

rawDescription: How to test for multiple command line arguments (sys.argv

description: Test for multiple command line arguments (sys.argv


code:
self.signal_video_framerate_variable = StringVar()
self.signal_video_framerate_combobox = ttk.Combobox(self.signal_standard_label_frame,
                                       textvariable = self.signal_video_framerate_variable,
                                       width = 15)
self.signal_video_framerate_combobox.state(['readonly'])

rawDescription: How to shorten the length of Tkinter Cobobox in python

description: Shorten the length of Tkinter Cobobox in python


code:
import requests

key = u'your_api_key'
url = u'https://www.googleapis.com/fusiontables/v1/tables/tableId/styles/styleId?key=%s' % key

body = dict()
body[u'markerOptions.iconName'] = u'geographic_features'
# put other settings here

r = request.put(url, data=body)

rawDescription: Fusion Tables API: How do I make a PUT request to update a style via Ruby, JavaScript, or other languages?


code:
wanted_size = 21
a = numpy.array([[1,2,3],[4,5,6],[7,8,9]])
b = numpy.zeros((wanted_size, wanted_size))

for i in range(wanted_size):
    for j in range(wanted_size):
        idx1 = i * len(a) / wanted_size
        idx2 = j * len(a) / wanted_size
        b[i][j] = a[idx1][idx2]

rawDescription: Python: Shrink/Extend 2D arrays in fractions


code:
from collections import defaultdict
reversed_dict = defaultdict(list)
for key,value in mydict.iteritems():
    reversed_dict[value].append(key)

rawDescription: How to reverse a dictionary that it has repeated values (python)

description: Reverse a dictionary that it has repeated values (python)


code:
import string
string.letters
import random
random.choice(string.letters)

rawDescription: Generate a random letter in Python

description: Generate a random letter in Python


code:
#!/usr/bin/env python
from __future__ import print_function
import os
import time
from threading import Thread
from Queue import Queue

try: import psutil # pip install psutil
except ImportError:
    psutil = None

def f(queue):
    while True:
        item = queue.get() # block until an item is available
        print("got %s" % (item,))
        break # end thread

# create threads
q = Queue()
threads = [Thread(target=f, args=(q,)) for _ in xrange(1000)]

# starts them
for t in threads:
    t.daemon = True # die with the program
    t.start()

# show cpu load while the threads are blocked on `queue.get()`
if psutil is None:
    print('Observe cpu load yourself (or install psutil and rerun the script)')
    time.sleep(10) # observe cpu load
else:
    p = psutil.Process(os.getpid())
    for _ in xrange(10):
        print("cpu %s%%" % (p.get_cpu_percent(interval=0),))
        time.sleep(1)

# finish threads
for i in range(len(threads)):
    q.put_nowait(i) #note: queue is unlimited so there is no reason to wait

for t in threads: t.join() # wait for completion
print('done')

rawDescription: CPU load of a Python threadpool

description: CPU load of a Python threadpool


code: secondtable = soup.findAll('table')[1]

rawDescription: BeautifulSoup in Python - getting the n-th tag of a type


rawDescription: Having problems with precision in python


code:
from kombu import Exchange, Queue, binding

exchange = Exchange('experiment', type='topic')
q = Queue(exchange=exchange, bindings=[
    binding(exchange, routing_key='foo.#'),
    binding(exchange, routing_key='bar.#')
], exclusive=True)

rawDescription: Using Kombu ConsumerMixin, how to declare multiple bindings?


rawDescription: Pyspark vectorize a collectionRDD of dictionaries into a single dataframeRDD?


rawDescription: Is there a more succinct for this date checking?


code:
import Queue
import threading

number_of_API_readers = 10

parameters_queue = Queue.Queue()
results_queue = Queue.Queue(100)

for par in list_of_parameters:
    parameters_queue.put(par)

def read_from_queue_and_write_to_db():
    while True:
        try:
            result = results_queue.get(timeout=30)
            #write result to DB
        except Queue.Full:
            break

def query_API_and_write_to_queue():
    while True:
        try:
            par = parameters_queue.get(timeout=5)
            result = SomeAPI.call(par)
            results_queue.put(result)
        except Queue.Empty:
            break

db_writer = threading.Thread(target=read_from_queue_and_write_to_db)
api_readers = [threading.Thread(target=query_API_and_write_to_queue)
                for i in range(number_of_API_readers)]

db_writer.start()
for ar in api_readers:
    ar.start()

rawDescription: Runing concurrent API calls

description: Runing concurrent API calls


code:
import numpy as np
from itertools import chain

def fun(i):
    return tuple(4*i + j for j in range(4))

a = np.fromiter(chain.from_iterable(fun(i) for i in range(5)), 'i', 5 * 4)
a.shape = 5, 4

print(repr(a))
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11],
#        [12, 13, 14, 15],
#        [16, 17, 18, 19]], dtype=int32)

rawDescription: Reconcile np.fromiter and multidimensional arrays in Python


code:
class MyHandler(tornado.web.RequestHandler):
    def get(self, *args, **kwargs):
        components = [x for x in self.request.path.split("/") if x]
        # results in ['myroute', 'something']

rawDescription: How do I get access to parts of my url/path in a tornado request handler

description: Get access to parts of my url/path in a tornado request handler


code:
lst = [1,2,3,10]
dictionary = {}
def test(x):
    for xx in range(100):
        for xxx in range(100):
            dictionary[x]=xx*xxx

for x in lst:
    test(x)

rawDescription: How can I reference a outside variable from within a function?

description: Reference a outside variable from within a function


code:
pk = open( 'public_key.pem', 'rb' ).read()
rsa = M2Crypto.RSA.load_pub_key(pk)

rawDescription: How to load an RSA key from a PEM file and use it in python-crypto

description: Load an RSA key from a PEM file and use it in python-crypto


code:
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        subprocess.call(something)
        # block until finished (depending on application)

rawDescription: When press button, python script runs automatically on Raspberry Pi


code:
store = pd.HDFStore('test.h5',mode='w')
store['foo'] = DataFrame(np.random.randn(10,2))
store['bar'] = DataFrame(np.random.randn(10,2))
store
'bar' in store
'foo' in store
'/foo' in store
'bah' in store

rawDescription: Check if key is in HDF5Store without path


code:
def flatten_multi_index(df):
    mi = df.columns
    suffixes, prefixes = mi.levels
    col_names = [prefixes[i_p] + '_' + suffixes[i_s] for (i_s, i_p) in zip(*mi.labels)]
    df.columns = col_names
    return df

flatten_multi_index(pd.pivot_table(ts,index='month', columns='company'))

rawDescription: pivoting pandas dataframe into prefixed cols, not a MultiIndex

description: Pivoting pandas dataframe into prefixed cols, not a MultiIndex


code: match=gene.match(line, re.IGNORECASE)

rawDescription: Regular expression in python is skipping first 2 characters of line


code: {k:v for k,v in json.loads(string_where_json_was_readed) if k not in keys_to_strip_list}

rawDescription: Is there a way to remove a json key/value using python and simplejson/json


code:
from django.template.defaultfilters import slugify
slugify("Something with a space in it")

rawDescription: How to create a CSS identifier from an arbitrary string with python?

description: Create a CSS identifier from an arbitrary string with python


code:
class MakeSlots(type):

    def __new__(cls, name, bases, attrs):
        attrs['__slots__'] = attrs.keys()

        return super(MakeSlots, cls).__new__(cls, name, bases, attrs)

class A(object):
    one = 1
    two = 2

    __metaclass__ = MakeSlots

rawDescription: Finding the static attributes of a class in Python


rawDescription: Selecting form input with Selenium isn't working?


code:
connection = boto.ec2.connect_to_region('eu-west-1', \
                                    aws_access_key_id='yourkey', \
                                    aws_secret_access_key='yoursecret', \
                                    proxy=yourProxy, \
                                    proxy_port=yourProxyPort)

# This is a way of fetching the image object for an AMI, when you know the AMI id
# Since we specify a single image (using the AMI id) we get a list containing a single image
# You could add error checking and so forth ... but you get the idea
images = connection.get_all_images(image_ids=['ami-cf86xxxx'])
images[0].deregister()

rawDescription: How to delete an AMI using boto?

description: Delete an AMI using boto


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

rawDescription: django several settings for local and production

description: Django several settings for local and production


code:
from datetime import datetime
from itertools import chain

my_date = datetime.date(2011, 10, 1)
old_data = OldData.objects.filter(date_gte=my_date)
current_data = CurrentData.objects.filter(date_gte=my_date)
all_data = sorted(chain(old_data, new_data), key=lambda x: x.date)

rawDescription: combine query from multiple tables in django

description: Combine query from multiple tables in django


rawDescription: how to Label the edges of a graph using python and graphviz


code:
from django.contrib.auth.models import AbstractUser

class MyUserModel(AbstractUser):
    # AbstractUser has already every fields needed: username, password, is_active...
    def myfunc(self):
        # Just a dummy working example
        return "My Username in uppercase: %s" % self.username.upper()

rawDescription: Add class method after definition in a Python / Django model


code:
import binascii
binascii.hexlify('foo'.encode('utf8'))
binascii.unhexlify(_).decode('utf8')

rawDescription: What's the correct way to convert bytes to a hex string in Python 3?


code:
def scoreEvalFromMafLine(mafLine):
    theScore, theEval = None, None
    for word in mafLine.split():
        if word.startswith("score="):
            theScore = word.split('=')[1]
        if word.startswith("expect="):
            theEval  = word.split('=')[1]

    if theScore is None or theEval is None:
        raise Exception("Invalid line: '%s'" % line)

    return (theScore, theEval)

rawDescription: Parsing line with delimiter in Python

description: Parsing line with delimiter in Python


code:
import pkg_resources
my_stream = pkg_resources.resource_stream(__name__, "foo.dat")

rawDescription: Using package files from script

description: Using package files from script


code:
#Create and Place Text Field

def on_variable_trace(*args):
    if entryVariable2.get() == "":
        entryWidget3.configure(state="disable")
    else:
        entryWidget3.configure(state="normal")

entryVariable2 = StringVar()
entryWidget2= Entry(textFrame, textvariable=entryVariable2)
entryWidget2["width"] = 30
entryWidget2.pack(side="top")
entryVariable2.trace("w", on_variable_trace)

rawDescription: Tkinter continuous check for text

description: Tkinter continuous check for text


code:
rows = soup.findAll('tr')
for r in rows:
    print("row:", r.name)
    for c in r.children:
        print("---")
        print(type(c))
        print(repr(c))

rawDescription: Why does BeautifulSoup .children contain nameless elements as well as the expected tag(s)


code:
old_position, old_team = 0, None
for position, team in enumerate(teamData, 1):
    # I use enumerate() so I can change the position variable.

    if old_position and '''old_team and team compare to the same rank''':
        position = old_position
    else:
        old_position, old_team = position, team

    print(position, team, sep=': ')

rawDescription: Print same position in soccer table if the points are the same


code:
for node in (n for n in nodelist if condition):
    node.x += 1

rawDescription: Cleaner assignment in for loop (python)

description: Cleaner assignment in for loop (python)


code:
while [th for th in threading.enumerate()
       if th is not threading.main_thread() and not th.isDaemon()]:
    sleep(5)
exit()

rawDescription: While loops and duplicate code in python


code:
def subdict(sub, key):
    return { key: sub }

d = reduce(subdict, reversed(keys), {})

rawDescription: Method for Creating a Nested Dictionary from a List of Keys


code:
from matplotlib.colors import hsv_to_rgb
rgb = hsv_to_rgb(hsv)

rawDescription: Plotting hsv values with imshow

description: Plotting hsv values with imshow


code:
import datetime

DAY, NIGHT = 1, 2
def check_time(time_to_check, on_time, off_time):
    if on_time > off_time:
        if time_to_check > on_time or time_to_check < off_time:
            return NIGHT, True
    elif on_time < off_time:
        if time_to_check > on_time and time_to_check < off_time:
            return DAY, True
    elif time_to_check == on_time:
        return None, True
    return None, False

on_time = datetime.time(23,30)
off_time = datetime.time(4,15)
timenow = datetime.datetime.now().time()
current_time = datetime.datetime.now().time()

when, matching = check_time(current_time, on_time, off_time)

if matching:
    if when == NIGHT:
        print("Night Time detected.")
    elif when == DAY:
        print("Day Time detected.")

rawDescription: Python - Working out if time now is between two times


code: m = p_oz.search(text)

rawDescription: How to extract a substring separate by square braquet and generate substrings

description: Extract a substring separate by square braquet and generate substrings


code:
lista = ['a', 'b', 'foo', 'c', 'd', 'e', 'bar']
next(i for i in lista if len(i) > 2)

rawDescription: Is there a neater way to get the first occurrence of something?


code:
cpu_req = cpu.get(15)  # Request 15% CPU capactiy
mem_req = mem.get(10)  # Request 10 memories
yield cpu_req & mem_req  # Wait until we have cpu time and memory
yield env.timeout(10)  # Use resources for 10 time units

rawDescription: Simpy - accessing multiple resources

description: Simpy - accessing multiple resources


code:
import pandas as pd
import os
import re

lookup = {
    11: 'Winter',
    12: 'Winter',
    1: 'Winter',
    2: 'Spring',
    3: 'Spring',
    4: 'Spring',
    5: 'Summer',
    6: 'Summer',
    7: 'Summer',
    8: 'Autumn',
    9: 'Autumn',
    10: 'Autumn'
}

os.chdir('C:/Users/n-jones/testdir/output/')

for fname in os.listdir('.'):
    if re.match(".*csv$", fname):
        data = pd.read_csv(fname, parse_dates=[0], dayfirst=True)
        data['Season'] = data['Date'].apply(lambda x: lookup[x.month])
        data['count'] = 1
        data = data.groupby(['Season'])['HAD', 'count'].sum()
        data['mean'] = data['HAD'] / data['count']
        data.to_csv('C:/Users/n-jones/testdir/season/' + fname)

rawDescription: Group data by seasons using python and pandas


code:
import tarfile
import os

ep_list = ['./foo/bar/file.txt', './foo/bar/file2.txt']

def ISIP_tar_files():
    with tarfile.open("eeg_files.tar", "w") as f:
        ep_dir = tarfile.TarInfo("Eplilepsy Reports")
        not_ep_dir = tarfile.TarInfo("Non Epilepsy Reports")
        ep_dir.type = not_ep_dir.type = tarfile.DIRTYPE
        ep_dir.mode = not_ep_dir.mode = 0o777
        f.addfile(ep_dir)
        f.addfile(not_ep_dir)
        for name in ep_list:
            f.add(name, arcname="Eplilepsy Reports/" + os.path.basename(name), recursive=False)

rawDescription: Add files to empty directory within Tar in Python


code: getattr(cls, method_name)

rawDescription: Given a method name as string, how to get the method instance?


code:
import re

regex = re.compile(r'^([^&]*)(&)([^&]*)(&)([^&]*)')

with open('test.tex', 'r+') as f:
    old = f.readlines() # Pull the file contents to a list
    f.seek(0) # Jump to start, so we overwrite instead of appending
    for line in old:
        s = raw_input(line)
        if s == 'y':
            f.write(regex.sub(r'\3\2\1\4\5',line))
        else:
            f.write(line)

rawDescription: Python writing to file using stdout and fileinput


code:
import imp
my_module = imp.load_source('my_module', 'example.txt')

rawDescription: How to use a python program stored in a text file?

description: Use a python program stored in a text file


code:
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import event
from sqlalchemy.orm import attributes

Base= declarative_base()

tagging = Table('tagging',Base.metadata,
    Column('tag_id', Integer, ForeignKey('tag.id', ondelete='cascade'), primary_key=True),
    Column('entry_id', Integer, ForeignKey('entry.id', ondelete='cascade'), primary_key=True)
)

class Tag(Base):

    __tablename__ = 'tag'
    id = Column(Integer, primary_key=True)
    name = Column(String(100), unique=True, nullable=False)

    def __init__(self, name=None):
        self.name = name

class Entry(Base):
    __tablename__ = 'entry'

    id = Column(Integer, primary_key=True)
    tag_names = association_proxy('tags', 'name')

    tags = relationship('Tag',
                        secondary=tagging,
                        backref='entries')

@event.listens_for(Session, 'after_flush')
def delete_tag_orphans(session, ctx):
    # optional: look through Session state to see if we want
    # to emit a DELETE for orphan Tags
    flag = False

    for instance in session.dirty:
        if isinstance(instance, Entry) and \
            attributes.get_history(instance, 'tags').deleted:
            flag = True
            break
    for instance in session.deleted:
        if isinstance(instance, Entry):
            flag = True
            break

    # emit a DELETE for all orphan Tags.   This is safe to emit
    # regardless of "flag", if a less verbose approach is
    # desired.
    if flag:
        session.query(Tag).\
            filter(~Tag.entries.any()).\
            delete(synchronize_session=False)

e = create_engine("sqlite://", echo=True)

Base.metadata.create_all(e)

s = Session(e)

r1 = Entry()
r2 = Entry()
r3 = Entry()
t1, t2, t3, t4 = Tag("t1"), Tag("t2"), Tag("t3"), Tag("t4")

r1.tags.extend([t1, t2])
r2.tags.extend([t2, t3])
r3.tags.extend([t4])
s.add_all([r1, r2, r3])

assert s.query(Tag).count() == 4

r2.tags.remove(t2)

assert s.query(Tag).count() == 4

r1.tags.remove(t2)

assert s.query(Tag).count() == 3

r1.tags.remove(t1)

assert s.query(Tag).count() == 2

rawDescription: Delete children after parent is deleted in SQLAlchemy


code:
import sys
from tkinter import *
from math import *

def sqrt_():
    text = ment.get()
    a = sqrt(text)
    label['text'] = a

def pi_():
    label_1['text'] = pi

root = Tk()
root.title('Conversions')
root.geometry('400x400')

#Get square root
sqrt_button = Button(root, text='Get Square root',command= sqrt_).place(x='160', y='5')
label = Label(root, text='')
label.place(x=5, y=30)
ment = IntVar()
entry = Entry(textvariable=ment).place(x='5', y= '10 ')
#Get Pi
pi_button = Button(root, text='Get Pi',command= pi_).place(x='160', y='50')
label_1 = Label(root, text='')
label_1.place(x=55, y=200)
ment_1 = IntVar()
entry_1 = Entry(textvariable=ment_1).place(x='5', y= '55 ')

root.mainloop()

rawDescription: Why don't any of my buttons work?

description: Why don't any of my buttons work


code:
class MyModelAdminForm(TranslatableModelForm):
    class Meta:
        model = MyModel
        exclude = ()

    def __init__(self, *args, **kwargs):
        super(MyModelAdminForm, self).__init__(*args, **kwargs)
        self.fields['services'].queryset = Service.objects.prefetch_related('translations').all()

class MyModelAdmin(TranslatableAdmin):

    form = MyModelAdminForm

rawDescription: Translatable Manytomany fields in admin generate many queries


code:
popen = Popen(command_args, stdout=PIPE, stderr=PIPE)
body_contents = popen.stdout().read()
popen.terminate()
popen.wait()
r = HttpResponse(body_contents, mimetype='application/pdf')

rawDescription: return pdf response from stdout with Django

description: Return pdf response from stdout with Django


code:
def your_magic(arr):
    res = np.empty_like(arr)
    for enu, subarr in enumerate(arr):
        res[enu] = np.sum(subarr)
    return res

rawDescription: Sums of arrays in an array

description: Sums of arrays in an array


code:
header = ["site", "ip", "date", "method","code", "browser","n1", "n2"]

# dict to store [ip] -> occurences
# this defaultdict returns 0 for unset keys
ip_freq = defaultdict(0)

with open("filename.csv", mode="r") as csvfile:
    reader = csv.DictReader(csvfile, header, delimiter=",", quoting=csv.QUOTE_ALL);
    for line in reader:
        # sets the ip and increments the number of occurences
        ip_freq[line["ip"]] += 1
        # print: "requestcount: 2   ip: 85.97.166.82"
        print("requestcount: {}\tip: {}".format(
                                        ip_freq[line["ip"]],
                                        line["ip"]))

rawDescription: python parsing text file

description: Python parsing text file


rawDescription: Python regex matching only if digit

description: Python regex matching only if digit


code:
from itertools import product
import string

for num_letters in [2, 3]:
    for chars in product(string.ascii_letters, repeat=num_letters):
        prefix = "".join(chars)
        url = "http://www.example.com/{}_file.pdf".format(prefix)
        # now look for the url

rawDescription: String creation from list of values

description: String creation from list of values


code: mime_text = msg.message().as_string()

rawDescription: Django EmailMessage - dump object as string


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

import matplotlib.dates as mdates

import datetime as dt

# Generate some random data for imshow
N = 10
arr = np.random.random((N, N))

# Create your x-limits. Using two of your unix timestamps you first
# create a list of datetime.datetime objects using map.
x_lims = list(map(dt.datetime.fromtimestamp, [982376726, 982377321]))

# You can then convert these datetime.datetime objects to the correct
# format for matplotlib to work with.
x_lims = mdates.date2num(x_lims)

# Set some generic y-limits.
y_lims = [0, 100]

fig, ax = plt.subplots()

# Using ax.imshow we set two keyword arguments. The first is extent.
# We give extent the values from x_lims and y_lims above.
# We also set the aspect to "auto" which should set the plot up nicely.
ax.imshow(arr, extent = [x_lims[0], x_lims[1],  y_lims[0], y_lims[1]],
          aspect='auto')

# We tell Matplotlib that the x-axis is filled with datetime data,
# this converts it from a float (which is the output of date2num)
# into a nice datetime string.
ax.xaxis_date()

# We can use a DateFormatter to choose how this datetime string will look.
# I have chosen HH:MM:SS though you could add DD/MM/YY if you had data
# over different days.
date_format = mdates.DateFormatter('%H:%M:%S')

ax.xaxis.set_major_formatter(date_format)

# This simply sets the x-axis data to diagonal so it fits better.
fig.autofmt_xdate()

plt.show()

rawDescription: Dates in the xaxis for a matplotlib plot with imshow


code:
class FileTransferUtils(object):

    def __init__(self, address, user, password, remote_path, port):
        self.host = address.split(':')[0]
        self.start = ftputil.FTPHost(self.host, user, password, port=port, session_factory=OpenFTPSession)
        self.start.chdir(remote_path)
        self.remote_path = remote_path
        self.total_size = 0
        self.received = 0

    def test_callback(self, chunk):
        self.received += len(chunk)
        print("     {} Progress : {}Kb / {}Kb".format(
            'Uploading', float(self.received)/1000, self.total_size), end='\r'
        )

    def upload(self, filename):
        try:
            def test_callback(i):
                print(" .", end='')

            self.start.upload(filename, filename, callback=self.test_callback)
        except Exception:
            print('Upload failed')
            raise
        finally:
            self.total_size = 0
            self.received = 0
            self.start.close()

rawDescription: How to pass chunk length from upload method on ftputil

description: Pass chunk length from upload method on ftputil


code:
im = np.random.rand(100,100)

for i in xrange(10):
    fig12 = plt.figure(12)
    fig_SN_final = plt.imshow(im)
    cbar = plt.colorbar()
    cbar.set_label(r'Signal to Noise (significance $\sigma$)')
    plt.xlabel('X (arcmin)')
    plt.ylabel('Y (arcmin)')
    plt.title('Signal to Noise map')
    plt.savefig(str('%03d'%i)+'.png')
    plt.clf()

rawDescription: One more colorbar at each loop

description: One more colorbar at each loop


code:
import cmd
class Test(cmd.Cmd):
    # your stuff (do_XXX methods should return nothing or False)
    def do_exit(self,*args):
        return True

rawDescription: How to exit the cmd loop of cmd module cleanly

description: Exit the cmd loop of cmd module cleanly


code:
rules = (
    Rule(
        LinkExtractor(
            restrict_xpaths=['xpath_to_category'],
            allow=('regex_for_links')
        ),
        follow=True,
    ),
    Rule(
        LinkExtractor(
            restrict_xpaths=['xpath_to_items'],
            allow=('regex_to_links')
        ),
        callback='some_parse_method',
    ),
)

rawDescription: scrapy: avoid circular re-crawling

description: Scrapy: avoid circular re-crawling


code:
string = "abcdefg"
print(string[:3]) # abc
print(string[3:]) # defg

rawDescription: can some one explain what is iv = enc[:16] and enc= enc[16:] means?

description: Can some one explain what is iv = enc[:16] and enc= enc[16:] means


code:
import numpy as np
import healpy as hp
import matplotlib.pyplot as plt
m = np.arange(hp.nside2npix(32))
hp.mollview(m, cbar=None)

fig = plt.gcf()
ax = plt.gca()
image = ax.get_images()[0]
cmap = fig.colorbar(image, ax=ax)

rawDescription: Custom colorbar in healpy mollview

description: Custom colorbar in healpy mollview


code:
count = 0
def ComputeVec(self):
    window.close()
    getVecValue(angle)
    global count
    count += 1
    if count in range(values) :
        self.CreatWindow()

rawDescription: Pausing a loop in python

description: Pausing a loop in python


code:
first, rest = 'someone.lastname@example.com'.split('.', 1)
first + rest[:1]

rawDescription: Python convert email

description: Python convert email


code:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys

class MyWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.resize(640,480)
        self.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)

if __name__=="__main__":
    app=QApplication(sys.argv)
    win=MyWindow()
    win.show()
    sys.exit(app.exec_())

rawDescription: Splash window and always on top at the same time in pyqt4?


code:
from io import FileIO
import time

class logfile(FileIO):
  def __init__(self, *args, **kwargs):
    FileIO.__init__(self, *args, **kwargs)

  def write(self, logstr):
    if logstr[-1] == '\n': logstr = logstr[:-1]
    super(logfile, self).write(bytes(time.strftime('%D-%T ') + str(logstr), 'UTF-8'))

import sys

sys.stdout = logfile('somefile', 'w')

print("This is a test")
sys.stdout.write('this is another test')

rawDescription: pythons 'print' statement doesn't call the .write() method?

description: Pythons 'print' statement doesn't call the .write() method


code:
import xml.etree.ElementTree as ET

html = '''

Some reasonably well-formed HTML content.

    <form action="login">
    <input name="foo" value="bar"/>
    <input name="username"/><input name="password"/>

It is not unusual to see   in an HTML page.

    </form>'''

magic = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" [
            <!ENTITY nbsp ' '>
            ]>'''  # You can define more entities here, if needed

et = ET.fromstring(magic + html)

rawDescription: How to parse HTML with entities such as   using builtin library ElementTree in Python 2 & Python 3?

description: Parse HTML with entities such as   using builtin library ElementTree in Python 2 & Python 3


rawDescription: Regex to match letters, numbers and some specific characters?


code:
import subprocess
subprocess.check_output(['xdg-user-dir', 'DESKTOP'])

rawDescription: How to get users desktop path in python independent of language install (linux)

description: Get users desktop path in python independent of language install (linux)


code:
with open('/pythonwork/thefile_subset1.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    for row in data:
        if counter[row[11]] >= 500:
           writer.writerow(row)

rawDescription: csv writer not closing file

description: Csv writer not closing file


code:
class MyWidget(QtGui.QWidget):

    def __init__(self):
        super(MyWidget, self).__init__()
        self.label = QtGui.QLabel(self)
        self.watcher = QtCore.QFileSystemWatcher(self)
        self.watcher.addPath('/path/to/file')
        self.watcher.fileChanged.connect(self.updateLabel)

    def updateLabel(self, path):
        with open(path, 'r') as f:
            self.label.setText(f.read())

rawDescription: How to update QLabel in while loop

description: Update QLabel in while loop


code:
from PyQt4 import QtGui
import sys

class SubWindow(QtGui.QDialog):
    def __init__(self):
        super(SubWindow , self).__init__()
        label = QtGui.QLabel("Hey, subwindow here!",self);

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow , self).__init__()
        self.window()

    def window(self):
        Action = QtGui.QAction(QtGui.QIcon('action.png') , 'action' , self)
        Action.triggered.connect(self.a)

        mb = self.menuBar()
        option = mb.addMenu('File')
        option.addAction(Action)

        self.show()

    def a(self):

        s = SubWindow()
        s.exec_()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())

rawDescription: Basic help needed in PyQt4

description: Basic help needed in PyQt4


rawDescription: Exclude items from results

description: Exclude items from results


code:
from django.contrib.auth.models import User

users = User.objects.all()

rawDescription: Django Get All Users

description: Django Get All Users


code: shutil.make_archive('public_html_backup', 'zip', '/home/john', '/home/john/public_html')

rawDescription: How to make function "make_archive" to include the parent directory in python

description: Make function "make_archive" to include the parent directory in python


code:
for D in L:
  del D['item1']

rawDescription: remove certain key from a dictionary in python?

description: Remove certain key from a dictionary in python


code:
neg = {'transit': 1, 'infect': 4, 'spam': 6}
pos =  {'transit': 3, 'infect': 5, 'spam': 2}
result = {}
for k,v in neg.items():
    result[k] = [v,pos[k]]
result # {'spam': [6, 2], 'transit': [1, 3], 'infect': [4, 5]}

rawDescription: how to store multiple values for one key in python

description: Store multiple values for one key in python


code:
dictionary = {'C1' : [10,20,30],'C2' : [20,30,40]}
dictionary['C1'] = [x+1 for x in dictionary['C1']]
dictionary

rawDescription: Python Dictionary contains List as Value - How to update?


code: firstpart, secondpart = string[:len(string)/2], string[len(string)/2:]

rawDescription: Split a string into 2 in Python

description: Split a string into 2 in Python


code: ba[i] = version << 6 | padding << 5 | extension << 4 | cc

rawDescription: python bytearray

description: Python bytearray


code:
with open('years.txt') as year:
    years = [yr.strip() for yr in year]
with open('names.txt') as names:
    for name in names:
        name = name.strip()
        for year in years:
            print("%s%s" % (name, year))

rawDescription: looping text in file python

description: Looping text in file python


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import griddata
from mpl_toolkits.mplot3d import Axes3D

data=np.genfromtxt('fluorescence_2.txt')
x=data[:,0]
fig=plt.figure()
ax=fig.gca(projection='3d')

for i in range(1,17,2):
    y=data[:,i]
    z=data[:,i+1]
    xi=np.linspace(min(x),max(x))
    yi=np.linspace(min(y),max(y))
    X,Y=np.meshgrid(xi,yi)
    Z=griddata(x,y,z,xi,yi)
    ax.plot_surface(X,Y,Z,rstride=50,cstride=1,cmap='RdYlBu')
    ax.set_zlim3d(np.min(Z),np.max(Z))

ax.set_title('Fluorescence spectra (WL ex = 350 nm)')
ax.set_xlabel('WL em (nm)')
ax.set_ylabel('Spectrum')
ax.set_yticks([])
ax.set_zlabel('Emission')
plt.show()

rawDescription: matplotlib 3D ribbon plot

description: Matplotlib 3D ribbon plot


code:
datetime.now().isoformat()
sorted([datetime.now().isoformat(), (datetime.now() - timedelta(days=100)).isoformat()])

rawDescription: Python/SQLite sorting by TEXT date

description: Python/SQLite sorting by TEXT date


code:
import numpy as np
import pandas as pd
import sympy as sp
import matplotlib.pyplot as plt
from scipy import optimize
from sympy import init_printing, symbols, lambdify, Matrix, latex
from sympy import pi, exp, log, sqrt, sin, cos, tan, sinh, cosh, tanh
from sympy.abc import a, b, c, x, y, z, r, w
x1, x2 = symbols('x1 x2')

# Expressions
f1_expr = sin(4 * pi * x1 * x2) - 2 * x2 - x1
f2_expr = ((4 * pi  - 1) / (4 * pi)) * (exp(2 * x1) - exp(1)) + 4 * exp(1) * (x2 ** 2) - 2 * exp(1) * x1
f_expr = [f1_expr, f2_expr]

# Expressions -> NumPy function
f1 = lambdify((x1, x2), f1_expr, 'numpy')
f2 = lambdify((x1, x2), f2_expr, 'numpy')
f = np.array([f1, f2])

def _f(args):
    return [f1(args[0], args[1]), f2(args[0], args[1])]

# Matrix and it's Jacobian
M_expr = Matrix([f1_expr, f2_expr])
M_jacob_expr = M_expr.jacobian([x1, x2])

# Matrix -> NumPy function
M = lambdify((x1, x2), M_expr, [{'ImmutableMatrix': np.array}, "numpy"])
M_jacob = lambdify((x1, x2), M_jacob_expr, [{'ImmutableMatrix': np.array}, "numpy"])

# Data points
x1pts = np.arange(-2, 3, 0.01)
x2pts = np.arange(-2, 3, 0.01)
xx1pts, xx2pts = np.meshgrid(x1pts, x2pts)
# Solved over ranges for plots
z1, z2 = M(xx1pts, xx2pts)
z1 = z1.reshape(z1.shape[1], z1.shape[2])
z2 = z2.reshape(z2.shape[1], z2.shape[2])

# Find roots
results = optimize.root(_f, x0=[-0.3, 0.05], method='Krylov')
zeros = results.get('x')
# First figure
fig, ax = plt.subplots(1)

im = ax.contourf(x1pts, x2pts, z1)
ax.scatter(zeros[0], zeros[1], linewidth=5, color='k')

ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_title('f1(x1, x2)')
plt.colorbar(im)
plt.tight_layout()
plt.set_cmap('seismic')
fig.savefig('img30_1.png')
plt.show()
plt.close(fig)

# Second figure
fig, ax = plt.subplots(1)

im = ax.contourf(x1pts, x2pts, z2)
ax.scatter(zeros[0], zeros[1], linewidth=5, color='white')

ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_title('f2(x1, x2)')
plt.colorbar(im)
plt.tight_layout()
plt.set_cmap('seismic')
fig.savefig('img30_2.png')
plt.show()
plt.close(fig)

rawDescription: Finding roots with a SymPy Matrix

description: Finding roots with a SymPy Matrix


code:
from wx.lib import wordwrap
import wx.grid

class CutomGridCellAutoWrapStringRenderer(wx.grid.PyGridCellRenderer):
    def __init__(self):
        wx.grid.PyGridCellRenderer.__init__(self)

    def Draw(self, grid, attr, dc, rect, row, col, isSelected):
        text = grid.GetCellValue(row, col)
        dc.SetFont( attr.GetFont() )
        text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False)
        hAlign, vAlign = attr.GetAlignment()
        if isSelected:
            bg = grid.GetSelectionBackground()
            fg = grid.GetSelectionForeground()
        else:
            bg = attr.GetBackgroundColour()
            fg = attr.GetTextColour()
        dc.SetTextBackground(bg)
        dc.SetTextForeground(fg)
        dc.SetBrush(wx.Brush(bg, wx.SOLID))
        dc.SetPen(wx.TRANSPARENT_PEN)
        dc.DrawRectangleRect(rect)
        grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)

    def GetBestSize(self, grid, attr, dc, row, col):
        text = grid.GetCellValue(row, col)
        dc.SetFont(attr.GetFont())
        text = wordwrap.wordwrap(text, grid.GetColSize(col), dc, breakLongWords = False)
        w, h, lineHeight = dc.GetMultiLineTextExtent(text)
        return wx.Size(w, h)

    def Clone(self):
        return CutomGridCellAutoWrapStringRenderer()

rawDescription: Auto wrap and newlines in wxPython grid

description: Auto wrap and newlines in wxPython grid


code:
for data in (self.mfiles, self.nmfiles):
    for key, value in data.iteritems():
        if key not in self.INPUT['extras']:
            data[key] = self.dirs['confdir'] + '/' + value

rawDescription: execute code block on two separate dicts at the same time

description: Execute code block on two separate dicts at the same time


rawDescription: Pandas read from csv and write back. Floats saving differing values


rawDescription: Histogram with independent line matplotlib


code:
import re
from urllib import parse
reponse = session.get('http://weibo.com/u/xxxxxxxxx')
#xxxxxxx is the account's ID.
html_raw_data = parse.unquote(reponse.content.decode())
#url decode
html_data = re.sub(r'\\'r'',html_raw_data)
#backslash has Escaped two times，get the raw code
follows_fans_articles_data = re.search(r'\[\'page_id\'\]=\'(\d+)',html_data,re.M)
#follows_fans_articles_data.group(1)  follows number    (2)  fans number  (3) articles number

rawDescription: Web scrape Weibo Follower count using python


code:
listed = "a.b.c.d.e".split('.')
subtrees = ['.'.join(listed[idx:]) for idx in xrange(len(listed))]

rawDescription: Obtain all subtrees in value

description: Obtain all subtrees in value


code:
from kivy.config import Config
Config.set('graphics', 'resizable', False)

rawDescription: Fixed window size for Kivy programs

description: Fixed window size for Kivy programs


code:
import svgutils.transform as st

template = st.fromfile('template.svg')
second_svg = st.fromfile('second.svg')
template.append(second_svg)
template.save('merged.svg')

rawDescription: svgwrite - How to enable write/add other objects as layers of an existing .svg file

description: Svgwrite - How to enable write/add other objects as layers of an existing .svg file


code:
orar = []
for line in csv.reader(f, delimiter='\t', lineterminator='\t\t', doublequote=False, skipinitialspace=True):
    orar.append(OraRend._make(line[1:]))

rawDescription: Python - Import file into multiple arrays, dicts, tuples


code:
import requests
repos = requests.get("https://api.github.com/users/gamesbrainiac/repos").json()
repos[0]['git_url']
repos[1]['git_url']
repos[2]['git_url']

rawDescription: What's the most canonical method for accessing a json api and storing it into a dict in python?


code: a = a or b

rawDescription: A better way to write: a = b if a is None else a


code:
import Tkinter as tk
def quit(event):
    print("You pressed Control-Key-1")
    root.quit()

root = tk.Tk()
root.bind('<Control-Key-1>', quit)
root.mainloop()

rawDescription: Control+Number Tkinter binding

description: Control+Number Tkinter binding


rawDescription: What is the best way to print this matrix without a for loop


code:
email = "tony@tiremove_thisger.net"
m = re.search("remove_this", email)
email[:m.start()] + email[m.end():]

rawDescription: Get start location of capturing group within regex pattern


code:
# a is your 'historical' data
# Now create a look-up table to access elements of interest:
types = defaultdict(list)

for i, item in enumerate(a):
    t = item[0]
    types[t].append(i)

# You can now use your look-up table to grab items of interest e.g:
new_record = ['TYPE_3','000000002',100,2,'K']
type_idxs = types[new_record[0]]

old_types_id_counts = [a[i][2] for i in type_idxs]

# Collect the indexes of the items you want to remove:
f = [type_idxs[i] for i in range(len(type_idxs)) if old_types_id_counts[i] < current_id_count - 2]

# Once you have your indexes of items to remove, you just need to remove those items and update your table

rawDescription: Python Dictionary Optimization

description: Python Dictionary Optimization


code:
import shutil

for i in range(100):
    shutil.copy2('/Users/bubble/Desktop/script.py', '/Users/bubble/Desktop/pics/script{}.py'.format(i))

rawDescription: Python - making copies of a file

description: Python - making copies of a file


code:
import warnings
warnings.warn("deprecated", DeprecationWarning)

rawDescription: How to formally deprecate a pip package

description: Formally deprecate a pip package


code: x_sub, y_sub = zip(*random.sample(list(zip(x, y)), m))

rawDescription: Random sample of paired lists in Python

description: Random sample of paired lists in Python


code:
itr = iter(biglist)
while True:
    lists = itertools.islice(((i, i/5*2, i/5) for i in itr if 7 < i/5 < 69), S)
    x_smallist, y_smallist, z_smallist = zip(*lists)
    if len(x_smallist) == 0:
        break       # reached the end of biglist
    # do stuff with your small lists

rawDescription: python filter calculated values from large lists to create multiple smaller lists

description: Python filter calculated values from large lists to create multiple smaller lists


code:
items_by_pid = dict([(item.product.pid, item) for item in cart.items])
item = items_by_pid.get(pid, None)
if item is None:
    item = model.CartItem(cart, product, 0, product.normalPrice)
    cart.items.append(item)
item.quantity += 1

rawDescription: Cart item management in python Turbogears 2.0


rawDescription: pandas groupby apply on multiple columns

description: Pandas groupby apply on multiple columns


code:
from networkx.algorithms.traversal.depth_first_search import dfs_edges

G = DiGraph( ... )
for edge in dfs_edges(G, parent1):
    color(edge)

rawDescription: Finding Successors of Successors in a Directed Graph in NetworkX


code:
from sqlalchemy.sql import func
# ...
~exist(func.next_value()).where(...)

rawDescription: python sqlalchemy exist & select 'next' syntax

description: Python sqlalchemy exist & select 'next' syntax