code:
import re

str = "Created by [[User:One|One]] ([[User talk:One|talk]]), [[User:Two|Two]] ([[User talk:Two|talk]]). Nominated by [[User:Three|Three]] ([[User talk:Three|talk]]) at 23:47, 25 January 2014 (UTC), How could I get a regex like (\[\[User:(.*?)\|.*?\]\]\s?)"

matcher = re.compile("(\[\[User:(.*?)\|.*?\]\]\s?)")
print matcher.findall(str)

rawDescription: Get regex to match a group any amount of times


code:
def gcs(*instances):
    classes = [type(x).mro() for x in instances]
    for x in classes[0]:
        if all(x in mro for mro in classes):
            return x

print gcs(b, e)
print gcs(d, e)
print gcs(e, e)

rawDescription: Greatest Common Superclass

description: Greatest Common Superclass


code:
if lineA == lineOne:
    print 'they match!'

rawDescription: Python - most efficient way of matching a list


code:
'''
Created on Feb 29, 2016

@author: begueradj
'''
import Tkinter
import MySQLdb
from Tkinter import IntVar

class Begueradj(Tkinter.Frame):
    '''
    classdocs
    '''

    def __init__(self, parent):
        '''
        Constructor
        '''
        Tkinter.Frame.__init__(self, parent)
        self.parent=parent
        self.initialize_user_interface()

    def initialize_user_interface(self):
        """Draw a user interface allowing the user to type
        MySQL server credentials
        """
        self.parent.title("DB operations")
        self.parent.grid_rowconfigure(0,weight=1)
        self.parent.grid_columnconfigure(0,weight=1)
        self.parent.config(background="lavender")

        self.label_user=Tkinter.Label(self.parent,text="DB User: ",anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8  bold")
        self.label_password=Tkinter.Label(self.parent,text="DB Password:", anchor=Tkinter.W,background="dark slate gray",foreground="white", font="Helvetica 8  bold")

        self.label_user.grid(row=0,column=0,sticky=Tkinter.E+Tkinter.W)
        self.label_password.grid(row=1,column=0, sticky=Tkinter.E+Tkinter.W)

        self.dbuser=Tkinter.Entry(self.parent)
        self.dbpassword=Tkinter.Entry(self.parent,show="*")

        self.dbuser.grid(row=0,column=1,sticky=Tkinter.E+Tkinter.W)
        self.dbpassword.grid(row=1,column=1,sticky=Tkinter.E+Tkinter.W)

        self.connectb=Tkinter.Button(self.parent,text="Log in",font="Helvetica 10 bold",command=self.dbconnexion)
        self.cancelb=Tkinter.Button(self.parent,text="Cancel",command=self.parent.quit,font="Helvetica 10 bold")

        self.connectb.grid(row=2,column=1,sticky=Tkinter.W)
        self.cancelb.grid(row=2,column=2)

    def item_insertion_window(self):
        self.new_window=Tkinter.Toplevel(self)
        self.new_window.wm_title("Add my favorite stars")
        self.new_window.grid_rowconfigure(0, weight=1)
        self.new_window.grid_columnconfigure(0, weight=1)

        self.exitb=Tkinter.Button(self.new_window,text="Exit",command=self.new_window.quit)
        self.submitb=Tkinter.Button(self.new_window,text="Submit",command=self.increment_db)
        self.exitb.grid(row=8,column=1)
        self.submitb.grid(row=8,column=0,sticky=Tkinter.W)

        self.v=IntVar()
        self.tvstars=[('YOWERI KAGUTA MUSEVENI', 1), ('KIIZA BESIGYE', 2),
                      ('AMAAMA JOHN MBABAZI ', 3), ('KARUNGI SHARON', 4),
                      ('BYAMUKAMA OSCAR', 5), ('MATILDA MOREEN', 6),
                      ('DUNCANS', 7)]
        self.i=0
        for self.txt, star in self.tvstars:
            self.i=self.i+1
            self.rb=Tkinter.Radiobutton(self.new_window,text=self.txt,variable=self.v,value=star)
            self.rb.grid(row=self.i,column=0,sticky=Tkinter.W)

    def which_artist(self,radiob):
        self.artists = {
                        1:"YOWERI KAGUTA MUSEVENI",
                        2:"KIIZA BESIGYE",
                        3:"AMAAMA JOHN MBABAZI",
                        4:"KARUNGI SHARON",
                        5:"BYAMUKAMA OSCAR",
                        6:"MATILDA MOREEN",
                        7:"DUNCANS",
        }
        return self.artists.get(radiob,"Unknown")

    def increment_db(self):
        #print self.v.get()
        self.chosenartist=self.which_artist(self.v.get())
        print self.chosenartist

        self.config = {
                  'user': 'begueradj',
                  'passwd': 'begueradj',
                  'host': '127.0.0.1',
                  'db': 'begueradj',
        }
        try:
            self.connecttodb=MySQLdb.connect(**self.config)
        except MySQLdb.Error:
            print"Connexion error"

        self.cursor=self.connecttodb.cursor()

        self.cursor.execute("""INSERT INTO mystars(starname) VALUES(%s)""",self.chosenartist)

        self.connecttodb.commit()
        self.connecttodb.close()

    def dbconnexion(self):
        if self.dbuser.get()=="begueradj" and  self.dbpassword.get()=="begueradj":
            self.item_insertion_window()
        else:
            self.initialize_user_interface()

def main():
    root=Tkinter.Tk()
    d=Begueradj(root)
    root.mainloop()

if __name__=="__main__":
    main()

rawDescription: how to connect to mysql database from gui made in python

description: Connect to mysql database from gui made in python


code:
import math
import random
n=5
m=100
def dist(a,b):
    return math.sqrt( (a[0]-b[0])**2 + (a[1]-b[1])**2 )
points = [ [random.randint(0,m), random.randint(0,m) ] for i in range(n) ]
D = [ [ dist( points[i], points[j] ) for i in range(n) ] for j in range(n) ]
print points
print D

rawDescription: Generating a map/graph for a traveling salesmanproblem (Python)


code:
for row in arr:
    if len(set(row)) != len(row):
        print 'Duplicates in row: ', row

rawDescription: Finding duplicates in each row and column


code:
results = [len(str(i)) for i in xrange(1000000)]
print len(results)

rawDescription: the different efficiency for number digits counting with python & C

description: The different efficiency for number digits counting with python & C


code:
vars(o).setdefault('some_attr', None)
print o.some_attr

rawDescription: Ways to set default value for class attribute without using hasattr


code:
print info.group(1) # or info.groups()[0]

print '"%s","%s","%s"' % (info.group(1), info.group(2), info.group(3))
"Michael Schenker Group","House of Blues Dallas","3/26"

rawDescription: Python parsing

description: Python parsing


rawDescription: Displaying unicode characters using python


code:
from bson.tz_util import utc
cursor = db.collection.find({'$or': [
    {'col1': 1, 'col2': 'foo'},
    {'col3': {'$gt': datetime(2012, 01, 01, tzinfo=utc)}},
    {'col3': {'$lt': datetime(2012, 01, 02, tzinfo=utc)}},
]}, fields=['col1', 'col2'])

rawDescription: Build mongoDB queries based on JSON from a user using Python


code:
a = MonFrozenSet([1, 2, 3])
for item in a:
    print item

rawDescription: Python: Subclassing frozenset not iterable?


code:
data = "Chateau d'Arsac, Bordeaux blanc, Cuvee Celine, 2012"
import re
print re.match(".*?,.*?,\s*(.*?),.*", data).group(1)

rawDescription: Extract words between the 2nd and the 3rd comma


code:
import gio

def directory_changed(monitor, file1, file2, evt_type):
    print "Changed:", file1, file2, evt_type

gfile = gio.File(".")
monitor = gfile.monitor_directory(gio.FILE_MONITOR_NONE, None)
monitor.connect("changed", directory_changed)

import glib
ml = glib.MainLoop()
ml.run()

rawDescription: Issues with time.sleep and Multithreading in Python


code:
global totalBottles, totalPayout, todayBottles
totalBottles=0 #store the accumulated bottle values
counter=1 #will control the loop
todayBottles=0 #store the number of bottles returned on a day
totalPayout=0 #store the calculated value of totalBottles x.10

def main():

    keepGoing='y'
    while keepGoing =='y':
        totalBottles = getBottles(totalBottles, todayBottles)
        totalPayout = calcPayout (totalBottles)
        printInfo(totalBottles, totalPayout)
        keepGoing == raw_input ('Do you want to run the program again?')

def getBottles (totalBottles, todayBottles):
    counter=1
    while counter <8:
        todayBottles = input ('Enter number of bottles returned for the day:')
        totalBottles = todayBottles + totalBottles
        counter=counter + 1
    return totalBottles

def calcPayout(totalBottles):
    totalPayout = totalBottles * .10
    return totalPayout

def printInfo(totalBottles, totalPayout):
    print totalBottles,('is the total bottles')
    print totalPayout, ('amount due')

main()

rawDescription: Python dumps variable from loop

description: Python dumps variable from loop


code:
# Your code to initialise everything:
import RPi.GPIO as GPIO
import time
import os
os.system('clear')

GPIO.setmode(GPIO.BOARD)

GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(32, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# New loop keeping track of the previous ("old") input states:
old_input_states = [None] * 3
while True:
    input_states = [GPIO.input(n) for n in (37, 32, 29)]
    if input_states != old_input_states:
        for n, name in enumerate(['One', 'Two', 'Three']):
            if input_states[n] == False:
                print name
        old_input_states = input_states

rawDescription: Python Don't Repeat Consecutive Lines

description: Python Don't Repeat Consecutive Lines


code:
dictionary = {'key1': ['value1', 'value2', 'value3'], 'key2': ['value1', 'value2']}

for key, values in dictionary.items():
   for value in values:
       print '{} {}'.format(key, value)

rawDescription: Ordered dictionary output in python

description: Ordered dictionary output in python


code:
print bool(0)
# False

rawDescription: 0 being passed as an optional parameter to a class' method, param not found


code:
with open("numbers.txt") as f:
   nums = [int(line) for line in f]
   nums.sort()
   print nums

rawDescription: Open File, then read file and make them no line


code:
from z3 import *

def one_op (op, arg1, arg2):
    return  If(op==1,
               arg1*arg2,
               If(op==2,
                  arg1-arg2,
                  If(op==3,
                     arg1+arg2,
                     arg1+arg2)))

s=Solver()

arg1, arg2, result, unk_op=Ints ('arg1 arg2 result unk_op')

s.add (unk_op>=1, unk_op<=3)
s.add (arg1==1)
s.add (arg2==2)
s.add (result==3)
s.add (one_op(unk_op, arg1, arg2)==result)

print s.check()
print s.model()

rawDescription: (Z3Py) any limitations in functions declaring?


code:
queue_id = notify.get()
if queue_id == 1:
  i1 = c1.get()
  print "received %s from c1" % i1
elif queue_id == 2:
  i2 = c2.get()
  print "received %s from c2" % i2

rawDescription: Multiplex on queue.Queue?

description: Multiplex on queue.Queue


code:
def foo(arg1, arg2):
    print arg1, arg2
arguments = ('spam', 'eggs')
foo(*arguments)

rawDescription: what is the asterisk in "replace(*something)" for? (python)

description: What is the asterisk in "replace(*something)" for? (python)


code:
def display_hex(pkt, cols=8):
    size = cols * 4
    data = ''.join('%02x' % ord(b) for b in pkt.get_data_as_string())
    for i in range(0, len(data), size):
        for j in range(0, size, 4):
            print data[i+j:i+j+4],
        print

if isinstance(child, TCP):
    display_hex(child)

rawDescription: Getting TCP packet payload from Python and impacket


code:
from time import sleep
from threading import Thread
from multiprocessing import Process, JoinableQueue

class TaskExecutor(Thread):
    def __init__(self):
        print("init taskExecutor")
        Thread.__init__(self)
        self.taskInfos = JoinableQueue()

    def getq(self):
        return self.taskInfos

    def run(self):
        print("start running taskExcecutor worker Thread")
        while self.is_running():
            try:
                print("try to get queued task from %s" % self.taskInfos)
                msg, task = self.taskInfos.get()
                print("got task %s for msg: %s" % (task, msg))
                task.execute(msg)
                self.taskInfos.task_done()
            except Exception, e:
                print("Error: %s" % e.message)
        print("shutting down TaskExecutor!")

    def is_running(self):
        return True

class Task(object):
    def execute(self, msg):
        print(msg)

def produce(q):
    cnt = 0
    while True:
        q.put(("Message " + str(cnt), Task()))
        cnt += 1
        sleep(1)

if __name__ == "__main__":
    executor = TaskExecutor()
    executor.start()
    for i in range(4):
        p = Process(target=produce, args=(executor.getq(),))
        p.start()

rawDescription: deadlock because of blocking Queue.get() method

description: Deadlock because of blocking Queue.get() method


code:
#decrypt.py

import sys

if not len(sys.argv) > 1: exit()

with open(sys.argv[1],"r+") as f:
    content = f.read()

content = content.replace(':', '')
msg, key = content[:len(content)/2], content[len(content)/2:]
msg = msg.decode('hex')
key = key.decode('hex')
out = ""

k = 0

for char in msg:
    out += chr(ord(char) ^ ord(key[k]))
    k += 1

print out
raw_input()

rawDescription: XOR cryptographic algorithm not working for > 54 chars


rawDescription: Python sort list of list containing integer and string with integers inside


code:
pattern = ur'([A-ZÄÖÜ][^., \t\n]*)'
result = " ".join(re.findall(pattern, text))

rawDescription: Remove lowercase words from python unicode string


code:
import urllib
import urlparse
from bs4 import BeautifulSoup

for i in xrange(3):
    url = "http://www.downloadcrew.com/?act=search&page=%d&cat=51" % i
    pageHtml = urllib.urlopen(url)
    soup = BeautifulSoup(pageHtml)

    for a in soup.select("div.productListingTitle a[href]"):
        try:
            print (a["href"]).encode("utf-8","replace")
        except:
            print "no link"

rawDescription: How can I get all the software link?

description: Get all the software link


code:
def calc(base, alist, blist):
    for num in range(1, 4):
        result = []
        for b in base:
            result.append(b + sum(alist[:num]) - sum(blist[:num]))
        print result

rawDescription: How can I solve this without list comprehensions

description: Solve this without list comprehensions


code:
import subprocess
sp = subprocess.Popen(["java", "-version"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print sp.communicate()
print sp.wait()

rawDescription: How to get 'java -version' output through python

description: Get 'java -version' output through python


code:
import geopy
import geopy.distance

# Define starting point.
start = geopy.Point(48.853, 2.349)

# Define a general distance object, initialized with a distance of 1 km.
d = geopy.distance.VincentyDistance(kilometers = 1)

# Use the `destination` method with a bearing of 0 degrees (which is north)
# in order to go from point `start` 1 km to north.
print d.destination(point=start, bearing=0)

rawDescription: Calculate point based on distance and direction


code:
def replace(match):
    d = {'1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', 'N': 'North', 'E': 'East', 'S': 'South', 'W': 'West'}
    source = match.group(1).strip()
    return d.get(source, source) + ' '

text = '1km N of Piedmont, California'
print re.sub(r'(\d+ *(?=km)|[NESW]\s+(?=of))', replace, text)

rawDescription: Specific String replacement

description: Specific String replacement


code:
c = None

def fib(n):
    global c

    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
        c = result
    return result

f100 = fib(100)
print result
print c

rawDescription: How to access a variable defined inside a function from out side?

description: Access a variable defined inside a function from out side


code:
import os

cmd = r'''echo its a small world after all \
        its a small world after all'''

def my_action(source, target, env):
    print cmd
    return os.system(cmd)

my_cmd_builder = Builder(
    action=my_action,
    suffix = '.foo')

env = Environment()
env['_MY_CMD'] = cmd  # <-- CREATE IMPLICIT DEPENDENCY ON CMD STRING
env.Append( BUILDERS = {'MyCmd' : my_cmd_builder } )

my_cmd = env.MyCmd('foo.foo',os.popen('which bash').read().strip())

rawDescription: How can I generate multi-line build commands?

description: Generate multi-line build commands


code:
class myHeap(object):
    pass

myHeap.array = [2, 2, 2, 1]

for n, head in enumerate(myHeap.array):
    if head != None:
        myHeap.array[n] = head + 1

print myHeap.array

rawDescription: Python variable scope in for loop

description: Python variable scope in for loop


code:
import errno
import functools
import socket
from tornado import ioloop, iostream

class Communicator(object):
    def connection_ready(self, sock, fd, events):
        while True:
            try:
                connection, address = sock.accept()
            except socket.error, e:
                if e[0] not in (errno.EWOULDBLOCK, errno.EAGAIN):
                    raise
                return
            connection.setblocking(0)
            self.stream = iostream.IOStream(connection)
            self.stream.read_until(delimiter='\n', callback=self.initial_state)

    def initial_state(self, msg):
        msg = msg.rstrip()
        print "entering initial state with message: %s" % msg
        transitions = {
            'a' : functools.partial(ioloop.IOLoop.instance().add_callback, self.state_a, msg),
            'b' : functools.partial(ioloop.IOLoop.instance().add_callback, self.state_b, msg),
            'c' : functools.partial(ioloop.IOLoop.instance().add_callback, self.final_state, msg)
        }
        try:
            transitions[msg[0]]()
        except:
            self.stream.write("Aborted (wrong input)\n", self.stream.close)

    def state_a(self, msg):
        print "entering state a with message: %s" % msg
        transitions = {
            'a' : functools.partial(ioloop.IOLoop.instance().add_callback, self.stream.write, "got a\n", functools.partial(self.state_a, msg[1:])),
            'b' : functools.partial(ioloop.IOLoop.instance().add_callback, self.state_b, msg),
            'c' : functools.partial(ioloop.IOLoop.instance().add_callback, self.final_state, msg[1:])
        }
        try:
            transitions[msg[0]]()
        except:
            self.stream.write("Aborted (wrong input)\n", self.stream.close)

    def state_b(self, msg):
        print "entering state b with message: %s" % msg
        transitions = {
            'a' : functools.partial(ioloop.IOLoop.instance().add_callback, self.state_a, msg),
            'b' : functools.partial(ioloop.IOLoop.instance().add_callback, self.stream.write, "got b\n", functools.partial(self.state_a, msg[1:])),
            'c' : functools.partial(ioloop.IOLoop.instance().add_callback, self.final_state, msg[1:])}
        try:
            transitions[msg[0]]()
        except:
            self.stream.write("Aborted (wrong input)\n" , self.stream.close)

    def final_state(self, msg):
        print "entering final state with message: %s" % msg
        self.stream.write("Finished properly with message %s\n" % msg, self.stream.close)

if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setblocking(0)
    sock.bind(("", 8000))
    sock.listen(5000)

    communicator = Communicator()
    io_loop = ioloop.IOLoop.instance()
    callback = functools.partial(communicator.connection_ready, sock)
    io_loop.add_handler(sock.fileno(), callback, io_loop.READ)
    try:
        io_loop.start()
    except KeyboardInterrupt:
        io_loop.stop()
        print "exited cleanly"

rawDescription: Coroutine based state machines

description: Coroutine based state machines


code:
# example.py
from multiprocessing import Process

class Example(object):
    def __init__(self, queue):
        """
        @type queue: multiprocessing.Queue
        """
        self.q = queue

    def run(self, a, m=None):
        p = Process(target=self.f, args=(a, m))
        p.start()
        print self.q.get()
        p.join()

    def f(self, a, m='No'):
        print m
        self.q.put(a)

rawDescription: Multiprocessing using imported modules

description: Multiprocessing using imported modules


code:
import csv

def search(search_column, search_value):

    with open('file.txt', 'rb') as f:
        header = []
        reader = csv.reader(f, delimiter='\t', quotechar='"')
        for row in reader:
            if not row:
                continue
            if not header:
                header = row
                continue
            row = dict(zip(header, row))
            if row.get(search_column) == search_value:
                return row.get('Product ID')
        return None

print search('Let\'s eat pizza', '1')

rawDescription: Query a column for row values in HDFS

description: Query a column for row values in HDFS


code:
#  Chunking function
def chunks(l, n):
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

# Resampling function
def resample(arr, newLength):
    chunkSize = len(arr)/newLength
    return [np.mean(chunk) for chunk in chunks(arr, chunkSize)]

# Example:
import numpy as np
x = np.linspace(-1,1,15)
y = resample(x, 5)
print y
# Result:
# [-0.85714285714285721, -0.4285714285714286, -3.7007434154171883e-17, 0.42857142857142844, 0.8571428571428571]

rawDescription: Compress an array in python?

description: Compress an array in python


code:
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('status', nargs='?')
args = parser.parse_args()
print args.status

rawDescription: Python argparse without character -

description: Python argparse without character -


code:
import sys

if __name__ == '__main__':
    try:
        arg_command = sys.argv[1]
    except IndexError:
        arg_command = ""

    Done = False
    while not Done:
        if arg_command=="":
            print('\nMenu')
            print('C Clear All')
            print('L Load Encrypted File')
            print('Q Quit')
            print('----------------')
            print('Enter Choice>')
            command = raw_input('Enter Selection> ').strip()[0].upper()
        else:
            command = arg_command
            #- set arg value to empty to run Menu option again.
            arg_command = ""

        if command == 'C':
            print "In Clear All event."
        elif command == 'L':
            print "In Clear All event."
        elif command == "Q":
            break
        else:
            print "Wrong Selection."

rawDescription: How to use sys.argv in python to check length of arguments so it can run as script?

description: Use sys.argv in python to check length of arguments so it can run as script


code:
def shortest_of(lists):
    return min(lists, key=len)

def contains_sublist(lst, sublst):
    n = len(sublst)
    return any((sublst == lst[i:i+n]) for i in xrange(len(lst)-n+1))

def longest_common(lists):
    if not lists:
        return ()
    res = set()
    base = shortest_of(lists)
    length = len(base)

    for i in xrange(length, 0, -1):
        for j in xrange(length - i + 1):
            candidate = ', ' + str(base[j:i+j]).strip('[]') + ','
            #candidate = base[j:i+j]

            for alist in lists:
                if not candidate in ', ' + str(alist).strip('[]') + ',':
                #if not contains_sublist(alist, candidate):
                    break
            else:
                res.add(tuple([int(a) for a in candidate[2:-1].split(',')]))
                #res.add(tuple(candidate))

        if res:
            return tuple(res)

    return ()

if __name__ == '__main__':
    a = [1,0,2,5,4,3,1]
    b = [1,2,5,4,3,0,1]
    c = [1,3,5,4,2,0,1]

    print longest_common([a,b,c])
    print longest_common([b,c])

rawDescription: how to intersect lists in python with respect to index

description: Intersect lists in python with respect to index


code:
keyList = ["pass1_v1", "pass1_v3", "pass1_v5", "pass2_v1", "pass2_v3", "pass3_v4"]
by_prefix = {}
for x in keyList:
    by_prefix.setdefault(x.rsplit('_', 1)[0], []).append(x)

for prefix, matches in by_prefix.iteritems():
    print 'Matches for {} are {}'.format(prefix, by_prefix[prefix])

rawDescription: check for multiple substring match in a list while iterating list

description: Check for multiple substring match in a list while iterating list


code:
from subprocess import check_output
output = check_output('netsh wlan connect _Wifi_name_', shell=True)

print output

rawDescription: Connection to wi-fi using python

description: Connection to wi-fi using python


code:
df = df.stack(0).reset_index()

df.index += 1    # to make index begin from 1

print df

#    level_0 level_1  x  y  z
# 1        1       A  5  7  4
# 2        1       B  7  7  8
# 3        1       C  9  1  9
# 4        2       A  8  5  1
# 5        2       B  8  5  9
# 6        2       C  4  4  2
# 7        3       A  4  9  6
# 8        3       B  2  1  4
# 9        3       C  6  1  6

rawDescription: How to Pandas read_csv multiple records per line


code:
if Math_symbol in ("*", "Multiply"):
    print Number_input_one * Number_input_two
elif Math_symbol in ("/", "Divide"):
    print Number_input_one / Number_input_two
elif Math_symbol in ("+", "Add"):
     print Number_input_one + Number_input_two
elif Math_symbol in ("-", "subtract"):
    print Number_input_one - Number_input_two
else:
    print "it doesn't match anything!"

rawDescription: If statement keeps returning true

description: If statement keeps returning true


code:
for key_1, key_2, value in ordered:
    print key_1, key2, value         # add whatever formatting you want to here

rawDescription: Sort a dictionary of dictionaries python


code:
from bs4 import BeautifulSoup as BS
text = 'Hello, This is the string i want to extract'
soup = BS(text)
print soup.code.text

rawDescription: How to extract string between selected string in python

description: Extract string between selected string in python


code:
class NonDelegatableItem(AttributeError):
    pass

class X:
    def __getattr__(self, method_name):
        self.check_method_name_is_delegator(method_name)
        return self.create_delegator(method_name)

    def check_method_name_is_delegator(self, method_name):
        if method_name not in self._allowed_items:
            raise NonDelegatableItem('{} can not be delegated'.format(method_name))

    @classmethod
    def create_delegator(cls, method_name):
        print 'new delegator'
        def delegator(self, *args, **kw):
            self.check_method_name_is_delegator(method_name)
            for instance in self.all_instances:
                getattr(instance, method_name)(*args, **kw)
        setattr(cls, method_name, delegator)
        return delegator

x = X()

x._allowed_items = ['a', 'b']

rawDescription: Method delegation in python

description: Method delegation in python


code:
list=['a','a','x','c','e','e','f','f','f']

i=0
count = 0

while count < len(list)-2:
    if list[i] == list[i+1]:
        if list [i+1] != list [i+2]:
            print list[i]
            i+=1
            count +=1
        else:
            print "no"
            count += 1
    else:
        i +=1
        count += 1

rawDescription: Conditional loop in python

description: Conditional loop in python


code:
a = "    indented string"
leading_spaces = len(a) - len(a.lstrip())
print leading_spaces

rawDescription: Python check string indentation

description: Python check string indentation


code:
def insert_break(s,n, br):
    while s:
        yield "{}{}".format(s[:n],br)
        s = s[n:]
with open('output.txt', 'r') as f,open('updated.txt', 'a') as f1:
    inserted= "".join((insert_break(f.read(),181,"\n")))
    f1.write(inserted)

with open('updated.txt', 'r') as infile,open("ABC1.txt","a") as ab1,open("ABC2.txt","a") as ab2,\
        open("ABC3.txt","a") as ab3,open("ABC4.txt","a") as ab4:
            for line in infile:
                print line.startswith("ABC1"),line
                if line.startswith("ABC1"):
                    ab1.write(line)
                elif line.startswith("ABC2"):
                    ab2.write(line)
                elif line.startswith("ABC3"):
                    ab3.write(line)
                elif line.startswith("ABC4"):
                    ab4.write(line)

rawDescription: How do you read a text file, then split that text file into multiple text files with python?

description: Read a text file, then split that text file into multiple text files with python


rawDescription: Decoding JSON to give me a certain variable in Python?


code:
class Foo(object):
    def __init__(self):
        pass

import new

def some_func(self):
    print "foobar"

Foo.my_method = new.instancemethod(some_func, None, Foo)

rawDescription: Rewrite a python script, injecting a method in each of the script's classes


code:
conf = Configure(env)
status, _ = conf.TryAction("fltk-config --cflags")
if status:
  env.ParseConfig("fltk-config --cflags")
else:
  print "Failed fltk"

rawDescription: Why does output of fltk-config truncate arguments to gcc?


code:
# Import our executor
import executors
# Import zeroMQ libraries
import zmq
from zmq.eventloop import ioloop, zmqstream
# Import db functions to process the message
import db

# zeroMQ.py deals with the communication between a zero message queue

def startListenToMessageQueue(subscribe_ports, subscribe_IP, subscribe_topic):
    # Usage:
    #       This function starts the subscriber for our application that will listen to the
    #       address and ports specified in the zeroMQ_settings.py, it will spawn a callback when we
    #       received anything relevant to our topic.
    # Arguments:
    #       None
    # Return:
    #       None

    # Get zmq context
    context = zmq.Context()

    # Get the context socket
    socket_sub = context.socket(zmq.SUB)

    # Connect to multiple subscriber ports
    for ports in subscribe_ports:
        socket_sub.connect("tcp://"+str(subscribe_IP)+":"+str(ports))

    # Subscribe to our relevant topics
    for topic in subscribe_topic:
        socket_sub.setsockopt(zmq.SUBSCRIBE, topic)

    # Setup ZMQ Stream with our socket
    stream_sub = zmqstream.ZMQStream(socket_sub)

    # When we recieve our data, we will process the data by using a callback
    stream_sub.on_recv(processMessage)

    # Print the Information to Console
    print "Connected to publisher with IP:" + \
          str(subscribe_IP) + ", Port" + str(subscribe_ports) + ", Topic:" + str(subscribe_topic)

def processMessage(message):
    # Usage:
    #       This function processes the data using a callback format. The on_recv will call this function
    #       and populate the message variable with the data that we recieved through the message queue
    # Arguments:
    #       message: a string containing the data that we recieved from the message queue
    # Return:
    #       None

    # Process the message with an executor, and use the addData function in our db to process the message
    executors.executor.submit(db.addData, message)

rawDescription: Python Tornado - How to create background process?


code:
import MySQLdb
con = MySQLdb.connect(host='localhost', user=username, passwd=password, db=db_name)
cur = con.cursor()
cur.execute("SELECT * FROM YOUR_TABLE_NAME")

# print all the first cell of all the rows
for row in cur.fetchall():
    print row[0]

con.close()

rawDescription: How to get data from MySQL table into Python 2.7

description: Get data from MySQL table into Python 2.7


code:
import re
import numpy

# open the file?
test_string = " a lot of text read from file ... Department of Something is called (DoS) and then more texts and more text..."
regex_acronym = r'\b[A-Z][a-zA-Z\.]*[A-Z]\b\.?'

ra = re.compile(regex_acronym)
for m in ra.finditer(test_string):
    print m.start(), m.group(), m.span()
    n = len(m.group()) * 2
    regex_pre_post = r"((?:[a-zA-Z'-]+[^a-zA-Z'-]+){0,%d})(" % n
    regex_pre_post += regex_acronym
    regex_pre_post += ")((?:[^a-zA-Z'-]+[a-zA-Z'-]+){0,%d})" % n
    found= re.findall(regex_pre_post, test_string)
    print found

    found = found[0] # For a single match, just do this.
    pre = found[0]
    acro = found[1]
    post = found[2]
    print pre, acro, post

rawDescription: Python Find n words before and after a certain words


code:
courses = (
    Course("today", "", "", "", "", ""),
    Course("wednesday", "", "", "", "", ""),
    Course("today", "", "", "", "", ""),
    Course("sunday", "", "", "", "", ""),
)

results = searchCourses(courses, "on today or wednesday")
for course in results:
    print course.date

rawDescription: Are python list comprehensions always a good programming practice?


code:
a = {
    (1, 2): 3,
    (1, 0): 4,
    (1, 5): 1,
    (2, 3): 9
}

import heapq
print heapq.nlargest(2, (k for k in a if k[0] == 1), key=lambda k: a[k])
print heapq.nlargest(2, (k for k in a if k[0] == 2), key=lambda k: a[k])

rawDescription: top n keys with highest values in dictionary with tuples as keys

description: Top n keys with highest values in dictionary with tuples as keys


code:
import lxml.html
content_tag = lxml.html.fromstring('
Goodbye.
')
content_tag.text = '' # assumes only text to start
for elem in lxml.html.fragments_fromstring('Hello world!'):
    if type(elem) == str: #but, only the first?
        content_tag.text += elem
    else:
        content_tag.append(elem)
print lxml.html.tostring(content_tag)

rawDescription: Python: Injecting HTML content into a tag using `lxml.html`


code:
from multiprocessing import Process, Pipe

def f(conn):
    conn.send([42, None, 'hello'])
    conn.close()

if __name__ == '__main__':
    parent_conn, child_conn = Pipe()
    p = Process(target=f, args=(child_conn,))
    p.start()
    print parent_conn.recv()   # prints "[42, None, 'hello']"
    p.join()

rawDescription: How do you share data between a parent and forked child process in Python?

description: Share data between a parent and forked child process in Python


code:
import urllib

query_args = { 'q':'query string', 'foo':'bar' }
encoded_args = urllib.urlencode(query_args)
print 'Encoded:', encoded_args

url = 'http://localhost:8080/?' + encoded_args
print urllib.urlopen(url).read()

rawDescription: Using Urllib2 with 511 api that requires token


code:
print "{:<8} {:<15} {:<10}".format('Key','Label','Number')
for k, v in d.iteritems():
    label, num = v
    print "{:<8} {:<15} {:<10}".format(k, label, num)

rawDescription: Python - Printing a dictionary as a horizontal table with headers


code:
x = {"foo":"bar"}
a = x
x = {}
print a

rawDescription: Python : Variable in a different module doesn't get changed


code:
import re
p = re.compile(ur'/\*.*?\*/', re.DOTALL)
test_str = u"/* remove this text */\n\n/*\n * remove this text\n */\n\n/*******\n * remove this text\n *******/"
subst = u""

result = re.sub(p, subst, test_str)

rawDescription: Regex catastrophic backtracking

description: Regex catastrophic backtracking


code: print [attr for attr in dir(request.user) if callable(attr)]

rawDescription: In Django, how can I view all the properties of my user?


code:
from os.path import basename
import re
from tempfile import NamedTemporaryFile

try:
    from subprocess import check_output, CalledProcessError, STDOUT
except ImportError:  # check_output new in 2.7, so use a backport for <=2.6
    from subprocess32 import check_output, CalledProcessError, STDOUT

class OpenSSLError(Exception):
    pass

def info_extension_cert(cert):
    """
    This function take a certificate and return the extensions in dict.

    @type cert : M2Crypto.X509
    @param cert : Certificate
    """
    certificateExtensions = {}

    for index in range(cert.get_ext_count()):
        ext = cert.get_ext_at(index)
        certificateExtensions[ext.get_name()] = ext.get_value()
    return certificateExtensions

def get_cert_url_ocsp(cert):
    """
    Get the OCSP url of a certificate

    @type cert : M2Crypto.X509
    @parm cert : Certificat

    @rtype : string
    @return : The OSCP url
    """

    infos = [x.strip() for x in info_extension_cert(cert)["authorityInfoAccess"].split('\n')]
    ocsp_url = None
    for info in infos:
        if re.match(r"^OCSP - URI:", info):
            ocsp_url = info.replace("OCSP - URI:","")
            break
    return ocsp_url.strip()

def is_revoked(cert, cert_parent):
    """
    Check if the certificate has been revoked.

    @type cert : M2Crypto.X509
    @param cert : The certificate

    @type cert_parent : string
    @param cert_parent : Issuer certificate file path

    @rtype : boolean
    @return : True if revoked or False
    """
    ocsp_url = get_cert_url_ocsp(cert)
    if re.match(r"^http", ocsp_url) is None:
        return False

    data = {'cert_parent': cert_parent,
            'ocsp_url': ocsp_url,
            'serial': cert.get_serial_number()}

    cmd = "openssl ocsp -issuer %(cert_parent)s -CAfile %(cert_parent)s -url %(ocsp_url)s -serial %(serial)s" % data
    print cmd
    try:
        output = check_output(cmd, shell=True, stderr=STDOUT).lower()
    except CalledProcessError, e:
        msg = u"[OpenSSL] Error while checking ocsp %s: %s. Output: %r" % (
                    cmd, e, e.output)
        raise OpenSSLError(msg)
    return not ('response verify ok' in output and '%s: good' % data['serial'] in output)

def is_revoked_crl(cert, cert_parent_with_crl):
    """
    Check if the certificate as been revoked with the crl.

    @type cert : M2Crypto.X509
    @param cert : The certificate

    @type cert_parent : string
    @param cert_parent : Issuer certificate file path

    @rtype : boolean
    @return : True if revoked or False
    """
    tmp_file = NamedTemporaryFile(prefix='cert')
    cert.save(tmp_file.name)
    data = {'cert': tmp_file.name,
            'cert_parent_with_crl': cert_parent_with_crl}
    cmd = "openssl verify -crl_check -CAfile %(cert_parent_with_crl)s %(cert)s" % data
    print cmd
    try:
        output = check_output(cmd, shell=True, stderr=STDOUT).lower()
    except CalledProcessError, e:
        msg = u"[OpenSSL] Error while checking ocsp %s: %s. Output: %r" % (
                    cmd, e, e.output)
        raise OpenSSLError(msg)
    print output
    return '%s: ok' % data['cert'] not in output

def get_cert_url_crl(cert):
    """
    Return the crl url from the certificate

    @type cert : M2Crypto.X509
    @parm cert : Certificate

    @rtype : string
    @return : CRL url
    """

    infos = [x.strip() for x in info_extension_cert(cert)["crlDistributionPoints"].split('\n')]
    crl_url = None
    for info in infos:
        print info
        if re.match(r"^URI:", info):
            crl_url = info.replace("URI:","")
            break
    return crl_url.strip()

rawDescription: Extract userCertificate from PKCS7 envelop in python


rawDescription: Regex into variables

description: Regex into variables


code:
c= []
for index in b:
    c.append(a[index])
print c

rawDescription: Getting a sublist of a Python list, with the given indices?


code:
import os.path

h = ['Header 1', 'Hea 2', 'Header 3', 'Header    4', 'H 5', 'Header', 'Header 7', 'Header 8']
a = [1, 2, 3, 4, 5, 6, 7, 8]

fileName = 'myFile.txt'

#write header
withHeader = not os.path.isfile(fileName)

f = open(fileName, 'a')
if withHeader:
    print 'Writing header'
    for s in h:
        f.write('%s\t'%s)
    f.write('\n')

#write numbers
for i in range(0, len(a)):
    space = len(h[i])/2;
    n = a[i]
    for c in range(0, space):
        f.write(' ')
    print 'Writing %d'%n
    f.write('%d'%n)
    for c in range(0, space):
        f.write(' ')
    f.write('\t')
f.write('\n')
f.close()

rawDescription: How to add values to existing columns in a text file (using Python)

description: Add values to existing columns in a text file (using Python)


code:
for payload_single in payloads:
    unsent_request.append(grequests.get('http://www.google.com',
                                        hooks={'response': do_something},
                                        params=payload_single))

print grequests.map(unsent_request)

rawDescription: Creat List of URLs for grequest

description: Creat List of URLs for grequest


code:
def list_append(args):
    count, id = args
    out_list = []
    for i in range(count):
        out_list.append(random.random())
    return out_list

if __name__ == "__main__":
    size = 10000000
    procs = 2

    pool = multiprocessing.Pool(procs)
    res = pool.map(list_append, [(size, i) for i in range(procs)])
    pool.close()
    pool.join()

    print "List processing complete."
    # print res

rawDescription: How to access the result of a function called in a multiprocessing in Python?

description: Access the result of a function called in a multiprocessing in Python


code:
from BeautifulSoup import BeautifulSoup

pageSource='''...omitted for brevity...'''

soup = BeautifulSoup(pageSource)
alltables = soup.findAll( "table", {"border":"2", "width":"100%"} )

results=[]
for table in alltables:
    rows = table.findAll('tr')
    lines=[]
    for tr in rows:
        cols = tr.findAll('td')
        for td in cols:
            text=td.renderContents().strip('\n')
            lines.append(text)
    text_table='\n'.join(lines)
    if 'Website' in text_table:
        results.append(text_table)
print "Number of tables found : " , len(results)
for result in results:
    print(result)

rawDescription: Need help with BeautifulSoup(Python) and parsing HTML table


code:
for ana in soup.findAll('a'):
  if ana.parent.name == 'td':
    print ana["href"]

rawDescription: How to get a nested element in beautiful soup

description: Get a nested element in beautiful soup


code:
key = [5,6,3,7,4,6,3,5,3]

for index,line in zip(key, result):
    print line[index]

rawDescription: Load text file into single string and table


code:
from functools import partial

def f(a, b):
    return a + b

x = range(10)
print map(partial(f, b=3), x)

rawDescription: Python: how to create a function pointer with a set argument?


code:
lines = [line.strip() for line in open('some.txt')]
nlines=[]
for line in lines:
    if line[-1]==':':
        nlines.append(':'+line)
    else:
        nlines.append(line)
total="\n".join(nlines)
temp=total.split(':')
answers=temp[2::2]
for ans in answers:print ans

rawDescription: In Python, how can I split a .txt file and store just the sections that I want in a list?


code:
from selenium.common.exceptions import NoSuchElementException

def get_value(driver, key):
    key = key + ":"
    try:
        return driver.find_element_by_xpath("//span[@class='minimal-list__title' and . = '%s']/following-sibling::span" % key).text
    except NoSuchElementException:
        print "Not Found"
        return None

rawDescription: Selenium: Find adjacent elements

description: Selenium: Find adjacent elements


code:
import re
pat = re.compile(r'(?:data:).*$')
string = " dnfhndkn data: ndknfdjoj pop"
res = re.search(pat,string)
print res.group()

rawDescription: regex to match a word and everything after it?

description: Regex to match a word and everything after it


code:
final_list = list(totalpair)
[x.append(y) for x,y in zip(final_list, totalprob)]
print final_list

rawDescription: I want to append the whole list to each other


code:
my_product = latest_products[0]
print my_product.avg_price.year

rawDescription: Django foreignkey and tables one to many relationship


code: print event['start']['date']

rawDescription: How to return ALL events in a Google Calendar without knowing whether it is a timed or all day event

description: Return ALL events in a Google Calendar without knowing whether it is a timed or all day event


code:
HELLO
12345
WORLD
67890
UTRECHT
030
dafsf434ffewr354tfffff44344fsfsd89087uefwerwe

rawDescription: Writing only lower case letters to file in Python


code:
import numpy as np

def lsqfity(X, Y):
    """
    Calculate a "MODEL-1" least squares fit.

    The line is fit by MINIMIZING the residuals in Y only.

    The equation of the line is:     Y = my * X + by.

    Equations are from Bevington & Robinson (1992)
    Data Reduction and Error Analysis for the Physical Sciences, 2nd Ed."
    pp: 104, 108-109, 199.

    Data are input and output as follows:

    my, by, ry, smy, sby = lsqfity(X,Y)
    X     =    x data (vector)
    Y     =    y data (vector)
    my    =    slope
    by    =    y-intercept
    ry    =    correlation coefficient
    smy   =    standard deviation of the slope
    sby   =    standard deviation of the y-intercept

    """

    X, Y = map(np.asanyarray, (X, Y))

    # Determine the size of the vector.
    n = len(X)

    # Calculate the sums.

    Sx = np.sum(X)
    Sy = np.sum(Y)
    Sx2 = np.sum(X ** 2)
    Sxy = np.sum(X * Y)
    Sy2 = np.sum(Y ** 2)

    # Calculate re-used expressions.
    num = n * Sxy - Sx * Sy
    den = n * Sx2 - Sx ** 2

    # Calculate my, by, ry, s2, smy and sby.
    my = num / den
    by = (Sx2 * Sy - Sx * Sxy) / den
    ry = num / (np.sqrt(den) * np.sqrt(n * Sy2 - Sy ** 2))

    diff = Y - by - my * X

    s2 = np.sum(diff * diff) / (n - 2)
    smy = np.sqrt(n * s2 / den)
    sby = np.sqrt(Sx2 * s2 / den)

    return my, by, ry, smy, sby

print lsqfity([0,2,4,6,8],[0,3,6,9,12])

rawDescription: retrieve the standard deviation of the y-intercept

description: Retrieve the standard deviation of the y-intercept


code:
def merge(times):
    saved = list(times[0])
    for st, en in sorted([sorted(t) for t in times]):
        if st <= saved[1]:
            saved[1] = max(saved[1], en)
        else:
            yield tuple(saved)
            saved[0] = st
            saved[1] = en
    yield tuple(saved)

lst1 = [[1, 2], [4, 7], [11, 13], [15, 21]]
lst2 = [[3, 4], [5,12], [23, 25]]

print list(merge(sorted(lst1 + lst2)))

rawDescription: Sort and Merge two list of lists

description: Sort and Merge two list of lists


code:
def match_date(abb_wk_dy, day_date, abb_mon):
    wkd, dd, ab = abb_wk_dy.capitalize(), day_date, abb_mon.capitalize()
    match = "{} {} {}".format(wkd, dd, ab)
    try:
        dte = "{} {} {} {}".format(1900, wkd, dd, ab)
        start = datetime.strptime(dte, "%Y %a %d %b")
    except ValueError:
        # first leap year since 1900
        dte = "{} {} {} {}".format(1904, wkd, dd, ab)
        start = datetime.strptime(dte, "%Y %a %d %b")
    day, mon = start.day, start.month
    greatest = None
    while start <= datetime.now():
        try:
            start = start.strptime("{} {} {}".format(start.year + 1, mon, day), "%Y %m %d")
        except ValueError:
            start = start.strptime("{} {} {}".format(start.year + 1, 01, 01), "%Y %m %d")
            continue
        if start.strftime("%a %d %b") == match:
            greatest = start
    return greatest.year if greatest else "No match"

rawDescription: string to date in python with the greatest possible year when dow is given

description: String to date in python with the greatest possible year when dow is given


code: print '\n'.join(['%s: %d' % (k, v) for k, v in d.items()])

rawDescription: How to add all the elements inside loop in Python?

description: Add all the elements inside loop in Python


code:
from noise import snoise2 # Simplex noise for 2D points

x, y = 0.5, 0.3
factor = 0.1
n = snoise2(x, y)
print x + n * factor, y + n * factor

rawDescription: How do you use Python's Noise 1.1.1 for Coordinates?

description: Use Python's Noise 1.1.1 for Coordinates


code:
import ast
import glob
import os

def my_load_files(folder, pattern):
    pathname = os.path.join(folder, pattern)
    for filename in glob.glob(pathname):
        with open(filename) as file:
            yield ast.literal_eval(file.read())

text_folder = 'C:/Users/username/Desktop/Samples'
print [[' '.join(x) for x in sample]
                        for sample in my_load_files(text_folder, 'File_*')]

rawDescription: How to load_files and process a .txt file with scikit-learn?

description: Load_files and process a .txt file with scikit-learn


code:
txt='''\
PATTERN1 PTR1 blah blah blah
needThis1  blah blah blah
thisOneAsWell1  blah blah blah
PATTERN2

PATTERN1 PTR2 blah blah blah
needThis2  blah blah blah
thisOneAsWell2  blah blah blah
PATTERN2

............................
............................

PATTERN1  PTRN blah blah
needThisN  blah blah blah
thisOneAsWellN blah blah blah
PATTERN2'''

import re

for m in re.finditer(r'^PATTERN1\s*(.*?)(?=^PATTERN2)', txt, re.M | re.S):
    print re.findall(r'(^\w+)', m.group(1), re.M)

rawDescription: Retreiving text from Pattern1 to Pattern2 - Python


code:
import urllib2
from bs4 import BeautifulSoup

content = urllib2.urlopen("http://link").read()
soup = BeautifulSoup(content)

with open('parseddata.txt', 'wb') as f:
    for a in soup.find_all('a', attrs={'class': 'book-title-link'}, href=True):
        print a['href']
        f.write(a['href'] + '\n')

rawDescription: How do i get my Beautiful soup output data to a text file?

description: Get my Beautiful soup output data to a text file


code:
def euler(df, x0, y0, dx, x1):
    x, y = x0, y0
    while x < x1:
        y = y + dx * df(x, y)
        x += dx
    return y

def fprime(x, y):
    return -0.5 * (x**2) - 0.5 * y

x0 = -3.0
y0 = 0.0
x1 = -1.0

for h in (0.5, 0.05, 0.001, 1E-6):
    print euler(fprime, x0, y0, h, x1)

rawDescription: How to stepwise approximate f(x) using Euler's method given f'(x)

description: Stepwise approximate f(x) using Euler's method given f'(x)


code:
class ClassA:
    def __init__(self):
        # Create or import ClassB right here
        class ClassB():
            # ClassA is a base class, ClassB is a child class
            # base argument is an internal object of the base class (ClassA)
            def __init__(self, base):
                # Assign it to the class variable for the further use
                self.base = base

            def main(self):
                # Use it here
                a = self.base.randomword(10)
                print 'hello, the output of ClassA.randomword is "' + str(a)+'"'

        # Instantiate ClassB
        class_b = ClassB(self)
        class_b.main()

    def randomword(self, num):
        print 'randomword was called'
        return 'hey there'

a = ClassA()

rawDescription: how to call a method from a class imported dynamically

description: Call a method from a class imported dynamically


code:
env.globals['session'] = session # Your session
# Your template can contain things like {{ session['key'] }}
template = env.get_template('mytemplate.html')
print template.render(the='variables', go='here')
#return response using rendered data

rawDescription: How do I access session data in Jinja2 templates (Bottle framework on app engine)?

description: Access session data in Jinja2 templates (Bottle framework on app engine)


code:
thelist = [{'name': 'a name', 'titles': [['foo','bar'],['foo','baz']]}]

print [
    {
        'name': d['name'],
        'titles': list(set(title for lst in d['titles'] for title in lst))
    }
    for d in thelist
]

rawDescription: List/Dict Data Manipulation - Delete Duplicates


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

for user in User.objects.all():
    print user.last_login

rawDescription: Get last_login for all users?

description: Get last_login for all users


code:
from collections import Counter
from itertools import repeat, chain
print list(chain.from_iterable(repeat(i, c) for i,c in Counter(a).most_common()))
# [5, 5, 5, 5, 3, 3, 3, 4, 4, 4, 1, 1, 2]

rawDescription: Sorting a List by frequency of occurrence in a list


code:
cur_health()
affect_health(20)
affect_health(-5)
affect_health(17)
cur_health()
print hp_history

rawDescription: How do I keep track of score increments in Python?

description: Keep track of score increments in Python


code:
class countcalls(object):

    def __init__(self, f):
        self._f = f
        self._numcalls = 0

    def __call__(self, *args, **kwargs):
        self._numcalls += 1
        return self._f(*args, **kwargs)

    def count(self):
        return self._numcalls

@countcalls
def f():
    print 'f called'

f()
f()
f()
print f.count()

# output:
#   f called
#   f called
#   f called
#   3

rawDescription: Unable to understand this python decorator


code:
list1 = [0,0,0,0,0,1,2]
count = 0
for item in list1:
    if item == 0:
        count = count + 1
    else:
        break

print count

rawDescription: While in a for loop

description: While in a for loop


code:
for myfilename in glob.iglob('*.gz'):
    for rec in rectypes:
        rec_count = get_rectype_count(myfilename, rec)
        print values.format(date=timestr, rec=rec, rec_count=rec_count)

rawDescription: how to join various bits of string and data together using python

description: Join various bits of string and data together using python


code:
def create_sub_cat(parent):
    print "creating subcat\n %s > " %(parent)
    conf=raw_input("type new subcat or press \"ENTER\" to save changes\n")
    if conf!="":
        parent+=" > "+conf
        return create_sub_cat(parent) # return
    return parent

rawDescription: Function that calls itself returns only first result of its call


code:
def outer():
    def inner():
        # some code
        inner.variab = "new_value"

    inner.variab = ""
    #some code including, presumably, a call to inner()
    print inner.variab

rawDescription: Change value of variable in outer-function


code:
class Character():
    """
    This is the main parents class for creation of
    characters, be they player, NPC or monsters they
    shall all share common traits
    """

    def __init__(self, name, health, defense):
        """Constructor for Character"""
        self.name = name
        self.health = health
        self.defense = defense

class Player(Character):
    """
    The player class is where heros are made
    They inherit common traits from the Character class
    """

    def __init__(self, name, health, defense, str, int):
        Character.__init__(self, name, health, defense)
        self.str = str
        self.int = int

hero = Player("Billy", 200, 10, 10, 2)
print hero.name

rawDescription: Python Child/Parent classes, Child Class Returning String Twice?


code:
#!/usr/bin/env python

import sys, os, time

t = int(sys.argv[1])
print 'hello, I am', t
time.sleep(t)
if t > 2:
    sys.exit()
os.execl(sys.argv[0], sys.argv[0], str(t+1))

rawDescription: Python: close all daemon threads

description: Python: close all daemon threads


code:
def foo( a, b, c=None ):
    if c is None:
        c = 'en'
    print c

rawDescription: Let default value for parameter take precedence if test fails


code:
class my_class(object):

    def __getitem__(self, key):
        return some_value_based_upon(key) #You decide the implementation here!

    def __setitem__(self, key, value):
        return store_based_upon(key, value) #You decide the implementation here!

i = my_class()
i[69] = 'foo'
print i[69]

rawDescription: python class [] function

description: Python class [] function


code:
L = range(100)
st1=len(L)%2
st2=0
while len(L)>1:
    del L[st2::2]
    st2=(st1+st2)%2
    st1=len(L)%2
    print L

rawDescription: How to delete elements of a circular list until there is only one element left using python?

description: Delete elements of a circular list until there is only one element left using python


code:
from bs4 import BeautifulSoup, Comment

data = """<html>
<head>
    <test1/>
    <test2/>
</head>
<body>
    test
</body>
</html>"""

soup = BeautifulSoup(data)
comment = Comment(' #mycomment ')
soup.head.insert(-1, comment)

print soup.prettify()

rawDescription: Python Beautiful soup insert comment in html


code:
import matplotlib.pyplot as plt

def has_been_closed(ax):
    fig = ax.figure.canvas.manager
    active_fig_managers = plt._pylab_helpers.Gcf.figs.values()
    return fig not in active_fig_managers

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(range(10))

print has_been_closed(ax)

plt.show()

print has_been_closed(ax)

rawDescription: How to detect that an axis belong to a window that has been closed in matplotlib

description: Detect that an axis belong to a window that has been closed in matplotlib


code:
import urllib2
print urllib2.urlopen('http://bit.ly/1cPIdPg').geturl()

rawDescription: Using python to expand a bit.ly link

description: Using python to expand a bit.ly link


code:
from socket import *
serverSocket = socket(AF_INET, SOCK_DGRAM)
serverSocket.bind(('', 12000))

while True:
    message, address = serverSocket.recvfrom(1024)
    print 'Server received', repr(message)
    serverSocket.sendto(message + message, address)

rawDescription: Send Null Byte?

description: Send Null Byte


code:
def fetch_quote(symbols):
    url = URL % '+'.join(symbols)
    fp = urllib.urlopen(url)
    try:
        data = fp.read()
    finally:
        fp.close()
    return data

def main():
    for symbol in symbols:
        data_fp = fetch_quote((symbol,))
        print data_fp

if __name__ == "__main__":
    main()

rawDescription: python multi threading/ multiprocess code

description: Python multi threading/ multiprocess code


code:
import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    def do_greet(self, person):
        """greet [person]
        Greet the named person"""
        if person:
            print "hi,", person
        else:
            print 'hi'

    def do_EOF(self, line):
        return True

    def postloop(self):
        print

if __name__ == '__main__':
    HelloWorld().cmdloop()

rawDescription: giving a command/shell type interaction in python

description: Giving a command/shell type interaction in python


code:
val = Map.scenes.get(scene_name)
print "next_scene returns", val
return val

rawDescription: Python: Understanding how 'return' works within a class (Learn Python Hard Way, exercise 43)


code:
def testFunc2():
    print testVar

rawDescription: Local and global variables in python functions


code:
p = Point((10, 13))
grid[(10, 13)]["coins"] = 50
print grid[p]["coins"]              # prints 50
print grid[p.r_neighbor]["coins"]   # prints 0; again, no KeyError

rawDescription: python: representing square grid that wraps on itself (cylinder)

description: Python: representing square grid that wraps on itself (cylinder)


code:
def numRange(A, B, C):

    current = []
    current_sum = 0
    count = 0

    for number in A:
        current.append(number)
        current_sum += number
        while current_sum > C:
            current_sum -= current[0]
            current = current[1:]

        if B <= current_sum <= C:
            count += 1
            print current_sum, current

    # Now check the remaining items in current, in case of a trailing sequence:
    # Test with A = [10, 5, 1, 0, 2, 4] to demonstrate the need.
    if not current:
        return count

    current_sum -= current[0]
    current = current[1:]
    while (B <= current_sum <= C):
        count += 1
        print current_sum, current
        current_sum -= current[0]
        current = current[1:]

    return count

print "Total of %d subarrays" % numRange( [10, 5, 1, 0, 2], 6, 8)
print
print "Total of %d subarrays" % numRange( [10, 5, 1, 0, 2, 4], 6, 8)

rawDescription: Number of subarrays within a sum range

description: Number of subarrays within a sum range


code:
import numpy as np
x = (np.random.random(100) * 5).astype(np.int)
unique_vals, indicies = np.unique(x, return_inverse=True)
counts = np.bincount(indicies)

print unique_vals, counts

rawDescription: comparing row in numpy array

description: Comparing row in numpy array


code:
import pandas as pd
import numpy as np

df=pd.DataFrame({'group':[1,2,3,' No Data',4,5,6]})

df = df.replace(r'No Data', np.nan, regex=True)
print df

rawDescription: Python ignoring leading whitespace when doing .replace on dataframe column with strings


code:
def foo(name):
    exec "{} = 1".format(name)

def bar(name):
    exec "{} = 1".format(name) in globals()

foo('a')
a
bar('a')
a

rawDescription: Class and function scope

description: Class and function scope


code:
def folders_in(path_to_parent):
    for fname in os.path.listdir(path_to_parent):
        if os.path.isdir(os.path.join(path_to_parent,fname)):
            yield os.path.join(path_to_parent,fname)

print list(folders_in("/path/to/parent"))

rawDescription: Check if a given directory contains any directory in python


code:
import collections

char, num = collections.Counter(stringToData).most_common(1)[0]
print "In your string, the most frequent character is %s, which occurred %d times" % (char, num)

rawDescription: Pretty-printing data from counter - Python


code:
import sys
a = 3
b = 4
c = 5
for a in range(3,500):
    for b in range(a+1,500):
        c = (a**2 + b**2)**0.5
        if a + b + c == 1000:
            print a, b, c
            print a*b*c
            sys.exit()     #stops the script

rawDescription: Exit while loop in Python

description: Exit while loop in Python


code:
import pprint
import pooler
from threading import Thread
import datetime
import logging
pp = pprint.PrettyPrinter(indent=4)

class myThread(Thread):
    """
    """
    def __init__(self, obj, cr, uid, context=None):
        Thread.__init__(self)
        self.external_id_field = 'id'
        self.obj = obj
        self.cr = cr
        self.uid = uid
        self.context = context or {}
        self.logger = logging.getLogger(module_name)
        self.initialize()

    """
        Abstract Method to be implemented in the real instance
    """
    def initialize(self):
        """
            init before import
            usually for the login
        """
        pass

    def init_run(self):
        """
            call after intialize run in the thread, not in the main process
            TO use for long initialization operation
        """
        pass

    def run(self):
        """
            this is the Entry point to launch the process(Thread)
        """
        try:
            self.init_run()
            #Your Code Goes Here
            #TODO Add Business Logic
            self.cr.commit()
        except Exception, err:
            sh = StringIO.StringIO()
            traceback.print_exc(file=sh)
            error = sh.getvalue()
            print error
        self.cr.close()

rawDescription: Python asynchronous processing in existing loop


code:
m = (sin(x) + 3*sin(x) + sin(x)+1).mean()
s = (sin(x) + 3*sin(x) + sin(x)+1).std()
print m, s

rawDescription: By Python. making standard deviation and mean lines with different graphes


code:
#!/usr/bin/env python
import settings
print settings.var_name

rawDescription: python global parameters file

description: Python global parameters file


code:
print min(filter(lambda x: x > num,li))
23

rawDescription: find the closest number in array that is bigger then the number

description: Find the closest number in array that is bigger then the number


code:
# original
print row.xpath(".//td[@class='forecast']/text()").extract()
# ported
print extract_text(row.find(r'.//td[@class="forecast"]'))

rawDescription: converting scrapy to lxml

description: Converting scrapy to lxml


code:
from fabric.api import env, task, run, sudo as _sudo, settings, hide

env.user = 'vagrant'
env.key_filename = '~/.vagrant/machines/default/virtualbox/private_key'
env.host_string = '127.0.0.1'
env.port = '2222'

def is_file_writable(filepath, sudo=False):
    fn = run if not sudo else _sudo
    with settings(warn_only=True), hide('everything'):
        response = fn('test -w ' + filepath)
        return response.return_code == 0

@task
def sometask():
    print is_file_writable('/etc/sudoers')
    print is_file_writable('/etc/sudoers', sudo=True)

rawDescription: Fabric check user has permissions on file/folder


code:
import json

dic1 = json.load('json_file_1')
dic2 = json.load('json_file_2')
dic1.update(dic2)

print dic1

rawDescription: How to merge 2 JSON files?

description: Merge 2 JSON files


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)
for a in soup.find_all('a'):
    # here is how you get the text from 'h3' tag
    header = a.parent.find_previous_sibling('h3').text
    # here is how you set the text of the anchor tag to be the text of 'h3' tag
    a.string = header
    print a

rawDescription: BeautifulSoup: Replace anchor text with text from another tag


code: print zip(my_list[0::2], my_list[1::2])

rawDescription: list of ints into a list of tuples python

description: List of ints into a list of tuples python


code:
import re
lanip="74.125.224.72"
notorm=re.search("([1-9])\w+$", lanip)
print notorm

rawDescription: Regex not working in python script

description: Regex not working in python script


code:
import Tkinter
root = Tkinter.Tk()
def func(event):
    print event.keysym
root.bind("<Key>", func)
root.mainloop()

rawDescription: How can I bind the Enter key to my tkinter window

description: Bind the Enter key to my tkinter window


code:
for e in matches:
   sibling = lxml.etree.Element("url")
   sibling.text = "www.espresso.com"
   e.addnext(sibling)
print lxml.etree.tostring(tree)

rawDescription: how to add an xml node based on the value of a text node

description: Add an xml node based on the value of a text node


code:
str = r'"IPAddress":"10.0.0.18","PolicerID":"","IPAddress":"","PolicerID":""'
print re.findall(r'"IPAddress":"([^"]*)', str)

rawDescription: regex: match all characteres between 2 words, returns strange output

description: Regex: match all characteres between 2 words, returns strange output


code:
mylist = list(range(10000))
b = mylist
del mylist
#here we didn't get all the references to the objects we created ...
print b #[0, 1, 2, 3, 4, ...]

rawDescription: Delete all objects in a list

description: Delete all objects in a list


code:
for number in numbers:
    formData = (
        ('__EVENTVALIDATION', eventvalidation),
        ('__VIEWSTATE', viewstate),
        ('__VIEWSTATEGENERATOR',viewstategenerator),
        ('number', str(number)), # Here you use the number obtained
        ('Button', 'Sorgula'),
    )

    encodedFields = urllib.urlencode(formData)
    # second HTTP request with form data
    f = myopener.open(url, encodedFields)

    soup = BeautifulSoup(f.read())

    name=soup.findAll('input',{'id':'name_field'})

    for eachname in name:
       print eachname['value']

rawDescription: Sending multiple POST data in Python

description: Sending multiple POST data in Python


code:
source
class Circle(object):

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

    @property
    def radius(self):
        print 'GET'
        return self.__radius

    @radius.setter
    def radius(self, radius):
        print 'SET'
        assert radius > 0, "radius must be nonzero and non-negative"
        self.__radius = radius

print 'creating circle:'
c = Circle(2)
print 'modifying radius:'
c.radius = 3
print 'verifying'
print c.radius

rawDescription: Python: Using Properties to Control Attribute Access


code:
import re
a_list  = re.sub("[^a-zA-Z ]","",s).split()#4957 words (lorum ipsum generated)
search_space = set("dog cat fish bear walrus".split())
def stephenPochmann():
    for needle in search_space:
        if needle in a_list: print needle

def joranbeasley():
    return search_space.intersection(a_list)

import timeit
print "Stephen Timeit:",timeit.timeit(stephenPochmann,number=1000)
print "joran Timeit:",timeit.timeit(joranbeasley,number=1000)

rawDescription: Optimize runtime of calling 'in' many times on same string


code:
parent = node()
wchild = node()
parent.farmer = True
wchild = parent
if wchild.farmer:
  print "Farmer"

rawDescription: Python: Parent and Child responding to child-specific changes in boolean values. Why is this happening?


code:
a = str(num)
b = str(num)
print a is b # <-- this will print False in most cases (but try str(1) is str(1))

rawDescription: Are strings pooled in Python

description: Are strings pooled in Python


code:
import re

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.google.com/?gws_rd=ssl#q=plumbers%2Bhouston%2Btx')

# waiting for results to load
wait = WebDriverWait(driver, 10)
box = wait.until(EC.visibility_of_element_located((By.ID, "lclbox")))

phone_re = re.compile(r"\(\d{3}\) \d{3}-\d{4}")

for result in box.find_elements_by_class_name("intrlu"):
    for span in result.find_elements_by_tag_name("span"):
        if phone_re.search(span.text):
            parent = span.find_element_by_xpath("../..")
            print parent.text
            break
    print "-----"

rawDescription: Webdriver/Selenium: How to find element when it has no class name, id, or css selecector?


code: print map(sum, izip(*twin))

rawDescription: Vector algebra in functional

description: Vector algebra in functional


code:
for i in range (0, len(names)):
    if 'Running' in states[i]:
        print " [*] stop if running {0} ".format(str(names[i]))
        #service_info(action, machine, names[i])
        terminated+=1
    else:
        print " [!] Passing over {0} ".format(str(names[i]))
        passed+=1
    sys.stdout.write(" [ ] {0}/{1} progress left\r".format(i+1, len(names)))
    sys.stdout.flush()
    sleep(2)

rawDescription: Sys.stdout.write not working as expected


code:
map(list, zip(*(map(list, zip(*x)) for x in a)))
for row in _:
    print row

rawDescription: How to unpack tuples in nested list?

description: Unpack tuples in nested list


code:
element_colors = {'[W]': 'cyan', '[X]': 'red', '[S]': 'green', '[H]': 'magenta'}

print " ".join(colored(element, element_colors[element]) for element in row)

rawDescription: Printing certain strings different colour with termcolor.colored?


code:
def parallel_predict(subjects, conditions):
    freeze_support()
    p = Pool(20)
    # set each matching item into a tuple
    job_args = list(itertools.product(*[subjects,conditions]))

    print job_args
    # map to pool
    ans = p.map(main_helperPredict, job_args)

    p.terminate()
    p.join()
    del p

    print "finished"
    return ans

rawDescription: Multiprocessing processes in uninterruptible sleep (D) state when run from iPython Notebook


code:
mac = '8:AA:C:3:ED:E'
print ':'.join([i.zfill(2) for i in mac.split(':')])

rawDescription: Perform a substring replacement in a single line in Python


code:
from yahoo_finance import Share

stock = Share('GOOG')
start_date = '2015-01-01'
end_date = '2015-06-30'

closes = [c['Close'] for c in stock.get_historical(start_date, end_date)]

for c in closes:
    print c

rawDescription: How can I store the historical stock prices (using an array)?

description: Store the historical stock prices (using an array)


code:
lists = [["abcd", "abc", "ab"], ["abcd", "abc", "ab"], ["abcd", "abc", "ab"]]
print [[item for item in clist if len(item) >= 3] for clist in lists]
# [['abcd', 'abc'], ['abcd', 'abc'], ['abcd', 'abc']]

rawDescription: Removing small words in python from nested lists


code:
import vardata
print vardata.something_else

rawDescription: python using variables from another file

description: Python using variables from another file


code:
import re
s = "5d4h2s"
p = re.compile("([0-9])([a-z])")
for m in p.findall(s):
  print m

rawDescription: Split based by a-z character in an alphanumeric string in python


code:
from matplotlib import pyplot

bck = pyplot.get_backend()
print "Backend is " + bck
mng = pyplot.get_current_fig_manager()
if (bck == "TkAgg"):
    mng.window.resizable(False, False)
elif (bck == "QT4Agg"):
    print "See previous answer"
else:
    print "?"

rawDescription: How do I lock window resizing in a Python matplotlib window?

description: Lock window resizing in a Python matplotlib window


code:
import urllib2

try:
    urllib2.urlopen(url)
    return True         # URL Exist
except ValueError, ex:
    return False        # URL not well formatted
except urllib2.URLError, ex:
    return False        # URL don't seem to be alive

rawDescription: Python verify url goes to a page

description: Python verify url goes to a page


code:
class Action(object):
    def __init__(self, count):
        self.count = count
    def perform(self):
        print "perform " + str(self.count)
        if self.count == 2:
            raise Exception("self.count is " + str(self.count))
    def commit(self):
        print "commit " + str(self.count)
    def rollback(self):
        print "rollback " + str(self.count)
    def __enter__(self):
        perform()
        return self
    def __exit__(self, exc_type, exc_value, traceback):
        if exc_value is None:
            self.commit()
        else:
            self.rollback()

with Action(1), Action(2), Action(3):
    pass

rawDescription: Maintaining a roll-backable flow of code in python without extreme identation


code:
  # init as before...
for newRGBA in tuple(RGBA for RGBA in NewSurf.get_palette() if RGBA != blankRGBA):
    try: swapidx = next(n for n, RGBA in enumerate(BaseSurf.get_palette()) if RGBA == blankRGBA)
    except StopIteration: raise IndexError, "Ran out of palette space!"
    BaseSurf.set_palette_at(swapidx, newRGBA)

BaseSurf.blit(NewSurf, (33,33))
screen.blit(BaseSurf, (50, 122))

rawDescription: Is there a way to merge palettes for indexed pygame.Surfaces?


code:
import re
import requests
from lxml import html

def getVideosLinks(content):
    videos = re.findall('(http://[\.\w/_]+\.mp[34])', content)
    return videos

def prepareJSONurl(episode_hash):
    json_url = "http://pepto.portico.net2.tv/playlist/{hash}".format(hash=episode_hash)
    return json_url

def extractEpisodeHash(content):
    tree = html.fromstring(content)
    video_url = tree.xpath('//meta[contains(@http-equiv, "refresh")]/@content')[0].split('=',1)[1]
    episode_hash = re.findall('episode=([\w]+)', video_url)
    return episode_hash[0]

def extractIframeURL(content):
    iframe_url = None
    tree = html.fromstring(content)
    try:
        iframe_url = tree.xpath('//iframe/@src')[0]
        is_video = True
    except:
        is_video = False
    return is_video, iframe_url

POPSCI_URL = "http://www.popsci.com/thorium-dream"

response = requests.get(POPSCI_URL)
is_video, iframe_url = extractIframeURL(response.content)

if is_video:
    response_from_iframe_url = requests.get(iframe_url)
    episode_hash = extractEpisodeHash(response_from_iframe_url.content)

    json_url = prepareJSONurl(episode_hash)
    final_response = requests.get(json_url)

    for video in getVideosLinks(final_response.content):
        print "Video: {}".format(video)
else:
    print "This is not a POPSCI video page :|"

rawDescription: How to scrape the video src url from video tag which is injected via javascript?

description: Scrape the video src url from video tag which is injected via javascript


code:
from gurobipy import *
from numpy import *

numT = 300;
numC = 300;

Assignment = random.random((numT,numC))

m=Model("Assignment")

X = []
for t in range(numT):
    X.append([])
    for c in range(numC):
        X[t].append(m.addVar(vtype=GRB.BINARY,name="X%d%d"% (t, c)))
m.update()
m.modelSense = GRB.MAXIMIZE
constraintT = []
constraintC = []
for t in range(numT):
    constraintT.append(m.addConstr(quicksum(X[t][c] for c in range(numC)) == 1 ,'constraintT%d' % t))

for c in range(numC):
    constraintT.append(m.addConstr(quicksum(X[t][c] for t in range(numT)) == 1 ,'constraintC%d' % t))

m.setObjective(quicksum(quicksum([X[t][c]*Assignment[t][c] for c in range(numC)]) for t in range(numT)))

m.update()
m.optimize()

print 'runtime is',m.Runtime

rawDescription: Gurobi: Get optimization runtime

description: Gurobi: Get optimization runtime


code:
import sys
var1 = sys.argv[1]
var2 = sys.argv[2]
print var1, var2

rawDescription: How to use variables from shell script in python script file?

description: Use variables from shell script in python script file


code:
list_1 = ['hostname1 ipaddress1', 'hostname2 ipaddress2', 'hostname3 ipaddress3']
list_interest = ['hostname1', 'hostname3']
new_dict = dict((x.split(' ') for x in list_1))
set_interest = set(list_interest)
for item in new_dict:
    if item in set_interest:
        print "yes"
    else:
        print "\t", item

rawDescription: Comparing Lists and Dictionaries correctly


code:
import fileinput
import re
files = ["FileToSub1.sbs", "FileToSub2.sbs"]
for line in fileinput.FileInput(files, inplace=1):
    line = re.sub('whatever', 'thing', line)
    print line,

rawDescription: Python - substitute inplace - removing .bak backup files with close()?


code:
set1= set(['apples', 'oranges'])
set2= set(['oranges', 'raisins'])
print len(set1.intersection(set2))

rawDescription: Advanced grouping in Python

description: Advanced grouping in Python


code:
query = "SELECT COUNT(*) FROM programs"
cursor.execute(query)
x = cursor.fetchone()[0]
running_total = 1 #Again, you want to start counting at 1 for some reason
while running_total < x:
    running_total += 69
    print running_total

rawDescription: How to count the values of rows in a database

description: Count the values of rows in a database


code:
from bs4 import BeautifulSoup
import requests

url = "http://www.ijreview.com/wp-admin/admin-ajax.php"
session = requests.Session()
page_size = 24

params = {
    'action': 'load_more',
    'numPosts': page_size,
    'category': '',
    'orderby': 'date',
    'time': ''
}

offset = 0
limit = 100
while offset < limit:
    params['offset'] = offset
    response = session.post(url, data=params)
    links = [a['href'] for a in BeautifulSoup(response.content).select('li > a')]
    for link in links:
        response = session.get(link)
        page = BeautifulSoup(response.content)
        title = page.find('title').text.strip()
        author = page.find('span', class_='author').text.strip()
        print {'link': link, 'title': title, 'author': author}

    offset += page_size

rawDescription: Working with post request to load more articles with scrapy python


code:
@simple_decorator
def my_simple_logging_decorator(func):
    def you_will_never_see_this_name(*args, **kwargs):
        print 'calling {}'.format(func.__name__)
        return func(*args, **kwargs)
    return you_will_never_see_this_name

rawDescription: On demand decorator?

description: On demand decorator


code:
import sys

# some functions...
def a():
   return 'a'

def b():
   return 'b'

def c():
   return 'c'

# get the module object for this file
module = sys.modules[__name__]

# get a list of the names that are in this module
for name in dir(module):
   # get the Python object from this module with the given name
   obj = getattr(module, name)
   # ...and if it's callable (a function), call it.
   if callable(obj):
      print obj()

rawDescription: loop through all methods in a python program

description: Loop through all methods in a python program


code:
# import the builtin time module, this is always available
import time

# initialize the t0 variable, "starting the stopwatch"
t0 = time.time()

while True:
  # calculate the time since some reference point (here the Unix Epoch)
  t1 = time.time()

  # calculate the difference, i.e. the time elapsed
  dt = t1 - t0

  if dt >= 3:
    print "Three seconds reached, resetting timer"
    t0 = t1
  else:
    print "Time elapsed is", dt, "seconds"

rawDescription: Resetting pygame's timer

description: Resetting pygame's timer


code:
def verbose(function):
    print 'i am verbose and func is ' + function.__name__
    def wrapper2(func):
        print 'func is ' + repr(func)
        result = function(func)
        return result
    return wrapper2

def more(function):
    print 'i am more and func is ' + function.__name__
    def wrapper1(*args, **kwargs):
        print 'args' + repr(args)
        result = function(*args)
        return result
    return wrapper1
more = verbose(more)

def hello(*args):
    print(sum(args))
hello = more(hello)

rawDescription: decorating decorators in python

description: Decorating decorators in python


code:
import ctypes

my_c_library = ctypes.CDLL('my_c_library.dll')

def call_my_func(input_list):
    length = len(input_list)
    array = (ctypes.c_ubyte * length)(*input_list)
    return my_c_library.func(array, length)

my_list = [0, 1, 2]
print call_my_func(my_list)

rawDescription: Passing python array to a c function using c_types


rawDescription: how to extend column size for some selected rows and also add additional rows of a 2D array python2.7

description: Extend column size for some selected rows and also add additional rows of a 2D array python2.7


code:
elevDF = sqlContext.createDataFrame(sc.parallelize([
        Row(date=datetime.datetime(1984, 1, 1, 0, 0), hour=1, value=638.55),
        Row(date=datetime.datetime(1984, 1, 1, 0, 0), hour=2, value=638.55),
        Row(date=datetime.datetime(1984, 1, 1, 0, 0), hour=3, value=638.55),
        Row(date=datetime.datetime(1984, 1, 1, 0, 0), hour=4, value=638.55),
        Row(date=datetime.datetime(1984, 1, 1, 0, 0), hour=5, value=638.55)]))

(elevDF
 .map(lambda (date, hour, value): (date.year, date.month, date.day))
 .collect())

rawDescription: Spark DataFrame TimestampType - how to get Year, Month, Day values from field?


code:
import time
import random

def makeg(n):
    g=dict()
    for i in xrange(n):
        g[i] = tuple([random.randint(0,n) for _ in xrange(3)])
    return g

g=makeg(100000)

def m(g):
    g2 = dict()
    for key in g.keys():
        old_vals=set(g[key])
        new_vals=[]
        for val in old_vals:
            try:
                new_vals.extend(g[val])
            except KeyError:
                pass
        new_vals = tuple(set(new_vals)-old_vals)
        g2[key]=new_vals
    return g2

s1 = time.time()
m(g)
e1 = time.time()

from collections import defaultdict

def h(g):
    a = defaultdict(set)
    [a[x].update(g.get(y, [])) for x in g for y in g[x]]
    [a[x].difference_update(g[x]) for x in g]
    g2={x:tuple(a[x]) for x in a}
    return g2

s2 = time.time()
h(g)
e2=time.time()

mt =(e1-s1)
ht=(e2-s2)
print mt,ht,mt/ht

rawDescription: Creating a new dict with old keys but with subvalues


code:
str1='''24M Technologies,Series A,8/19/10
24M Technologies,Grant,8/16/10
2B Energy,Private Equity,3/18/14
2B Energy,Series B,3/18/14
2B Energy,Unattributed VC,5/1/08
3GSolar Photovoltaics,Series A,12/17/12
3sun Group,Growth Equity,3/3/14
3Tier Group,Series C,11/17/08'''

list1= str1.split('\n')
print list1

rawDescription: How can I create a dynamic dictionaries in Python using a CSV file

description: Create a dynamic dictionaries in Python using a CSV file


code:
config_string = '''Current configuration : 1543 bytes

!
version 12.4
service timestamps debug datetime msec
service timestamps log datetime msec
no service password-encryption
!
hostname ESW1
!
boot-start-marker
boot-end-marker
!'''

import re
hostname = re.findall(r'hostname\s+(\S*)', config_string)[0]
print hostname
# ESW1

rawDescription: Extract specific string from Telnet output with Python


code:
import collections

class NestedListIter(object):
    '''A mutable container that enables flat traversal of a nested tree of
    lists. nested_list should contain only a list-like mutable sequence.
    To preserve a clear demarcation between 'leaves' and 'branches', empty
    sequences are not allowed as toplevel objects.'''
    def __init__(self, nested_list):
        if not nested_list:
            raise ValueError, 'nested_list must be a non-empty sequence'
        self.nested_list = nested_list # at some point, vet this to make sure
        self.go_to_head()              # it contains no empty sequences

    def _is_sequence(self, item=None):
        '''Private method to test whether an item is a non-string sequence.
        If item is None, test current item.'''
        if item is None:
            item = self._get_item_at(self.stack)
        return isinstance(item, collections.Sequence) and not isinstance(item, basestring)

    def _is_in_range(self, index_tuple=None):
        '''Private method to test whether an index is in range.
        If index is None, test current index.'''
        if index_tuple is None:
            index_tuple = self.stack
        if any(x < 0 for x in index_tuple):
            return False
        try:
            self._get_item_at(index_tuple)
        except IndexError:
            return False
        else:
            return True

    def _get_item_at(self, index_tuple):
        '''Private method to get item at an arbitrary index, with no bounds checking.'''
        item = self.nested_list
        for i in index_tuple:
            item = item[i]
        return item

    def _set_item_at(self, index_tuple, value):
        '''Private method to set item at an arbitrary index, with no bounds checking.
        Throws a ValueError if value is an empty non-string sequence.'''
        if self._is_sequence(value) and not value:
            raise ValueError, "Cannot set an empty list!"
        containing_list = self._get_item_at(index_tuple[:-1])
        containing_list[index_tuple[-1]] = value

    def _insert_at(self, index_tuple, value):
        '''Private method to insert item at an arbitrary index, with no bounds checking.
        Throws a ValueError if value is an empty non-string sequence.'''
        if self._is_sequence(value) and not value:
            raise ValueError, "Cannot insert an empty list!"
        containing_list = self._get_item_at(index_tuple[:-1])
        containing_list.insert(index_tuple[-1], value)

    def _delete_at(self, index_tuple):
        '''Private method to delete item at an arbitrary index, with no bounds checking.
        Recursively deletes a resulting branch of empty lists.'''
        containing_list = self._get_item_at(index_tuple[:-1])
        del containing_list[index_tuple[-1]]
        if not self._get_item_at(index_tuple[:-1]):
            self._delete_at(index_tuple[:-1])

    def _increment_stack(self):
        '''Private method that tires to increment the top value of the stack.
        Returns True on success, False on failure (empty stack).'''
        try:
            self.stack[-1] += 1
        except IndexError:
            return False
        else:
            return True

    def _decrement_stack(self):
        '''Private method that tries to decrement the top value of the stack.
        Returns True on success, False on failure (empty stack).'''
        try:
            self.stack[-1] -= 1
        except IndexError:
            return False
        else:
            return True

    def go_to_head(self):
        '''Move the cursor to the head of the nested list.'''
        self.stack = []
        while self._is_sequence():
            self.stack.append(0)

    def go_to_tail(self):
        self.stack = []
        '''Move the cursor to the tail of the nested list.'''
        while self._is_sequence():
            self.stack.append(len(self.get_item()) - 1)

    def right(self):
        '''Move cursor one step right in the nested list.'''
        while self._increment_stack() and not self._is_in_range():
            self.stack.pop()
        if not self.stack:
            self.go_to_tail()
            return False
        while self._is_sequence():
            self.stack.append(0)
        return True

    def left(self):
        '''Move cursor one step left in the nested list.'''
        while self._decrement_stack() and not self._is_in_range():
            self.stack.pop()
        if not self.stack:
            self.go_to_head()
            return False
        while self._is_sequence():
            self.stack.append(len(self.get_item()) - 1)
        return True

    def move_cursor(self, index_tuple):
        '''Move cursor to the location indicated by index_tuple.
        Raises an error if index_tuple is out of range or doesn't correspond
        to a toplevel object.'''
        item = self._get_item_at(index_tuple)
        if self._is_sequence(item):
            raise IndexError, 'index_tuple must point to a toplevel object'

    def get_item(self):
        '''Get the item at the cursor location.'''
        return self._get_item_at(self.stack)

    def set_item(self, value):
        '''Set the item a the cursor locaiton.'''
        return self._set_item_at(self.stack, value)

    def insert(self, value):
        '''Insert an item at the cursor location. If value is a sequence,
        cursor moves to the first toplevel object in value after insertion.
        Otherwise, cursor does not move.'''
        temp_stack = self.stack[:]
        self.left()
        self._insert_at(temp_stack, value)
        self.right()

    def delete(self):
        '''Deete an item at the cursor location. Cursor does not move.'''
        temp_stack = self.stack[:]
        self.left()
        self._delete_at(temp_stack)
        self.right()

    def iterate(self):
        '''Iterate over the values in nested_list in sequence'''
        self.go_to_head()
        yield self.get_item()
        while self.right():
            yield self.get_item()

    def iterate_left(self):
        '''Iterate over the values in nested_list in reverse.'''
        self.go_to_tail()
        yield self.get_item()
        while self.left():
            yield self.get_item()

    def find(self, value):
        '''Search for value in nested_list; move cursor to first location of value.'''
        for i in self.iterate():
            if i == value:
                break

    def find_left(self, value):
        '''Search for value backwards in nested_list; move cursor to last location of value.'''
        for i in self.iterate_left():
            if i == value:
                break

def _NLI_Test():
    l = [1, 2, 3, ['a', 'b', 'c'], 4, ['d', 'e', [100, 200, 300]], 5, ['a', 'b', 'c'], 6]
    nli = NestedListIter(l)
    print nli.nested_list
    for i in nli.iterate():
        print i,
    print
    for i in nli.iterate_left():
        print i,
    print

    nli.go_to_head()
    for i in range(5):
        nli.right()
    nli.insert('cow')
    nli.insert(['c', ['o', 'w']])
    print nli.nested_list
    nli.find('cow')
    print nli.get_item()
    nli.delete()
    print nli.nested_list
    nli.find('c')
    nli.delete()
    print nli.nested_list
    nli.find_left('w')
    nli.delete()
    nli.find('o')
    nli.delete()
    print nli.nested_list
    print nli.nested_list == l
    nli.find(100)
    nli.set_item(100.1)
    print nli.nested_list

if __name__ == '__main__':
    _NLI_Test()

rawDescription: Convert list of positions [4, 1, 2] of arbitrary length to an index for a nested list


code:
for candidate in name:
    for word in name:
        # candidate is a compound if it contains any other word (not equal to it)
        if word != candidate and word in candidate:
            break      # a compound. break inner loop, continue outer
    else:              # no breaks occured, must be a basic noun
        print candidate

rawDescription: Separating compound nouns from basic nouns


code:
def is_john_here(d):
    for v in d.values():
        if v[1][1] == "john": return True
    return False

print is_john_here(sampleDict)  # False

rawDescription: How to iterate through a list in a dictionary in python

description: Iterate through a list in a dictionary in python


code:
import unittest
import time

class SomeTest(unittest.TestCase):
    def setUp(self):
        self.startTime = time.time()

    def tearDown(self):
        t = time.time() - self.startTime
        print "%s: %.3f" % (self.id(), t)

    def testOne(self):
        time.sleep(1)
        self.assertEquals(int('42'), 42)

    def testTwo(self):
        time.sleep(2)
        self.assertEquals(str(42), '42')

if __name__ == '__main__':
    suite = unittest.TestLoader().loadTestsFromTestCase(SomeTest)
    unittest.TextTestRunner(verbosity=0).run(suite)

rawDescription: How to know time spent on each test when using unittest?

description: Know time spent on each test when using unittest


code:
def horror():
  exec "x=23"
  return x

print horror()

rawDescription: Running exec inside function

description: Running exec inside function


code: print max(d.keys(), key=lambda x: d[x])

rawDescription: Print the key of the max value in a dictionary the pythonic way


code:
import itertools
print list(itertools.chain.from_iterable(x.split()))

rawDescription: Two for loops in a comprehensive list

description: Two for loops in a comprehensive list


rawDescription: Rearrange several sublists into two sublists


code: print result

rawDescription: Generate list of all combinations and maintain index position


code:
from StringIO import StringIO

class CtxStringIO(StringIO):
    def __enter__(self):
        return self
    def __exit__(self, *args):
        return False

infile = CtxStringIO(''.join('{}\n'.format(i) for i in xrange(19*10)))
outfile = CtxStringIO()

# this should be the main loop of your program.
# just replace infile and outfile with real file objects
with infile as ifp, outfile as ofp:
    for chunk in grouper(19, ifp, '\n'):
        ofp.writelines(process_chunk(chunk))

# see what was written to the file
print ofp.getvalue()

rawDescription: python read specific lines from file and continue

description: Python read specific lines from file and continue


code:
import os

def mp3_files(root):
  # this is a generator that will return mp3 file paths within given dir
  for f in os.listdir(root):
      fullpath = os.path.join(root,f)
      if os.path.isdir(fullpath) and not os.path.islink(fullpath):
          for x in mp3_files(fullpath):  # recurse into subdir
              yield x
      else:
          if fullpath[len(fullpath)-3:] == 'mp3':
            yield fullpath

for p in mp3_files(root_dir):
  id3_data = ID3(p)
  print 'Artist:', id3_data['ARTIST']
  print 'Title:', id3_data['TITLE']

rawDescription: Python scripted mp3 database, with a php front end


code:
from ete2 import Tree
import re

def find_id(file,id):

    f= open(file)
    s = open(file).read()
    if re.search(id,s,re.MULTILINE):
        found= True
    else:
        found= False
    return found

def remove_nodes(treeFile,idFile):

    t= Tree(treeFile,format=8)
    print t.get_ascii()
    removed=[]
    for node in t.iter_descendants():
        # print node.name
        if not find_id(idFile,'^'+node.name+'\s'):
            node.delete(prevent_nondicotomic=False)
            removed.append(node)

    print [n.name for n in removed]
    print t.get_ascii()
    t.write(format=1, outfile="newtree.nw")

remove_nodes('arthropods.nw','taxidMap.txt')

rawDescription: python ete2 delete node from tree

description: Python ete2 delete node from tree


code:
for infile in sorted(glob.glob('*.txt')):
    print "Current File Being Processed is: " + infile

rawDescription: Reading files in a particular order in python


code:
import numpy as np

def parseCircuit(it, header):
    loadCurrent, surfaceTemp, time = [], [], []
    for line in it:
        line = line.strip()
        if not line:
            break
        elif line.startswith('CIRCUITNAME='):
            name = line[12:]
        else:
            point=line.split(' ')
            h, m = map(int, point[0].split('.'))
            time.append(h * 3600 + m * 60)
            loadCurrent.append(float(point[2]))
            surfaceTemp.append(float(point[3]))
    return {'name': name,
            'surfaceTemp': np.array(surfaceTemp),
            'loadCurrent': np.array(loadCurrent),
            'time': np.array(time)}

def parseAmbient(it, header):
    ambient = []
    for line in it:
        line = line.strip()
        if not line:
            break
        point=line.split(' ')
        ambient.append(float(point[2]))
    return np.array(ambient)

def load_ci(filepath):
    fileObj=open(filepath, 'r')
    circuits = {}
    ambient = None
    for line in fileObj:
        line = line.strip()                  # remove \n from end of line
        if not line:                         # skip empty lines
            continue
        if line.startswith('[CIRCUIT'):
            circuit = parseCircuit(fileObj, line)
            circuits[circuit['name']] = circuit
        elif line.startswith('[AMBIENT'):
            ambient = parseAmbient(fileObj, line)
    return circuits, ambient

print load_ci('test.ci')

rawDescription: Need help to read a file in python

description: Need help to read a file in python


code:
for item in errors_list:
    print format(item, right_align)

rawDescription: What is the pythonic way to print values right aligned?


code:
import re
pattern = re.compile(r"([0-9]+?)\1+")
print pattern.search("1110111011").group()

rawDescription: Python Regex to find reccuring patterns or binary strings


code:
#!/usr/bin/env python

import sys, re

# states for our state machine:
OUTSIDE = 0
INSIDE = 1
INSIDE_AFTER_STATUTE = 2

def eachCite(stream):
  state = OUTSIDE
  for lineNumber, line in enumerate(stream):
    if state in (INSIDE, INSIDE_AFTER_STATUTE):
      capture += line
    if re.match('^-CITE-', line):
      if state == OUTSIDE:
        state = INSIDE
        capture = line
      elif state in (INSIDE, INSIDE_AFTER_STATUTE):
        raise Exception("-CITE- in -CITE-??", lineNumber)
      else:
        raise NotImplementedError(state)
    elif re.match('^-End-', line):
      if state == OUTSIDE:
        raise Exception("-End- without -CITE-??", lineNumber)
      elif state == INSIDE:
        yield False, capture
        state = OUTSIDE
      elif state == INSIDE_AFTER_STATUTE:
        yield True, capture
        state = OUTSIDE
      else:
        raise NotImplementedError(state)
    elif re.match('^-STATUTE-', line):
      if state == OUTSIDE:
        raise Exception("-STATUTE- without -CITE-??", lineNumber)
      elif state == INSIDE:
        state = INSIDE_AFTER_STATUTE
      elif state == INSIDE_AFTER_STATUTE:
        raise Exception("-STATUTE- after -STATUTE-??", lineNumber)
      else:
        raise NotImplementedError(state)
  if state != OUTSIDE:
    raise Exception("EOF in -CITE-??")

for withStatute, cite in eachCite(sys.stdin):
  if withStatute:
    print "found cite with statute:"
    print cite

rawDescription: Extracting parts of text between specific delimiters from a large text file with custom delimiters and writing it to another file using Python


rawDescription: Determine a,b,c of quadratic equation using data points


code:
prodemployees = ['prd1 employe4 employe2', ' prd2 employe2']
for prod_emp in prodemployees:
    prod_emp = prod_emp.split()
    prod, emp = prod_emp[0], prod_emp[1:]
    print prod, emp

rawDescription: Python : split a list dynamically

description: Python : split a list dynamically


code:
import time

s = '2012-06-21 20:36:11'

t = time.strptime(s, '%Y-%m-%d %H:%M:%S')
print time.strftime('%d %B %Y %H:%M', t)

rawDescription: Format datestamp in python

description: Format datestamp in python


code:
import networkx as nx
G = nx.Graph()
G.add_node(1)
G.add_nodes_from('spam')
print G.nodes()

rawDescription: What is the most efficient way to move/rename a node in NetworkX?