code:
import signal, time

def request(arg):
  """Your http request"""
  time.sleep(2)
  return arg

class Timeout():
  """Timeout class using ALARM signal"""
  class Timeout(Exception): pass

  def __init__(self, sec):
    self.sec = sec

  def __enter__(self):
    signal.signal(signal.SIGALRM, self.raise_timeout)
    signal.alarm(self.sec)

  def __exit__(self, *args):
    signal.alarm(0) # disable alarm

  def raise_timeout(self, *args):
    raise Timeout.Timeout()

# Run block of code with timeouts
try:
  with Timeout(3):
    print request("Request 1")
  with Timeout(1):
    print request("Request 2")
except Timeout.Timeout:
  print "Timeout"

# Prints "Request 1" and "Timeout"

rawDescription: What should I do if socket.setdefaulttimeout() is not working?


code:
with open('exclusions.tsv', 'wb') as tsvfile:
    writer = csv.writer(tsvfile, delimiter='\t')

    for row in old_emails_list[1:]:
        print str(row[old_email_index])
        if row[old_email_index] in exclusion_emails:
            writer.writerow(row)

rawDescription: Improving python performance in comparing two lists


code:
if any(re.search(r'\b{}\b'.format(re.escape(word)), sentence, re.I) for word in listword):
    print 'yes'

rawDescription: Finding whether a word exist in a string in python


code:
# save this as client.py
import greeting
name=raw_input("What is your name? ")
greeting_maker=greeting.GreetingMaker()
print greeting_maker.get_fortune(name)

rawDescription: How can I send python code to another machine to be executed?

description: Send python code to another machine to be executed


code:
import __builtin__
import collections
import sys

IN_USE = collections.defaultdict(set)
_IMPORT = __builtin__.__import__

def _myimport(name, globs=None, locs=None, fromlist=None, level=-1):
    global IN_USE
    if fromlist is None:
        fromlist = []
    IN_USE[name].update(fromlist)
    return _IMPORT(name, globs, locs, fromlist, level)

# monkey-patch __import__
setattr(__builtin__, '__import__', _myimport)

# import and run the target project here and run the routine
import foobar
foobar.do_something()

# when it finishes running, dump the imports
print 'modules and symbols imported by "foobar":'
for key in sorted(IN_USE.keys()):
    print key
    for name in sorted(IN_USE[key]):
        print '  ', name

rawDescription: Python dependencies?

description: Python dependencies


code:
import bisect
import scipy.optimize

def fulldist(x, data):
    dists = [sum([(x[i]-d[i])*(x[i]-d[i]) for i in range(len(x))])**0.5 for d in data]
    obj = 0.0
    for i in range(len(data)-1):
        for j in range(i+1, len(data)):
            obj += (dists[i]-dists[j]) * (dists[i]-dists[j])
    return obj

def f1p(x, d):
    lownum = bisect.bisect_left(d, x)
    highnum = len(d) - lownum
    lowsum = highnum * (x*lownum - sum([d[i] for i in range(lownum)]))
    highsum = lownum * (x*highnum - sum([d[i] for i in range(lownum, len(d))]))
    return 4.0 * (lowsum + highsum)

data = [(0.0,), (3.0,), (3.0,)]
opt = []
centroid = []
for d in range(len(data[0])):
    thisdim = [x[d] for x in data]
    meanval = sum(thisdim) / len(thisdim)
    centroid.append(meanval)
    thisdim.sort()
    opt.append(scipy.optimize.newton(f1p, meanval, args=(thisdim,)))
print "Proposed", opt, "objective", fulldist(opt, data)
# Proposed [1.5] objective 0.0
print "Centroid", centroid, "objective", fulldist(centroid, data)
# Centroid [2.0] objective 2.0

rawDescription: Finding a vector that is approximately equally distant from all vectors in a set


code:
from itertools import permutations

xlist = ["word1", "word2", "word3"]

for n in range(1, len(xlist)+1):
    for perm in permutations(xlist, n):
        print "".join(perm)

rawDescription: Generating subsets of a permuted wordlist in Python


code:
for team, players in teams.iteritems():
   print '{}|{}'.format(team, '|'.join(players))

rawDescription: Aggregate text keys-values python defaultdict


code:
H=set(A)
ans = sum(1 for a in A if a-k in H)
print ans

rawDescription: Fastest algorithm possible to pick number pairs


code:
def canonical_form(li):
  """ Note, this implementation overwrites li """
  first = dict()
  for i in range(len(li)):
    v = first.get(li[i])
    if v is None:
      first[li[i]] = i
      v = i
    li[i] = v
  return li

print canonical_form([ 1, 1, 1, 0, 0, 2, 6 ])
# [0, 0, 0, 3, 3, 5, 6]
print canonical_form([ 2, 2, 2, 9, 9, 3, 1 ])
# [0, 0, 0, 3, 3, 5, 6]
print canonical_form([ 2, 2, 2, 9, 9, 3, 3 ])
# [0, 0, 0, 3, 3, 5, 5]
print canonical_form(['A','B',0,'D',0,'B','A'])
# [0, 1, 2, 3, 2, 1, 0]
print canonical_form([1,1,1,0,0,2,6]) == canonical_form([2,2,2,9,9,3,1])
# True
print canonical_form([1,1,1,0,0,2,6]) == canonical_form([2,2,2,9,9,3,3])
# False

rawDescription: How to determine if two partitions (clusterings) of data points are identical?

description: Determine if two partitions (clusterings) of data points are identical


code:
from collections import defaultdict
import csv

curr_id = None
found = False
goal = 'bob'
ids = defaultdict(list)

with open('my_data.csv') as ifile:
    for row in csv.reader(ifile):
        if row[0] != curr_id:
            found = False
            curr_id = row[0]
        if found:
            ids[curr_id].append(row[1])
        elif row[2] == goal:
            found = True

print dict(ids)

rawDescription: Getting values from file if match found for lines with same id in Python


code:
from django.shortcuts import render
import os

def index(request):
        os.chdir("/home/ubuntu/newproject/static")
        files = []
        for file in os.listdir("."):
            files.append(file)
        return render(request, 'sslcert/index.html', dict(files = files))

def adduser(request):
        return render(request, 'sslcert/adduser.html')

def redirect(request):
        username = request.POST['username']
        password = request.POST['password']

        print username
        print password

        return render(request, 'sslcert/redirect.html')

rawDescription: How can i submit data in django?

description: Submit data in django


code: for x in portfolios(): print x

rawDescription: Python - most efficient way to generate combinations of large sets subject to criteria?


code:
class Timer(object):
    def __init__(self, name=None):
        self.name = name

    def __enter__(self):
        self.tstart = time.time()

    def __exit__(self, type, value, traceback):
        if self.name:
            print '[%s]' % self.name,
        print 'Elapsed: %s' % (time.time() - self.tstart)

rawDescription: tic, toc functions analog in Python

description: Tic, toc functions analog in Python


code:
n_emails= [[1,'CAFEBABE010'],[2,'bfeafe3df1ds'],[3,'deadbeef101'],[5,'CAFEBABE010']]

d = {}
for id, hash in n_emails:
    if hash not in d:
        d[hash] = [id]
    else:
        d[hash].append(id)

for hash, ids in d:
    if len(ids) > 1:
       print hash, ids

rawDescription: Comparing Value of one list inside Gigantic Two Dimen list in python, Fastest way?


code:
list = [['apples', 'oranges', 'cherries', 'banana'],
        ['Alice', 'Bob', 'Carol', 'David'],
        ['dogs', 'cats', 'moose', 'goose']]

def swaplist(list):
    for index in range(0, len(list[0])):
        temp_list = []
        for i in list:
            temp_list.append(i[index])
        print " ".join(temp_list)

swaplist(list)

rawDescription: Print List Of List. Go from list[x][y] to list[y][x]


code:
real='lasts'
guess='brass'
exact=[l==r for l,r in zip(real,guess)]
approx=[l in real for l in guess]
chars=[[' ','~'],['!','!']]
print guess, '\n', ''.join(chars[e][a] for e,a in zip(exact,approx))

rawDescription: Python- Replace all occurrences of letter at specific position on a string


code:
l = ['a', 'b', 'c', 'd', 'e']
subarraysize = 3
for i in range(len(l)-subarraysize+1):
    print l[i:i+subarraysize]

rawDescription: Pythonic loops--how to get multiple elements while iterating a list


code:
# The primes below 200 used as bases for the strong Fermat test,
# prime bases have more discriminatory power than composite bases,
# therefore we use prime bases first
bases = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]

# The strong Fermat test for base b, where n is odd and large,
# n - 1 = m * 2^s with odd m
# the strong test checks whether b**m % n == 1 or
# b**(2**j) % n == n-1 for a 0 <= j < s
# If the test returns False, n is definitely composite, otherwise probably prime
def strongFermat(b,n,m,s):
    a = pow(b,m,n)
    if a == 1:
        return True
    n1 = n-1
    for i in xrange(s):
        if a == n1:
            return True
        a = (a*a) % n
    return False

# Multiple strong Fermat tests, by default use 10 bases
# The probability that a composite passes is less than 0.25**iters
def sppTest(n, iters = 10):
    # Assumes n > 1 and with no prime divisors < 200
    m = n-1
    s = 0
    while (m & 1) == 0:
        m >>= 1
        s += 1
    pbases = iters if iters < 47 else 46
    for i in xrange(pbases):
        if not strongFermat(bases[i],n,m,s):
            return False
    if pbases < iters:
        for i in xrange(iters-pbases):
            if not strongFermat(211 + 2*i,n,m,s):
                return False
    return True

# Trial division to weed out most composites fast
def trialDivisionPrime(n):
    if n < 2:
        return 0        # Not a prime
    if n < 4:
        return 2        # definitely prime
    if n % 2 == 0 or n % 3 == 0:
        return 0        # definitely composite
    for d in xrange(5, 200, 6):
        if d*d > n:
            return 2    # definitely prime
        if n % d == 0 or n % (d+2) == 0:
            return 0    # composite
    return 1            # not yet decided

# The prime test, first do trial division by numbers < 200,
# if that doesn't decide the matter, use some strong Fermat tests
# using 20 tests is the paranoid setting for largish numbers,
# for numbers in 64-bit range, ten or fewer suffice
def prime(n):
    td = trialDivisionPrime(n)
    return td > 1 or (td == 1 and sppTest(n,20))

# just check a couple of larger numbers
for c in xrange(100000):
    if prime(c + 10**25):
        print c

rawDescription: Python Generator returning stop iteration?


code:
ttlToSearchFor = 62
ipToSearchFor = "150.100.15"
ipSide = "right"

import csv

with open('path/to/input') as textfile:
    for row in csv.reader(textfile):
        #Make sure the record is the correct format
        if len( row ) == 10:
            #Get the ttl or any other info here
            ttl = row[1].rstrip().lstrip()
            #Remove the ttl text and any spaces to leave the number
            ttlNumber = ttl.replace( "ttl", "" ).replace( " ", "" )
            if ttlNumber == str( ttlToSearchFor ):
                #Get IPs
                splitBySpace = row[8].split( ":" )[0].split( " " )
                rightIP  = splitBySpace[-1]
                leftIP = splitBySpace[-3]
                #Search the left or right (or both) IPs for a match
                if (ipSide == "right" and ipToSearchFor in rightIP) or (ipSide == "left" and ipToSearchFor in leftIP) or (ipSide == "both" and (ipToSearchFor in leftIP or ipToSearchFor in rightIP)):
                    print row
                    #You found the record

rawDescription: Python - Parsing a text file

description: Python - Parsing a text file


code:
class stuff(Scene):
    def __init__(self):
        self.seen_description = False
        #other initialization goes here

    def enter(self):
        print "End Of The Road"
        if not self.seen_description:
            print "You are standing beside a small brick building at the end of a road from the north."
            print "A river flows south."
            print "To the north is open country, and all around is dense forest."
            self.seen_description = True

        action = raw_input("> ")

        if action == "go inside":
            print "You enter the brick building"
            return 'brick building'

x = stuff()
x.enter()
x.enter()

rawDescription: Python- How to skip whats been printed when returning?


code:
for word in dict_gen(n):  # replace n with the max word length you want
    print word

rawDescription: improve this very-simple dictionary generator in python

description: Improve this very-simple dictionary generator in python


code:
# original
print row.xpath(".//td[@class='impact']/span/@title").extract()
# ported
td = row.find(r'.//td[@class="impact"]/span')
if td is not None and 'title' in td.attrib:
    print td.attrib['title']

rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
context = zmq.Context()
pull = context.socket(zmq.PULL)
pull.bind(results_addr)

while True:
    worker_id, result = pull.recv_multipart()
    print worker_id, result

rawDescription: 0mq one-to-many connection

description: 0mq one-to-many connection


code:
from selenium import webdriver
import time

driver = webdriver.Firefox()
driver.get('http://www.walmart.com/ip/Sony-Xperia-ZL-LTE-C6506-5-Smartphone-Unlocked/24566601')

elements = driver.find_elements_by_class_name('SwatchAnchor')

for element in elements:
    element.click()
    time.sleep(2)
    print driver.find_element_by_class_name('bigPriceText1').text + driver.find_element_by_class_name('smallPriceText1').text

driver.close()

rawDescription: How do I send Ajax post request using urllib2 or Mechanize after opening the page?

description: Send Ajax post request using urllib2 or Mechanize after opening the page


code:
import Tkinter as tk

class newdemo:
    def __init__(self, root):
        root.wm_title("Calendar Demo")
        self.myentry = tk.Entry(root, text="Choose date")
        self.myentry.pack()

        self.button = tk.Button(root, text="Choose date", command=onclick)
        self.button.pack()

def onclick():
    cd = CalendarDialog(root)
    print cd.result
    demo.myentry.delete(0, tk.END)
    demo.myentry.insert(0, cd.result)

root = tk.Tk()
demo = newdemo(root)
root.mainloop()

rawDescription: return function value as entry text

description: Return function value as entry text


code:
def lambdas(*args):
    for arg in args:
        yield lambda: str(arg)

one, two, three, four = lambdas(1, 2, 3, 4)
print one(), two(), three(), four()

rawDescription: Lambda or functools.partial for deferred function evaluation?


code:
import socket

HOST = ''    #server name goes in here
PORT = 3820
socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.connect((HOST,PORT))
with open('myUpload.txt', 'rb') as file_to_send:
    for data in file_to_send:
        socket.sendall(data)
print 'end'
socket.close()

rawDescription: Python Client Server Transfer .txt Not Writing to File


code:
def is_prime(n):
  if n == 2 or n == 3: return True
  if n < 2 or n%2 == 0: return False
  if n < 9: return True
  if n%3 == 0: return False
  r = int(n**0.5)
  f = 5
  while f <= r:
    print '\t',f
    if n%f == 0: return False
    if n%(f+2) == 0: return False
    f +=6
  return True

rawDescription: isPrime Function for Python Language

description: IsPrime Function for Python Language


code:
output = []
with open("table.csv", "rU") as x:
    next(x)    # skip header row
    for line in x:
        cells = line.split(",")
        output.append((cells[7].strip()))
output.sort()
print output

rawDescription: Getting a column into an array from excel in python


code:
#!/usr/bin/python

import sys
import pygame

import bounceball

#----------------------------------------------------------------------

WHITE = (255, 255, 255)
RED   = (255,   0,   0)
BLACK = (  0,   0,   0)

#----------------------------------------------------------------------

class MenuItem(pygame.font.Font):

    def __init__(self, text, font=None, font_size=30,
                 font_color=WHITE, (pos_x, pos_y)=(0, 0)):

        pygame.font.Font.__init__(self, font, font_size)
        self.text = text
        self.font_size = font_size
        self.font_color = font_color
        self.label = self.render(self.text, 1, self.font_color)
        self.width = self.label.get_rect().width
        self.height = self.label.get_rect().height
        self.dimensions = (self.width, self.height)
        self.pos_x = pos_x
        self.pos_y = pos_y
        self.position = pos_x, pos_y

    def is_mouse_selection(self, (posx, posy)):
        if (posx >= self.pos_x and posx <= self.pos_x + self.width) and \
            (posy >= self.pos_y and posy <= self.pos_y + self.height):
                return True
        return False

    def set_position(self, x, y):
        self.position = (x, y)
        self.pos_x = x
        self.pos_y = y

    def set_font_color(self, rgb_tuple):
        self.font_color = rgb_tuple
        self.label = self.render(self.text, 1, self.font_color)

#----------------------------------------------------------------------

class GameMenu():

    def __init__(self, screen, items, funcs, bg_color=BLACK, font=None, font_size=30,
                 font_color=WHITE):
        self.screen = screen
        self.scr_width = self.screen.get_rect().width
        self.scr_height = self.screen.get_rect().height

        self.bg_color = bg_color
        self.clock = pygame.time.Clock()

        self.funcs = funcs
        self.items = []
        for index, item in enumerate(items):
            menu_item = MenuItem(item, font, font_size, font_color)

            # t_h: total height of text block
            t_h = len(items) * menu_item.height
            pos_x = (self.scr_width / 2) - (menu_item.width / 2)
            pos_y = (self.scr_height / 2) - (t_h / 2) + (index * menu_item.height)

            menu_item.set_position(pos_x, pos_y)
            self.items.append(menu_item)

        self.mouse_is_visible = True
        self.cur_item = None

    def set_mouse_visibility(self):
        if self.mouse_is_visible:
            pygame.mouse.set_visible(True)
        else:
            pygame.mouse.set_visible(False)

    def set_keyboard_selection(self, key):
        """
        Marks the MenuItem chosen via up and down keys.
        """
        for item in self.items:
            # Return all to neutral
            item.set_italic(False)
            item.set_font_color(WHITE)

        if self.cur_item is None:
            self.cur_item = 0
        else:
            # Find the chosen item
            if key == pygame.K_UP and \
                    self.cur_item > 0:
                self.cur_item -= 1
            elif key == pygame.K_UP and \
                    self.cur_item == 0:
                self.cur_item = len(self.items) - 1
            elif key == pygame.K_DOWN and \
                    self.cur_item < len(self.items) - 1:
                self.cur_item += 1
            elif key == pygame.K_DOWN and \
                    self.cur_item == len(self.items) - 1:
                self.cur_item = 0

        self.items[self.cur_item].set_italic(True)
        self.items[self.cur_item].set_font_color(RED)

        # Finally check if Enter or Space is pressed
        if key == pygame.K_SPACE or key == pygame.K_RETURN:
            text = self.items[self.cur_item].text
            self.funcs[text]()

    def set_mouse_selection(self, item, mpos):
        """Marks the MenuItem the mouse cursor hovers on."""
        if item.is_mouse_selection(mpos):
            item.set_font_color(RED)
            item.set_italic(True)
        else:
            item.set_font_color(WHITE)
            item.set_italic(False)

    def run(self):
        mainloop = True
        while mainloop:
            # Limit frame speed to 50 FPS
            self.clock.tick(50)

            mpos = pygame.mouse.get_pos()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    mainloop = False
                if event.type == pygame.KEYDOWN:
                    self.mouse_is_visible = False
                    self.set_keyboard_selection(event.key)
                if event.type == pygame.MOUSEBUTTONDOWN:
                    for item in self.items:
                        if item.is_mouse_selection(mpos):
                            self.funcs[item.text]()

            if pygame.mouse.get_rel() != (0, 0):
                self.mouse_is_visible = True
                self.cur_item = None

            self.set_mouse_visibility()

            # Redraw the background
            self.screen.fill(self.bg_color)

            for item in self.items:
                if self.mouse_is_visible:
                    self.set_mouse_selection(item, mpos)
                self.screen.blit(item.label, item.position)

            pygame.display.flip()

#----------------------------------------------------------------------

def run_bounceball():
    print "run bounceball"
    bounceball.run(screen)

#----------------------------------------------------------------------

if __name__ == "__main__":

    pygame.init()

    # Creating the screen
    screen = pygame.display.set_mode((300, 300), 0, 32)

    menu_items = ('Start', 'Quit')
    funcs = {'Start': run_bounceball,
             'Quit': sys.exit}

    pygame.display.set_caption('Game Menu')

    gm = GameMenu(screen, funcs.keys(), funcs)
    gm.run()

rawDescription: Combine effects to menu in pygame

description: Combine effects to menu in pygame


code:
element = driver.find_element_by_xpath('//a[@class="playlist_small"]/strong')
print element.text

rawDescription: xpath does not work with this site, pls verify

description: Xpath does not work with this site, pls verify


code:
print '%d'%math.pi
print '%f'%math.pi
print '%.10f'%math.pi
print '%.20f'%math.pi
print '%.30f'%math.pi

rawDescription: Why is python rounding this value?

description: Why is python rounding this value


code:
class Animal(Base):
    __tablename__ = 'animal'
    key = Column(Integer(), Sequence("My Counter" ,1 ,1), primary_key = True)
    name = Column(String())
    discriminator = Column('type',String())
    __mapper_args__ = { 'polymorphic_on':discriminator, }

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

class Cat(Animal):
    __tablename__ = 'cat'
    __mapper_args__ = {'polymorphic_identity':'cat'}
    key = Column(Integer(), ForeignKey('animal.key'), primary_key = True)

    def walk(self):
        print "cat walking"

class Dog(Animal):
    __tablename__ = 'dog'
    __mapper_args__ = {'polymorphic_identity':'dog'}
    key = Column(Integer(), ForeignKey('animal.key'), primary_key = True)

    def walk(self):
        print "dog walking"

rawDescription: Instantiating a subclass from a superclass


code:
#!/usr/bin/env python
import re, sys, subprocess, shlex
from bs4 import BeautifulSoup
from optparse import OptionParser

def main():
    usage = "usage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-u", "--url",
                      action="store", dest="url",
                      help="URL of host to check version number of.")
    (options, args) = parser.parse_args()
    # Make warning and critical command line arguments mandatory.
    if options.url is None:
        print "Incorrect usage. URL is mandatory."
        parser.print_help()
        sys.exit(3)
    try:
        url = options.url
        curl_command = '/usr/bin/curl {url}'.format(url=url)
        args = shlex.split(curl_command)
        curl = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        (html_doc, err) = curl.communicate()
        if err:
            print "CRITICAL - ERROR connecting to url: %s | 'rev'='NA';;;;" % (str(err))
            sys.exit(2)
        soup = BeautifulSoup(html_doc, 'html.parser')
        soup_svnrev = soup.find(id='svnrev')
        if soup_svnrev is None:
            print "CRITICAL - ERROR - Revision number could not be found on site! | 'rev'='NA';;;;"
            sys.exit(2)
        svnrev = soup_svnrev.get_text().strip()
        rev_num = svnrev.split()[1]
        print "OK. revision number = {rev_num} | 'rev_num'={rev_num};;;;".format(rev_num=rev_num)
        sys.exit(0)
    except Exception, e:
        print "CRITICAL - Unexpected error: %s | 'rev'='NA';;;;" % (str(e))
        sys.exit(2)

if __name__ == '__main__':
    main()

rawDescription: How to integrate my python plugin with nagios

description: Integrate my python plugin with nagios


rawDescription: Iterating over dictionary returns keys in sorted order


code:
def foo(arg1, arg2):
  return (arg1,arg2,3)

a, b, _ = asd(1,2)
print a, b # will print "1 2"

rawDescription: Resources on Professional Python Programming


code:
test.py
import testmodule
print testmodule.__doc__

rawDescription: Can a python module have a __repr__?

description: Can a python module have a __repr__


code:
from lxml import etree
doc = etree.fromstring(xmlstr.strip())

namespaces = {"aws": "http://awis.amazonaws.com/doc/2005-07-11"}
texts = doc.xpath("//aws:LinksInCount/text()", namespaces=namespaces)
print texts[0]

rawDescription: python alexa result parsing with lxml.etree

description: Python alexa result parsing with lxml.etree


code:
from itertools import cycle

for n, (obj, color) in enumerate(zip(objects, cycle(colors))):
    print n, obj, color

rawDescription: Python: Associate for loop with a list

description: Python: Associate for loop with a list


code:
class SkippedException(Exception):
    pass

class SkipContext:
    def __enter__(self):
        pass
    def __exit__(self, type, value, tb):
        return type is SkippedException

class SomeContext:
    def __init__(self, arg):
        self.arg = arg
    def __enter__(self):
        if self.arg == 1:
            print "arg", self.arg
            raise SkippedException()
    def __exit__(self, type, value, tb):
        pass

with SkipContext(), SomeContext(1):
    print "body"

rawDescription: How to stop statements within a Python with context from executing?

description: Stop statements within a Python with context from executing


code:
for line in r:
    print "test" # Test print
    if line[0] == s:
        orig_dict[int(line[3])].append(int(line[4]))

rawDescription: For-Loop Execution in Python - Does the Executable Code Reset?


code:
def __foo(x):
    print x

__foo("foo")

class Bar(object):
    def baz(self, x):
        __foo(x)

Bar().baz("foo")

rawDescription: Best way for a class to access a private method (private name mangling)


code:
class Klass(object):
    def say_hi(self):
        print 'hi from', self

func = Klass().say_hi
func()   # hi from <__main__.Klass object at 0x024B4D70>

rawDescription: Python append function as dot notation

description: Python append function as dot notation


code:
for k in sorted(groups, key=lambda k: int(k.split('-')[0])):
    print k, groups[k]

rawDescription: How to cluster (or group) the data from a CSV file?

description: Cluster (or group) the data from a CSV file


rawDescription: Python's tabulate number of decimal

description: Python's tabulate number of decimal


code:
with open('Infodoc 2.txt','r') as infile:
    incsv = DictReader(infile)
    for row in incsv:
        print row['Publisher']
        DocIn = row['DocIn']
        DocOut = row['DocOut']
        WebAddy = row['WebAddy']

rawDescription: Python, need to input csv file into a loop


code:
from __future__ import division # for getting a float result from int division
from collections import defaultdict
from random import randrange

TURNS = 150

def monopoly(turns):
    square = 0
    allsquares = defaultdict(lambda: 0)
    while turns > 0:
        roll = randrange(1,7) + randrange(1,7)
        square = square + roll
        if square > 39:
            square = square % 40 # this should be modulo 40 since there are 40 squares
        if square == 30:
            square = 10
        allsquares[square]+=1
        turns = turns - 1
    return allsquares

allsquares = monopoly(TURNS)
print "The actual ratio for landing on each square in %d turns for this particular game instance was:" % (TURNS, )
for i in xrange(40):
    print "square %d - %.2f%%" % (i, 100 * allsquares[i] / TURNS)

rawDescription: Using Python to calculate the probability of an output


code:
result = set()
for items in paths:
    splitted = items.split("/")[:-1]
    for idx in xrange(1, len(splitted) + 1):
        result.add("/".join(splitted[:idx]))

only_dirs = sorted(result)
print only_dirs

rawDescription: Extract directories from a list of paths


code:
greeting = ['hello','my','name','is','bob','how','are','you','my','name','is']

def find_sub_list(sl,l):
    sll=len(sl)
    for ind in (i for i,e in enumerate(l) if e==sl[0]):
        if l[ind:ind+sll]==sl:
            return ind,ind+sll-1

print find_sub_list(['my','name','is'], greeting)
# (1, 3)

rawDescription: Find starting and ending indices of sublist in list


code:
class A:
   def __str__(self):
       return '1'
   def __repr__(self):
       return 'A()'

print A(), A()
print (A(), A())

rawDescription: pyparsing: getting results from parsed data

description: Pyparsing: getting results from parsed data


code:
from collections import defaultdict

flags = defaultdict(lambda : None)
flags.update({'F1':True,'F3':True,'F8':True})

for f in 'F1 F2 F3 F4 F5 F6 F7 F8'.split():
    print f, '->', flags[f]

rawDescription: Pythonic representation of a list of values with optional flags


code:
def f(t, a, b, c):
    print t, a, b, c
    return a + b + c

func = foo([1,2,None,4,None,6], f, 3)
print func("foo", 3, 5)

rawDescription: List defining the list of variable of function in Python


code:
with open('ing words.txt', 'r') as file:
    print pattern.findall(file.read())
    print re.findall(r'>(\w+ing<', file.read())

rawDescription: TypeError: findall() takes at least 2 arguments (1 given)


code:
import sqlite3
ins = '''\
hello hello hello
hello hello hello hello hello
hello hello hello hello
'''.splitlines()
parseTable = []
for line in ins:
    row = line.split()[:5]
    row.extend([None] * (5 - len(row)))
    parseTable.append(row)

conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute('''CREATE TABLE sample (Item_0 TEXT, Item_1 TEXT, Item_2 TEXT, Item_3 TEXT, Item_4 TEXT)''')
query_string = 'INSERT INTO sample VALUES (?, ?, ?, ?, ?)'
c.executemany(query_string, parseTable)
conn.commit()
c.execute('SELECT * FROM sample')
for row in c:
    print row

rawDescription: transfer textfile contents into database python

description: Transfer textfile contents into database python


code:
file = open('c:\\1.txt','r')
searchLetter = "A"
for line in file.readlines():
    if searchLetter in line:
        positionOfA = line.index(searchLetter)
        if line[positionOfA+1].isdigit() & line[positionOfA+2].isdigit():
            print searchLetter+line[positionOfA+1]+line[positionOfA+2]

rawDescription: Read a file, and print a specific line of it in Python


code:
from collections import defaultdict
d=defaultdict(list)
for word in li:
    d[len(word)].append(word)

result=[d[n] for n in sorted(d, reverse=True)]
print result

rawDescription: Group a list by word length

description: Group a list by word length


code:
nmax = np.max(n)
arg_max = None
for j, _n in enumerate(n):
    if _n == nmax:
        arg_max = j
        break
print b[arg_max]

rawDescription: Find the x value corresponding to a histogram max


code:
for tr in tableClassResults.find_all('tr'):
    th = tr.find('th')
    print "th = ", th
    td = tr.find('td')
    print "td = ", td

rawDescription: BeautifulSoup HTML extracting tabular data; loop <tr> <th> <td>


code:
number = ord(unicode_char)
print 'number = {0:d} = 0x{0:04x}'.format(number)
# output: number = 8320 = 0x2080

rawDescription: Generate python unicode character from int value


code:
def echo_fname(f):
    def newfun():
       print f.__name__
       f()
    return newfun

rawDescription: What's the meaning of '@' in python code?


code:
src = """
BEGIN_META
    stuff
    to
    discard
END_META
BEGIN_DB
    header
    to
    discard

    1data I
    1wish to
    1extract
 END_DB

BEGIN_META
    stuff
    to
    discard
END_META
BEGIN_DB
    header
    to
    discard

    2data I
    2wish to
    2extract
 END_DB
"""

src = iter(src.splitlines())
for chunk in chunks(src):
    for line in chunk:
        print line.strip()
    print

rawDescription: Lazy parse a stateful, multiline per record data stream in Python?


code:
from PyQt4 import QtGui, QtCore
import sys
from PyQt4.QtCore import Qt

class Label(QtGui.QLabel):
    def __init__(self, img):
        super(Label, self).__init__()
        self.setFrameStyle(QtGui.QFrame.StyledPanel)
        self.pixmap = QtGui.QPixmap(img)

    def paintEvent(self, event):
        size = self.size()
        painter = QtGui.QPainter(self)
        point = QtCore.QPoint(0,0)
        scaledPix = self.pixmap.scaled(size, Qt.KeepAspectRatio, transformMode = Qt.SmoothTransformation)
        # start painting the label from left upper corner
        point.setX((size.width() - scaledPix.width())/2)
        point.setY((size.height() - scaledPix.height())/2)
        print point.x(), ' ', point.y()
        painter.drawPixmap(point, scaledPix)

class Main(QtGui.QWidget):
    def __init__(self):
        super(Main, self).__init__()
        layout = QtGui.QGridLayout()
        label = Label(r"/path/to/some/image.png")
        layout.addWidget(label)
        layout.setRowStretch(0,1)
        layout.setColumnStretch(0,1)

        self.setLayout(layout)
        self.show()

if __name__ =="__main__":
    app = QtGui.QApplication(sys.argv)
    widget = Main()
    sys.exit(app.exec_())

rawDescription: Resizing QPixmap while maintaining aspect ratio


code:
x = 10
def func():
    print x
    x = 5

func()

rawDescription: How to bring variables into functions - Python

description: Bring variables into functions - Python


code:
for word in sentence:
    if word in map_:
        print map_[word]
    else:
        print word

rawDescription: How to use two list in a file together?

description: Use two list in a file together


code:
import numpy as np
import scipy.ndimage as ndimage

x = np.array([[1.1, 0.0, 1.1, 0.9, 0.9],
              [1.1, 0.0, 1.1, 0.9, 0.9],
              [3.3, 0.0, 3.3, 4.4, 4.4],
              [3.3, 0.0, 3.3, 4.4, 4.4]])
ignored_val = 0.0
labels = np.zeros(data.shape, dtype=np.int)

i = 1
for val in np.unique(x):
    if val != ignored_val:
        labels[x == val] = i
        i += 1

# Now we can use the "labels" array as input to find_objects
for i, item in enumerate(ndimage.find_objects(labels), start=1):
    print i, ':'
    print x[item], '\n'

rawDescription: How to select all locations of unique elements in numpy 2d array with bounding box around them?

description: Select all locations of unique elements in numpy 2d array with bounding box around them


code:
from bs4 import BeautifulSoup
import csv
import urllib2

# get page source and create a BeautifulSoup object based on it
print "Reading page..."
page = urllib2.urlopen("https://silkroad5v7dywlc.onion.to/index.php?action=printpage;topic=28536.0")
soup = BeautifulSoup(page)

# if you look at the HTML all the titles, dates,
# and authors are stored inside of <dt ...> tags
metaData = soup.find_all("dt")

# likewise the post data is stored
# under <dd ...>
postData = soup.find_all("dd")

# define where we will store info
titles = []
authors = []
times = []
posts = []

# now we iterate through the metaData and parse it
# into titles, authors, and dates
print "Parsing data..."
for html in metaData:
    text = BeautifulSoup(str(html).strip()).get_text().encode("utf-8").replace("\n", "") # convert the html to text
    titles.append(text.split("Title:")[1].split("Post by:")[0].strip()) # get Title:
    authors.append(text.split("Post by:")[1].split(" on ")[0].strip()) # get Post by:
    times.append(text.split(" on ")[1].strip()) # get date

# now we go through the actual post data and extract it
for post in postData:
    posts.append(BeautifulSoup(str(post)).get_text().encode("utf-8").strip())

# now we write data to csv file
# ***csv files MUST be opened with the 'b' flag***
csvfile = open('silkroad.csv', 'wb')
writer = csv.writer(csvfile)

# create template
writer.writerow(["Time", "Author", "Title", "Post"])

# iterate through and write all the data
for time, author, title, post in zip(times, authors, titles, posts):
    writer.writerow([time, author, title, post])

# close file
csvfile.close()

# done
print "Operation completed successfully."

rawDescription: BeautifulSoup - scraping a forum page

description: BeautifulSoup - scraping a forum page


code:
import numpy as np

# Lists can be modified, but preserve order. That's important in this case.
names = ['Name1', 'Name2', 'Name3']
# It's "S3" instead of "a3" for a string field in numpy, by the way
formats = ['S3', 'i4', 'f8']

# It's often cleaner to specify the dtype this way instead of as a giant string
dtype = dict(names=names, formats=formats)

# This won't preserve the order we're specifying things in!!
# If we iterate through it, things may be in any order.
header = dict(Name1='abc', Name2=456, Name3=3.45)

# Therefore, we'll be sure to pass things in in order...
# Also, np.array will expect a tuple instead of a list for a structured array...
values = tuple(header[name] for name in names)
header_array = np.array(values, dtype=dtype)

# We can access field in the array like this...
print header_array['Name2']

# And dump it to disk (similar to a C struct) with
header_array.tofile('test.dat')

rawDescription: Writing to numpy array from dictionary

description: Writing to numpy array from dictionary


code:
import itertools
import collections

person_to_networks = collections.defaultdict(set)
person_to_friends = collections.defaultdict(set)

def format_name(name):
    return name.split(',')[1][1:] + ' ' + name.split(',')[0]

with open('exampletext') as f:
    #cheap hack so that we detect the need for a new leader on the first line
    lines = [''] + [line.strip() for line in f]

for line in lines:
    if line == '':
        new_leader = True
    else:
        if new_leader:
            leader = format_name(line)
            new_leader = False
        else:
            if ',' in line:
                person_to_friends[leader].add(format_name(line))
            else:
                person_to_networks[leader].add(line)

print 'Person to Networks'
for p in person_to_networks:
    print '%s: %r' % (p, [e for e in person_to_networks[p]])

print '\nPerson to Friends'
for p in person_to_friends:
    print '%s: %r' % (p, [e for e in person_to_friends[p]])

rawDescription: New to python Creating two Dictionaries from a file need specific lines


code:
items = [1,2,3,4,5,6,7,8,9]

def SplitSets(iterable,set_size):
    for i, elem in enumerate(iterable):
        yield i/set_size, i%set_size, elem

for a,b,c in SplitSets(items,3):
    print a,b,c

rawDescription: Python: Split list in to sets

description: Python: Split list in to sets


code:
from inspect import currentframe

class NamePrint(object):

    def __getattribute__(self, name):
        frame = currentframe().f_back
        locs, globs = frame.f_locals, frame.f_globals
        value = locs[name] if name in locs else globs.get(name, "???")
        print name, "=", value
        del frame

n = 42
name_print = NamePrint()
name_print.n

rawDescription: Python: a way to avoid <<print "variable_name =", variable_name>>?


code:
test="""track(1, 2.0, 4000, 3, 300).
track(2, 1.0, 9000, 5, 500).
track(3, 7.0, 9000, 2, 200)."""

result = prolog_sentences.parseString(test)

print result['facts'][0]['arguments'].asList()
# outputs ['1', '2.0', '4000', '3', '300']

print result['facts'][1]['relationship']
# outputs 'track'

print result['facts'][2]['arguments'][1]
# outputs ['7.0']

rawDescription: prolog fact lexical analyser in python

description: Prolog fact lexical analyser in python


code:
s="TTCCGACTGACTTACGAAAAAA"
import re
m = re.search('A{3,}',s)
if m:
    print"A is more than 3 times"

rawDescription: Python - How to identify a string containing a sequence of repeated letters


code:
from functools import wraps
import time

def timed(f):
    """Time a function."""
    @wraps(f)
    def wrapper(*args, **kwds):
        start = time.clock()
        result = f(*args)
        end = 1000 * (time.clock() - start)
        print '%s: %.3f ms' % (f.func_name, end)
        return result
    return wrapper

rawDescription: How do you have python scripts display how much time it takes to execute each process?

description: Have python scripts display how much time it takes to execute each process


code:
for result in grammar.scanString(names):
    print result[0].asDict()

rawDescription: Pyparsing strings where the order is unknown and some items may be missing


code:
import sys
from OuterPackage.SubPackage import TestWithGlobals
print "In Script1", id(sys.modules['OuterPackage.SubPackage.TestWithGlobals'])
import Script2
print TestWithGlobals.__name__

print "TestWithGlobals:", TestWithGlobals.global_string
Script2.MakeStringBall()
print "TestWithGlobals:", TestWithGlobals.global_string
print "Script2.TestWithGlobals:", Script2.TestWithGlobals.global_string

rawDescription: Import from subpackage or full path difference


code:
for a,b in states.items():
   print "%s is abbreviated %s" % (a,b)

for b,a in states.items():
   print "%s is abbreviated %s" % (b,a)

rawDescription: How does this python dictionary code actually work?


code:
for fname in itertools.chain(*map(glob.iglob, sys.argv[1:])):
    print fname

rawDescription: List of files (which might contain wildcards) as arguments of a Python script


code:
import numpy as np
a = np.random.randint(-1, 2, (5,5))
a

b[a<0] = '.'
b = np.empty(a.shape, dtype='S1')
b[a<0] = '_'
b[a==0] = ''
b[a>0] = '#'
b

for row in b:
    for elm in row:
        print elm,
    print

rawDescription: "Simplest" way to graphically represent a matrix


code:
class Game(object):
        def __init__(self, num_players):
                self.players = {}
                for i in range(num_players):
                        self.players[input("Enter name for Player%s" % i)] = 0
        def play(self):
                while all([x < 10 for x in self.players.values()]):
                        self.players[input("enter name for player ")] +=1
                for name, score in self.players.iteritems():
                        print "Game over"
                        print "%s: %s" % (name, score)

if __name__ == '__main__':
        Game(2).play()

rawDescription: Modifying code avoiding break statements


code:
foo = 'appleorangepearbanana '
foo = foo.replace('apple', 'x').replace('banana', 'x').replace('pear', 'x').replace('orange','y')
print foo

rawDescription: Replace multiple strings in pile of text python


code:
x=6
def example2():
   print x
   print (x+10)
print x  # this will be called first, so the first output is 6
example2() # then this, which will print 6 and then 16

rawDescription: confusion in output of local and global variables in python

description: Confusion in output of local and global variables in python


code:
def visit_item(item):
    print 'visit_item(): called on %r' % item

def traverse_list(l):
    for item in l:
        if isinstance(item, (list, tuple)):
            traverse_list(item)
        else:
            visit_item(item)

rawDescription: Multidimensional Enumeration, how to avoid Code Macro Generation


code:
import pandas as pd

def stretch(start_date, end_date, value, freq):
    freq_dict = {'d': pd.datetools.day,
                 'h': pd.datetools.Hour(1)}
    dr = pd.DateRange(start_date, end_date, offset=freq_dict[freq])
    return pd.TimeSeries(value / dr.size, index=dr)

print stretch('2011-01-01 00:00', '2011-01-20 00:00', 200, 'd')

rawDescription: Convert interval datetime values to any-frequency time series


code:
for _ in range(10):
    total = total + (total * .10)
    print total

rawDescription: repeating a function in python (2.7)

description: Repeating a function in python (2.7)


code:
import csv
with open('file.csv', 'rb') as open_csv:
    csv_reader = csv.reader(open_csv)

print csv_reader

rawDescription: importing a text file in python

description: Importing a text file in python


code:
import types
import os
import os.path
import imp

class PluginMeta(type):
    def __new__(cls, name, bases, dct):
        modules = [imp.load_source(filename, os.path.join(dct['plugindir'], filename))
                    for filename in os.listdir(dct['plugindir']) if filename.endswith('.py')]
        for module in modules:
            for name in dir(module):
                function = getattr(module, name)
                if isinstance(function, types.FunctionType):
                    dct[function.__name__] = function
        return type.__new__(cls, name, bases, dct)

class User(object):
    __metaclass__ = PluginMeta
    plugindir = "path/to/the/plugindir"

    def foo(self):
        print "foo"

user = User()
print dir(user)

rawDescription: Adding functions from other files to a Python class


rawDescription: How to turn a Dataframe into an Array with certain specifications on Python Pandas

description: Turn a Dataframe into an Array with certain specifications on Python Pandas


code:
import timeit
from bisect import bisect_left

def doit():
    f_in = open('b.in', 'r')
    n, m = [int(i) for i in f_in.readline().rstrip().split(' ')]
    a = [int(i) for i in f_in.readline().rstrip().split(' ')]
    found = {}
    indices = []
    for i in range(n - 1, 0, -1):
        if not a[i] in found:
            indices.append(i+1)
            found[a[i]] = True

    indices.reverse()
    length = len(indices)
    for i in range(m):
        l = int(f_in.readline().rstrip())
        index = bisect_left(indices, l)
        print length - index

if __name__ == "__main__":
    print (timeit.timeit('doit()', setup="from bisect import bisect_left;from __main__ import doit", number=10))

rawDescription: Efficient algorithm for counting unique elements in "suffixes" of an array


code:
#!/usr/bin/python

import threading
import time

def worker():
    if l._RLock__owner is threading.current_thread():
        print "I own the lock"
    else:
        print "I don't own the lock"
    l.acquire()
    if l._RLock__owner is threading.current_thread():
        print "Now I own the lock"
    else:
        print "Now I don't own the lock"
    time.sleep(5)
    l.release()

if __name__ == "__main__":
    l = threading.RLock()
    thds = []
    for i in range(0, 2):
        thds.append(threading.Thread(target=worker))
        thds[i].start()

    for t in thds:
        t.join()

rawDescription: python how to know if current thread is holding a lock

description: Python how to know if current thread is holding a lock


code:
def okClicked(self, event):
    self.date = self.calend.GetDate()
    print self.date
    return self.date

rawDescription: wxpython event handling between multiple classes

description: Wxpython event handling between multiple classes


code:
s = StringIO('1234 Hello World -13.48 -678 12.45')
for data in scan_input('%d %s %s %f %d %f', s): print repr(data)

rawDescription: Python Stream Extraction

description: Python Stream Extraction


code:
class WSHandler(tornado.websocket.WebSocketHandler):
  connections = set()

  def open(self):
     self.connections.add(self)
     print 'New connection was opened'
     self.write_message("Conn!")

  def on_message(self, message):
     print 'Got :', message
     self.write_message("Received: " + message)

  def on_close(self):
     self.connections.remove(self)
     print 'Conn closed...'

rawDescription: python tornado send message to all connections

description: Python tornado send message to all connections


code:
import string

shift = 221

letters = string.ascii_letters + string.punctuation + string.digits
text = raw_input("Please enter text\n")

e = ''.join(letters[letters.index(l) + shift % len(letters)]
            if letters.index(l) is not None else ' ' for l in text)
print e

rawDescription: Python cypher: how to build a complexer shifter system?


code:
for page_title in page_titles:
    # All the stuff that has to be done for each single title goes here.
    # I'm not entirely clear on what you're doing, but I think that's this part:
    parent = None
    if selection is not None:
        parent = feed.entry[selection - 1]

    new_entry = self.client.CreatePage(
        'webpage', page_title, '<b>Your html content</b>',
        parent=parent)
    if new_entry.GetAlternateLink():
        print 'Created. View it at: %s' % new_entry.GetAlternateLink().href

rawDescription: get a list of comma separated values in python and use that list to perform a command as many times as necessary with each value

description: Get a list of comma separated values in python and use that list to perform a command as many times as necessary with each value


code:
import sys
print(sys.version)
import _collections as _cl
print "_collections:", dir(_cl)
import collections as cl
print "collections:", dir(cl)

rawDescription: OrderedDict in IronPython missing?

description: OrderedDict in IronPython missing


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(post.description)
texts = soup.findAll(text = True)
print ''.join(texts)

rawDescription: how to parse HTML tags in RSS feed in Python

description: Parse HTML tags in RSS feed in Python


code:
def solve(f, n, m):
    lis = []
    for line in f:
        if len(lis) > m:
            yield lis[:m]
            lis = lis[m:]
        lis.extend(map(int, line.split()))
    for i in xrange(0, len(lis), m):
        yield lis[i:i+m]

with open('abc1') as f:
    n, m = map(int, next(f).split())
    # Now you can either load the whole array at once using the list() call,
    # or use a simple iteration to get one row at a time.
    matrix = list(solve(f, n, m))
    print matrix

rawDescription: reading a file and storing values in a matrix using python

description: Reading a file and storing values in a matrix using python


code:
# Python 2.7

import httplib

conn = httplib.HTTPConnection(host='www.boursorama.com',timeout=30)

req = '/includes/cours/last_transactions.phtml?symbole=1xEURUS'

try:
    conn.request('GET',req)
except:
     print 'echec de connexion'

content = conn.getresponse().read()

print content

rawDescription: Web scraping urlopen in python

description: Web scraping urlopen in python


code:
from apscheduler.scheduler import Scheduler

def job_def(var1, var2):
    print "%s - %s" % (str(var1), str(var2))

s = Scheduler()

s.add_cron_job(job_def, args=['hello', 'there'], second='*/30')

s.start()

rawDescription: python: advanced python scheduler - cron style scheduling - pass function parameters

description: Python: advanced python scheduler - cron style scheduling - pass function parameters


code:
def swap(a, b):
    a = np.copy(a)
    b = np.asarray(b, dtype=np.bool)
    a[b] = a[b, ::-1]  # equivalent to: a[b] = np.fliplr(a[b])
    return a

# the following is faster, but modifies the original array
def swap_inplace(a, b):
    b = np.asarray(b, dtype=np.bool)
    a[b] = a[b, ::-1]

print swap(a=[[1,2],[2,3],[3,4],[3,5],[5,6]],b=[0,0,0,1,1])

rawDescription: Shuffle in one dimension of a matrix(effeciently)?


code:
for key in crucial.keys():
    if key in dishes.keys():
        print dishes[key]

rawDescription: Comparing Two Dictionaries Key Values and Returning the Value If Match


code:
import multiprocessing

def f(x):
    created = multiprocessing.Process()
    current = multiprocessing.current_process()
    print 'running:', current.name, current._identity
    print 'created:', created.name, created._identity
    return x * x

p = multiprocessing.Pool()
print p.map(f, range(6))

rawDescription: Get a unique ID for worker in python multiprocessing pool


code:
def my_import(*args,**kwargs):
  ret = old_import(*args,**kwargs)
  print ret
  return ret
__builtins__.__import__ = my_import
import datetime
import django

rawDescription: Do something every time a module is imported


code:
import cookielib

import mechanize

base_url = "http://www.ananda-pur.de/23.html"

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_robots(False)
br.set_handle_equiv(False)
br.set_handle_redirect(True)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent',
                  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
page = br.open(base_url, timeout=10)

links = {}
for link in br.links():
    if link.url not in links:
        links[link.url] = {'count': 1, 'texts': [link.text]}
    else:
        links[link.url]['count'] += 1
        links[link.url]['texts'].append(link.text)

# printing
for link, data in links.iteritems():
    print "%s - %s - %s - %d" % (base_url, link, ",".join(data['texts']), data['count'])

rawDescription: Python web scraper, same link with different text, counting


code:
tup[0].splitlines()
for line in tup[0].splitlines():
    print line

rawDescription: split a string in python on linebreaks

description: Split a string in python on linebreaks


code:
diff = difflib.unified_diff(lines1, lines2, fromfile='file1', tofile='file2', lineterm='', n=0)
lines = list(diff)[2:]
added = [line[1:] for line in lines if line[0] == '+']
removed = [line[1:] for line in lines if line[0] == '-']

print 'additions:'
for line in added:
    print line
print
print 'additions, ignoring position'
for line in added:
    if line not in removed:
        print line

rawDescription: Compare two files report difference in python


code:
print compute(master, skip={'im':1, 'is':1, 'overflow':1})
print compute(master, skip={'im':2, 'is':1, 'overflow':1})

rawDescription: How to replace different blocks of text in all combinations using awk?

description: Replace different blocks of text in all combinations using awk


code:
from base64 import b64encode
from reportlab.lib import units
from reportlab.graphics import renderPM
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.graphics.shapes import Drawing

def get_barcode(value, width, barWidth = 0.05 * units.inch, fontSize = 30, humanReadable = True):

    barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable)

    drawing_width = width
    barcode_scale = drawing_width / barcode.width
    drawing_height = barcode.height * barcode_scale

    drawing = Drawing(drawing_width, drawing_height)
    drawing.scale(barcode_scale, barcode_scale)
    drawing.add(barcode, name='barcode')

    return drawing

def get_image():

    barcode = get_barcode(value = '01234567890', width = 600)
    data = b64encode(renderPM.drawToString(barcode, fmt = 'PNG'))
    print '<img src="data:image/png;base64,{0}">'.format(data)

rawDescription: Code128 Barcode as HTML Image Tag with Data URI Scheme in Python


code:
inputs = input("Enter something").split() # read input and split it
print inputs

rawDescription: Python Basic Input Confusion

description: Python Basic Input Confusion


code:
print dictionary[0]
{'key2': 2, 'key1': 1}

print dict_list[0]
{'key2': 2, 'key1': 1}

rawDescription: How convert list of dictionaries to a dictionary of dictionaries in Python


code:
my_file = io.open("myfile.txt", "rb")
my_file_buffer = io.BufferedReader(my_file, buffer_size=1) # Not as performant but a larger buffer will "eat" into the binary data
my_file_text_reader = io.TextIOWrapper(my_file_buffer, encoding="utf-8")
string_buffer = ""

while True:
    while "near the end" not in string_buffer:
        string_buffer += my_file_text_reader.read(1) # read one Unicode char at a time

    # binary data must be next. Where do we get the binary length from?
    print string_buffer
    data = my_file_buffer.read(3)

    print data
    string_buffer = ""

rawDescription: Reading binary and text from same file in Python


code:
rows = [
    {'long':1, 'lat':4, 'time': '7:00'},
    {'long':2, 'lat':5, 'time': '8:00'},
    {'long':3, 'lat':6, 'time': '9:00'}
]
data = [
    {'long':1,  'lat':4, },
    {'long':20, 'lat':5, },
    {'long':3,  'lat':6, }
]

for row in rows:
    for i, d in enumerate(data):
        if row['long'] == d['long'] and row['lat'] == d['lat']:
           data[i]['time'] = row['time']

for x in data:
    print x

rawDescription: Python: how to search dictionary and copy values?


code:
import subprocess

initial_data = """
import sys
print sys.path
"""

for test in [1,2,3] :
    p = subprocess.Popen("C:/python26/python", shell=True, bufsize=512,
          stdin=subprocess.PIPE,  stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)

    p.stdin.write(initial_data)
    print p.communicate()

rawDescription: Python: Running command line application to the script. Sending parameters in files


code:
import pymel.core as pm

const = pm.geometryConstraint('pSphere1', 'locator1', n='geoConstraint')
print const
const.rename('fred')
print const

rawDescription: How do I use the GeometryConstraint class?

description: Use the GeometryConstraint class


code:
from BeautifulSoup import BeautifulSoup, Comment

soup = BeautifulSoup('''

paragraph 1

paragraph 2

paragraph 3

''')

def right_comment(e):
    return isinstance(e, Comment) and e == 'some comment text'

e = soup.find(text=right_comment)

print e.findPreviousSibling('p')

rawDescription: Beautiful Soup - identify tag based on position next to comment


code:
import csv

fi = open("filein.txt", "rb")
reader = csv.reader(fi,delimiter=' ')

dict = {}
for row in reader:
    if row[0] in dict:
        if float(dict[row[0]][-1]) < float(row[-1]):
            dict[row[0]] = row[1:]
    else:
        dict[row[0]] = row[1:]
print dict

rawDescription: Conditional filtering of the csv file with duplicates in python


code:
for x in your_list:
    current_date = x[0][:10]
    print current_date

rawDescription: Slicing a list from csv.reader

description: Slicing a list from csv.reader


code:
from math import floor

def floored_percentage(val, digits):
    val *= 10 ** (digits + 2)
    return '{1:.{0}f}%'.format(digits, floor(val) / 10 ** digits)

print floored_percentage(0.995, 1)

rawDescription: Python string.format() percentage to one decimal place


code:
s = u"100ஆம்"
match = re.match(pattern, s, re.UNICODE)
before, digits, after = match.groups()
print before
print digits
print after

rawDescription: identifying if the character is a digit or Unicode character within a word in python

description: Identifying if the character is a digit or Unicode character within a word in python


code:
import re

def showit(r, inp):
  ms = r.finditer(inp)
  for i,m in enumerate(ms):
    print "  match", i, " start:", m.start(0), " end:", m.end(0)
  print ""

print "regex x$ against x\\nx"
showit(re.compile("x$"), "x\nx")

print "regex x$ against x\\nx\\n"
showit(re.compile("x$"), "x\nx\n")

print "regex x$ re.MULTILINE against x\\nx"
showit(re.compile("x$", re.MULTILINE), "x\nx")

rawDescription: Python regex erronously matches trailing newlines


code:
# module dataparser_user.py

import dataparser as dp

data = """\
XX20120101NAME1CITYA
XY20120101NAME2CITYB""".splitlines()

for d in data:
    print d, dp.parse((dp.INFO01, dp.INFO03), d)

# or use partial to define a function object that takes your
# subset number of slices
from functools import partial
specific_parse = partial(dp.parse, (dp.INFO01, dp.INFO03))

for d in data:
    print d, specific_parse(d)

rawDescription: Designing a reusable parser in python

description: Designing a reusable parser in python


code:
from collections import Counter

truth      = [1, 2, 1, 2, 1, 1, 1, 2, 1, 3, 4, 1]
prediction = [1, 1, 2, 1, 1, 2, 1, 2, 1, 4, 4, 3]

# make confusion matrix
confusion_matrix = Counter()
for t, p in zip(truth, prediction):
    confusion_matrix[t,p] += 1

# print confusion matrix
labels = set(truth + prediction)
print "t/p",
for p in sorted(labels):
    print p,
print
for t in sorted(labels):
    print t,
    for p in sorted(labels):
        print confusion_matrix[t,p],
    print

rawDescription: How to find true positives, true negatives, false positives, false negatives in Python

description: Find true positives, true negatives, false positives, false negatives in Python


code:
import networkx as nx
edgelist = [
    (1, 3, {'weight':5250}),
    (1, 4, {'weight': 74}),
    (1, 5, {'weight': 3659})]

G = nx.Graph(edgelist)
for a, b, data in sorted(G.edges(data=True), key=lambda (a, b, data): data['weight']):
    print('{a} {b} {w}'.format(a=a, b=b, w=data['weight']))

rawDescription: Sorting a networkx graph object Python

description: Sorting a networkx graph object Python


code:
for group,value in tab.groupby('date'):
    print group
    print pd.to_datetime(value.time).diff().mean()

## 2012-05-01
## 0   00:14:03.901400
## dtype: timedelta64[ns]
## 2012-05-02
## 0   00:06:21.062500
## dtype: timedelta64[ns]

rawDescription: Average inter signout time in pandas dataframe


code:
tuples1 = [('A', 4), ('D', 8), ('L', 38)]
tuples2 = [('A', 24), ('B', 84), ('C', 321), ('D', 19), ("L", 15)]
dict_tuples2 = dict(tuples2)
print [(char, count, dict_tuples2.get(char, 0)) for char, count in tuples1]

rawDescription: match strings in tuples - python

description: Match strings in tuples - python


code:
class Node:
    def __init__(self, value=None, *children):
        self.value = value
        self.children = children

def evalTree(t, variables):
    if t.value == "Or":
        return evalTree(t.children[0], variables) or evalTree(t.children[1], variables)
    elif t.value == "Equals":
        return evalTree(t.children[0], variables) == evalTree(t.children[1], variables)
    elif t.value == "Literal":
        return t.children[0].value
    elif t.value == "Variable":
        name = t.children[0].value
    else:
        raise Exception("Unrecognized node type")

t = Node("Or",
    Node("Equals",
        Node("Variable", Node("element")),
        Node("Literal", Node("Apple"))
    ),
    Node("Or",
        Node("Equals",
            Node("Variable", Node("element")),
            Node("Literal", Node("Apple"))
        ),
        Node("Equals",
            Node("Variable", Node("element")),
            Node("Literal", Node("Apple"))
        )
    )
)

print evalTree(t,{"element": "Apple"})
print evalTree(t,{"element": "Duck"})
print evalTree(t,{"element": "Banana"})

rawDescription: Create python functions from expression trees


code:
import numpy as np

def dirichlet_sample(alphas):
    """
    Generate samples from an array of alpha distributions.

    `alphas` must be a numpy array with shape (n, k).
    """
    r = np.random.standard_gamma(alphas)
    r /= r.sum(-1).reshape(-1, 1)
    return r

if __name__ == "__main__":
    alphas = 2 ** np.random.randint(0, 4, size=(6, 3))

    np.random.seed(1234)
    d1 = dirichlet_sample(alphas)
    print "dirichlet_sample:"
    print d1

    np.random.seed(1234)
    d2 = np.empty(alphas.shape)
    for k in range(len(alphas)):
        d2[k] = np.random.dirichlet(alphas[k])
    print "np.random.dirichlet:"
    print d2

    # Compare d1 and d2:
    err = np.abs(d1 - d2).max()
    print "max difference:", err

rawDescription: Why does numpy.random.dirichlet() not accept multidimensional arrays?


code:
def has_duplicates(d):
    return len(d) != len(set(d.values()))

print has_duplicates({'a': 1, 'b': 1, 'c': 2})

rawDescription: Duplicates in a dictionary (Python)

description: Duplicates in a dictionary (Python)


code:
import random
import collections

results = collections.Counter()

for __ in range(50):
    digit1 = random.randint(0,9)
    digit2 = random.randint(0,9)
    digit3 = random.randint(0,9)
#    results.update([(digit1,digit2,digit3)])
    results.update([ '%d%d%d' % (digit1,digit2,digit3) ])

#print results

print
print '3 most common numbers:', results.most_common(3)

print
print '3 most common numbers:'
for x in results.most_common(3):
    print '%s - %d times' % x

grouped_results = {}

for number, times in results.items():
    if times not in grouped_results:
        grouped_results[times] = []
    grouped_results[times].append(number)

print
print 'grouped results:', grouped_results

for times, numbers in grouped_results.items():
    print
    print 'drawn %d time(s):' % times
    print numbers

rawDescription: First Python program, "lotto prediction"


code:
file2_lines = list(file2)
for line1 in file1:
    print line1
    for line2 in file2_lines:
        print line2

rawDescription: Compare two files lines with python

description: Compare two files lines with python


code:
import re

txt='''\
# Version: #.#"

# Date Created: ##/##/####"

etc.

One of my problems is that the version numbers and dates wont always be in that format. Sometimes, they may look like:

# Version #"

# Date Created: ##/####'''

print 'versions found:', re.findall(r'^\s*#+\s*Version:?\s*(.*)$', txt, re.M)
print 'dates found:', re.findall(r'^\s*#+\s* Date Created:?\s*(.*)$', txt, re.M)

rawDescription: Trying to pull out dates and version numbers from script comments, using regex


code:
for i in range(len(obj)):
    print 'obj [%s]:' % i, obj[i].centroid

rawDescription: How to print the coordinates of all the objects in an image, in python

description: Print the coordinates of all the objects in an image, in python


code:
def lock_choice(self, combos):
    if combos.isEnabled(True):
        combos.setEnabled(False)
        print 'Choice {choice} locked'.format(choice=combos.currentText())

rawDescription: Lock a choice in PyQt4 QComboBox using a QCheckBox


code:
def timeout(self, msg, opp, turn):
    time.sleep(60)
    if not turn_is_already_done:
        print "Timeout"

rawDescription: Python - How to make a background response timeout function


code:
def listen(self, ip, port):
    print "Starting!"
    sock = socket.socket(AF_INET, SOCK_DGRAM)
    sock.bind((ip, port))
    kq = select.kqueue()
    kev0 = select.kevent(sock)
    kev1 = select.kevent(self.pipe)
    kq.control([kev0, kev1], 0, 0)
    while True: #this loop never breaks! so this whole function blocks forever like this
        revents = kq.control([], 1, None)
        if revents:
            event = revents[0]
            if event.flags & select.KQ_EV_EOF:
                new_event = select.kevent(event.ident, flags=select.KQ_EV_CLEAR)
                kq.control([new_event], 0, 0)
            else:
                print event

rawDescription: Python kqueue with multiple events

description: Python kqueue with multiple events


code:
print {'--files': "a,b,c"}.get("--files", NoneFound).split(',')
print {}.get("--files", NoneFound).split(',')

rawDescription: In Python, what is a minimal way of assigning a value to a variable depending on whether a dictionary contains a key?


code:
import parsedatetime
p = parsedatetime.Calendar()
print p.parse("25th this month")
print p.parse("25th July")
print p.parse("25th July 2014")

rawDescription: Python NLTK interpret a fixed pattern of sentence and tokenize it


code:
def records(file_path):
    with open(file_path) as f:
        chunk = []
        for line in f:
            if 'name' in line:
                if chunk:
                    yield chunk
                chunk = [line]
            else:
                chunk.append(line)
        if chunk:
            yield chunk

for record in records('data.txt'):
    print '--------'
    print ''.join(record)

rawDescription: Split the text file in python

description: Split the text file in python


code:
import requests
from lxml import html

URL = "http://www.realclearpolitics.com/epolls/other/president_obama_job_approval-1044.html"
response = requests.get(URL)
tree = html.fromstring(response.content)

rcp_approve = '//table[@class="chart_legend small_legend"]/tbody/tr/td[@class="candidate"][1]/div[1]/span/text()'
rcp_disapprove = '//table[@class="chart_legend small_legend"]/tbody/tr/td[@class="candidate"][2]/div[1]/span/text()'

rcp_approve = float(tree.xpath(rcp_approve)[0])
rcp_disapprove = float(tree.xpath(rcp_disapprove)[0])

print "Obama's approve rate: {}".format(rcp_approve)
print "Obama's disapprove rate: {}".format(rcp_disapprove)

rawDescription: Using lxml and xpath to get text from a webpage


code:
t= "tweet_text" #I'm not sure how do you use this variable
returnedSentiment= sTwitter.getTweetSentiment(t) #This is how you run your function
print t

rawDescription: Assigning to global variable inside a function doesn't work


code:
from rdflib import Graph, URIRef

def is_person(uri):
    uri = URIRef(uri)
    person = URIRef('http://dbpedia.org/ontology/Person')
    g = Graph()
    g.parse(uri)
    resp = g.query(
        "ASK {?uri a ?person}",
        initBindings={'uri': uri, 'person': person}
    )
    print uri, "is a person?", resp.askAnswer
    return resp.askAnswer

uri = URIRef('http://dbpedia.org/resource/Richard_Nixon')
person = URIRef('http://dbpedia.org/ontology/Person')

nixon = 'http://dbpedia.org/resource/Richard_Nixon'
is_person(nixon)

pear = 'http://dbpedia.org/resource/Pear'
is_person(pear)

rawDescription: Test for a resource type of a dbpedia page using rdflib and python


code:
class SubClass(dict):
    def __init__(self, *args, **kwargs):
        super(SubClass, self).__init__(*args, **kwargs)
        self.update({
            'id':None,
            'number':None,
            'oneMoreDefaultKey':None})

instance = SubClass({'city':'New York', 'year':2014})
print instance['id'], instance['year']

rawDescription: How to define default dictionary keys for sub-classed from built-in dictionary class

description: Define default dictionary keys for sub-classed from built-in dictionary class


code:
class App(Frame):
    '''the main window class'''
    def __init__(self, parent):
        Frame.__init__(self, parent)

        # create instances of MyEntry, passing whatever operators as args
        # we can make as many of these instances as we need in just a couple of lines
        # and it retains readability
        self.divide = MyEntry(self, '/')
        self.multiply = MyEntry(self, '*')

        self.divide.pack()
        self.multiply.pack()

        Button(self, text='Calculate', command=self._print_result).pack()

    def _print_result(self):
        '''print the return of the calculate method from the instances of
        the MyEntry class'''
        print self.divide.calculate()
        print self.multiply.calculate()

class MyEntry(Frame):
    '''creates two entries and a label and has a method to calculate
    the entries based on an operator'''
    def __init__(self, parent, operator): # include the operator as an arg
        Frame.__init__(self, parent)

        # make an instance variable from the operator to access it between methods
        self.operator = operator

        # make two entries
        self.num1 = Entry(self)
        self.num2 = Entry(self)

        # grid the entries and a label which contains the operator
        self.num1.grid(row=0, column=0)
        Label(self, text=self.operator).grid(row=0, column=1)
        self.num2.grid(row=0, column=2)

    def calculate(self):
        '''return the value of the two entries based on the operator specified'''
        if self.operator is '/':
            return int(self.num1.get()) / int(self.num2.get())
        elif self.operator is '*':
            return int(self.num1.get()) * int(self.num2.get())
        else:
            return

root = Tk()
App(root).pack()
mainloop()

rawDescription: How to reference Tkinter widgets inside classes in python

description: Reference Tkinter widgets inside classes in python


code:
from settings import List1
print List1

rawDescription: Importing lists from another file

description: Importing lists from another file


code:
with open('surf.txt', 'rb') as fp:
 for line in fp.readlines():
  tokens = line.strip().split(';') # this creates a list of strings
  name = tokens[0] # extract the first (left-most) string (the name)
  nums = [float(k) for k in tokens[1:]] # convert strings to floats
  mean = sum(nums) / len(nums) # compute the arithmetic mean
  print "%s has a mean of %f" % (name, mean) # print the result

rawDescription: Finding out an average in python

description: Finding out an average in python


code:
import os

def walk(top, maxdepth):
    dirs, nondirs = [], []
    for name in os.listdir(top):
        (dirs if os.path.isdir(os.path.join(top, name)) else nondirs).append(name)
    yield top, dirs, nondirs
    if maxdepth > 1:
        for name in dirs:
            for x in walk(os.path.join(top, name), maxdepth-1):
                yield x

for x in walk(".", 2):
    print x

rawDescription: Python 3 - travel directory tree with limited recursion depth


code: print s.decode("raw_unicode_escape")

rawDescription: Converting escaped unicode characters to unicode notation


rawDescription: datetime strptime - set format to ignore part of string

description: Datetime strptime - set format to ignore part of string


code:
class drag:
    def __init__(self, source):
        self.source = source
    def to(self, target):
        print 'Dragging ' + self.source + ' to ' + target

rawDescription: Simplifying code: classes as simple containers in Python


code:
import numpy as np
infile = file('input_filename.txt')
names = infile.next().strip().split()

# Create the dtype... The 'S10' indicates a string field with a length of 10
dtype = {'names':names, 'formats':['S10'] + (len(names) - 1)*[np.float]}
data = np.loadtxt(infile, dtype=dtype, delimiter=',')

# The "workers" field is now a string array
print data['workers']

# Compare this to the other fields
print data['constant']

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

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


code:
import json
obj = json.loads('[ { "id": "12345", "name": "London", "active": true, "status": "true", "version": "1.0", "tags": [ ] }, { "id": "12457", "name": "Newyork", "active": true, "status": "false", "version": "1.1", "tags": [ ] } ]')
print "Names:", ",".join(x["name"] for x in obj if x["status"] == "true")

rawDescription: Parse JSON using python to fetch value based on condition


code:
values=d["chr"]

for i,j in enumerate(values[:-1]):
    first,second=set(j),set(values[i+1])  # current value and next
    print first.intersection(second)

rawDescription: Extract list of values in dictionary for comparison?


code:
def someroutine():
    with open('test.txt', 'a') as f:
        f.write('A')
    while True:
        pass
    return 0

routine = threading.Thread(someroutine)
print 'I am working in parallel!'

rawDescription: Python loop in a coroutine

description: Python loop in a coroutine


code:
import csv

with open('14144315.csv', 'rb') as csvfile:
    rowreader = csv.reader(csvfile, delimiter=',', quotechar='`')
    for row in rowreader:
        print row

rawDescription: Remove unwanted commas from CSV using Python


code:
line = "hello, world    , 1       , 2   , 3"
parts = line.rsplit(",", 3)
print parts  # prints ['hello, world    ', ' 1       ', ' 2   ', ' 3']

rawDescription: parsing CSV files backwards

description: Parsing CSV files backwards


code:
import difflib

REF = ['455 Gateway Dr, Brooklyn, NY 11239',
       '10 Devoe St, Brooklyn, NY 11211',
       '8801 Queens Blvd, Elmhurst, NY 11373 ',
       '342 Wythe Ave, Brooklyn, NY 11249 ',
       '4488 E Live Oak Ave, Arcadia, CA 91006',
       '1134 N Vermont Ave, Los Angeles, CA 90029',
       '1101 17th St NW, Washington, DC 20036 ',
       '3001 Syringa St, Hopeful-City, AL 48798',
       '950 Laurel St, Minneapolis, KS 67467']

INPUT = ['4554 Gagate Dr, Brooklyn, NY 11239',
         '10 Devoe St, Brooklyn, NY 11211',
         '8801 Queens Blvd, Elmhurst, NY 11373 ',
         '342 Wythe Ave, Brooklyn, NY 11249 ',
         '4488 E Live Oak Ave, Arcadia, CA 91006',
         '1134 N Vermont Ave, Los Angeles, CA 90029',
         '1101 17th St NW, Washington, DC 20036 ',
         '3001 Syrinuy St, Hopeful Dam, AL 48798',
         '950 Laurel St, Minneapolis, KS 67467',
         '455 Gateway Doctor, Forgotten Place, NY 11239',
         '10 Devoe St, Brook., NY 11211',
         '82477 Queens Blvd, Elmerst, NY 11373 ',
         '342 Waithe Street, Brooklyn, MN 11249 ',
         '4488 E Live Poke Ave, Arcadia, CA 145',
         '1134 N Vermiculite Ave, Liz Angelicas, CA 90029',
         '1101 1st St NW, Washing, DC 20036 ']

def treatment(inp,reference,crit,gcm = difflib.get_close_matches):
    for input_item in inp:
        yield (input_item,gcm(input_item,reference,1000,crit))

for a,b in treatment(INPUT,REF,0.65):
    print '\n- %s\n     %s' % (a, '\n     '.join(b))

rawDescription: Slow Python regex without catastrophic backtracking


code:
name1 = "naveen"; name2 = "darshana"

name1_set=set(name1)
name2_set=set(name2)

clean1=[x for x in  name1 if x not in name2_set]
clean2=[x for x in name2 if x not in name1_set]

clean1.extend(['0']*(len(name1)-len(clean1)))
clean2.extend(['1']*(len(name2)-len(clean2)))

print clean1,clean2

rawDescription: Python Iteration Problem!

description: Python Iteration Problem!


code:
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
  print hex(pe.OPTIONAL_HEADER.ImageBase + exp.address), exp.name, exp.ordinal

rawDescription: Getting IAT and EAT from PE

description: Getting IAT and EAT from PE


code:
def print_smth(smth):
    print smth

rawDescription: Import order coding standard

description: Import order coding standard


code:
import threading
import Queue

def select(*queues):
    combined = Queue.Queue(maxsize=0)
    def listen_and_forward(queue):
        while True:
            combined.put((queue, queue.get()))
    for queue in queues:
        t = threading.Thread(target=listen_and_forward, args=(queue,))
        t.daemon = True
        t.start()
    while True:
        yield combined.get()

def main():

    c1 = Queue.Queue(maxsize=0)
    c2 = Queue.Queue(maxsize=0)
    quit = Queue.Queue(maxsize=0)

    def func1():
        for i in range(10):
            c1.put(i)
        quit.put(0)

    threading.Thread(target=func1).start()

    def func2():
        for i in range(2):
            c2.put(i)

    threading.Thread(target=func2).start()

    for which, msg in select(c1, c2, quit):
        if which is c1:
            print 'Received value from c1'
        elif which is c2:
            print 'Received value from c2'
        elif which is quit:
            print 'Received value from quit'
            return
main()

rawDescription: Python equivalent of Golang's select on channels


code:
import sys

class Task(object):

    def __init__(self, **kwargs):

        functions_mapping = {
            'cre': self.create,
            'inf': self.info,
            'sre': self.search,
            'com': self.comment
        }

        for key, value in kwargs.items():
            self.description = value
            try:
                functions_mapping.get(key)()
            except TypeError:
                print "Please provide a correct function name: {}".format(', '.join([x for x in functions_mapping.keys()]))

    def create(self):
        print "Task in progress --> {}".format(self.description)

    def search(self):
        print "Searching for task --> {}".format(self.description)

    def info(self):
        print "Task Info --> {}".format(self.description)

    def comment(self):
        print "Comment task --> {}".format(self.description)

if __name__=='__main__':
    if len(sys.argv) > 1 and len(sys.argv) < 4:
        function, description = sys.argv[1:]
        my_dict = dict()
        my_dict[function] = description
        Task(**my_dict)

rawDescription: Call to function by using argument on python


code: for ip, (mac, t) in database['PC1'][3].items(): print mac, ip

rawDescription: How to access particular element of tuples which are inside a nested dictionary Python

description: Access particular element of tuples which are inside a nested dictionary Python


code:
def OnChar(self, evt):
    try:
        print keyMap[evt.GetKeyCode()]
    except KeyError:
        print "keycode",evt.GetKeyCode(), "not found"

rawDescription: wxPython wx.KeyEvent GetKeyCode()

description: WxPython wx.KeyEvent GetKeyCode()


code:
import requests as rq
from lxml import html

mainPage = rq.get("http://umall.pk/rivo-rhythm-rx40.html")
mainTree = html.fromstring(mainPage.text)

paths_dict = {
    'title': '(//*[@id="product_addtocart_form"]/div[@class="product-name"]/text())[2]',
    'table': '//*[@id="product-attribute-specs-table"]/tr/td/text()'
}

title = mainTree.xpath(paths_dict['title'])[0].strip()
print title

table = mainTree.xpath(paths_dict['table'])
header = table[::2]
values = table[1::2]

print header
print values

rawDescription: Save Table or li items in csv dynamically using python


code:
d = {0: 'apple', 1: 'banana', 2: 'cherry', 3: 'damson', }
for k in d:
    print k
    print d[k]

rawDescription: How do I get the key of an item when doing a FOR loop through a dictionary or list in Python?

description: Get the key of an item when doing a FOR loop through a dictionary or list in Python


code:
import threading
import time

boolean = False

class ThreadClass(threading.Thread):
    def __init__(self):
        super(ThreadClass, self).__init__()

    def run(self):
        global boolean
        for i in range(6):
            print str(i), boolean
            time.sleep(1)

t = ThreadClass()
t.start()

time.sleep(3)
boolean = True

t.join()
print 'end script'

rawDescription: Global variable in thread class does not update


code:
s1 = "ATCGATCG"
s2 = "ATCGATCGFOOBAR"

counter = 0

for c1, c2 in zip(s1, s2):
  if c1 == c2:
    counter += 1
  else:
    break

print "String share the longest common prefix of length %d: %s" % (counter, s1[:counter])

rawDescription: Count matches iteratively with 2 strings indicies


code: print '\x68\x65\x6c\x6c\x6f'  # it's actually string of 5 chars

rawDescription: garbage printing value of a key-value pair in python

description: Garbage printing value of a key-value pair in python


code:
import re
m = re.search(r'ip nat (\S+)\s+(\S+)\s+(\S+)\s+(\S+) vrid 0', s)
print m.groups()
print m.group(1)

rawDescription: Regex retrieving a variable string from a string


code:
l, b = [1,1,1,1,1,1], ['-', 2, 2, 2, '-', 2]
print [item for items in zip(l, b) for item in items if item != '-']

rawDescription: Python - alternating lists

description: Python - alternating lists


code:
N=3
M=1
print sum([pair[0] for pair in list_of_pairs[M:N+1]])

rawDescription: Summing the first value in each tuple in a list over a specific range in Python


code:
import functools
import random
def foo1(param_of_foo1):
    x = param_of_foo1 + 1
    print "x is:", x

def foo2(param_of_foo2):
    y = param_of_foo2 + 2
    print "y is:", y

def foo3(useless_param):
    print "I have a parameter but I don't really need it..."

f = functools.partial(foo1, 4)
f()
list_of_foo = [functools.partial(foo1, 4), functools.partial(foo2, 10), functools.partial(foo3, 'discarded argument')]
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()
random.choice(list_of_foo)()

rawDescription: Calling random functions from a list when functions have different number of parameters


code:
with open('test.txt', 'rb') as f:
    exec(f)
    print d

rawDescription: How do you read in a text (.txt) file as a .py file in Python 2.7?

description: Read in a text (.txt) file as a .py file in Python 2.7


code:
class deferring_delete(object):
    def __init__(self, d):
        self._dict = d
    def __enter__(self):
        self._deletes = set()
        return self
    def __exit__(self, type, value, tb):
        for key in self._deletes:
            try:
                del self._dict[key]
            except KeyError:
                pass
        del self._deletes
    def __delitem__(self, key):
        if key not in self._dict:
            raise KeyError(str(key))
        self._deletes.add(key)

d = dict(a=1, b=2, c=3)

with deferring_delete(d) as dd:
    for k, v in d.iteritems():
        if k is "a":
            del dd[k]    # delete through wrapper

print d

rawDescription: custom dict that allows delete during iteration

description: Custom dict that allows delete during iteration


code:
import os.path
filename = "inputfilename.csv"
root, extension = os.path.splitext(filename)
output_filename = root + ".out" + extension
print output_filename

rawDescription: Beginner Python : How to save output in the same location and with a different name?


code:
def get_time():
    return int(time.time())

while(count < 10):
    filepath='/home/shared/word_out.gz'
    start = get_time()
    f = gzip.open( filepath, "rb" )
    pickleFile = cPickle.Unpickler( f )
    paData = pickleFile.load()
    f.close()
    end = get_time()

    print 'Time ellapsed' , end - start
    count+=1

rawDescription: Python Pickle Load Takes Longer Each Iteration


code:
from bs4 import BeautifulSoup

data = """

            Name:

                Robin Hood

"""

soup = BeautifulSoup(data)
print soup.find('td', class_="result-value-bold").get_text(strip=True)

rawDescription: Navigating to second string text using BeautifulSoup


code:
from graphics import *

class Button(object):
    def __init__(self, text, text_pos, rect_pos, win, callback):
        self.win = win
        self.text = Text(text_pos, text)
        self.text.draw(self.win)

        # the limits of the button will be defined by the rectangle
        self.coords = [rect_pos[0].x,rect_pos[0].y,rect_pos[1].x,rect_pos[1].y]
        self.rect = Rectangle(*rect_pos)
        self.rect.draw(self.win)

        # set the buttons callback
        self.callback = callback

    def _is_inside(self,click):
        limits = self.coords
        return  (limits[0] < click.x < limits[2]) and (limits[1] < click.y < limits[3])

class MyWindow(object):
    def __init__(self,coords=(0,0,100,100)):
        self.win = GraphWin()
        self.win.setCoords(*coords)

        # a list of all possible buttons
        self.buttons = []

        # register the master callback function
        self.win.setMouseHandler(self._callback)
        self._quit = False

        #create a quit and confess button with a custom create method
        self.create_button("Quit",(Point(10,10),Point(40,40)),Point(20,20),self.quit)
        self.create_button("Confess",(Point(50,50),Point(80,80)),Point(60,60),self.confess)

    def create_button(self,text,coords,text_coords,callback):
        button = Button(text,text_coords,coords,self.win,callback)
        # create a button and add it to our list of buttons
        self.buttons.append(button)

    def confess(self,point):
        print
        print "I love unicorns!"
        print

    def quit(self,point):
        self._quit = True
        self.win.close()

    # This function is called for any click on the canvas
    def _callback(self,point):
        # Need to do a coordinate transform here to get the click in the coordinates of the button
        x,y = self.win.trans.world(point.x,point.y)
        point = Point(x,y)
        print "Clicked at x=%d, y=%d"%(point.x,point.y)

        # cycle through buttons and execute all for which the clicked point lies inside their rectangle
        for button in self.buttons:
            if button._is_inside(point):
                button.callback(point)

    # a loop to keep getting mouse input
    def run(self):
        while not self._quit:
            try:
                self.win.getMouse()
            except GraphicsError:
                break

if __name__ == "__main__":
    x = MyWindow()
    x.run()

rawDescription: Creating two button methods Python

description: Creating two button methods Python


code:
import itertools

for i, a in enumerate(A):
    print fmt % tuple(itertools.chain((i, ), a))

rawDescription: Elegant way to print formatted list together with index values in python?


code:
class A(threading.Thread):

    def run(self):
        global a
        global b
        while True:
            a += 1

class B(threading.Thread):

    def run(self):
        global a
        global b
        while True:
            print a
            print b
            b -= 1

rawDescription: Python: Global variables in threads

description: Python: Global variables in threads


code:
file = open('myfile.txt', 'r')
temp = []
for line in file:
    item = line.strip('\n').split('.')
    temp.append(';'.join(item[::-1])+';'*3+'12345678;D:\lotus\NotesIDs\Users\;'+''.join(item)+'.id;SERVER01;mail\users\;'+''.join(item)+'.nsf;;;;;;;;;;template.ntf;')

file.close()

file = open('myfile.txt', 'w')

file.write('\n'.join(temp))

file.close()

rawDescription: Importing a text file and manipulating it to create and new file


code:
from pyPdf import  PdfFileReader
input1 = PdfFileReader(file("Billineg.pdf", "rb"))
# print the title of document1.pdf
print "title = %s" % (input1.getDocumentInfo().title)

rawDescription: How to use Python to pick up texts on PDF documents

description: Use Python to pick up texts on PDF documents


code:
sql = "SELECT * FROM webuserid WHERE userid = %s"

try:
    # Execute the SQL command
    cursor.execute(sql, (dub_userid,))
    found = cursor.rowcount
except:
    print "SQL Error: Unable to fetch data"
if not found:
    print "User doesn't exist - Creating"
else:
    sys.exit("User exists")

sql = """INSERT INTO webuserid(userid)
         VALUES (%s)"""
try:
    # Execute the SQL command
    cursor.execute(sql, (userid,))
    # Commit your changes in the database
    db.commit()
except:
    #Rollback in case there is any error
    db.rollback()
# disconnect from server
db.close()

rawDescription: Getting value from MySQL and loop over it with if


code:
import functools
import operator

import timeit
import types

N = 1000
R = 3
SZ = 10000
SUFFIX = '_test'
SUFFIX_LEN = len(SUFFIX)

def setup():
    import random
    global a_list
    a_list = [random.randrange(100) for _ in xrange(SZ)]

def lambda_test():
    global a_list
    f = lambda i: a_list[i]
    for i in xrange(len(a_list)): f(i)

def foo(indexable):
    return functools.partial(operator.__getitem__, indexable)

def foo_test():
    global a_list
    g = foo(a_list)
    for i in xrange(len(a_list)): g(i)

def foo2(indexable):
    return indexable.__getitem__

def foo2_test():
    global a_list
    g = foo2(a_list)
    for i in xrange(len(a_list)): g(i)

# find all the functions named *SUFFIX in the global namespace
funcs = tuple(value for id,value in globals().items()
            if id.endswith(SUFFIX) and type(value) is types.FunctionType)

# run the timing tests and collect results
timings = [(f.func_name[:-SUFFIX_LEN],
            min(timeit.repeat(f, setup=setup, repeat=R, number=N))
           ) for f in funcs]
timings.sort(key=lambda x: x[1])  # sort by speed (ironic use of lambda?)
fastest = timings[0][1]  # time fastest one took to run
longest = max(len(t[0]) for t in timings) # len of longest func name (w/o suffix)

print 'fastest to slowest *_test() function timings:\n' \
      ' {:,d} elements, {:,d} timeit calls, best of {:d}\n'.format(SZ, N, R)

def times_slower(speed, fastest):
    return speed/fastest - 1.0

for i in timings:
    print "{0:>{width}}{suffix}() : {1:.2f} ({2:.2f} times slower)".format(
                i[0], i[1], times_slower(i[1], fastest), width=longest, suffix=SUFFIX)

rawDescription: Inverse of `operator.itemgetter`

description: Inverse of `operator.itemgetter`


rawDescription: Change the display of a list took from text file


code:
def splitList(myList,option):

    result = []

    if option == 0:
        for item in myList:
            if (item < 0):
                result.append(item)
    elif option == 1:
        for item in myList:
            if (item % 2 == 0):
                result.append(item)
    else:
        return "Option Error"

    return sorted(result)

print splitList([1,-3,5,7,-9,-11,0,2,-4], 0)
print splitList([1,-3,5,7,-9,-11,0,2,-4], 1)
print splitList([1,-3,5,7,-9,-11,0,2,-4], 2)

rawDescription: Divisible by 2: Python

description: Divisible by 2: Python


code:
v = open('C:/test/test.csv')
r = csv.reader(v)
row0 = r.next()
row0.append('berry')
print row0
for item in r:
    item.append(item[0])
    print item

rawDescription: How to add a new column to a CSV file using Python?

description: Add a new column to a CSV file using Python


code:
from itertools import product

d = [
    ['who','whom','whose'],
    ['are','all'],
    ['that','than','this']
]

print list(product(*d))

rawDescription: convert a list to matrix in python

description: Convert a list to matrix in python


rawDescription: How to print the variables represent instead of the variable?

description: Print the variables represent instead of the variable


code:
import urllib
import urllib2
# from bs4 import BeautifulSoup
from BeautifulSoup import BeautifulSoup
import re
gamesForDay = ['/games/20151002/DENLAC/gameinfo.html']
for game in gamesForDay:
   url =  "http://www.nba.com/"+game
   page = urllib2.urlopen(url).read()
   soup = BeautifulSoup(page)

   # fetch the tables you are interested in
   tables = soup.findAll(id="nbaGITeamStats")
   for table in tables:
       team_name = table.thead.tr.th.text
       # odd/even class rows (tr)
       rows = [ x for x in table.findAll('tr') if x.get('class',None) in ['odd','even'] ]
       for player in rows:
           # search the row cols based on 'id'
           player_name = player.find('td', attrs={'id':'nbaGIBoxNme'}).text

           # search the row cols based on 'class'
           player_position = player.find('td', attrs={'class':'nbaGIPosition'}).text

           # search for all td where the class is not defined
           player_numbers = [ x.text for x in player.findAll('td', attrs={'class':None})]

           print player_name, player_position, player_numbers

rawDescription: How to scrape data from a table using a loop to get all td data using python

description: Scrape data from a table using a loop to get all td data using python


code:
import functools

class cacher(object):

    def __init__(self, f):
        self.f = f
        self.cache = {}

    def __call__(self, *args):
        fname = self.f.__name__
        if (fname not in self.cache):
            self.cache[fname] = self.f(self,*args)
        else:
            print "using cache"
        return self.cache[fname]

    def __get__(self, instance, instancetype):
        """Implement the descriptor protocol to make decorating instance
        method possible.

        """

        # Return a partial function with the first argument is the instance
        #   of the class decorated.
        return functools.partial(self.__call__, instance)

rawDescription: Python decorator, self is mixed up

description: Python decorator, self is mixed up


rawDescription: Python integer prefix range algorithm

description: Python integer prefix range algorithm


code:
for k,v in names.items():
    for kk,vv in v.items():
        print k,kk,len(vv),max(vv)-min(vv)

rawDescription: accessing lists values of dictionary

description: Accessing lists values of dictionary


code:
for el in mySeries:
    print myDF["name"][(myDF["low"] < el) & (myDF["high"] > el)]

rawDescription: Classify data by value in pandas

description: Classify data by value in pandas


code:
for i, item in cycle(enumerate([1, 2, 3])):
    print i, item

rawDescription: Is it a way to know index using itertools.cycle()?


code:
import argparse

parser=argparse.ArgumentParser()
parser.add_argument('--shortname')
args=parser.parse_args()

print 'starting: ' + str(args.shortname)

rawDescription: Get raw input as an option?

description: Get raw input as an option


code:
#!/usr/bin/python

import re

xml = '''\
<Emp>
<Name>Raja</Name>
<Location>
     <city>ABC</city>
     <geocode>123</geocode>
     <state>XYZ</state>
</Location>
</Emp>'''

locUpdate = '''\
    <Location isupdated=1>
         <city>MyCity</city>
         <geocode>10.12</geocode>
         <state>MyState</state>
    </Location>'''

output = re.sub(r"(?s)<Location>.*?</Location>", r"%s" % locUpdate, xml)

print output

rawDescription: Replace xml tag contents using python

description: Replace xml tag contents using python


code: print ord('^A')

rawDescription: Python: remove ^A in a string

description: Python: remove ^A in a string


code:
from itertools import groupby

list_one=[1,2,9,9,9,3,4,9,9,9,9,2]
list_two=["A","B","C","D","A","E","F","G","H","Word1","Word2"]
data = zip(list_one, list_two)
i = 0
out = []

for key, group in groupby(data, lambda x: x[0]):
        number, word = next(group)
        elems = len(list(group)) + 1
        if number == 9 and elems > 1:
            out.append((key, elems, i))
        i += elems

print out

rawDescription: How to get the index and occurance of each item using itertools.groupby()

description: Get the index and occurance of each item using itertools.groupby()


code: print '%.*f' % (places[i], data[i])

rawDescription: On significant figures of numbers in python


code:
import sys
from numpy import linspace
from scipy.integrate import odeint
from scipy.optimize import fsolve

y0 = [0, 5]
time = linspace(0., 10., 1000)
F_lon = 10.
mass = 1000.

def F_r(a, v):
    return (((1 - a) / 3) ** 2 + (2 * (1 + a) / 3) ** 2) * v

def constraint(a, v):
    return (F_lon - F_r(a, v)) / mass - a

def integral(y, _):
    v = y[1]
    a, _, ier, mesg = fsolve(constraint, 0, args=[v, ], full_output=True)
    if ier != 1:
        print "I coudn't solve the algebraic constraint, error:\n\n", mesg
        sys.stdout.flush()
    return [v, a]

dydt = odeint(integral, y0, time)

rawDescription: Solve an implicit ODE (differential algebraic equation DAE)


code:
while not q.empty():
    data = q.get()
    time.sleep(1)
    print "get (loop: %s)" %loop
    loop += 1

rawDescription: Multiprocessing and Queue with Dataframe


code:
for k in sorted(return_message):
    print k

rawDescription: Sort nodes after using getElementsByTagName by the nodes attributes


code:
class FooClass(object):
    @staticmethod
    def static_method(x):
        print x

registry = {'FooClass':FooClass}

rawDescription: How to call a static method of a class using method name and class name

description: Call a static method of a class using method name and class name


code:
for note in s.query(Note):
    print note.id_item, note.name, note.value

rawDescription: Dictionary of tags in declarative SQLAlchemy?


code:
df = df.to_period('M')
print df

#         Prediction  MAPE  Score
#
#2015-11        7.93  1.83      1

rawDescription: Python/Datetime/Pandas - Changing datetime format


code:
def foo(a=0):
  print id(a)
  a+=1
  print id(a)
  print a

foo()
foo()
foo()

rawDescription: Default values for arguments

description: Default values for arguments


code: print request.form['username']

rawDescription: How are POST and GET variables handled in Python?


code:
import string

for letters in itertools.product(string.ascii_lowercase, repeat=4):
    word = ''.join(letters)

    if word in english_words:
        print word

rawDescription: words being left out

description: Words being left out


rawDescription: good practice for string.partition in python

description: Good practice for string.partition in python