code:
import SocketServer
import SimpleHTTPServer

import requests
import multiprocessing

# Variables
PORT = 8000
URL = 'localhost:{port}'.format(port=PORT)

# Setup simple sever
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "Serving at port", PORT

# start the server as a separate process
server_process = multiprocessing.Process(target=httpd.serve_forever)
server_process.daemon = True
server_process.start()

# Getting HTML from the target page
values = {
    'name': 'Thomas Anderson',
    'location': 'unknown'
}

r = requests.post(URL, data=values)
r.text

# stop the server
server_process.terminate()

rawDescription: Python: run SimpleHTTPServer and make request to it in a script


code:
import random
my_dict = {'key1': 'val1', 'key2': 'val2', 'key3': 'val3', 'key4': 'val4'}
rand_key = random.choice(my_dict.keys())
user_input = raw_input('Guess...')

if user_input == my_dict[rand_key]:
    print 'You guessed it!'
else:
    print 'Better luck next time!'

rawDescription: Python random.choice

description: Python random.choice


code:
import win32evtlog
import win32api
import win32con
import win32security # To translate NT Sids to account names.

import win32evtlogutil

def ReadLog(computer, logType="Application", dumpEachRecord = 0):
    # read the entire log back.
    h=win32evtlog.OpenEventLog(computer, logType)
    numRecords = win32evtlog.GetNumberOfEventLogRecords(h)
#       print "There are %d records" % numRecords

    num=0
    while 1:
        objects = win32evtlog.ReadEventLog(h, win32evtlog.EVENTLOG_BACKWARDS_READ|win32evtlog.EVENTLOG_SEQUENTIAL_READ, 0)
        if not objects:
            break
        for object in objects:
            # get it for testing purposes, but dont print it.
            msg = win32evtlogutil.SafeFormatMessage(object, logType)
            if object.Sid is not None:
                try:
                    domain, user, typ = win32security.LookupAccountSid(computer, object.Sid)
                    sidDesc = "%s/%s" % (domain, user)
                except win32security.error:
                    sidDesc = str(object.Sid)
                user_desc = "Event associated with user %s" % (sidDesc,)
            else:
                user_desc = None
            if dumpEachRecord:
                print "Event record from %r generated at %s" % (object.SourceName, object.TimeGenerated.Format())
                if user_desc:
                    print user_desc
                try:
                    print msg
                except UnicodeError:
                    print "(unicode error printing message: repr() follows...)"
                    print repr(msg)

        num = num + len(objects)

    if numRecords == num:
        print "Successfully read all", numRecords, "records"
    else:
        print "Couldn't get all records - reported %d, but found %d" % (numRecords, num)
        print "(Note that some other app may have written records while we were running!)"
    win32evtlog.CloseEventLog(h)

def usage():
    print "Writes an event to the event log."
    print "-w : Dont write any test records."
    print "-r : Dont read the event log"
    print "-c : computerName : Process the log on the specified computer"
    print "-v : Verbose"
    print "-t : LogType - Use the specified log - default = 'Application'"

def test():
    # check if running on Windows NT, if not, display notice and terminate
    if win32api.GetVersion() & 0x80000000:
        print "This sample only runs on NT"
        return

    import sys, getopt
    opts, args = getopt.getopt(sys.argv[1:], "rwh?c:t:v")
    computer = None
    do_read = do_write = 1

    logType = "Application"
    verbose = 0

    if len(args)>0:
        print "Invalid args"
        usage()
        return 1
    for opt, val in opts:
        if opt == '-t':
            logType = val
        if opt == '-c':
            computer = val
        if opt in ['-h', '-?']:
            usage()
            return
        if opt=='-r':
            do_read = 0
        if opt=='-w':
            do_write = 0
        if opt=='-v':
            verbose = verbose + 1
    if do_write:
        ph=win32api.GetCurrentProcess()
        th = win32security.OpenProcessToken(ph,win32con.TOKEN_READ)
        my_sid = win32security.GetTokenInformation(th,win32security.TokenUser)[0]

        win32evtlogutil.ReportEvent(logType, 2,
            strings=["The message text for event 2","Another insert"],
            data = "Raw\0Data".encode("ascii"), sid = my_sid)
        win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_WARNING_TYPE,
            strings=["A warning","An even more dire warning"],
            data = "Raw\0Data".encode("ascii"), sid = my_sid)
        win32evtlogutil.ReportEvent(logType, 1, eventType=win32evtlog.EVENTLOG_INFORMATION_TYPE,
            strings=["An info","Too much info"],
            data = "Raw\0Data".encode("ascii"), sid = my_sid)
        print("Successfully wrote 3 records to the log")

    if do_read:
        ReadLog(computer, logType, verbose > 0)

if __name__=='__main__':
    test()

rawDescription: Reading windows event log in Python using pywin32 (win32evtlog module)


code:
import random

# Define your list
l = []
for x in range(1,100):
    q=random.randint(0,99)
    w=random.randint(0,99)
    e=q*w
    # Append the result to your list
    l.append(e)
    print '%s * %s = %s'%(q,w,e)

print 'The list becomes: %s' % l

rawDescription: Storing items in a list for python

description: Storing items in a list for python


code:
n1=input('Enter first number ')
n2=input('Enter second number ')
print sum(range(min(n1,n2)+1,max(n1,n2)))

rawDescription: How to add a set of numbers that the user inputted in Python?

description: Add a set of numbers that the user inputted in Python


rawDescription: Python combine multiple lists of tuples

description: Python combine multiple lists of tuples


code:
def negationDetection(tweet):
    emoticonList = ['❤']
    for i in emoticonList:
        if tweet.find(i) == -1:
            continue
        else:
            print "Found", i

a = "It's a good thing I didn't apply anywhere else.. American University 2020 ❤❤ URL"
b = negationDetection(a)

rawDescription: how to detect twitter emoji that are typed continuly, in python

description: Detect twitter emoji that are typed continuly, in python


code:
import Tkinter as Tk

root = Tk.Tk()

def submit():
    print "entered text were " + entry.get()

entry = Tk.Entry(root)
entry.pack()
button = Tk.Button(root,text='submit',command=submit)
button.pack()

root.mainloop()

rawDescription: How to make a simple GUI in python without using any tools such as Tkinter, Eclipse etc?

description: Make a simple GUI in python without using any tools such as Tkinter, Eclipse etc


code:
import codecs
f = codecs.open('unicode.rst', encoding='utf-8')
for line in f:
    print repr(line)

rawDescription: How do I define strings read from a file as Unicode?

description: Define strings read from a file as Unicode


code:
x = [1,2,3,4,5]
for i in xrange(len(x)):
    for j, xi in enumerate(x):
        if i == j:
            print xi,
        else:
            print -xi,
    print

rawDescription: print line in specific order

description: Print line in specific order


code:
import sys

sys.stdout = open('out.dat', 'w')
print "Hello"

sys.stdout.close()

rawDescription: Python capture all printed output

description: Python capture all printed output


code:
from Tkinter import *
def start():
    #something
    save_place = save_location.get()
    website_url = url.get()
    print save_place,website_url
app = Tk()
top_app = Frame(app)
top_app.pack()
save_location = Entry(top_app, width=20)
url = Entry(top_app, width=20)
save_location.grid(sticky=W, row=0)
url.grid(sticky=W, row=1)
button_start = Button(top_app, text="Start", fg="green", command=start)
button_start.grid(sticky=W, row=2, pady=20)
app.mainloop()

rawDescription: I need a to send data to script with submit button


code:
import multiprocessing as mp

def writeToFile(conn):
    with open(filename, "a") as f: # Just leave the file open for performance reasons.
        while True:
            currentPos = conn.recv()
            f.write("{}\n".format(currentPos))

if __name__ == "__main__":
    parent_conn, child_conn = mp.Pipe()
    p = mp.Process(target=writeToFile, args=(child_conn,))
    p.start()
    while True:
        currentPos = encoder.readEncoderPos()
        parent_conn.send(currentPos)
        if currentPos < minCorrectPos or currentPos > maxCorrectPos:
            print 'Lever is out of range, wrong trial'
            writeData(timestamp)
        if time.time()-t_trialBegin > timeToHoldLever:
            print 'Lever has been held for enough time within correct range of positions. Rewarding pe'
            break

rawDescription: Most efficient way to record data and simultaneously check for conditions?


code:
#   reset  val  desired_col
#0      0    1            1
#1      0    5            6
#2      0    4           10
#3      1    2            2
#4      1   -1           -1
#5      0    6            5
#6      0    4            9
#7      1    2            2
df['cumsum'] = df['reset'].cumsum()
#cumulative sums of groups to column des
df['des']= df.groupby(['cumsum'])['val'].cumsum()
print df
#   reset  val  desired_col  cumsum  des
#0      0    1            1       0    1
#1      0    5            6       0    6
#2      0    4           10       0   10
#3      1    2            2       1    2
#4      1   -1           -1       2   -1
#5      0    6            5       2    5
#6      0    4            9       2    9
#7      1    2            2       3    2
#remove columns desired_col and cumsum
df = df.drop(['desired_col', 'cumsum'], axis=1)
print df
#   reset  val  des
#0      0    1    1
#1      0    5    6
#2      0    4   10
#3      1    2    2
#4      1   -1   -1
#5      0    6    5
#6      0    4    9
#7      1    2    2

rawDescription: Pandas dataframe - running sum with reset


code:
item= self.tree.GetSelection()
self.tree.SetPyData(item,{"Source":"C:\hi.png","Opacity":1})
print self.tree.GetPyData(item)

rawDescription: Getting selection of wx.treectrl

description: Getting selection of wx.treectrl


code:
from bs4 import BeautifulSoup

data = """

    100 Days of English File

"""

soup = BeautifulSoup(data)
for resource in soup.find_all('a'):
    if '/mod/resource/view.php?id=' in resource.get('href'):
        src = resource.img.get('src')
        print src.split('/')[-1]

rawDescription: How do you read contents of adjacent html element using Python and BS4?

description: Read contents of adjacent html element using Python and BS4


code:
import xml.sax
import xml.sax.handler

class PseudoStream(object):
    def read_iterator(self):
        yield '<foo>'
        yield '<bar>'
        for line in open('test.xml'):
            yield line
        yield '</bar>'
        yield '</foo>'

    def __init__(self):
        self.ri = self.read_iterator()

    def read(self, *foo):
        try:
            return self.ri.next()
        except StopIteration:
            return ''

class SAXHandler(xml.sax.handler.ContentHandler):
    def startElement(self, name, attrs):
        print name, attrs

d = xml.sax.parse(PseudoStream(), SAXHandler())

rawDescription: Parsing large pseudo-xml files in python


code:
import heapq, time, threading

START_TIME = time.time()
SERIALIZE_STDOUT = threading.Lock()
def consumer(message):
    """the actual work function.  nevermind the locks here, this just keeps
       the output nicely formatted.  a real work function probably won't need
       it, or might need quite different synchronization"""
    SERIALIZE_STDOUT.acquire()
    print time.time() - START_TIME, message
    SERIALIZE_STDOUT.release()

def produce(work_queue, condition, timeout, message):
    """called to put a single item onto the work queue."""
    prio = time.time() + float(timeout)
    condition.acquire()
    heapq.heappush(work_queue, (prio, message))
    condition.notify()
    condition.release()

def worker(work_queue, condition):
    condition.acquire()
    stopped = False
    while not stopped:
        now = time.time()
        if work_queue:
            prio, data = work_queue[0]
            if data == 'stop':
                stopped = True
                continue
            if prio < now:
                heapq.heappop(work_queue)
                condition.release()
                # do some work!
                consumer(data)
                condition.acquire()
            else:
                condition.wait(prio - now)
        else:
            # the queue is empty, wait until notified
            condition.wait()
    condition.release()

if __name__ == '__main__':
    # first set up the work queue and worker pool
    work_queue = []
    cond = threading.Condition()
    pool = [threading.Thread(target=worker, args=(work_queue, cond))
            for _ignored in range(4)]
    map(threading.Thread.start, pool)

    # now add some work
    produce(work_queue, cond, 10, 'Grumpy')
    produce(work_queue, cond, 10, 'Sneezy')
    produce(work_queue, cond, 5, 'Happy')
    produce(work_queue, cond, 10, 'Dopey')
    produce(work_queue, cond, 15, 'Bashful')
    time.sleep(5)
    produce(work_queue, cond, 5, 'Sleepy')
    produce(work_queue, cond, 10, 'Doc')

    # and just to make the example a bit more friendly, tell the threads to stop after all
    # the work is done
    produce(work_queue, cond, float('inf'), 'stop')
    map(threading.Thread.join, pool)

rawDescription: How to efficiently do many tasks a "little later" in Python?

description: Efficiently do many tasks a "little later" in Python


code:
import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen("http://www.icc-ccs.org/prc/piracyreport.php")
soup = BeautifulSoup(page)
for incident in soup('td', width="90%"):
    where, linebreak, what = incident.contents[:3]
    print where.strip()
    print what.strip()
    print

rawDescription: How to scan a webpage and get images and youtube embeds?

description: Scan a webpage and get images and youtube embeds


code:
import re,requests
raw = requests.get('http://www.geoiptool.com/').text
latlon = re.search("GPoint\(([^)]+)\)",raw).groups(0)
lat,lon = map(float,latlon[0].split(","))
print "Latitude:%s   Longitude:%s"%(lat,lon)

rawDescription: Getting map location Python

description: Getting map location Python


code:
def fun1(val1, val2):
    val2 -= 1
    if val2 > 0:
        print "val2: ", val2
        return fun2(val1, val2)
    else:
        print "val2 ended"
        return val1

def fun2(val1, val2):
    val1 -= 1
    if val1 > 0:
        print "val1: ", val1
        return fun1(val1, val2)
    else:
        print "val1 ended"
        return val2

blah = fun1(10,8)
print blah

rawDescription: returning values from python functions

description: Returning values from python functions


code:
import simplejson

from sqlobject import *

# Replace this with the URI for your actual database
connection = connectionForURI('sqlite:/:memory:')
sqlhub.processConnection = connection

# This defines the columns for your database table. See SQLObject docs for how it
# does its conversions for class attributes <-> database columns (underscores to camel
# case, generally)

class Song(SQLObject):

    name = StringCol()
    artist = StringCol()
    album = StringCol()

# Create fake data for demo - this is not needed for the real thing
def MakeFakeDB():
    Song.createTable()
    s1 = Song(name="B Song",
              artist="Artist1",
              album="Album1")
    s2 = Song(name="A Song",
              artist="Artist2",
              album="Album2")

def Main():
    # This is an iterable, not a list
    all_songs = Song.select().orderBy(Song.q.name)

    songs_as_dict = []

    for song in all_songs:
        song_as_dict = {
            'name' : song.name,
            'artist' : song.artist,
            'album' : song.album}
        songs_as_dict.append(song_as_dict)

    print simplejson.dumps(songs_as_dict)

if __name__ == "__main__":
    MakeFakeDB()
    Main()

rawDescription: return SQL table as JSON in python

description: Return SQL table as JSON in python


code:
import os
print os.path.splitext('/home/user/somefile.txt')[0]+'.jpg'

rawDescription: How to replace (or strip) an extension from a filename in Python?

description: Replace (or strip) an extension from a filename in Python


code:
import urllib.error
import urllib.request
import time

imageURL = 'http://YOUR-WEB-SITE.com/imageName.jpg'

directoryForImages = '/where/you/want/to/store/image/'
imageBaseName = 'basename'
extension = '.jpg'

maxLoops = 100

for imageNumber in range(0, maxLoops):
    try:
        # Open a file object for the webpage where you will get the image
        f = urllib.request.urlopen(imageURL)
        # Open the local file where you will store the image
        imageF = open('{0}{1}{2}{3}'.format(directoryForImages, imageBaseName, imageNumber, extension), 'wb')
        # Write the image to the local file
        imageF.write(f.read())

        # Clean up
        imageF.close()
        f.close()
    except urllib.error.HTTPError:   # The 'except' block executes if an HTTPError is thrown by the try block, then the program continues as usual.
        print "Image fetch failed.  Waiting another 15 seconds..."

    time.sleep(15)

rawDescription: Download Time Lapse Images in Python

description: Download Time Lapse Images in Python


rawDescription: Value comparison

description: Value comparison


code:
ctr = 0;
for line in text:
    ctr = ctr + 1
    if ctr > 1000:
        break
    if line.startswith('MH =') or line.startswith('MN ='):
        value, key = line.strip().split('=')
        res[key] = value
        if num < 1000:
            print '%s : %s' % (key, value)
        num = num + 1

rawDescription: Creating key value pairs from file

description: Creating key value pairs from file


code:
docs = [('0,1,12','doc1'),('0,2,30','doc1'),('0,1','doc2')]
keys = [('0','key1'),('0,1','key2'),('0,1,12','key3'),('0,2','key4'),('0,2,30','key5')]

keydict = dict(keys)
resultDict = {}

for doc in docs:
    (path, docname) = doc
    pathList = path.split(',')
    keyPath = []
    for i in range(0, len(pathList)):
        aPath = ','.join(pathList[:i+1])
        keyPath.append(keydict[aPath])

    if docname not in resultDict :
        resultDict[docname] = []
    resultDict[docname].append(tuple(keyPath))

print resultDict

rawDescription: Matching hierarchical keywords to a document


code:
with open("luawrite", "r") as f:
    for line in f:
        hashes = line.split()
        ckey = hashes[1]
        val1 = int(hashes[2])
        val2 = int(hashes[3])
        a,b = result[ckey]
        result[ckey] = (a+val1, b+val2)
print result

rawDescription: manipulating two values of a key in dictionary at the same time

description: Manipulating two values of a key in dictionary at the same time


code:
str = '192.168.1.1'
str = str.replace('.','\.')
str
print str

rawDescription: str.replace adding back slash

description: Str.replace adding back slash


code:
if isinstance(var, int):
  print "Int"
elif isinstance(var, str):
  print "Str"
else:
  print "Other:", type(var)

rawDescription: Telling what type of data is what, Python


code:
import numpy as np
import ctypes
FE_TONEAREST = 0x0000
FE_DOWNWARD = 0x0400
FE_UPWARD = 0x0800
FE_TOWARDZERO = 0x0c00
libc = ctypes.CDLL('libc.dylib')

v = 1. / (1<<23)
print repr(np.float32(1+v) - np.float32(v/2)) # prints 1.0
libc.fesetround(FE_UPWARD)
print repr(np.float32(1+v) - np.float32(v/2)) # prints 1.0000002

rawDescription: Tie breaking of round with numpy

description: Tie breaking of round with numpy


code:
#!/usr/bin/python
import re

star = r"[^\/]+"
doubleStar = r".*"
slash = r"\/"
questionMark = r"\w"
dot = r"\."

antPath = "foo/*/bar/**.ex?"
expectedPath = r"foo\/[^\/]+\/bar\/.*\.ex\w"

# Apply transformation
output = antPath.replace(r"/", slash).replace(r".", dot)
output = re.sub(r"(?<!\*)\*(?!\*)", star, output)
output = output.replace(r"**", doubleStar)
output = output.replace(r"?", questionMark)

if (output == expectedPath):
    print "Success!"
else:
    print "Failure..."
print "filteredPath: ", output
print "expectedPath: ", expectedPath

rawDescription: Regex matching an Ant-path

description: Regex matching an Ant-path


code:
def is_drive_ready(drive_name):
    try:
        win32api.GetVolumeInformation(drive_name)
        return True
    except:
        return False

print 'ready:', is_drive_ready('c:\\') # true
print 'ready:', is_drive_ready('d:\\') # false (on my system)

rawDescription: Python Win32 - equivalent function to DriveInfo.IsReady


code:
import glob
for name in glob.glob('testing*08-05.txt'):
    print name

rawDescription: How to find all files in current directory with filenames that match a certain pattern in python?

description: Find all files in current directory with filenames that match a certain pattern in python


code:
import wx

########################################################################
class Car:
    """"""

    #----------------------------------------------------------------------
    def __init__(self, id, model, make, year):
        """Constructor"""
        self.id = id
        self.model = model
        self.make = make
        self.year = year

########################################################################
class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        ford = Car(0, "Ford", "F-150", "2008")
        chevy = Car(1, "Chevrolet", "Camaro", "2010")
        nissan = Car(2, "Nissan", "370Z", "2005")

        sampleList = []
        lb = wx.ListBox(panel,
                        size=wx.DefaultSize,
                        choices=sampleList)
        self.lb = lb
        lb.Append(ford.make, ford)
        lb.Append(chevy.make, chevy)
        lb.Append(nissan.make, nissan)
        lb.Bind(wx.EVT_LISTBOX, self.onSelect)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(lb, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onSelect(self, event):
        """"""
        print "You selected: " + self.lb.GetStringSelection()
        obj = self.lb.GetClientData(self.lb.GetSelection())
        text = """
        The object's attributes are:
        %s  %s    %s  %s

        """ % (obj.id, obj.make, obj.model, obj.year)
        print text

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

rawDescription: How to add extra data to wxListBox items

description: Add extra data to wxListBox items


rawDescription: Write a regular expression to get a substring in Python


code:
#!/usr/bin/env python
from itertools import combinations

import igraph
import optparse

parser = optparse.OptionParser(usage="%prog [options] infile")
parser.add_option("-k", metavar="K", default=3, type=int,
        help="use a clique size of K")

options, args = parser.parse_args()

if not args:
    parser.error("Required input file as first argument")

k = options.k
g = igraph.load(args[0], format="ncol", directed=False)
cls = map(set, g.maximal_cliques(min=k))

edgelist = []
for i, j in combinations(range(len(cls)), 2):
    if len(cls[i].intersection(cls[j])) >= k-1:
        edgelist.append((i, j))

cg = igraph.Graph(edgelist, directed=False)
clusters = cg.clusters()
for cluster in clusters:
    members = set()
    for i in cluster:
        members.update(cls[i])
    print "\t".join(g.vs[members]["name"])

rawDescription: Overlapping community detection with igraph or other libaries


code:
def mail(receiver,Message):
    import smtplib
    try:
        s=smtplib.SMTP()
        s.connect("smtp.gmail.com",465)
        s.ehlo()
        s.starttls()
        s.ehlo()
        s.login("email@gmail.com", "password")
        s.sendmail("email@gmail.com", receiver, Message)
    except Exception,R:
            return R

rawDescription: SMTP AUTH extension not supported by server in python 2.4


code:
import glob
import json

# read the whole folder
read_files = glob.glob("bus_stop_1012/*.json")

with open("bus_stop_1012/bus_arrival_1012.json", "wb") as outfile:
    # this is the beginning of the combined file
    outfile.write('{"data": [\n')
    sep = ''
    for f in read_files:
        # will append each data file
        with open(f) as infile:
             try:
                for obj in json.load(infile)['data']:
                    outfile.write(sep)
                    json.dump(obj, outfile)
                    sep = ','
             except ValueError:
                 print 'Failed to load {}'.format(f)
    outfile.write(']}')

rawDescription: How to remove first few characters from every 1st line of each json file

description: Remove first few characters from every 1st line of each json file


code:
import lxml.html
root = lxml.html.fromstring('''

      link2nd Mar 2011
XYZ Consultancy Ltd

....

''')
for br_with_tail in root.cssselect('table.results > tr > td > a + span + br'):
    print br_with_tail.tail
    # => XYZ Consultancy Ltd

rawDescription: HTML parsing using lxml code

description: HTML parsing using lxml code


code:
from Tkinter import *

root = Tk()

# Create a dictionary where the keys are the checkbuttons
# and the values are their BooleanVars
buttons = dict()
for _ in xrange(10):
    buttons[Checkbutton(root)] = BooleanVar()

for button in buttons:
    # Place each button on the window
    button.grid()
    # Hook each button up to its BooleanVar
    button.config(variable=buttons[button])
    # Set each BooleanVar to True
    buttons[button].set(True)

# I printed this just to show what is going on
print buttons

root.mainloop()

rawDescription: 'Check all boxes' Check box in Python

description: 'Check all boxes' Check box in Python


code:
import re

types = {
    'WORD': r'\w+',
    'NUMBER': r'\d+',
    # todo: extend me
}

def compile(pat):
    return re.sub(r'%{(\w+):(\w+)}',
        lambda m: "(?P<" + m.group(2) + ">" + types[m.group(1)] + ")", pat)

rr = compile("%{WORD:method} %{NUMBER:bytes} %{NUMBER:duration}")

print re.search(rr, "hello 123 456").groupdict()
# {'duration': '456', 'bytes': '123', 'method': 'hello'}

rawDescription: Is there any Python implementation of logstash's grok functionality?


rawDescription: Extract single True/False value from JSON Twitter response ( Tweepy and Python )


code:
import re
import foo as module # XXX use the correct module name here!

module_name = module.__name__
import_line = 'from %s import (%%s)' % module_name
length = len(import_line) - 3
print import_line % (',\n' + length * ' ').join([a for a in dir(module)
                                                               if not re.match('__.*[^_]{2}', a)])

rawDescription: Tool to help eliminate wildcard imports

description: Tool to help eliminate wildcard imports


code:
import wx
from Queue import Queue, Empty, Full
import threading
from time import sleep
from math import sin
from itertools import count

class Producer(threading.Thread):
    def __init__(self,queue):
        self.queue=queue
        self.t=0
        threading.Thread.__init__(self)
        self.daemon=False

    def run(self):
        print "initializing producer"
        freq=0.1
        secondsBetweenReadings=1.0/32
        try:
            while True:
                readings=[ 256.0*sin(freq*t) for t in range(self.t,self.t+4) ]
                self.t = self.t+4
                self.queue.put(readings,timeout=0.1)
                sleep(secondsBetweenReadings)
        except Full:
            print "Queue Full. Exiting"

class App(wx.App):
    def __init__(self,queue):
        self.queue=queue
        wx.App.__init__(self,redirect=False)

    def OnInit(self):
        self.frame=wx.Frame(parent=None,size=(256,256))
        self.plotPanel=wx.Panel(self.frame,size=(256,256))
        self.plotPanel.SetBackgroundColour(wx.BLACK)
        self.data=[]
        self.plotPanel.Bind(wx.EVT_PAINT,self.OnPaintPlot)
        self.plotPanel.Bind(wx.EVT_ERASE_BACKGROUND,lambda evt: None) #For Windows
        self.Bind(wx.EVT_IDLE,self.CheckForData)
        self.frame.Show()
        return True

    def CheckForData(self,evt):
        try:
            data=self.queue.get(timeout=0.05)
            self.data.extend(data)
            self.plotPanel.Refresh()
        except Empty:
            pass
        evt.RequestMore()

    def OnPaintPlot(self,evt):
        w,h=self.plotPanel.GetClientSize()
        dc=wx.PaintDC(self.plotPanel)
        dc.SetBrush(wx.BLACK_BRUSH)
        dc.DrawRectangle(0,0,w,h)
        dc.SetPen(wx.WHITE_PEN)
        coords=zip(count(),self.data)
        if len(coords) > 2:
            dc.DrawLines(coords)

if __name__ == "__main__":
    maxReadings=32
    queue=Queue(maxReadings)
    producer=Producer(queue)
    plotterApp=App(queue)

    producer.start()
    plotterApp.MainLoop()

rawDescription: Plotting changing arrays

description: Plotting changing arrays


code:
import filecmp
compare = filecmp.dircmp( "C:/", "D:/" )
for f in compare.left_only:
    print "C: new", f
for f in compare.right_only:
    print "D: new", f

rawDescription: How to test a folder for new files using python

description: Test a folder for new files using python


code:
print 'Uudet L\xc3\xb6yt\xc3\xb6retket'
print 'Uudet L\xc3\xb6yt\xc3\xb6retket'.decode('utf-8')

rawDescription: Python - writing unicode strings to a file & beautiful soup


code:
import traceback
def my_func():
    my_other_func()

def my_other_func():
    my_third()

def my_third():
    print "Stack"
    traceback.print_stack()
    print "Extracted"
    print repr(traceback.extract_stack())

my_func()

rawDescription: How to see complete traceback?

description: See complete traceback


code:
from time import strptime

def validate(date):
    try:
        return strptime(date, '%Y-%m-%d')
    except ValueError:
        return

while True:
    date = validate(raw_input("please input date:"))
    if not date:
        print "sorry not a date try again."
    else:
        break

rawDescription: I'm making a python function to check if input is a date


code:
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print line

rawDescription: How to delete all blank lines in the file with the help of python?

description: Delete all blank lines in the file with the help of python


code:
import re
p = re.compile(ur'(\w)\1\1\1+')
test_str = u'aaaaaadgfdhgfbbbbgggee)ehje ekekkkkkeefh ehfeff88888éh\"hr ekkKKKKK\njhxjhjhhhhh\nthe the\n\"eeeeeeeeaaaabcaaaaadexxxdddzdzzzz\"\n\"eeeeeeeeaaaabcaaaaadexxxdddzdzzzz\"\n\"eeeeeeeeaaaabcaaaaadexxxdddzdzzzz\"\n\"aaaeeeeeeeeaaaadzxzzzzffffrrrr\"\n\"aaa\"'
print ([x.group(0) for x in p.finditer(test_str)])

rawDescription: Python re module findall and print the entire match substring


code:
import bitarray

class BoolArray(object):

  # create from an ndarray
  def __init__(self, array):
    ba = bitarray.bitarray()
    ba.pack(array.tostring())
    self.arr = ba.tostring()
    self.shape = array.shape
    self.size = array.size

  # convert back to an ndarray
  def to_array(self):
    ba = bitarray.bitarray()
    ba.fromstring(self.arr)
    ret = np.fromstring(ba.unpack(), dtype=np.bool)[:self.size]
    return ret.reshape(self.shape)

  def __cmp__(self, other):
    return cmp(self.arr, other.arr)

  def __hash__(self):
    return hash(self.arr)

import numpy as np

x = (np.random.random((2,3,2))>0.5)
b1 = BoolArray(x)
b2 = BoolArray(x)
d = {b1: 12}
d[b2] += 1
print d
print b1.to_array()

rawDescription: Efficient serialization of numpy boolean arrays


code:
class KeyRememberer(type):

    def __new__(meta, classname, bases, classDict):
        cls = type.__new__(meta, classname, bases, classDict)

        # Define init that creates the set of remembered keys
        def __init__(self, *args, **kwargs):
            self.memory = set()
            return super(cls, self).__init__(*args, **kwargs)
        cls.__init__ = __init__

        # Decorator that stores a requested key in the cache
        def remember(f):
            def _(self, key, *args, **kwargs):
                self.memory.add(key)
                return f(self, key, *args, **kwargs)
            return _

        # Apply the decorator to each of the default implementations
        for method_name in [  '__getitem__', '__contains__', 'get' ]:
            m = getattr(cls, method_name)
            setattr(cls, method_name, remember(m))

        return cls

class DictWithMemory(dict):

    # A metaclass that ensures the object
    # has a set called 'memory' as an attribute,
    # which is updated on each call to __getitem__,
    # __contains__, or get.
    __metaclass__ = KeyRememberer

    def unused_keys(self):
        """
        Returns the list of unused keys.
        """
        print "Used", self.memory
        return list(set(super(DictWithMemory,
                              self).keys()).difference(self.memory))

rawDescription: Python dictionary with memory of keys that were accessed?


code: print "b%sa%sc%sk%ss%sl%sa%ss%sh%s " % bs

rawDescription: "\b" acting improperly in string

description: "\b" acting improperly in string


code:
def fib():
    a = 0
    b = 1
    while True:
        yield a
        a = b
        b += a
        if a > 100:
            raise StopIteration
for value in fib():
    print value
# assign the resulting object to 'generator'
generator = fib()
generator.next()
generator.next()
for value in generator:
    print value

rawDescription: What is the "yield" statement in a Python function?


code:
import re

class ParseError(Exception):
    pass

# Tokenize a string.
# Tokens yielded are of the form (type, string)
# Possible values for 'type' are '(', ')' and 'WORD'
def tokenize(s):
    toks = re.compile(' +|[A-Za-z]+|[()]')
    for match in toks.finditer(s):
        s = match.group(0)
        if s[0] == ' ':
            continue
        if s[0] in '()':
            yield (s, s)
        else:
            yield ('WORD', s)

# Parse once we're inside an opening bracket.
def parse_inner(toks):
    ty, name = next(toks)
    if ty != 'WORD': raise ParseError
    children = []
    while True:
        ty, s = next(toks)
        if ty == '(':
            children.append(parse_inner(toks))
        elif ty == ')':
            return (name, children)

# Parse this grammar:
# ROOT ::= '(' INNER
# INNER ::= WORD ROOT* ')'
# WORD ::= [A-Za-z]+
def parse_root(toks):
    ty, _ = next(toks)
    if ty != '(': raise ParseError
    return parse_inner(toks)

def show_children(tree):
    name, children = tree
    if not children: return
    print '%s -> %s' % (name, ' '.join(child[0] for child in children))
    for child in children:
        show_children(child)

example = '( Root ( AB ( ABC ) ( CBA ) ) ( CD ( CDE ) ( FGH ) ) )'
show_children(parse_root(tokenize(example)))

rawDescription: How to parse parenthetical trees in python?

description: Parse parenthetical trees in python


code:
for i in range(len(vocab_list)-1):
    for j in range(len(sentence)-1):
        if(vocab_list[i]==sentence[j] and vocab_list[i+1]==sentence[j+1]):
            print "match"

rawDescription: How to access the next element in for loop in current index/iteration?

description: Access the next element in for loop in current index/iteration


code:
try:
    myfile = open("myfile.csv", "r+") # or "a+", whatever you need
except IOError:
    print "Could not open file! Please close Excel!"

with myfile:
    do_stuff()

rawDescription: check if a file is open in Python

description: Check if a file is open in Python


code:
def turnOn(self):
    self.isTurnedOn = True
    self.updateThread = threading.Thread(target=self.updateNeighborsList, args=())
    self.updateThread.daemon = True
    self.updateThread.start()

def updateNeighborsList(self):
    while self.isTurnedOn:
        self.neighbors=[]
        for candidate in points:
            distance = math.sqrt((candidate.X-self.X)**2 + (candidate.Y-self.Y)**2)
            if distance <= maxDistance and candidate!=self and candidate.isTurnedOn:
                self.neighbors.append(candidate)
        print self.neighbors
        print points
        time.sleep(1)

rawDescription: Endless loop in python class member function. Threads?


code:
import lxml.etree as ET
xmldata="""
<locations>

<continent region="Africa" >
    <country id="84" countryname="Algeria" ></country>
</continent>

<continent region="Asia" >
    <country id="84" countryname="India" ></country>
</continent>

<continent region="America" >
    <country id="84" countryname="Mexico" ></country>
</continent>

</locations>
"""
root = ET.fromstring(xmldata)
print root.xpath('//continent[./country/@countryname="India"]/@region')

rawDescription: Get parent node using child node lxml python


code:
import sip
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui

import systray_rc

window = Window()
window.show()

class Window(QtGui.QDialog):
    def __init__(self):
        super(Window, self).__init__()

        self.createIconGroupBox()
        self.createMessageGroupBox()

        self.iconLabel.setMinimumWidth(self.durationLabel.sizeHint().width())

        self.createActions()
        self.createTrayIcon()

        self.showMessageButton.clicked.connect(self.showMessage)
        self.showIconCheckBox.toggled.connect(self.trayIcon.setVisible)
        self.iconComboBox.currentIndexChanged.connect(self.setIcon)
        self.trayIcon.messageClicked.connect(self.messageClicked)
        self.trayIcon.activated.connect(self.iconActivated)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.iconGroupBox)
        mainLayout.addWidget(self.messageGroupBox)
        self.setLayout(mainLayout)

        self.iconComboBox.setCurrentIndex(1)
        self.trayIcon.show()

        self.setWindowTitle("Systray")
        self.resize(400, 300)

    def setVisible(self, visible):
        self.minimizeAction.setEnabled(visible)
        self.maximizeAction.setEnabled(not self.isMaximized())
        self.restoreAction.setEnabled(self.isMaximized() or not visible)
        super(Window, self).setVisible(visible)

    def closeEvent(self, event):
        if self.trayIcon.isVisible():
            QtGui.QMessageBox.information(self, "Systray",
                    "The program will keep running in the system tray. To "
                    "terminate the program, choose <b>Quit</b> in the "
                    "context menu of the system tray entry.")
            self.hide()
            event.ignore()

    def setIcon(self, index):
        icon = self.iconComboBox.itemIcon(index)
        self.trayIcon.setIcon(icon)
        self.setWindowIcon(icon)

        self.trayIcon.setToolTip(self.iconComboBox.itemText(index))

    def iconActivated(self, reason):
        if reason in (QtGui.QSystemTrayIcon.Trigger, QtGui.QSystemTrayIcon.DoubleClick):
            self.iconComboBox.setCurrentIndex(
                    (self.iconComboBox.currentIndex() + 1)
                    % self.iconComboBox.count())
        elif reason == QtGui.QSystemTrayIcon.MiddleClick:
            self.showMessage()

    def showMessage(self,text='test'):
        icon = QtGui.QSystemTrayIcon.MessageIcon(
                self.typeComboBox.itemData(self.typeComboBox.currentIndex()))
#        self.trayIcon.showMessage(self.titleEdit.text(),
#                self.bodyEdit.toPlainText(), icon,
#                self.durationSpinBox.value() * 1000)
        self.trayIcon.showMessage('stock alarm',
                'test', icon,
                self.durationSpinBox.value() * 1000)

    def messageClicked(self):
        QtGui.QMessageBox.information(None, "Systray",
                "Sorry, I already gave what help I could.\nMaybe you should "
                "try asking a human?")

    def createIconGroupBox(self):
        self.iconGroupBox = QtGui.QGroupBox("Tray Icon")

        self.iconLabel = QtGui.QLabel("Icon:")

        self.iconComboBox = QtGui.QComboBox()
        self.iconComboBox.addItem(QtGui.QIcon(':/images/bad.svg'), "Bad")
        self.iconComboBox.addItem(QtGui.QIcon(':/images/heart.svg'), "Heart")
        self.iconComboBox.addItem(QtGui.QIcon(':/images/trash.svg'), "Trash")

        self.showIconCheckBox = QtGui.QCheckBox("Show icon")
        self.showIconCheckBox.setChecked(True)

        iconLayout = QtGui.QHBoxLayout()
        iconLayout.addWidget(self.iconLabel)
        iconLayout.addWidget(self.iconComboBox)
        iconLayout.addStretch()
        iconLayout.addWidget(self.showIconCheckBox)
        self.iconGroupBox.setLayout(iconLayout)

    def createMessageGroupBox(self):
        self.messageGroupBox = QtGui.QGroupBox("Balloon Message")

        typeLabel = QtGui.QLabel("Type:")

        self.typeComboBox = QtGui.QComboBox()
        self.typeComboBox.addItem("None", QtGui.QSystemTrayIcon.NoIcon)
        self.typeComboBox.addItem(self.style().standardIcon(
                QtGui.QStyle.SP_MessageBoxInformation), "Information",
                QtGui.QSystemTrayIcon.Information)
        self.typeComboBox.addItem(self.style().standardIcon(
                QtGui.QStyle.SP_MessageBoxWarning), "Warning",
                QtGui.QSystemTrayIcon.Warning)
        self.typeComboBox.addItem(self.style().standardIcon(
                QtGui.QStyle.SP_MessageBoxCritical), "Critical",
                QtGui.QSystemTrayIcon.Critical)
        self.typeComboBox.setCurrentIndex(1)

        self.durationLabel = QtGui.QLabel("Duration:")

        self.durationSpinBox = QtGui.QSpinBox()
        self.durationSpinBox.setRange(5, 60)
        self.durationSpinBox.setSuffix(" s")
        self.durationSpinBox.setValue(15)

        durationWarningLabel = QtGui.QLabel("(some systems might ignore this "
                "hint)")
        durationWarningLabel.setIndent(10)

        titleLabel = QtGui.QLabel("Title:")

        self.titleEdit = QtGui.QLineEdit("Cannot connect to network")

        bodyLabel = QtGui.QLabel("Body:")

        self.bodyEdit = QtGui.QTextEdit()
        self.bodyEdit.setPlainText("Don't believe me. Honestly, I don't have "
                "a clue.\nClick this balloon for details.")

        self.showMessageButton = QtGui.QPushButton("Show Message")
        self.showMessageButton.setDefault(True)

        messageLayout = QtGui.QGridLayout()
        messageLayout.addWidget(typeLabel, 0, 0)
        messageLayout.addWidget(self.typeComboBox, 0, 1, 1, 2)
        messageLayout.addWidget(self.durationLabel, 1, 0)
        messageLayout.addWidget(self.durationSpinBox, 1, 1)
        messageLayout.addWidget(durationWarningLabel, 1, 2, 1, 3)
        messageLayout.addWidget(titleLabel, 2, 0)
        messageLayout.addWidget(self.titleEdit, 2, 1, 1, 4)
        messageLayout.addWidget(bodyLabel, 3, 0)
        messageLayout.addWidget(self.bodyEdit, 3, 1, 2, 4)
        messageLayout.addWidget(self.showMessageButton, 5, 4)
        messageLayout.setColumnStretch(3, 1)
        messageLayout.setRowStretch(4, 1)
        self.messageGroupBox.setLayout(messageLayout)

    def createActions(self):
        self.minimizeAction = QtGui.QAction("Mi&nimize", self,
                triggered=self.hide)

        self.maximizeAction = QtGui.QAction("Ma&ximize", self,
                triggered=self.showMaximized)

        self.restoreAction = QtGui.QAction("&Restore", self,
                triggered=self.showNormal)

        self.quitAction = QtGui.QAction("&Quit", self,
                triggered=QtGui.qApp.quit)

    def createTrayIcon(self):
         self.trayIconMenu = QtGui.QMenu(self)
         self.trayIconMenu.addAction(self.minimizeAction)
         self.trayIconMenu.addAction(self.maximizeAction)
         self.trayIconMenu.addAction(self.restoreAction)
         self.trayIconMenu.addSeparator()
         self.trayIconMenu.addAction(self.quitAction)

         self.trayIcon = QtGui.QSystemTrayIcon(self)
         self.trayIcon.setContextMenu(self.trayIconMenu)

def callback(ch, method, properties, body):
        print " [x] %r:%r" % (method.routing_key, body,)
        window.showMessage(body)

def subsribeRabbit():
    import pika
    import sys
    from util import settings

    connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=settings.RABBIT_SERVER))
    channel = connection.channel()

    channel.exchange_declare(exchange=settings.STOCK_ALARMS_TOPIC,
                             type='topic')

    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue

    binding_keys = '#'

    for binding_key in binding_keys:
        channel.queue_bind(exchange=settings.STOCK_ALARMS_TOPIC,
                           queue=queue_name,
                           routing_key=binding_key)

    print ' [*] Waiting for logs. To exit press CTRL+C'

    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)

    channel.start_consuming()

if __name__ == '__main__':

    import sys
    from cron.realtimemonitorschedule import startMonitor

    app = QtGui.QApplication(sys.argv)

    if not QtGui.QSystemTrayIcon.isSystemTrayAvailable():
        QtGui.QMessageBox.critical(None, "Systray",
                "I couldn't detect any system tray on this system.")
        sys.exit(1)

    QtGui.QApplication.setQuitOnLastWindowClosed(False)

    from threading import Thread
    thread = Thread(target = subsribeRabbit)
    thread.start()
    #thread.join()

    sys.exit(app.exec_())

rawDescription: How could i pass parameter to pika callback


code:
def enterguess(turncount):
    def guess(name):
        return (int(input("\n" + players[name] + ", Enter row number: ")),
                int(input(players[name] + ", Enter column number: ")))

    if turncount % 2 == 0:
        (row, col) = guess('player1')

        try:
            if board[row - 1][col - 1] in [sym['player1'], sym['player2']]:
                print "\n" + players['player2'] + " already took that spot! Please guess again."
                return enterguess(turncount)
        except:
            print "\nPlease enter the indexes between 1 and 3."
            return enterguess(turncount)

    else:
        (row, col) = guess('player2')

        try:
            if board[row - 1][col - 1] in [sym['player1'], sym['player2']]:
                print "\n" + players['player1'] + " already took that spot! Please guess again."
                return enterguess(turncount)
        except IndexError:
            print "\nPlease enter a number between 1 and 3."
            return nterguess(turncount)

    return (row - 1, col - 1)

rawDescription: Tic-Tac-Toe Game

description: Tic-Tac-Toe Game


code:
from datetime import date, timedelta

def generate_month_days(year, month):
    start_date = date(year, month, 01)

    cur_date = start_date
    while cur_date.month == start_date.month:
        yield cur_date
        cur_date += timedelta(days=1)

for d in generate_month_days(2015,11):
    print d

rawDescription: Days in month as date objects

description: Days in month as date objects


code:
#define the decorator, it accepts the function to be wrapped
def dec(f):
    #it returns a function that takes some arguments
    def wrapper(*args):
        s, = args  #take the first one (it should be self)
        print(s.dir) #do something
        result = f(*args) #call the wrapped function
        print(s.dir) #do something
        return result #return the result of the function
    #and returns the wrapped function
    return wrapper

class A:
    def __init__(self, dir):
        self.dir = dir

    @dec
    def p(self):
        print "P!"

a = A("ABC")
a.p()

rawDescription: Python 2: class scope

description: Python 2: class scope


code:
from sklearn.feature_extraction.image import extract_patches

data=np.array([
    [0,0,1,1,1,1],
    [1,0,0,1,1,1],
    [1,0,1,1,0,1],
    [1,1,0,1,0,0]])

patches = extract_patches(data, patch_shape=(2, 2), extraction_step=(2, 2))
most_frequent_number = ((patches > 0).sum(axis=-1).sum(axis=-1) > 2).astype(int)
print most_frequent_number

rawDescription: Changing structure of numpy array using most common value


code:
import time
strings = ["Hello","My name is blahblah","This is blah blah","blah blah","What's your name?"]

for txt in strings:
    print txt
    time.sleep(1)

rawDescription: Have Python print lines at different times


code:
import re

original_word = 'to'
err = 'vo'
sentence = 'I will go tomorrow to the'

pattern = re.compile(r'\b%s\b' % re.escape(original_word))
modified_sentence = re.sub(pattern, err, sentence, count=1)

print modified_sentence

rawDescription: python regex selecting whole words

description: Python regex selecting whole words


code:
import multiprocessing
import logging

def g1(results, x):
    y = x*x
    print "processing: y = %s" % y
    results.append(y)

def f1():
    logger = multiprocessing.log_to_stderr()
    logger.setLevel(multiprocessing.SUBDEBUG)
    manager = multiprocessing.Manager()
    results = manager.list()
    pool = multiprocessing.Pool(processes=4)
    for (i) in range(0,15):
        pool.apply_async(g1, [results, i])
    pool.close()
    pool.join()

def main():
    f1()

if __name__ == "__main__":
    main()

rawDescription: python multiprocessing, manager initiates process spawn loop

description: Python multiprocessing, manager initiates process spawn loop


code:
#!/usr/bin/python
import os
import fileinput

new_file_list=[]
all_files=os.listdir("/tmp")
for ff in all_files:
 if ff.endswith(".txt"):
    new_file_list.append(ff)

for files in new_file_list:
 print files
 if files == "a.txt":
   print "======================================="
   file_name="/tmp/"+str(files)
   print file_name

   f = fileinput.FileInput(file_name, inplace=1)

   print "======================================="
   for line in f:
        line = line.replace("hellohello","hello")
        print line,
   f.close()

rawDescription: python search and write to file

description: Python search and write to file


code:
import pandas as pd

df = pd.read_csv('test/Copy of AppCrawler.csv', sep="\t")

df['genres'] = df['genres'].str.strip('[]')
df['genres'] = df['genres'].str.replace(' ', '')

df = df.join(df.genres.str.split(",").apply(pd.value_counts).fillna(0))

#temporaly display 30 rows and 60 columns
with pd.option_context('display.max_rows', 30, 'display.max_columns', 60):
    print df
    #remove for clarity
print df.columns
Index([u'Unnamed: 0', u'appid', u'currency', u'final_price', u'genres',
       u'initial_price', u'is_free', u'metacritic', u'release_date',
       u'Accounting', u'Action', u'Adventure', u'Animation&Modeling',
       u'AudioProduction', u'Casual', u'Design&Illustration', u'EarlyAccess',
       u'Education', u'FreetoPlay', u'Indie', u'MassivelyMultiplayer',
       u'PhotoEditing', u'RPG', u'Racing', u'Simulation', u'SoftwareTraining',
       u'Sports', u'Strategy', u'Utilities', u'VideoProduction',
       u'WebPublishing'],
      dtype='object')

rawDescription: Python pandas - value_counts not working properly


code:
import numpy as np
import glumpy
from OpenGL import GLUT as glut
from time import time
from matplotlib.pyplot import subplots,close
from matplotlib import cm

def randomwalk(dims=(256,256),n=3,sigma=10,alpha=0.95,seed=1):
    """ A simple random walk with memory """
    M = np.zeros(dims,dtype=np.float32)
    r,c = dims
    gen = np.random.RandomState(seed)
    pos = gen.rand(2,n)*((r,),(c,))
    old_delta = gen.randn(2,n)*sigma
    while 1:
        delta = (1.-alpha)*gen.randn(2,n)*sigma + alpha*old_delta
        pos += delta
        for ri,ci in pos.T:
            if not (0. <= ri < r) : ri = abs(ri % r)
            if not (0. <= ci < c) : ci = abs(ci % c)
            M[ri,ci] += 1
        old_delta = delta
        yield M

def mplrun(niter=1000):
    """ Visualise the simulation using matplotlib, using blit for
    improved speed"""
    fig,ax = subplots(1,1)
    rw = randomwalk()
    im = ax.imshow(rw.next(),interpolation='nearest',cmap=cm.hot,animated=True)
    fig.canvas.draw()
    background = fig.canvas.copy_from_bbox(ax.bbox) # cache the background

    tic = time()
    for ii in xrange(niter):
        im.set_data(rw.next())          # update the image data
        fig.canvas.restore_region(background)   # restore background
        ax.draw_artist(im)          # redraw the image
        fig.canvas.blit(ax.bbox)        # redraw the axes rectangle

    close(fig)
    print "Matplotlib average FPS: %.2f" %(niter/(time()-tic))

def gprun(niter=1000):
    """ Visualise the same simulation using Glumpy """
    rw = randomwalk()
    M = rw.next()

    # create a glumpy figure
    fig = glumpy.figure((512,512))

    # the Image.data attribute is a referenced copy of M - when M
    # changes, the image data also gets updated
    im = glumpy.image.Image(M,colormap=glumpy.colormap.Hot)

    @fig.event
    def on_draw():
        """ called in the simulation loop, and also when the
        figure is resized """
        fig.clear()
        im.update()
        im.draw( x=0, y=0, z=0, width=fig.width, height=fig.height )

    tic = time()
    for ii in xrange(niter):
        M = rw.next()           # update the array
        glut.glutMainLoopEvent()    # dispatch queued window events
        on_draw()           # update the image in the back buffer
        glut.glutSwapBuffers()      # swap the buffers so image is displayed

    fig.window.hide()
    print "Glumpy average FPS: %.2f" %(niter/(time()-tic))

if __name__ == "__main__":
    mplrun()
    gprun()

rawDescription: Display NumPy array as continuously updating image with Glumpy


code:
from sklearn.naive_bayes import MultinomialNB
from sklearn.cross_validation import train_test_split
from sklearn.datasets import load_iris
from copy import copy
iris = load_iris()
X = iris.data[:, :2]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
clf1 = MultinomialNB()
clf2 = MultinomialNB()
print id(clf1), id(clf2) # two different instances
clf1.fit(X_train, y_train)
print clf1.score(X_test, y_test)
print clf2.fit(X_train, y_train).score(X_test, y_test)

rawDescription: scikit learn clf.fit / score model accuracy

description: Scikit learn clf.fit / score model accuracy


code:
#!/usr/bin/env python
import threading

shared_balance = 0

class Deposit(threading.Thread):
    def run(self):
        for _ in xrange(1000000):
            global shared_balance
            balance = shared_balance
            balance += 100
            shared_balance = balance

class Withdraw(threading.Thread):
    def run(self):
        for _ in xrange(1000000):
            global shared_balance
            balance = shared_balance
            balance -= 100
            shared_balance = balance

threads = [Deposit(), Withdraw()]

for thread in threads:
    thread.start()

for thread in threads:
    thread.join()

print shared_balance

rawDescription: Are locks unnecessary in multi-threaded Python code because of the GIL?


code:
from collections import defaultdict

class invertedIndex(object):

  def __init__(self,docs):
      self.docSets = defaultdict(set)
      for index, doc in enumerate(docs):
          for term in doc.split():
              self.docSets[term].add(index)

  def search(self,term):
        return self.docSets[term]

docs=["new home sales top forecasts june june june",
                     "home sales rise in july june",
                     "increase in home sales in july",
                     "july new home sales rise"]

i=invertedIndex(docs)
print i.search("sales") # outputs: set([0, 1, 2, 3])

rawDescription: How to make the basic inverted index program more pythonic

description: Make the basic inverted index program more pythonic


code:
lists = int(raw_input('Introduce the number of lists you want to have: '))
for i in xrange(lists):
    numbers = int(raw_input('Introduce how many numbers you want it to have: '))
    l = []
    for j in xrange(numbers):
        l.append(int(input('Number: ')))
    print l

rawDescription: Python: Print list sequence

description: Python: Print list sequence


code:
def span(lst, most=float("inf")):
  if not lst:
    yield []
    return

  for i in range(1, min(len(lst), most) + 1):
    for x in span(lst[i:], most):
      yield [lst[:i]] + x

lst = [1,2,3,4,5,6,7]
n = 5
spannings = list(span(lst, n))
print '\n'.join(map(str, spannings))

# proof that it produces the correct result
assert sorted(spannings) == sorted(s for s in span_orig(lst) if max(map(len, s)) <= n)
# proof that it produces the same result as the old
# function if called without a second argument
assert sorted(span_orig(lst)) == sorted(span(lst))

rawDescription: Algorithm to generate (not quite) spanning set in Python with no set longer than 5


code:
def in_ranges(x, range_tuples):
    return any(t[0] <= x < t[1] for t in range_tuples)
range_tuples = [(64600,64799), (67000,67319), (67399,67600),
                (68000,68699), (69400,69599)]
with open('testfile') as f:
    for number in map(int, f.read().split()):
        if in_ranges(number, range_tuples):
            sign = '+'
        else:
            sign = '-'
        print '|{}{}|'.format(number, sign)

rawDescription: How to evaluate a list of numbers in a text file with python

description: Evaluate a list of numbers in a text file with python


code:
def find_x_and_y(array):
    '''Step 1: Get unique x and y coordinates,
    and the width and height of the matrix'''
    x = sorted(list(set([i[0] for i in array])))
    y = sorted(list(set([i[1] for i in array])))

    width = len(x) + 1
    height = len(y) + 1

    return x, y, width, height

def construct_initial_matrix(array):
    '''Step 2: Make the initial matrix (filled with zeros)'''
    x, y, width, height = find_x_and_y(array)

    matrix = []
    for i in range(height):
        matrix.append([0] * width)

    return matrix

def add_edging(array, matrix):
    '''Step 3: Add the x and y coordinates to the edges'''
    x, y, width, height = find_x_and_y(array)

    for coord, position in zip(x, range(1, height)):
        matrix[position][0] = coord

    for coord, position in zip(y, range(1, width)):
        matrix[0][position] = coord

    return matrix

def add_z_coordinates(array, matrix):
    '''Step 4: Map the coordinates in the array to the position
    in the matrix'''
    x, y, width, height = find_x_and_y(array)

    x_to_pos = dict(zip(x, range(1, height)))
    y_to_pos = dict(zip(y, range(1, width)))

    for x, y, z in array:
        matrix[x_to_pos[x]][y_to_pos[y]] = z
    return matrix

def make_csv(matrix):
    '''Step 5: Pretty-printing'''
    return '\n'.join(', '.join(str(i) for i in row) for row in matrix)

def main():
    #example = [[1, 1, 10], [1, 2, 11], [2, 1, 12], [2, 2, 13]]
    example = [[1000,250,12.2],[1000,500,10],[2000,250,15],[2000,500,13.5]]

    matrix = construct_initial_matrix(example)
    matrix = add_edging(example, matrix)
    matrix = add_z_coordinates(example, matrix)

    print make_csv(matrix)

main()

rawDescription: Sorting array of XYZ coordinates into table in Python


code:
class A(type):
    def __repr__(self):
            return '<this is not a class!>'
class B(object):
    __metaclass__ = A
print B

rawDescription: python: change printing of class

description: Python: change printing of class


code:
import numpy as np

def is_less(a, b):
    # this ugliness is needed because we want to compare lexicographically same as np.lexsort(), from the last column backward
    for i in range(len(a)-1, -1, -1):
        if a[i]<b[i]: return True
        elif a[i]>b[i]: return False
    return False

def is_equal(a, b):
    for i in range(len(a)):
        if a[i] != b[i]: return False
    return True

# lat1 = np.array(([48.78,47.45],[38.56,39.53]))
# lat2 = np.array(([7.78,5.45],[48.78,47.45],[7.56,5.53]))
lat1 = np.load('arr.npy')
lat2 = np.load('refarr.npy')

idx1 = np.lexsort( lat1.transpose() )
idx2 = np.lexsort( lat2.transpose() )
ii = 0
jj = 0
while ii < len(idx1) and jj < len(idx2):
    a = lat1[ idx1[ii] , : ]
    b = lat2[ idx2[jj] , : ]
    if is_equal( a, b ):
        # do stuff with match
        print "match found: lat1=%s lat2=%s %d and %d" % ( repr(a), repr(b), idx1[ii], idx2[jj] )
        ii += 1
        jj += 1
    elif is_less( a, b ):
        ii += 1
    else:
        jj += 1

rawDescription: How to vectorize a simple for loop in Python/Numpy

description: Vectorize a simple for loop in Python/Numpy


code:
j = '{"action": "print", "method": "onData", "data": "Madan Mohan"}'
import json

class Payload(object):
    def __init__(self, j):
        self.__dict__ = json.loads(j)

p = Payload(j)
print p.action
print p.method
print p.data

rawDescription: Deserialize a json string to an object in python


code:
def function(x):
    if(x != 1):
        function(x / 2)
    print x

rawDescription: Recursion in Python without using a local/internal variable


code:
lst = [1,2,3,4,5]
for number in lst:
   print number #number is 1 then 2 then 3... every iteration its different according to lst

# output:
1
2
3
4
5

rawDescription: "For item in list" loop, need explanation


code:
aDemo = []
a = { "apple": "red", "banana": "yellow", "cucumber": "green" }
aDemo.append(a)
print aDemo[0]["apple"] # "red"

rawDescription: Python Array with string AND integer indices


code:
import multiprocessing as mp
import itertools

partitioned = {}
partitioned['0-20'] = []
partitioned['20-40'] = []
partitioned['40-60'] = []
partitioned['60+'] = []

def map_func1(f):
    # for line in f:
    gen = f[14:15] #15 1=male 2=female
    age = f[17:19] #18-19
    htin = f[1947:1950] #1948-1950 tall in inches, self reported !888! !999!
    wtlbs = f[1950:1953] #1951-1953 wt in lbs, self reported !888! !999!
    ovwt = f[1963:1964] #1964 consider myself overweight 1,under 2,over 3, !8!, !9!
    chwt = f[1964:1965] #1965 change weight or stay same 1=more, 2=less, 3=same, !8!, !9!
    return [gen, age, htin, wtlbs, ovwt, chwt]

def partitioner(m):
    for element in m:
        if int(element[1]) < 20:
            partitioned['0-20'].append(element)
        elif int(element[1]) < 40:
            partitioned['20-40'].append(element)
        elif int(element[1]) < 60:
            partitioned['40-60'].append(element)
        else:
            partitioned['60+'].append(element)

    return partitioned

if __name__ == "__main__":
    pool = mp.Pool(processes=3)
    f = open('adult.dat')
    m = pool.map(map_func1, f)
    print m[0]
    p = partitioner(m)
    print len(p['60+'])

rawDescription: Strange behavior using Python multiprocessing library


code:
for i, v in enumerate(a):
   print "{} : {}".format(i, v)

rawDescription: Python equivalent of Ruby's each_with_index?


code:
capitals = {"Alabama": "Montgomery", "Alaska": "Juneau", "Arizona": "Phoenix", "Arkansas": "Little Rock"}
while True:
    x = str(raw_input('Please enter a state: ' ))
    if x in capitals:
        print capitals[x], "is the capital of", x

rawDescription: Trouble with python while loop

description: Trouble with python while loop


code:
from collections import Counter

counter = Counter()

for line in file_handle:
    name, gender, age = line.split(',')
    counter[name] += 1

print counter.most_common()

rawDescription: Trying to Find Most Occurring Name in a File


rawDescription: Python separate matching string at the end of a string


code:
import os

def file_size(files):

    if isinstance(files, str):
        files = [files] # enlist

    size = 0
    for f in files:
        size += os.path.getsize(f)

    return size

if __name__ == '__main__':
    print file_size(__file__)
    print file_size([__file__, __file__])
    print file_size([])

rawDescription: python, without method overloading, howto?

description: Python, without method overloading, howto


code:
if m:  #only execute this if a match was found
   print m.group()  #m.group() is the portion of the string that matches your regex.

rawDescription: get first digit in a string in python

description: Get first digit in a string in python


code: regex.findall(ur'\p{Lu}', u'ÜìÑ')

rawDescription: Python regex uppercase unicode word

description: Python regex uppercase unicode word


code:
rdd.map(lambda x: (x, 1)) \
  .reduceByKey(lambda x, y: x + y) \
  .map(lambda ((day, city), count): (day, (city, count))) \
  .reduceByKey(lambda x, y: max(x, y, key=lambda x: x[1]))

rawDescription: Spark python most repeated value for each key


code:
def parse_csv(content, delimiter = ';'):
  csv_data = []
  for line in content.split('\n'):
    csv_data.append( [x.strip() for x in line.split( delimiter )] ) # strips spaces also
  return csv_data

content = open(uri,'r').read()
list_data = parse_csv( content )
print list_data[2][1]

rawDescription: Getting the content of .CSV cell

description: Getting the content of .CSV cell


code:
import os, shutil

srcDir = 'Users/photos/2008/thumbnails/' #files in two more dir deep
toDir = 'Users/external_drive/2008/'

try:
    os.makedirs(toDir)
except:
    pass

for root, dirs, files in os.walk(srcDir, topdown=True):
    for file in files:
        src = os.path.join(root, file)
        target = os.path.join(toDir, file)

        if file.lower().endswith('.jpg'):
            index = 1

            while os.path.exists(target):
                index +=1
                target = os.path.join(toDir, os.path.splitext(file)[0]+ "_%d.jpg" % index)

            print "Copying: '%s'  to  '%s'" % (src, target)
            shutil.copy(src, target)

rawDescription: copying files in python with check for duplicates

description: Copying files in python with check for duplicates


code:
# Example path, I guess something like this is produced by path2indice?!
indices = ("home", "username", "Desktop")

tree = {}

d = tree
for indice in indices[:-1]:
    if indice not in d:
        d[indice] = {}

    d = d[indice]

d[indices[-1]] = "some value"

print tree # this will print {'home': {'username': {'Desktop': 'some value'}}}

rawDescription: Python - How to update a multi-dimensional dict


code:
for key,group in itertools.groupby(dirtup2,operator.itemgetter(2)):
    newlist=sorted(group,key=operator.itemgetter(1),reverse=True)
    tuple_with_max=newlist[0]
    print tuple_with_max

rawDescription: Extract the tuple with the largest item from sublist (find most recent report in directory)


code:
def recv():
    while True:
         data = s.recv(1024)
         if not data: sys.exit(0)
         print data

Thread(target=recv).start()

rawDescription: Python Socket - Send/Receive messages at the same time


code:
def cluster(order, distance, points, threshold):
    ''' Given the output of the options algorithm,
    compute the clusters:

    @param order The order of the points
    @param distance The relative distances of the points
    @param points The actual points
    @param threshold The threshold value to cluster on
    @returns A list of cluster groups
    '''
    clusters = [[]]
    points   = sorted(zip(order, distance, points))
    splits   = ((v > threshold, p) for i,v,p in points)
    for iscluster, point in splits:
        if iscluster: clusters[-1].append(point)
        elif len(clusters[-1]) > 0: clusters.append([])
    return clusters

    rd, cd, order = optics(points, 4)
    print cluster(order, rd, points, 38.0)

rawDescription: Python Implementation of OPTICS (Clustering) Algorithm


code:
def dowork():
  wlen = random.random()
  sleep(wlen) # Emulate doing some work
  print 'work done in %0.2f seconds' % wlen

def main():
  while 1:
    t = threading.Thread(target=dowork)
    time.sleep(4)

rawDescription: timer interrupt thread python

description: Timer interrupt thread python


code:
names = ['Chris', 'Jack', 'Kim']
target = ['Chris Smith', 'I hijacked this thread', 'Kimberly','Christmas is here', 'CHRIS']

import re
matches = [targ for targ in target if any(re.search(r'\b{}'.format(name), targ, re.I) for name in names)]
print matches
# ['Chris Smith', 'Kimberly', 'Christmas is here', 'CHRIS']

rawDescription: Searching a list based on values in another list


code:
class some_magic_adaptor:
  def __init__( self, it ):
    self.it = it
    self.next_chunk = ""
  def growChunk( self ):
    self.next_chunk = self.next_chunk + self.it.next()
  def read( self, n ):
    if self.next_chunk == None:
      return None
    try:
      while len(self.next_chunk)<n:
        self.growChunk()
      rv = self.next_chunk[:n]
      self.next_chunk = self.next_chunk[n:]
      return rv
    except StopIteration:
      rv = self.next_chunk
      self.next_chunk = None
      return rv

def str_fn():
  for c in 'a', 'b', 'c':
    yield c * 3

ff = some_magic_adaptor( str_fn() )

while True:
  data = ff.read(4)
  if not data:
    break
  print data

rawDescription: Adapt an iterator to behave like a file-like object in Python


code:
string=raw_input("Input: ")
string_lower=string.lower()
string_list=list(string_lower)

string_dict = dict()
for j in string_list:
    string_dict[j]=list()
for i, j in enumerate(string_list):
    string_dict[j].append(i)
print string_dict

rawDescription: Appending list B with indices from enumerated list A


code:
import datetime

dnow = datetime.datetime.now()

with open("test.txt", 'r') as f:
    racetime = f.read()
racetime = racetime.rstrip()

NewRaceTime = datetime.datetime.strptime(racetime, '%Y-%m-%d %H:%M:%S.%f')
timedifference = dnow - NewRaceTime

print racetime

rawDescription: Python and Strptime Difference When Variable Set or Read From File But Exact Same Value


code:
class tivoRemote(Plugin):

    def __init__(self):
        self.tivo_address = '192.168.0.9'
        self.tivo_name = ''
        self.tivo_swversions = {}
        self.have_zc = True
        self.captions_on = False
        self.sock = None
        self.outer = None

    def connect(self):
        """ Connect to the TiVo within five seconds or report error. """
        try:
            sock = socket.socket()
            sock.settimeout(5)
            sock.connect((tivo_address, 31339))
            sock.settimeout(None)
        except Exception, msg:
            msg = 'Could not connect to %s:\n%s' % (tivo_name, msg)
            print(msg)
        self.sock = sock

    def send(self, message):
        """ The core output function, called from irsend(). Re-connect if
            necessary (including restarting the status_update thread), send
            message, sleep, and check for errors.
        """

        if not self.sock:
            self.connect()
            thread.start_new_thread(status_update, ()) # status_update must be some global at this point
        try:
            self.sock.sendall(message)
            time.sleep(0.1)
        except Exception, msg:
            error_window(str(msg))

    def irsend(self, *codes):
        """ Expand a command sequence for send(). """
        for each in codes:
            self.send('IRCODE %s\r' % each)

    @register("en-US", ".*Change.*Channel.*")
    def channelChanger(self, speech, language, matchedRegex):
            if language == 'en-US':
                    answer = self.ask(u"Which channel would you like?")
                    self.say(u"Ok, one moment..".format(answer))
                    self.connect()
                    self.irsend(answer)
            self.complete_request()

rawDescription: Python undefined function from an each loop


code:
d = {'world': 3, 'hello': 2, 'bye': 2, 'test': 2}
[x for x, y in sorted(d.items(), key=lambda (x, y): (-y, x))[:3]]

rawDescription: Top 3 frequency of words in a sentence

description: Top 3 frequency of words in a sentence


code:
if re.search(r'(?<!\.)\.\.(?!\.)', s):
    print 'Contain two dots'

rawDescription: How to check if the name doesn't contain two dots going together with regex?

description: Check if the name doesn't contain two dots going together with regex


code:
import numpy as np
import scipy.spatial

def main():
    xyz = np.random.random((100, 3))
    area_underneath = trapezoidal_area(xyz)
    print area_underneath

def trapezoidal_area(xyz):
    """Calculate volume under a surface defined by irregularly spaced points
    using delaunay triangulation. "x,y,z" is a <numpoints x 3> shaped ndarray."""
    d = scipy.spatial.Delaunay(xyz[:,:2])
    tri = xyz[d.vertices]

    a = tri[:,0,:2] - tri[:,1,:2]
    b = tri[:,0,:2] - tri[:,2,:2]
    proj_area = np.cross(a, b).sum(axis=-1)
    zavg = tri[:,:,2].sum(axis=1)
    vol = zavg * np.abs(proj_area) / 6.0
    return vol.sum()

main()

rawDescription: Volume under "plane" defined by data points - python


code:
def f(x):
    return 48 * x * (1 + x) ** 60 - (1 + x) ** 60 + 1

def df(x):
    return 12 * (1 + x) ** 59 * (244 * x - 1)

n = int(raw_input('Enter the number of times to approximate the root: '))
x = 0.0076
for i in range(n):
    x = x - f(x) / df(x)
    print x

rawDescription: Trying to implement Newton's Method in order to find root. Not working, but why?


code:
import os, os.path

# simple version for working with CWD
print len([name for name in os.listdir('.') if os.path.isfile(name)])

# path joining version for other paths
DIR = '/tmp'
print len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])

rawDescription: How to count the number of files in a directory using Python

description: Count the number of files in a directory using Python


code:
#!/usr/bin/python
# Filename : hello.py

from sqlite3 import *

conn = connect('database')

c = conn.cursor()

c.execute('select * from table limit 2')
result = c.fetchall()

for row in result:
    newname = row[1]
    newname = newname[:-3]+"hello"
    newdata = "UPDATE table SET name = '" + newname + "', originalPath = '' WHERE id = '" + str(row[0]) + "'"
    print row
    c.execute(newdata)
conn.commit()
c.close()
conn.close()

rawDescription: Why does "c.execute(...)" break the loop?


code:
import requests
from bs4 import BeautifulSoup

def parse_page(data):
        subsoup = BeautifulSoup(data)
        matchoverview = subsoup.find('div', attrs={'id':'match-overview'})
        print '--------------'
        homeTeam = matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('a').text
        homeScore = matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('span').text
        homeGoalScorers = ["Home Goal Scorers:"]
        for goals in matchoverview.find('div', attrs={'class':'team-match-details'}).findNext('p').find_all('span'):
            homeGoalScorers.append(goals.text.replace(u'\u2032', "'"))
        homeGoals = "\n".join(homeGoalScorers)
        awayTeam = matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('a').text
        awayScore = matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('span').findNext('span').text
        awayGoalScorers = ["Away Goal Scorers:"]
        for goals in matchoverview.find('div', attrs={'id': 'away-team'}).find('div', attrs={'class':'team-match-details'}).findNext('p').find_all('span'):
            awayGoalScorers.append(goals.text.replace(u'\u2032', "'"))
        awayGoals = "\n".join(awayGoalScorers)
        print '{0} {1} - {2} {3}'.format(homeTeam, homeScore, awayTeam, awayScore)
        print homeGoals
        print awayGoals

def all_league_results():
    r = requests.get("http://www.bbc.co.uk/sport/football/league-one/results")
    soup = BeautifulSoup(r.content)

    # Save Teams
    for link in soup.find_all("a", attrs={'class': 'report'}):
        fullLink = 'http://www.bbc.com' + link['href']
        subr = requests.get(fullLink)
        parse_page(subr.text)

def specific_game_results(url):
    subr = requests.get(url)
    parse_page(subr.text)

#get specific games results
specific_game_results('http://www.bbc.co.uk/sport/0/football/32460049')
#get all current league results
all_league_results()

rawDescription: Access Data from Behind Java

description: Access Data from Behind Java


rawDescription: How can I generate \x-escaped UTF-8 in Python?

description: Generate \x-escaped UTF-8 in Python


code:
from datetime import datetime

birthday = datetime(1988, 2, 19, 12, 0, 0)
diff = datetime.now() - birthday
print diff
# 8954 days, 7:03:45.765329

rawDescription: Subtracting Dates With Python

description: Subtracting Dates With Python


code:
import os

with open('file.txt') as f:
    for line in f:
        newname, file, checksum = line.split()
        if os.path.exists(file):
            try:
                os.rename(file, os.sep.join([os.path.dirname(file), newname]))
            except OSError:
                print "Got a problem with file {}. Failed to rename it to {}.".format(file, newname)

rawDescription: Renaming files in folder from a text file


code:
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.google.com')
print browser.current_url

rawDescription: Can I read the browser url using selenium webdriver?

description: Read the browser url using selenium webdriver


code:
import re
pattern = re.compile(ur'[\u0980-\u09FF]+', re.UNICODE)
words = re.findall(pattern, text)

rawDescription: Splitting text by a Unicode script in Python


code:
text = 'slfdk gaklsdjfl asdkfljasdkljf qkwlejlqwekj bazaklajsdfkj gsadf'
l = ['foo', 'bar', 'baz']

print any(e in text for e in l)

rawDescription: What is the fastest way of searching a list of regexs in a text using Python?


code:
import threading
import lib.logging
import time
from itertools import count

class Monitor(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self, count=count()):
        # next(count) starts from 0, so to log first '1', + 1 is used
        lib.logging.debug ("Count is: " + str(next(count) + 1))

def main():
    for i in xrange(3):
        t1 = Monitor()
        t2 = Monitor()
        t1.start()
        t2.start()
        t1.join()
        t2.join()
        time.sleep(3)

    print "done"

rawDescription: Count how many times I visit a thread

description: Count how many times I visit a thread


code:
class Foo(object):
   def __init__(self, a, b, c=None):
       self.a = a
       self.b = b
       self.c = c  # c is presumed to be a list
   def __eq__(self, other):
       return self.a == other.a \
               and self.b == other.b and self.c == other.c

a,b = Foo(2,3), Foo(5,6)
c = Foo(1,2, [a,b])
d = Foo(1,2)
e,f = Foo(2,3), Foo(5,6)
g = Foo(1,2, [e,f])

print c == d #False
print c == g #True

rawDescription: Comparison of two objects with nested lists as properties


code:
class objectD(object):
  def __init__(self,val1,val2,val3):
    self.val1 = val1
    self.val2 = val2
    self.val3 = val3

  def __str__(self):
    return str(str(self.val1)+","+str(self.val2)+","+str(self.val3))

  def dependence(self,dependent):
    print "values val1 and val2 of "+str(self)+" now depend on "+dependent
    self.val1 = dependent.val1
    self.val2 = dependent.val2
    self.dependent = dependent

  def update(self):
    self.val1 = self.dependent.val1
    self.val2 = self.dependent.val2

#test
obj1 = objectD(350,4,500)
print obj1
obj2 = objectD(230,1,1000)
print obj2
obj2.dependence(obj1)#problem with imported class no longer occurs there
print obj2
obj1.val1 = 1315
obj1.val2 = 6464
print obj1
obj2.update()
print obj2

rawDescription: python difference between imported and defined class

description: Python difference between imported and defined class


code:
data = """blahblahblah
I DONT CARE ABOUT THIS EITHER
blahblahblah"""

import lxml.html
tree = lxml.html.fromstring(data)
print tree.xpath('//a/@href')

# ['THIS IS WHAT I WANT']

rawDescription: Parse HTML using LXML in Python

description: Parse HTML using LXML in Python


code:
import pickle

class Player:
    def __init__(self, hp, dmg):
        self.hp = hp
        self.dmg = dmg

def save(obj):
    save_file = open('save.dat', 'wb')
    pickle.dump(obj, save_file)
    save_file.close()

def load():
    load_file = open('save.dat', 'rb')
    loaded_game_data = pickle.load(load_file)
    return loaded_game_data

def start():
    player.hp = input('Player Hp: ')
    player.dmg = input('Player Dmg: ')

player = Player(0, 0)

start()
save(player)
loaded_player = load()
print loaded_player.hp, loaded_player.dmg

rawDescription: Pickle user inputs - Python 3

description: Pickle user inputs - Python 3


code:
from sets import Set

class MySet(Set):

    def __init__(self, iter, klass=None):
        if klass is not None:
            for item in iter:
                if not isinstance(item, klass):
                    raise Exception("Error")
        super(MySet, self).__init__(iter)

if __name__ == '__main__':

    set1 = MySet([1,2,3], int)
    set2 = MySet([2,3,4], int)

    print set2.intersection(set1)

rawDescription: Python - a set of one type only

description: Python - a set of one type only


code:
import csv

ids = {}

f=open("D_Matrix.csv")
csv_f = csv.reader(f)
n=0

for row in csv_f:
    str_list0=str(row[0])
    str_list1=str(row[1])
    str_list2=str(row[2])

    if not str_list0 in ids.keys():
        ids[str_list0] = {}

    input_dict = ids[str_list0]

    if not str_list1 in input_dict.keys():
        input_dict[str_list1] = str_list2 # your value
    else:
        # we already have the key, check if the new value is shorter:
        #
        if input_dict[str_list1] > str_list2:
            input_dict[str_list1] = str_list2

# Now you have a dict like:
#
# {'InputID1': {'TargetID1' : minimum target distance}}
for input_key in ids.keys():
    for target_key in ids[input_key].keys():
        print "In: [%s], Tgt: [%s], Min distance: [%s]" % \
            (input_key, target_key, ids[input_key][target_key])

rawDescription: Python: Trouble organizing list values imported from .csv


code:
import re

m = re.search('upc content=([0-9]*)', string)
if m:
   upc = m.group(1)
   print upc

rawDescription: Extracting portion of large string

description: Extracting portion of large string


code:
import PyKCS11
import getopt
import sys
import platform
import hashlib
from M2Crypto import RSA

pkcs11 = PyKCS11.PyKCS11Lib()
lib_path = "/opt/safenet/protecttoolkit5/ptk/lib/libcryptoki.so"
pkcs11.load(lib_path)
info = pkcs11.getInfo()
slots = pkcs11.getSlotList()
if len(slots) > 0:
    session = pkcs11.openSession(slots[0], PyKCS11.CKF_RW_SESSION)
    session.login("12345678")
    mech = PyKCS11.Mechanism(PyKCS11.CKM_RSA_PKCS_KEY_PAIR_GEN, None)
    public_template = [
        (PyKCS11.CKA_CLASS, PyKCS11.CKO_PUBLIC_KEY),
        (PyKCS11.CKA_PRIVATE, PyKCS11.CK_FALSE),
        (PyKCS11.CKA_TOKEN, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_ENCRYPT, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_VERIFY, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_WRAP, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_KEY_TYPE, PyKCS11.CKK_RSA),
        (PyKCS11.CKA_VERIFY_RECOVER, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_MODULUS_BITS, 2048),
    ]
    private_template = [
        (PyKCS11.CKA_CLASS, PyKCS11.CKO_PRIVATE_KEY),
        (PyKCS11.CKA_PRIVATE, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_TOKEN, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_DECRYPT, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_SIGN, PyKCS11.CK_TRUE),
        (PyKCS11.CKA_UNWRAP, PyKCS11.CK_TRUE)
        ]
    (pub, priv) = session.generateKeyPair(public_template, private_template, mech)
    (pubExp,pubModulus) = session.getAttributeValue(pub,[PyKCS11.CKA_PUBLIC_EXPONENT,PyKCS11.CKA_MODULUS], True)
    # ==================================================
    # Signing data
    binaryData = "Hello world"
    # Generate SHA1
    sha1 = hashlib.sha1()
    sha1.update(str(bytearray(binaryData)))
    digest=sha1.digest()
    # Indicate SHA1 is used
    binaryData2='\x30\x21\x30\x09\x06\x05\x2b\x0e\x03\x02\x1a\x05\x00\x04\x14'+digest
    signMechanism = PyKCS11.Mechanism(PyKCS11.CKM_RSA_PKCS, None)
    signedData = session.sign(priv, binaryData2, signMechanism)
    session.logout()
    session.closeSession()
    # ==================================================
    # Verify
    pubkey = RSA.new_pub_key(('\x00\x00\x00\x03' + str(bytearray(pubExp)), '\x00\x00\x01\x01\x00'+str(bytearray(pubModulus))))
    result=pubkey.verify(str(bytearray(digest)), str(bytearray(signedData)), 'sha1')
    print "VERIFY:" + str(result)

rawDescription: How to verify signed data with PyKCS11 Library

description: Verify signed data with PyKCS11 Library


code:
class Base(object):
    def __init__(self):
        super(Base, self).__init__()
        print "BASE"

class Next(Base):
    def __init__(self):
        super(Next, self).__init__()
        print "NEXT"

class Final(Next):
    def __init__(self):
        super(Final, self).__init__()
        print "FINAL"

f = Final()

rawDescription: Parent initializer not called in multi-level inheritence?


code:
import re
DATA = "Hey, you - what are you doing here!?"
print re.findall(r"[\w']+", DATA)
# Prints ['Hey', 'you', 'what', 'are', 'you', 'doing', 'here']

rawDescription: Python - Split Strings with Multiple Delimiters


code:
numbers_data = [int(e.text) for e in soup.find_all('td', 'right')]

print numbers_data

rawDescription: Python - Beautiful Soup - Remove Tags

description: Python - Beautiful Soup - Remove Tags


code:
import numpy as np

strings = np.array(['hello    ', 'world    '], dtype='|S10')

print np.char.strip(strings) == 'hello'
# prints [ True False]

rawDescription: String comparison in Numpy

description: String comparison in Numpy


code:
import signal
import sys
import time

def signal_handler(signal, frame):
    print 'You pressed Ctrl+C!'
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print 'Press Ctrl+C'
while True:
    time.sleep(1)

rawDescription: Capture keyboardinterrupt in Python without try-except


code:
import scraperwiki
import lxml.html

html = scraperwiki.scrape('http://ahr13.mapyourshow.com/5_0/exhibitor_results.cfm?  alpha=%40&type=alpha&page=1')

root = lxml.html.fromstring(html)
# get the links
hrefs = root.xpath('//td[@class="mys-elastic mys-left"]/a')

for href in hrefs:
   print 'http://ahr13.mapyourshow.com' + href.attrib['href']

rawDescription: Scraperwiki + lxml. How to get the href attribute of a child of an element with a class?


code:
#Read cvs File from url
import csv
import urllib2
url = 'mycsvfile.csv'
response = urllib2.urlopen(url)
cr = csv.reader(response)
csvset = [set(i) for i in cr]

#Read txt File
import linecache
fileName = 'myfile.txt'
myFile = open(fileName,'r')
list_of_lists = []
try:
    for line in myFile:
        list_of_lists.append(line.strip())

    #Lookup Lines
    for element in list_of_lists:
        elementstring=''.join(element)
        for row in csvset:
            if elementstring in row:
                print row

finally:
    myFile.close()

rawDescription: Python nested for loop to match strings from a text file to strings in a csv file


code:
import Tkinter as tk

def keyPress(event):
    if event.char in ('V', 'F', ' '):
        print event.char
    elif event.keysym not in ('Alt_r', 'Alt_L', 'F4'):
        print event.keysym
        return 'break'

root = tk.Tk()
entry = tk.Entry()
entry.bind('<KeyPress>', keyPress)
entry.pack()
entry.focus()

root.mainloop()

rawDescription: How to insert only some specified characters in a tkinter Entry widget

description: Insert only some specified characters in a tkinter Entry widget


code:
from urlparse import urlparse
from threading import Thread
import httplib, sys
from Queue import Queue

concurrent = 200

def doWork():
    while True:
        url = q.get()
        status, url = getStatus(url)
        doSomethingWithResult(status, url)
        q.task_done()

def getStatus(ourl):
    try:
        url = urlparse(ourl)
        conn = httplib.HTTPConnection(url.netloc)
        conn.request("HEAD", url.path)
        res = conn.getresponse()
        return res.status, ourl
    except:
        return "error", ourl

def doSomethingWithResult(status, url):
    print status, url

q = Queue(concurrent * 2)
for i in range(concurrent):
    t = Thread(target=doWork)
    t.daemon = True
    t.start()
try:
    for url in open('urllist.txt'):
        q.put(url.strip())
    q.join()
except KeyboardInterrupt:
    sys.exit(1)

rawDescription: What is the fastest way to send 100,000 HTTP requests in Python?


code:
import sys # Used to add the BeautifulSoup folder the import path
import urllib2 # Used to read the html document

if __name__ == "__main__":
    ### Import Beautiful Soup
    ### Here, I have the BeautifulSoup folder in the level of this Python script
    ### So I need to tell Python where to look.
    sys.path.append("./BeautifulSoup")
    from BeautifulSoup import BeautifulSoup

    ### Create opener with Google-friendly user agent
    opener = urllib2.build_opener()
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]

    ### Open page & generate soup
    ### the "start" variable will be used to iterate through 10 pages.
    for start in range(0,15):
        url = "http://www.google.com/search?q=site:stackoverflow.com&start=" + str(start*10)
        page = opener.open(url)
        soup = BeautifulSoup(page)

        ### Parse and find
        ### Looks like google contains URLs in <cite> tags.
        ### So for each cite tag on each page (10), print its contents (url)
        for cite in soup.findAll('cite'):
            print cite.text

rawDescription: Python Script to Get 150 Google Search Result


rawDescription: How to keep a number below n

description: Keep a number below n


code:
S = set(['a', 'ab', 'ba'])

collect = set()
step = set([''])
while step:
   step = set(a+b for a in step for b in S if len(a+b) <= 6)
   collect |= step

print sorted(collect)

rawDescription: Get every combination of strings

description: Get every combination of strings


code:
from multiprocessing import Pool

def part_crack_helper(args):
    solution = do_job(args)
    if solution:
        return True
    else:
        return False

class Worker():
    def __init__(self, workers, initializer, initargs):
        self.pool = Pool(processes=workers, initializer=initializer, initargs=initargs)

    def callback(self, result):
        if result:
            print "Solution found! Yay!"
            self.pool.terminate()

    def do_job(self):
        for args in product(seed_str, repeat=4):
            self.pool.apply_async(part_crack_helper, args=args, callback=self.callback)
        self.pool.close()
        self.pool.join()
        print "good bye"

w = Worker(num_proc, init, [total_count])
w.do_job()

rawDescription: Python Multiprocess Pool. How to exit the script when one of the worker process determines no more work needs to be done?


code:
class Slave(Process):
    def __init__(self):
        multiprocessing.Process.__init__(self)
        self.start()
    def run(self):
        Master.register("getNextInput", getNextInput)
        m = Master(('localhost', 5000))
        m.connect()
        while True:
            input = m.getNextInput()
            # Check for None first
            self.process(input)
    def process(self):
        print "Processed " + str(input)

rawDescription: Managing a fixed number of workers in Python


code:
def save_changes():
    for i in value_list: # access the list of stringvars
        print i.get()    # and print the .get() method of each

root = Tk()

d = {'item1':'value1', 'item2':'value2'}
value_list = []

row=1
for k,v in d.iteritems(): # iterate over k,v in dict
    var = StringVar()
    var.set(v) # set var to value of dict item
    Label(root, text=k).grid(row=row, column=0)
    Entry(root, textvariable=var).grid(row=row, column=1)
    row+=1
    value_list.append(var) # append the stringvar instance to a list

Button(root, text='Save', command=save_changes).grid()

mainloop()

rawDescription: Create an entry per item in an iterable of unknown length with Tkinter


code:
for fruit in thelist[1:]:
   print fruit

rawDescription: How do I do a "for each" , starting at a certain index of a list (Python)?

description: Do a "for each" , starting at a certain index of a list (Python)


code:
s = int(input("Enter a number "))
for i in range(-s, s+1):
    print i

rawDescription: Python Range() for positive and negative numbers


code:
import re
print re.sub(r'(\d+)','{\sub \\1}','C5B2GH5')
#output: C{\sub 5}B{\sub 2}GH{\sub 5}

rawDescription: How can I insert text at every number in a string

description: Insert text at every number in a string


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

    def eat(self):
        print "The %s eats some %s" % (self.name, self.food)

class Pet(object):
    def __init__(self, animal=None):
        self.animal = animal

    def __getattr__(self, key):
        return getattr(self.animal, key)

    def __setattr__(self, key, value):
        return setattr(self.animal, key, value)

    def eat(self):
        return self.animal.eat()

    def pet(self):
        print "You pet the %s." % self.name

    def feed(self):
        print "You put some %s into the bowl." % self.food
        self.eat()

rawDescription: Creating an object from a base class object in Python


code:
import itertools

bin1 = [1,2]
bin2 = [3,4]
bin3 = [5,6]

print [[bin1[0],y] for y in bin3] + [[bin1[1],y] for y in bin2]

rawDescription: Return all possible combinations when selecting a single item from multiple bins, with an IF condition


code:
with open("fk.txt") as file:   # Use file to refer to the file object
    data = file.readlines()
column1, column2 = [], []
for line in data:
    try:
        entry1, entry2 = line.split(' ')
        column1.append(int(entry1))
        column2.append(int(entry2))
    except ValueError:
        pass
print column1, column2

rawDescription: How to print selected elements from file?

description: Print selected elements from file


code:
import csv
import urllib2

req = urllib2.Request('http://financials.morningstar.com/ajax/ReportProcess4CSV.html?&t=XNAS:BRCM&region=usa&culture=en-US&cur=USD&reportType=is&period=12&dataType=A&order=desc&columnYear=5&rounding=3&view=raw&r=886300&denominatorView=raw&number=3')
response = urllib2.urlopen(req)

response.readline() # This reads (and discards) the first row of data which is not needed.
response.readline() # skip the

myFieldnames = ["firstColName", "TTM", "2012", "2011", "2010", "2009", "2008"]
csvio = csv.DictReader(response, fieldnames=myFieldnames)

print csvio.fieldnames
for row in csvio:
    print row

rawDescription: Parse csv to dict

description: Parse csv to dict


code:
import urlparse
myurl = urlparse.urlparse('https://localhost/?code=bork&charlie=brown')
print myurl
print myurl.scheme
print urlparse.parse_qs(myurl.query)

rawDescription: Odd behavior with urlparse

description: Odd behavior with urlparse


code:
def sort_key(data):
    title, size, format, provider = data
    return p_dict[provider], f_dict[format], -size

print sorted( mydata, key = sort_key)

rawDescription: complex sorting in python

description: Complex sorting in python


code:
import numpy as np
import pylab
pylab.ion()

def get_fig(fig_num, some_data, some_labels):

    fig = pylab.figure(fig_num,figsize=(8,8),frameon=False)
    ax = fig.add_subplot(111)
    ax.set_ylim([0.1,0.8]); ax.set_xlim([0.1, 0.8]);
    ax.set_title("Quarterly Stapler Thefts")
    ax.pie(some_data, labels=some_labels, autopct='%1.1f%%', shadow=True);
    return fig

my_labels = ("You", "Me", "Some guy", "Bob")

# To ensure first plot is always made.
do_plot = 1; num_plots = 0;

while do_plot:
    num_plots = num_plots + 1;
    data = np.random.rand(1,4).tolist()[0]

    fig = get_fig(num_plots,data,my_labels)
    fig.canvas.draw()
    pylab.draw()

    print "Close any of the previous plots? If yes, enter its number, otherwise enter 0..."
    close_plot = raw_input()

    if int(close_plot) > 0:
        pylab.close(int(close_plot))

    print "Create another random plot? 1 for yes; 0 for no."
    do_plot = raw_input();

    # Don't allow plots to go over 10.
    if num_plots > 10:
        do_plot = 0

pylab.show()

rawDescription: How to animate a time-ordered sequence of matplotlib plots

description: Animate a time-ordered sequence of matplotlib plots


rawDescription: Python: How to write data in file in specific format?


rawDescription: Appending values to dictionary in Python


code:
LOG_DIR = '/tmp/'
LOG_LEVEL = 4
WSFC_HOME = '/opt/wso2/wsf_c'
END_POINT = 'http://localhost:9090/axis2/services/echo/echoString'

if __name__ == '__main__':
    message = """
    <ns1:echoString xmlns:ns1="http://ws.apache.org/axis2/services/echo">
        <text>Hello World!</text>
    </ns1:echoString>
    """
    try:
        client = wso2.wsf.WSClient({
            'to':END_POINT,
            'WSF_LOG_DIR':LOG_DIR,
            'WSF_LOG_LEVEL':LOG_LEVEL,
            'WSFC_HOME':WSFC_HOME,
            })

        print 'Sending: ' + message

        response = client.request(message)

        if response is not None:
            print 'Respose: ' + response + '\n'
        else:
            print 'Error occurred!'
    except wso2.wsf.WSFault, e:
        print 'Exception occurred:'
        print e

rawDescription: SOAP 1.2 python client

description: SOAP 1.2 python client


code:
def repl(m):
    return re.sub(r"'(\w+)':", r"'\1 Smith':", m.group())

my_regex = r'spirit_animals\s*=\s*\{[^}]*(?:}(?!;(?:$|\n))[^}]*)*};(?:$|\n)'
print re.sub(my_regex, repl, contents)

rawDescription: Append a suffix to each regex match in python?


code:
from copy import copy

def squaredError(a, b):
    r = copy(a)

    for i in range(len(a)):
        r[i] -= b[i]
        r[i] *= r[i]

    return sum(r)

given = [0, 1, 2, 3, 4, 5]
A = [0.1, 0.9, 2, 3.3, 3.6, 5.1]
B = [-0.1, 0.9, 2.1, 3.1, 3.9, 5]
C = [0, 1.1, 2, 2.9, 4, 5.1]

print squaredError(given, A)
print squaredError(given, B)
print squaredError(given, C)

match = min(map(lambda x: (squaredError(given, x), x), [A,B,C]))[1]
print match

rawDescription: Python: Find list that most closely matches input list value by value


rawDescription: Python dictionary with frequency

description: Python dictionary with frequency


code:
import glob

def avg(lst):
    return float(sum(lst))/len(lst)

all_numbers = []
for filename in glob.glob("100107_902988_6188DAAXX_s_*.sorted"):
    numbers = []
    with open(filename, "r") as file:
        for line in file:
            numbers.append(int(line.split()[7]))
    print "{0}: {1:.2f}".format(filename, avg(numbers))
    all_numbers.extend(numbers)
print "Total average: {0:.2f}".format(avg(all_numbers))

rawDescription: How to calculate the average of only one column in a series of file with python?

description: Calculate the average of only one column in a series of file with python


rawDescription: python - decode unicode string

description: Python - decode unicode string