code:
import numpy as np

length = 10

coord_r = np.random.rand(length)*10
coord_alpha = np.random.rand(length)*np.pi

# Repeat vector to matrix form
coord_r_X = np.tile(coord_r, [length,1])
coord_r_Y = coord_r_X.T
coord_alpha_X = np.tile(coord_alpha, [length,1])
coord_alpha_Y = coord_alpha_X.T

matDistance = np.sqrt(coord_r_X**2 + coord_r_Y**2 - 2*coord_r_X*coord_r_Y*np.cos(coord_alpha_X - coord_alpha_Y))
print matDistance

rawDescription: Python Numpy efficient Polar euclidean distance


code:
diff = future - today
print diff.days

rawDescription: How much days left from today to given date


rawDescription: Compare i vs other items in a Python list


code:
if request.method == 'POST':
    PostEntries = len(request.POST)
    x = 1
    while x < PostEntries:
            p = Product.objects.get(pk=x)
            p.order = int(request.POST.get(str(x),''))
            print "Itr: " + str(x)
            x = x + 1
            p.save() # NOTE HERE <- saving in loop instead of outside
    print "Product Order saved"
    return HttpResponse("Saved")

rawDescription: .order_by() isn't working how it should / how I expect it to


rawDescription: Inputs going into lists and adding

description: Inputs going into lists and adding


code:
import sys

class Tee(object):
    def __init__(self, input_handle, output_handle):
        self.input = input_handle
        self.output = output_handle

    def readline(self):
        result = self.input.readline()
        self.output.write(result)

        return result

if __name__ == '__main__':
    if not sys.stdin.isatty():
        sys.stdin = Tee(input_handle=sys.stdin, output_handle=sys.stdout)

    a = raw_input('Type something: ')
    b = raw_input('Type something else: ')

    print 'You typed', repr(a), 'and', repr(b)

rawDescription: Input redirection with python

description: Input redirection with python


code:
for i in range(0, len(input)-1):
    print input[i:i+2]

rawDescription: How to iterator over every [:2] overlapping characters in a string of DNA code?

description: Iterator over every [:2] overlapping characters in a string of DNA code


code:
import time
import sys
from PySide import QtCore, QtGui

class Worker(QtCore.QObject):

    send_signal = QtCore.Signal(str) # using PySide
    # QtCore.pyqtSignal(str) ## using PyQt

    # @QtCore.pyqtSlot(str)
    @QtCore.Slot(str)
    def receive_slot(self, data):
        # data could be a filepath
        # open file
        # ... do stuff
        # close file
        QtCore.QThread.sleep(1) # to simulate doing stuff
        self.send_signal.emit(data + ' success')

class Main(QtGui.QWidget):

    send_signal = QtCore.Signal(str)

    def __init__(self):
        super(Main, self).__init__()
        self.worker = Worker()
        self.thread = QtCore.QThread(self)
        self.worker.moveToThread(self.thread)
        self.worker.send_signal.connect(self.receive_slot)
        self.send_signal.connect(self.worker.receive_slot)
        self.thread.start()
        self.send_signal.emit('Start')

    @QtCore.Slot(str)
    def receive_slot(self, data):
        print 'Main: {}'.format(data)
        self.send_signal.emit('Message {}'.format(time.time()))

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.show()
    app.exec_()

rawDescription: Interrupting QThread sleep

description: Interrupting QThread sleep


code:
p = re.compile("^[A-Z]+-[0-9]+ \| .+$", re.DOTALL)
print p.search("test message")
print p.search("DEV-5 | some message")

rawDescription: Python regular expression gives unexpected result


code:
import xml.etree.ElementTree as ET
tree = ET.parse('Tags.xml')
root = tree.getroot()

tags = {tag.attrib['TagName']:tag.attrib for tag in root}

print tags['bayesian']['WikiPostId']
print tags['scales']['Count']

rawDescription: Reading and storing XML data in arrays

description: Reading and storing XML data in arrays


code:
import random
import string
import sys
import os

# handle input
def get_input(msg):
    try:
        return raw_input(msg)
    except EOFError:
        print os.linesep + "user quit."
        sys.exit(0)

name= get_input("what is your name?")
taunts=['taunts1','taunts2','taunts3','taunts4']
point = get_input("Enters the number of points required for a win. ")
human_p = 0
computer_p = 0
selection = ['rock', 'paper', "scissors"]

while human_p < point and computer_p < point:
    computer = random.choice(selection)
    human = get_input("Choose rock, paper, or scissors ? ")
    print name," selects :", human, "computer selects :", computer
    if computer==human:
        print  name+(" score is %d  computer score is %d  A draw" %(human_p, computer_p))

    if (computer == 'rock' and human == 'scissors') or (computer== 'paper' and human == 'rock') or(computer == 'scissors' and human == 'paper'):
        computer_p = computer_p + 1
        print name+(" score is %d  computer score is %d Computer wins" %(human_p, computer_p))
        print random.choice(taunts)

    if (computer == 'rock' and human == 'paper') or (computer== 'paper' and human == 'scissors') or (computer == 'scissors' and human == 'rock'):
        human_p = human_p + 1
        print (name+(" score is %d  computer score is %d Human wins" %(human_p, computer_p)))

print("Final score is : %d  %d and computer %d" % (name,human_p, computer_p))
if human_p == point:
    print ("The overall winner is %d" %name)
elif computer_p == point:
    print ("The overall winner is Computer")

rawDescription: how to exit in the middle of the python program while user press" ctrl+D " for the first time?

description: Exit in the middle of the python program while user press" ctrl+D " for the first time


code:
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header
from email import Charset
Charset.add_charset('utf-8', Charset.QP, Charset.QP, 'utf-8')

maillist = []

def send_email(messages_list, smtpUser=None, smtpPass=None, tls=False):
    failed = []
    try:
        s = smtplib.SMTP('smtp.gmail.com',587)
        s.ehlo()
        if tls:
            s.starttls()
            s.ehlo()
        if smtpUser and smtpPass:
            s.login(smtpUser, smtpPass)
    except:
        print "ehlo failed"
        failed = [x[0] for x in messages_list]
    else:
        for to_address,from_address,subject,encoding,mesg in messages_list:
            try:
                if len(mesg) == 2:
                    msg = MIMEMultipart('alternative')
                else:
                    msg = MIMEText(mesg[0],'plain','utf-8')
                msg['Subject'] = "%s" % Header(subject, 'utf-8')
                if len(from_address) == 2:
                    msg['From'] = "\"%s\" <%s>" % (Header(from_address[0], 'utf-8'), from_address[-1])
                else:
                    msg['From'] = from_address[-1]
                if len(to_address) == 2:
                    msg['From'] = "\"%s\" <%s>" % (Header(to_address[0], 'utf-8'), to_address[-1])
                else:
                    msg['To'] = to_address[-1]
                msg.set_charset("utf-8")
                if len(mesg) == 2:
                    part1 = MIMEText(mesg[0], 'plain','utf-8')
                    part2 = MIMEText(mesg[1], 'html','utf-8')
                    msg.attach(part1)
                    msg.attach(part2)
                s.sendmail(from_address[-1], to_address[-1], msg.as_string())
            except:
                traceback.print_exc()
                failed.append(to_address[-1])
    try:
        s.quit()
    except:
        pass
    return failed

maillist.append(( ['someone@gmail.com'],["Me","noreply@example.com"],'Subject','utf-8',['text_message','html but you can delete this list element'] ))
for k in send_email(maillist, smtpUser='you@gmail.com', smtpPass='pwd', tls=True):
    print k, 'not delivered'

rawDescription: sending emails in a loop In PYTHON 2.7

description: Sending emails in a loop In PYTHON 2.7


code:
recovery = {'full_path': '/home/michael/Data',
 'items': {'UpgradeToCentOS5': {'full_path': '/home/michael/Data/UpgradeToCentOS5',
                                'type': 'f',
                                'versions': ['current', '09/10/12']},
           'basic_debian': {'full_path': '/home/michael/Data/basic_debian',
                            'type': 'f',
                            'versions': ['current']},
           'deploy.tgz': {'full_path': '/home/michael/Data/deploy.tgz',
                          'type': 'f',
                          'versions': ['current']},
           'firewall': {'full_path': '/home/michael/Data/firewall',
                        'type': 'f',
                        'versions': ['current']},
           'legalholder.sh': {'full_path': '/home/michael/Data/legalholder.sh',
                              'type': 'f',
                              'versions': ['current']},
           'lists': {'full_path': '/home/michael/Data/lists',
                     'items': {'boothosts': {'full_path': '/home/michael/Data/lists/boothosts',
                                             'type': 'f',
                                             'versions': ['current']},
                               'checklist': {'full_path': '/home/michael/Data/lists/checklist',
                                             'type': 'f',
                                             'versions': ['current']},
                               'ns_ip': {'full_path': '/home/michael/Data/lists/ns_ip',
                                         'type': 'f',
                                         'versions': ['current']},
                               'server_info': {'full_path': '/home/michael/Data/lists/server_info',
                                               'type': 'f',
                                               'versions': ['current']},
                               'temp': {'full_path': '/home/michael/Data/lists/temp',
                                        'type': 'f',
                                        'versions': ['current']},
                               'tsm_clients': {'full_path': '/home/michael/Data/lists/tsm_clients',
                                               'type': 'f',
                                               'versions': ['current']}},
                     'type': 'd',
                     'versions': ['current']},
           'salt': {'full_path': '/home/michael/Data/salt',
                    'type': 'f',
                    'versions': ['current']},
           'std-srv': {'full_path': '/home/michael/Data/std-srv',
                       'type': 'f',
                       'versions': ['current']},
           'upgrade_debian_6': {'full_path': '/home/michael/Data/upgrade_debian_6',
                                'type': 'f',
                                'versions': ['current']},
           'using-imaps': {'full_path': '/home/michael/Data/using-imaps',
                           'type': 'f',
                           'versions': ['current']},
           'xiv_online_resize': {'full_path': '/home/michael/Data/xiv_online_resize',
                                 'type': 'f',
                                 'versions': ['current']}},
 'type': 'd',
 'versions': ['current']}

import os

def get_parent_dirs(path):
    parents = []
    x = path.split('/')[1:-1]
    for i in range(len(x)):
        parents.append('/'.join(x[0:i+1]))
    return parents

# Get a list of files and versions from the dictionary
items = []
pending = [recovery]
while pending:
    x = pending.pop(0)
    for item in x['items']:
        item = x['items'][item]
        if item.has_key('items'): # a directory
            pending.append(item)
        else: # a file
            items.append((item['full_path'], item['versions']))
items.sort()

parent_dirs = []
for path, versions in items:

    # Print parent directories if necessary
    for directory in get_parent_dirs(path):
        if not directory in parent_dirs:
            parent_dirs.append(directory)
            print '%s /%s' % (len(directory.split('/'))*'-', os.path.split(directory)[1])

    # Print file versions
    directory, filename = os.path.split(path)
    padding = len(path.split('/'))-1
    print '%s %s' % (padding*'-', filename)
    for version in versions:
        print '%s [] Version: %s' % (' '*padding, version)

rawDescription: Build list of directories and files to website


code:
import random

with open("somefile.txt", "rb") as f:
    for line in f:
        print ' '.join(random.sample(line.strip().split(), random.randint(1, 10)))

rawDescription: Printing random number of words per line


code:
menu = raw_input("Please choose a operation from the menu:")

if menu == '1':
  run_the_code()
elif menu == '2':
  exit_the_code()
else:
  print "No such option!"

rawDescription: Creating Menu in Python

description: Creating Menu in Python


code:
# load all categories, then hold onto them
categories = Session.query(Category).all()

for user in Session.query(User):
    print user, user.category  # no SQL will be emitted for the Category

rawDescription: SqlAlchemy look-ahead caching?

description: SqlAlchemy look-ahead caching


code:
#!/usr/bin/env python

__all__ = ["monotonic_time"]

import ctypes, os

CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>

class timespec(ctypes.Structure):
    _fields_ = [
        ('tv_sec', ctypes.c_long),
        ('tv_nsec', ctypes.c_long)
    ]

librt = ctypes.CDLL('librt.so.1', use_errno=True)
clock_gettime = librt.clock_gettime
clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]

def monotonic_time():
    t = timespec()
    if clock_gettime(CLOCK_MONOTONIC_RAW , ctypes.pointer(t)) != 0:
        errno_ = ctypes.get_errno()
        raise OSError(errno_, os.strerror(errno_))
    return t.tv_sec + t.tv_nsec * 1e-9

if __name__ == "__main__":
    print monotonic_time()

rawDescription: How do I get monotonic time durations in python?

description: Get monotonic time durations in python


rawDescription: Python inserting backslashes during regular expression


code:
import os
import sys
import zipfile

if __name__ == "__main__":
    args = sys.argv[1:]

    print "Testing zip file: %s" % args[0]

    the_zip_file = zipfile.ZipFile(args[0])
    ret = the_zip_file.testzip()

    if ret is not None:
        print "First bad file in zip: %s" % ret
        sys.exit(1)
    else:
        print "Zip file is good."
        sys.exit(0)

rawDescription: Python script to check if a zip file is corrupt


code:
def subs():
    string1 = 'abcdef'
    string2 = 'abcijk'
    subs = [string1[s:s+3] for s in range(len(string1)) if len(string1[s:s+3]) > 2]
    for s in subs:
        print s, string2.count(s)

rawDescription: Python: Slicing string in three-character substrings


code:
import code

def show(input):
    lines = iter(input.splitlines())

    def readline(prompt):
        try:
            command = next(lines)
        except StopIteration:
            raise EOFError()
        print prompt, command
        return command

    code.interact(readfunc=readline)

import sys
if __name__=="__main__":
    show(open(sys.argv[1]).read())

rawDescription: Simulate interactive python session

description: Simulate interactive python session


code:
import matplotlib.pyplot as plt
import scipy
from scipy.optimize import curve_fit, minimize, leastsq
from scipy.integrate import quad
from scipy import pi, sin
x = scipy.linspace(0, pi, 100)
y = scipy.sin(x) + (0. + scipy.rand(len(x))*0.4)
def func1(x, a0, a1, a2, a3):
    return a0 + a1*x + a2*x**2 + a3*x**3

# here you include the penalization factor
def residuals(p,x,y):
    integral = quad( func1, 0, pi, args=(p[0],p[1],p[2],p[3]))[0]
    penalization = abs(2.-integral)*10000
    return y - func1(x, p[0],p[1],p[2],p[3]) - penalization

popt1, pcov1 = curve_fit( func1, x, y )
popt2, pcov2 = leastsq(func=residuals, x0=(1.,1.,1.,1.), args=(x,y))
y_fit1 = func1(x, *popt1)
y_fit2 = func1(x, *popt2)
plt.scatter(x,y, marker='.')
plt.plot(x,y_fit1, color='g', label='curve_fit')
plt.plot(x,y_fit2, color='y', label='constrained')
plt.legend(); plt.xlim(-0.1,3.5); plt.ylim(0,1.4)
print 'Exact   integral:',quad(sin ,0,pi)[0]
print 'Approx integral1:',quad(func1,0,pi,args=(popt1[0],popt1[1],
                                                popt1[2],popt1[3]))[0]
print 'Approx integral2:',quad(func1,0,pi,args=(popt2[0],popt2[1],
                                                popt2[2],popt2[3]))[0]
plt.show()

#Exact   integral: 2.0
#Approx integral1: 2.60068579748
#Approx integral2: 2.00001911981

rawDescription: How do I put a constraint on SciPy curve fit?

description: Put a constraint on SciPy curve fit


code:
def hi():
    cluster = Cluster(['127.0.0.1'])
    metadata = cluster.metadata
    session = cluster.connect("sujata")
    global queue
    while True:
        y=queue.get()
        if y=="exit":
            os._exit(0)
        else:
            print y
            session.execute(y)

rawDescription: Executing a cassandra insert query through Python multiprocessing queue


code:
with open('input.txt', 'rU') as fp:
  for line in fp:
    print line

rawDescription: Python: Specify end-of-line format for reading files


code:
def inversio(interes_fix_anual):
    interes_fix_anual=float(interes_fix_anual)
    money = input('Write here your credit ')
    interes = interes_fix_anual / 100.0
    years = 0
    inversion = money
    while inversion <= (money * 2):
        inversion = inversion + (inversion * interes)
        years = years+1
        print 'money=' + str(money) + ' inversion=' + str(inversion) + ' years=' + str(years)
    return years
inversio(4)

rawDescription: Calculate how many years need your money to duplicate in the bank inputing the interest


rawDescription: Aligning individual columns in pandas to_latex


code:
import numpy as np
from scipy.stats import pareto
b = 0.9
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)
np.random.seed(seed=233423)
print pareto.rvs(b, loc=0, scale=1, size=5)

rawDescription: scipy.stats seed?

description: Scipy.stats seed


code:
sensortag=0
while sensortag != "B4:99:4C:64:33:E0":
    #call the command and write to scan.txt file and then fill the process.
    #loop to find if the MAC address given is available
    os.system("hcitool lescan> scan.txt & pkill --signal SIGINT hcitool")
    scan = open("scan.txt","r")
    readscan = scan.read()
    if "B4:99:4C:64:33:E0" in readscan:
        print "SensorTag found."
        sensortag = "B4:99:4C:64:33:E0"

rawDescription: Hcitool lescan will not print in real time to a file


code:
def gold_room():
    print "This room is full of gold. How much do you take?"

    next = raw_input("> ")
    try:
        how_much = int(next)
    except:
        dead("Man, learn to type a number.")
        return # I generally put a "return" statement in places like this one.

    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

rawDescription: Python help: Evaulating if statements

description: Python help: Evaulating if statements


code:
import traceback

def magic():
   return traceback.extract_stack()[-2][2]

class Something(object):
   print magic()

rawDescription: Python name of class in class body

description: Python name of class in class body


code:
class Queue:
    def __init__(self):
        self.items = []
    def enqueue(self, item):
        self.items.append(item)
    def __str__(self):
        '''Allow print to be called on the queue object itself'''
        return str(self.items)
    def __getitem__(self, i):
        '''Allow the queue object to be indexable directly'''
        return self.items[i]

def mirror(q):
    return q[::-1]

q = Queue()
for i in range(10):
    q.enqueue(i)

print q
print mirror(q)

rawDescription: Mirrorring a queue in python

description: Mirrorring a queue in python


code:
fields = ('fps', 'sps', 'tps', 'fpp', 'spp', 'tpp')
for r in results.values():
    print 'Row with id=%s:' % r['id']
    for f in fields:
        if word_name in str(r[f]):
            print '  Field %s matches' % f

rawDescription: Get field name from an object retrieved by orm query


rawDescription: combinations of boolean variables - no repeats

description: Combinations of boolean variables - no repeats


code:
import json
tree = ElementTree.fromstring(js_String).getroot() #get the root
#use etree.find or whatever to find the text you need in your html file
script_text = tree.text.strip()

#extract json string
#you could use the re module if the string extraction is complex
json_string = script_text.split('var exampleVar =')[1]
#note that this will work only for the example you have given.
try:
    data = json.loads(json_string)
except ValueError:
    print "invalid json", json_string
else:
    value = data['abo']['param3']['param3b1']

rawDescription: How can I retrieve a JavaScript variable using Python?

description: Retrieve a JavaScript variable using Python


code:
#you need to explicitly name the columns of the incoming parameter in the append statement
df = DataFrame(columns=['col1', 'col2'])
print df.append(Series([None]*2, index=['col1','col2']), ignore_index=True)

#as an aside

df = DataFrame(np.random.randn(8, 4), columns=['A','B','C','D'])
dfRowImproper = [1,2,3,4]
#dfRowProper = DataFrame(arange(4)+1,columns=['A','B','C','D']) #will not work!!! because arange returns a vector, whereas DataFrame expect a matrix/array#
dfRowProper = DataFrame([arange(4)+1],columns=['A','B','C','D']) #will work

print df.append(dfRowImproper) #will make the 0 named column with 4 additional rows defined on this column

print df.append(dfRowProper) #will work as you would like as the column names are consistent

print df.append(DataFrame(np.random.randn(1,4))) #will define four additional columns to the df with 4 additional rows

print df.append(Series(dfRow,index=['A','B','C','D']), ignore_index=True) #works as you want

rawDescription: Appending row to Pandas DataFrame adds 0 column


code:
if os.access("/data/lims/LI", os.X_OK):
     print "pass"

rawDescription: Checking permission of a file using python


code:
import re
a = re.compile('(my|your) regex')
match = a.search('Somewhere in this string is my regex; not much different from your regex.')
print match.end()
print match.end(1)

rawDescription: I want to know the last index of a regex match in python's re lib


code:
import random

list1 = set()
list2 = set()
player = 0, 0

while len(list1) != 6:
    item1 = (random.randint(0, 5), random.randint(0, 5))
    if item1 == player:
        continue

    list1.add(item1)

while len(list2) != 6:
    item2 = (random.randint(0, 5), random.randint(0, 5))
    if (item2 == player) or (item2 in list1):
        continue

    list2.add(item2)

print list1
print list2

rawDescription: Making lists with different values Python 3.4.3


rawDescription: How can I extract x, y and z coordinates from geographical data by Python?

description: Extract x, y and z coordinates from geographical data by Python


code:
def solve( a ) :
    """Sudoku solver"""

    # grid solved
    if np.sum( a == 0 ) == 0 :
        print "Grid solved"
        print a
        return a

    else :

        #
        # Focus on the 1st 0
        tmp_where = np.where( a == 0 )
        i, j = tmp_where[ 0 ][ 0 ], tmp_where[ 1 ][ 0 ]

        #
        for e in candidates( a, i, j ) :
            #
            tmp = a.copy()
            tmp[ i, j ] = e
            #
            res = solve( tmp )
            if res: #a failed path will return None, a success the solved matrix
                return res

rawDescription: My sudoku solver always returns None

description: My sudoku solver always returns None


code:
import json

json_document='''
{
  "originalRequest": {
    "category": {}
  },
  "totalResultSize": 209,
  "products": [
    {
      "id": "1000004006560322",
      "ean": "0828768235928",
      "gpc": "music",
      "title": "title",
      "specsTag": "tag",
      "summary": "summary",
      "rating": 45,
      "urls": [
        {
          "key": "DESKTOP",
          "value": "http://www.url.com"
        },
        {
          "key": "MOBILE",
          "value": "https://m.url.com"
        }
      ]
    }
  ]
}
'''

python_obj = json.loads(json_document)

for url in python_obj["products"][0]["urls"]:
    if url["key"] == "MOBILE":
        value = url["value"]
        break
else:
    # Some default action
    print "No url found"
    value = "http://www.url.com"

print "Value:", value

rawDescription: how to select specific json element in python

description: Select specific json element in python


code:
import boto

connection = boto.connect_s3()
bucket = connection.get_bucket('myBucketName')

fileKey = bucket.get_key('myFileName.txt')
print fileKey.get_contents_as_string()

for key in bucket.list('myFolderName'):
    print key.get_contents_as_string()

rawDescription: Import specific file from an S3 subfolder into Python


code:
import re
print dict(re.findall('TEXTSTART\[([^\]]+)\](.*?)TEXTEND', report, re.DOTALL))

rawDescription: Reading substrings from string in Python


code:
def extractIPs(fileContent):
    pattern = r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})"
    ips = [each[0] for each in re.findall(pattern, fileContent)]
    for item in ips:
        location = ips.index(item)
        ip = re.sub("[ ()\[\]]", "", item)
        ip = re.sub("dot", ".", ip)
        ips.remove(item)
        ips.insert(location, ip)
    return ips

myFile = open('***INSERT FILE PATH HERE***')
fileContent = myFile.read()

IPs = extractIPs(fileContent)
print "Original file content:\n{0}".format(fileContent)
print "--------------------------------"
print "Parsed results:\n{0}".format(IPs)

rawDescription: Python - parse IPv4 addresses from string (even when censored)


code:
import foolib
print foolib.__file__

rawDescription: How can I tell what directory an imported library comes from in python?

description: Tell what directory an imported library comes from in python


code:
regex=''.join(postfix)

keys=list(set(re.sub('[^A-Za-z0-9]+', '', regex)+'e'))

s=[];stack=[];start=0;end=1

counter=-1;c1=0;c2=0

for i in regex:
    if i in keys:
        counter=counter+1;c1=counter;counter=counter+1;c2=counter;
        s.append({});s.append({})
        stack.append([c1,c2])
        s[c1][i]=c2
    elif i=='*':
        r1,r2=stack.pop()
        counter=counter+1;c1=counter;counter=counter+1;c2=counter;
        s.append({});s.append({})
        stack.append([c1,c2])
        s[r2]['e']=(r1,c2);s[c1]['e']=(r1,c2)
        if start==r1:start=c1
        if end==r2:end=c2
    elif i=='.':
        r11,r12=stack.pop()
        r21,r22=stack.pop()
        stack.append([r21,r12])
        s[r22]['e']=r11
        if start==r11:start=r21
        if end==r22:end=r12
    else:
        counter=counter+1;c1=counter;counter=counter+1;c2=counter;
        s.append({});s.append({})
        r11,r12=stack.pop()
        r21,r22=stack.pop()
        stack.append([c1,c2])
        s[c1]['e']=(r21,r11); s[r12]['e']=c2; s[r22]['e']=c2
        if start==r11 or start==r21:start=c1
        if end==r22 or end==r12:end=c2

print keys

print s

rawDescription: How can I convert a regex to an NFA?

description: Convert a regex to an NFA


code:
from bs4 import BeautifulSoup as Soup

def BrightstormPageTest():
    soup = Soup(urllib.urlopen('http://brightstorm.com/science/chemistry/chemical-reaction-rates/collision-theory/').read())
    sections = soup.findAll("section")
    for section in sections:
        ps = section.findAll("p")
        for p in ps:
            print p.text

def BrightstormPageTest2():
    soup = Soup(urllib.urlopen('http://brightstorm.com/science/chemistry/chemical-reaction-rates/collision-theory/').read())
    sections = soup.findAll("section")
    for section in sections:
        while True:
             try:
                 print section.nextSibling.text
             except TypeError:
                 # .text is a valid method on a <p> element, but not a NavigableString.
                 break

rawDescription: Return an uncertain number of paragraphs in BeautifulSoup


code:
import Image
import cStringIO
import math
import os
import stat

# try no more than 10 times, then give up
MAX_TRIES = 10

def getThumbnail(filename, max_bytes=(10*1024)):
    '''Get a thumbnail image of filename, <max_bytes'''
    original_size = os.stat(filename)[stat.ST_SIZE]
    print "Original file size: %.1f KB" % (original_size/1024.)
    image = Image.open(filename)
    image.load()
    print "Original image size: %dx%d pixels" % image.size
    min_bytes = int(0.9 * max_bytes)
    largest_side = max(image.size)
    smallest_side = 16
    for attempt in range(MAX_TRIES):
        try_side = (largest_side + smallest_side) / 2
    print "Attempt #%d of %d" % (attempt+1, MAX_TRIES)
    print "Side must be within [%d:%d], now trying %d" % (
        smallest_side, largest_side, try_side)
    thumb = image.copy()
    thumb.thumbnail((try_side,try_side), Image.ANTIALIAS)
    afile = cStringIO.StringIO()
    thumb.save(afile, "PNG")
    resulting_size = len(afile.getvalue())
    afile.close()
    print "Reduced file size: %.1f KB" % (resulting_size/1024.)
    print "Reduced image size: %dx%d pixels" % thumb.size
    if min_bytes <= resulting_size <= max_bytes:
        print "Success!"
        return thumb
    elif resulting_size > max_bytes:
        print "Too large (>%d), reducing more" % max_bytes
        largest_side = try_side
    else:
        print "Too small (<%d), reducing less" % min_bytes
        smallest_side = try_side
    print "too many attempts, returning what I've got!"
    return thumb

def main():
    thumb = getThumbnail("pyth.png")
    print "Reduced image size: %dx%d pixels" % thumb.size
    print "Saving to thumb.png"
    thumb.save("thumb.png")
    thumb_size = os.stat("thumb.png")[stat.ST_SIZE]
    print "Reduced file size: %.1f KB" % (thumb_size/1024.)
    print "Done, bye!"

if __name__ == '__main__':
    main()

rawDescription: Generating an image thumbnail that is <10KB and did not lose proportions


code:
import gc
#garbage collector should do the trick
#all the other code
for obj in gc.get_objects():
    if isinstance(obj, Fruit):
        print "object name :",obj.name
        printall(obj)

rawDescription: Iteration on members and attributes on a Class


code:
word='www.wgoowglew.cowm'

print '.' + word.split('.')[1].replace('w', '') + '.'

rawDescription: Remove lowercase letters from a string in python


code:
uvals, uidx = np.unique(w, return_inverse=True)

output = np.zeros_like(uvals)
np.add.at(output, uidx, z.ravel())

print uvals
# [-4. -3. -2. -1.  0.  1.  2.  3.  4.]
print output
# [  5.  14.  26.  40.  55.  40.  26.  14.   5.]

rawDescription: python summing over 2d array to get a 1d array

description: Python summing over 2d array to get a 1d array


code:
prefixed_bits = "extract this double:\xc2\x8eET\xfb!\t@"
pos = prefixed_bits.index(":") + 1
print "That looks like the value %f" % struct.unpack("d", prefixed_bits[pos:pos+8])

rawDescription: Reading parts of binary data in a string in Python


code:
import msvcrt
while 1:
    print 'Testing..'
    if msvcrt.kbhit():
        if ord(msvcrt.getch()) == 32:
            break

rawDescription: How to break a for loop when pressing space bar?

description: Break a for loop when pressing space bar


code:
import datetime
import itertools

accrual_months_days = (1,1), (4,1), (7,1), (10,1)

def accruals(begin_date, end_date, hours_per=8):
  """Vacation accrued between begin_date and end_date included."""
  cur_year = begin_date.year - 1
  result = 0
  for m, d in itertools.cycle(accrual_months_days):
    if m == 1: cur_year += 1
    d = datetime.date(cur_year, m, d)
    if d < begin_date: continue
    if d > end_date: return result
    result += hours_per

if __name__ == '__main__':  # examples
  print accruals(datetime.date(2010, 1, 12), datetime.date(2010, 9, 20))
  print accruals(datetime.date(2010, 4, 20), datetime.date(2012, 12, 21))
  print accruals(datetime.date(2010, 12, 21), datetime.date(2012, 4, 20))

rawDescription: "Passing Go" in a (python) date range

description: "Passing Go" in a (python) date range


code:
for w in words:
    if w['case']:
        r = re.compile("(^|\s)#?%s" % w['word'], re.IGNORECASE|re.MULTILINE)
    else:
        r = re.compile("(^|\s)#?%s" % w['word'], re.MULTILINE)
    if r.search(status):
        print "Found word %s" % w['word']

rawDescription: Python regex multiple search

description: Python regex multiple search


code:
import time
import multiprocessing

def multi_thread(files):
    m = multiprocessing.Manager()
    q = m.Queue()

    for f in files:
        q.put(f)

    p = multiprocessing.Pool(5)
    for i in range(5):
        p.apply_async(worker_test, args=(i, q))

    p.close()
    p.join()

def worker_test(i, q):
    print 'hello'
    print i

def main():
    files = ['a', 'b', 'c', 'd']

    multi_thread(files[0:4])

if __name__ == '__main__':
    main()

rawDescription: multiprocessing apply_async strange behaviour with multiprocessing queue

description: Multiprocessing apply_async strange behaviour with multiprocessing queue


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

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

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

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

show()

rawDescription: Possible to make labels appear when hovering over a point in matplotlib?


code:
pattern = r'(\url{).*/([^/]*/[^/]*})'
re.sub(pattern, prefix, '\url{www.mywebsite.com/home/us/index.html}')

rawDescription: Substitution regex with groupings

description: Substitution regex with groupings


code:
while True:
        human_move, bot_move = human(), bot()
        if human_move == bot_move:
                print "Grr... a tie!"
        elif (human_move, bot_move) in winning_combos:
                print "Woo-hoo! You win this round, human"
        else:
                print "Bwahahaha! The almighty bot wins!"

rawDescription: Comparing the Output of Functions Correctly


code:
from datetime import date
import calendar
today = date.today()

first_of_month = today.replace(day=1)
_, number_of_days_in_month = calendar.monthrange(first_of_month.year, first_of_month.month)

for i in range(1, number_of_days_in_month+1):
    newdate = today.replace(day = i)
    print newdate #prints all dates in months

#Lets calculate difference between 14th of this month and 1st
randomdate = today.replace(day=14)
delta = randomdate - first_of_month
print delta.days

rawDescription: get dates of present month in a list and difference in dates

description: Get dates of present month in a list and difference in dates


code:
class Item(object):
    def __init__(self):
        self.broken = False

    def smash(self):
        print "This object broke."
        self.broken = True

class Person(object):
    def __init__(self, holding):
        self.holding = holding

    def using(self):
        if self.holding.broken != True:
            print "Pass."
        else:
            print "Fail."

Foo = Person(Item())
Bar = Person(Item())

Foo.holding.smash()
Foo.using()
Bar.using()

rawDescription: Instantiating a unique object every time when using object composition?


code:
class SSHTunnel(object):
    """
    A context manager implementation of an ssh tunnel opened from python

    """

    def __init__(self, tunnel_command):

        assert "-fN" in tunnel_command, "need to open the tunnel with -fN"
        self._tunnel_command = tunnel_command
        self._delay = 0.1

    def create_tunnel(self):

        tunnel_cmd = self._tunnel_command
        import time, psutil, subprocess
        ssh_process = subprocess.Popen(tunnel_cmd,  universal_newlines=True,
                                                    shell=True,
                                                    stdout=subprocess.PIPE,
                                                    stderr=subprocess.STDOUT,
                                                    stdin=subprocess.PIPE)

        # Assuming that the tunnel command has "-f" and "ExitOnForwardFailure=yes", then the
        # command will return immediately so we can check the return status with a poll().

        while True:
            p = ssh_process.poll()
            if p is not None: break
            time.sleep(self._delay)

        if p == 0:
            # Unfortunately there is no direct way to get the pid of the spawned ssh process, so we'll find it
            # by finding a matching process using psutil.

            current_username = psutil.Process(os.getpid()).username
            ssh_processes = [proc for proc in psutil.get_process_list() if proc.cmdline == tunnel_cmd.split() and proc.username == current_username]

            if len(ssh_processes) == 1:
                self.ssh_tunnel = ssh_processes[0]
                return ssh_processes[0]
            else:
                raise RuntimeError, 'multiple (or zero?) tunnel ssh processes found: ' + str(ssh_processes)
        else:
            raise RuntimeError, 'Error creating tunnel: ' + str(p) + ' :: ' + str(ssh_process.stdout.readlines())

    def release(self):
        """ Get rid of the tunnel by killin the pid
        """
        self.ssh_tunnel.terminate()

    def __enter__(self):
        self.create_tunnel()
        return self

    def __exit__(self, type, value, traceback):

        self.release()

    def __del__(self):
        self.release()

def test():
    #do things that will fail if the tunnel is not opened

    print "done =========="

command = "ssh username@someserver.com -L %d:localhost:%d -p 222 -fN" % (someport, someport)

with SSHTunnel(command):
    test()

rawDescription: Psycopg2 access PostgreSQL databese on remote host without manually opening ssh tunnel


code:
dicCSK = set()
for line in fin:
    line=line.strip('\n')
    try:
        c1, c2 = line.split("\t")
    except ValueError: print line
    if c1 not in dicCSK:
        dicCSK.add(c1)
        fout.writelines(c1+"\t"+c2+'\n')

rawDescription: How to store only the first element in python

description: Store only the first element in python


code:
from sys import exit

def menu():
    a = raw_input("choose: apple[a], grape[g], quit[q] ")
    if a == 'a':
        return 'apple'
    elif a == 'g':
        return 'grape'
    elif a == 'q':
        print 'Are you sure you want to quit?'
        b = raw_input ('Choose: yes[y], no[n] ')
        if b == 'y':
            exit()
        elif b == 'n':
            return menu()  # This calls the function again, so we're asked question "a" again

menu()

rawDescription: Go to line in python?

description: Go to line in python


code:
currentTime = datetime.datetime.utcnow()
s = '2013-01-29 02:11:11.151996+00:00'
storedTime = datetime.datetime.strptime(s[:-6], "%Y-%m-%d %H:%M:%S.%f")
if storedTime + datetime.timedelta(minutes=60) > currentTime:
    print "stuff..."

rawDescription: Python storing and comparing timestamps

description: Python storing and comparing timestamps


code:
import cv2

# to see all ORB parameters and their values
detector = cv2.FeatureDetector_create("ORB")
print "ORB parameters (dict):", detector.getParams()
for param in detector.getParams():
    ptype = detector.paramType(param)
    if ptype == 0:
        print param, "=", detector.getInt(param)
    elif ptype == 2:
        print param, "=", detector.getDouble(param)

# to set the nFeatures
print "nFeatures before:", detector.getInt("nFeatures")
detector.setInt("nFeatures", 1000)
print "nFeatures after:", detector.getInt("nFeatures")

rawDescription: Setting ORB parameters in OpenCv with Python


code:
lst = ["my name is Bob Jenkins", "Tommy Cooper I am", "I be Bazil", "I Tarzan"]
names = ["Bob", "Tarzan"]

new=[a for a in lst if all(x not in a for x in names) ]
print new

rawDescription: Convert For Loop to List Comprehension: Testing if elements in list 2 are partial match for elements in list 1


code:
from sklearn.grid_search import GridSearchCV
from sklearn.metrics.scorer import make_scorer
from sklearn.svm import SVR

import numpy as np

rng = np.random.RandomState(1)

def my_custom_loss_func(X_train_scaled, Y_train_scaled):
    error, M = 0, 0
    for i in range(0, len(Y_train_scaled)):
        z = (Y_train_scaled[i] - M)
        if X_train_scaled[i] > M and Y_train_scaled[i] > M and (X_train_scaled[i] - Y_train_scaled[i]) > 0:
            error_i = (abs(Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(z))
        if X_train_scaled[i] > M and Y_train_scaled[i] > M and (X_train_scaled[i] - Y_train_scaled[i]) < 0:
            error_i = -(abs((Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(z)))
        if X_train_scaled[i] > M and Y_train_scaled[i] < M:
            error_i = -(abs(Y_train_scaled[i] - X_train_scaled[i]))**(2*np.exp(-z))
    error += error_i
    return error

# Generate sample data
X = 5 * rng.rand(10000, 1)
y = np.sin(X).ravel()

# Add noise to targets
y[::5] += 3 * (0.5 - rng.rand(X.shape[0]/5))

train_size = 100

my_scorer = make_scorer(my_custom_loss_func, greater_is_better=True)

svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1),
                   scoring=my_scorer,
                   cv=5,
                   param_grid={"C": [1e0, 1e1, 1e2, 1e3],
                               "gamma": np.logspace(-2, 2, 5)})

svr.fit(X[:train_size], y[:train_size])

print svr.best_params_
print svr.score(X[train_size:], y[train_size:])

rawDescription: How to create/customize your own scorer function in scikit-learn?

description: Create/customize your own scorer function in scikit-learn


code:
# Read in a WAV and find the freq's
import pyaudio
import wave
import numpy as np

chunk = 2048

# open up a wave
wf = wave.open('test-tones/440hz.wav', 'rb')
swidth = wf.getsampwidth()
RATE = wf.getframerate()
# use a Blackman window
window = np.blackman(chunk)
# open stream
p = pyaudio.PyAudio()
stream = p.open(format =
                p.get_format_from_width(wf.getsampwidth()),
                channels = wf.getnchannels(),
                rate = RATE,
                output = True)

# read some data
data = wf.readframes(chunk)
# play stream and find the frequency of each chunk
while len(data) == chunk*swidth:
    # write data out to the audio stream
    stream.write(data)
    # unpack the data and times by the hamming window
    indata = np.array(wave.struct.unpack("%dh"%(len(data)/swidth),\
                                         data))*window
    # Take the fft and square each value
    fftData=abs(np.fft.rfft(indata))**2
    # find the maximum
    which = fftData[1:].argmax() + 1
    # use quadratic interpolation around the max
    if which != len(fftData)-1:
        y0,y1,y2 = np.log(fftData[which-1:which+2:])
        x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
        # find the frequency and output it
        thefreq = (which+x1)*RATE/chunk
        print "The freq is %f Hz." % (thefreq)
    else:
        thefreq = which*RATE/chunk
        print "The freq is %f Hz." % (thefreq)
    # read some more data
    data = wf.readframes(chunk)
if data:
    stream.write(data)
stream.close()
p.terminate()

rawDescription: Python frequency detection

description: Python frequency detection


code:
if a == b:
  print 'Same elements, same order'
elif sorted(a) != sorted(b):
  print 'Same lists, different order'
else:
  print 'Completely differeent'

rawDescription: How do you compare the order of two lists that contain the same elements?

description: Compare the order of two lists that contain the same elements


code:
import signal

def print_linenum(signum, frame):
    print "Currently at line", frame.f_lineno

signal.signal(signal.SIGINT, print_linenum)

rawDescription: How to check which line of a Python script is being executed?

description: Check which line of a Python script is being executed


code:
import urllib
from HTMLParser import HTMLParser
import re

adib = urllib.urlopen('http://www.bulldoghax.com/secret/spinner')

htmlsource = adib.read()

from bs4 import BeautifulSoup
soup = BeautifulSoup(htmlsource)
for each_div in soup.findAll('div',{'class':'number'}):
    print each_div.text

rawDescription: Reading a specific class in a webpage using python


code:
from time import clock
from random import randint
a=[True for _ in range(randint(1000000,10000000))]
spacing=randint(3,101)
t=clock()
try:
    a[::spacing]=[False]
except ValueError as e:
    a[::spacing]=[False]*int(e.message.rsplit(' ',1)[-1])

print spacing,clock()-t

# baseline

t=clock()
a[::spacing]=[False]*len(a[::spacing])
print 'Baseline:',spacing,clock()-t

rawDescription: Yielding until all needed values are yielded, is there way to make slice to become lazy


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

cur_key = None
cur_key_count = 0
for line in sys.stdin:
    line = line.strip()
    key, count = line.split('\t', 1)

    try:
        count = int(count)
    except ValueError:
        continue

    # if new key, reset count, note current key, and output lastk key's result
    if key != cur_key:
        if cur_key is not None:
            print '%s\t%s'% (cur_key, cur_key_count)
        cur_key = key
        cur_key_count = 0
    cur_key_count += count
# printing out final key if set
if cur_key:
    print '%s\t%s'% (cur_key, cur_key_count)

rawDescription: Efficient way to reduce MapReduce results?


code:
import itertools
import csv

fileLocation = 'newslot.csv'
with open(fileLocation,'rb') as f:
    r = csv.reader(f)
    lis=sorted([(i[0],i[1]) for i in r])
    for k,g in itertools.groupby(lis,key=lambda x:x[0]):
        g=list(g)
        print k,sum(int(i[1]) for i in g)/len(g)

rawDescription: How to get average from two columns in csv file

description: Get average from two columns in csv file


code:
import threading
import time

#Routine that processes whatever you want as background
def YourLedRoutine():
    while 1:
        print 'tick'
        time.sleep(1)

t1 = threading.Thread(target=YourLedRoutine)
#Background thread will finish with the main program
t1.setDaemon(True)
#Start YourLedRoutine() in a separate thread
t1.start()
#You main program imitated by sleep
time.sleep(5)

rawDescription: Backgroundworker in python

description: Backgroundworker in python


code:
data = [ [1, "data1"], [1, "data2"], [2, "moredata"], [2, "stuff"],
  [2, "things"], [2, "foo"], [3, "bar"], [4, "baz"] ]
from itertools import groupby
from operator import itemgetter
def process_data(key, keydata):
    print key, ':', keydata
for key,keydata in groupby(data, key=itemgetter(0)):
  process_data(key, [d[1] for d in keydata])

rawDescription: Grouping data within a for loop

description: Grouping data within a for loop


code:
import xlrd
workbook = xlrd.open_workbook(filename)
# This will get you the very first sheet in the workbook.
worksheet = workbook.sheet_by_name(workbook.sheet_names()[0])
for index in range(worksheet.nrows):
    try:
        row = worksheet.row(index)
        row_value = [col.value for col in row]
        # now row_value is a list contains all the column values
        print row_value[3:99]
    except:
        pass

rawDescription: Going down columns using xlrd

description: Going down columns using xlrd


code:
from Tkinter import *

master = Tk()

def callback():
    print "click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

rawDescription: In Python, how do I assign a Tkinter button to a piece of code?


rawDescription: Get the last item in hash from redis using python


code:
previous = lst[-1]           # j = len(lst) - 1
for current in lst:          # i = 0; i < len(lst); i++
    print current, previous  # lst[i], lst[j]
    # do stuff
    previous = current       # j = i before incrementing

rawDescription: Implementing a Pythonc "i, j" style for loop in Python 3?


code:
import wxversion
wxversion.select("2.8") # version 3.0 works, too.
import wx
import sys
import threading
import time

MAX_COUNT = 200

class WorkerThread(threading.Thread):
    def __init__(self, target, countNum):
        threading.Thread.__init__(self, target = target)
        self.setDaemon(True)
        self.cnt = countNum
        self.target = target
        self.pb = self.target.pb

    def run(self):
        for i in xrange(self.cnt):
            print i+1
            wx.MilliSleep(50)
            wx.CallAfter(self.pb.SetValue, i+1)

        wx.CallAfter(self.target.MakeModal, False)
        wx.CallAfter(self.target.Close)

class ProgressBarFrame(wx.Frame):
    def __init__(self, parent, title, range = 100) :
        wx.Frame.__init__(self, parent = parent, title = title)
        self.range = range
        self.createProgressbar()
        self.SetMinSize((400, 10))
        self.Centre()
        self.Show()
        self.t0 = time.time()
        self.elapsed_time_timer.Start(1000)

    def createProgressbar(self):
        self.pb       = wx.Gauge(self)
        self.pb.SetRange(range = self.range)

        self.elapsed_time_st  = wx.StaticText(self, label = 'Elapsed Time:')
        self.elapsed_time_val = wx.StaticText(self, label = '00:00:00')

        vbox_main = wx.BoxSizer(wx.VERTICAL)
        hbox_time = wx.BoxSizer(wx.HORIZONTAL)
        hbox_time.Add(self.elapsed_time_st,  0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
        hbox_time.Add(self.elapsed_time_val, 0, wx.ALIGN_LEFT | wx.EXPAND | wx.ALL, 5)
        vbox_main.Add(self.pb,   0, wx.EXPAND | wx.ALL, 5)
        vbox_main.Add(hbox_time, 0, wx.EXPAND | wx.ALL, 5)

        self.SetSizerAndFit(vbox_main)

        self.elapsed_time_timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTickTimer, self.elapsed_time_timer)

    def onTickTimer(self, event):
        fmt='%H:%M:%S'
        self.elapsed_time_val.SetLabel(time.strftime(fmt, time.gmtime(time.time()-self.t0)))

class Frame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, title=title, pos=(150,150), size=(350,200))

        panel = wx.Panel(self)
        box = wx.BoxSizer(wx.VERTICAL)

        m_btn = wx.Button(panel, wx.ID_ANY, "Run Stuff")
        self.Bind(wx.EVT_BUTTON, self.OnRunButton, m_btn)
        box.Add(m_btn, 0, wx.ALL, 10)

        panel.SetSizer(box)

    def OnRunButton(self, event):
        self.progressbar = ProgressBarFrame(self, 'Working Processing', MAX_COUNT)
        self.progressbar.MakeModal(True)
        worker = WorkerThread(self.progressbar, MAX_COUNT)
        worker.start()

app = wx.App(redirect=False)
top = Frame("ProgressDialog Test")
top.Show()
app.MainLoop()

rawDescription: wx.ProgressDialog causing seg fault and/or GTK_IS_WINDOW failure when being destroyed

description: Wx.ProgressDialog causing seg fault and/or GTK_IS_WINDOW failure when being destroyed


code:
with open('4026.txt','r') as out1, open('4089.txt', 'r') as out2,  open('4116.txt', 'r') as out3, open('4121.txt', 'r') as out4, open('4542.txt', 'r') as out5, open('4553.txt', 'r') as out6:

    # Build two lists
    x = []
    y = []

    for data1 in out1.readlines():
        col1 = data1.strip().split()
        x.append(col1[9])

    for data2 in out2.readlines():
        col2 = data2.strip().split()
        y.append(col2[9])

    for i in range(0,len(x)):

        # Make sure the denominator is not zero
        if y[i] != 0:
           print (1.0 * x[i])/y[i]
        else:
           print "Not possible"

rawDescription: Operations with columns from different files


code:
# looping over the items in a list
value = 1
for item in aList:
    if item == value:
        print "value is 1!"

# basic function with arguments and a return value
def aFunc(start):
    end = start + 1
    return end
print aFunc(1)
# result: 2

# some useful comparison operators
print 1 > 2  # False
print 2 > 1  # True

rawDescription: Python Min-Max Function - List as argument to return min and max element


code:
import libtorrent as lt
import time
import sys

ses = lt.session()
ses.listen_on(6881, 6891)

info = lt.torrent_info(sys.argv[1])
h = ses.add_torrent({'ti': info, 'save_path': './'})
print 'starting', h.name()

while (not h.is_seed()):
   s = h.status()

   state_str = ['queued', 'checking', 'downloading metadata', \
      'downloading', 'finished', 'seeding', 'allocating', 'checking fastresume']
   print '\r%.2f%% complete (down: %.1f kb/s up: %.1f kB/s peers: %d) %s' % \
      (s.progress * 100, s.download_rate / 1000, s.upload_rate / 1000, \
      s.num_peers, state_str[s.state]),
   sys.stdout.flush()

   time.sleep(1)

print h.name(), 'complete'

rawDescription: How to write a simple Bittorrent application?

description: Write a simple Bittorrent application


code:
""" extrapolate y,m,d data with scipy UnivariateSpline """
import numpy as np
from scipy.interpolate import UnivariateSpline
    # pydoc scipy.interpolate.UnivariateSpline -- fitpack, unclear
from datetime import date
from pylab import *  # ipython -pylab

__version__ = "denis 23oct"

def daynumber( y,m,d ):
    """ 2005,1,1 -> 0  2006,1,1 -> 365 ... """
    return date( y,m,d ).toordinal() - date( 2005,1,1 ).toordinal()

days, values = np.array([
    (daynumber(2005,1,1), 1.2 ),
    (daynumber(2005,4,1), 1.8 ),
    (daynumber(2005,9,1), 5.3 ),
    (daynumber(2005,10,1), 5.3 )
    ]).T
dayswanted = np.array([ daynumber( year, month, 1 )
        for year in range( 2005, 2006+1 )
        for month in range( 1, 12+1 )])

np.set_printoptions( 1 )  # .1f
print "days:", days
print "values:", values
print "dayswanted:", dayswanted

title( "extrapolation with scipy.interpolate.UnivariateSpline" )
plot( days, values, "o" )
for k in (1,2,3):  # line parabola cubicspline
    extrapolator = UnivariateSpline( days, values, k=k )
    y = extrapolator( dayswanted )
    label = "k=%d" % k
    print label, y
    plot( dayswanted, y, label=label  )  # pylab

legend( loc="lower left" )
grid(True)
savefig( "extrapolate-UnivariateSpline.png", dpi=50 )
show()

rawDescription: Is there easy way in python to extrapolate data points to the future?


code:
from sklearn.metrics import classification_report
print classification_report(y_true, y_predicted)

rawDescription: F1 smaller than both precision and recall in Scikit-learn


code:
from django.db import transaction
transaction.get_connection().in_atomic_block
with transaction.atomic():
    print transaction.get_connection().in_atomic_block

transaction.get_connection().in_atomic_block

rawDescription: Django: atomic(): Force transaction, raise AssertionError if already inside a transaction


code:
for line in f:
    for s in keywords:
         if s in line:
            print 'Key word found!', f.name, s

rawDescription: Obtaining keyword for matching string in Python


code:
from xml.etree import ElementTree as ET

# `ET.fromstring()` expects a string containing XML to parse.
# tree = ET.fromstring(solrdata)
# Use `ET.parse()` for a filename or open file object, such as returned by urllib2:
ET.parse(urllib2.urlopen(url))

for doc in tree.findall('.//doc'):
    for elem in doc:
        print elem.attrib['name'], elem.text

rawDescription: Parse Solr output in python

description: Parse Solr output in python


code:
f = "Makimak-cg_mk_Mokarmi"
pattern = "([A-Za-z][A-Za-z0-9]+)((?:[-_][a-z]{2})+)"
mO = re.match(pattern, f)
print mO.groups() + tuple(re.split('[-_]', mO.group(2))[1:])

rawDescription: Python Regular Expression group repetition within a repetition


code:
def my_range(end):
    start = 0
    while start < end:
        yield start
        start +=1

def conforms(k,s):
    k = k + 1
    if s.find("0" * k) == -1 and s.find("1" * k) == -1:
        return True
    else:
        return False

def brute(n,k,s):
    min_val = n + 1
    min_str = ""
    desired = long(s,2)
    for i in my_range(2 ** n):
        xor = desired ^ i # gives number of bit changes
        i_rep = bin(i)[2:].zfill(n) # pad the binary representation with 0s - for conforms()
        one_count = bin(xor).count('1')
        if one_count < min_val and conforms(k, i_rep):
            min_val = bin(xor).count('1')
            min_str = i_rep
    return (min_val,min_str)

T = 1
for i in range(T):
    words = [100, 1]
    start = '00000001'
    sol = brute(words[0], words[1], start)
    print sol[0]
    print sol[1]

rawDescription: Python Code Fails

description: Python Code Fails


code:
for path, subdirs, files in os.walk(path):
    for name in files:
        if os.path.splitext(name)[1] in filefilter:
            f = str(os.path.join(path, name))
            with open(f) as fp:
                if s in fp.read():
                    print "%s has the string" % f
                else:
                    print "%s doesn't have the string" % f

rawDescription: python processs complete list files matched

description: Python processs complete list files matched


code:
from PyQt4 import QtGui
from PyQt4.QtCore import *

class WarningMessage(QtGui.QMessageBox):
  '''
  Creates a message for the case when something is wrong
  '''
  def __init__(self, message, choice=False):
    super(WarningMessage, self).__init__()
    self.choice = choice
    if not isinstance(message, basestring):
      self.message = str(message)
    else:
      self.message = message
    self.user_choice = None
    self.initUI()

  def initUI(self):
    if not self.choice:
      self.warning(self, u'Warning!',
          self.message, QtGui.QMessageBox.Ok)
      return None
    else:
      reply = QtGui.QMessageBox.question(self, u'Warning!',
            self.message, QtGui.QMessageBox.Yes |
            QtGui.QMessageBox.No)

      if reply == QtGui.QMessageBox.Yes:
        self.user_choice = True
      else:
        self.user_choice = False

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    choice = WarningMessage(u'Continue?', choice = True).user_choice
    print choice
    sys.exit(app.exec_())

rawDescription: How to create custom PyQt accept/reject dialogue?

description: Create custom PyQt accept/reject dialogue


code:
from scrapy.selector import HtmlXPathSelector
from scrapy.spider import BaseSpider
import html2text

class WikiSpider(BaseSpider):
    name = "wiki_spider"
    allowed_domains = ["www.wikipedia.org"]
    start_urls = ["http://en.wikipedia.org/wiki/Python_(programming_language)"]

    def parse(self, response):
        hxs = HtmlXPathSelector(response)
        sample = hxs.select("//div[@id='mw-content-text']/p[1]").extract()[0]

        converter = html2text.HTML2Text()
        converter.ignore_links = True
        print converter.handle(sample)

rawDescription: Is it possible that Scrapy to get plain text from raw html data directly instead of using xPath selectors?


code:
import Skype4Py
import time
from pytify import Spotify

# Create Skype object
skype = Skype4Py.Skype()
skype.Attach()

# Create Spotify object
spotify = Spotify()
spotifyPlaying = spotify.isPlaying()

# Create handler for when Skype call status changes
def on_call_status(call, status):
  if status == Skype4Py.clsInProgress:
    # Save current spotify state
    global spotifyPlaying
    spotifyPlaying = spotify.isPlaying()

    if spotify.isPlaying():
      print "Call started, pausing spotify"
      # Call started, pause Spotify
      spotify.stop()

  elif status == Skype4Py.clsFinished:
    # Call finished, resume Spotify if it was playing
    if spotifyPlaying and not spotify.isPlaying():
      print "Call finished, resuming spotify"
      spotify.playpause()

skype.OnCallStatus = on_call_status

while True:
  time.sleep(10)

rawDescription: How to programatically pause spotify when a call comes in on skype

description: Programatically pause spotify when a call comes in on skype


code:
import wx

########################################################################
class LoginPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        userSizer = wx.BoxSizer(wx.HORIZONTAL)
        pwdSizer = wx.BoxSizer(wx.HORIZONTAL)

        userLbl = wx.StaticText(self, label="Username:")
        userSizer.Add(userLbl, 0, wx.CENTER|wx.ALL, 5)
        self.userTxt = wx.TextCtrl(self)
        userSizer.Add(self.userTxt, 1, wx.EXPAND|wx.ALL, 5)

        pwdLbl = wx.StaticText(self, label="Password:")
        pwdSizer.Add(pwdLbl, 0, wx.CENTER|wx.ALL, 5)
        self.pwdTxt = wx.TextCtrl(self, style=wx.TE_PASSWORD)
        pwdSizer.Add(self.pwdTxt, 1, wx.EXPAND|wx.ALL, 5)

        loginBtn = wx.Button(self, label="Login")
        loginBtn.Bind(wx.EVT_BUTTON, self.onLogin)

        self.mainSizer.Add(userSizer, 0, wx.EXPAND)
        self.mainSizer.Add(pwdSizer, 0, wx.EXPAND)
        self.mainSizer.Add(loginBtn, 0, wx.CENTER|wx.ALL, 5)
        self.SetSizer(self.mainSizer)

    #----------------------------------------------------------------------
    def onLogin(self, event):
        """
        Login or show msg box if password wrong
        """
        username = self.userTxt.GetValue()
        pwd = self.pwdTxt.GetValue()

        if pwd == "password":
            print "logging in"
        else:
            msg = "Wrong password or username!"
            wx.MessageBox(msg, "Error", wx.OK|wx.ICON_INFORMATION)

########################################################################
class LoginFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Login")
        panel = LoginPanel(self)
        self.Show()

if __name__ == "__main__":
    app = wx.App(False)
    frame = LoginFrame()
    app.MainLoop()

rawDescription: Dont reset fields when message box show up


code:
print min(timeit.Timer("""
l =list()
for i in range(5):
    l.append((A(),A(),A()))
    l[-1][0].s='foo'
    l[-1][1].s='foo2'
for _ in sorted(l): pass
print A.eq_calls
print A.cmp_calls""", setup=setup).repeat(1, 1))

rawDescription: Why in comparing python tuples of objects is __eq__ and then __cmp__ called?


code:
for i in range(len(mainlist)):
    for j in range(i,len(mainlist)):
        if mainlist[j] == mainlist[i]:
            print 'Skipping'
        else:
            print mainlist[i] + ' ' + mainlist[j]

rawDescription: Generate every permutation of list elements pairs without repeating or inverted pairs


code:
ps = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE).communicate()[0]
processes = ps.split('\n')
# this specifies the number of splits, so the splitted lines
# will have (nfields+1) elements
nfields = len(processes[0].split()) - 1
for row in processes[1:]:
    print row.split(None, nfields)

rawDescription: Splitting out the output of ps using Python


code:
nt=input("Ingrese el numero de terminos:", )
fib=[0,1]
for i in range(nt-2):
    fib.append(sum(fib[-2:]))
print sum(i for i in fib if i%2 == 0)

rawDescription: sum(list) function Python

description: Sum(list) function Python


code:
filelist = glob.glob(os.path.join(path, 'FV/*.txt'))
for infile in sorted(filelist):
  #do some fancy stuff
  print str(infile)

rawDescription: Python - looping over files - order

description: Python - looping over files - order


code:
headers = wks.row_values(1)
row = 2 # start from the second row because the first row are headers
for k in range(len(lstdic)):
        values=[]
        cell_list=wks.range('B%s:AB%s' % (row,row)) # make sure your row range equals the length of the values list
        for key in headers:
            values.append(lstdic[k][key])
        for i in range(len(cell_list)):
            cell_list[i].value = values[i]
        wks.update_cells(cell_list)
        print "Updating row " + str(k+2) + '/' + str(len(lstdic) + 1)
        row += 1

rawDescription: Populating google spreadsheet by row, not by cell


rawDescription: Remove '\x' from bytes

description: Remove '\x' from bytes


rawDescription: Pad integer after hyphen

description: Pad integer after hyphen


code:
n = [3, 5, 7]

def double_list(x):
    for i in range(0, len(x)):
        x[i]*=2
    return x

print double_list(n)

rawDescription: Returning more than one number in a python list


code:
import itertools
listname = [[1,2,3],[4,5,6,7,8],[9,10,11,12]]
for x in itertools.izip_longest(*listname, fillvalue="-"):
    print '\t'.join([str(e) for e in x])

rawDescription: How to print variable length lists as columns in python?

description: Print variable length lists as columns in python


code:
script_username="test"
script_password="1"
login_username=raw_input('Please Enter the script username:')
login_password=getpass.getpass('Please Enter the script password:')

if login_password == script_password and login_username == script_username:
    print "         Welcome to the Script           "
else:
    print "     your Username or Password is false      "
    exit()

name=raw_input("Enter your name: ")
family=raw_input("Enter your family: ")
sex=raw_input("Enter your Sex: ")
print "hello " + sex + name + family
raw_input("press<enter>")

rawDescription: Python if statements

description: Python if statements


code:
def cb(self):
    print "variable is", self.var.get()

rawDescription: Tkinter Checkbutton and event callback function


code:
f1 = open ('car_names.txt')
f2 = open ('car_descriptions.txt')
for car_name in f1.readlines ():
        for i in range (6):   # echo the first 6 lines
                print f2.readline ()
        assert f2.readline() == '@CAR_NAME'  # skip the 7th, but assert that it is @CAR_NAME
        print car_name    # print the real car name
        for i in range (33):  # print the remaining 33 of the original 40
               print f2.readline ()

rawDescription: Process two files at the same time in Python


code:
selection = raw_input("Now is your chance to pick which brand you want. ")
print "You selected ", selection
if selection in car_brands:
    print "It is in the list"
else:
    print "Its not in the list"

rawDescription: Check selection and compare to list for python


code:
import wx

class ShapedButton(wx.PyControl):
    def __init__(self, parent, normal, pressed=None, disabled=None):
        super(ShapedButton, self).__init__(parent, -1, style=wx.BORDER_NONE)
        self.normal = normal
        self.pressed = pressed
        self.disabled = disabled
        self.region = wx.RegionFromBitmapColour(normal, wx.Color(0, 0, 0, 0))
        self._clicked = False
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.Bind(wx.EVT_SIZE, self.on_size)
        self.Bind(wx.EVT_PAINT, self.on_paint)
        self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
        self.Bind(wx.EVT_LEFT_DCLICK, self.on_left_dclick)
        self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
        self.Bind(wx.EVT_MOTION, self.on_motion)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window)
    def DoGetBestSize(self):
        return self.normal.GetSize()
    def Enable(self, *args, **kwargs):
        super(ShapedButton, self).Enable(*args, **kwargs)
        self.Refresh()
    def Disable(self, *args, **kwargs):
        super(ShapedButton, self).Disable(*args, **kwargs)
        self.Refresh()
    def post_event(self):
        event = wx.CommandEvent()
        event.SetEventObject(self)
        event.SetEventType(wx.EVT_BUTTON.typeId)
        wx.PostEvent(self, event)
    def on_size(self, event):
        event.Skip()
        self.Refresh()
    def on_paint(self, event):
        dc = wx.AutoBufferedPaintDC(self)
        dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
        dc.Clear()
        bitmap = self.normal
        if self.clicked:
            bitmap = self.pressed or bitmap
        if not self.IsEnabled():
            bitmap = self.disabled or bitmap
        dc.DrawBitmap(bitmap, 0, 0)
    def set_clicked(self, clicked):
        if clicked != self._clicked:
            self._clicked = clicked
            self.Refresh()
    def get_clicked(self):
        return self._clicked
    clicked = property(get_clicked, set_clicked)
    def on_left_down(self, event):
        x, y = event.GetPosition()
        if self.region.Contains(x, y):
            self.clicked = True
    def on_left_dclick(self, event):
        self.on_left_down(event)
    def on_left_up(self, event):
        if self.clicked:
            x, y = event.GetPosition()
            if self.region.Contains(x, y):
                self.post_event()
        self.clicked = False
    def on_motion(self, event):
        if self.clicked:
            x, y = event.GetPosition()
            if not self.region.Contains(x, y):
                self.clicked = False
    def on_leave_window(self, event):
        self.clicked = False

def main():
    def on_button(event):
        print 'Button was clicked.'
    app = wx.PySimpleApp()
    frame = wx.Frame(None, -1, 'Shaped Button Demo')
    panel = wx.Panel(frame, -1)
    button = ShapedButton(panel,
        wx.Bitmap('button-normal.png'),
        wx.Bitmap('button-pressed.png'),
        wx.Bitmap('button-disabled.png'))
    button.Bind(wx.EVT_BUTTON, on_button)
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.AddStretchSpacer(1)
    sizer.Add(button, 0, wx.ALIGN_CENTER)
    sizer.AddStretchSpacer(1)
    panel.SetSizer(sizer)
    frame.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

rawDescription: WxPython changing the shape of bitmap button


code:
import sys
last_print = None
# this class will replace sys.stdout
class PrintCatcher:
    # catch and save writes (prints)
    def write(self, data):
        global last_print
        if data != "\n": last_print = data

        # write the data to the original stdout object
        sys.__stdout__.write(data)
        sys.__stdout__.flush()
    # forward attribute gets to the original stdout object
    def __getattr__(self, attr):
        return getattr(sys.__stdout__, attr)

print "Hello"
last_print
sys.stdout = PrintCatcher()
print "Goodbye"
last_print

rawDescription: Is there a way to fetch past (or at least the last) "print" calls?


code:
from bs4 import BeautifulSoup

html = "http://www.kumby.com/avatar-the-last-airbender-book-3-chapter-5/"
soup = BeautifulSoup(html)
l = soup.findall("embed","object","param","video")
for i in l:
  print i.string

rawDescription: Scan a webpage and get the video embed url only


code:
import matplotlib.pyplot as p
from scipy import eye
p.imshow(eye(3))
p.show()
print 'a'
p.imshow(eye(6))
p.show()
print 'b'
p.imshow(eye(9))
p.show()
print 'c'

rawDescription: (python) matplotlib pyplot show() .. blocking or not?


code:
import threading
import telnetlib
import datetime
import sys
# Global Variable Declarations
TIMEOUT = 30
USER = "Noel"
PROMPT = "Noel"

class listener(threading.Thread):
    def __init__(self, filename, ip):
        # Have to make a call to the super classes' __init__ method
        super(listener, self).__init__()
        self.f = open(filename,"a")
        try:
            self.tn = telnetlib.Telnet(ip, 23, TIMEOUT)
        except:
            print "Bad Connection"
            sys.exit(0)

    def run(self):
        # login
        e = self.tn.read_until("Login: ")
        self.tn.write(USER+"\n")
        # and password
        e = self.tn.read_until("Password: ")
        self.tn.write(PASSWORD+"\n")
        while True:
            e = self.tn.read_until(PROMPT, TIMEOUT)
            if e is not "":
                self.f.write(datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S"))
                self.f.write(e.strip())
                self.f.flush()

            # avoid session timeout
            self.tn.write("\n")

if __name__ == "__main__":
    # Things to listen to is a dictionary of hosts and files to output
    # to, to add more things to listen to just add an extra entry into
    # the things_to_listen_to in the format: host : outputfile
    things_to_listen_to = {"localhost" :"localhost_output.txt"}
    # Thread holder is going to hold all the threads we are going to start
    thread_holder = []
    for host, file in things_to_listen_to.iteritems():
        thread_holder.append(listener(file, host))
    for thread in thread_holder:
        thread.run()

rawDescription: Python multiple telnet sessions

description: Python multiple telnet sessions


code:
import os
import sys

# Import Custom libraries
from BeautifulSoup import BeautifulStoneSoup

xml_str = \
'''
<?xml version="1.0" encoding="UTF-8"?>
    <first-tag>
      <second-tag>
        <events-data>
           <event-date>
                <date>20040913</date>
           </event-date>
        </events-data>

      <events-data>
         <event-date>
           <date>20040913</date>
         </event-date>
      </events-data>
     </second-tag>
   </first-tag>
'''
soup = BeautifulStoneSoup(xml_str)

event_data_location = lambda x: x.name == "events-data"

events = soup.findAll(event_data_location)
if(events):
    # The last event-data
    print events[-1].text

rawDescription: BeautifulSoup XML Python extracting attributes from specific tags


code:
badwords = ["love", "peace", "hello"]
message = "hi I love you"
for badword in badwords:
  if (badword in message.lower()):
   print "oh thats bad /ban"

rawDescription: IRC Bot anti swear

description: IRC Bot anti swear


code:
import sys

for arg in sys.argv:
    print arg

rawDescription: Passing parameterized build parameter to Jython plugin in Jenkins


code:
import json
import types

_gen = (value for value in range(5, 10))
property = {'mcu':{'led0':{'duty': _gen}}}

def generator_handler(obj):
    if isinstance(obj, types.GeneratorType):
      return obj.next()
    else:
      return None

print json.dumps(property, default=generator_handler)
print json.dumps(property, default=generator_handler)
print json.dumps(property, default=generator_handler)

rawDescription: Evaluating dictionary value during json serialization


code:
import getpass
import os
import sys

userName = getpass.getuser() #Get the username
path = "C:\\Users\\"+userName+"\\AppData\\Roaming\\Skype\\"

if (os.path.isdir(path)) == True: #Check if Skype folder exists
    subdirectories = os.listdir(path)
    for i in subdirectories:
        if os.path.isdir(path+i) == True:
            if os.path.exists(path+i+"\\main.db"):
                print ("Skype Id is %s") %i

else:
    print 'Skype folder does not exists'

rawDescription: python to guess specific path?

description: Python to guess specific path


code:
collapse_space = lambda s: str.join(" ", s.split())
logical_lines = []
for line in open("text"):
  if line[0].isspace():
    logical_lines[-1] += line #-- append the continuation to the last logical line
  else:
    logical_lines.append(line) #-- start a new logical line

l = map(collapse_space, logical_lines)
print str.join("\n", l)

rawDescription: How can I clean up text files in python?

description: Clean up text files in python


code:
from gi.repository import Gtk, GLib

class MySpawned(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)

        vb = Gtk.VBox(False, 5)
        self.tw = Gtk.TextView()
        bt = Gtk.Button('Run')
        bt.connect('clicked', self.process)

        vb.pack_start(self.tw, True, True, 0)
        vb.pack_start(bt, False, False, 0)

        self.add(vb)
        self.set_size_request(200, 300)
        self.connect('delete-event', Gtk.main_quit)
        self.show_all()

    def run(self):
        Gtk.main()

    def process(self, widget, data=None):
        params = ['python', '-h']

        def write_to_textview(io, condition):
            print condition
            if condition is GLib.IO_IN:
               line = io.readline()
               self.tw.props.buffer.insert_at_cursor(line)
               return True
            elif condition is GLib.IO_HUP|GLib.IO_IN:
                GLib.source_remove(self.source_id)
                return False

        self.source_id = None

        pid, stdin, stdout, stderr = GLib.spawn_async(params,
                                        flags=GLib.SpawnFlags.SEARCH_PATH,
                                        standard_output=True)

        io = GLib.IOChannel(stdout)

        self.source_id = io.add_watch(GLib.IO_IN|GLib.IO_HUP,
                                 write_to_textview,
                                 priority=GLib.PRIORITY_HIGH)
if __name__ == '__main__':
    s = MySpawned()
    s.run()

rawDescription: pygtk: asynchronous output implemented with io_add_watch blocks when printing large output dataset

description: Pygtk: asynchronous output implemented with io_add_watch blocks when printing large output dataset


code:
from itertools import chain
def my_range(*args):
    args = map(int,args)
    if len(args) == 1:
        return args
    return range(args[0], args[1]+1)

def func(strs):
    return list(chain.from_iterable(my_range(*x.split('-')) for x in strs.split(', ')))
strs = "1, 3-4, 7-9"
print func(strs)
#[1, 3, 4, 7, 8, 9]

rawDescription: Pyqt range input widget and python ranges


code:
# File Name RandonProteinSequences.py
# standard library
import os
import random

# biopython
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
from Bio.SeqRecord import SeqRecord
import Bio.writers.SeqRecord.fasta
from Bio import SeqIO
from sys import *

residueList1 = ["C","D","E","F","G","H","I"]
residueList2 = ["A","K","L","M","N","S"]
residueList3 = ["P","Q","R","T","V","W","Y"]
residueList4 = ["C","A","G","U"]
def getProteinSeqRecord(residue, seqcount):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    seqRec = SeqRecord(sequence, id = 'randSeq' + str(seqcount), description= 'A random sequence using Amino acid residues.')
    return seqRec

def getProteinSequence(residue):
    strSeq = ""
    for i in range(0,100,1):
        index = random.randint(0, len(residue)-1)
        strSeq += residue[index]

    sequence = Seq(strSeq, IUPAC.IUPACProtein)
    return sequence

def randomProteinSeqRecord(index):
    if(index%2)==0:
        return getProteinSeqRecord(residueList1, index)
    elif(index%3)==0:
        return getProteinSeqRecord(residueList2, index)
    else:
        return getProteinSeqRecord(residueList3, index)

#information
print '--- This is python based program to generate random sequences ---'
print '--- Provide number of random sequences to generate. Default 10 ---'
print '--- Inorder to save to a file provide file path or filename ---'
print '--- If none or invalid filepath is provided then results will be displayed to console ---'
print '--- The file will be created in fasta format ---'
print

filepathProvided = False
#raw_input received the user input as string
try:
    filepath = raw_input('Enter filepath to save sequences ... ')
    filepath = filepath + '.fasta'
    handle = open(filepath, "w")
    handle.close()

    filepathProvided = True
except IOError:
    print 'Invalid or No File provided will print results to console'
print
ranSeqCount = 10
try:
    ranSeqCount = int(raw_input('Enter number of random sequences to generate ... '))
except ValueError:
    ranSeqCount = 10
pass

if(filepathProvided):
    handle = open(filepath, "w")

if(filepathProvided):
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(handle)
else:
    fasta_writer = Bio.writers.SeqRecord.fasta.WriteFasta(stdout)
print 'Sequence Count : '
print ranSeqCount

for i in range(0,ranSeqCount,1):
    fasta_writer.write(randomProteinSeqRecord(i+1))
if(filepathProvided):
    handle.close()
print 'File created at : ' + filepath

print
raw_input('Press any key to exit ...')
print

rawDescription: return outside function

description: Return outside function


code:
import cv2
import numpy as np

img = cv2.imread('5585T.jpg')
print type(img)
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
new=[[[0,0,255%j] for j in i] for i in img_gray]
dt = np.dtype('f8')
new=np.array(new,dtype=dt)

cv2.imwrite('img.jpg',new)

rawDescription: Change color of a pixel with OpenCV

description: Change color of a pixel with OpenCV


code:
# initially we declare a variable that will hold the current sum
# and at the end of the for it will hold
# the sum of the numbers you have mentioned.
b = 0
for i in range(1000):
    # Then whether i is evenly divided by 3 or 5
    # we add this number to the current sum
    if i % 3 == 0 or i % 5 == 0:
        b += i

# last we print the sum
print b

rawDescription: Project Euler – Exercise 1

description: Project Euler – Exercise 1


code:
KEYWORDS = ["CNAME", "A"]

for line in data:
    tokens = line.split()
    if len(tokens) > 2:
        record = ""
        for keyword in KEYWORDS:
            if keyword in tokens:
                record = keyword + " " + tokens[tokens.index(keyword) + 1]

        if "TXT" in line:
            txt_data = line[line.index("TXT") + 3:]
            record += "TXT: " + txt_data

        print record

rawDescription: work through a file in python

description: Work through a file in python


code:
lista=list()
for index in range(len(lista)):
 lista.insert(p)
 index=index+1
print lista

rawDescription: for loop in Python creating a list

description: For loop in Python creating a list


code:
class A(dict):
    def __init__(self):
        self['a'] = 'success'

    def __getitem__(self, name):
        print 'getitem!'
        return dict.__getitem__(self, name)

class B(object):
    def __init__(self):
        self._a = A()
        B.__getitem__ = self._a.__getitem__

b = B()
c = b['a']

rawDescription: Redirecting function definitions in python


code:
for x in sorted(frequency, key=lambda y: frequency[y]/float(n), reverse=True):
    print x,frequency[x],frequency[x]/float(n)

rawDescription: UPDATE:Sorting a list from a for output from highest to lowest in python


code:
import numpy as np
from scipy.integrate import dblquad
import itertools

f = lambda xa,xb: np.exp(-(xa-xb)**2)
intg = lambda (x1,x2): dblquad(f,-np.inf,x1,
                               lambda x:-np.inf,
                               lambda x:x2)

X1 = np.arange(1,6)
X2 = np.arange(5,10)
for limit in itertools.product(X1,X2):
    print limit, intg(limit)

rawDescription: Performing a double integral over a matrix of limits


code:
import urllib2
import urllib
import cookielib
import re

def write_f_to_f(fname,f):
    f1 = open(fname,"w")
    f1.write(f.read())
    f1.close()

def gen_req(url,referer=None):
    req = urllib2.Request(url)
    return req

myjar = cookielib.FileCookieJar("cookies.txt");
cookieHandler = urllib2.HTTPCookieProcessor(myjar)

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
tlurl="http://127.0.0.1:8081/webgoat/attack"

password_mgr.add_password(None,tlurl,user="guest",passwd="guest")
find = "Blind Numeric."

authhandler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(authhandler,cookieHandler)

url = "http://127.0.0.1:8081/webgoat/attack"
req = gen_req(url,url)
f = opener.open(req)# Setup session and login.

params = urllib.urlencode({'start':'Start WebGoat'})
url = "http://127.0.0.1:8081/webgoat/attack"
req = gen_req(url,url)
f = opener.open(req,params) # Submit the "Start" form
dat = f.read() # Get the menu html from the firstpage.
f.close()

m = re.search("attack\?Screen=(\d+).*%s"%find,dat) # Try to find screen id for what i want
if not m is None:
    scr= m.group(1)
    qs = urllib.urlencode( {'Screen':scr, 'menu':1200 } )
    ourl = url
    url = "http://127.0.0.1:8081/webgoat/attack?%s"%qs
    req = gen_req(url,ourl)
    f = opener.open(req)
    write_f_to_f("f4.html",f)
    f.close()
    print "Found the page and saved it to f4.html"
else:
    print "Didnt find screen id for %s"%find

rawDescription: Script access to WebGoat urls?

description: Script access to WebGoat urls


code:
import itertools
import collections
for item in itertools.permutations(test.items()):
    print collections.OrderedDict(item)

rawDescription: How to use itertools to generate dictionaries of all permutations of a given dictionary

description: Use itertools to generate dictionaries of all permutations of a given dictionary


code:
import random

listb = []

def random_numbers():
    n = 0
    lista = (345, 348, 333, 347, 346, 340, 342, 349, 330, 331, 334, 335, 336, 337, 338, 339, 360,                         366, 368, 320, 324, 327, 328, 329, 380, 388, 389, 391, 392, 393)
    while n < 150:
        x = random.randint(234325,876432)
        x = str(x)
        z =  str(random.choice(lista)) + x
        n += 1
        listb.append(z)

iterations = 0
while len(listb) == len(set(listb)):
    listb = []
    iterations += 1
    random_numbers()
    print len(listb), len(set(listb))

print iterations

rawDescription: Generating random numbers with prefix

description: Generating random numbers with prefix


code:
import os
import sys
import shutil

source = raw_input("Enter source file path: ")
dest = raw_input("Enter destination path: ")

if not os.path.isfile(source):
    print "Source file %s does not exist." % source
    sys.exit(3)

try:
    shutil.copy(source, dest)
except IOError, e:
    print "Could not copy file %s to destination %s" % (source, dest)
    print e
    sys.exit(3)

rawDescription: How to copy a file in Python?

description: Copy a file in Python


code:
import readline

defaultText = 'I am the default value'
readline.set_startup_hook(lambda: readline.insert_text(defaultText))
res = raw_input('Edit this:')
print res

rawDescription: Python: Get user input by letting user edit default value


code:
done = None
command = 'my loading command'
proc = subprocess.Popen(command, stdout=subprocess.PIPE)

while proc.poll() is None:
    line = proc.stdout.readline(77)
    print line.strip()
    if "Done" in line:
        done = True

rawDescription: Reading and printing progress bar that is being updated on same line while it is still updating


code:
result = []
for i in range(3):
   for j in range(3):
       paths = [i,j]
       result.append(paths)
       print paths

print result

rawDescription: Nested For Loops To Fill An Array

description: Nested For Loops To Fill An Array


code:
import Tkinter as tk

def TestMath():
    global result # to return calculation

    result = int(entry.get())
    result += 4

    print result

result = 0

root = tk.Tk()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="Calculate", command=TestMath)
button.pack()

root.mainloop()

rawDescription: Tkinter: Link an Entry Widget to a Button to a Function


code:
from Tkinter import *
import datetime
import openpyxl
import os

wb = openpyxl.load_workbook("TimeSheet.xlsx", use_iterators=True)
ws1 = wb.get_sheet_by_name("TimeSheet")

master = Tk()

firstDate = Entry(master)
firstDate.pack()

firstDate.focus_set()

def date_receiving():

    maxBlank = -1
    for rowNum in ws1.iter_rows("B6:B"+str(ws1.max_row)):
        for cell in rowNum:
            if cell.internal_value == None:
                if cell.row > maxBlank:
                    maxBlank =  cell.row
    print maxBlank

rawDescription: openpyxl - edit the last value of a row

description: Openpyxl - edit the last value of a row


code:
from ast import literal_eval

s = "'a', 'b', ['c', 'd', 'z']"
print literal_eval(s)

rawDescription: split a string by separator in python

description: Split a string by separator in python


code:
from datetime import date
end = date(2012, 5, 21)
start = date(2012, 4, 1)
Diff = end - start
print Diff.days / 7     # convert to number of weeks

rawDescription: Python week number generating

description: Python week number generating


code:
from rauth import OAuth1Service

import re
import webbrowser

request_token_url = 'http://api-public.netflix.com/oauth/request_token'
access_token_url = 'http://api-public.netflix.com/oauth/access_token'
authorize_url = 'https://api-user.netflix.com/oauth/login'
base_url = 'http://api-public.netflix.com/'

netflix = OAuth1Service(consumer_key='123',
                        consumer_secret='456',
                        request_token_url=request_token_url,
                        authorize_url=authorize_url,
                        access_token_url=access_token_url,
                        base_url=base_url)

request_token, request_token_secret = netflix.get_request_token()

oauth_callback = 'http://example.com/oauth/authorized'

params = {'oauth_callback': oauth_callback, 'application_name': 'your_app'}
authed_url = netflix.get_authorize_url(request_token, **params)

print 'Visit this URL in your browser: ' + authed_url
webbrowser.open(authed_url)

url_with_token = raw_input('Copy URL from your browser\'s address bar: ')
request_token = re.search('\?oauth_token=([^&]*)', url_with_token).group(1)

s = netflix.get_auth_session(request_token, request_token_secret)

r = s.get('users/current')
print r.content

rawDescription: oauth2 library and Netflix API return None access token

description: Oauth2 library and Netflix API return None access token


code:
import sys

from hive import ThriftHive
from hive.ttypes import HiveServerException

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol

try:
  transport = TSocket.TSocket('localhost', 10000)
  transport = TTransport.TBufferedTransport(transport)
  protocol = TBinaryProtocol.TBinaryProtocol(transport)
  client = ThriftHive.Client(protocol)
  transport.open()
  client.execute("CREATE TABLE r(a STRING, b INT, c DOUBLE)")
  client.execute("LOAD TABLE LOCAL INPATH '/path' INTO TABLE r")
  client.execute("SELECT * FROM r")
  while (1):
    row = client.fetchOne()
    if (row == None):
       break
    print row

  client.execute("SELECT * FROM r")
  print client.fetchAll()
  transport.close()
except Thrift.TException, tx:
  print '%s' % (tx.message)

rawDescription: Access Hive Data Using Python

description: Access Hive Data Using Python


code:
import itertools, collections

tweets = ['I went to work but got delayed at other work and got stuck in a traffic and I went to drink some coffee but got no money and asked for money from work',
          'We went to get our car but the car was not ready. We tried to expedite our car but were told it is not ready']

words = set(word.lower() for tweet in tweets for word in tweet.split())
_pairs = list(itertools.permutations(words, 2))
# We need to clean up similar pairs: sort words in each pair and then convert
# them to tuple so we can convert whole list into set.
pairs = set(map(tuple, map(sorted, _pairs)))

c = collections.Counter()

for tweet in tweets:
    for pair in pairs:
        if pair[0] in tweet and pair[1] in tweet:
            c.update({pair: 1})

print c.most_common(10)

rawDescription: Create tuples consisting of pairs of words


code:
#!/usr/bin/python
import bottle
import os.path

@bottle.route("/get/<filepath:path>")
def index(filepath):
    filepath = "/" + filepath
    print "getting", filepath
    if not os.path.exists(filepath):
        return "file not found"

    print open(filepath).read() # prints file
    return '
'.join(open(filepath).read().split("\n")) # prints file with
 for browser readability

@bottle.route("/exists/<filepath:path>")
def test(filepath):
    filepath = "/" + filepath
    return str(os.path.exists(filepath))

bottle.run(host='0.0.0.0', port=8090, reloader=True)

rawDescription: Does File Exist on Client Python TCP server


code:
import matplotlib.pyplot as plt
import matplotlib.text as mpl_text

class AnyObject(object):
    def __init__(self, text, color):
        self.my_text = text
        self.my_color = color

class AnyObjectHandler(object):
    def legend_artist(self, legend, orig_handle, fontsize, handlebox):
        print orig_handle
        x0, y0 = handlebox.xdescent, handlebox.ydescent
        width, height = handlebox.width, handlebox.height
        patch = mpl_text.Text(x=0, y=0, text=orig_handle.my_text, color=orig_handle.my_color, verticalalignment=u'baseline',
                                horizontalalignment=u'left', multialignment=None,
                                fontproperties=None, rotation=45, linespacing=None,
                                rotation_mode=None)
        handlebox.add_artist(patch)
        return patch

obj_0 = AnyObject("A", "purple")
obj_1 = AnyObject("B", "green")

plt.legend([obj_0, obj_1], ['Model Name A', 'Model Name B'],
           handler_map={obj_0:AnyObjectHandler(), obj_1:AnyObjectHandler()})

plt.show()

rawDescription: How to add a string as the artist in matplotlib legend?

description: Add a string as the artist in matplotlib legend


code:
peoples = []
for specified_person in soup.find_all('ns:servicetechnicianparty'):
    person = {child.name: child.text for child in specified_person.findChildren()}
    for people in person:
        peoples.append(person)
print peoples[0]

rawDescription: Object Returns Two Dict How Do I Look Only At One Dict


rawDescription: How to exclude the occurence of alpha char at the end the string

description: Exclude the occurence of alpha char at the end the string


rawDescription: Prevent empty elements in list when using an OR statement with regex in Python


code:
#!/usr/bin/python

import sys
import collections

counter = collections.Counter()

for line in sys.stdin:
    k, v = line.strip().split("\t", 2)

    counter[k] += int(v)

print counter.most_common(10)

rawDescription: MapReduce (Python) - How to sort reducer output for Top-N list?


code:
import os
path="/root/Desktop"
dirList=os.listdir(path)
for i in range(0,len(dirList)): # generate an index an loop over it
    print "%d)" % (i+1), dirList[i] # print a selection number matching each file

selected = raw_input("Select a file above: ")
selected = int(selected) # cast the input to int

print "You have selected:", dirList[selected-1] # you can get the corresponding entry!

rawDescription: Selecting a file from a stated directory in Python


code:
s = 'a\\b\tc'
print s

rawDescription: Backslashes in Python Regular Expressions


code:
data_in = ["|| 19 || 99210831 ", "John || 19 || 83291234", "Angelica |||| 99210831", "|||| 99210831"]

data = {}

for line in data_in:
    line_parts = line.replace(' ','').split('||')
    name_in, age_in, tel_in = line_parts[:3]

    saved_name, saved_age = data.get(tel_in, ('',0))

    if len(name_in) > len(saved_name):
        saved_name = name_in

    if age_in > saved_age:
        saved_age = age_in

    data[tel_in] = (saved_name, saved_age)

for tel, (name, age) in data.items():
    print '||'.join((name, age, tel))

rawDescription: Remove similar sentences in a list

description: Remove similar sentences in a list


code:
class PushIt(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        menBut=self.makeQMenuButton()
        self.show()

    def makeQMenuButton(self):
        button=QtGui.QPushButton("Men...you!", self)
        #Set up qmenu and its items
        pushMenu=QtGui.QMenu(button)
        menBut1=pushMenu.addAction("Select me!")
        menBut2=pushMenu.addAction("Don't select me!")
        button.setMenu(pushMenu)

        #set up mapping to create your own indexing scheme
        menuMapping=QtCore.QSignalMapper(self)
        menuMapping.setMapping(menBut1, 0)
        menuMapping.setMapping(menBut2, 1)

        menBut1.triggered.connect(menuMapping.map)
        menBut2.triggered.connect(menuMapping.map)
        menuMapping.mapped[int].connect(self.menButSlot)

        return button

    @QtCore.Slot(int)
    def menButSlot(self, menDat):
        print "Home-grown triggered index:", menDat

rawDescription: How to send index to slot with a QMenu

description: Send index to slot with a QMenu


code:
import re

mysql_orders = ((50434L, 5901L), (50733L, 5901L))

opera_orders = [{'orderId': 'WEB050434', 'accountId': '00T001'}, {'orderId': 'WEB050733', 'accountId': '00T001'}, {'orderId': 'DOC075185', 'accountId': '00T001'}, {'orderId': 'WEB081859', 'accountId': '00T001'}]

mysql_orders = [element for tupl in mysql_orders for element in tupl]

missing_order_ids = [opera_order['orderId'] for opera_order in opera_orders if long(re.split('\D+', opera_order['orderId'])[1]) not in mysql_orders]

print missing_order_ids

rawDescription: python compare tuples and dictionary

description: Python compare tuples and dictionary


code:
import scrapy
from scrapy.linkextractors import LinkExtractor

class RustSpider(scrapy.Spider):
    name = "rust"
    allowed_domains = ["www.rust-lang.org"]
    start_urls = (
        'http://www.rust-lang.org/',
    )

    def parse(self, response):
        extractor = LinkExtractor(allow_domains='rust-lang.org')
        links = extractor.extract_links(response)
        for link in links:
            print link.url

rawDescription: Scrapy: store all external links and crawl all interal links


code:
from urllib import urlopen
airports = 'KSFO, KSJC, KOAK'

for airport_code in airports.split(","):
    for metar in urlopen('http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' % airport_code.strip()):
        metar = metar.decode("utf-8")
        print metar

rawDescription: open file with string that has multiple variables

description: Open file with string that has multiple variables


code:
from distutils.version import StrictVersion # Or LooseVersion, if you prefer

if StrictVersion('10.2.3') > StrictVersion('10.2'):
    print "10.2.3 is newer"

rawDescription: python: dot separated versions comparizon

description: Python: dot separated versions comparizon


code:
import xmlrpclib
import httplib

class TimeoutHTTPConnection(httplib.HTTPConnection):

    def __init__(self,host,timeout=10):
        httplib.HTTPConnection.__init__(self,host,timeout=timeout)
        self.set_debuglevel(99)
        #self.sock.settimeout(timeout)

"""
class TimeoutHTTP(httplib.HTTP):
    _connection_class = TimeoutHTTPConnection
    def set_timeout(self, timeout):
        self._conn.timeout = timeout
"""

class TimeoutTransport(xmlrpclib.Transport):
    def __init__(self, timeout=10, *l, **kw):
        xmlrpclib.Transport.__init__(self,*l,**kw)
        self.timeout=timeout

    def make_connection(self, host):
        conn = TimeoutHTTPConnection(host,self.timeout)
        return conn

class TimeoutServerProxy(xmlrpclib.ServerProxy):
    def __init__(self,uri,timeout=10,*l,**kw):
        kw['transport']=TimeoutTransport(timeout=timeout, use_datetime=kw.get('use_datetime',0))
        xmlrpclib.ServerProxy.__init__(self,uri,*l,**kw)

if __name__ == "__main__":
    s=TimeoutServerProxy('http://127.0.0.1:8888',timeout=2)
    print s.test()

rawDescription: problem with xmlrpc server

description: Problem with xmlrpc server


code:
n=int(input('n='))

for m in range(1, n+1):
    d=m**2
    x = m

    all = 0
    all2 = 0

    while x != 0:
        if x%10 == d%10:
            all += 1
        x = x//10
        d = d//10
        all2 += 1

    if all == all2:
        print m

rawDescription: When does `n` match last digits of `n^2`?


code:
data = [['a', 'b', 'c'], ['aaaaaaaaaa', 'b', 'c'], ['a', 'bbbbbbbbbb', 'c']]
col_width = max(len(word) for row in data for word in row) + 2  # padding
for row in data:
    print "".join(word.ljust(col_width) for word in row)

rawDescription: Create nice column output in python

description: Create nice column output in python