code:
import sys, shlex, readline, os, string
List, assign, call, add, sub, div, Pow, mul, mod, fac, duf, read,\
kill, clr, STO, RET, fib, curs, dup, swap = {}, "set", "get", "+", "-", "/", "^", "*",\
"%", "fact", "func", "read", "kill", "clear", ">", "@", "fib", "vars", "dup", "swap"
funcdict = {}
def fact(num):
    if num == 1: return 1
    else: return num*fact(num-1)
def builtin_op(op, stack):
    global List
    global funcdict
    if op == mul: stack.append(float(stack.pop())*float(stack.pop()))
    elif op == div: stack.append(float(stack.pop())/float(stack.pop()))
    elif op == sub: stack.append(float(stack.pop())-float(stack.pop()))
    elif op == add: stack.append(float(stack.pop())+float(stack.pop()))
    elif op == Pow: stack.append(float(stack.pop())**float(stack.pop()))
    elif op == assign: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == call: Eval(funcdict[stack.pop()], stack)
    elif op == fac: stack.append(fact(stack.pop()))
    elif op == duf: name = stack.pop(); funcdict[name] = stack.pop(); stack.append(name)
    elif op == mod: stack.append(float(stack.pop())%float(stack.pop()))
    elif op == kill: del List[stack.pop()]
    elif op == clr: os.system("clear")
    elif op == STO: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == RET: stack.append(List[stack.pop()])
    elif op == curs: stack.append(List)
    elif op == dup: val = stack.pop(); stack.append(val); stack.append(val)
    elif op == swap: val1 = stack.pop(); val2 = stack.pop(); stack.append(val1); stack.append(val2)
    elif op == read: prompt = stack.pop(); List[prompt] = Eval(raw_input("%s "%prompt)); stack.append(List[prompt])
def Eval(expr, stack=None):
    ops = "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s"%(mul, add, sub, div, Pow, assign, call, fac, duf, mod, read, kill, clr, STO, RET, curs, dup, swap)
    if stack is None:
        stack = []
    expr, ops = shlex.split(string.lower(expr)), ops.split()
    for i in expr:
        if i[0] != ';':
            if i not in ops: stack.append(i)
            elif i in ops: builtin_op(i, stack)
        else: stack.append("ok")
    return stack[0]
def shell():
    try:
        x = ""
        while x != "quit":
            x = raw_input("star>   ")
            try: l = Eval(x)
            except KeyError: l = "does not exist"
            except: l = "parse error!"
            if l != None: print "   =>",l,"\n"
    except (EOFError, KeyboardInterrupt): print
if len(sys.argv) > 1:
    x = open(sys.argv[1], 'r'); l = x.readlines(); x.close()
    for i in l:
        if i[0] != ";":
            i = ' '.join(i.split())
            x = Eval(i)
            if x != None: print i,"\n   =>",x,"\n"
        else: pass
    shell()
else: shell()

rawDescription: small language in python

description: Small language in python


code:
import mcint
import random
import math

def w(r, theta, phi, alpha, beta, gamma):
    return(-math.log(theta * beta))

def integrand(x):
    r     = x[0]
    theta = x[1]
    alpha = x[2]
    beta  = x[3]
    gamma = x[4]
    phi   = x[5]

    k = 1.
    T = 1.
    ww = w(r, theta, phi, alpha, beta, gamma)
    return (math.exp(-ww/(k*T)) - 1.)*r*r*math.sin(beta)*math.sin(theta)

def sampler():
    while True:
        r     = random.uniform(0.,1.)
        theta = random.uniform(0.,2.*math.pi)
        alpha = random.uniform(0.,2.*math.pi)
        beta  = random.uniform(0.,2.*math.pi)
        gamma = random.uniform(0.,2.*math.pi)
        phi   = random.uniform(0.,math.pi)
        yield (r, theta, alpha, beta, gamma, phi)

domainsize = math.pow(2*math.pi,4)*math.pi*1
expected = 16*math.pow(math.pi,5)/3.

for nmc in [1000, 10000, 100000, 1000000, 10000000, 100000000]:
    random.seed(1)
    result, error = mcint.integrate(integrand, sampler(), measure=domainsize, n=nmc)
    diff = abs(result - expected)

    print "Using n = ", nmc
    print "Result = ", result, "estimated error = ", error
    print "Known result = ", expected, " error = ", diff, " = ", 100.*diff/expected, "%"
    print " "

rawDescription: Integrating a multidimensional integral in scipy


code:
class CustomList(list):
    def __getitem__(self, index):
        return super(CustomList, self).__getitem__(index % len(self))

a = CustomList([1, 2, 3])
for index in xrange(5):
    print index, a[index]

rawDescription: Circular array indices in Python

description: Circular array indices in Python


code:
import SocketServer

class MyTCPHandler(SocketServer.BaseRequestHandler):
    """
    The RequestHandler class for our server.

    It is instantiated once per connection to the server, and must
    override the handle() method to implement communication to the
    client.
    """

    def handle(self):
        # self.request is the TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print "{} wrote:".format(self.client_address[0])
        print self.data
        # just send back the same data, but upper-cased
        self.request.sendall(self.data.upper())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)

    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

rawDescription: connecting python to javascript for two-direction communication

description: Connecting python to javascript for two-direction communication


code:
s = 'some \\\\ doubles'
print s
print s.replace('\\\\', '\\')

rawDescription: Python regex to replace double backslash with single backslash


rawDescription: Change part of string in a file using regex in Python


code:
with open('test.txt', 'r') as f:
     lines = f.readlines()
     foo1=map(float, lines)
     print foo1

sum(foo1)

rawDescription: Sum two text files containing floats row by row in python


code:
import re
from datetime import datetime, timedelta
# Custom classes to hold your data.
class ElevatorInterval(object):
    def __init__(self, timestamp, record, loc_strt, loc_cut):
        timestamp = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
        self.timestop = self.timestart = timestamp + timedelta(hours=6) #UTC+6
        self.measures = []
        self.location = 'bottom' if float(loc_strt) < 300 else 'top'
class NH3Measure(object):
    def __init__(self, date, tim, nh3_raw):
        self.timestamp = datetime.strptime(date + tim, "%Y-%m-%d%H:%M:%S")
        self.nh3 = nh3_raw
    def __repr__(self):
        return str(self.nh3)
# Read data from file and assign them to elevator measures, and NH3 measures.
ele_intervals, nh3_measures = [], []
with open('aem.txt', 'r') as f:
    for line in f:
        linematch = re.match(r'^"([0-9-]+\s[0-9:]+)(?:\.[0-9])?",([0-9]+),([0-9.]+),([0-9.]+)', line)
        if linematch:
            ele_intervals.append(ElevatorInterval(*linematch.groups()))
            if len(ele_intervals) > 1: # Set timestop for the last elevator interval.
                ele_intervals[-2].timestop = ele_intervals[-1].timestart - timedelta(seconds=22)
del ele_intervals[-1] # Remove last interval as it has no stop time.
with open('pic.txt', 'r') as f:
    for line in f:
        linematch = re.match(r'^([0-9-]+)\s+([0-9:]+)[0-9.]*\s+([0-9E.+]+)', line)
        if linematch: nh3_measures.append(NH3Measure(*linematch.groups()))
# Assign NH3 measures to their proper interval, and output the intervals.
for ele in ele_intervals:
    ele.measures = filter(lambda x: ele.timestart < x.timestamp < ele.timestop, nh3_measures)
    print ele.location, ele.measures

rawDescription: How to Join two separate text data files and align data over varying time intervals and avg


rawDescription: Making a grid with curses from a dictionary of flat tuples


code:
pattern = re.compile('<\?xml version="1\.0" encoding="UTF-8"\?>.+?<\/nmaprun>', re.DOTALL)
result= re.findall(pattern,data)
for item in result:
            print item

rawDescription: Extract recursivly multiple line data between two String- Regex & Python & XML


code:
import Image
im = Image.load("image.png")
diff_colors = list(set(im.getdata()))
if len(diff_colors) == 1 and diff_colors[0] == (0, 0, 0):
    print "all black"

rawDescription: Check if image is black

description: Check if image is black


code:
class Account:
    def __init__(self, x, y):
        self.x = x
        self.y = y

d_in = {0: Account(13, 1), 1: Account(15, 5), 2: Account(55, 1), 3: Account(33 ,1)}
d_out = collections.defaultdict(list)

for index, k in enumerate(d_in.keys()):
    d_out[d_in[k].y].append(index)

print d_out

rawDescription: How to map dict into another dict Python

description: Map dict into another dict Python


code:
import sched,time

test = 0
s = sched.scheduler(time.time, time.sleep)

Y = 2000

def initer(self):
    global test,Y
    if self.__class__.CLASSIES == 0:
        Y += 1
        self.__class__.CLASSIES += 1

def linker(self):
    global test,Y
    test = test + 1
    self.X = self.X + 1
    print 'Y == %d' % Y
    print 'test == %d     self.X == %d' % (test,self.X)

def loop1(sc):
    fb = type('fb',(object,),{'__init__':initer,
                              'link':linker,
                              'CLASSIES':0})
    print '--------'
    print 'id(fb) == %d' % id(fb)
    m1 = fb()
    m1.X = 0
    m1.link()
    print
    m2 =fb()
    m2.X = 1
    m2.link()
    print
    m3 =fb()
    m3.X = 2
    m3.link()
    print '--------'

    # Update loop every 10 second
    sc.enter(10, 10, loop1, (sc,))

# end loop
s.enter(1, 1, loop1, (s,))
s.run()

rawDescription: Local variable in each class - python timed loop


code:
#!/usr/bin/env python

import os, os.path
from contactsheet import make_contact_sheet

def make_film_strip(fnames,
                   (photow,photoh),
                   (marl,mart,marr,marb),
                   padding):
    return make_contact_sheet(fnames,
                              (1, len(fnames)),
                              (photow,photoh),
                              (marl,mart,marr,marb),
                              padding)

rawDescription: How to generate a filmstrip image in python from a folder of images?

description: Generate a filmstrip image in python from a folder of images


code:
import pandas as pd

files = ['university1.csv','university2.csv','university3.csv']

for f in files:
    i = f[-5]
    print i

    #Importing the csv file into df
    df = pd.read_csv(f, sep=";", skiprows=1)

    #Changing datetime
    df['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df['YYYY-MO-DD HH-MI-SS_SSS'],
                                                   format='%Y-%m-%d %H:%M:%S:%f')

    #Set index from column
    df = df.set_index('YYYY-MO-DD HH-MI-SS_SSS')

    #Add Magnetic Magnitude Column
    df['magnetic_mag'] = np.sqrt(df['MAGNETIC FIELD X (μT)']**2 + df['MAGNETIC FIELD Y (μT)']**2 + df['MAGNETIC FIELD Z (μT)']**2)

    #Copy interesting values
    df2 = df[[ 'ATMOSPHERIC PRESSURE (hPa)',
              'TEMPERATURE (C)', 'magnetic_mag']].copy()

    #Hourly Average and Standard Deviation for interesting values
    df3 = df2.resample('H').agg(['mean','std'])
    df3.columns = [' '.join(col) for col in df3.columns]

    #Daily Average and Standard Deviation for interesting values
    df4 = df2.resample('D').agg(['mean','std'])
    df4.columns = [' '.join(col) for col in df4.columns]

    #Write to new csv
    df3.to_csv('Hours'+ i + '.csv')
    df4.to_csv('Day'+ i + 's.csv')

    #New csv with stacked hour averages
    #df5 = pd.read_csv('Hours.csv')
    #df5['YYYY-MO-DD HH-MI-SS_SSS'] = pd.to_datetime(df5['YYYY-MO-DD HH-MI-SS_SSS'])
    df5 = df3.reset_index()
    hour = pd.to_timedelta(df5['YYYY-MO-DD HH-MI-SS_SSS'].dt.hour, unit='H')
    df6 = df5.groupby(hour).mean()
    df6.to_csv('Hours - stacked'+ i + '.csv')

rawDescription: Python: Loop acting on several files and writing new ones


code: for x in g: print x[0], list(x[1])

rawDescription: Separate a set of sublists from within a list by a given element of a sublist


code:
from pyparsing import *
from string import whitespace

def test(phrase):
        """
        @summary: try to grab a keyword+ "keyword+ " and free text following
        the keyword
        @param phrase: a phrase of text to parse
        @type phrase: str
        @date: 20140213
        """
        test = 1
        print 'Phrase \n         1         2         3\n'
        print '123456789012345678901234567890\n'
        print '%s\n' % phrase
        kw = Combine(Word(alphas + nums) + Literal(':'))('KEY')
        punc = printables.replace(':', '')
        p2 = oneOf(")[]()/.")
        # but punc now has '/' in it twice
        p3 = punc | p2
        kw.setDebug(True)
        s = OneOrMore(p3)
        # pepper handles body1
        # http://structure.usc.edu/pyparsing/pyparsing.Word-class.html
        #      init char, body chars
        body1 = originalTextFor(OneOrMore(~kw + OneOrMore(Word(alphas +
                                            '/.' + nums))))('BODY1')
        body2 = originalTextFor(OneOrMore(~kw + (Word(alphas + punc + nums))))(
            'BODY2')
        body1.setDebug(True)
        body1.setName('BODY1')
        body2.setDebug(True)
        body2.setName('BODY2')
        grammar = OneOrMore(Group(kw + body1) | Group(kw + body2))

        print '============= %s =================' % test
        # this grabs only the first one
        print ("grammar %s" % grammar)
        output = grammar.parseString(phrase)
        print 'XXXXXXXXXXXXX'
        print 'XXXXXXXXXXXXX'
        print 'XXXXXXXXXXXXX'
        result, start, end = next(grammar.scanString(phrase))
        print len(phrase), end
        print 'NOTICE:'
        print phrase[end:end+10]

        print ("Test %d output %s" % (test, output))
        for res in output:
            print res.dump()

if __name__ == '__main__':

    phrase = """

COTTON2: (RAW) NEED HARVEST DATE.

SALAMI2: (COOKED) SOUTHERN VARIES; SUGGEST ALT.

PEPPER1:  ON TREE/ROOTS UNDERGROUND REQUEST PERMISSION
TO DIG PLANT AND RELOCATE.
"""
    # when I run the output stops at '/' in the 'pepper' parsing.
    test(phrase)

rawDescription: My grammar stops on an embedded '/' . I tried adding it as an allowed value but it isn't working


code:
a = ['green egg','snail and lettuce','bacon','dorse naga','rutabaga ripple','cheese']
a.sort(key=len)
print a

rawDescription: Arranging number of strings using Python's lambda function


code:
def foo(default_value):
    def wrapped(f):
        def bar(param1, param2=default_value):
            print "inside bar"
            print "param1=%s param%s"%(param1, param2)
        return bar
    return wrapped

rawDescription: How can i set a default value for a function parameter with a decorator?

description: Set a default value for a function parameter with a decorator


rawDescription: How to create a date range until some date inclusive with python dateutils?

description: Create a date range until some date inclusive with python dateutils


code:
def make_properties(c):
    from collections import defaultdict
    props=defaultdict(dict)
    for k,v in vars(c).items():
        if k.startswith("Get"):
            props[k[3:]]['getter']=v
        if k.startswith("Set"):
            props[k[3:]]['setter']=v
    for k,v in props.items():
        setattr(c,k,property(v.get('getter'),v.get('setter')))
    return c

@make_properties
class C(object):
    def GetX(self):
        print "GetX"
        return self._x

    def SetX(self, value):
        print "SetX"
        self._x = value

c=C()
c.X=5
c.X

rawDescription: Automatic transformation from getter/setter to properties


code:
import itertools
lists = [
    ['loop01item01', 'loop01item02', 'loop01item03', 'loop01item04', 'loop0item05'],
    ['loop02item01', 'loop02item02', 'loop02item03', 'loop0item04', 'loop0item05'],
    ['loop03item01', 'loop03item02', 'loop03item03', 'loop03item04', 'loop03item05']
]

loops = int(raw_input("Enter the number of nested loops you want: "))
list_size = len(lists[0])
for indices in itertools.permutations(range(5), loops):
    elements = [lists[i][idx] for i, idx in enumerate(indices)]
    print elements

rawDescription: All combinations with multiple loops

description: All combinations with multiple loops


code:
def numeric_ip(ip):
    return [int(x) for x in ip.split('.')]

desired_ip = numeric_ip('66.35.205.88')
with open('GeoIPCountryWhois.csv', mode='r') as f:
    for num, row in enumerate(csv.reader(f)):
        if numeric_ip(row[0]) <= desired_ip <= numeric_ip(row[1]):
            print num, row

rawDescription: Searching and Matching a part of a string in a CSV file using Python


code:
sentence = raw_input("Please enter a sentence : ").lower()
newString = ''
validLetters = "abcdefghijklmnopqrstuvwxyz " #adding whitespace to valid chars...
space = []
for char in sentence:
    if char in validLetters or char in space:
        newString += char
shift = input("Please enter your shift : ")
resulta = []
for ch in newString:
    x = ord(ch)
    x = x+shift
    # special case for whitespace...
    resulta.append(chr(x if 97 <= x <= 122 else 96+x%122) if ch != ' ' else ch)
print sentence
print("")
print("Your encryption is :")
print("")
print ''.join(resulta)

rawDescription: Python Caesar Cipher keeping spaces

description: Python Caesar Cipher keeping spaces


code:
class AClass(object):
      aprop = None
      def __init__(self):
           self.aprop = "avalue"
AClass.aprop
print AClass.aprop

rawDescription: Initialize object properties as class properties first in Python


rawDescription: Sort list of dictionaries by value, regardless of keyname in python


code:
binarySearch.counter =0
l=[1,4,5,6]
binarySearch(l,1,0,-1)
print binarySearch.counter

rawDescription: How could I use a for loop to count the number of searches being performed in binary search?


code:
import csv

uniq = {} #Create an empty dictionary, which we will use as a hashmap as Python dictionaries support key-value pairs.

ifile = open('data.csv', 'r') #whatever your CSV file is named.
reader = csv.reader(ifile)

for row in reader:
    joined = row[0] + row[1] #The joined string is simply the first and second columns in each row.
    #Check to see that the key exists, if it does increment the occurrence by 1
    if joined in uniq.keys():
        uniq[joined] += 1
    else:
        uniq[joined] = 1 #This means the key doesn't exist, so add the key to the dictionary with an occurrence of 1

print uniq #Now output the results

rawDescription: Counting how many unique identifiers there are by merging two columns of data?


code: print '\n'.join(alist)

rawDescription: How to make the output shows tabs instead of showing \t?

description: Make the output shows tabs instead of showing \t


code:
import re
from nltk.stem import SnowballStemmer as SS

text = """Dancing has evolved many styles. African dance is interpretative.
Ballet, ballroom (such as the waltz), and tango are classical styles of dance
while square dancing and the electric slide are forms of step dances.
More recently evolved are breakdancing and other forms of street dance,
often associated with hip hop culture.
Every dance, no matter what style, has something in common.
It not only involves flexibility and body movement, but also physics.
If the proper physics are not taken into consideration, injuries may occur."""
#extract words
words = [word.lower() for word in re.findall(r'\w+',text)]

stemmer = SS('english')
counts = dict()

#count stems and extract shortest words possible
for word in words:
    stem = stemmer.stem(word)
    if stem in counts:
        shortest,count = counts[stem]
        if len(word) < len(shortest):
            shortest = word
        counts[stem] = (shortest,count+1)
    else:
        counts[stem]=(word,1)

#convert {key: (word, count)} to [(word, count, key)] for convenient sort and print
output = [wordcount + (root,) for root,wordcount in counts.items()]
#trick to sort output by count (descending) & word (alphabetically)
output.sort(key=lambda x: (-x[1],x[0]))
for item in output:
    print '%s:%d (Root: %s)' % item

rawDescription: Probability count/frequency of related words?


code:
import numpy as np
from scipy import spatial

H, W = 200, 100

np.random.seed(1)
a = np.random.randint(0, 20, (H, W, 3))
b = np.random.randint(0, 20, (20, 3))
tree = spatial.cKDTree(a.reshape(-1, 3))
res = tree.query_ball_point(b, 0.5, p=1)
print res

rawDescription: fastest way to get lookup table indices with numpy

description: Fastest way to get lookup table indices with numpy


code:
def getAvailableLetters2(lettersGuessed):
    return string.ascii_lowercase.translate(None, lettersGuessed)

from timeit import timeit
print 'filter-->', timeit("getAvailableLetters('Welcome')", setup="from __main__ import getAvailableLetters")
print '.join-->', timeit("getAvailableLetters1('Welcome')",setup="from __main__ import getAvailableLetters1")
print 'trans-->', timeit("getAvailableLetters2('Welcome')",setup="from __main__ import getAvailableLetters2")

rawDescription: Removing a char from String, what's a more efficient way to do this?


code:
import re
import collections
words = re.findall('\w+', open('1976.03.txt').read().lower())
print collections.Counter(words)

rawDescription: Python - Finding word frequencies of list of words in text file


code:
import argparse

if __name__ == '__main__':
   parser = argparse.ArgumentParser()
   parser.add_argument('--arg1')
   parser.add_argument('--arg2')
   args = parser.parse_args()

   print args.arg1
   print args.arg2

   my_dict = {'arg1': args.arg1, 'arg2': args.arg2}
   print my_dict

rawDescription: Python file keyword argument?

description: Python file keyword argument


code:
#! /usr/bin/env python

import time
import calendar

def timestr_to_secs(timestr):
    fmt = '%Y-%m-%d %H:%M:%S'
    time_struct = time.strptime(timestr, fmt)
    secs = calendar.timegm(time_struct)
    return secs

timestrs = [
    '2013-09-03 17:34:04',
    '2013-09-03 17:34:05',
    '2013-09-03 17:35:04',
    '1970-01-01 00:00:00'
]

for ts in timestrs:
    print ts,timestr_to_secs(ts)

rawDescription: From date-time to usable value Python

description: From date-time to usable value Python


code:
class Application(Frame):

    def my_time(self):  # creates a timer starting at 5 min , counts down to 0 then repeats
        min = 4
        sec = 59
        while sec <=60:
            self.Button1.configure(text="{}:{}".format(min,sec))
            self.Button1.update()
            os.system('cls')
            print min, "Minutes", sec, "Seconds"
            time.sleep(1)
            sec -= 1
            if sec == 0:
                 min -= 1
                 sec = 59
            elif min == 0:
                 min = 4

    def my_time2(self):                    # tries to display my_time method into the button font, doesnt seem to work.
        self.Button1["text"] = str(self.my_time)

    def createButtons(self):                      # creats a button
        self.Button1 = Button(self)
        self.Button1["text"] = "Blue Buff"
        self.Button1["fg"]   = "Blue"
        self.Button1["command"] = self.my_time    # suppose to implement countdown in button text when click.
                            # currently problematic?
        self.Button1.pack({"side": "left"})
    def __init__(self, master=None):
        Frame.__init__(self, master)    # initializes window
        self.pack()
        self.createButtons()

rawDescription: Error when trying to display a timer into button text


code:
with open('urls.txt') as inf:
    urls = (line.strip() for line in inf)
    for url in urls:
        site = urlopen(url)
        soup = BeautifulSoup(site)
        for td in soup.find_all('td', {'class': 'subjectCell'}):
            print td.find('a').text

rawDescription: Is it possible to open multiple URLs from a .txt and scrape all the webpages at once?


code:
def hanoi(pegs, start, target, n):
    assert len(pegs[start]) >= n, 'not enough disks on peg'
    if n == 1:
        pegs[target].append(pegs[start].pop())
        print '%i -> %i: %s' % (start, target, pegs)
    else:
        aux = 3 - start - target  # start + target + aux = 3
        hanoi(pegs, start, aux, n-1)
        hanoi(pegs, start, target, 1)
        hanoi(pegs, aux, target, n-1)

rawDescription: Trying to implement recursive Tower of Hanoi algorithm with arrays


code:
_, out, err = ssh.exec_command('ls -l /etc/passwd')
# block until remote command completes
status = out.channel.recv_exit_status()
print status

rawDescription: SSH a linux box using subprocess module

description: SSH a linux box using subprocess module


code:
import feedparser
d = feedparser.parse("http://www.cbc.ca/cmlink/rss-topstories")
soup = BeautifulSoup("".join([item.summary for item in d.entries]))
print [''.join(s.findAll(text=True)) for s in soup.findAll('p')]
[u"Search teams are returning to the home of Kathy and Alvin Liknes today for another sweep of the property, close to two weeks after the couple and their grandson Nathan O'Brien were discovered missing in Calgary.", u"Israel widened its air assault against the Gaza Strip's Hamas militants on Saturday, hitting targets that included a mosque the Israeli military said was being used to conceal rockets. Meanwhile, there are reports Hamas has launched rockets at Tel Aviv.", u'The Sunni militant group ISIS, which wants to create an Islamic state spanning Iraq and Syria, has issued a recruitment video using the image and words of a dead Ontario man who had become a jihadist and joined the fighting in Syria.', u'A Hamilton-area man\u2019s dashcam may have saved him a pricey car insurance payout \u2013 and maybe even from falling victim to an insurance scam, an industry expert says.', u'Tommy Ramone, a co-founder of the seminal punk band the Ramones and the last surviving member of the original group, has died, a business associate said Saturday.', u"During high-stake police interrogations and on seemingly meaningless online dating profiles, some people find themselves lying. So, how can you tell if someone isn't telling you the truth?", u"Israeli strikes in Gaza have led to sleepless nights and anxious Palestinian children, CBC's Derek Stoffel reports from a refugee camp in Gaza City.", u'Saskatchewan Premier Brad Wall has been a vocal proponent of abolishing the Senate. With the Prime Minister now under pressure to fill vacancies in the upper chamber, Wall argues that not appointing new senators might be the way to get rid of the institution.', u"Bassist Charlie Haden, who helped change the shape of jazz more than a half-century ago as a member of Ornette Coleman's groundbreaking quartet and liberated the bass from its traditional rhythm section role, has died. He was 76.", u"Tracy Morgan has sued Wal-Mart over last month's highway crash that seriously injured him and killed a fellow comedian.", u'Buying pot is normally a subtle affair, but not for Mike Boyer, who camped out to become the first person to legally purchase marijuana in Washington state.', u"Monika Platek, CBC's lead producer for social media during the World Cup, looks at some of the standout moments so far from the 2014 World Cup", u'Our weekly round-up of remarkable photos includes scenes from Brazil, Spain, Germany, India and elsewhere around the world.', u'The European Union said on Saturday that it has extended sanctions to cover 11 leaders of the pro-Moscow rebellion in eastern Ukraine.', u'The Edmonton Public School Board has said it will tell teachers not to use an anti-abortion centre to teach part of its sexual education curriculum, after a McNally high school student filed a human rights complaint over what she was taught.']

rawDescription: Retrieve info between paragraph tags with feedparser


code:
import httplib
conn = httplib.HTTPConnection("sstatic.net")
conn.request("HEAD", "/stackoverflow/img/favicon.ico")
res = conn.getresponse()
print res.getheaders()

rawDescription: python check url type

description: Python check url type


code:
import xmltodict

xml_input = """
<mediawiki xmlns="http://www.mediawiki.org/xml/export-0.10/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mediawiki.org/xml/export-0.10/ http://www.mediawiki.org/xml/export-0.10.xsd" version="0.10" xml:lang="en">
<siteinfo>
    <sitename>Wikipedia</sitename>
    <dbname>enwiki</dbname>
    <base>https://en.wikipedia.org/wiki/Main_Page</base>
    <generator>MediaWiki 1.27.0-wmf.18</generator>
    <case>first-letter</case>
    <namespaces>...</namespaces>
</siteinfo>
<page>
    <title>Zhuangzi</title>
    <ns>0</ns>
    <id>42870472</id>
    <revision>
        <id>610251969</id>
        <timestamp>2014-05-25T20:08:14Z</timestamp>
        <contributor>
            <username>Patric</username>
            <id>8761551</id>
        </contributor>
    </revision>
    <revision>
        <id>610251969</id>
        <timestamp>2014-05-26T20:08:14Z</timestamp>
        <contributor>
            <username>Don</username>
            <id>8761551</id>
        </contributor>
    </revision>
    <revision>
        <id>610251969</id>
        <timestamp>2014-05-27T20:08:14Z</timestamp>
        <contributor>
            <username>Patric</username>
            <id>8761551</id>
        </contributor>
    </revision>
</page>
</mediawiki>
"""

dic_xml = xmltodict.parse(xml_input)

for rev in dic_xml['mediawiki']['page']['revision']:
    if rev['contributor']['username'] == 'Patric':
        print rev['id']
        print rev['timestamp']

rawDescription: Extract data from XML file if arguments are of certain values


code:
for sel in response.xpath('//div[contains(@class, "region")]/*[self::h2 or self::strong]'):
    print " ".join(item.strip() for item in sel.xpath('following-sibling::div[position() < 4 and not(starts-with(., "Timings"))]/text()').extract())

rawDescription: How to exclude a particular html tag(without any id) from several tags while using scrapy?

description: Exclude a particular html tag(without any id) from several tags while using scrapy


code:
while closeProgram != "q":
    numTimes = int(raw_input("Enter number of dice rolls: "))
    for i in range(numTimes):
        print rollDice()
    closeProgram = raw_input("Press 'q' to quit or 'enter' to roll again.")

rawDescription: python dice roll simulator, issues with quit and roll again

description: Python dice roll simulator, issues with quit and roll again


code:
a = 5
b = a
b += 1             # created a new int
print id(a), id(b) # different

rawDescription: Why equal integers behaves differently to equal lists?


code:
myVariable = 'iheoiahwd'
idxs = [0, 3, 4, 6, 7]

myVariable = [myVariable[i] for i in idxs]
print ''.join(myVariable)

rawDescription: printing parts of a string while skipping others in Python

description: Printing parts of a string while skipping others in Python


code:
from decimal import Decimal

def round_to_10_cents(x):
    """
    Round a Decimal value to the nearest multiple of 0.10,
    unless already an exact multiple of 0.05.

    """
    remainder = x.remainder_near(Decimal('0.10'))
    if abs(remainder) == Decimal('0.05'):
        return x
    else:
        return x - remainder

# Test code.
for x in range(80, 120):
    y = Decimal(x) / Decimal('1E2')
    print "{0} rounds to {1}".format(y, round_to_10_cents(y))

rawDescription: Python ROUND_05UP

description: Python ROUND_05UP


code:
inputstring = "{'data': [{'name': 'abc'},{'name': 'xyz'}]}"
import ast
data = ast.literal_eval(inputstring)
for key, sublist in data.items():
    print '{}:'.format(key)
    for subdict in sublist:
        for key, value in subdict.items():
            print '{}:{}'.format(key, value)

rawDescription: How to remove commas, brackets in python using regular expression?

description: Remove commas, brackets in python using regular expression


code:
def foo_a():
    print 'a'

def foo_b():
    print 'b'

funcs = {'a': foo_a, 'b': foo_b}

# dynamically select function based on some key
key = 'a'
foo_fn = funcs[key]

foo_fn() # calling
another_function(foo_fn) # passing

rawDescription: Python naming convention for variables that are functions


code:
print re.sub(r"\\\[(.*?)\\\]", repl, "frfrfr\nfrrffr
\[something\nblah\]
frff\nfrfrr", re.S)
print re.sub(r"\\\[(.*?)\\\]", repl, "frfrfr\nfrrffr
\[something\nblah\]
frff\nfrfrr", flags=re.S)

rawDescription: Matching \[\] in Python regexes

description: Matching \[\] in Python regexes


code:
reader = csv.reader(scsv.split('\n'), delimiter=',')
for row in reader:
    print '\t'.join(row)

rawDescription: Python csv string to array

description: Python csv string to array


code:
import sys
import time
import os
import mechanize
import cookielib
from getpass import getpass

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
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')]
url = 'http://site23.way2sms.com/content/index.html?'
op = br.open(url)
br.select_form(nr=0)
stamp = "\n        +=======================================+\n        |..........Way2SMS Msg Tool v 1.........|\n        +---------------------------------------+\n        |#Author: 7H3 !N5|D3R                   |\n        |#Contact: www.fb.com/Gowtham95india    |\n        |#Date: 10/09/2014                      |\n        |#This tool is made for pentesting.     |\n        |#Changing the Description of this tool |\n        |Won't make you the coder ^_^ !!!       |\n        |#Respect Coderz Plz ^_^                |\n        |#I Take No Responsibilities For The    |\n        |  Use Of This Program !                |\n        +=======================================+\n        |.........  Way2SMS Automation .........|\n        +---------------------------------------+\n"
print stamp
username = raw_input('Mobile Number:')
password = getpass('Password:')
token = ''

def login():
    global username
    global token
    global password
    br.select_form(nr=1)
    br.form['username'] = username
    br.form['password'] = password
    br.submit()
    a = br.geturl()
    if username in br.geturl():
        print "[-] Login Failed."
        time.sleep(3)
        print "[-] Exiting Now..."
        time.sleep(3)
        os.system('clear')
        sys.exit()
        return False
    else:
        i = a.index('=')
        j = a.index('?')
        token = a[i + 1:j]
        return token

def send(mobile, message):
    smsurl = 'http://site23.way2sms.com/sendSMS?Token=' + token
    br.open(smsurl)
    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'),
                     ('Referer', 'http://site25.way2sms.com/sendSMS?Token=' + token)]
    br.select_form(nr=0)
    br.form.set_all_readonly(False)
    message += ' ' * (140 - len(message))
    br.form['mobile'] = mobile
    br.form['message'] = message
    br.form['msgLen'] = str(140)
    br.form.method = 'POST'
    sendurl = 'http://site23.way2sms.com/smstoss.action'
    br.form.action = sendurl
    br.form.fixup()
    res = br.submit()
    if 'status=0' in br.geturl():
        return True
    else:
        return False

if __name__  == '__main__':
    send(raw_input("Enter recpt: "),raw_input("Enter Msg: "))

rawDescription: Python Mechanize - Way2Sms Sending Message


code:
from find_ORF_sequences import find_ORF_seq
from ATOz_get_sequences import getSequences

fdna = 'atgggatggtagatggatgggATGGGGTGA'

print 'fdna == %s' % fdna
print 'find_ORF_seq(fdna)\n',find_ORF_seq(fdna)
print 'getSequences(fdna)\n',getSequences(fdna)

rawDescription: Limiting re.findall() to record values before a certain number. Python


rawDescription: Placing 2D Array from PostgreSQL Database into 2d List Python using Django Interface


code:
import numpy as np
from tvtk.api import tvtk

def main():
    # Generate some data with anisotropic cells...
    # x,y,and z will range from -2 to 2, but with a
    # different (20, 15, and 5 for x, y, and z) number of steps
    x,y,z = np.mgrid[-2:2:20j, -2:2:15j, -2:2:5j]
    r = np.sqrt(x**2 + y**2 + z**2)

    dx, dy, dz = [np.diff(it, axis=a)[0,0,0] for it, a in zip((x,y,z),(0,1,2))]

    # Your actual data is a binary (logical) array
    max_radius = 1.5
    data = (r <= max_radius).astype(np.int8)

    ideal_volume = 4.0 / 3 * max_radius**3 * np.pi
    coarse_volume = data.sum() * dx * dy * dz
    est_volume = vtk_volume(data, (dx, dy, dz), (x.min(), y.min(), z.min()))

    coarse_error = 100 * (coarse_volume - ideal_volume) / ideal_volume
    vtk_error = 100 * (est_volume - ideal_volume) / ideal_volume

    print 'Ideal volume', ideal_volume
    print 'Coarse approximation', coarse_volume, 'Error', coarse_error, '%'
    print 'VTK approximation', est_volume, 'Error', vtk_error, '%'

def vtk_volume(data, spacing=(1,1,1), origin=(0,0,0)):
    data[data == 0] = -1
    grid = tvtk.ImageData(spacing=spacing, origin=origin)
    grid.point_data.scalars = data.T.ravel() # It wants fortran order???
    grid.point_data.scalars.name = 'scalars'
    grid.dimensions = data.shape

    iso = tvtk.ImageMarchingCubes(input=grid)
    mass = tvtk.MassProperties(input=iso.output)
    return mass.volume

main()

rawDescription: Good algorithm for computing volume or surface area in python


code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-f','--filename',action='store',help='file!')
namespace = parser.parse_args()
print namespace.filename

rawDescription: How to parse arguments in python 2.6 with prefix option as -f file.xml

description: Parse arguments in python 2.6 with prefix option as -f file.xml


code:
import functools

class MetaTrackinits(type):

  being_inited = []

  def __new__(cls, n, b, d):
    clob = type.__new__(cls, n, b, d)

    theinit = getattr(clob, '__init__')
    @functools.wraps(theinit)
    def __init__(self, *a, **k):
      MetaTrackinits.being_inited.append(self)
      try: theinit(self, *a, **k)
      finally: MetaTrackinits.being_inited.pop()
    setattr(clob, '__init__', __init__)

    def Instantiator(self, where=-2):
      return MetaTrackinits.being_inited[where]
    setattr(clob, 'Instantiator', Instantiator)

    return clob

__metaclass__ = MetaTrackinits

class Parent():
  def __init__(self):
    self.child = Child()

class Child():
  def __init__(self):
    self.parent = self.Instantiator()

p = Parent()
print p
print p.child.parent

rawDescription: Possible to return instantiator in Python?


code:
bins = 32
size = 16384
plotData = []
for distribution in DISTRIBUTIONS:
    try:
        # Create random data
        rv = pd.Series(distribution.rvs(size=size))
        # Get sane start and end points of distribution
        start = distribution.ppf(0.01)
        end = distribution.ppf(0.99)

        # Build PDF and turn into pandas Series
        x = np.linspace(start, end, size)
        y = distribution.pdf(x)
        pdf = pd.Series(y, x)

        # Get histogram of random data
        b = np.linspace(start, end, bins+1)
        y, x = np.histogram(rv, bins=b, normed=True)
        x = [(a+x[i+1])/2.0 for i,a in enumerate(x[0:-1])]
        hist = pd.Series(y, x)

        # Create distribution name and parameter string
        title = '{}({})'.format(distribution.dist.name, ', '.join(['{}={:0.2f}'.format(k,v) for k,v in distribution.kwds.items()]))

        # Store data for later
        plotData.append({
            'pdf': pdf,
            'hist': hist,
            'title': title
        })

    except Exception:
        print 'could not create data', distribution.dist.name

rawDescription: What do all the distributions available in scipy.stats look like?


code:
import re

import pandas as pd
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import _find_element

class text_to_change(object):
    def __init__(self, locator, text):
        self.locator = locator
        self.text = text

    def __call__(self, driver):
        actual_text = _find_element(driver, self.locator).text
        return actual_text != self.text

#Load URL
driver = webdriver.Firefox()
driver.get(url)

#Load DataFrame of terms to search for
df = pd.read_csv("searchkey.csv")

#Crawling function
def crawl(searchkey):
    try:
        text_before = driver.find_element_by_class_name("ac_results").text
    except NoSuchElementException:
        text_before = ""

    searchbox = driver.find_element_by_name("searchbox")
    searchbox.clear()
    searchbox.send_keys(searchkey)
    print "\nSearching for %s ..." % searchkey

    WebDriverWait(driver, 10).until(
        text_to_change((By.CLASS_NAME, "ac_results"), text_before)
    )

    search_result = driver.find_element_by_class_name("ac_results")
    if search_result.text != "none":
        names = re.match(r"^.*(?=(\())", search_result.text).group().encode("utf-8")
        insrefs = re.findall(r"((?<=\()[0-9]*)", search_result.text)
    if search_result.text == "none":
        names = re.match(r"^.*(?=(\())", search_result.text)
        insrefs = re.findall(r"((?<=\()[0-9]*)", search_result.text)
    return pd.Series([insrefs, names])

#Run crawl
df[["Insref", "Name"]] = df["ISIN"].apply(crawl)

#Print DataFrame
print df

rawDescription: Selenium: Wait until text in WebElement changes


code:
from shapely.geometry import LineString

l1 = LineString([(0,0), (10,10)])
l2 = LineString([(1,0), (5,10), (10,0)])

intersection = l1.intersection(l2)
intersect_points = [list(p.coords)[0] for p in intersection]
print intersect_points

rawDescription: Finding intersection of points on a python graph generated by a list of points


code:
from jinja2 import Template

template = Template("""
{%- for key, value in dictionary.items() recursive %}
  <li>{{ key }}
    {%- if value %}
      Recursive {{ key }}, {{value}}
      <ul>{{ loop(value.items())}}</ul>
    {%- endif %}
  </li>
{%- endfor %}
""")

print template.render(dictionary={'a': {'b': {'c': {}}}})

rawDescription: jinja2 recursive loop vs dictionary

description: Jinja2 recursive loop vs dictionary


code:
my_list_of_tuples = [(1,2),(3,5),(7,8),(7,9)]
for item in my_list_of_tuples:
    #on first loop item=(1,2) ; 2nd loop item=(3,5); 3rd loop item=(7,8)
    left_number = item[0]
    right_number = item[1]
    #these 2 lines can be simplified to
    left_number,right_number = item

    #now if left number is already in the data dictionary we will append the new right number to its list
    try:
        data[left_number].append(right_number)
    except KeyError: #: if the left_number is not already in our data dictionary
        data[left_number] = [right_number] #: create a new list that contains only the right number

#now that we are done we have data that maps left numbers to right numbers
print data
#{1:[2],3:[5],7:[8,9]}

rawDescription: How can I separate and traverse data in a list of tuples in Python 2.6?

description: Separate and traverse data in a list of tuples in Python 2.6


code:
from matplotlib.pyplot import figure, show
import numpy as npy
from numpy.random import rand

if 1: # picking on a scatter plot (matplotlib.collections.RegularPolyCollection)

    x, y, c, s = rand(4, 100)
    def onpick3(event):
        ind = event.ind
        print 'onpick3 scatter:', ind, npy.take(x, ind), npy.take(y, ind)

    fig = figure()
    ax1 = fig.add_subplot(111)
    col = ax1.scatter(x, y, 100*s, c, picker=True)
    #fig.savefig('pscoll.eps')
    fig.canvas.mpl_connect('pick_event', onpick3)

show()

rawDescription: Python: get corresponding information of data points interactively with mouse


code:
searchNode = 'coordinates'
# loop through all 'Placemark nodes' in document
for mark in placemark:
    # Get children of 'Placemark' node
    children = mark.getchildren()
    # I imagine that getchildren() might return None, so check it
    # otherwise you'll get an error when trying to iterate on it
    if children:
        # Loop through children nodes
        for child in children:
            # if a 'Polygon' node is found
            if child.tag == 'Polygon':
                # call recursion function and print (or save) result
                print getCoords( child, searchNode)

rawDescription: Xml parsing with Python using recursion. Problem with return value


code:
tests = [
    {
        'text': 'test file with words fish, steak fish chips fish and '+
            'chips and fries',
        'wanted': ["fish and chips","fish","chips","steak"]
    },
    {
        'text': 'fish, fish and chips, fish and chips and burgers',
        'wanted': ["fish and chips","fish","fish and chips and burgers"]
    },
    {
        'text': 'fish, fish and chips and burgers',
        'wanted': ["fish and chips","fish","fish and chips and burgers"]
    },
    {
        'text': 'My fish and chips and burgers. My fish and chips and '+
            'burgers',
        'wanted': ["fish and chips","fish","fish and chips and burgers"]
    },
    {
        'text': 'fish fish fish',
        'wanted': ["fish fish","fish"]
    },
    {
        'text': 'fish fish fish',
        'wanted': ["fish fish","fish","fish fish fish"]
    }
]

for i in range(0,len(tests)):
    test = tests[i]['text']
    print test
    print get_word_counts(test, tests[i]['wanted'])
    print ''

rawDescription: Count strings in a file, some single words, some full sentences


code:
myList = [4, 5, [7, [3], 4], 8]

def tuple_recursive(my_list):
    if isinstance(my_list, list):
        return tuple(tuple_recursive(inner) for inner in my_list)
    else:
        return my_list

print tuple_recursive(myList)

rawDescription: multiple list to tuple

description: Multiple list to tuple


code:
import argparse

class Default(object):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return str(self.value)

DEFAULT_PATH = Default('/some/path')

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument('--path', '-p',
                   default=DEFAULT_PATH)
    return p.parse_args()

def main():
    args = parse_args()

    if args.path is DEFAULT_PATH:
        print 'USING DEFAULT'
    else:
        print 'USING EXPLICIT'

    print args.path

if __name__ == '__main__':
    main()

rawDescription: Can I check if a value was supplied by the default or by the user?

description: Check if a value was supplied by the default or by the user


code:
def table_things(**kwargs):
    for name, value in kwargs.items():
        print '{0} = {1}'.format(name, value)
table_things(apple = 'fruit', cabbage = 'vegetable')

rawDescription: *args and **kwargs?

description: *args and **kwargs


rawDescription: python selenium - print xpath value

description: Python selenium - print xpath value


code:
for combo in dashed_combos('the man said hi'):
    print combo

rawDescription: Python replace spaces in string iteratively


code:
with open("file1") as f1,open("file2") as f2:
    words=set(line.strip() for line in f1)   #create a set of words from dictionary file

    #why sets? sets provide an O(1) lookup, so overall complexity is O(N)

    #now loop over each line of other file (word, freq file)
    for line in f2:
        word,freq=line.split()   #fetch word,freq
        if word in words:        #if word is found in words set then print it
            print word

rawDescription: compare two file and find matching words in python

description: Compare two file and find matching words in python


code:
import threading
import socket

is_shutdown = False
BUFFERSIZE_IN = 32768

def my_tcp_server():

    # Establish a TCP Connection
    sock_in = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock_in.bind((host_ip, host_port))
    sock_in.settimeout(10000)
    sock_in.listen(1)
    print "ready"

    while not is_shutdown:
        print "before accept"
        conn, address = sock_in.accept()
        print "after accept"
        try:
            recv_buffer = conn.recv(BUFFERSIZE_IN)
            print "recv works!"
            if recv_buffer != "":
                msg = recv_buffer.decode('utf-8')
                print "Received",msg
        except socket.error as socket_error:
            print  "SocketError: %s", str(socket_error)
        conn.close()
    print "Shutting down server"
    sock_in.close()

def main():
    print "Starting.."
    threading.Thread(target=my_tcp_server).start()
def sendData():
    sock_out = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP Connection
    sock_out.connect((remote_ip, remote_port))
    sock_out.send("ASD")
    sock_out.close()

host_ip="127.0.0.1"
remote_ip = host_ip
host_port = 8073
remote_port = host_port

main()
print "Sending"
sendData()

print "Completed"
is_shutdown = True

rawDescription: Python error: [Errno 111] Connection refused


code:
import urllib2
from bs4 import BeautifulSoup

url = "http://www.dailysabah.com/world/2012/02/20/seeking-international-support-to-block-assad"

data = urllib2.urlopen(url)
soup = BeautifulSoup(data)

print soup.find('meta', itemprop='datePublished', content=True)['content']

rawDescription: Get the date from span tags

description: Get the date from span tags


code:
from lxml import etree
from StringIO import StringIO

xml_str = '''<?xml version="1.0" encoding="iso-8859-1"?>
 <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
  <root>
   <a>&tasty;</a>
 </root>
'''

# get the ElementTree using parse
parse_tree = etree.parse(StringIO(xml_str))
encoding = parse_tree.docinfo.encoding
result = etree.tostring(parse_tree, xml_declaration=True, encoding=encoding)
print "%s\nparse ElementTree:\n%s\n" % ('-'*20, result)

# get the ElementTree using fromstring
fromstring_tree = etree.fromstring(xml_str).getroottree()
encoding = fromstring_tree.docinfo.encoding
result = etree.tostring(fromstring_tree, xml_declaration=True, encoding=encoding)
print "%s\nfromstring ElementTree:\n%s\n" % ('-'*20, result)

# DOCTYPE is lost, and no access to encoding
fromstring_element = etree.fromstring(xml_str)
result = etree.tostring(fromstring_element, xml_declaration=True)
print "%s\nfromstring Element:\n%s\n" % ('-'*20, result)

rawDescription: Preserving original doctype and declaration of an lxml.etree parsed xml


code:
for link in mysite.getallLinks():
    if link[0] == firstletter:
        print link

rawDescription: A question on python sorting efficiency

description: A question on python sorting efficiency


code:
for x in gradebook:
    print x[0], x[1]

rawDescription: How to print a list of tuples

description: Print a list of tuples


code:
def word_locator(file, list_of_words):
    dict={}
    with open(file) as f:
        for i, l in enumerate(f):
            for word in l.split(" "):
                if word in list_of_words:
                    if word in dict.keys():dict[word].append(i)
                    else:dict[word]=[i]
    return dict

word_location_dictionary = word_locator('sawyer.txt', ['hello','cloud','cherry','tree'])

for k, v in word_location_dictionary.items():
    lines = map(str, v)
    print k + " " + ' '.join(lines)

rawDescription: Print the line number of a text file for each word from a list in python 3 without importing anything


code:
for word, text in data:
    print word, text

rawDescription: Add in words after line split and loop it?


code:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import RectangleSelector

def main():
    x, y, z = np.random.random((3, 100))
    z *= 10
    fig, axes = plt.subplots(figsize=(6, 8), nrows=2, sharex=True)
    axes[0].scatter(x, y, color='black')
    axes[1].scatter(x, z, color='black')
    axes[0].set(ylabel='Y')
    axes[1].set(xlabel='X', ylabel='Y')

    highlighter = Highlighter(axes, x, y, z)
    plt.show()

    selected_regions = highlighter.mask
    print x[~selected_regions], y[~selected_regions], z[~selected_regions]

class Highlighter(object):
    def __init__(self, axes, x, y, z):
        self.axes = axes
        self.canvas = axes[0].figure.canvas
        self.x, self.y, self.z = x, y, z
        self.mask = np.zeros(x.shape, dtype=bool)

        self._highlights = [ax.scatter([], [], s=200, color='yellow', zorder=10)
                               for ax in axes]

        self._select1 = RectangleSelector(axes[0], self.select_xy, useblit=True)
        self._select2 = RectangleSelector(axes[1], self.select_xz, useblit=True)

    def select_xy(self, event1, event2):
        self.mask |= self.inside(event1, event2, self.x, self.y)
        self.update()

    def select_xz(self, event1, event2):
        self.mask |= self.inside(event1, event2, self.x, self.z)
        self.update()

    def update(self):
        xy = np.column_stack([self.x[self.mask], self.y[self.mask]])
        self._highlights[0].set_offsets(xy)

        xz = np.column_stack([self.x[self.mask], self.z[self.mask]])
        self._highlights[1].set_offsets(xz)

        self.canvas.draw()

    def inside(self, event1, event2, x, y):
        x0, x1 = sorted([event1.xdata, event2.xdata])
        y0, y1 = sorted([event1.ydata, event2.ydata])
        return (x > x0) & (x < x1) & (y > y0) & (y < y1)

main()

rawDescription: Choosing a box of data points from a plot


code:
class A:
    def __init__(self, name):
        self.__name = name

    def print_name(self):
        print self.__name

class B(A):
    def __init__(self, name):
        A.__init__(self, name)
        self.__name = name + "yes"

    def print_name(self):
        print self.__name

    def print_super_name(self):
        print self._A__name #class name mangled into attribute

rawDescription: Subclass variables with the same name of superclass ones


code:
import numpy as np

A = np.arange(10)
cut = 6
B = np.array([A[k-cut:k] for k in xrange(cut,A.size)])
print B

rawDescription: How to preprocess time series data in Python for forecasting

description: Preprocess time series data in Python for forecasting


code:
from datetime import date, timedelta

def allsundays(year):
    """This code was provided in the previous answer! It's not mine!"""
    d = date(year, 1, 1)                    # January 1st
    d += timedelta(days = 6 - d.weekday())  # First Sunday
    while d.year == year:
        yield d
        d += timedelta(days = 7)

Dict = {}
for wn,d in enumerate(allsundays(2010)):
    # This is my only contribution!
    Dict[wn+1] = [(d + timedelta(days=k)).isoformat() for k in range(0,7) ]

print Dict

rawDescription: How can I get the current week using Python?

description: Get the current week using Python


code:
#!/usr/bin/python

import inspect
import os
import sys
import webbrowser
from pydoc import Helper

__all__ = ['MyHelper', 'old_help', 'help']

class MyHelper(Helper):

    def __init__(self):
        Helper.__init__(self)

    def getdocloc(self, object):
        global old_help
        """Return the location of module docs or None"""
        try:
            file = inspect.getabsfile(object)
        except TypeError:
            file = '(built-in)'

        docloc = os.environ.get("PYTHONDOCS", "http://docs.python.org/library")
        basedir = os.path.join(sys.exec_prefix, "lib", "python"+sys.version[0:3])
        BUILTINS_LIST = ( 'errno', 'exceptions', 'gc', 'imp', 'marshal', 'posix', 'signal', 'sys', 'thread', 'zipimport')
        if (isinstance(object, type(os)) and (object.__name__ in BUILTINS_LIST or file.startswith(basedir)) and object.__name__ not in ('xml.etree', 'test.pydoc_mod')):
            if docloc.startswith("http://"):
                docloc = "%s/%s" % (docloc.rstrip("/"), object.__name__)
            else:
                docloc = os.path.join(docloc, object.__name__ + ".html")
        else:
            docloc = None
        return docloc

    def __repr__(self):
        global old_help
        if inspect.stack()[1][3] == '?':
            return self()
            return ''
        else:
            return '<HHhelp instance>'

    def help(self, *args):
        print args
        global old_help
        if isinstance(args[0], object):
            docloc = self.getdocloc(args[0])
            print '********',docloc,type(docloc)
            if isinstance(docloc, str):
                if docloc.find('http://') == 0:
                    webbrowser.open(docloc)
        else:
            old_help(args)

global old_help, help
old_help = help
help = MyHelper()

rawDescription: Set the wrap width for python docs in a terminal


code:
import json

data = [('king', 'governor'),
        ('governor', 'editor'),
        ('king', 'state'),
        ('state', 'collapse'),
        ('collapse', 'coverage'),
        ('collapse', 'author'),
        ('collapse', 'opening'),
        ('state', 'head'),
        ('state', 'lord')];

root = data[0][0]
node2chilren = {root: []}
for parent, child in data:
    childnode = {child: []}
    children = node2chilren[parent]
    children.append(childnode)
    node2chilren[child] = childnode[child]

jsonstr = json.dumps({root: node2chilren[root]}, indent=4)
print jsonstr

rawDescription: Convert list of graph edges to JSON tree


code:
from urlparse import urlparse
def nestForLoop():
    lines = open("ampersand_right_split.txt", 'r').readlines()
    with open("newfile3.txt".format(), 'w') as f:

        for l in lines:
            url = urlparse(l)
            if url.query:
                #param, value = str.split("?",1)
                queryvars = url.query # Good to know, but why did we get this again?
                if len(l) == 0:
                    break
                print l
                f.write(l)

nestForLoop()

rawDescription: Looking to write out to separate files; Python:


code:
import json
a = open('abc.log','r')
text = a.read()
text_as_list = text.split('\n')
keys = text_as_list[2].split()
result = []
for item in text.split('\n')[4:len(text_as_list)]:
    temp_dict = {}
    for i,j in zip(keys,item.split()):
        if j.isdigit():
            temp_dict[i] = int(j)
        else:
            temp_dict[i] = j
    result.append(temp_dict)
print json.dumps(result)

rawDescription: How can I parse a log file to JSON

description: Parse a log file to JSON


code:
odds = set()
remove = odds.remove
add = odds.add
for i in B:
    if i in odds:
        remove(i)
    else:
        add(i)
print len(odds) <= 1

rawDescription: How to check if a sequence could be turned into a palindrome

description: Check if a sequence could be turned into a palindrome


code:
import types

class ClassA(object):
    def Test(self):
        print 'haha'

obj = ClassA()
obj.Test()
# haha

rawDescription: How to update the the class type and methods in an existing object

description: Update the the class type and methods in an existing object


code:
class DisjointSet(object):

    def __init__(self):
        self.leader = {} # maps a member to the group's leader
        self.group = {} # maps a group leader to the group (which is a set)

    def add(self, a, b):
        leadera = self.leader.get(a)
        leaderb = self.leader.get(b)
        if leadera is not None:
            if leaderb is not None:
                if leadera == leaderb: return # nothing to do
                groupa = self.group[leadera]
                groupb = self.group[leaderb]
                if len(groupa) < len(groupb):
                    a, leadera, groupa, b, leaderb, groupb = b, leaderb, groupb, a, leadera, groupa
                groupa |= groupb
                del self.group[leaderb]
                for k in groupb:
                    self.leader[k] = leadera
            else:
                self.group[leadera].add(b)
                self.leader[b] = leadera
        else:
            if leaderb is not None:
                self.group[leaderb].add(a)
                self.leader[a] = leaderb
            else:
                self.leader[a] = self.leader[b] = a
                self.group[a] = set([a, b])

data = """T1 T2
T3 T4
T5 T1
T3 T6
T7 T8
T3 T7
T9 TA
T1 T9"""
# data is chosen to demonstrate each of 5 paths in the code
from pprint import pprint as pp
ds = DisjointSet()
for line in data.splitlines():
    x, y = line.split()
    ds.add(x, y)
    print
    print x, y
    pp(ds.leader)
    pp(ds.group)

rawDescription: A set union find algorithm

description: A set union find algorithm


code:
xings = [123,125,127,1045,1049,1050,2147,2147,2151,2155]

THRESHOLD = 100

xlast = -1000000
tot = 0
n = 0
results = []
i = 0
while i < len(xings):
    x = xings[i]
    if x-xlast > THRESHOLD:
        # emit a transition, averaged for the
        if n > 0:
            results.append(tot/n)
        tot = 0
        n = 0
    tot += x
    n += 1
    xlast = x
    i += 1
if n > 0:
    results.append(tot/n)

print results

rawDescription: determine mean zero crossing

description: Determine mean zero crossing


code:
while word != solution_list1[0]:
    print "Sorry, you are wrong. Try again!"
    user_input = raw_input("Type in a: " + replacement + " ") # ask the user again
    word = word.replace(replacement, user_input)

rawDescription: Nested If Expression with user validation - repeating the prompt for user input


code:
combining_cedilla = u'\u0327'
c_with_cedilla = u'\u00e7'
letter_c = u'\u0063'
print c_with_cedilla
print letter_c + combining_cedilla

rawDescription: How do I iterate through unicode symbols, not bytes in python?

description: Iterate through unicode symbols, not bytes in python


code:
from BeautifulSoup import BeautifulSoup
import re

html_text = """

this is cool #12345678901

this is nothing

foo #126666678901

this is interesting #126666678901

this is blah #124445678901

"""

soup = BeautifulSoup(html_text)

for elem in soup(text=re.compile(r' #\S{11}')):
    print elem.parent

rawDescription: Using BeautifulSoup to find a HTML tag that contains certain text


code:
def sort_by_level(gb):
    return sorted(gb, key=lambda x: int(next(i for i in x[0] if i.isdigit())))

data = [('CS1010S', 'A+'), ('MA1101R', 'C'), ('SSA1207', 'B+'), ('CS2020', 'A')]
print sort_by_level(data)

rawDescription: Sorting from first lowest number

description: Sorting from first lowest number


code:
from collections import OrderedDict

lis=[[1,2,1],
     [3,4,1],
     [1,2,2],
     [3,4,2]]

def f1(lis):
    seen=set()
    return [li for li in lis
             if tuple(li[:2]) not in seen and not seen.add(tuple(li[:2]))]

def f2(lis):
    dic = OrderedDict()
    for item in lis:
        key = tuple(item[:2])
        if key not in dic:
            dic[key] = item

    return dic.values()

if __name__ == '__main__':
    import timeit
    print 'f1, LC+set:',timeit.timeit("f1(lis)", setup="from __main__ import f1,lis"),'secs'
    print 'f2, OrderedDic:',timeit.timeit("f2(lis)", setup="from __main__ import f2,lis,OrderedDict"),'secs'

rawDescription: Cycling through a list of list, checking and removing?


code:
class Person:
    ID = itertools.count()
    def __init__(self, name, parent=None, level=0):
        self.id = self.__class__.ID.next() # next(self.__class__.ID) in python 2.6+
        self.parent = parent
        self.name = name
        self.level = level
        self.children = []

def createTree(d, parent=None, level=0):
    if d:
        member = Person(d['parent'], parent, level)
        level = level + 1
        member.children = [createTree(child, member, level) for child in d['children']]
        return member

t = createTree(my_tree)          # my_tree is the name of your dictionary
def printout(parent, indent=0):
    print '\t'*indent, parent.name
    for child in parent.children:
        printout(child, indent+1)
printout(t)

rawDescription: How to create Family tree?

description: Create Family tree


code:
dictionary = {'Louis': [2, 10, 1],
               'John': [6, 1, 16]
               }
for name in sorted(dictionary.keys()):
    print name
    print sorted(dictionary[name], reverse=True)

rawDescription: Sorting a list inside dictionary keys

description: Sorting a list inside dictionary keys


code:
def print_tree(current_node, indent="", last='updown'):

    nb_children = lambda node: sum(nb_children(child) for child in node.children) + 1
    size_branch = {child: nb_children(child) for child in current_node.children}

    """ Creation of balanced lists for "up" branch and "down" branch. """
    up = sorted(current_node.children, key=lambda node: nb_children(node))
    down = []
    while up and sum(size_branch[node] for node in down) < sum(size_branch[node] for node in up):
        down.append(up.pop())

    """ Printing of "up" branch. """
    for child in up:
        next_last = 'up' if up.index(child) is 0 else ''
        next_indent = '{0}{1}{2}'.format(indent, ' ' if 'up' in last else '│', " " * len(current_node.name))
        print_tree(child, indent=next_indent, last=next_last)

    """ Printing of current node. """
    if last == 'up': start_shape = '┌'
    elif last == 'down': start_shape = '└'
    elif last == 'updown': start_shape = ' '
    else: start_shape = '├'

    if up: end_shape = '┤'
    elif down: end_shape = '┐'
    else: end_shape = ''

    print '{0}{1}{2}{3}'.format(indent, start_shape, current_node.name, end_shape)

    """ Printing of "down" branch. """
    for child in down:
        next_last = 'down' if down.index(child) is len(down) - 1 else ''
        next_indent = '{0}{1}{2}'.format(indent, ' ' if 'down' in last else '│', " " * len(current_node.name))
        print_tree(child, indent=next_indent, last=next_last)

rawDescription: How to print a tree in Python?

description: Print a tree in Python


code:
print BeautifulSoup(txt, 'html5lib').prettify()
print BeautifulSoup(txt, 'lxml').prettify()

rawDescription: beautifulsoup configure autoclosing tags

description: Beautifulsoup configure autoclosing tags


code:
#!/usr/bin/env python2.7

import sys, lxml.etree

tree = lxml.etree.parse(sys.stdin)

for el in tree.findall('setting'):
    print el.attrib['id'], '=', el.attrib['value']

rawDescription: XML conversion into a cfg file

description: XML conversion into a cfg file


code:
for i, j in dict.iteritems():
    phoneNum = phoneNum.replace(i, j)
    print "Number: ",newNumber

rawDescription: Trouble with Changing Characters in a String Then Returning the String


code:
p = subprocess.Popen(some_cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
# Send input to p.
p.stdin.write("some input\n")
p.stdin.flush()
# Now start grabbing output.
while p.poll() is None:
    l = p.stdout.readline()
    print l
print p.stdout.read()

rawDescription: printing stdout in realtime from a subprocess that requires stdin

description: Printing stdout in realtime from a subprocess that requires stdin


code:
import re

def split_caps(s):
    return "_".join(re.findall('[A-Z][^A-Z]*', s)).lower()

print split_caps("SomeStringLikeThis")
# some_string_like_this

rawDescription: python: SomeStingLikeThis to some_string_like_this

description: Python: SomeStingLikeThis to some_string_like_this


code:
import re

ss = '''This is the [first]       line
(And) another line
   [Inter][A] initially shifted
[Finally][B] the last
    Additional ending lines (this one without brackets):
[Note that [ by the way [ref [ 1]] there are]    [some] other ]cases
tuvulu[]gusti perena[3]              bdiiii
    [Away [is this] [][4] ] shifted content
    fgjezhr][fgh
'''

def clean(x, regx = re.compile('( |(?<! ))+((?<!])\[[^[\]]*\])( *)')):
    while regx.search(x):
        print '------------\n',x,'\n','\n'.join(map(str,regx.findall(x)))
        x = regx.sub('\\1',x)
    return x

print '\n==========================\n'+clean(ss)

rawDescription: Using regex to replace object within brackets in a text file


code:
root = Tk.Tk()
root.wm_title("minimal example")

root.image = plt.imread('test.jpg')
fig = plt.figure(figsize=(5,4))
im = plt.imshow(root.image) # later use a.set_data(new_data)
ax = plt.gca()
ax.set_xticklabels([])
ax.set_yticklabels([])

# a tk.DrawingArea
canvas = FigureCanvasTkAgg(fig, master=root)
canvas.show()
canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)

def rotate(*args):
    print 'rotate button press...'
    root.image = ndimage.rotate(root.image, 90)
    im.set_data(root.image)
    canvas.draw()

rawDescription: updating matplotlib imshow from within a Tkinter gui

description: Updating matplotlib imshow from within a Tkinter gui


code:
r = re.compile("(([\d,]+).*\(?(\w+)?\)?)")
for line, value, unit in r.findall(text):
    print value, unit

rawDescription: Extracting numeric data Python

description: Extracting numeric data Python


code: print os.path.basename(directoryPath)

rawDescription: Python: get the names of specific file types in a given folder


code:
for r in range(1,10):
    for c in range(r):
        print c + 1,
    print ""

rawDescription: range() function is giving me trouble

description: Range() function is giving me trouble


code:
from django.conf import settings #this imports also your specific settings.py

print settings.SITE_URL

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

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


code:
_cls_lookup = {}
def register_cls(cls):
    _cls_lookup[cls.__name__] = cls
    return cls

def lookup_cls(cls_name):
    return _cls_lookup[cls_name]

@register_cls
class BigClass:
    static_data = 42

bc = lookup_cls("BigClass")
print bc.static_data

rawDescription: Python - How to call static member using class string?


code:
def split_by(iterable, split_by):
    group = []
    for elem in iterable:
        if elem == split_by:
            if group:
                yield group
            group = []
        group.append(elem)
    if group:
        yield group

L = ['@@', ' n', ' .', '  ', '-\\', '@@', '+A', '+u', '@@', '+g', '+r', '+u']
for group in split_by(L, '@@'):
    print group

rawDescription: How can I split a List (in python) in different parts (after a certain element)?

description: Split a List (in python) in different parts (after a certain element)


code:
n = 2
with open("t.txt") as fin:
    for i in range(n):
        any(x.isspace() for x in fin)
    print next(fin)

rawDescription: Python Print text after certain number of blank lines


code:
from subproccess import *
x = Popen('sh /root/script.sh', stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True)
print x.stdout.readline()
print x.stderr.readline()

rawDescription: Popen mixed data stream?

description: Popen mixed data stream


code:
the_line_you_started_with = [{'card_month': '10'}, {'card_year':'2011'}]
new_dict = {}
new_dict.update(the_line_you_started_with[0])
new_dict.update(the_line_you_started_with[1])

print new_dict

rawDescription: how do i get rid of dict sign from a list in python?

description: Get rid of dict sign from a list in python


code:
day_clicks = []
for item in items:
    day = datetime.datetime.strptime(item.get('date_created'), "%Y-%m-%d").date().strftime("%a")
    click_count = item.get('created_count')
    day_clicks.append(day + ':' + str(click_count))
print ", ".join(day_clicks)

rawDescription: Django order_by last week days

description: Django order_by last week days


code:
idnum = 11
print sum(idnum in idnumber for idnumber in A.itervalues())

rawDescription: Search for a key in a nested Python dictionary


code:
print [str(x) for x in idx.tolist()]
['Newal', 'Saraswati Khera', 'Tohana']

rawDescription: how to convert Index into list?

description: Convert Index into list


code:
import numpy as np
data = np.arange(1000).reshape((10,10,10))

# Delete the third slice along the first axis
# (note that you can delete multiple slices at once)
data = np.delete(data, [2], axis=0)

print data.shape

rawDescription: Delete image frame from 3d array

description: Delete image frame from 3d array


code:
user_input = '2'
for key,value in elements.items():
   if user_input == key:
      print value
   if user_input == value:
      print key

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

description: Access part of a dictionary in python


code:
def str(self, item):
    print item

    prev, current = None, self.__iter.next()
    while isinstance(current, int):
        print current
        prev, current = current, self.__iter.next()

rawDescription: Making a python iterator go backwards?

description: Making a python iterator go backwards


code:
l= df[df['B']==2]
print l

rawDescription: Conditional probability density estimation in python


code:
from cStringIO import StringIO
for item in timesplit(StringIO(a)):
  print "Found:", item
  print "Parsed:", p.parse(StringIO(item))

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

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


code:
b = soup.new_tag('b')
b.append(text[start:end])
soup.p.append(b)
print soup

rawDescription: Python BeautifulSoup - Add Tags around found keyword


code:
with open("test.txt") as f:
    print f.readline() # => prints the first line, sets file pointer to second line
    print f.read() # => prints everything from the second line on

rawDescription: Reading and Writing files in Python

description: Reading and Writing files in Python


code:
socket.socket().connect(("192.168.95.148",21))
ans = socket.socket().recv(1024)
print ans

rawDescription: Clarification on socket.socket()

description: Clarification on socket.socket()


code:
# If thread is active
if p.is_alive():
    print "foo is running... let's kill it..."

    # Terminate foo
    p.terminate()

rawDescription: Stop code after time period

description: Stop code after time period


code:
from collections import Counter

def check_in_given_word(given_word, to_check):
    given_word_counter = Counter(given_word)
    word_counter = Counter(to_check)
    given_word_counter.subtract(word_counter)
    #if any -ve letter count is found, it is not in given_word
    if any([c < 0 for c in given_word_counter.values()]):
        # do whatever you want, or return False
        print "{} is NOT in {}".format(to_check, given_word)
    else:
        print "{} is in {}".format(to_check, given_word)
    # print the counter for your info
    print given_word_counter

rawDescription: Find characters of this word in another word Python


code:
def StartProcess():
    strings = [x.Text_Var.get() for x in entries]
    print " ".join(strings)

rawDescription: How get value from Tkinter Var in class?

description: How get value from Tkinter Var in class


code:
import collections

def d_sum(a, b):
    d = collections.defaultdict(int, a)
    for k, v in b.items():
        d[k] += v
    return dict(d)

print d_sum(d1, d2)
# {16: 7, 153: 21, 18: 9, 14: 3}

rawDescription: Merge two arrays by collections of two elements


code:
import re
st1='A B C D E F'
st2='B D E'
n=2
pat=r'(?=({}))'.format(r's+'.join(r'\w+' for i in range(n)))
print [(s, s in st1) for s in re.findall(pat, st2)]

rawDescription: Check if string contains a certain amount of words of another string


code:
import multiprocessing
import time

data = (
    ['a', '2'], ['b', '4'], ['c', '6'], ['d', '8'],
    ['e', '1'], ['f', '3'], ['g', '5'], ['h', '7']
)

def mp_worker((inputs, the_time)):
    print " Processs %s\tWaiting %s seconds" % (inputs, the_time)
    time.sleep(int(the_time))
    print " Process %s\tDONE" % inputs

def mp_handler():
    p = multiprocessing.Pool(2)
    p.map(mp_worker, data)

if __name__ == '__main__':
    mp_handler()

rawDescription: Dead simple example of using Multiprocessing Queue, Pool and Locking


code:
from collections import defaultdict
def identity(x):
    return x

class TreeReprMixin(object):
    def __repr__(self):
        base = dict(self)
        return repr(base)

class PrefixTree(TreeReprMixin, defaultdict):
    '''
    A hash-based Prefix or Suffix Tree for testing for
    sequence inclusion. This implementation works for any
    slice-able sequence of hashable objects, not just strings.
    '''
    def __init__(self):
        defaultdict.__init__(self, PrefixTree)
        self.labels = set()

    def add(self, sequence, label=None):
        layer = self
        if label is None:
            label = sequence
        if label:
            layer.labels.add(label)
        for i in range(len(sequence)):
            layer = layer[sequence[i]]
            if label:
                layer.labels.add(label)

        return self

    def add_ngram(self, sequence, label=None):
        if label is None:
            label = sequence
        for i in range(1, len(sequence) + 1):
            self.add(sequence[:i], label)

    def __contains__(self, sequence):
        layer = self
        j = 0
        for i in sequence:
            j += 1
            if not dict.__contains__(layer, i):
                break
            layer = layer[i]
        return len(sequence) == j

    def depth_in(self, sequence):
        layer = self
        count = 0
        for i in sequence:
            if not dict.__contains__(layer, i):
                print "Breaking"
                break
            else:
                layer = layer[i]
            count += 1
        return count

    def subsequences_of(self, sequence):
        layer = self
        for i in sequence:
            layer = layer[i]
        return layer.labels

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

class SuffixTree(PrefixTree):
    '''
    A hash-based Prefix or Suffix Tree for testing for
    sequence inclusion. This implementation works for any
    slice-able sequence of hashable objects, not just strings.
    '''
    def __init__(self):
        defaultdict.__init__(self, SuffixTree)
        self.labels = set()

    def add_ngram(self, sequence, label=None):
        if label is None:
            label = sequence
        for i in range(len(sequence)):
            self.add(sequence[i:], label=label)

rawDescription: Finding all common, non-overlapping substrings


code:
def test():
    "Stupid test function"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()

rawDescription: Managing Setup code with TimeIt

description: Managing Setup code with TimeIt


code:
from struct import *

def FindDateTimeOffsetFromCR2( buffer, ifd_offset ):
    # Read the number of entries in IFD #0
    (num_of_entries,) = unpack_from('H', buffer, ifd_offset)
    print "ifd #0 contains %d entries"%num_of_entries

    # Work out where the date time is stored
    datetime_offset = -1
    for entry_num in range(0,num_of_entries-1):
        (tag_id, tag_type, num_of_value, value) = unpack_from('HHLL', buffer, ifd_offset+2+entry_num*12)
        if tag_id == 0x0132:
            print "found datetime at offset %d"%value
            datetime_offset = value
    return datetime_offset

if __name__ == '__main__':
    with open("IMG_6113.CR2", "rb") as f:
        buffer = f.read(1024) # read the first 1kb of the file should be enough to find the date / time
        datetime_offset = FindDateTimeOffsetFromCR2(buffer, 0x10)
        print unpack_from(20*'s', buffer, datetime_offset)

rawDescription: Reading a CR2 (Raw Canon Image) header using Python


code:
with cat("/etc/passwd") as lines:
    for line in lines:
        if "mail" in line:
            print line.strip()
            break

rawDescription: How to break from a Python generator with open file handles

description: Break from a Python generator with open file handles


code:
import re

line = """<http://dbpedia.org/data/Plasmodium_hegneri.xml> <http://code.google.com/p/ldspider/ns#headerInfo> _:header16125770191335188966549 <http://dbpedia.org/data/Plasmodium_hegneri.xml> ."""
print re.sub('^(?P<before>.*)(?P<match>\<[^>]+\>)(?P<after>[^<]*)$', '#@ \g<match>\n\g<before>\g<after>', line)

line = """_:header16125770191335188966549 <http://www.w3.org/2006/http#responseCode> "200"^^<http://www.w3.org/2001/XMLSchema#integer> <http://dbpedia.org/data/Plasmodium_hegneri.xml> ."""
print re.sub('^(?P<before>.*)(?P<match>\<[^>]+\>)(?P<after>[^<]*)$', '#@ \g<match>\n\g<before>\g<after>', line)

rawDescription: Retaining split characters

description: Retaining split characters


code:
import getopt
import xml.etree as et

INSERTMAINCODEHERE

if __name__ == '__main__':
    try:
        opts, args = getopt.getopt(sys.argv[1:], "f:", ["configoption="])

    except getopt.GetoptError, e:
        print "getopt.GetoptError: %s" % e
        sys.exit(1)

    for o, a in opts:
        if o in ("-f","--configoption="):
            b=et.ElementTree.parse('configfilename')
            options=b.getroot().find(a)
            for s in list(options):
                print s, s.tag, s.tail, s.attrib #ETC. ETC.

rawDescription: Suitable ways to store python script configurations


code:
if index >= 5:
    break
print item.title

rawDescription: How "break" in iterative Python loop works


code:
import pickle
class vehicle:
    def __init__(self, vehicletype, fueltype, cost):
        self.vehicletype = vehicletype
        self.fueltype = fueltype
        self.cost = cost
    def GetVdata(self):
        self.vehicletype = raw_input("enter the vehicle type: ")
        self.fueltype = raw_input("enter the fuel type: ")
        self.cost = input("enter the cost of the vehicle: ")
    def ShowVdata(self):
        print "vehicle type : ", self.vehicletype, "\n" , "fuel type: ", self.fueltype , "\n", "cost : ", self.cost

dat1 = vehicle("4 wheeler", "diesel" , 2000000)
dat2 = vehicle("2 wheeler", "petrol" , 1000000)
dat3 = vehicle("2 wheeler", "petrol",  5000000)
dat4 = vehicle("4 wheeler", "diesel",  3000000)

data = [dat1, dat2, dat3, dat4]
costs = [i.cost for i in data]
costs.sort()

File = open('transport.dat' , 'wb')

for value in costs:
    for i in range(len(data)):
        if data[i].cost == value:
            pickle.dump(data[i], File)
File.close()

File = open('transport.dat','rb')
try:
    while True:
        print"\n"
        output = pickle.load(File)
        output.ShowVdata()
except EOFError:
    File.close()

rawDescription: Sorting objects of a class based on a particular attribute


code:
def generator():
    for i in range(startfrom, until+jump, jump):
        si = str(i)
        booz = sx in str(i) or i%x == 0
        bang = sy in str(i) or i%y == 0
        yield [[si, 'BANG'],['BOOZ','BOOZ-BANG']][booz][bang]

print ",".join(generator())

rawDescription: A simple prog in Python. Involves digits of numbers


code:
import asyncore, socket

class client(asyncore.dispatcher):
    def __init__(self, host):
        self.host = host
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, 22))

    def handle_connect(self):
        print 'Connected to', self.host

    def handle_close(self):
        self.close()

    def handle_write(self):
        self.send('')

    def handle_read(self):
        print ' ', self.recv(1024)

clients = []
for i in range(50, 100):
    clients.append(client('cluster%d' % i))

asyncore.loop()

rawDescription: How can I get non-blocking socket connect()'s?

description: Get non-blocking socket connect()'s


code:
text = "This is, a sample text. Some more text. $1,200 test."
print re.findall(r'(?:\d[,.]|[^,.])*(?:[,.]|$)', text)

rawDescription: python: split string after comma and dots

description: Python: split string after comma and dots


code:
import os
os.umask(002)

rawDescription: Cron Job File Creation - Created File Permissions


rawDescription: Python: Combine several nested lists into a dictionary


code:
def generate_session_from_onetime_code(fb, code):
    fb.auth_token = code
    return fb.auth.getSession()
print generate_session_from_onetime_code(fb, session_onetime_code)

rawDescription: Facebook API and Python

description: Facebook API and Python


code:
import httplib, urllib

headers = {'X-API-TOKEN': 'your_token_here'}
payload = "'title'='value1'&'name'='value2'"

conn = httplib.HTTPConnection("heise.de")
conn.request("POST", "", payload, headers)
response = conn.getresponse()

print response

rawDescription: how to make post request in python

description: Make post request in python


code:
import numpy as np

class TestClass(object):
    def interpolate_to_distance(self, distance):
        dshape = self.dist.shape
        dist = self.dist.T.reshape(-1, dshape[-1])
        data = self.data.T.reshape(-1, dshape[-1])
        intdata = np.array([np.interp(distance, di, da)
                            for di, da in zip(dist, data)])
        return intdata.reshape(dshape[0:2]).T

    def interpolate_to_distance_old(self, distance):
        interpolated_data=np.ndarray(self.dist.shape[1:])
        for j in range(interpolated_data.shape[1]):
            for i in range(interpolated_data.shape[0]):
                interpolated_data[i,j]=np.interp(
                           distance,self.dist[:,i,j],self.data[:,i,j])
        return(interpolated_data)

if __name__ == '__main__':
    testobj = TestClass()

    testobj.dist = np.random.randn(3, 3, 3)
    testobj.data = np.random.randn(3, 3, 3)

    distance = 0
    print 'Old:\n', testobj.interpolate_to_distance_old(distance)
    print 'New:\n', testobj.interpolate_to_distance(distance)

rawDescription: NumPy: 1D interpolation of a 3D array

description: NumPy: 1D interpolation of a 3D array


code:
numberOfThings = ctypes.c_int32()
indexes = ctypes.c_void_p()

_Dll.doSomeThing(ctypes.byref(numberOfThings),ctypes.byref(indexes))

ptrt = ctypes.POINTER(ctypes.c_double*numberOfThings.value)
mydblPtr = cast(indexes, ctypes.POINTER(ctypes.c_double*numberOfThings.value))

indexes = ptrt(mydblPtr.contents)

for content in indices.contents:
    print content

rawDescription: How to access pointer to pointer values in python?

description: Access pointer to pointer values in python


code: print session.query(Course).all()

rawDescription: Converting SQL commands to Python's ORM

description: Converting SQL commands to Python's ORM


code:
import re, StringIO, sys

def exec_block(block, variables):
    """Captures output of exec'd code block"""
    code = compile(block.strip(), '<string>', 'exec')
    _stdout, result = sys.stdout, StringIO.StringIO()
    sys.stdout = sys.__stdout__ = result
    exec(code, variables)
    sys.stdout = sys.__stdout__ = _stdout
    return result.getvalue()

def format_template(template):
    """Replaces code blocks with {0} for string formating later"""
    def sub_blocks(matchobj):
        """re.sub function, adds match to blocks and replaces with {0}"""
        blocks.append(matchobj.group(0)[2:-2].strip())
        return '{0}'

    blocks = []
    template = re.sub(r'<:.+?:>', sub_blocks, template, flags=re.DOTALL).splitlines()
    blocks.reverse()
    return blocks, template

def render_template(blocks, template):
    """renders template, execs each code block and stores variables as we go"""
    composed, variables = [], {}
    for line in template:
        if '{0}' in line:
            replacement = exec_block(blocks.pop(), variables).strip()
            line = line.format(replacement)
        if not line.startswith('#') and '=' in line:
            k, v = [x.strip() for x in line.split('=')]
            variables[k] = v
        composed.append(line)
    return '\n'.join(composed)

if __name__ == '__main__':
    import sys
    with open(sys.argv[1]) as f:
        blocks, template = format_template(f.read())
        print rend_template(blocks, template)

rawDescription: templated configuration in python or perl?

description: Templated configuration in python or perl


code:
@tornado.web.stream_request_body
class ValueHandler(tornado.web.RequestHandler):
    def initialize(self):
        self.start = None

    def post(self):
        end = time.time()
        print self.request
        if self.start:
            print("Upload time %s" % end-self.start)
        self.write({})

    def data_received(self, data):
        if not self.start:
            self.start = time.time()

rawDescription: How can I benchmark a tornado handler within the handler?

description: Benchmark a tornado handler within the handler


code:
import string

print string.lstrip("abc", "a") # "bc"
print string.lstrip(" abc") # "abc"

rawDescription: How to read Python Documentation

description: Read Python Documentation


code:
test_string = "the quick brown fox jumps over the lazy dog"

encoded = encode_str(test_string)
packed = pack(encoded)
unpacked = unpack(packed)
decoded = decode_str(unpacked)

print "Test str (len: %d): %r" % (len(test_string), test_string)
print "Encoded (len: %d):  %r" % (len(encoded), encoded)
print "Packed (len: %d):   %r" % (len(packed), packed)
print "Unpacked (len: %d): %r" % (len(unpacked),unpacked)
print "Decoded (len: %d):  %r" % (len(decoded), decoded)

rawDescription: How to encode English plain-text (consisting only of letters a-z and whitespace) using a 5-bit character encoding in Python?

description: Encode English plain-text (consisting only of letters a-z and whitespace) using a 5-bit character encoding in Python


code:
myvariable = 'whatever'
another = 'something else'

print f('{myvariable} {another}')

rawDescription: auto-name keyword arguments after variable names

description: Auto-name keyword arguments after variable names


code:
for movie in movies:
    print type(movie)
    print movie['tagline']

# <class 'py2neo.neo4j.Node'>
# Welcome to the Real World

rawDescription: py2neo (1.6) how to get an existing node by node property without CypherQuery

description: Py2neo (1.6) how to get an existing node by node property without CypherQuery


code:
def writeToFile(haikuList):
    with open("results.txt", "w") as fp:
        fp.write('{}\n'.format('\n\n'.join(['\n'.join(haiku) for haiku in haikuList])))
        print "There are {} lines in your file.".format(len(haikuList)*3 + len(haikuList)-1)

rawDescription: Writing to a File, Adding an Additional Space


code:
def main():
    key = ''
    infile = open('shoppinglist.txt', 'r')

    count = infile.readline()
    groceries = {}

    print('This program keeps a running total of your shopping list.')
    print('Use \'EXIT\' to exit.')

    grocery = raw_input('Enter an item: ')

    for line in infile:
        line = line.strip()     #take out newlines codes.
        if key == '':
            key = line
        else:
            groceries[key] = line     #maybe use here float(line) instead
            key = ''

    print 'Your current total is %s $' % groceries[grocery]

main()

rawDescription: Reading a .txt into a definition

description: Reading a .txt into a definition


code:
import urllib2, csv

def fetchsource(url):
    page = urllib2.urlopen(url)
    source = page.read()
    return source

fooReader = csv.reader(open('your_file.csv'))
for url in fooReader:
    print fetchsource(url)

rawDescription: How to apply Loop to working Python Selenium Script?

description: Apply Loop to working Python Selenium Script


rawDescription: How to Aggregate records in Python?

description: Aggregate records in Python


code:
m = max(sortDict.values())
for val in sortDict.values():
    if len(val) == m:
        print val

rawDescription: Find all values with maximum length in a dictionary


code:
answer = True

def play():
    # your code

    if playerChoice == computerChoice:
        print "It's a draw, want to try again?, please type YES or NO: "
        # use a dictionary to convert; default answer is False
        answer = {'YES' : True, 'NO' : False}.get(input(), False)

if answer:
    play()

rawDescription: Rock, Paper, Scissors - How to start a new game when there's a draw


code:
def main():
    dog = Animal()
    dog.set_owner('Sue')
    print dog.get_owner()
    dog.noise()

if  __name__ =='__main__':main()

rawDescription: main method in Python

description: Main method in Python


code:
x = raw_input("Enter word: ")
with open('dictionary.txt') as file:
    data = file.read()

x_pos = data.find(x)
meaning= None
if x_pos == 0 or (x_pos != -1 and data[x_pos - 1] == '\n' and data[x_pos - 2] == '\n'):
    i = x_pos
    while i < len(data):
        if data[i] == '\n' and i > 0 and data[i - 1] == '\n':
            break
        meaning += data[i]
        i += 1

print meaning if meaning else "Not found"

rawDescription: Reading a text file which contains an English dictionary to print the matching word meaning in python


code:
FactorList=[2,5,5]
print "50=" + str(FactorList[0]) + "*" + str(FactorList[1]) + "*" + str(FactorList[2])

rawDescription: How can I convert a list into the following format?

description: Convert a list into the following format


code: print repr('\xb9\xfe')

rawDescription: How can I print the raw unicode in python?

description: Print the raw unicode in python


code:
a = 257

def f():
    b = 257
    print a is b

f()

rawDescription: Python3 multiple assignment and memory address


code:
"""
DOC STRING!!
"""

def hello():
    'doc string'
    print 'hello'

hello()

rawDescription: Parsing Python Module Docstrings

description: Parsing Python Module Docstrings


code:
import pygame
import random

# Player
class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, gravity):

        # Player dimensions and position

        self.gravity = gravity

        # Player image and animation
        self.images = []
        self.images.append(pygame.image.load('images/soldier.png'))
        self.images.append(pygame.image.load('images/soldier2.png'))
        #~ self.images.append(pygame.image.load('ball1.png'))
        #~ self.images.append(pygame.image.load('ball2.png'))
        self.maxImage = len(self.images)
        self.currentImage = 0

        #~ self.rect = pygame.Rect(x, y, 80, 80)
        self.rect = self.images[0].get_rect()
        self.rect.x = x
        self.rect.y = y

        self.timeTarget = 10
        self.timeNum = 0

        self.velX = 0
        self.velY = 0

        # Jump and gravity
        self.vSpeed = 1
        self.jumpForce = 8
        self.maxVspeed = 3
        self.isJumping = False

    # Jump inputs
    def handle_events(self, event):

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_SPACE:
                if not self.isJumping:
                    self.isJumping = True

            elif event.key == pygame.K_a:
                self.velX = -5

            elif event.key == pygame.K_d:
                self.velX = +5

        elif event.type == pygame.KEYUP:
            if event.key in (pygame.K_a, pygame.K_d):
                self.velX = 0

        print "isJumping:", self.isJumping

    # PLayer updates
    def update(self, ground):

        # Jumping

        self.vSpeed -= self.gravity

        if self.vSpeed > self.maxVspeed:
            self.vSpeed = self.maxVspeed

        self.rect.y -= self.vSpeed

        if self.isJumping:
            self.vSpeed -= self.jumpForce

        if self.rect.bottom >= ground.y:
            self.vSpeed = 0
            self.rect.bottom = ground.y
            self.isJumping = False

        # Animations

        if self.timeNum == self.timeTarget:
            self.currentImage += 1
            if self.currentImage >= self.maxImage:
                self.currentImage = 0
            self.timeNum = 0

        self.rect.centerx += self.velX
        self.rect.centery += self.velY

        # Screen wrap
        if self.rect.right > 1280:
            self.rect.left = 0

        elif self.rect.left < 0:
            self.rect.right = 1280

    # Player rendering
    def render(self, surface):
        surface.blit(self.images[self.currentImage], self.rect)

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

class Zombie():

    def __init__(self, x, y):

        self.image = pygame.image.load('images/zombie.png')
        #~ self.image = pygame.image.load('ball2.png')
        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = y

        self.direction_left = True

    def update(self, surface_rect):
        if self.direction_left:
            self.rect.x -= 1
            if self.rect.left <= surface_rect.left:
                self.direction_left = not self.direction_left
        else:
            self.rect.x += 1
            if self.rect.right >= surface_rect.right:
                self.direction_left = not self.direction_left

    def render(self, surface):
        surface.blit(self.image, self.rect)

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

class Background():

    def __init__(self):

        self.image = pygame.image.load('images/arena2.jpg')
        #~ self.image = pygame.image.load('background.jpg')
        self.rect = self.image.get_rect()

    def render(self, surface):
        surface.blit(self.image, self.rect)

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

class Game():

    def __init__(self):
        pygame.init()

        # A few variables
        self.gravity = .50
        self.ground = pygame.Rect(0, 720, 0, 0)

        # Screen
        size = (1280, 720)
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption('Moon Survival!')

        # Moon / Background
        self.moon = Background()

        # Zombies
        self.zombies = []
        for i in range(10):
            self.zombies.append( Zombie(random.randint(0,1280), random.randint(0,720)) )

        # Player
        self.player = Player(25, 320, self.gravity)

        # Font for text
        self.font = pygame.font.SysFont(None, 72)

        # Pause - center on screen
        self.pause_text = self.font.render("PAUSE", -1, (255,0,0))
        self.pause_rect = self.pause_text.get_rect(center = self.screen.get_rect().center)

    def run(self):

        clock = pygame.time.Clock()

        # "state machine"
        RUNNING   = True
        PAUSED    = False
        GAME_OVER = False

        # Game loop
        while RUNNING:

            # (all) Events

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = False

                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

                    elif event.key == pygame.K_p:
                        PAUSED = not PAUSED

                # Player/Zomies events

                if not PAUSED and not GAME_OVER:
                    self.player.handle_events(event)

            # (all) Movements / Updates

            if not PAUSED and not GAME_OVER:
                self.player.update(self.ground)
                for z in self.zombies:
                    z.update(self.screen.get_rect())

            # (all) Display updating

            self.moon.render(self.screen)

            for z in self.zombies:
                z.render(self.screen)

            self.player.render(self.screen)

            if PAUSED:
                self.screen.blit(self.pause_text, self.pause_rect)

            pygame.display.update()

            # FTP

            clock.tick(75)

        # --- the end ---
        pygame.quit()

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

Game().run()

rawDescription: Pygame - AttributeError: 'int' object has no attribute 'y'