code:
import re
import cPickle
from functools import partial
from cStringIO import StringIO

SENTINEL = '#S'

# when scanning look for #S, but NOT ##S
sentinel_pattern = '(?<!#)#S' # uses negative look-behind
sentinel_re = re.compile(sentinel_pattern)
find_sentinel = sentinel_re.search

# when writing replace single # with double ##
write_pattern = '#'
write_re = re.compile(write_pattern)
fix_write = partial(write_re.sub, '##')

# when reading, replace double ## with single #
read_pattern = '##'
read_re = re.compile(read_pattern)
fix_read = partial(read_re.sub, '#')

class RecordWriter(object):
    def __init__(self, stream):
        self._stream = stream
        self._write_buffer = None

    def __enter__(self):
        self._write_buffer = StringIO()
        return self

    def __exit__(self, et, ex, tb):
        if self._write_buffer.tell():
            self._stream.write(SENTINEL) # start
            cPickle.dump(self._write_buffer.tell(), self._stream, cPickle.HIGHEST_PROTOCOL) # byte length of user's original data
            self._stream.write(fix_write(self._write_buffer.getvalue()))
            self._write_buffer = None
        return False

    def write(self, data):
        if not self._write_buffer:
            raise ValueError("Must use StreamWriter as a context manager")
        self._write_buffer.write(data)

class BadBlock(Exception): pass

def verify_length(block):
    fobj = StringIO(block)
    try:
        stated_length = cPickle.load(fobj)
    except (ValueError, IndexError, cPickle.UnpicklingError):
        raise BadBlock
    data = fobj.read()
    if len(data) != stated_length:
        raise BadBlock
    return data

def RecordReader(stream):
    ' Read one record '
    accum = StringIO()
    seen_sentinel = False
    data = ''
    while True:
        m = find_sentinel(data)
        if not m:
            if seen_sentinel:
                accum.write(data)
            data = stream.read(80)
            if not data:
                if accum.tell():
                    try: yield verify_length(fix_read(accum.getvalue()))
                    except BadBlock: pass
                return
        else:
            if seen_sentinel:
                accum.write(data[:m.start()])
                try: yield verify_length(fix_read(accum.getvalue()))
                except BadBlock: pass
                accum = StringIO()
            else:
                seen_sentinel = True
            data = data[m.end():] # toss

if __name__ == '__main__':
    import random

    stream = StringIO()
    data = [str(random.random()) for _ in xrange(3)]
    # test with a string containing sentinel and almost-sentinel
    data.append('abc12#jeoht38#SoSooihetS#')
    count = len(data)
    for i in data:
        with RecordWriter(stream) as r:
            r.write(i)

    size = stream.tell()
    start_pos = random.random() * size
    stream.seek(start_pos, os.SEEK_SET)
    read_data = [s for s in RecordReader(stream)]
    print 'Original data: ', data
    print 'After seeking to %d, RecordReader returned: %s' % (start_pos, read_data)

rawDescription: Unpickling mid-stream (python)

description: Unpickling mid-stream (python)


code:
from urllib2 import Request, urlopen
from mutagen.mp3 import MP3

def getInfo(url):
    start_byte = 0
    end_byte = 5000
    url = Request(url)
    url.add_header('Range', 'bytes=' + str(start_byte) + '-' + str(end_byte))
    filename = urlopen(url)

    output = open("test_file.mp3",'wb')
    output.write(filename.read())
    output.close()
    audio = MP3("test_file.mp3")

    print audio.info.pprint()

rawDescription: Fetching the title and artist of online MP3 files in Python 2.7


code:
def keyDown(self,e):
    if e.keystring == "f2":
        selectedNumber = ""
    elif e.keystring == "enter":
        print int(selectedNumber) # If you are only printing you don't need int()
        selectedNumber = ""
    else:
        selectedNumber += e.keystring

rawDescription: process multiple key down in python?

description: Process multiple key down in python


code:
import csv
from collections import Counter, defaultdict

FOSSIL = 0   # fossil name is the first column (?)
FORM   = 13  # formation is the 14th column

with open("BivalviaGRDWIS.csv", "rb") as inputfile:
    data = csv.reader(inputfile)
    next(data)  # skip header

    seen = defaultdict(set)

    counts = Counter(
        row[FORM]
        for row in data
        if row[FORM] and row[FORM] not in seen[row[FOSSIL]] and not seen[row[FOSSIL]].add(row[FORM])
    )

print counts

rawDescription: Counting unique elements in sections of .csv columns (Python)


code:
from selenium import webdriver

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to the page
driver.get("http://www.link.cs.cmu.edu/link/submit-sentence-4.html")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is Sentence
inputElement = driver.find_element_by_name("Sentence")

# type in the search
inputElement.send_keys("You're welcome, now accept the answer!")

# submit the form
inputElement.submit()

rawDescription: How to automate interaction for a website with POST method

description: Automate interaction for a website with POST method


code:
import numpy as np

#lats x lons
a2d=np.arange(20).reshape( (4,5) )
print a2d

lats=np.arange(4)
lats2d=np.ones(5)*lats[:,None]
yi=[1,3]
nlats=np.sum(np.bitwise_and(lats>=np.min(yi),lats<=np.max(yi)))

lons=np.arange(5)
lons2d=np.ones(4)[:,None]*lons
xi=[1,2]
nlons=np.sum(np.bitwise_and(lons>=np.min(xi),lons<=np.max(xi)))

#slice= lats2d>=1 & lats2d<=2 & lons2d>=1 & lons2d<=2
s1=np.bitwise_and(lats2d>=np.min(yi),lats2d<=np.max(yi))
s2=np.bitwise_and(lons2d>=np.min(xi),lons2d<=np.max(xi))
slice=np.bitwise_and(s1,s2)
print slice
slice=np.where(slice)
print a2d[slice].reshape( (nlats,nlons) )

import matplotlib.mlab as mlab

print mlab.griddata(lats2d[slice],lons2d[slice],a2d[slice],
#              np.array([1.3,2.1,2.9]),np.array([1.1,1.9]))
            np.array([1,2,3]),np.array([1,2]))

rawDescription: Dynamic 3d array slicing and sorting

description: Dynamic 3d array slicing and sorting


code:
map = {
    'a': ['b', 'c'],
    'b': ['c', 'd'],
    'c': ['d', 'a'],
    'd': []
}

def print_paths(map, start, length, prefix = ''):
    if length == 0:
        print prefix
    else:
        for a in map[start]:
            print_paths(map, a, length - 1, prefix + start)

for a in map.keys():
    print_paths(map, a, 5)

rawDescription: Better equivalent of this crazy nested python for loop


code:
import re

str1 = 'mprage_scan_0006__002.nii'
str2 = 'fgre3d_scan_0005__001+orig.HEAD'
str3 = 'f3dgr2_scan_7_afni_009.nii'
str4 = '2dfgre_scan_09_08.nii'

pattern = r'(?<=scan_)0*(:?\d+)'

for s in [str1, str2, str3, str4]:
    m = re.search(pattern, s)
    print m.group(1)

rawDescription: Finding a Pattern in string

description: Finding a Pattern in string


code:
import Bio
from Bio import Entrez
from Bio import SeqIO
Entrez.email = "mail@example.com"

gi_protein = "GI:588489721"
gi_genome = "GI:588489708"

handle=Entrez.efetch(db="sequences", id=gi_protein,rettype="fasta", retmode="text")
protein = SeqIO.parse(handle, "fasta").next()

handle=Entrez.efetch(db="sequences", id=gi_genome, rettype="gbwithparts", retmode="text")
genome = SeqIO.parse(handle, "gb").next()

#to extract feature with 'id' equal to protein
feature = [f for f in gb.features if "db_xref" in f.qualifiers and gi_protein in f.qualifiers["db_xref"]]

#to get location of CDS
start = feature[0].location.start.position
end = feature[0].location.end.position
strand = feature[0].location.strand

seq = genome[start: end]

if strand == 1:
  print seq.seq
else:
  #if strand is -1 then to get reverse complement
  print seq.reverse_complement().seq

print protein.seq

rawDescription: Biopython retrieving particular CDS from a whole genome


code:
for line in iter(sys.stdin.readline, ""):
    print line

rawDescription: Streaming pipes in Python

description: Streaming pipes in Python


code:
import multiprocessing

try:
    cpus = multiprocessing.cpu_count()
except NotImplementedError:
    cpus = 2   # arbitrary default

def square(n):
    return n * n

pool = multiprocessing.Pool(processes=cpus)
print pool.map(square, xrange(1000))

rawDescription: How to parallelize list-comprehension calculations in Python?

description: Parallelize list-comprehension calculations in Python


code:
hangmanWord = "hello"
visuals = ['_'] *len(hangmanWord)
ended = False
while not ended:
    userInput = raw_input("Enter a letter to guess: ")
    for idx,ch in enumerate(hangmanWord):
        if userInput == hangmanWord[idx]:
            visuals[idx] = userInput
    print visuals
    ended = '_' not in visuals

rawDescription: So I'm trying to find a way to replace the items in a string but I seem to be failing


code:
class MyFileDropTarget(wx.FileDropTarget):

    def __init__(self, mainFrame):
            wx.FileDropTarget.__init__(self)
            self.window = mainFrame

    def OnDropFiles(self, x, y, filenames):
            for fil in filenames:
                    print str(fil)

    def OnEnter(self, x, y, nul):
            self.window.OnDragEnter()
            return wx.DragMove

    def OnLeave(self):
            self.window.OnDragLeave()
            return

    def OnDragOver(self, x, y, nul):
            return wx.DragMove

rawDescription: What is the event I can handle dragged and external file onto my frame


code:
class Class1(object):
    def __init__(self, *args, **kw):
        self.x = Class2("Woo!", self)

class Class2(object):
    def __init__(self, word, creator, *args, **kw):
        self._creator = creator
        print word

rawDescription: Python: Get name of instantiating class?

description: Python: Get name of instantiating class


code:
import sys
import cv2
import numpy

outbasename = 'hexagon_%02d.png'

img = cv2.imread(sys.argv[1])
template = cv2.cvtColor(cv2.imread(sys.argv[2]), cv2.COLOR_BGR2GRAY)
theight, twidth = template.shape[:2]

# Binarize the input based on the saturation and value.
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
saturation = hsv[:,:,1]
value = hsv[:,:,2]
value[saturation > 35] = 255
value = cv2.threshold(value, 0, 255, cv2.THRESH_OTSU)[1]
# Pad the image.
value = cv2.copyMakeBorder(255 - value, 3, 3, 3, 3, cv2.BORDER_CONSTANT, value=0)

# Discard small components.
img_clean = numpy.zeros(value.shape, dtype=numpy.uint8)
contours, _ = cv2.findContours(value, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for i, c in enumerate(contours):
    area = cv2.contourArea(c)
    if area > 500:
        cv2.drawContours(img_clean, contours, i, 255, 2)

def closest_pt(a, pt):
    if not len(a):
        return (float('inf'), float('inf'))
    d = a - pt
    return a[numpy.argmin((d * d).sum(1))]

match = cv2.matchTemplate(img_clean, template, cv2.TM_CCORR_NORMED)

# Filter matches.
threshold = 0.8
dist_threshold = twidth / 1.5
loc = numpy.where(match > threshold)
ptlist = numpy.zeros((len(loc[0]), 2), dtype=int)
count = 0
print "%d matches" % len(loc[0])
for pt in zip(*loc[::-1]):
    cpt = closest_pt(ptlist[:count], pt)
    dist = ((cpt[0] - pt[0]) ** 2 + (cpt[1] - pt[1]) ** 2) ** 0.5
    if dist > dist_threshold:
        ptlist[count] = pt
        count += 1

# Adjust points (could do for the x coords too).
ptlist = ptlist[:count]
view = ptlist.ravel().view([('x', int), ('y', int)])
view.sort(order=['y', 'x'])
for i in xrange(1, ptlist.shape[0]):
    prev, curr = ptlist[i - 1], ptlist[i]
    if abs(curr[1] - prev[1]) < 5:
        y = min(curr[1], prev[1])
        curr[1], prev[1] = y, y

# Crop in raster order.
view.sort(order=['y', 'x'])
for i, pt in enumerate(ptlist, start=1):
    cv2.imwrite(outbasename % i,
            img[pt[1]-2:pt[1]+theight-2, pt[0]-2:pt[0]+twidth-2])
    print 'Wrote %s' % (outbasename % i)

rawDescription: Copy a part of an image in opencv and python


code:
A = [2, 3, [6, 7], 0]
B = [4, 7, 10, 2]

def has_lists(lst):
    return any(isinstance(x, list) for x in lst)

print has_lists(A) # True
print has_lists(B) # False

rawDescription: Know if there are lists of lists or not - Python


code:
import itertools
list_1 = [1,5,4]
list_2 = [2,3,4]
# using list comprehensions
comparisons = [a == b for (a, b) in itertools.product(list_1, list_2)]
sums = [a + b for (a, b) in itertools.product(list_1, list_2)]
# using map and lambda
comparisons = map(lambda (a, b): a == b, itertools.product(list_1, list_2))
sums = map(lambda (a, b): a + b, itertools.product(list_1, list_2))

rawDescription: Compare each item of two lists in Python


code:
from StringIO import StringIO
import csv
import sqlite3 as sq3
from operator import methodcaller, itemgetter
from itertools import groupby

data1 = """068D556A1A665123A6DD2073A36C1CAF
A76EEAF6D310D4FD2F0BD610FAC02C04DFE6EB67
D7C970DFE09687F1732C568AE1CFF9235B2CBB3673EA98DAA8E4507CC8B9A881"""

data2 = """00000040f2213a27ff74019b8bf3cfd1|index.docbook|Redhat 7.3 (32bit)|Linux
00000040f69413a27ff7401b8bf3cfd1|index.docbook|Redhat 8.0 (32bit)|Linux
00000965b3f00c92a18b2b31e75d702c|Localizable.strings|Mac OS X 10.4|OSX
0000162d57845b6512e87db4473c58ea|SYSTEM|Windows 7 Home Premium (32bit)|Windows
000011b20f3cefd491dbc4eff949cf45|totem.devhelp|Linux Ubuntu Desktop 9.10 (32bit)|Linux"""

file1 = StringIO(data1)
file2 = StringIO(data2)

db = sq3.connect(':memory:')
db.execute('create table keys (key)')
db.execute('create table details (key, f1, f2, f3)')

for f1data in file1:
    db.execute('insert into keys values(?)', (f1data.strip(),))

for f2data in file2:
    row = map(methodcaller('strip'), f2data.split('|'))
    db.execute('insert into details values (?,?,?,?)', row)

results = db.execute('select * from keys natural join details')

for key, val in groupby(results, itemgetter(0)):
    print key, list(val)

rawDescription: Finding matches in two files and outputting them


code:
query1 = """START n=node(*)
         MATCH n-[r?]-()
         WHERE ID(n) <> 0
         DELETE n,r
         RETURN COUNT(n)"""

print gdb.query(query1).get_response()
# something like {u'columns': [u'COUNT(n)'], u'data': [[3]]}

rawDescription: NEO4JRESTCLIENT - Deleting all nodes/ rels query issue?


code:
dict_a = OrderedDict((rows[1],rows[2]) for rows in reader)
dict_b = dict((rows[3],rows[4]) for rows in reader)

for key, value in dict_a.iteritems():
    if dict_b[key] == value:
        print value

rawDescription: Iterate over a column containing keys from a dict. Return matched keys from second dict keeping order of keys from first dict


code:
from HTMLParser import HTMLParser
import urllib2

query="http://code.google.com/feeds/issues/p/chromium/issues/full/2"

def get_issue_report(query):
    request = urllib2.Request(query)
    response = urllib2.urlopen(request)
    response_headers = response.info()
    return response.read()

s = get_issue_report(query)

p = HTMLParser()

print p.unescape(s)

p.close()

rawDescription: Decoding response while opening a URL

description: Decoding response while opening a URL


code:
def checkNextID(ID):
    global numOfRuns, curRes, lastResult
    while ID < lastResult:
        try:
            numOfRuns += 1
            if numOfRuns % 10 == 0:
                time.sleep(3) # sleep every 10 iterations
            if isValid(ID + 8):
                parseHTML(curRes)
                ID = ID + 8
            elif isValid(ID + 18):
                parseHTML(curRes)
                ID = ID + 18
            elif isValid(ID + 7):
                parseHTML(curRes)
                ID = ID + 7
            elif isValid(ID + 17):
                parseHTML(curRes)
                ID = ID + 17
            elif isValid(ID+6):
                parseHTML(curRes)
                ID = ID + 6
            elif isValid(ID + 16):
                parseHTML(curRes)
                ID = ID + 16
            else:
                ID = ID + 1
        except Exception, e:
            print "somethin went wrong: " + str(e)

rawDescription: Python: maximum recursion depth exceeded while calling a Python object


code:
parameters = cgi.FieldStorage()
a = parameters[name]
print a

rawDescription: How to pass in values from a XMLHttprequest with .open() into a python script

description: Pass in values from a XMLHttprequest with .open() into a python script


code:
result = re.findall('\s\d+', x)

print result[1]  # 2958
print result[3]  # 3103

rawDescription: python regular expression repeating group matches

description: Python regular expression repeating group matches


rawDescription: How to print or join specific elements of a list (Python)?

description: Print or join specific elements of a list (Python)


code:
from bs4 import BeautifulSoup

tbody = """

9. I want this text and number

"""
soup = BeautifulSoup(tbody)
for item in soup.find_all('td',class_="Details clearfix"):
    print item.div.b.text.strip()

#output= 9. I want this text and number

rawDescription: Retrieving string between whitespace

description: Retrieving string between whitespace


code:
import gtk
import glib

def yieldsleep(func):
    def start(*args, **kwds):
        iterable = func(*args, **kwds)
        def step(*args, **kwds):
            try:
                time = next(iterable)
                glib.timeout_add_seconds(time, step)
            except StopIteration:
                pass
        glib.idle_add(step)
    return start

class Fun(object):
    def __init__(self):
        window = gtk.Window()

        vbox = gtk.VBox()

        btnone = gtk.Button('one')
        btnone.connect('clicked', self.click_one)
        btnone.show()
        vbox.pack_start(btnone)

        btntwo = gtk.Button('two')
        btntwo.connect('clicked', self.click_two)
        btntwo.show()
        vbox.pack_start(btntwo)

        vbox.show()
        window.add(vbox)
        window.show()

    @yieldsleep
    def click_one(self, widget, data=None):
        yield 1 #time.sleep(1)
        print '1'
        yield 1 #time.sleep(1)
        print '2'
        yield 1 #time.sleep(1)
        print '3'

    @yieldsleep
    def click_two(self, widget, data=None):
        yield 1 #time.sleep(1)
        print '4'
        yield 1 #time.sleep(1)
        print '5'
        yield 1 #time.sleep(1)
        print '6'

do = Fun()
gtk.main()

rawDescription: Simple pygtk and threads example please

description: Simple pygtk and threads example please


code:
a = np.array([[1,2,3],[2,4,6],[-1,-2,-3]])
print '\n'.join(''.join(str(cell) for cell in row) for row in a)

rawDescription: Printing Numpy array without any extra notation


code:
from datetime import timedelta

a = timedelta(seconds=6928727.56235)

# a is now datetime.timedelta(80, 16727, 562350)

print "%d days %02d:%02d:%02d" % (a.days, a.seconds / 3600, (a.seconds / 60) % 60, a.seconds % 60)

rawDescription: Python: Seconds since epoch to relative date


code:
import serial
import datetime
import time

###Sensor serial settings
temp = '/dev/ttyAMA0'
tempser = serial.Serial(temp, 9600)
do = '/dev/ttyUSB0'
doser = serial.Serial(do, 9600)

###Temperature Sensor Settings
tempser.write("SF\r")

###DOSensor Settings
doser.write("C,1\r")

###Looop
line = ""
line2 = ""
lineout = ""

while True:
    dodata = doser.read()
    tempdata = tempser.read()
    f=open('2Sensor.txt','a')
    date = datetime.datetime.now()
    timestamp = date.strftime("%Y/%m/%d %H:%M")
    outstring = str(timestamp)+" "+line
    outstring2 = " "+line2+"\n"

    if (dodata == "\r"):
        lineout = " D.O. " + line
        f.write(outstring)
        line = ""

    else:
        line = line + dodata

    if(tempdata == "\r"):
        lineout = lineout + " Temp "+line2
        f.write(outstring2)
        line2 = ""

    else:
        line2 = line2 + tempdata

    if len(lineout) != 0:
        print timestamp + lineout
#       optional if you want to print the same result to a file uncomment the next line and remove the previous f.write instructions
#       f.write(timestamp + lineout)
        lineout = ""

rawDescription: Raspberry Pi 2 / Python + Mutliple Senors output?


code:
if any(key in metakey for key in keywords for metakey in metakeywords):
    print 'ok'

rawDescription: How to match strings even if only they match partially?

description: Match strings even if only they match partially


rawDescription: Place decimal before last two digits for an integer


code:
my_string = "1"
print my_string.zfill(2)   # Prints 01

my_string = "1000"
print my_string.zfill(2)   # Prints 1000

rawDescription: Add leading Zero Python

description: Add leading Zero Python


code:
from datetime import datetime, timedelta
import email
from imapclient import IMAPClient

HOST = 'imap.gmail.com'
USERNAME = 'username'
PASSWORD = 'password'
ssl = True

today = datetime.today()
cutoff = today - timedelta(days=5)

## Connect, login and select the INBOX
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')

## Search for relevant messages
## see http://tools.ietf.org/html/rfc3501#section-6.4.5
messages = server.search(
    ['FROM "Alerts@foobank.com"', 'SINCE %s' % cutoff.strftime('%d-%b-%Y')])
response = server.fetch(messages, ['RFC822'])

for msgid, data in response.iteritems():
    msg_string = data['RFC822']
    msg = email.message_from_string(msg_string)
    print 'ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date'])

rawDescription: Download a specific email from Gmail using Python


code:
from subprocess import Popen, PIPE

(stdout, stderr) = Popen(["cat","foo.txt"], stdout=PIPE).communicate()
print stdout

rawDescription: Redirecting console output to a Python string


code:
from subprocess import Popen, PIPE

cmdline = ['hipi-i2c', 'r', '1', '0x1D', '0x01']
result = Popen(
    cmdline
    , stdin=PIPE
    , stdout=PIPE
    , stderr=PIPE
)
output = result.stdout.readlines()
try:
    X = int(output[0])
except:
    pass

print X

rawDescription: Python-Raspberry pi: get int from popen result


code:
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle

class Annotate(object):
    def __init__(self):
        self.ax = plt.gca()
        self.rect = Rectangle((0,0), 1, 1)
        self.x0 = None
        self.y0 = None
        self.x1 = None
        self.y1 = None
        self.ax.add_patch(self.rect)
        self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press)
        self.ax.figure.canvas.mpl_connect('button_release_event', self.on_release)

    def on_press(self, event):
        print 'press'
        self.x0 = event.xdata
        self.y0 = event.ydata

    def on_release(self, event):
        print 'release'
        self.x1 = event.xdata
        self.y1 = event.ydata
        self.rect.set_width(self.x1 - self.x0)
        self.rect.set_height(self.y1 - self.y0)
        self.rect.set_xy((self.x0, self.y0))
        self.ax.figure.canvas.draw()

a = Annotate()
plt.show()

rawDescription: Matplotlib: draw a selection area in the shape of a rectangle with the mouse


code:
links = browser.find_elements_by_partial_link_text('##')
for link in links:
    print link.get_attribute("href")

rawDescription: How can I get the href of elements found by partial link text?

description: Get the href of elements found by partial link text


code:
import datetime,os,sys
import requests
import time
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def checkInternetConnection(url='http://www.google.com/', timeout=5):
    try:
        req = requests.get(url, timeout=timeout)
        return True
    except requests.ConnectionError:
        print 'Sem conexao com a internet aguarde...'
        return False
def main():
    while checkInternetConnection() == True:
        count = 0
        i = 0
        url = 'www.guiadosquadrinhos.com'
        #driver = webdriver.PhantomJS(executable_path='/home/suedx1000/Dropbox/python/scraping/node_modules/phantomjs/bin/phantomjs', port=65000)
        driver = webdriver.PhantomJS(executable_path=r"C:/Users/UserName/Desktop/plumb/phantomjs.exe", port=65000)
        driver.set_window_size(1120, 550)
        driver.get("http://www.guiadosquadrinhos.com/todas-capas-disponiveis")
        driver.implicitly_wait(50)
        while True:
            pg = driver.page_source
            s = BeautifulSoup(pg, "html.parser")
            for link in s.find_all('a', {'class': 'suppress'}):
                print url+'/'+ link.get('href').encode('utf-8')
                print link.get('title').encode('utf-8')
            count+=1
            print count
            wait = WebDriverWait(driver, 100)
            wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@class='next_last']")))
            wait.until(EC.visibility_of_element_located((By.XPATH, "//div[@class='Lista_album_capas']")))
            pg_next = driver.find_elements_by_xpath("//a[@class='next_last']")
            pg_next[0].click()
        else:
            pass
        time.sleep(1)

        print datetime.datetime.now()
        print "Passamos em "+count+" paginas."
    else:
            print "Esta sem internet ou sua conexao caiu, reiniciando..."
main()

rawDescription: Python Selenium + PhantomJS Erro: (IndexError: list index out of range)


code:
from itertools import izip
with open('data.txt', 'r') as f:
    for line1, line2 in izip(*(iter(f),) * 2):
        print line1, line2

rawDescription: How to jump between lines in for loop in python?

description: Jump between lines in for loop in python


code:
import os
print os.getenv('APPLICATION_ID')
print os.getenv('CURRENT_VERSION_ID')
print os.environ

rawDescription: Determine what project id my App Engine code is running on


code:
a = [e for e in list1 if e.id == "obj1"]
print a

b = [e for e in list1 if e.id != "obj1"]
print b

c = [e for e in list1 if e.id != "obj1" and e.id != "obj2"]
# Or:
# [e for e in list1 if e.id not in ("obj1", "obj2")]
print c

rawDescription: Python: retrieving from list of objects by id


code:
for i in circles[0,:]:

    x = i[0]
    y = i[1]
    radius = i[2]

    # Draw text in image
    font = cv2.FONT_HERSHEY_SIMPLEX
    text = '(' + str(x) + ',' + str(y) + ')'
    cv2.putText(cimg, text,(x + 5, y - 5), font, 0.5, (0, 125, 255), 2)

    # Print circle position
    print 'Circle with radius %.2f is at (%d, %d)' % (radius, x, y)

    cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
    cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),2)

rawDescription: OpenCV & Python : Printing the Red dot pixel value in HoughCircles


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

# Enable interactive plotting
plt.ion()

# Create a figure and axis for plotting on
fig = plt.figure()
ax = fig.add_subplot(111)

# Add the line 'artist' to the plotting axis
# use 'steps' to plot binary codes
line = plt.Line2D((),(),drawstyle='steps-pre')
ax.add_line(line)

# Apply any static formatting to the axis
ax.grid()
ax.set_ylim(-2, 2)
# ...

try:
    limit = int(raw_input('Enter the value of limit:'))
    codelength = int(np.ceil(np.log2(limit)))+1 # see note*
    ax.set_xlim(0,codelength)
    for x in range(0,limit):
        # create your fake data
        y = bin(x)[2:].zfill(codelength)
        data = [int(i) for i in y]
        print data
        xs = range(len(data))

        line.set_data(xs,data)  # Update line data
        fig.canvas.draw()       # Ask to redraw the figure

        # Do a *required* pause to allow figure to redraw
        plt.pause(2)            # delay in seconds between frames
except KeyboardInterrupt:   # allow user to escape with Ctrl+c
    pass
finally:                    # Always clean up!
    plt.close(fig)
    plt.ioff()
    del ax,fig

rawDescription: How do you generate an animated square wave from binary number for the respective decimal number using for loop

description: Generate an animated square wave from binary number for the respective decimal number using for loop


rawDescription: Parsing repeated output using regex and returning multiple values for a single key


code:
import ast
import collections
import nltk
import re
import os

user_name = "UserName"
path = "c://Users/%s/Desktop/corpus files" % user_name

def cleanDoc(doc):
    stopset = set(nltk.corpus.stopwords.words('english'))
    stemmer = nltk.PorterStemmer()
    tokens = nltk.WordPunctTokenizer().tokenize(doc)
    clean = [token.lower() for token in tokens
                           if token.lower() not in stopset and len(token) > 2]
    final = [stemmer.stem(word) for word in clean]
    return final

# phase 1 -- find unique words, create word files, update overall unique word set

corpus_file_pattern = re.compile(r"""file(\d+).txt""")
unique_words = set()
longest_filename = 0
document_nums = []

for filename in os.listdir(path):
    corpus_file_match = corpus_file_pattern.match(filename)
    if corpus_file_match:  # corpus text file?
        if len(filename) > longest_filename:
            longest_filename = len(filename)
        document_num = int(corpus_file_match.group(1))
        document_nums.append(document_num)
        with open(os.path.join(path, filename)) as file:
            data = file.read()
        words = cleanDoc(data)
        unique_words.update(words)
        fd = collections.Counter(words)
        words_filename = "words%d.txt" % document_num
        with open(os.path.join(path, words_filename), mode = 'wt') as fw:
            fw.write(repr(dict(fd)) + '\n')  # write representation as dict

# phase 2 -- create table using unique_words and data in word files

unique_words_list = sorted(unique_words)
unique_words_empty_counter = collections.Counter({word: 0 for word
                                                            in unique_words})
document_nums = sorted(document_nums)
padding = 2  # spaces between columns
min_col_width = 5
col_headings = ["Document"] + unique_words_list
col_widths = [max(min_col_width, len(word))+padding for word in col_headings]
col_widths[0] = longest_filename+padding  # first col is special case

# print table headings
for i, word in enumerate(col_headings):
    print "{:{align}{width}}".format(word, align='>' if i else '<',
                                     width=col_widths[i]),
print

for document_num in document_nums:
    # read word in document dictionary back in
    filename = "words%d.txt" % document_num
    file_words = unique_words_empty_counter.copy()
    with open(os.path.join(path, filename)) as file:
        data = file.read()
    # convert data read into dict and update with file word counts
    file_words.update(ast.literal_eval(data))
    # print row of data
    print "{:<{width}}".format(filename, width=col_widths[0]),
    for i, word in enumerate(col_headings[1:], 1):
        print "{:>{width}n}".format(file_words[word], width=col_widths[i]),
    print

rawDescription: how to create table to find mean of document using python

description: Create table to find mean of document using python


code:
print 'Enter some text.'
foo = raw_input('> ')
print 'You entered "{0}"'.format(foo)

rawDescription: Python 2.7.10: readline causes a leading space on the following line


code:
#!/usr/bin/python

input_list = []

while True:
    input_str = raw_input(">")
    if input_str == "." and input_list[-1] == "":
        break
    else:
        input_list.append(input_str)

for line in input_list:
    print line

rawDescription: Multiline user input python

description: Multiline user input python


code:
# test_client.py
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

# Register the streaming http handlers with urllib2
register_openers()

# headers contains the necessary Content-Type and Content-Length
# datagen is a generator object that yields the encoded parameters
datagen, headers = multipart_encode({
    'categoryID' : 1,
    'cID'        : -3,
    'FileType'   : 'zip',
    'name'       : 'Kylie Minogue',
    'file1'      : open('At the Beach x8-8283.zip')
})

# Create the Request object
request = urllib2.Request("http://localhost:5000/upload_data", datagen, headers)

# Actually do the request, and get the response
print urllib2.urlopen(request).read()

rawDescription: Python 2.6 - Upload zip file - Poster 0.4


code:
import scrapy

class GotP(scrapy.Spider):
    name = "gotp"
    allowed_domains = ["www.sfbay.craigslist.org"]
    start_urls = ["http://sfbay.craigslist.org/search/sss"]

    results_per_page = 100

    def parse(self, response):
        total_count = int(response.xpath('//span[@class="totalcount"]/text()').extract()[0])
        for page in xrange(0, total_count, self.results_per_page):
            yield scrapy.Request("http://sfbay.craigslist.org/search/sss?s=%s&" % page, callback=self.parse_result, dont_filter=True)

    def parse_result(self, response):
        results = response.xpath("//p[@data-pid]")
        for result in results:
            try:
                print result.xpath(".//span[@class='price']/text()").extract()[0]
            except IndexError:
                print "Unknown price"

rawDescription: scrapy crawl multiple pages, extracting data and saving into mysql

description: Scrapy crawl multiple pages, extracting data and saving into mysql


rawDescription: comparing entities of list of lists and get the indexess for lists with more than two similar identities

description: Comparing entities of list of lists and get the indexess for lists with more than two similar identities


code:
def spaced(s):
    text = "hello"
    textLength = len(text)
    lineLength = textLength + 2 * (2 + 1)
    height = 5

    # at this point we know the first and fifth lines are the same and
    # we know the first and fourth are the same.  (reflected against the x-axis)

    hBorder = ""
    for c in range(lineLength):
        if c % 2:
            hBorder = hBorder + '.'
        else:
            hBorder = hBorder + '-'
    spacer = "." + " " * (lineLength - 2) + "."
    fancyText = "-  " + text + "  -"
    return (hBorder, spacer, fancyText, spacer, hBorder)

textTuple = spaced("hello world")
for line in textTuple:
    print line

rawDescription: Border around string python

description: Border around string python


code:
from io import StringIO

indata = StringIO(u"""read_1    +   345995|PACid:16033981   599 AGTAGTAATCAGTCACCCGCAAGGTAGACAAGG   qqqqqqqqqqqqqqqqqqqqq!!qqqqqqqqqq   0
read_2  +   949205|PACid:16054220   338 TACCAGCACTAATGCACCGGATCCCATCAGATC   qqqqqqqqqqqqqqqqqqqqqqqqqqqqqq!!q   0   31:A>T
read_3  +   932004|PACid:16034380   1226    GGCACCTTATGAGAAATCAAAGTTTTTGGGTTC   qqqqqqqqqqqqqqq!!qqqqqqqqqqqqq!!q   3
""")

# that StringIO stuff is just for testing, you should do
# with open('your_file_name', 'r') as indata:
# before the 'for' loop, and then indent the rest one level.

from csv import reader

for line in reader(indata, delimiter='\t'):
    if len(line) > 3:
        line[3] = str(int(line[3]) - 1)
    print '\t'.join(line)

rawDescription: python: How to decrement and update values for a field in delimited text

description: Python: How to decrement and update values for a field in delimited text


code:
import dbus

bus = dbus.SystemBus()
ud_manager_obj = bus.get_object("org.freedesktop.UDisks", "/org/freedesktop/UDisks")
ud_manager = dbus.Interface(ud_manager_obj, 'org.freedesktop.UDisks')

for dev in ud_manager.EnumerateDevices():
    device_obj = bus.get_object("org.freedesktop.UDisks", dev)
    device_props = dbus.Interface(device_obj, dbus.PROPERTIES_IFACE)
    print device_props.Get('org.freedesktop.UDisks.Device', "DriveVendor")
    print device_props.Get('org.freedesktop.UDisks.Device', "DeviceMountPaths")
    print device_props.Get('org.freedesktop.UDisks.Device', "DriveSerial")
    print device_props.Get('org.freedesktop.UDisks.Device', "PartitionSize")

rawDescription: python udisks - enumerating device information

description: Python udisks - enumerating device information


code:
from scipy.special import gamma, beta
x = 10
print gamma(x+0.5)/gamma(x)
print gamma(0.5)/beta(x, 0.5)

rawDescription: Compute Gamma(x+1/2)/Gamma(x)

description: Compute Gamma(x+1/2)/Gamma(x)


code:
ss = ('Found 1 items\n'
      "drwxr-xr-x   - hadoop supergroup          "
      '0 2013-02-16 13:21 /user/hadoop/wiki\n')

('Found **n** items\n'
 '**permissions**   - **username** **group**          '
 '**notsurewhatthisis** **date** **time** **folders(or file)**\n')

import re

r = re.compile('Found +(\d+) +items *\n *(.+?) *- ')

print r.search(ss).groups()

rawDescription: extracting regular expression from string

description: Extracting regular expression from string


code:
a = 123456
b = str(a)
c = []

for digit in b:
    c.append (int(digit))

print c

rawDescription: Convert a number to a list of integers

description: Convert a number to a list of integers


code:
import csv
from collections import deque
filename = 'table.csv'
GROUP_SIZE = 5
THRESHOLD = 40
cond_deque = deque(maxlen=GROUP_SIZE)

with open(filename) as datafile:
    reader = csv.reader(datafile) # assume delimiter=','
    reader.next() # skip header row
    for linenum, row in enumerate(reader, start=1):  # process rows of file
        col0, col1, col4, col5, col6, col23, col24, col25 = (
            float(row[i]) for i in (0, 1, 4, 5, 6, 23, 24, 25))
        cond_deque.append(col1 < THRESHOLD)
        if cond_deque.count(True) == GROUP_SIZE:
            print 'lines {}-{} had {} consecutive rows with col1 < {}'.format(
                linenum-GROUP_SIZE+1, linenum, GROUP_SIZE, THRESHOLD)
            break  # found, so stop looking

rawDescription: What is the best way to sort a sequence in Python?


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

browser = webdriver.PhantomJS()
browser.maximize_window()
browser.get("http://www.jamiiforums.com/kenyan-news/225589-kenyan-and-tanzanian-surburbs.html")

username = browser.find_element_by_id("navbar_username")
password = browser.find_element_by_name("vb_login_password_hint")

username.send_keys("username")
password.send_keys("password")

browser.find_element_by_class_name("loginbutton").click()

wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_element_located((By.XPATH, '//h2[contains(., "Redirecting")]')))
wait.until(EC.title_contains('Kenyan & Tanzanian'))
wait.until(EC.visibility_of_element_located((By.ID, 'postlist')))

link = browser.find_element_by_xpath('//div[@class="vbseo_liked"]/a[contains(@onclick, "return vbseoui.others_click(this)")]')
link.click()

browser.save_screenshot('screenie.png')
print 'success!!'
browser.close()

rawDescription: Screen scraping with selenium 8

description: Screen scraping with selenium 8


code:
import glob
from collections import OrderedDict
import csv
import sys

fieldMap = OrderedDict([
    ('zipcode', ['zipcode', 'ZIPCODE', 'Zip5', 'zip9', 'ZIP', 'zip_code', 'zip']),
    ('firstname', ['firstname', 'FIRSTNAME']),
    ('lastname', ['lastname', 'LASTNAME']),
    ('cust_no', ['cust_no', 'CUST_NO']),
    ('user_name', ['user_name', 'USER_NAME']),
    ('status', ['status', 'STATUS']),
    ('cancel_date', ['cancel_date', 'CANCEL_DATE']),
    ('reject_date', ['reject_date', 'REJECT_DATE']),
    ('streetaddr', ['streetaddr', 'STREETADDR', 'ADDRESS', 'address']),
    ('streetno', ['streetno', 'STREETNO']),
    ('streetnm', ['streetnm', 'STREETNM']),
    ('suffix', ['suffix', 'SUFFIX']),  # suffix of street name: dr, ave, st
    ('city', ['city', 'CITY']),
    ('state', ['state', 'STATE']),
    ('phone_home', ['phone_home',' PHONE_HOME']),
    ('email', ['email', 'EMAIL']),
])

def readFile(fn,ofp):
    with open(fn, 'rb') as csvfile:
        # the following reads the header line into csvReader.fieldnames
        csvReader = csv.DictReader(csvfile, dialect='excel', delimiter=',')
        # create a list of recognized fieldnames in the csv file
        hdrlist = []
        for ft in fieldMap:
            for ft_test in fieldMap[ft]:
                if ft_test in csvReader.fieldnames:
                    hdrlist.append(ft_test)
                    break
            else:
                hdrlist.append(None)  # placeholder (could  also be '')
        hdrlen = len(hdrlist)
        ofp.write('hdrlist: {}\n'.format(hdrlist))
        if hdrlen != len(fieldMap):
            print "Note that not all field names were present in file."

        ofp.write("\t".join(fieldMap) + '\n')
        for row in csvReader:
            fmtstring = "%s\t" * hdrlen % tuple(
                row[field] if field else 'NA' for field in hdrlist)
            ofp.write(fmtstring+'\n')

if __name__ == '__main__':
#    sys.argv = [sys.argv[0], 'ofp_input.csv']  # hardcode for testing
    if len(sys.argv) != 2:
        print "Error:  Filename argument missing!"
        sys.exit(-1)
    filenames = glob.glob(sys.argv[1])
    ofp = sys.stdout
    for filename in filenames:
        readFile(filename, ofp)

rawDescription: Insert blanks into output file pointer (ofp)


code:
import time
import random
a = ['Spooky','Sexy','Snazzy','Random','Wild','Smoggy','Enchanting','Quick','Acoustic','Irritating','Annoying','Thirsty','Fierce','Embarassed','Touch']
b = ['Kurtis','Tisa','Randy','Theda','Dani','Beulah','Dallas','Jeannette','Vera','Kristopher','Donna','Wanda','Sergio','Betsy','Holly']
c = ['1873','123','448','8781','1284','3','45','34']
d = ['Hicks','Ryan','Houston','Cunningham','Ortiz','Baker','Erickson','Pittman','Patrick','Blake','Allison','Taylor','Harper','Romero']
random.shuffle(a)
random.shuffle(b)
random.shuffle(c)
random.shuffle(d)
a = raw_input('Would you like a random username? ')
if a.lower().startswith('y'):
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print 'PROCESSING..'
    time.sleep(.5)
    print a[3] + b[2] + d[5] + c[4]
else:
    print "I didn't have a name for you anyways"

rawDescription: Python doesn't follow the 'if statement'


code:
import string
chars = string.lowercase + string.uppercase

def identifier(x, chars):
    output = []
    base = len(chars)
    while x:
        output.append(chars[x % base])
        x /= base
    return ''.join(reversed(output))

print identifier(1, chars)

rawDescription: python unique string creation

description: Python unique string creation


code:
def actual_kwargs():
    """
    Decorator that provides the wrapped function with an attribute 'actual_kwargs'
    containing just those keyword arguments actually passed in to the function.
    """
    def decorator(function):
        def inner(*args, **kwargs):
            inner.actual_kwargs = kwargs
            return function(*args, **kwargs)
        return inner
    return decorator

if __name__ == "__main__":

    @actual_kwargs()
    def func(msg, a=None, b=False, c='', d=0):
        print msg
        for arg, val in sorted(func.actual_kwargs.iteritems()):
            print '  %s: %s' % (arg, val)

    func("I'm only passing a", a='a')
    func("Here's b and c", b=True, c='c')
    func("All defaults", a=None, b=False, c='', d=0)
    func("Nothin'")
    try:
        func("Invalid kwarg", e="bogon")
    except TypeError, err:
        print 'Invalid kwarg\n  %s' % err

rawDescription: Getting the the keyword arguments actually passed to a Python method


code:
with open('niktoscan.txt') as f:
    content = f.readlines()

foundone = False
print type(content)

cleanedContent = []
for line in content:
    print line

    foundnik = line.find('<niktoscan')
    if not (foundnik != -1 and foundone):
        cleanedContent.append(line)

    if foundnik != -1:
        foundone = True
print "\n\n ########### cleaned content ########### \n\n"

for line in cleanedContent:
    print line

rawDescription: Unable to parse XML file with python.Want to remove a line from python file for it .Help me to remove the line


code:
text = open('file.txt').readlines()

for line in text:
    print line.split(' ')[0]

rawDescription: Reading a line partially from a file

description: Reading a line partially from a file


code:
child_pid = os.fork()
if child_pid == 0:
    print "This is the child."
    sys.exit(0)
print "This is the parent."

rawDescription: What is the Python equivalent of Ruby's fork block?


code:
for key, value in items.iteritems():
    print "{}:\t{}".format(key, value)

rawDescription: Working on json feed with python

description: Working on json feed with python


code:
import Tkinter as tk, tkSimpleDialog

class MyDialog(tkSimpleDialog.Dialog):
    def body(self, master):
        self.geometry("800x600")
        tk.Label(master, text="Enter your search string text:").grid(row=0)

        self.e1 = tk.Entry(master)
        self.e1.grid(row=0, column=1)
        return self.e1 # initial focus

    def apply(self):
        first = self.e1.get()
        self.result = first

root = tk.Tk()
root.withdraw()
test = MyDialog(root, "testing")
print test.result

rawDescription: Increase tkSimpleDialog window size

description: Increase tkSimpleDialog window size


code:
from lxml import etree

def validate(xmlparser, xmlfilename):
    try:
        with open(xmlfilename, 'r') as f:
            etree.fromstring(f.read(), xmlparser)
        return True
    except etree.XMLSchemaError:
        return False

with open(schema_file, 'r') as f:
    schema_root = etree.XML(f.read())

schema = etree.XMLSchema(schema_root)
xmlparser = etree.XMLParser(schema=schema)

filenames = ['input1.xml', 'input2.xml', 'input3.xml']
for filename in filenames:
    if validate(xmlparser, filename):
        print "%s validates" % filename
    else:
        print "%s doesn't validate" % filename

rawDescription: XML (.xsd) feed validation against a schema


code:
import sys
from PyQt4 import QtGui
import sip

class SomeScene(QtGui.QGraphicsScene):
    def __init__(self, parent = None):
        QtGui.QGraphicsScene.__init__(self, parent)

        pixmap = QtGui.QPixmap('someImage')
        item = QtGui.QGraphicsPixmapItem(pixmap)
        self.addItem(item)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent = None):
        QtGui.QMainWindow.__init__(self, parent)

        # This scene will be destroyed because it is local.
        tmpScene = SomeScene()
        tmpScene.destroyed.connect(self.onSceneDestroyed)

        self.scene = SomeScene()
        view = QtGui.QGraphicsView(self.scene)

        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(view)

        mainWidget = QtGui.QWidget()
        mainWidget.setLayout(hbox)

        self.setCentralWidget(mainWidget)

    def onSceneDestroyed(self, obj):
        print 'tmpScene destroyed'

app = QtGui.QApplication(sys.argv)
mainWindow = MainWindow()
mainWindow.show()
sys.exit(app.exec_())

rawDescription: QGraphicsView not displaying in QMainWindow


code:
import re
import xml.sax.saxutils

def anchor_from_email_address_match(match):
    address = match.group(0)
    return "%s" % (
        xml.sax.saxutils.quoteattr("mailto:" + address),
        xml.sax.saxutils.escape(address))

def replace_email_addresses_with_anchors(text):
    return re.sub("\w+@(?:\w|\.)+", anchor_from_email_address_match, text)

print replace_email_addresses_with_anchors(
    "An address: bob@example.com, and another: joe@example.com")

rawDescription: Python: Convert email address to HTML link


code:
from scipy.stats import mvn
import numpy as np
low = np.array([-10, -10])
upp = np.array([.1, -.2])
mu = np.array([-.3, .17])
S = np.array([[1.2,.35],[.35,2.1]])
p,i = mvn.mvnun(low,upp,mu,S)
print p

0.2881578675080012

rawDescription: Multivariate Normal CDF in scipy

description: Multivariate Normal CDF in scipy


rawDescription: Python Compare element in a list with the previous one and the next one


code:
c = []
for i in range(5):
    for j in range(5):
        c.append(a[i] - a[j])
        print c[-1],
    print

rawDescription: How to print 2d lists in python like a C table

description: Print 2d lists in python like a C table


code:
s= "((Random filler goes here))"
print s[2:-2]

rawDescription: finding specific instances of substring in string in Python

description: Finding specific instances of substring in string in Python


code:
import re

myString = 'This is my tweet check it out http://tinyurl.com/blah'

r = re.compile(r'(http://[^ ]+)')
print r.sub(r'\1', myString)

rawDescription: How to transform hyperlink codes into normal URL strings?

description: Transform hyperlink codes into normal URL strings


code:
k = 2 #assign how many top values you want to see
id = 1 #assign queryID
topK = [val for key,val in sorted_dict.items() if key[0] == id][0:k]
print topK

rawDescription: get top items in a sorted dictionary for every sub-key with key as tuple

description: Get top items in a sorted dictionary for every sub-key with key as tuple


code:
addrs = conn.get_all_addresses()
for a in addrs:
    print a.public_ip

rawDescription: aws + boto list of eips

description: Aws + boto list of eips


code:
str = 'If you do not know where you are going, any road will get you there.'
info = {'you': [(3, 3), (25, 3)], 'get you': [(54, 7)], 'know': [(14, 4)]}
replacer = [('you', 'you'), ('know', 'know')]

info2 = {}
replacer2 = {}

for original, replacement in replacer:
    replacer2[original] = replacement

for k, v in info.items():
    for start, length in v:
        replacement = None
        if k in replacer2:
            replacement = replacer2[k]
        info2[start] = (k, length, replacement)

for position in sorted(info2.iterkeys(), reverse=True):
    original, length, replacement = info2[position]
    if replacement is not None:
        str[:position] + replacement + str[position + length]

print str

rawDescription: multiple string replace by position in python

description: Multiple string replace by position in python


code:
mylist = [1,2,3,1,4,4,5,6]
dic = {}

for i in range(0,len(mylist)):
    if mylist[i] in dic:
        dic[mylist[i]].append(i)
    else:
        dic[mylist[i]] = [i]
print dic.values()
# prints [[0, 3], [1], [2], [4, 5], [6], [7]]

rawDescription: How to find 2 items in a list that are the same in Python

description: Find 2 items in a list that are the same in Python


code:
import urllib, json
author = 'Youtube_Username'

foundAll = False
ind = 1
videos = []
while not foundAll:
    inp = urllib.urlopen(r'http://gdata.youtube.com/feeds/api/videos?start-index={0}&max-results=50&alt=json&orderby=published&author={1}'.format( ind, author ) )
    try:
        resp = json.load(inp)
        inp.close()
        returnedVideos = resp['feed']['entry']
        for video in returnedVideos:
            videos.append( video )

        ind += 50
        print len( videos )
        if ( len( returnedVideos ) < 50 ):
            foundAll = True
    except:
        #catch the case where the number of videos in the channel is a multiple of 50
        print "error"
        foundAll = True

for video in videos:
    print video['title'] # video title
    print video['link'][0]['href'] #url

rawDescription: python: get all youtube video urls of a channel

description: Python: get all youtube video urls of a channel


code:
for line in r.readlines():
    line = line.rstrip()      # Remove trailing whitespace.
    if line:                  # Only process non-empty lines.
        ref = line.split();
        print ref[0], ref[1]

rawDescription: Ignore last \n when using readlines with python


code:
from scipy.integrate import quad
from numpy import *
from sympy import *

x = Symbol('x')

#The function we are required to approximate
f1 = -1.0*x
f2 = 1.0*x

phi_0= 1.0
phi_1 = 2.0*x
phi_2 = 4.0*x*x-1

w = 1.0*sqrt(1.0-x*x)

#compute the coefficient

lf10 = lambdify((x,), f1*phi_0*w, 'numpy')
lf11 = lambdify((x,), f1*phi_1*w, 'numpy')
lf12 = lambdify((x,), f1*phi_2*w, 'numpy')

lf20 = lambdify((x,), f2*phi_0, 'numpy')
lf21 = lambdify((x,), f2*phi_1, 'numpy')
lf22 = lambdify((x,), f2*phi_2, 'numpy')

c_0 = quad(lf10, -1, 0)[0] + quad(lf20, 0, 1)[0]
c_1 = quad(lf11, -1, 0)[0] + quad(lf21, 0, 1)[0]
c_2 = quad(lf12, -1, 0)[0] + quad(lf22, 0, 1)[0]

print c_0, c_1, c_2
# 0.833333333333 0.273967584968 0.7

rawDescription: numerical value for an expression in sympy

description: Numerical value for an expression in sympy


code:
import urllib
s1 = u'La+Cit%C3%A9+De+la+West'
type(s1)
print urllib.unquote_plus(s1)
s2 = str(s1)
type(s2)
print urllib.unquote_plus(s2)
import sys
sys.stdout.encoding

rawDescription: Convert url encoded string into python unicode string


code:
import urllib2
from lxml import etree

url = 'http://www.nfl.com/stats/categorystats?archive=false&conference=null&role=OPP&offensiveStatisticCategory=null&defensiveStatisticCategory=INTERCEPTIONS&season=2014&seasonType=REG&tabSeq=2&qualified=false&Submit=Go'
response = urllib2.urlopen(url)
htmlparser = etree.HTMLParser()

tree = etree.parse(response,htmlparser)

text = tree.xpath('//a[contains(text(),"Miami Dolphins")]/parent::td/following-sibling::td[10]/text()')
if text:
    print text[0].strip()

rawDescription: Python Beautiful Soup Scraping Exact Content From Charts


code:
import functools

class static_or_instance(object):
  def __init__(self, func):
    self.func = func

  def __get__(self, instance, owner):
    return functools.partial(self.func, instance)

class C(object):
  @static_or_instance
  def a(self, arg):
    if self is None:
      print "called without self:", arg
    else:
      print "called with self:", arg

C.a(42)
C().a(3)

rawDescription: Static and instance methods in Python

description: Static and instance methods in Python


code:
res = []
for contour in contours:
    contour = cv2.approxPolyDP(contour,10,True)
    print "--------------------------"
    print contour
    print "--------------------------"
    res.append(contour)
print np.vstack(res)

rawDescription: python and combining a multidimensional list (opencv)

description: Python and combining a multidimensional list (opencv)


code:
class Num(object):
    def __init__(self, i):
            self.i = i

print map(Num, range(10))

rawDescription: Python: how to put constructors in map() function?


code:
import sys
print sys.path

rawDescription: where to unzip the pytesser.zip file

description: Where to unzip the pytesser.zip file


code:
import sys
import traceback

class StacktraceOnPrint:

    def __init__(self, orig_stdout, substring):
        self.orig_stdout = orig_stdout
        self.substring = substring

    def write(self, txt):
        if self.substring in txt:
            traceback.print_stack() # OR  import pdb; pdb.set_trace()
        self.orig_stdout.write(txt)

sys.stdout = StacktraceOnPrint(sys.stdout, 'blah')
print 'test ...'
print 'Hello blah.'
print 'test ...'

rawDescription: Python debugging, stop at particular output


code:
import random
correct = 0
incorrect = 0
usedwords = []
print 'Welcome to text twist, you have 14 guesses to get 7 words made up of 4, 5 or 6 letters. Good Luck!'

for i in range(14):
    print "Your letters are 'E' 'P' 'L' 'B' 'E' 'B', what is your guess?"
    answer = raw_input()
    if answer in usedwords:
        print "Sorry, you've already used this word"
    else: #Newly added
        usedwords.append(answer) #We have to add the input words to the list if they are new
        if answer == 'belle' or answer == 'bleep' or answer == 'pebble' or answer == 'beep' or answer == 'bell' or answer == 'peel' or answer == 'peep':
            if answer in usedwords:
                print 'Nice that was one of the words!'
            usedwords.append(answer)
            correct = correct + 1
        if answer != 'belle' and answer != 'bleep' and answer != 'pebble' and answer != 'beep' and answer != 'bell' and answer != 'peel' and answer != 'peep':
            print 'Sorry, that was not one of the words.'
            incorrect = incorrect + 1

print 'Your final score was', correct, 'correct and', incorrect, 'wrong.'

rawDescription: Prevent user from entering the same input twice


code:
# steps not shown where you set up api
u = api.get_user(783214)
print u.screen_name

rawDescription: User ID to Username tweepy

description: User ID to Username tweepy


code:
with open('data.txt') as inf:
  for lc, line in enumerate(inf):  # lc - current line count
    if lc >= 2: # netstat usually has 2 lines of header info
      print ' '.join(line.split()[3:5]) # cols 3-4 are the addresses

rawDescription: Filtering files with python

description: Filtering files with python


code:
ordered_list_one = list(sorted_ls('C:\Users\folder_one\\'))
ordered_list_two = list(sorted_ls('C:\Users\folder_two\\'))

for item_one, item_two in zip(ordered_list_one, ordered_list_two):
    print "The %s likes to eat %s" % (item_one, item_two)

rawDescription: Python print variables from two for loops


code:
tuple_1 = ('asdf@', 'asdf')

tuple_2 = ()

for elem in [item for item in tuple_1 if '@' in item]: tuple_2 = tuple_2 + (elem,)

print tuple_2

rawDescription: Given a tuple t of strings, compute the tuple consisting of all strings in t containing the character @


code:
topCandidate = 20
candidate = 3
for i in range(candidate*2, topCandidate+1, candidate):
  print i

rawDescription: Python: for loop expression

description: Python: for loop expression


code:
#!/usr/bin/env python
rows = 20
cols = 20
pre_list = {}

count = 0
pre_list = {}
for i in xrange(0, rows + 1):
    for j in xrange(0, rows + 1):
        pre_list[(i,j)] = []

for i in xrange(0, rows + 1):
    for j in xrange(0, rows + 1):
        if i > 0:
            pre_list[(i,j)].append((i-1, j))
        if j > 0:
            pre_list[(i,j)].append((i, j-1))

route = {(rows,cols):1}

keys = route.keys()
while True:
    if not keys:
        break

    new_route = {}
    for key in keys:
        # print 'key', key
        pre = pre_list[key]
        if not pre:
            continue
        for item in pre:
            new_route[item] = 1
            # print 'route', route
            if item in route:
                route[item] += route[key]
            else:
                route[item] = route[key]
            # print 'route', route
        route[key] = 0
        # print new_route
    keys = new_route.keys()
    # print 'new keys', keys

print route[(0,0)]

rawDescription: Is there any better solution for projecteuler 15?


code:
import csv
import itertools

f1 = open("file1.csv")
f2 = open("file2.csv")

csv_f1 = csv.reader(f1)
csv_f2 = csv.reader(f2)

for row1, row2 in itertools.izip(csv_f1, csv_f2):
    if row1 == row2:
        print row1, row2
    else:
        print row1, "Invalid"

rawDescription: Read two csv files and compare every line. If the lines match print both lines, if it isn't similar print invalid


code:
from os import listdir
from os.path import isdir, isfile, join
base_path = "gamedata/areas/"
files = listdir(base_path)
only_directories = [path for path in files if isdir(join(base_path,path))]

for directory_path in only_directories:
    dir_path = join(base_path, directory_path)
    for file_path in listdir(dir_path):
        full_file_path = join(base_path, dir_path, file_path)
        is_file = isfile(full_file_path)
        is_example = "example" in file_path
        if is_file and is_example:
            print "Found One!!"

rawDescription: How to check if multiple files exist in different directories

description: Check if multiple files exist in different directories


code:
with open('input.txt') as infile:
    rows = [map(int, line.split()) for line in infile]

def part(minval, maxval):
    return [r for r in rows if minval <= r[0] <= maxval]

with open('output.txt', 'w') as outfile:
    for half in [part(0, 4), part(5, 9)]:
        half.sort(key=lambda (a, b, c): (b, a, c))
        for row in half:
            outfile.write('%s %s %s\n' % tuple(row))

rawDescription: How to set array index in this case?

description: Set array index in this case


code: print browser.current_url

rawDescription: How do I get current URL in Selenium Webdriver 2 Python?

description: Get current URL in Selenium Webdriver 2 Python


code:
def lookup(word):
    word_upper = word.upper()
    found_word = False
    found_def = False
    defn = ''
    with open('dict.txt', 'r') as file:
        for line in file:
            l = line.strip()
            if not found_word and l == word_upper:
                found_word = True
            elif found_word and not found_def and l.startswith("Defn:"):
                found_def = True
                defn = l[6:]
            elif found_def and l != '':
                defn += ' ' + l
            elif found_def and l == '':
                return defn
    return False

print lookup('hallucination')

rawDescription: Creating a dictionary using python and a .txt file


code:
a = "abcdefg"
b ="dgggg"

if set(b) & set(a) == set(b) :
    print "foo"
else:
    print "bar"

rawDescription: Python: Compare 2 strings and see if they contain the same letters


code:
import urllib2

r = urllib2.urlopen('https://graph.facebook.com')
print r.info()
html = r.read()

rawDescription: How do I use Python to make a HTTPS request to this? and get the ACCESS_TOKN back?

description: Use Python to make a HTTPS request to this? and get the ACCESS_TOKN back


code:
customerIndex = { customer['name']:customer for customer in customers }
for row in rows:
    name = row.value('customer','name')
    print name, customerIndex[name]

rawDescription: List comprehension on Python dict doesn't perform well


code: print ' '.join(['word1', 'word2', 'word3'])

rawDescription: Python equivalent for PHP's implode?

description: Python equivalent for PHP's implode


code:
import sys

A = {
    1: [2, 4],
    2: [1, 5],
    3: [4, 5],
    4: [1, 3, 5],
    5: [2, 3, 4],
    6: []
}

num_vertices = len(A)
sys.setrecursionlimit(num_vertices + 2)

mark = [None] * (num_vertices + 1)
cur_path = []

def DFS(vertex, cur_mark = 1):
    mark[vertex] = cur_mark
    global cur_path
    cur_path.append(vertex)
    for neighbour in A[vertex]:
        if not mark[neighbour]:
            DFS(neighbour, 3 - cur_mark)
        elif mark[neighbour] == cur_mark:
            print 'Found a loop of odd length: ', cur_path[cur_path.index(neighbour):]
            sys.exit(0)

    cur_path = cur_path[:-1]

# Visit all connected components
for vertex in xrange(1, num_vertices + 1):
    if not mark[vertex]:
        DFS(vertex)

print 'Cycle of odd length not found'

rawDescription: algorithm to get cycle in graph?

description: Algorithm to get cycle in graph


code:
text = "He was carefully disguised but captured quickly by police."
lines = [text, 't', 'l']
for line in lines:
    if re.findall(r"\w+ly", line):
        print line

rawDescription: Re.findall display row of tekst

description: Re.findall display row of tekst


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

    def movement(self):
        while True:
            print room.userpos
            move = raw_input("[W], [A], [S], or [D]: ").lower() #Movement WASD.
            while True:
                if move == 'w':  #Have only coded the 'w' of the wasd for simplicity.
                    x, y = (1, 0)   #x, y are column, row respectively.  This is done
                    break           ##to apply changes to the player's position on the map.
            a = room.column + x  #a and b represent the changes that take place to player's
            b = room.row + y  ##placement index in the map, or 'tilemap' list.
            room.userpos = tilemap[a][b]
            if room.userpos == 3:
                print "LOOT!"   #Representing what happens when a player comes across
            return room.userpos ##a tile with the value 3 or 'loot'.
            break

class Room(object):
    def __init__(self, column, row):
        self.userpos = tilemap[column][row]
        self.column = column    #Column/Row dictates player position in tilemap.
        self.row = row

floor = 0
entry = 1
exit = 2
loot = 3                #Tile map w/ vairbale names.
tilemap = [[0, 1, 0],   #[floor, entry, floor],
           [3, 0, 0],   #[loot, floor, floor],
           [0, 2, 0]]   #[floor, exit, floor]

room = Room(0, 0)       #Player position for 'room' and 'Room' class -- too similar names
user = Player('Bryce')  #for larger exercices, but I figure I could get away with it here.

def main():     #Loads the rest of the program -- returns user position results.
    inp = raw_input("Press any button to continue.: ")
    if inp != '':
        user.movement()
        print room.userpos
main()

rawDescription: Creating a text-based map for a text based RPG


code:
import sys, bz2, string, os
#instead of hardcoding filename, get it from arguments
#filename = os.getcwd()
filename = raw_input("Enter the path of bz2 document e.g. files/access_log-20130301.bz2: ")
print "Using file : " + filename
with open(filename+"record.txt", "wb+") as fp:
    source_file = bz2.BZ2File(filename, "r")
    for line in source_file:
        #Extract the date and put into a variable
        logdate = string.split(line)[3][1:12]
        #Extract movie name and put into variable movie
        movie = string.split(line)[6]
        #extract who read the movie username =
        usernames = string.split(line)[2]
        #Only process the movie line if we have /media/movie in it.
        if movie.find('media/movies') > 0:
            #Prints all things prosscesed
            print "User:" + usernames + " On:" +  logdate + " Was watching:"+ movie
            #p=open(filename+"record.txt", "w")
            fp.write("User: " + usernames + " On: " +  logdate + " Was watching: "+ movie+" File from:"+filename+"\n")

# The with block has ended at this point, so the file will already be closed here.

sys.exit()

rawDescription: What is wrong with my code? python

description: What is wrong with my code? python


code:
import sys
print sys.executable

rawDescription: Can I find the path to the python executable from inside python?

description: Find the path to the python executable from inside python


code:
from PyQt4 import QtCore, QtGui
import sys

app = QtGui.QApplication([])
table = QtGui.QTableWidget(6,2)

tableItem = QtGui.QLineEdit()
tableItem.setText( "Testing" )

def onTextChanged(text):
    print 'onTextChanged', text
    pass

tableItem.textChanged.connect(onTextChanged)
table.setCellWidget(0, 1, tableItem )
table.show()
app.exec_()

rawDescription: PyQt4 QLineEdit()

description: PyQt4 QLineEdit()


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

    def __getslice__(self, start, stop):
        return [self[n] for n in range(start, stop)]

foo = cyclist([1, 2, 3])
print foo[0:2]    # [1, 2]
print foo[7:13]   # [2, 3, 1, 2, 3, 1]
print foo[0:5]    # [1, 2, 3, 1, 2]

rawDescription: Cyclical indexing of lists in Python

description: Cyclical indexing of lists in Python


rawDescription: Predicting intercept and coefficient for linear regression model for multiple variable


code:
import numpy as np
from scipy import interpolate

def f(x):
    x_points = [ 0, 1, 2, 3, 4, 5]
    y_points = [12,14,22,39,58,77]

    tck = interpolate.splrep(x_points, y_points)
    return interpolate.splev(x, tck)

print f(1.25)

rawDescription: How to perform cubic spline interpolation in python?

description: Perform cubic spline interpolation in python


code:
import pandas as pd
import io

#test data
temp=u"""id|col1|col2|col3
1|13|15|0
1|13|15|1
1|13|15|2
1|12|15|3
1|13|15|4
1|12|15|5
1|12|15|6
2|18|15|7"""

#skip first data rows, keep header
nbr_rows_to_skip = range(1, 2)
nbr_rows_to_read = 6

data_df = pd.read_csv(io.StringIO(temp), delimiter="|", nrows=nbr_rows_to_read, skiprows=nbr_rows_to_skip, dtype={'col1':object,'col2':object,'col3':object, 'col4': object})
print data_df
#   id col1 col2 col3
#0   1   13   15    1
#1   1   13   15    2
#2   1   12   15    3
#3   1   13   15    4
#4   1   12   15    5
#5   1   12   15    6

rawDescription: Keep column names while reading pandas dataframe with nrows and skiprows


code:
print("Please enter the file name for mol bio results + count")
first = raw_input('(Type it here and press enter) : ')

print("Please enter the file name for physics journal list")
second = raw_input(': ')

with open(first, 'r') as f1:
    f1data = f1.readlines()

with open(second, 'r') as f2:
    f2data = f2.readlines()

total = 0
for f1line in f1data:
    i = 0
    # print(len(f1line))
    for f2line in f2data:
        # print(len(f2line))
        if f1line.rstrip() in f2line:
            i += 1
            total += 1
    print f1line + ':' + str(i) + "\n"

print total

rawDescription: used raw_input and doesn't yield results

description: Used raw_input and doesn't yield results


rawDescription: Calculating CRC16 in Python

description: Calculating CRC16 in Python


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

plt.axis([0, 1000, 0, 1])
plt.ion()
plt.show()

for i in range(1000):
    print "in"
    y = np.random.random()
    plt.scatter(i, y)
    plt.pause(0.05)

rawDescription: Matplotlib doesn't open Plotting window when ploting in a cycle


code:
from PyQt4 import QtGui, QtCore
import sys
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)

        self.resize(300, 100)
        self.setLayout(QtGui.QVBoxLayout())

        button = QtGui.QPushButton('Submit')
        button.clicked.connect(self.onclick)
        self.layout().addWidget(button)

    def onclick(self):
        self.close()
        message = " Rich Html Title

The clickable link Google. The lower and upper case text."
        messagebox = QtGui.QMessageBox(QtGui.QMessageBox.Warning, "title", message, parent=self)
        messagebox.addButton("ResetRole Left Most", QtGui.QMessageBox.ResetRole)
        messagebox.addButton("ApplyRole Left", QtGui.QMessageBox.ApplyRole)
        messagebox.addButton("RejectRole Right", QtGui.QMessageBox.RejectRole)
        messagebox.addButton("NoRole Right Most", QtGui.QMessageBox.NoRole)
        exe = messagebox.exec_()
        print 'exe: %s  clickedButton: %s'%(exe, messagebox.clickedButton())

dialog = Dialog()
dialog.show()
app.exec_()

rawDescription: How to change the buttons order in QMessageBox

description: Change the buttons order in QMessageBox


code:
import urllib
import json

response = urllib.urlopen("http://sncf.mobi/infotrafic/iphoneapp/transilien/?gare=BEC")
if response.getcode() == 200:
    data = json.load(response)
    for train in data["D"]:
        print "Train number", train["numerotrain"], "code", train["codevoyageur"]
else:
    print "Failed to load data:", response.getcode()

rawDescription: Mixed-characters strings to ordered list


code:
#!/usr/bin/env python

from random import randrange
import wx
import wx.lib.scrolledpanel

# imports added by GreenAsJade
import time
import threading
from wx.lib.pubsub import setupkwargs
from wx.lib.pubsub import pub

class GUI(wx.Frame):

    def __init__(self, parent, id, title):
        screenWidth = 800
        screenHeight = 450
        screenSize = (screenWidth, screenHeight)
        wx.Frame.__init__(self, None, id, title, size=screenSize)
        self.locationFont = locationFont = wx.Font(15, wx.MODERN, wx.NORMAL, wx.BOLD)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer = sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel = panel = wx.lib.scrolledpanel.ScrolledPanel(self, -1, style=wx.SIMPLE_BORDER)
        panel.SetupScrolling()
        panel.SetBackgroundColour('#FFFFFF')
        panel.SetSizer(sizer)
        mainSizer.Add(panel, 15, wx.EXPAND|wx.ALL)
        self.SetSizer(mainSizer)

        pub.subscribe(self.OnNewLabels, "NEW_LABELS")

    def OnNewLabels(self, labels):
        k = 0
        locations = labels
        print locations
        for i in locations:
            sPanels = 'sPanel'+str(k)
            sPanels = wx.Panel(self.panel)
            label = str(k+1)
            print "doing", label
            text = wx.StaticText(sPanels, -1, label)
            text.SetFont(self.locationFont)
            text.SetForegroundColour('#0101DF')
            self.sizer.Add(sPanels, 0, wx.ALL, 5)
            self.sizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 0)
            k += 1
        self.sizer.Layout()

###############################
#
#

def InterfaceThread(id, log):
    label_generator = Labels()
    while True:
        labels = label_generator.getLabel()   # get the info from the server
        # Tell the GUI about them
        wx.CallAfter(pub.sendMessage, "NEW_LABELS", labels = labels)
        # Tell the logger about them
        wx.CallAfter(log.AppendText, "Sent %s \n" % str(labels))
        time.sleep(5)

class ServerInterface(wx.Frame):

    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title)
        self.log = wx.TextCtrl(self, style = wx.TE_MULTILINE)
        interface_thread = threading.Thread(target = InterfaceThread, args = (1, self.log))
        interface_thread.start()

################################################
class Labels():
    def getLabel(self):
        mylist =[]
        i = 4 #randrange(10)
        for k in range(1,i+1):
            mylist.append(k)
        return mylist
###############################################

if __name__=='__main__':
    app = wx.App()
    frame = GUI(parent=None, id=-1, title="Test")
    frame.Show()
    server_interface = ServerInterface(parent = None, id=-1, title="Server Log")
    server_interface.Show()
    app.MainLoop()

rawDescription: Adding panels to GUI using threading in wxPython


code:
def puzzle():
  print 'hi'

puzzle() is None
def puzzle():
  print 'hi'
  return None

puzzle() is None
def puzzle():
  return 'hi'

puzzle()
puzzle() is None

rawDescription: AttributeError: 'NoneType' object has no attribute 'replace'


code:
from pyparsing import Word, alphas

# define grammar
greet = Word( alphas ) + "," + Word( alphas ) + "!"

# input string
hello = "Hello, World!"

# parse input string
print hello, "->", greet.parseString( hello )

rawDescription: Advice on Python Parser Generators

description: Advice on Python Parser Generators


code:
for t in trends:
    for k, v in t.__dict__.items():
        if not k.startswith('__'):
            print k, v

rawDescription: Best way to dynamically show all keys for arrays in Python

description: Dynamically show all keys for arrays in Python


code:
from random import choice
random_function_selector = [foo, foobar, fudge]

print choice(random_function_selector)()

rawDescription: Choosing a function randomly

description: Choosing a function randomly


code:
def doodie(cls):
    print cls.teststr

class Testarino(object):
    def __init__(self):
        self.teststr = 'Yay!'
        self.callback = None

    def set_callback(self, callback):
        self.callback = callback

    def go(self):
         if self.callback is None:
             raise NotImplementedError
         self.callback(self)

tester = Testarino()
tester.set_callback(doodie)

tester.go()

rawDescription: Creating Python callback via monkey-patching


rawDescription: Python: Change Text Color Based on Var Value


code:
class Some_Class(object):

    def __init__(self):
        def init_property1(value): self.prop1 = value
        def init_property2(value): self.prop2 = value

        self.func_list = [init_property1, init_property2]

    @property
    def prop1(self):
        return 'hey im the first property'

    @prop1.setter
    def prop1(self, value):
        print value

    @property
    def prop2(self):
        return 'hey im the second property'

    @prop2.setter
    def prop2(self, value):
        print value

class Some_Other_Class(object):

    def __init__(self):

        myvalues = ['1 was set by a nested func','2 was set by a nested func']
        some_class= Some_Class()

        # now I simply set the properties without dealing with them individually
        # this assumes I know how they are ordered (in the list)
        # if necessary, I could use a map

        for idx, func in enumerate(some_class.func_list):
            func(myvalues[idx])

        some_class.prop1 = 'actually i want to change the first property later on'

if __name__ == '__main__':
    test = Some_Other_Class()

rawDescription: python reference a property like a function

description: Python reference a property like a function


code:
print df
df2 = pd.get_dummies(df, columns=['Class'])
print df2

rawDescription: Convert a pandas data frame to a pandas data frame with another style


code:
import csv

#Read in data anbd store it in dictionary.
#The element name will be stored in lowercase.
elemen_data = { element.lower():int(weight) for element,weight in csv.reader(open("in", "rb"))}

first = raw_input("Enter first element: ")
second = raw_input("Enter second element: ")

#Look for the weight of the entered element (after converting it to all lowercase)
weight_first = elemen_data.get(first.lower())
weight_second = elemen_data.get(second.lower())

if weight_first is not None:
    print 'First Element:', first, 'Weight:', weight_first
else:
    print 'First Element', first, 'not found'

if weight_second is not None:
    print 'Second Element:', second, 'Weight:', weight_first
else:
    print 'Second Element', second, 'not found'

rawDescription: Getting a specific value from a CSV file with Python raw_input?


code:
from lxml import etree
raw = '''<root>
<Product xmlns="http://mynetwork.products.com/new"/>
<Product xmlns="http://mynetwork.products.com/new" something="else"/>
<Product xmlns="http://mynetwork.products.com/old" />
<Product xmlns=""/>
<Product/>
</root>'''

root = etree.fromstring(raw)
for p in root.findall('*'):
    isValid = len(p.nsmap) == 1 \
        and p.nsmap[None] == 'http://mynetwork.products.com/new' \
        and not p.attrib
    print isValid

rawDescription: How to check xmlns in every element using lxml

description: Check xmlns in every element using lxml


code:
import itertools

with open('file1.txt') as f1, open('file2.txt') as f2:
    for lineno, (line1, line2) in enumerate(itertools.izip(f1, f2), 1):
        if line1 != line2:
            print 'mismatch in line no:', lineno

rawDescription: how to compare two files and print mismatched line number in python?

description: Compare two files and print mismatched line number in python


code:
f = gzip.open(filename, 'rb')
try:
    train_set, valid_set, test_set, dicts = pickle.load(f, encoding='latin1')
except:
    train_set, valid_set, test_set, dicts = pickle.load(f)

print np.shape(train_set)

rawDescription: What is the structure of ATIS (Airline Travel Information System) dataset


code:
from lxml import etree
html = u"""

<title>Test</title>

Example

"""
html = html.encode('utf-8')
parser = etree.HTMLParser()
tree = etree.fromstring(html, parser)
print tree.xpath('//p/@test')[0]
parser = etree.HTMLParser(encoding='utf-8')
tree = etree.fromstring(html, parser)
print tree.xpath('//p/@test')[0]

rawDescription: What is this encoding and how to I convert it?


rawDescription: Changing pixel color in for-loop

description: Changing pixel color in for-loop


code:
while browser.find_element_by_xpath("(//ul[@class='pagination']/li)[last()]").get_attribute("class") != "disabled":
    browser.find_element_by_xpath("(//ul[@class='pagination']/li/a)[last()]").click()
    time.sleep(2)
    print 'something'
else:
    browser.close()

rawDescription: Assign Maximum no. of attempts for while loop


code:
import lxml.etree

xml = lxml.etree.XML("""<something>Fileimage.jpg-(1.61 MB, 1000x1542, what the file is actually called.jpg)
</something>""",)

print xml.xpath("a[preceding-sibling::span/text()='File']/@href")

rawDescription: Python + XPath: Is it possible to select the next element after the one I actually want?


code:
from mako.template import Template
print Template("${a/b}").render(a=1, b=2)
print Template("${a/b}", future_imports=['division']).render(a=1, b=2)
print Template("${'{0:.2f}'.format(a/b)}", future_imports=["division"]).render(a=1, b=2)

rawDescription: How to use mako template for divide?

description: Use mako template for divide


code:
import datetime
from dateutil.parser import parse
print parse('2014 Apr', default=datetime.datetime(2015, 1, 1))

rawDescription: How can I set day to 1 when parsing incomplete date with dateutil.parser?

description: Set day to 1 when parsing incomplete date with dateutil.parser


code:
import sys
print "Content-Type: image/png"
print "Content-Disposition: attachment; filename='pythonchart.png'"
print

if sys.platform == "win32":
    import os, msvcrt
    msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
print open("C:\\wamp\\www\\myproj\\pythonchart.png", "rb").read()

rawDescription: Download image by Python CGI

description: Download image by Python CGI


code:
class IntContainer(int):
    pass
i = IntContainer(3)
print i + 5 # 8
print 4 + i # 7

class StrContainer(str):
    pass
s = StrContainer(3)
print s + '5' # 35
print '4' + s # 43

rawDescription: does python have conversion operators?

description: Does python have conversion operators


code:
from javax.swing import *
from java.awt import *

class Test(JFrame):
    def __init__(self):
        JFrame.__init__(self,
                        'JDesktopPane and JInternalFrame Demo',
                        size=(600, 300),
                        defaultCloseOperation=JFrame.EXIT_ON_CLOSE)

        self.desktop = JDesktopPane()
        self.contentPane.add(JScrollPane(self.desktop)) # This is the same as self.getContentPane().add(...)

        frame = JInternalFrame("Frame", 1, 1, 1, 1, size=(400, 400), visible=1)
        panel = JPanel()

        self.label = JLabel('Hello from Jython')
        panel.add(self.label)

        self.textfield1 = JTextField('Type something here',15,focusGained=self.myOnFocus)

        panel.add(self.textfield1)

        self.textfield2 = JTextField('and click Copy', 15)
        panel.add(self.textfield2)

        copyButton = JButton('Copy',actionPerformed=self.noAction)
        panel.add(copyButton)

        frame.add(panel)
        frame.pack()
        self.desktop.add(frame)

        frame.setSelected(1)
        frame.moveToFront()

    def myOnFocus(self,event):
        print "testing..."

    def noAction (self, event):
        pass

if __name__ == '__main__':
    test = Test()
    test.setLocation(100, 100)
    test.show()

rawDescription: Handling Swing focus events with Jython

description: Handling Swing focus events with Jython


code:
import collections
import multiprocessing
import random
import select
import socket
import time

class MessagePassingProcess(multiprocessing.Process):
    def __init__(self, id_, processes):
        self.id = id_
        self.processes = processes
        self.queue = collections.deque()
        super(MessagePassingProcess, self).__init__()

    def run(self):
        print "Running"
        inputs = []
        outputs = []
        server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        address = self.processes[self.id]["address"]
        print "Process %s binding to %s"%(self.id, address)
        server.bind(address)
        server.listen(5)
        inputs.append(server)
        process = self.processes[self.id]
        process["listening"] = True
        self.processes[self.id] = process
        print "Process %s now listening!(%s)"%(self.id, process)
        while inputs:
            readable, writable, exceptional = select.select(inputs,
                                                           outputs,
                                                           inputs,
                                                           0.1)
            for sock in readable:
                print "Process %s has a readable scoket: %s"%(self.id,
                                                              sock)
                if sock is server:
                    print "Process %s has a readable server scoket: %s"%(self.id,
                                                              sock)
                    conn, addr = sock.accept()
                    conn.setblocking(0)
                    inputs.append(conn)
                else:
                    data = sock.recv(1024)
                    if data:
                        self.queue.append(data)
                        print "non server readable socket with data"
                    else:
                        inputs.remove(sock)
                        sock.close()
                        print "non server readable socket with no data"

            for sock in exceptional:
                print "exception occured on socket %s"%(sock)
                inputs.remove(sock)
                sock.close()

            while len(self.queue) >= 1:
                print "Received:", self.queue.pop()

            # send a message to a random process:
            random_id = random.choice(list(self.processes.keys()))
            print "%s Attempting to send message to %s"%(self.id, random_id)
            random_process = self.processes[random_id]
            print "random_process:", random_process
            if random_process["listening"]:
                random_address = random_process["address"]
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                try:
                    s.connect(random_address)
                except socket.error:
                    print "%s failed to send to %s"%(self.id, random_id)
                else:
                    s.send("Hello World!")
                finally:
                    s.close()

            time.sleep(1)

if __name__=="__main__":
    print "hostname:", socket.getfqdn()
    print dir(multiprocessing)
    manager = multiprocessing.Manager()
    processes = manager.dict()
    joinable = []
    for n in xrange(multiprocessing.cpu_count()):
        mpp = MessagePassingProcess(n, processes)
        processes[n] = {"id":n,
                        "address":("127.0.0.1",7000+n),
                        "listening":False,
                        }
        print "processes[%s] = %s"%(n, processes[n])
        mpp.start()
        joinable.append(mpp)
    for process in joinable:
        process.join()

rawDescription: How to do dynamic creation of per-process queues in Python multiprocessing

description: Do dynamic creation of per-process queues in Python multiprocessing


code:
import json

def openfile():
    d={}
    with open('s.json', 'r') as f:
        for line in f:
            line_ = json.loads(line)
            name = line_.keys()[0]
            status = line_[name].keys()[0]
            ip = line_[name][status]
            d[name] = {'name':name, 'status':status, 'ip':ip}
        f.close()
        return d

data = openfile()

# print whole dict
for ele in data:
   print ele, data[ele]

# retrieving info w.r.t name
print data['www.svetaine.lt']

rawDescription: How to convert Double dict from file and use it

description: Convert Double dict from file and use it


code:
import numpy as np
from matplotlib import pyplot as plt
x = np.random.random(50)
y = np.random.random(50)
a = np.random.random(50)
b = np.random.random(50)
s = c=zip(a, b, np.zeros(50))
ax = plt.gca()
print ax.get_position()
plt.scatter(x,y,c = s)
#adjust limits to make room for inset axes
plt.xlim(xmax = 1.5)
plt.ylim(ymax = 1.5)
#create inset axes
ax = plt.axes([.7, .675, .2, .2], axisbg='y')
n = 20
red = np.linspace(min(a), max(a), n)
green = np.linspace(min(b), max(b), n)
floats = np.linspace(0, 1, n)
#make arrays of all possible values between 0 and 1
X, Y = np.meshgrid(floats, floats)
#stack the arrays with a third array of zeros
Z = np.dstack((X, Y, np.zeros(X.shape)))
Z = np.rot90(Z)
red = np.round(red[::-1], 2)
green = np.round(green, 2)
plt.imshow(Z)
ticks = np.arange(0, n, 6)
plt.yticks(ticks, [red[i] for i in ticks])
plt.xticks(ticks, [green[i] for i in ticks], rotation = 'vertical')
plt.xlabel('Green')
plt.ylabel('Red')
plt.show()

rawDescription: matplotlib scatter plot colour as function of third and fourth variable

description: Matplotlib scatter plot colour as function of third and fourth variable


code:
def get_table_column_1_values_into_a_list(self):
    try:
        WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))
        table_id = WebDriverWait(self.driver, 20).until(EC.presence_of_element_located((By.ID, 'data_configuration_data_previews_ct_fields_body')))

        rows = table_id.find_elements(By.TAG_NAME, "tr")
        return [row.find_element(By.TAG_NAME, "td")[1].text for row in rows]
    except NoSuchElementException, e:
        print e
        return False

rawDescription: Selenium Python the rows of the table from a column is not being stored in my list variable


code:
from Levenshtein import distance, hamming, median
query1 = '你好世界'
query2 = '你好'
print distance(query1,query2)
print distance(query1.decode('utf8'),query2.decode('utf8'))

rawDescription: How is Levenshtein Distance calculated on Simplified Chinese characters?


code:
#!/usr/bin/env python
import sys
class Logger(object):
    def __init__(self, stream):
        self.stream = stream

    def write(self, msg):
        self.stream.write(msg)

    def flush(self):
        pass

if __name__ == '__main__':
    sys.stdout = Logger(sys.stdout)
    print 'User says hello!',
    print 'Goodbye, cruel world!'

rawDescription: Strange behavior of python's print

description: Strange behavior of python's print


code:
from xlrd import open_workbook

data = []

def get_data():
    filename = "Demo.xlsx"
    exl_file = open_workbook(filename)
    sheet = exl_file.sheet_by_index(0)
    for row_index in range(1,4):
            col_a_data = sheet.cell(row_index,0).value
            col_b_data = sheet.cell(row_index,1).value
            data.append({col_a_data:col_b_data});

get_data()

print data

rawDescription: How to achieve one to one mapping between elements just like dictionaries but allowing duplicates also?

description: Achieve one to one mapping between elements just like dictionaries but allowing duplicates also


code:
class Foo(object):
    pass

a = Foo()
b = Foo()
c = Foo()

x = set([a, b])
y = set([a, b])
z = set([a, c])

print x == y # True
print x == z # False

rawDescription: Python: Testing equivalence of sets of custom classes when all instances are unique by definition?


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

plt.ion()       # Enable interactive mode
fig = plt.figure()  # Create figure
axes = fig.add_subplot(111) # Add subplot (dont worry only one plot appears)

axes.set_autoscale_on(True) # enable autoscale
axes.autoscale_view(True,True,True)

l, = plt.plot([], [], 'r-') # Plot blank data
plt.xlabel('x')         # Set up axes
plt.title('test')

k = 5
xdata=[0.5 for i in range(k+1)]     # Generate a list to hold data
ydata=[j for j in range(k+1)]

while True:
    y = float(raw_input("y val :")) #Get new data
    xdata.append(y)     # Append new data to list
    k = k + 1       # inc x value
    ydata.append(k)
    l.set_data(ydata,xdata) # update data
    print xdata     # Print for debug perposes
    print ydata
    axes.relim()        # Recalculate limits
    axes.autoscale_view(True,True,True) #Autoscale
    plt.draw()      # Redraw

rawDescription: Matplotlib update a graph with new data, graph does not show


code:
import numpy as np
import multiprocessing as mp

class Tester:

    num = 0.0
    name = 'none'
    def __init__(self,tnum=num, tname=name):
        self.num  = tnum
        self.name = tname

    def __str__(self):
        return '%f %s' % (self.num, self.name)

def mod(test, nn, out_queue):
    print test.num
    test.num = np.random.randn()
    print test.num
    test.name = nn
    out_queue.put(test)

if __name__ == '__main__':
    num = 10
    out_queue = mp.Queue()
    tests = np.empty(num, dtype=object)
    for it in range(num):
        tests[it] = Tester(tnum=it*1.0)

    print '\n'
    workers = [ mp.Process(target=mod, args=(test, 'some', out_queue) ) for test in tests ]

    for work in workers: work.start()

    for work in workers: work.join()

    res_lst = []
    for j in range(len(workers)):
        res_lst.append(out_queue.get())

    for test in res_lst: print test

rawDescription: Modify object in python multiprocessing

description: Modify object in python multiprocessing


code:
import objc
from Cocoa import NSUserDefaults
defs = NSUserDefaults.standardUserDefaults()
langs = defs.objectForKey_("AppleLanguages")
preferred_lang = langs.objectAtIndex_(0)
print preferred_lang

rawDescription: How to figure out what the current system preferred language is

description: Figure out what the current system preferred language is


code:
print'\xe2\x80\xa6'
print repr("…")

rawDescription: Printing a UTF char by its non-ASCII code in Python


code:
import re
data = "This is a test message I Res O:Myself (KTP:SMTP/me@domain.com) R:KTP:SMS.CLASS/+345854595 id:21"
result = re.findall(r'[\w\.]+(?=:*/)',data)
print result

['SMTP', 'SMS.CLASS']

rawDescription: I would like to extract the words from ":" to slash


code:
import csv
import collections
import datetime as dt

open('temp.csv', 'w').write("""Master #,Scrape,Date of Transaction
2C7E4B,6854585658,5/2/2007
2C7E4B,8283876134,5/8/2007
2C7E4B,4258586585,5/18/2007
C585ED,5554541212,5/18/2004
585868,5555551214,8/16/2012
""")

with open('temp.csv') as f:
    f.next()
    reader = csv.reader(f)
    # map master to list of transactions
    result = collections.defaultdict(list)
    for row in reader:
        key = row[0]
        # make date sortable
        sortable_date = dt.datetime.strptime(row[2], '%m/%d/%Y')
        result[key].append([sortable_date, row[1], row[2]])

for value in result.values():
    # discard old records
    if len(value) > 1:
        value.sort()
        del value[0]
        # or to delete all but the last one
        # del value[:-1]

keys = result.keys()
keys.sort()

for key in keys:
    transactions = result[key]
    for transaction in transactions:
        print key, transaction[1], transaction[2]

rawDescription: Python Dictionary Multiple Keys, Search Function


code:
#!/opt/local/bin/python
import hashlib
import Exscript

from Exscript.util.interact import read_login
from Exscript.protocols import SSH2

account = read_login()              # Prompt the user for his name and password
conn = SSH2()                       # We choose to use SSH2
conn.connect('192.168.221.235')     # Open the SSH connection
conn.login(account)                 # Authenticate on the remote host
conn.execute('conf t')              # Execute the "uname -a" command
conn.execute('interface Serial1/0')
conn.execute('ip address 114.168.221.202 255.255.255.0')
conn.execute('no shutdown')
conn.execute('end')
conn.execute('sh run int Serial1/0')
print conn.response

conn.execute('show ip route')
print conn.response

conn.send('exit\r')                 # Send the "exit" command
conn.close()                        # Wait for the connection to close

rawDescription: Persistent ssh session to Cisco router

description: Persistent ssh session to Cisco router


rawDescription: Creating a new dictionary that contains the number of elements for each key


code:
from itertools import groupby

with open(f, "r") as fin:
    groups = groupby(fin, key=lambda k:k.strip() in ("HEADER", "FOOTER"))
    any(k for k,g in groups)
    content = list(next(groups)[1])
print content

rawDescription: search files in python based on header and footer patterns

description: Search files in python based on header and footer patterns


code:
import libvirt
from xml.etree import ElementTree as ET

URI = "qemu:///system"
VM = "truffles"

# Get the virDomain object
conn = libvirt.open(URI)
domain_object = conn.lookupByName(VM)

# Get the XML description of the VM
vm_xml = domain_object.XMLDesc(0)

# Get the volume in use from the element tree
root = ET.fromstring(vm_xml)
disk_source = root.find('./devices/disk/source')
volume_in_use = disk_source.get('file')

print volume_in_use

rawDescription: libvirt - Determining the volume that a domain uses via API

description: Libvirt - Determining the volume that a domain uses via API


code:
all_events = [
    200.0, 420.0, 560.0, 1100.0, 1900.0, 2700.0, 3400.0, 3900.0, 4234.2, 4800.0]

def get_events_by_hour(all_events):
    return [
        len([x for x in all_events if int(x/3600.0) == hour])
        for hour in xrange(24)
    ]

print get_events_by_hour(all_events)

rawDescription: How to resample / downsample an irregular timestamp list?

description: Resample / downsample an irregular timestamp list


code: print str(11).zfill(3)

rawDescription: Print different value that is stored in MySQL DB


code:
import itertools

def generator():
    for i in range(10):
       yield i

def process(l):
    for i in l:
        if i == 5:
            break

def counter_value(counter):
    import re
    return int(re.search('\d+', repr(counter)).group(0))

counter = itertools.count()
process(i for i, v in itertools.izip(generator(), counter))

print "Element consumed by process is : %d " % counter_value(counter)
# output: Element consumed by process is : 6

rawDescription: Python: Counting the items in a generator consumed by other code


code:
import sys

class Borg(object):
    __shared_state = {}
    def __init__(self, nombre):
        self.__dict__ = self.__shared_state
        self.nombre = nombre

    def instance_method_1(self):
        print 'my instance vr::', self.nombre
        panga = 'pangas'
        print 'te copio con  '
        print panga
        print self.nombre

pingus = Borg('varString')

print pingus.nombre
pingus.instance_method_1()

rawDescription: Accessing a Python instance var from that class


code:
from kafka import KafkaClient
from kafka.protocol.offset import OffsetRequest, OffsetResetStrategy
from kafka.common import OffsetRequestPayload

client = KafkaClient(brokers)

partitions = client.topic_partitions[topic]
offset_requests = [OffsetRequestPayload(topic, p, -1, 1) for p in partitions.keys()]

offsets_responses = client.send_offset_request(offset_requests)

for r in offsets_responses:
    print "partition = %s, offset = %s"%(r.partition, r.offsets[0])

rawDescription: How to get latest offset for a partition for a kafka topic?

description: Get latest offset for a partition for a kafka topic


code:
import string
import re

name = re.compile(r'[a-zA-Z]+') #This will check for alphabet.
yname = raw_input("Your Name:") #ask the user for input.

while not name.match(yname):
    print "invalid characters"
    yname = raw_input("Your Name:")
if 5<=len(yname)<=10:
    print "Hi,", yname, "!"
elif len(yname)>10:
    print "too long!"
elif len(yname)<5:
    print "too short!"

rawDescription: Alphabet check as input

description: Alphabet check as input