code:
def frequencydict(thefile):
    proteins = {}

    for line in thefile:
        if line[0:4] == 'PDBF':
            protein = line[17:20]
            atom = line[77:78]
            if protein not in proteins:
                proteins[protein] = {}
                proteins[protein][atom] = 1
            else:
                if atom not in proteins[protein]:
                    proteins[protein][atom] = 1
                else:
                    proteins[protein][atom] += 1

    atomlist = []
    thefile.seek(0)
    for line in thefile:
        if line[0:4] == 'PDBD':
            atomlist.append(line[77:78])
    htotal = atomlist.count('H')
    ntotal = atomlist.count('N')
    ototal = atomlist.count('O')

    proteinnames = list(proteins.keys())
    for name in proteinnames:
        proteins[name]['freq'] = {}
        if 'H' in proteins[name]:
            proteins[name]['freq']['H'] = proteins[name]['H'] / float(htotal)
        else:
            proteins[name]['freq']['H'] = 0.00
        if 'N' in proteins[name]:
            proteins[name]['freq']['N'] = proteins[name]['N'] / float(ntotal)
        else:
            proteins[name]['freq']['N'] = 0.00
        if 'O' in proteins[name]:
            proteins[name]['freq']['O'] = proteins[name]['O'] / float(ototal)
        else:
            proteins[name]['freq']['O'] = 0.00

    print '       H      N      O'
    for name in proteinnames:
        print '%s   %.2f   %.2f   %.2f' % (name, proteins[name]['freq']['H'],
                                                 proteins[name]['freq']['N'],
                                                 proteins[name]['freq']['O'])
    print 'Total:  %d       %d       %d' % (htotal, ntotal, ototal)

    return proteins

rawDescription: Python: Parse through file and count specific values


code:
from numbers import Number
from decimal import Decimal
from fractions import Fraction
for n in [2, 2.0, Decimal('2.0'), complex(2,0), Fraction(2,1), '2']:
    print '%15s %s' % (n.__repr__(), isinstance(n, Number))

rawDescription: Python: most pythonic way to check if an object is a number


code:
import selenium.webdriver as webdriver

url = "http://www.youtube.com/watch?v=OuSdU8tbcHY"

driver = webdriver.Firefox()
driver.get(url)

embed = driver.find_elements_by_tag_name('embed')[0]

print embed

rawDescription: Simulate browser access to load all html elements


code:
import sys
from pylab import *
from subprocess import *
from shlex import split

# some arbitrary x,y points
points = [(11,31),(13,33),(15,37),(16,35),(17,38),(18,39.55)]

commandline = 'my_unix_prog option1 option2 ....'
command = split(commandline)

process = Popen(command, stdin=PIPE, stdout=open('outfile', 'w'), stderr=PIPE)
for p in points:
    process.stdin.write(str(p[0]) + ' ' + str(p[1]) + '\n')

print process.communicate()

rawDescription: Piping data from python to an external command


code:
with open('the_CSV_file.csv','r') as f:
  box = f.readlines()

result_box = []
for line in box[1:]:
  items = line.split(';') # adjust the separator character in the CSV as needed
  result_box.append(items[1:])

print result_box

rawDescription: Excel CSV help Python

description: Excel CSV help Python


code:
class A_part1:
    def m1(self):
        print "m1"

class A_part2:
    def m2(self):
        print "m2"

class A(A_part1, A_part2):
    pass

a = A()
a.m1()
a.m2()

rawDescription: What is a partial class?

description: What is a partial class


code:
while True:
    try:
        weight = int(raw_input("Enter your Weight: "))
    except ValueError:
        print "Please enter a valid number. Try Again."
    else:
        if 0 < weight < 500:
            break

        print "Invalid Number (should be between 0 and 500). Try Again."

rawDescription: Issues with python operators

description: Issues with python operators


code:
# columns in table x
for row in cursor.columns(table='x'):
    print row.column_name

rawDescription: Select column names in a table using PyODBC


code:
from bs4 import BeautifulSoup

data = """

                              MWF

TH

"""

soup = BeautifulSoup(data)
print list(soup.stripped_strings)  # prints [u'MWF', u'TH']

rawDescription: extracting text from mangled html tag with <br> separating the elements

description: Extracting text from mangled html tag with <br> separating the elements


code:
class PseudoDict(object):
    def __init__(self, c):
        self.c = c

    def __getitem__(self, key):
        return self.c.somethingmagical()

class C(object):
    def __init__(self):
        self.a = PseudoDict(self)
        self.b = PseudoDict(self)

c = C()
print c.a['foo']
print c.b['bar']

rawDescription: Pseudo-dicts as properties

description: Pseudo-dicts as properties


code:
import httplib
import urlparse

def getUrl(url):
    maxattempts = 10
    turl = url
    while (maxattempts  >  0) :
        host,path,query = urlparse.urlsplit(turl)[1:4]
        if  len(host.strip()) == 0 :
            return None
        try:
            connection = httplib.HTTPConnection(host,timeout=10)
            connection.request("GET", path+'?'+query)
            resp = connection.getresponse()
        except:
            return None
        maxattempts = maxattempts - 1
        if (resp.status >= 300) and (resp.status <= 399):
            turl = resp.getheader('location')
        elif (resp.status >= 200) and (resp.status <= 299) :
            return turl
        else :
            #some problem with this url
            return None
    return None
print getUrl('http://bit.ly/cDdh1c')

rawDescription: URL redirection problem

description: URL redirection problem


code:
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason
data1 = r1.read()
conn.request("GET", "/parrot.spam")
r2 = conn.getresponse()
print r2.status, r2.reason
data2 = r2.read()
conn.close()

rawDescription: multiple requests in a single connection?

description: Multiple requests in a single connection


code:
import sys
from urllib import urlretrieve
import time

url = "http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tgz" # 14.135.620 Byte
startTime = time.time()

class TooSlowException(Exception):
    pass

def convertBToMb(bytes):
    """converts Bytes to Megabytes"""
    bytes = float(bytes)
    megabytes = bytes / 1048576
    return megabytes

def dlProgress(count, blockSize, totalSize):
    global startTime

    alreadyLoaded = count*blockSize
    timePassed = time.time() - startTime
    transferRate = convertBToMb(alreadyLoaded) / timePassed # mbytes per second
    transferRate *= 60 # mbytes per minute

    percent = int(alreadyLoaded*100/totalSize)
    sys.stdout.write("\r" + "progress" + "...%d%%" % percent)
    sys.stdout.flush()

    if transferRate < 4 and timePassed > 2: # download will be slow at the beginning, hence wait 2 seconds
        print "\ndownload too slow! retrying..."
        time.sleep(1) # let's not hammer the server
        raise TooSlowException

def main():
    try:
        urlretrieve(url, '/tmp/localfile', reporthook=dlProgress)

    except TooSlowException:
        global startTime
        startTime = time.time()
        main()

if __name__ == "__main__":
    main()

rawDescription: Cancel slow download in python

description: Cancel slow download in python


code:
import numpy as np
from operator import mul

def apparatus(n, m, x, y):
    if not m:
        return np.math.factorial(n) % 1000003
    result = 1
    tmp = np.matrix([False] * x.shape[1])

    for i in xrange(x.shape[1]):
        if tmp[0, i]:
            continue
        tmp0 = np.prod(x[:, i] == x, 0)
        tmp1 = np.prod(x[:, i] == y, 0)
        if np.sum(tmp1) != np.sum(tmp0):
            return 0
        result *= np.math.factorial(np.sum(tmp1))
        result %= 1000003
        tmp += tmp1

    return result

x = np.matrix([[True, True, False]])
y = np.matrix([[False, True, True]])
print apparatus(3, 1, x, y)

x = np.matrix([[True, False, False, False], [False, False, False, False]])
y = np.matrix([[True, False, False, False], [False, False, True, False]])
print apparatus(4, 2, x, y)

print apparatus(1000, 0, [], [])

rawDescription: Solving a Programming Challenge: Apparatus, from Kattis


code:
from tkinter import *

root = Tk()
root.title("MyApp")

myvar = StringVar()

def mywarWritten(*args):
    print "mywarWritten",myvar.get()

myvar.trace("w", mywarWritten)

label = Label(root, textvariable=myvar)
label.pack()

text_entry = Entry(root, textvariable=myvar)
text_entry.pack()

root.mainloop()

rawDescription: How to connect a variable to Entry widget?

description: Connect a variable to Entry widget


code:
import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://yahoo.com').read()
soup = BeautifulSoup(page)
soup.prettify()
for anchor in soup.findAll('a', href=True):
    print anchor['href']

rawDescription: Beautiful Soup to parse url to get another urls data


code:
def find_folders(start_path,needle):
   for cwd, folders, files,in os.walk(start_path):
       if needle in folders:
           yield os.path.join(cwd,needle)

for path in find_folders("/","a_folder_named_x"):
    print path

rawDescription: Change to a known directory name but unkown absolute path in Python


code:
print "Change Calclator"

quarter = 25
dime = 10
nickel = 5
penny = 1

moneygiven = raw_input("Enter how much money given: ")
citem = raw_input("How much did the item cost?: ")
moneygiven = int(float(moneygiven) * 100)
citem = int(float(citem) * 100)
moneyback = moneygiven - citem

qmb = moneyback / quarter
partialtotal = moneyback - qmb * quarter
dmb = partialtotal // dime
dpartialtotal = partialtotal - dmb * dime
nmb = dpartialtotal // nickel
npartialtotal = dpartialtotal - nmb * nickel
pmb = npartialtotal // penny
ppartialtotal = npartialtotal - pmb * penny

print "You need %s quarters, %s dimes, %s nickels, %s pennies." % (qmb, dmb, nmb, pmb)

rawDescription: Change Calculator with exact type of change given


code:
s  ='<CUSTOMER_ID COL_ID="4000.01">79997</CUSTOMER_ID><CUSTOMER_ID COL_ID="4000.01">799</CUSTOMER_ID>'
data = re.findall(r"(?<=>)(\d+)<",s)
print sum(map(int,data))
80796

rawDescription: Python, how to get substring between two known words


code:
shared_items = set(x.items()) & set(y.items())
print len(shared_items)

rawDescription: Comparing two dictionaries in Python

description: Comparing two dictionaries in Python


code:
import os
import platform

print os.name #returns os name in simple form

platform.system() #returns the base system, in your case Linux
platform.release() #returns release version

rawDescription: How do I detect the Ubuntu version?

description: Detect the Ubuntu version


code:
import datetime, pytz
now = datetime.datetime.now(pytz.timezone('Asia/Kolkata'))

print now.strftime('%Y-%m-%d %H:%M:%S %z')

# prints: '2011-04-27 13:56:09 +0530'

rawDescription: How to get the difference in Localtime and GMT time python?

description: Get the difference in Localtime and GMT time python


code:
def callback(event):
    canvas = event.widget
    x = canvas.canvasx(event.x)
    y = canvas.canvasy(event.y)
    print canvas.find_closest(x, y)

rawDescription: Python, Tkinter: How to get coordinates on scrollable canvas


code:
cmd = "adb shell \"echo \'GET / HTTP/1.0\n\n\' | nc 173.194.33.103 80\""
p = Popen(shlex.split(cmd), stdout=PIPE, stderr=STDOUT)
#The output will be "HTTP/1.1 200 OK", "HTTP/1.0 200 OK" OR "HTTP/2.0 200 OK"
for line in iter(p.stdout.readline, b''):
    if line.rstrip().startswith('HTTP/') and line.rstrip().endswith('200 OK'):
        print '\nFOUND IT: ' + line.rstrip()
        wifi_flag = True
        break

rawDescription: Python script to check if android device is connected to internet


code:
import inspect
last_frame = inspect.stack()[1]
print 'Module imported from file:line_no = %s:%i' % last_frame[1:3]

rawDescription: Overwrite import, show names of modules importing a module


code:
if followblow == "1":
    global HaveBow
    print Victory
    HaveBow = True

rawDescription: Trying to return a True variable from a nested chain of functions (in Python)...


code:
import subprocess
import os
import sys
import threading

def on_timeout(proc, status_dict):
    """Kill process on timeout and note as status_dict['timeout']=True"""
    # a container used to pass status back to calling thread
    status_dict['timeout'] = True
    print("timed out")
    proc.kill()

runs = 1000 # Default run is 1000
if len(sys.argv)>1: # If I want to change the num of runs
    runs = int(sys.argv[1])
FNULL = open(os.devnull, 'w')
logfile = open('logfile', 'w')
# replacing example with a running program. This is a simple python
# we can call from the command line.
# args = "exe" # Exe to test
test_script = "import time;import sys;time.sleep(%d);sys.exit(100)"
succ = 0
fail = 0
for i in range (0,runs):
    # set by timer
    status_dict = {'timeout':False}
    # test prog sleeps i seconds
    args = ["python", "-c", test_script % i]
    proc = subprocess.Popen(args,
        stdout = logfile, stderr = FNULL)
    # trigger timout and kill process in 5 seconds
    timer = threading.Timer(5, on_timeout, (proc, status_dict))
    timer.start()
    proc.wait()
    # in case we didn't hit timeout
    timer.cancel()
    print status_dict
    if not status_dict['timeout'] and proc.returncode == 100:
        succ += 1 # If returned 100 then success
    else:
        fail += 1 # Else Failed
        break # Break on failure
    open('logfile', 'w').close() # Empties the file
print "Succ: %d , Fail: %d" % (succ, fail)

rawDescription: Python run Subprocess for certain time

description: Python run Subprocess for certain time


code:
for si, solution in enumerate(solutions):
    solution = np.matrix(solution)
    matched_solution = True
    tran_solution = np.transpose(solution)
    for i, row_x in enumerate(x):
        row = tran_solution[i]
        row_x_calc = []
        cur = 0
        for val in row.flat:
            if val:
                cur += 1
            elif cur:
                row_x_calc.append(cur)
                cur = 0
        else:
            if cur:
                row_x_calc.append(cur)
        if row_x != row_x_calc:
            matched_solution = False
            break
    if matched_solution:
        print 'Found', si
        print solution
        break

rawDescription: Python: Test to check proper order of boolean elements in a list of lists


code:
f = open('your_file.txt')
lines = f.readlines()
res = [x for x in lines if str(year) in x.split()[1]]
print res

rawDescription: Using a function to retrieve specific data from text file. Python 3


code:
import urllib2
from xml.etree import ElementTree as etree
#reddit parse
reddit_file = urllib2.urlopen('http://www.reddit.com/r/videos/top/.rss')
#convert to string:
reddit_data = reddit_file.read()
print reddit_data
#close file because we dont need it anymore:
reddit_file.close()

#entire feed
reddit_root = etree.fromstring(reddit_data)
item = reddit_root.findall('channel/item')
print item

reddit_feed=[]
for entry in item:
    #get description, url, and thumbnail
    desc = entry.findtext('description')
    reddit_feed.append([desc])

rawDescription: How to parse an xml feed using python?

description: Parse an xml feed using python


code:
line1 ="['a.b.c','b.c.a','c.d.e']"
line2 ="['def','efg']"
line3 ="['f']"
line4 ="['g']"
lines = [line1, line2, line3, line4]

print eval('+'.join(lines))

rawDescription: Convert and merge strings into a list in Python


code:
print "mysqldump database",
for i in range(n):
    print "shard_"+str(2*i),

rawDescription: How to automatically move specific MySQL tables from one machine to another?

description: Automatically move specific MySQL tables from one machine to another


code:
import wx

class MyForm(wx.Frame):

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

        self.radio = wx.RadioButton(panel, label="Test", style = wx.RB_GROUP)
        self.radio2 = wx.RadioButton(panel, label="Test2")

        btn = wx.Button(panel, label="Check Radio")
        btn.Bind(wx.EVT_BUTTON, self.onBtn)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.radio, 0, wx.ALL, 5)
        sizer.Add(self.radio2, 0, wx.ALL, 5)
        sizer.Add(btn, 0, wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def onBtn(self, event):
        """"""
        print "First radioBtn = ", self.radio.GetValue()
        print "Second radioBtn = ", self.radio2.GetValue()

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

rawDescription: Radio buttons using wxpython

description: Radio buttons using wxpython


code:
import os
import subprocess
import re
import win32api
pid=subprocess.check_output('netstat.exe -abno | find /i "listening" |find ":80"', shell=True).split('\n', 1)[0].split()[-1]
webserver= subprocess.check_output('tasklist /FI "PID eq '+pid+'" /v /fo List', shell=True).splitlines()[-1].split("Window Title:", 1)[1].rsplit("Window Title:", 1)[0].strip()
def getFileDescription(windows_exe):
    try:
        language, codepage = win32api.GetFileVersionInfo(windows_exe, '\\VarFileInfo\\Translation')[0]
        stringFileInfo = u'\\StringFileInfo\\%04X%04X\\%s' % (language, codepage, "FileDescription")
        description = win32api.GetFileVersionInfo(windows_exe, stringFileInfo)
    except:
        description = "unknown"
    return description
print getFileDescription(webserver)

rawDescription: python get file process name on cmd using netstat command

description: Python get file process name on cmd using netstat command


code: print tbl.findAll('tr')[2]

rawDescription: Why is my parser not working

description: Why is my parser not working


code:
cat = subprocess.Popen(["hadoop", "fs", "-cat", "/path/to/myfile"], stdout=subprocess.PIPE)
for line in cat.stdout:
    print line

rawDescription: Python read file as stream from HDFS

description: Python read file as stream from HDFS


code:
import ctypes as ct

mylib = ct.cdll.myfile
c_int = ct.c_int(0)
mylib.my_function(ct.byref(c_int))
print c_int.value

rawDescription: Is it possible to call a C Function by reference from python??


code:
import os

root = r'/root/folder'

for dirpath, dirnames, filenames in os.walk(root):
    py_files = set()
    pyc_files = set()

    for file in filenames:
        name, ext = os.path.splitext(file)
        if ext == '.py':
            py_files.add(name)
        if ext == '.pyc':
            pyc_files.add(name)

    for file in py_files.intersection(pyc_files):
        delete_file = os.path.join(dirpath, file + '.py')
        print 'Removing - {}'.format(delete_file)
        os.remove(delete_file)

rawDescription: if "a.pyc" file exist then delete "a.py" file

description: If "a.pyc" file exist then delete "a.py" file


code:
if isinstance(payload, list):
    for m in payload:
        print str(m).split()

else:
    print str(m).split()

rawDescription: payload of an email in string format, python

description: Payload of an email in string format, python


code:
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
r = redis.StrictRedis(connection_pool=pool)
cmd = "hscan <hashname> 0 match *:atlanta"
print r.execute_command(cmd)

rawDescription: redis: matching partial keys of hash

description: Redis: matching partial keys of hash


code:
class FavoriteEnroledTrainee(SessionMixin, View):
    def get(self, request, *args, **kwargs):
        print 'here'
favorite_enroled_trainee = dajaxice_register(method='GET', name='company.favorite')(FavoriteEnroledTrainee.as_view())

rawDescription: Dajaxice with Class Based View

description: Dajaxice with Class Based View


code:
from lxml import html
import time
import requests

x = 'x'

while x == 'x':
    time.sleep(10)
    page = requests.get('http://www.puregym.com/gyms/holborn/whats-happening')
    string = html.fromstring(page.content)

    people = string.xpath('normalize-space(//span[@class="people-number"]/text()[last()])')
    print people
    #printing it for debug purposes

    with open("people.txt", "w") as f:
        f.write('{}\n'.format(people))

rawDescription: lxml not getting updated webpage

description: Lxml not getting updated webpage


code:
words = ['Password1', 'password2', 'passwordthree', 'P4', 'mypassworD1!!!', '898*(*^$^@%&#abcdef']
for x in words:
    print re.search('\d.*[A-Z]|[A-Z].*\d', x)
#<_sre.SRE_Match object at 0x00000000088146B0>
#None
#None
#<_sre.SRE_Match object at 0x00000000088146B0>
#<_sre.SRE_Match object at 0x00000000088146B0>
#None

rawDescription: Python Regex: password must contain at least one uppercase letter and number


code:
line = 'This is a group of words that include #this and @that but not ME ME'

words = line.split()

result = []

pos = 0
try:
    while True:
        if words[pos].startswith(('#', '@')):
            result.append(words[pos])
            pos += 1
        elif words[pos] == 'ME':
            result.append('ME ' + words[pos + 1])
            pos += 2
        else:
            pos += 1
except IndexError:
    pass

print result

rawDescription: Python line.split to include a whitespace


code:
data = ['tua15', 'tua16', 'tub15', 'tub16', 'tuc15', 'tud15']
print sorted(data, key=lambda k: int(k[3:]))

rawDescription: ?How to sort with chars and numbers?

description: ?How to sort with chars and numbers


code:
from geopy import geocoders

g = geocoders.Google()
place, (lat, lng) = g.geocode("10900 Euclid Ave in Cleveland")
print "%s: %.5f, %.5f" % (place, lat, lng)

rawDescription: How to geocode addresses on the server

description: Geocode addresses on the server


code:
def get_negatives_at_front(list1, list2):
   a = max(len(list2), len(list1))
   ### Make the length of lists equal by adding NONE item
   if len(list1) > len(list2):
      for d in range(0, len(list1) - len(list2)):
         list2.append(None)
   elif len(list2) > len(list1):
      for d in range(0, len(list2) - len(list1)):
         list1.append(None)
   ### add negative & positive items in two seperate lists while iterating
   neg = []
   pos = []
   for l in range(0, a):
     print l
     if list1[l] != None: ###Ignore none items which was added to make length equal to the other list
        if list1[l] < 0:
           neg.append(list1[l])
        else:
           pos.append(list1[l])
     if list2[l] != None:
        if list2[l] < 0:
           neg.append(list2[l])
        else:
           pos.append(list2[l])
   print neg+pos   ### join two lists

list1 = [1, 2, -3, 4, 7]
list2 = [4, -6, 3, -1]
print(get_negatives_at_front(list1, list2))

rawDescription: Combine, compare, and order a list in a certain order


code:
if platform == 'facebook':
    from pyfacebook import FaceBook as Platform
elif platform == 'myspace':
    from pymyspace import Myspace as Platform
else:
    raise RuntimeError, "not a valid platform"

rawDescription: Load different modules without changing the logic file


code:
@memoize
def square (x):
  return x*x

square(2)
square(3)

print square.__dict__
# {'cache': {(2,): 4, (3,): 9}}

square.cache.clear()
print square.__dict__
# {'cache': {}}

rawDescription: How can the memoize cache be cleared?

description: How can the memoize cache be cleared


code:
import re

source = """one
two
three
four
five
six
seven
eight
nine
ten"""

print re.search('^three.*six.*?^.*?$', source, re.DOTALL|re.MULTILINE).group(0)

rawDescription: Matching ranges of lines in python (like sed ranges)


code:
words = ['var01', 'var02', 'var03']
label = 'Title: '
lines = ['{0}{1}'.format(label, words[0])] + ['{0}{1}'.format(' ' * len(label), w) for w in words[1:]]
print '\n'.join(lines)

rawDescription: best way to left-align strings after padding in python

description: Left-align strings after padding in python


code:
def chunker3(sentence):
    tokens = sentence.split(' ')
    len_tokens = len(tokens)
    nodes = set()

    for i in xrange(0, len_tokens):
        for j in xrange(i, len_tokens):
            chunks = tokens[i:j]

            if len(chunks) > 1:
                nodes.add(' '.join(chunks))

    intersect = mwe_list.intersection(n)

    for i in intersect:
        print i
        sentence = sentence.replace(i, i.replace(' ', '-'))

    return sentence

rawDescription: Is there a faster way of looping through a set and replacing MWE in a sentence? - Python


code:
import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.list_ctrl_1 = wx.ListCtrl(self, -1, style=wx.LC_REPORT|wx.SUNKEN_BORDER)
        sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_1.Add(self.list_ctrl_1, 1, wx.EXPAND, 0)
        self.list_ctrl_1.InsertColumn(0,"1")
        self.list_ctrl_1.InsertStringItem(0,"HELLO1")
        self.list_ctrl_1.InsertStringItem(0,"HELLO2")
        self.list_ctrl_1.InsertStringItem(0,"HELLO3")
        self.list_ctrl_1.InsertStringItem(0,"HELLO4")
        self.list_ctrl_1.InsertStringItem(0,"HELLO5")
        self.list_ctrl_1.InsertStringItem(0,"HELLO6")
        self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list_ctrl_1)
        self.Bind(wx.EVT_LIST_ITEM_DESELECTED, self.OnItemDeselected, self.list_ctrl_1)
        self.dirty = False
    def Cleanup(self, StringToPrint):
        print 'No Longer Dirty!'
        self.dirty = False

    def OnItemSelected(self,event):
        print str(self.__class__) + " - OnItemSelected"
        if not self.dirty:
            self.dirty = True
            wx.CallAfter(self.Cleanup)
        event.Skip()

    def OnItemDeselected(self,event):
        print str(self.__class__) + " - OnItemDeselected"
        if not self.dirty:
            self.dirty = True
            wx.CallAfter(self.Cleanup)
        event.Skip()

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    frame_1 = MyFrame(None, -1, "")
    app.SetTopWindow(frame_1)
    frame_1.Show()
    app.MainLoop()

rawDescription: Respond to Listctrl change exactly once

description: Respond to Listctrl change exactly once


code:
df = pd.read_excel('test.xls', header=None, index_col=0)

print df
#            1     2     3
#0
#Quarter    Q1    Q1    Q1
#Year     2013  2014  2015
#Company     A     A     A
#PA        500   300   963
#NY        200   150  1500

#set indexes
df = df.T.set_index(['Quarter', 'Year', 'Company'])

#stack and reset index
df = df.stack().reset_index(name='idx')

#rename and reorder columns
df.columns = ['Quarter', 'Year', 'Company', 'State', 'Amount']
df = df[['Quarter', 'Year', 'State', 'Company',  'Amount']]

#sort by column State
df = df.sort_values(by=['State'], ascending=['False']).reset_index(drop=True)
print df
#  Quarter  Year State Company Amount
#0      Q1  2013    NY       A    200
#1      Q1  2014    NY       A    150
#2      Q1  2015    NY       A   1500
#3      Q1  2013    PA       A    500
#4      Q1  2014    PA       A    300
#5      Q1  2015    PA       A    963

rawDescription: Un-pivoting/Stacking a pivot table with Python Pandas


code:
import Tkinter as tk

#create window & frames
class App:
    def __init__(self):
        self.root = tk.Tk()
        self._job = None
        self.slider = tk.Scale(self.root, from_=0, to=256,
                               orient="horizontal",
                               command=self.updateValue)
        self.slider.pack()
        self.root.mainloop()

    def updateValue(self, event):
        if self._job:
            self.root.after_cancel(self._job)
        self._job = self.root.after(500, self._do_something)

    def _do_something(self):
        self._job = None
        print "new value:", self.slider.get()

app=App()

rawDescription: TkInter, slider: how to trigger the event only when the iteraction is complete?


code:
with open(file1) as f1:
  with open(file2) as f2:
    for line1, line2 in itertools.izip(f1, f2):
      if line1 != line2:
        print 'files are different'
        break

rawDescription: Python double iteration

description: Python double iteration


code:
import mechanize
from bs4 import BeautifulSoup
import urllib2
import cookielib

cj = cookielib.CookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.open("https://id.arduino.cc/auth/login/")

br.select_form(nr=0)
br.form['username'] = 'username'
br.form['password'] = 'password.'
br.submit()

print br.response().read()

rawDescription: How to scrape a website which requires login using python and beautifulsoup?

description: Scrape a website which requires login using python and beautifulsoup


code:
import urllib2

def read_range(url, rstart, rstop):
    # First request url
    response = urllib2.urlopen(url)

    # Ignore content to start byte
    response.read(max(rstart-1,0))

    # Read bytes we want.
    return response.read(rstop - rstart)

# First 200 bytes.
print read_range("http://stackoverflow.com", 0, 200)

# Last 200 bytes
print read_range("http://stackoverflow.com", 200, 0)

# Some bytes right in the middle
print read_range("http://stackoverflow.com", 400, 1000)

# Read whole file
print read_range("http://stackoverflow.com", 0, -1)

rawDescription: How to download a file from one byte to another byte.(for example downloading footer of an html file)

description: Download a file from one byte to another byte.(for example downloading footer of an html file)


code:
import serial
import time
from datetime import datetime

ser = serial.Serial('/dev/ttyAMA0', 9600)

while True:
   if ser.isOpen()==True:
       ser.write('k') # Arduino sends sensor analog reading when receiving the letter "k"
       datetime = str(datetime.now())
       print(datetime)
       print(ser.readline())
       time.sleep(1) # take a break so that you dont thrash the sensor with requests all the time
   else:
       try:
           print "Lost connection at %s" % str(datetime.now())
           ser.open()
       except:
           print "Cannot open port."
           exit(-1)

rawDescription: Raspberry Pi connection with Arduino over XRF continuous sensor monitoring


code:
#!/usr/bin/env python
import os
import sqlite3
import pwd

_cookieName = "preferredLanguage"

def getPreferredLanguageFromCookieDB():
    retval="en-US"
    cookieDBFilename = os.path.join(pwd.getpwuid(1000).pw_dir, ".config/google-chrome/Default/Cookies")
    if os.path.isfile(cookieDBFilename):
        connection = sqlite3.connect(cookieDBFilename)
        querier = connection.cursor()
        numCookiesMatching = int(querier.execute('SELECT COUNT(*) FROM cookies WHERE (host_key="127.0.0.1" or host_key="localhost") and name="%s"' % (_cookieName)).fetchone()[0])
        if numCookiesMatching == 1:
            retval = querier.execute('SELECT value FROM cookies WHERE (`cookies`.`host_key`="127.0.0.1" or `cookies`.`host_key`="localhost") and `cookies`.`name` = "%s"' % (_cookieName)).fetchone()[0]
        elif numCookiesMatching == 0:
            print("::getPreferredLanguageFromCookieDB > No cookie for '%s' found. Assuming wizard hasn't run yet, which is weird, but not critical" % (_cookieName))
            retval="en-US"
        else:
            raise KeyError("Found %s cookies matching %s in file %s. This shouldn't have happened" % (numCookiesMatching, _cookieName, cookieDBFilename))
            retval=None
    else:
        print("::getPreferredLanguageFromCookieDB > Cookie 'db' (actually, file) %s doesn't exist" % (cookieDBFilename))
        retval="en-US"

    return retval

if __name__ == "__main__":
    print "Prefered language: %s" % getPreferredLanguageFromCookieDB()

rawDescription: Ubuntu Chrome: How to read a cookie from a python script


code:
tlist = ['Hello world',
   'Hello world print',
   'Text',
   'World hello print']

print [i for i in tlist if "print" in i]

rawDescription: Serach in list string element by value

description: Serach in list string element by value


code:
backupDir = "C:/Users/myuser/backups/"
backupFile = "mybackup.zip"
file = zipfile.ZipFile(backupDir + backupFile, "w")
for name in os.listdir(backupDir):
    if name == backupFile:
        continue
    fullname = backupDir + name
    print "Found " + name
    file.write(fullname, os.path.basename(name), zipfile.ZIP_DEFLATED)
file.close()

rawDescription: Python zipping all other contents inside the same file


code:
import urllib
import urllib2
import json

url = 'https://www.googleapis.com/rpc'
requests = [{
  'method': 'freebase.text.get',
  'apiVersion': 'v1',
  'params': {
   'id': ['en','bob_dylan']
  }
},{
  'method': 'freebase.text.get',
  'apiVersion': 'v1',
  'params': {
    'id': ['en','blade_runner']
  }
}]
headers = { 'Content-Type': 'application/json' }
req = urllib2.Request(url, json.dumps(requests), headers)
response = urllib2.urlopen(req)
print response.read()

rawDescription: Making multiple MQL queries to Freebase from Google GWT Appengine with Python apiclient


code:
def confirm():
    while True:
        answer = raw_input('Are you sure? ')
        if answer == 'yes':
            return True
        if answer == 'no':
            return False
        else:
            continue # to reprompt user if the answer is not "yes" or "no"

def your_name():
    while True:
        name = raw_input("What is your name? ")
        if not name:
            continue # to reprompt user if they do not enter anything
        elif confirm():
            print 'Congratulations! You have a name!'
            break
your_name()

rawDescription: Python: Problems when calling outside function


code:
class foo:

   def f0(self, s):
      print s

   def f1(self, s):
      print "not " + s
      yield 1

   def run(self):
      handlers={0 : self.f0, 1 : self.f1}
      handlers[0]('good')
      handlers[1]('good').next()

bar = foo()
bar.run()

rawDescription: dynamic call of functions and generator function (python)

description: Dynamic call of functions and generator function (python)


code:
NUM_TABLES = 10

factories = [ ]
for i in range(0, NUM_TABLES):
    print i
    factory = Factory()
    factory.protocol = Socket
    factory.clients = []
    factories.append(factory)
    reactor.listenTCP(1025+i, factory)

# print "Blackjack server started"

reactor.run()

# whatever code you put here, is executed only **after reactor has stopped**

rawDescription: Reactor.run freezes loop

description: Reactor.run freezes loop


code: print "key=(%s), val=(%s)" % (str(y), df2[df2.cyear == y])

rawDescription: Python Dictionary Data Issue

description: Python Dictionary Data Issue


code:
class Test(object):
    def __getattribute__(self, name):
        attr = super(Test, self).__getattribute__(name)
        if callable(attr):
            return attr()
        return attr
    def f(self):
        print 'f'
        return self
    def g(self):
        print 'g'
        return self
    def h(self, x):
        print x
        return self

t = Test()
t.f.g # Works fine
t.f().g # Fails - the parens aren't optional, they must be left off now
t.f.g.h(1) # Fails - unable to provide an argument

rawDescription: Python API - pattern for creating optional parenthesis?


code:
class bad(object):
    def __eq__(self, other):
        return False

x = bad()
print x is x, x==x # True, False

rawDescription: Does it make sense to check for identity in __eq__?


code:
d = {}
# Group the weights based on the indices
for wt, idx in test:
    d.setdefault(idx, []).append(wt)

# Create a new list with the max of weights and the index tuples
print [(max(d[idx]), idx) for idx in d]
# [(1.0, 1490), (1.0, 1491), (1.0, 2456), (0.75, 250), (1.0, 219), (1.0, 218)]

rawDescription: Array of tuples: return unique values based on weights (Python)


code:
with open('file.txt', 'r+') as f:
   flag = False
   for line in f:
        if line.startswith("PARN"):
            flag = False
        if flag:
            print line
        if line.startswith("ABST"):
            flag = True

rawDescription: Reading certain paragraphs out of text file in Python?


code:
import numpy as np
import time
def createdictfun(n):
    optdict = { x: 2*x for x in range(0,n) }
    def dictfun(x):
        return optdict[x]
    return dictfun

def createiffun(n):
    s="def iffun(x):\n"
    s+="  if x==0: return 0\n"
    for i in range(1,n):
        s+="  elif x=={}: return {}\n".format(i,i*2)
    s+="  else: raise ValueError\n"
    exec s
    return iffun
ntry=10
maxchoice=1000
trialsize=1000
ifvals=np.zeros((maxchoice,2))
dictvals=np.zeros((maxchoice,2))
ns=np.arange(1,maxchoice)
for n in ns:
    ift = np.zeros(ntry)
    dictt = np.zeros(ntry)
    vals=np.random.randint(0,n,size=trialsize)
    iffun = createiffun(n)
    dictfun = createdictfun(n)
    for trial in range(0,ntry):
        ts=time.time()
        for x in vals:
            iffun(x)
        ift[trial]=time.time()-ts
        ts=time.time()
        for x in vals:
            dictfun(x)
        dictt[trial]=time.time()-ts
    ifvals[n,0]=np.mean(ift)/trialsize
    ifvals[n,1]=np.std(ift)/trialsize
    dictvals[n,0]=np.mean(dictt)/trialsize
    dictvals[n,1]=np.std(dictt)/trialsize
    print str(n)+" ",

rawDescription: Python: How to test efficiency of IF statement to a Dictionary Case statement?


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from examples.dogpile_caching.caching_query import query_callable, FromCache, RelationshipCache
from hashlib import md5
from dogpile.cache.region import make_region

Base = declarative_base()

class Person(Base):
    __tablename__ = 'person'
    id = Column(Integer, primary_key=True)
    name = Column(String(100), nullable=False)
    type = Column(String(50))
    __mapper_args__ = {
        'polymorphic_identity': 'object',
        'polymorphic_on': type
        }

class Man(Person):
    __tablename__ = 'man'
    id = Column(Integer, ForeignKey('person.id'), primary_key=True)
    age = Column(String(100), nullable=False)
    __mapper_args__ = {'polymorphic_identity': 'man'}

class SomethingElse(Person):
    __tablename__ = 'somethingelse'

    id = Column(Integer, ForeignKey('person.id'), primary_key=True)
    age = Column(String(100), nullable=False)
    __mapper_args__ = {'polymorphic_identity': 'somethingelse'}

class Config(Base):
    __tablename__ = "config"
    id = Column(Integer, primary_key=True)
    person = Column(Integer, ForeignKey('person.id'))
    address = Column(String)
    person_ref = relationship(Person)

e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)

def md5_key_mangler(key):
    """Receive cache keys as long concatenated strings;
    distill them into an md5 hash.

    """
    return md5(key.encode('ascii')).hexdigest()

regions = {}
regions['default'] = make_region(
            key_mangler=md5_key_mangler
            ).configure(
                'dogpile.cache.memory_pickle',
            )

Session = scoped_session(
                sessionmaker(
                    bind=e,
                    query_cls=query_callable(regions)
                )
            )

sess = Session()
sess.add(Config(person_ref=SomethingElse(age='45', name='se1')))
sess.add(Config(person_ref=Man(age='30', name='man1')))
sess.commit()

all_types = with_polymorphic(Person, "*", aliased=True)

conf = sess.query(Config).options(joinedload(Config.person_ref.of_type(all_types)), FromCache("default")).first()
sess.commit()
sess.close()

print "_____NO MORE SQL!___________"

conf = sess.query(Config).options(joinedload(Config.person_ref.of_type(all_types)), FromCache("default")).first()
print conf.person_ref.age

rawDescription: SqlAlchemy cache with dogpile

description: SqlAlchemy cache with dogpile


code:
cursor.execute("""SELECT fname from employees WHERE user = %s""", (useruname,))
row = cursor.fetchone()
print row[0]

rawDescription: Trying to get one cell's values with MySQLdb


code:
from Cookie import SimpleCookie
c = SimpleCookie()
c['test'] = 'MYTEST'
print c

rawDescription: Create a cookie that lasts until the browser is closed(Session Cookie?)


code:
from itertools import izip

for synset, synset_scores in izip(wn.synsets(wordstr), swn_senti_synset):
    # Now you can deal with both at once in this loop.
    len_lemma_names = len(synset.lemma_names)
    count_lemma += len_lemma_names
    count_synset += 1
    # Mix to taste.
    print len_lemma_names, count_synset

rawDescription: Python: print variable together from different for loop


code:
def startElement(self, name, attrs):
    self.map[name] = ''
    self.tag = name

def characters(self, content):
    self.map[self.tag] += content

def endElement(self, name):
    print self.map[name]

rawDescription: How can I get the text between tags using python SAX parser?

description: Get the text between tags using python SAX parser


code:
import functools
def f(x,y):
    return x+y

print functools.reduce(f, [1, 2, 3, 4]) # prints 10
print functools.reduce(f, [1, 2, 3, 4], 10) # prints 20, because it initializes at 10, not 0.

rawDescription: Python 3: apply an operator over an iterable


code:
from bs4 import BeautifulSoup as BS
text = ''''''
soup = BS(text)
print soup.find('img').attrs['alt']

rawDescription: Regex to find a string python

description: Regex to find a string python


code:
from collections import deque

NUM_LINES=5 #The number of lines to process.  Should equal the deque maxlen

lines = open("myfile.csv").readlines()[-NUM_LINES:] #Assumes the file can fit into memory
mydata = [line.split()[0] for line in lines]
d = deque(mydata, maxlen=NUM_LINES)
print d

rawDescription: python - read csv file from last line

description: Python - read csv file from last line


code:
import win32gui
import win32ui
from ctypes import windll
import Image

hwnd = win32gui.FindWindow(None, 'Calculator')

# Change the line below depending on whether you want the whole window
# or just the client area.
#left, top, right, bot = win32gui.GetClientRect(hwnd)
left, top, right, bot = win32gui.GetWindowRect(hwnd)
w = right - left
h = bot - top

hwndDC = win32gui.GetWindowDC(hwnd)
mfcDC  = win32ui.CreateDCFromHandle(hwndDC)
saveDC = mfcDC.CreateCompatibleDC()

saveBitMap = win32ui.CreateBitmap()
saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)

saveDC.SelectObject(saveBitMap)

# Change the line below depending on whether you want the whole window
# or just the client area.
#result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 1)
result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
print result

bmpinfo = saveBitMap.GetInfo()
bmpstr = saveBitMap.GetBitmapBits(True)

im = Image.frombuffer(
    'RGB',
    (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
    bmpstr, 'raw', 'BGRX', 0, 1)

win32gui.DeleteObject(saveBitMap.GetHandle())
saveDC.DeleteDC()
mfcDC.DeleteDC()
win32gui.ReleaseDC(hwnd, hwndDC)

if result == 1:
    #PrintWindow Succeeded
    im.save("test.png")

rawDescription: Python Screenshot of inactive window PrintWindow + win32gui


code:
import NetworkManager

for dev in NetworkManager.NetworkManager.GetDevices():
    if dev.DeviceType != NetworkManager.NM_DEVICE_TYPE_WIFI:
        continue
    for ap in dev.SpecificDevice().GetAccessPoints():
        print ap.Ssid

rawDescription: How can I get a list of all WLANs my computer can see with Python?

description: Get a list of all WLANs my computer can see with Python


code:
import pyHook
import pythoncom

def onclick(event):
    print event.Position
    return True

hm = pyHook.HookManager()
hm.SubscribeMouseAllButtonsDown(onclick)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()

rawDescription: Detecting Mouse clicks in windows using python


code:
from numpy import *

#vs,fs,rs are all m*n matrixs,got initial values in,i.e vs[0],fs[0],rs[0] are known
#want use this foor loop to update them
vs=zeros((10,3))
vs[0]=([1,2,3])
fs=zeros((10,3))
fs[0]=([2,3,4])
rs=zeros((10,3))
rs[0]=([3,4,5])

for i in range(5):
    #start looping..
    vs[i+1]=vs[i]+fs[i]
    fs[i+1]=rs[i]-re[i]
    rs[i+1]=rs[i]+vs[i]
    print vs,fs,rs

rawDescription: help!for function

description: Help!for function


code:
#!/usr/bin/env python
import httplib
import optparse
import sys
import urllib

class GoogleMaps(object):
    source = "daybarr.com-kmluploader-0.1"

    def __init__(self, email, passwd):
        self.email = email
        self.passwd = passwd
        self._conn = None
        self._auth_token = None

    def _get_connection(self):
        if not self._auth_token:
            conn = httplib.HTTPSConnection("www.google.com")
            params = urllib.urlencode({
                "accountType": "HOSTED_OR_GOOGLE",
                "Email": self.email,
                "Passwd": self.passwd,
                "service": "local",
                "source": self.source,
            })
            headers = {
                "Content-type": "application/x-www-form-urlencoded",
                "Accept": "text/plain",
            }
            conn.request("POST", "/accounts/ClientLogin", params, headers)
            response = conn.getresponse()
            if response.status != 200:
                raise Exception("Failed to login: %s %s" % (
                    response.status,
                    response.reason))
            body = response.read()
            for line in body.splitlines():
                if line.startswith("Auth="):
                    self._auth_token = line[5:]
                    break
            if not self._auth_token:
                raise Exception("Cannot find auth token in response %s" % body)
        if not self._conn:
            self._conn = httplib.HTTPConnection("maps.google.com")
        return self._conn

    connection = property(_get_connection)

    def upload(self, kml_data):
        conn = self.connection
        headers = {
            "GData-Version": "2.0",
            "Authorization": 'GoogleLogin auth=%s' % (self._auth_token,),
            "Content-Type": "application/vnd.google-earth.kml+xml",
        }
        conn.request("POST", "/maps/feeds/maps/default/full", kml_data, headers)
        response = conn.getresponse()
        if response.status != 200:
            raise Exception("Failed to upload kml: %s %s" % (
                response.status,
                response.reason))
        return response.read()

if __name__ == "__main__":
    parser = optparse.OptionParser()
    parser.add_option("-e", "--email", help="Email address for login")
    parser.add_option("-p", "--passwd", help="Password for login")
    options, args = parser.parse_args()
    if not (options.email and options.passwd):
        parser.error("email and passwd required")
    if args:
        kml_file = open(args[0], "r")
    else:
        kml_file = sys.stdin
    maps = GoogleMaps(options.email, options.passwd)
    print maps.upload(kml_file.read())

rawDescription: how can I upload a kml file with a script to google maps?

description: Upload a kml file with a script to google maps


code:
import pyodbc
cnxn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Public\\Database1.accdb;')
cursor = cnxn.cursor()
cursor.execute("INSERT INTO Clients (FirstName, LastName) VALUES (?, ?)", ['Mister', 'Gumby'])
cursor.commit()
cursor.execute("SELECT @@IDENTITY AS ID")
row = cursor.fetchone()
print row.ID
cnxn.close()

rawDescription: Using SELECT LAST() with pyodbc and MSACCESS sometimes returns same value


code:
def find_documents(query_string, limit, cursor):
    try:
        date_desc = search.SortExpression(expression='date',
                direction=search.SortExpression.DESCENDING,
                default_value=datetime(1999,01,01))

        hr_desc = search.SortExpression(expression='hour',
                direction=search.SortExpression.DESCENDING,
                default_value=1)

        min_desc = search.SortExpression(expression='minute',
                direction=search.SortExpression.DESCENDING,
                default_value=1)

        # Sort up to  matching results by subject in descending order
        sort = search.SortOptions(expressions=[date_desc, hr_desc,
                                  min_desc], limit=ACCURACY)

        # Set query options
        options = search.QueryOptions(limit=50, cursor=cursor,
                sort_options=sort,
                number_found_accuracy=10000,
              #  returned_fields=['title', 'city', 'region','category', 'adID', 'date','price', 'type', 'company_ad', 'adID', 'cityID','regionID', 'hour','minute'],
             #snippeted_fields=['text']
              )
        query = search.Query(query_string=query_string, options=options)
        index = search.Index(name=_INDEX_NAME)
        logging.debug('query_string i find %s' , str(query.query_string))
        logging.debug('query_options i find %s' , str(query.options))
        # Execute the query
        return index.search(query)

    except search.PutError as e:
        logging.exception('caught PutError %s', e)

    except search.InternalError as e:
        logging.exception('caught InternalError %s', e)

    except search.DeleteError as e:
        logging.exception('caught DeleteError %s', e)

    except search.TransientError as e:
        logging.exception('caught TransientError %s', e)

    except search.InvalidRequest as e:
        logging.exception('caught InvalidError %s', e)

    except search.Error as e:
        logging.exception('caught unknown error  %s', e)

    return None

rawDescription: Google app Engine offset greater than 1000 in Full text search causing error


code:
import win32pdh
import os

def getPIDInfo():
    """
    Return a dictionary with keys the PID of all running processes.
    The values are dictionaries with the following key-value pairs:
        - name: <Name of the process PID>
        - parent_id: <PID of this process parent>
    """

    # get the names and occurences of all running process names
    items, instances = win32pdh.EnumObjectItems(None, None, 'Process', win32pdh.PERF_DETAIL_WIZARD)
    instance_dict = {}
    for instance in instances:
        instance_dict[instance] = instance_dict.get(instance, 0) + 1

    # define the info to obtain
    counter_items =  ['ID Process', 'Creating Process ID']

    # output dict
    pid_dict = {}

    # loop over each program (multiple instances might be running)
    for instance, max_instances in instance_dict.items():
        for inum in xrange(max_instances):
            # define the counters for the query
            hq = win32pdh.OpenQuery()
            hcs = {}
            for item in counter_items:
                path = win32pdh.MakeCounterPath((None,'Process',instance, None,inum,item))
                hcs[item] = win32pdh.AddCounter(hq,path)
            win32pdh.CollectQueryData(hq)

            # store the values in a temporary dict
            hc_dict = {}
            for item, hc in hcs.items():
                type,val=win32pdh.GetFormattedCounterValue(hc,win32pdh.PDH_FMT_LONG)
                hc_dict[item] = val
                win32pdh.RemoveCounter(hc)
            win32pdh.CloseQuery(hq)

            # obtain the pid and ppid of the current instance
            # and store it in the output dict
            pid, ppid = (hc_dict[item] for item in counter_items)
            pid_dict[pid] = {'name': instance, 'parent_id': ppid}

    return pid_dict

def getParentInfo(pid):
    """
    Returns a PID, Name tuple of the parent process for the argument pid process.
    """
    pid_info = getPIDInfo()
    ppid = pid_info[pid]['parent_id']
    return ppid, pid_info[ppid]['name']

if __name__ == "__main__":
    """
    Print the current PID and information of the parent process.
    """
    pid = os.getpid()
    ppid, ppname = getParentInfo(pid)

    print "This PID: %s. Parent PID: %s, Parent process name: %s" % (pid, ppid, ppname)
    dummy = raw_input()

rawDescription: Detect script start up from command prompt or "double click" on Windows


code:
import __builtin__
openfiles = set()
oldfile = __builtin__.file
class newfile(oldfile):
    def __init__(self, *args):
        self.x = args[0]
        print "### OPENING %s ###" % str(self.x)
        oldfile.__init__(self, *args)
        openfiles.add(self)

    def close(self):
        print "### CLOSING %s ###" % str(self.x)
        oldfile.close(self)
        openfiles.remove(self)
oldopen = __builtin__.open
def newopen(*args):
    return newfile(*args)
__builtin__.file = newfile
__builtin__.open = newopen

def printOpenFiles():
    print "### %d OPEN FILES: [%s]" % (len(openfiles), ", ".join(f.x for f in openfiles))

rawDescription: check what files are open in Python

description: Check what files are open in Python


code:
from sqlalchemy.sql import func
qry = session.query(func.max(Score.score).label("max_score"),
                    func.sum(Score.score).label("total_score"),
                    )
#qry = qry.group_by(Score.some_group_column)
for _res in qry.all():
    print _res

rawDescription: sqlalchemy simple example of `sum`, `average`, `min`, `max`

description: Sqlalchemy simple example of `sum`, `average`, `min`, `max`


code:
import urllib
import urllib2
from bs4 import BeautifulSoup as bs

url = raw_input('enter - ')
req = urllib2.Request(url, headers={'User-Agent': 'Mozilla/5.0'})  # <--
html = urllib2.urlopen(req).read()
soup = bs(html)
tags = soup('a')
for tag in tags:
    print tag.get('href', None)

rawDescription: finding unique web links using python

description: Finding unique web links using python


code:
for base in 'ACGT':
  print base, thesequence.count(base) + thesequence.count(base.lower())

rawDescription: Count the number of occurences of letters in a Python string


code:
import shelve
db = shelve.open('data.db')
print db['keyname']
db['keyname'] = 'some new user data'
db.sync()
db.close()

rawDescription: File editing and saving

description: File editing and saving


code:
def lambda_handler(event, context):

        print("Received event: " + json.dumps(event, indent=2))

        if 'Records' not in event:
            print ('records not in event')
            return

        for record in event['Records']:
            if record['eventName'] == 'INSERT':
                print 'do something on insert'

            your_integer_hash_key = record['dynamodb']['Keys']['your_hash_key']['N']

            # if all image is stream
            if not 'NewImage' in record['dynamodb']:
                continue

            # get new image
            new_image = record['dynamodb']['NewImage']

rawDescription: DynamoDB JSON formatting on AWS Lambda

description: DynamoDB JSON formatting on AWS Lambda


code:
wd = webdriver.Firefox()
wd.implicitly_wait(3)

def get_items(items):
    print len(items)
    wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    # len(items) and len(wd.find_elements-by...()) both always seem to return the same number
    # if I were to start the loop with while True: it would work, but of course... never end

    sleep(5) #seconds
    while len(wd.find_elements_by_class_name('stream-item')) > len(items):
        items = wd.find_elements_by_class_name('stream-item')
        print items
        wd.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    return items

def test():
    get_page('http://twitter.com/')
    get_items(wd.find_elements_by_class_name('stream-item'))

rawDescription: Selenium: retrieve data that loads while scrolling down


code:
for x in popped:
    print 'Popping',x,'from the stack.'

rawDescription: Python: I'm misunderstanding stack logic, but I'm not sure where


code:
import codecs
import tamil
from random import shuffle

inp_file = codecs.open("/home/sibi/Desktop/scramble.txt",encoding="utf-8")
inp_text = inp_file.read().rstrip()

tamil_letters = tamil.utf8.get_letters(inp_text)
shuffle(tamil_letters)
tamil_letters = "".join(tamil_letters).encode("utf-8")
print tamil_letters

out_file = open('outputscrambled.txt','w')
out_file.write(tamil_letters)

rawDescription: Scrambling of letters within a text file


code:
import unidecode
image = Image.open(ImagePath)
text = image_to_string(image)
print unidecode(text)

rawDescription: image to text - remove non-ascii chars in python 2.7

description: Image to text - remove non-ascii chars in python 2.7


code:
class scrap(object):

    def dirae(self, word):
        url = 'http://dirae.es/palabras/' + word
        site = urllib2.urlopen(url)
        soup = BeautifulSoup(site.read())
        print soup.select('div.definitionContent')[0].get_text().enconde('utf-8')

rawDescription: How do I stop each letter from printing on different line?

description: Stop each letter from printing on different line


rawDescription: Compare 2D lists and return index where they differ


code:
for cookie in cj:
   print cookie.name, cookie.value, cookie.domain #etc etc

rawDescription: Get cookie from CookieJar by name

description: Get cookie from CookieJar by name


code:
list1 = ['3','ex','fish nets','orange','banana','exampl','apple']
list2 = ['e','x','blah','exam','p','l','blahblah']
a=list2
j=['e','x','blah','exam','p','l','blahblah']
t=[]
for y in range(0, len(j)):
            for i in range(1,len(j)-y):
                    r=''
                    t=j[y:y+i+1]    #Create a list of adjacent elements.
                    for x in t:
                        r+=str(x)    #make it string and check if it fits.
                    if r in list1:
                        list2[y]=r
                        for e in range(y+1,y+i+1):
                            list2[e]=0    # to avoid a mess with indexes.
while 0 in list2:
    list2.remove(0)
print list2

rawDescription: Referencing value and values after it

description: Referencing value and values after it


code:
class DefaultList(list):
    def __init__(self,*args,**kwargs):
        list.__init__(self,*args)
        self.default=kwargs.get('default',None)
    def __getitem__(self,key):
        # retrieving an item does not expand the list
        if isinstance(key,slice):
            return [self[elt] for elt in range(key.start,key.stop,key.step)]
        else:
            try:
                return list.__getitem__(self,key)
            except IndexError:
                return self.default
    def __setitem__(self,key,value):
        # setting an item may expand the list
        try:
            list.__setitem__(self,key,value)
        except IndexError:
            self.extend([self.default]*(key-len(self)))
            self.append(value)

x = DefaultList(list('abc'), default='*')
print(x)
# ['a', 'b', 'c']
x[6] = 'g'
print(x)
# ['a', 'b', 'c', '*', '*', '*', 'g']
print x[2], x[4], x[6], x[8]  # should print 'c * g *'
# c * g *
print(x[2:9:2])
# ['c', '*', 'g', '*']

rawDescription: Autogrowing list in Python?

description: Autogrowing list in Python


code:
from selenium import webdriver

driver = webdriver.Firefox()
driver.get(url)
driver.set_window_position(0, 0)
driver.set_window_size(100000, 200000)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(5) # wait to load

# now print the response
print driver.page_source

rawDescription: How to scrape a website with table content that is retrieved by javascript?

description: Scrape a website with table content that is retrieved by javascript


code: print "\\bin\\example.exe"

rawDescription: How to stop python from reading regex from a string

description: Stop python from reading regex from a string


code:
from twisted.web.server     import Site
from twisted.web.resource   import Resource
from twisted.internet       import reactor

class StatusPageResource(Resource):
    isLeaf           = True

    def __init__(self, param1):
        self.param1 = param1
        # Call the constructor of the super class
        Resource.__init__(self)

    def render_GET(self, request):
        return "%s" % self.param1

my_res = Resource()
my_res.putChild('GetStatusPage1', StatusPageResource(param1='abc'))
my_res.putChild('GetStatusPage2', StatusPageResource(param1='xyz'))

factory = Site(my_res)
reactor.listenTCP(8080, factory)
print 'Runnning on port 8080'
reactor.run()

rawDescription: Python holding an webpage active

description: Python holding an webpage active


code:
var_list = ['default']
print var_list[-1] # 'default'
var_list.append('new var')
print var_list[-1] # 'new var'

rawDescription: How to store multiple variable values in Python

description: Store multiple variable values in Python


code:
import Image, numpy

def subimg(img1,img2):
    img1=numpy.asarray(img1)
    img2=numpy.asarray(img2)

    #img1=numpy.array([[1,2,3],[4,5,6],[7,8,9]])
    #img2=numpy.array([[0,0,0,0,0],[0,1,2,3,0],[0,4,5,6,0],[0,7,8,9,0],[0,0,0,0,0]])

    img1y=img1.shape[0]
    img1x=img1.shape[1]

    img2y=img2.shape[0]
    img2x=img2.shape[1]

    stopy=img2y-img1y+1
    stopx=img2x-img1x+1

    for x1 in range(0,stopx):
        for y1 in range(0,stopy):
            x2=x1+img1x
            y2=y1+img1y

            pic=img2[y1:y2,x1:x2]
            test=pic==img1

            if test.all():
                return x1, y1

    return False

small=Image.open('small.tif')
big=Image.open('big.tif')

print subimg(small, big)

rawDescription: How to find an image within another image using python

description: Find an image within another image using python


code:
import gtk

icon_theme = gtk.icon_theme_get_default()
print icon_theme.list_icons()

rawDescription: How to know which icons are provided by AppIndicator?

description: Know which icons are provided by AppIndicator


code:
from Bio.Blast import NCBIXML

with open('/home/edson/ungulate/tblastn_result_test_xml') as tblastn_file:
    tblastn_records = NCBIXML.parse(tblastn_file)
    for tblastn_record in tblastn_records:
        for alignment in record.alignments[:4]:
            print alignment.title
            print alignment.hsps[0]

rawDescription: Extracting top four tblastn subject hits using biopython


code:
from pymongo import MongoClient

client = MongoClient()

db = client.test_database

print db.command("collstats", "test_collection")

rawDescription: How to run raw mongodb commands from pymongo

description: Run raw mongodb commands from pymongo


code:
from PIL import Image
im = Image.open("C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg")
im.save("C:\Users\Public\Pictures\Sample Pictures\Jellyfish_compressed.jpg", quality=10)

rawDescription: In Python's PIL, how do I change the quality of an image?


code:
import os

for r, d, f in os.walk(self.DirFilename):
    for file in f:
        f_name, f_ext = os.path.splitext(file)
        if ".mov" == f_ext:
            if f_name in selectedList:
                src_abs_path = os.path.join(r, file)
                src_relative_path = os.path.relpath(src_abs_path, self.DirFilename)
                dst_abs_path = os.path.join(self.DirDest, src_relative_path)
                dst_dir = os.path.dirname(dst_abs_path)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                ret = os.system('cp -fr %s %s' % (src_abs_path, dst_abs_path))
                if ret != 0:
                    print 'An error occurred!'

rawDescription: Copy Files From List Python

description: Copy Files From List Python


code:
#!/usr/bin/env python
import time
from gi.repository import Gst, GstTag
Gst.init(None)
uri='file:///home/rolf/H.mkv'
pipeline=Gst.ElementFactory.make("playbin", "playbin")
pipeline.set_property('uri',uri)
try:
    prefix = uti.rindex('.')
    suburi = uri[:prefix]
    suburi = suburi+".srt"
    suburi = pipeline.set_property('suburi', suburi)
except:
    pass
pipeline.set_state(Gst.State.PLAYING)
time.sleep(0.5)
subs = pipeline.get_property('n-text')
auds = pipeline.get_property('n-audio')
vids = pipeline.get_property('n-video')
print vids, "Video Streams ", auds, "Audio Streams ", subs, "Subtitle Streams"
subc = pipeline.get_property('current-text')

dur = (pipeline.query_duration(Gst.Format.TIME)[1]) / Gst.SECOND
hh = int(dur/3600)
mm, ss = (divmod(int(divmod(dur,3600)[1]),60))
print("Duration : %02d:%02d:%02d" % (hh,mm,ss))
for x in xrange(subs):
    tags = pipeline.emit("get-text-tags", x)
    if (tags):
        name = tags.nth_tag_name(0)
        if name == "language-code":
            current_code = tags.get_string(name)[1]
            language = GstTag.tag_get_language_name(current_code)
            print "Subtitle stream ",current_code, "Index ", x, language

    else:
        print "No subtitle tags listed"
print "Currently using Subtitle set ", subc
time.sleep(dur)

rawDescription: How do you get the subtitle index number in gstreamer-1.0

description: Get the subtitle index number in gstreamer-1.0


code:
import numpy as np
A = np.matrix('1 1; 1 0')
B = np.matrix('1 0; 1 0')
cor = np.corrcoef(A.reshape(-1), B.reshape(-1))[0][1]
print cor

rawDescription: Correlation between two binary images

description: Correlation between two binary images


code:
data = "Hello world, my name dosen't matter!"

data = data.split(' ')
for i in data:
    i.encode()
data = ' '.join(data)
print data

rawDescription: Do not encrpyt spaces

description: Do not encrpyt spaces


code:
try:
    s = pxssh.pxssh(timeout=60, maxread=2000000)
    s.force_password = True
    hostname = raw_imput('hostname: ')
    username = raw_input('password: ')
    password = getpass.getpass('password: ')
    s.PROMPT= 'SSH> '
    s.login (hostname, username, password, auto_prompt_reset=False)
    s.prompt()
    s.sendline('foo bar')
    s.prompt()
    data = s.before
    print data
    s.logout()
except pxssh.ExceptionPxssh, e:
    print "pxssh failed on login."
    print str(e)

rawDescription: Can't seem to get pexpect to print data from command


code:
# for each pair of start and end time that are not null
for start, end in zip(df[df['start_time'].notnull() & df['end_time'].notnull()]['start_time'],df[df['start_time'].notnull() & df['end_time'].notnull()]['end_time']) :

   start_time = pd.to_datetime(start, format ='%H:%M')
   end_time = pd.to_datetime(end, format ='%H:%M')
   #create a list of 24 indexes and initialize them to 0
   activity = [0]*24
   #round start and end time to the nearest hour
   i = (start_time + dt.timedelta(minutes=((start_time.minute // 60 + (1 if start_time.minute>30 else 0) ) * 60) - start_time.minute)).hour
   rounded_end_time = (end_time + dt.timedelta(minutes=((end_time.minute // 60 + (1 if end_time.minute>30 else 0) ) * 60) - end_time.minute)).hour
   #calculate the number of hours of activity (which is also the number of buckets to be filled)
   duration = (pd.to_datetime(rounded_end_time , format ='%H') - pd.to_datetime(i, format ='%H')).seconds//3600
   #initialize a count to count the number of buckets we fill
   count = 0
   while duration > count:
      activity[i] = 1
      count = count +1
      #set the index of the bucket to be filled to the next indes, unless it goes beyond the last bucket, in which case continue from the first bucket
      i = (i+1 if i+1 < 24 else 0)
   print activity

rawDescription: Filling in hourly buckets of activity time - Python


code:
def padArgsWithTrue(func, *args):
    passed_args = list(args)
    num_args = len(inspect.getargspec(func).args)
    passed_args += [True] * (num_args - len(args))
    return func(*passed_args)

print padArgsWithTrue(lambda x,y,z,w: (x*y, z, w), 5, 6)

rawDescription: optional arguments when calling a function without modifying function definition

description: Optional arguments when calling a function without modifying function definition


code:
import requests

print requests.post("https://api.mailgun.net/v2/samples.mailgun.org/messages",
                    auth=("api", "key-3ax6xnjp29jd6fds4gc373sgvjxteol0"),
                    files=[("attachment", open("files/test.jpg")),
                           ("attachment", open("files/test.txt"))],
                    data={"from": "Excited User <me@samples.mailgun.org>",
                          "to": "foo@example.com",
                          "cc": "baz@example.com",
                          "bcc": "bar@example.com",
                          "subject": "Hello",
                          "text": "Testing some Mailgun awesomness!",
                          "html": "<html>HTML version of the body</html>"})

rawDescription: Send multiple file attachments with Mailgun in Python


code:
1>2# : ^
'''
@echo off
echo normal
echo batch code
echo Switch to python
python "%~f0"
exit /b
rem ^
'''
print "This is Python code"

rawDescription: How to embed python code in batch script

description: Embed python code in batch script


code:
gen = (x for x in xyz if x not in a)

for x in gen:
    print x

rawDescription: Pythonic way to combine FOR loop and IF statement


code:
import sys

if len(sys.argv) != 3:
    print 'parameters: list1 list2'
    sys.exit(1)

list1 = []
for line in file(sys.argv[1]):
    fields = line.split()
    f1 = fields[0]
    f2, f3 = fields[1].split('-')
    list1.append((f1, int(f2), int(f3), [], ))

for line in file(sys.argv[2]):
    value = int(line)
    for name, lb, ub, values in list1:
        if value >= lb and value <= ub:
            values.append(str(value))

for name, lb, ub, values in list1:
    if values: vals = ','.join(values)
    else: vals = ''
    print '%s %d-%d %s' % (name, lb, ub, vals, )

rawDescription: How to combine two list that contain intervals

description: Combine two list that contain intervals


code:
x = 9
def changex():
    global x
    x = 10
    return x
changex()
print x

rawDescription: Saving an assignment made within a function


code:
if not any((foo == '1', bar == '2', baz == '3')):
    print 'Invalid'

rawDescription: Combining if with not and or

description: Combining if with not and or


code:
def print_column_labels():
    return "   a b c d e f g h" + "\n"

def print_separator_row(num_columns):
    return "   " +"+-" * num_columns+"+" + "\n"

def print_squares_white(row_number, num_columns):
    if row_number<=9:
        return " "+str(row_number) +" "+"| |#" * (num_columns / 2)+"| "+str(row_number) + "\n"
    else:
        return " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"

def print_squares_black(row_number, num_columns):
    if row_number<=9:
        return " "+ str(row_number ) +" "+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"
    else:
        return " "+ str(row_number ) +""+"|#| " * (num_columns / 2)+"| "+str(row_number) + "\n"

def print_chessboard(row_num,num_columns):
    result = ""
    result = result + print_column_labels()
    result = result + print_separator_row(num_columns)
    if row_num%2==0:
        while row_num>=1:
            result = result + print_squares_white(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            result = result + print_squares_black(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            if row_num==0:
                result = result + print_column_labels()
    else:
        while row_num>=2:
            result = result + print_squares_white(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            result = result + print_squares_black(row_num,num_columns)
            row_num-=1
            result = result + print_separator_row(num_columns)
            if row_num==1:
                result = result + print_squares_white(row_num,num_columns)
                result = result + print_separator_row(num_columns)
                result = result + print_column_labels()

    return result

chessboard = print_chessboard(8,8)

print chessboard

rawDescription: Getting a function to return rather than print


code:
def myrange(n):
    print "myrange(%s)"%n
    return range(n)

a = lambda x: (i for i in myrange(x))
def b(x):
    for i in myrange(x):
        yield i

a(100)
b(100)
next(_)   # <-- first iteration of b(100)

rawDescription: Why is this lambda function lazier than the for loop version?


code:
import subprocess
import time

p = subprocess.Popen("yes", stdout=subprocess.PIPE)
result = ""

start_time = time.time()

while (p.poll() is None):
  result += p.stdout.read(8192)
  time.sleep(1)
  if (time.time() - start_time) > 5:
    print "Timeout"
    break

print result

rawDescription: Python: subprocess.popen()

description: Python: subprocess.popen()


code:
#!/usr/bin/env python3
from gi.repository import Gdk
from gi.repository import GdkX11

Gdk.Window.process_all_updates()
xlib_window = 0x2a00005 # from xwininfo command
gdk_display = GdkX11.X11Display.get_default()
gdk_window = GdkX11.X11Window.foreign_new_for_display(gdk_display, xlib_window)
print gdk_window.get_geometry()

rawDescription: How to get Gdk window from xid?

description: Get Gdk window from xid


code:
conn = psycopg2.connect(DSN)
curs = conn.cursor()
curs.execute("CREATE TABLE test_copy (fld1 text, fld2 text, fld3 int4)")

# anything can be used as a file if it has .read() and .readline() methods
data = StringIO.StringIO()
data.write('\n'.join(['Tom\tJenkins\t37',
                  'Madonna\t\N\t45',
                  'Federico\tDi Gregorio\t\N']))
data.seek(0)

curs.copy_from(data, 'test_copy')

rawDescription: Recreating Postgres COPY directly in Python?


code:
my_str = "/projects/myproject/"
matches = re.findall("/(.+?)/(.+)/",my_str)
print matches  #prints ['projects','myproject']

rawDescription: Regex Python: Get Page and Subpage from URL


code:
def alert_read_text(df, alert_status=None):
    if (alert_status is None):
        print 'Warning: A column name with the alerts must be specified'
    copy = df.copy()
    alert_read_criteria = copy[alert_status] >= 1
    copy[alert_status].loc[alert_read_criteria] = 1
    alert_status_dict = {0 : 'Not Read',
                         1 : 'Read'}
    copy[alert_status] = copy[alert_status].map(alert_status_dict)
    return copy[alert_status]

rawDescription: Return output of function that takes pandas dataframe as a parameter


rawDescription: python how to convert from string.template object to string

description: Python how to convert from string.template object to string


code:
def p1(a=0):
    while True:
        yield a
        a = a + 1

def p2(b=0):
    while True:
        yield b
        b = b + 1

def p3(a, b):
    c = a + b
    print c

a_gen = p1()
b_gen = p2(3)
for _ in range(5):
    a = next(a_gen)
    b = next(b_gen)
    p3(a, b)

rawDescription: How to combine multiple while True function processes?

description: Combine multiple while True function processes


code:
import inspect

class DynamicVars(object):
    def __init__(self):
        object.__setattr__(self, "variables", {})

    def normalize(self, stackframe):
        return [hash(tpl[0]) for tpl in stackframe[1:]]

    def __setattr__(self, attr, value):
        stack = self.normalize(inspect.stack())
        d = {"value": value, "stack": stack}
        if not attr in self.variables:
            self.variables[attr] = []
            self.variables[attr].append(d)
        else:
            our_value = self.variables[attr]
            if our_value[-1]["stack"] == stack:
                our_value[-1]["value"] = value
            elif len(stack) <= len(our_value):
                while our_value and stack !=  our_value["stack"]:
                    our_value.pop()
                our_value.append(d)
            else: #len(stack) > len(our_value):
                our_value.append(d)
    def __getattr__(self, attr):
        if not attr in self.variables:
            raise AttributeError
        stack = self.normalize(inspect.stack())
        while self.variables[attr]:
            our_stack = self.variables[attr][-1]["stack"]
            if our_stack == stack[-len(our_stack):]:
                break
            self.variables[attr].pop()
        else:
            raise AttributeError
        return self.variables[attr][-1]["value"]

# for testing:
def c():
    D = DynamicVars()
    D.c = "old"
    print D.c
    def a():
        print D.c
    a()
    def b():
        D.c = "new"
        a()
    b()
    a()
    def c():
        D.c = "newest"
        a()
        b()
        a()
    c()
    a()

c()

rawDescription: How to create dynamical scoped variables in Python?

description: Create dynamical scoped variables in Python


code:
#!/usr/bin/python

import re, urllib, urllib2

class Spreadsheet(object):
    def __init__(self, key):
        super(Spreadsheet, self).__init__()
        self.key = key

class Client(object):
    def __init__(self, email, password):
        super(Client, self).__init__()
        self.email = email
        self.password = password

    def _get_auth_token(self, email, password, source, service):
        url = "https://www.google.com/accounts/ClientLogin"
        params = {
            "Email": email, "Passwd": password,
            "service": service,
            "accountType": "HOSTED_OR_GOOGLE",
            "source": source
        }
        req = urllib2.Request(url, urllib.urlencode(params))
        return re.findall(r"Auth=(.*)", urllib2.urlopen(req).read())[0]

    def get_auth_token(self):
        source = type(self).__name__
        return self._get_auth_token(self.email, self.password, source, service="wise")

    def download(self, spreadsheet, gid=0, format="csv"):
        url_format = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=%s&exportFormat=%s&gid=%i"
        headers = {
            "Authorization": "GoogleLogin auth=" + self.get_auth_token(),
            "GData-Version": "3.0"
        }
        req = urllib2.Request(url_format % (spreadsheet.key, format, gid), headers=headers)
        return urllib2.urlopen(req)

if __name__ == "__main__":
    import getpass
    import csv

    email = "" # (your email here)
    password = getpass.getpass()
    spreadsheet_id = "" # (spreadsheet id here)

    # Create client and spreadsheet objects
    gs = Client(email, password)
    ss = Spreadsheet(spreadsheet_id)

    # Request a file-like object containing the spreadsheet's contents
    csv_file = gs.download(ss)

    # Parse as CSV and print the rows
    for row in csv.reader(csv_file):
        print ", ".join(row)

rawDescription: Download a spreadsheet from Google Docs using Python


code:
from Queue import Empty
from multiprocessing import Process, Queue
import time
import hashlib

class FillQueue(object):
    def __init__(self, q):
        self.q = q

    def run(self):
        i = 0
        while i < 5:
            print 'putting'
            self.q.put('foo %d' % i )
            i+=1
            time.sleep(.5)
        self.q.put('|STOP|')

class ConsumeQueue(object):
    def __init__(self, q):
        self.q = q

    def run(self):
        while True:
            try:
                value =  self.q.get(False)
                print value
                if value == '|STOP|':
                    print 'done'
                    break;
            except Empty:
                print 'Nothing to process atm'
                time.sleep(.2)

if __name__ == '__main__':
    q = Queue()
    f = FillQueue(q)
    c = ConsumeQueue(q)

    p1 = Process(target=f.run)
    p1.start()

    p2 = Process(target=c.run)
    p2.start()

    p1.join()
    p2.join()

rawDescription: Python - start two processes to run indefinitely


code:
import os, errno

def mkdir(path, overwrite=False):
    try:
        os.makedirs(path)
    except OSError as exc: # Python >2.5
        if exc.errno == errno.EEXIST:
            if not overwrite:
                print "path '%s' already exists" % path   # overwrite == False and we've hit a directory that exists
                return
        else: raise
    # data making code...

rawDescription: A more elegant way to control overwriting with try-except-else in python? or Can I do better than C-style code?


code:
import HTMLParser
parser = HTMLParser.HTMLParser()
parser.unescape('é')
print parser.unescape('é')

rawDescription: Python, XML, &#233 type encodings

description: Python, XML, &#233 type encodings


code:
for key in sorted(my_dict, key = len):
    print my_dict[key]
1
2
3

rawDescription: Iterate through dict from smallest to largest key


code:
x=[['these are some words'], ['these are some more words'], ['these are even more words'], ['these are the last words']]
print [i[0].split() for i in x]

rawDescription: How do I split strings within nested lists in Python?

description: Split strings within nested lists in Python


code:
"""
Simulation of a train network
"""
from SimPy.Simulation import *
from math import sqrt
from random import randint
from itertools import cycle

def timeTo(A, maxV, d):
    """
    Given a trapezoidal velocity envelope defined by
    A       constant acceleration, m/s/s
    maxV    maximumum velocity, m/s
    return time in seconds required to travel
    d       distance, m
    """
    tA = float(maxV)/A          # time to accelerate to maxV
    dA = A*tA*tA                # distance traveled during acceleration from 0 to maxV and back to 0
    if (d < dA):                # train never reaches full speed?
        return sqrt(4.0*d/A)        # time needed to accelerate to half-way point then decelerate to destination
    else:
        return 2*tA + (d-dA)/maxV   # time to accelerate to maxV plus travel at maxV plus decelerate to destination

class Train(Process):
    def __init__(self, name, sim, accel=1.0, maxV=50.0, passengers=0, maxPassengers=400):
        super(Train, self).__init__(name, sim)
        self.accel = accel
        self.maxV  = maxV
        self.p     = passengers
        self.maxP  = maxPassengers

    def roll(self, route):
        here = route.next()     # starting location
        for dest in route:
            # travel to next station
            print "{:.1f}s: {} leaving {} for {}".format(self.sim.now(), self.name, here, dest)
            yield hold, self, timeTo(self.accel, self.maxV, here.distanceTo[dest])
            # arrive at next station
            here = dest
            print "{:.1f}s: {} at {}".format(self.sim.now(), self.name, here)
            yield hold, self, here.arrive(self)

    def getOff(self, num):
        if self.p >= num:
            print "  {} passengers got off".format(num)
            self.p -= num
        else:
            num = self.p
            print "  train is empty - only {} passengers got off".format(num)
            self.p = 0

    def getOn(self, num):
        if (self.maxP is None) or (self.p + num <= self.maxP):
            print "  {} passengers got on".format(num)
            self.p += num
        else:
            num = self.maxp - self.p
            print "  train is full - only {} passengers got on".format(num)
            self.p = self.maxp

class TrackNode(object):
    def __init__(self, name, delay=5.0):
        self.name = name
        self.delay = delay
        self.distanceTo = {}
    def arrive(self, train):
        pass
    def __str__(self):
        return self.name

class Station(TrackNode):
    def arrive(self, train):
        train.getOff(randint(1,15))
        train.getOn(randint(1,15))
        return self.delay

class Switch(TrackNode):
    def arrive(self, train):
        print("  switching tracks")
        return self.delay

class SampleRailroad(Simulation):
    def run(self, maxTime=100.0):
        self.initialize()
        # create places
        x = Switch("switch x", 20.0)
        A = Station("Station A", 24.0)
        B = Station("Station B", 27.0)
        C = Station("Station C", 25.0)
        y = Switch("switch y", 18.0)
        # distances between places
        x.distanceTo[A] = 50.0
        A.distanceTo[B] = 5000.0
        B.distanceTo[C] = 2000.0
        C.distanceTo[y] = 80.0
        y.distanceTo[C] = 80.0
        C.distanceTo[B] = 2000.0
        B.distanceTo[A] = 5000.0
        A.distanceTo[x] = 50.0
        # set up first train
        sf = Train("Santa Fe 219", self)
        self.activate(sf, sf.roll(cycle([A,B,C,y,C,B,A,x])), at=0.0)
        # set up second train
        cn = Train("Canadian National 41", self, maxPassengers=200)
        self.activate(cn, cn.roll(cycle([C,B,A,x,A,B,C,y])), at=5.0)
        # start simulating!
        self.simulate(until=maxTime)

def main():
    rr = SampleRailroad()
    rr.run(800.0)

if __name__=="__main__":
    main()

rawDescription: Need help modeling a train system with SimPy


code:
def wait_for_input(callback=lambda x:x):
    callback(raw_input("Enter some Crap:"))

def mock_wait_for_input(callback=lambda x:x):
    while True:
        if random_time_condition_thing():
            callback("a") # mock hitting an A

def main_thread():
    def OnInput(what):
        print "GOT:",what
    th = threading.Thread(target=wait_for_input,args=[OnInput,])
    while True:
        if not th.is_alive():
            th = threading.Thread(target=wait_for_input,args=[OnInput,])
        print "Waiting..."
        time.sleep(1)

rawDescription: Python: Mocking keyboard interrupt from code


code:
def paths(grid, x, y, l):
    """Returns a list of positions that the required letter is at"""

    positions = [(x - 1, y - 1), (x - 1, y), (x - 1, y + 1), (x, y - 1), (x, y + 1), (x + 1, y - 1), (x + 1, y), (x + 1, y + 1)]
    return [p for p in positions if p in grid and grid[p] == l]

def matchWord(grid, pos, word):
    """Returns true if the word was found in the grid starting from pos"""
    if word == "" : return True
    pos_paths = paths(grid, pos[0], pos[1], word[0])
    if pos_paths == [] : return False

    return any(matchWord(grid, the_pos, word[1:]) for the_pos in pos_paths)

def wordInGrid(grid, word):
    """returns true if the word is in the grid"""
    return any(matchWord(grid, key, word[1:]) for (key, val) in dict.iteritems(grid) if val == word[0])

gr_dict = {(0, 1): 'T', (1, 2): 'A', (3, 2): 'A', (0, 0): 'W', (3, 3): 'W', (3, 0): 'A', (3, 1): 'B', (2, 1): 'R', (0, 2): 'S', (2, 0): 'S', (1, 3): 'H', (2, 3): 'S', (2, 2): 'T', (1, 0): 'A', (0, 3): 'V', (1, 1): 'X'}

print wordInGrid(gr_dict, "RATS")
print wordInGrid(gr_dict, "WASABATAS")

rawDescription: How to recursively check an answer in a boggle type game

description: Recursively check an answer in a boggle type game


code:
import pandas as pd

checklist = []
indexes_to_drop = []

interactions = pd.read_csv('original_interactions.txt', delimiter = '\t')

for index, row in interactions.iterrows():
    check_string = row['InteractorA'] + row['InteractorB']
    check_string_rev = row['InteractorB'] + row['InteractorA']
    if (check_string or check_string_rev) in checklist:
        indexes_to_drop.append(index)
    else:
        pass
    checklist.append(check_string)
    checklist.append(check_string_rev)

no_dups = interactions.drop(interactions.index[indexes_to_drop])

print no_dups.shape

no_dups.to_csv('no_duplicates.txt',sep='\t',index = False)

rawDescription: Pandas drop duplicates if reverse is present between two columns


code:
import random

def eachIndexSorted(a):  # ... without -5 and -10
  for value in sorted(set(a) - { -5, -10 }):
    indexes = [ i for i in range(len(a)) if a[i] == value ]
    random.shuffle(indexes)
    for i in indexes:
      yield i

def firstN(iterator, n):
  for i in range(n):
    yield iterator.next()

print list(firstN(eachIndexSorted(my_array), 3))

rawDescription: Get randomly the 3 minimum values of an repeated-values array in Python


code:
def MyObject():
  import sys
  if sys.platform == 'darwin':
    return MyDarwinObject()
  else:
    return MyDefaultObject()

my_object = MyObject()
print my_object.get_home_directory()

rawDescription: Write Python classes that have different behavior for Mac and Windows


code:
from lxml import etree
from lxml import objectify
raw = '''<root>
<foo>foo</foo>
<bar>bar</bar>
</root>'''

root = objectify.fromstring(raw)
print type(root)
print etree.tostring(root)

description: Print lxml.objectify.ObjectifiedElement


code:
def insert_top_to_bottom(test_list, v):
    return test_list[v : -1] + test_list[:v] + [test_list[-1]]

test_list = [1, 28, 3, 4, 27, 8, 7, 6, 5]
test_list = insert_top_to_bottom(test_list, 5)
print test_list

rawDescription: insert top to bottom of list in python

description: Insert top to bottom of list in python


code:
from subprocess import  Popen, PIPE

def svn_co(url):
    return run_program('svn', 'co', url)

def run_program(*args):
    popen_obj = Popen(args, stderr=PIPE)
    _, errors = popen_obj.communicate()
    if popen_obj.returncode:
        print errors

# This will get all the files in myproject's tagged version
# named tagname into the current directory
svn_co('http://svn.myserver.net/myproject/tags/tagname')

rawDescription: download a folder of files given the http url of the files directory with python

description: Download a folder of files given the http url of the files directory with python


code:
a = [1, 2]
b = a  # b & a point to same list object [1, 2]
b[0] = 9  # As list is mutable the original list is altered & since a is referring to the same list location, now a also changes
print a
print b

rawDescription: python pointer memory reallocation

description: Python pointer memory reallocation


code:
from xlrd import open_workbook
from xml.dom.minidom import parse

# Open XML document using minidom parser
DOMTree = parse("output.xml")
testsuites = DOMTree.documentElement
testCaseCollection = testsuites.getElementsByTagName("testcase")

OutputXLS = open_workbook('output.xls')

for testCase in testCaseCollection:
    sibNode = testCase.nextSibling.nextSibling
    if sibNode and sibNode.nodeName == 'failure':
        print testCase.getAttribute("name"), " --> ","FAIL"
    else:
        print testCase.getAttribute("name"), " --> ","PASS"

rawDescription: getElementsByTagName() in Python's xml.dom.minidom is not working

description: GetElementsByTagName() in Python's xml.dom.minidom is not working


code:
import re
print re.findall(r'(?=([a-zA-Z]{2}))', 'find bigrams here')

rawDescription: Finding letter bigrams in text using Python regex


code:
class PermissiveDict(dict):
    default = None

    def __getitem__(self, item):
        try:
            return dict.__getitem__(self, item)
        except KeyError:
            return self.default

def exec_with_default(code, default=None):
    ns = PermissiveDict()
    ns.default = default
    exec code in ns
    return ns

rawDescription: Python __setattr__ and __getattr__ for global scope?


code:
NestedLst = ['1234567', 'abc456789', ['cde678945']]
SearchLst = ['123', 'cde']

def get_list(a,b):
    for i in b:
        if type(i)==list:          #if this is a nested list, do the same function for the contents of that list, returning that list if it meets the conditions.
            return get_list(a,i)
        elif a in i:               #if element in list is not a nested list, just do normal check for containment using in
            return b
    return []

x = [get_list(i,NestedLst) for i in SearchLst]  #just calls the function for all search terms

for k in x:
    print k

[OUTPUT]
['1234567', 'abc456789', ['cde678945']]
['cde678945']

rawDescription: Search a nested list against items from alternative list, returning subsets that contain partial match


code:
import difflib

str1 = "black_red_yellow"
str2 = "blue_red_green"

difference = difflib.SequenceMatcher()

difference.set_seqs(str1, str2)

for match in difference.get_matching_blocks():
    print str1[match[0]:match[0] + match[2]]

rawDescription: how can I tell if two strings have a common character section?--Python

description: Tell if two strings have a common character section?--Python


code:
b = open ('file2.txt', 'r') # list of interest

db = open ('file.txt', 'r').readlines() # database file

for line1 in b:
    IPR= line1.split()[1]
    for line2 in db:
        IPR2= line2.split()[0]
        if IPR == IPR2:
            print line2.rstrip()

rawDescription: Python: extracting lines from a database


code:
import os
import shlex
from subprocess import Popen, PIPE
import subprocess

print "Content-type:text/html\r\n\r\n"
p = subprocess.Popen(shlex.split("java weka.filters.unsupervised.attribute.StringToWordVector -b -i data_new.arff -o data_new_std.arff -r tweetin.arff -s tweetin_std.arff"),
    stdout = subprocess.PIPE, stderr = subprocess.PIPE)
out, error = p.communicate()
return_code = subprocess.call(shlex.split("java weka.classifiers.functions.SMO -T tweetin_std.arff -t data_new_std.arff -p 2 -c first > result.txt"))
print out
print "DONE!"

rawDescription: Use bat file in CGI Python on localhost

description: Use bat file in CGI Python on localhost


code:
import dns
from dns import resolver
import itertools
import collections
import multiprocessing.pool

def worker(arg):
    """query dns for (hostname, qname) and return (qname, [rdata,...])"""
    try:
        url, qname = arg
        rdatalist = [rdata for rdata in resolver.query(url, qname)]
        return qname, rdatalist
    except dns.exception.DNSException, e:
        return qname, []

def resolve_dns(url_list):
    """Given a list of hosts, return dict that maps qname to
    returned rdata records.
    """
    response_dict = collections.defaultdict(list)
    # create pool for querys but cap max number of threads
    pool = multiprocessing.pool.ThreadPool(processes=min(len(url_list)*3, 60))
    # run for all combinations of hosts and qnames
    for qname, rdatalist in pool.imap(
            worker,
            itertools.product(url_list, ('CNAME', 'MX', 'NS')),
            chunksize=1):
        response_dict[qname].extend(rdatalist)
    pool.close()
    return response_dict

url_list = ['example.com', 'stackoverflow.com']
result = resolve_dns(url_list)
for qname, rdatalist in result.items():
    print qname
    for rdata in rdatalist:
        print '   ', rdata

rawDescription: Batch/Bulk DNS lookup in python?

description: Batch/Bulk DNS lookup in python


code:
for line in inputfile.readlines():
    newString += string.replace('1111', line.strip())

print newString

rawDescription: Replace in for loop

description: Replace in for loop


code:
import threading
from  Queue import Queue
import time

# threads advertise to this queue when they're waiting
wait_queue = Queue()
# threads get their task from this queue
task_queue = Queue()

def do_stuff():
    print "%s doing stuff" % str(threading.current_thread())
    time.sleep(5)
def queue_thread(sleep_time):

    # advertise current thread waiting
    time.sleep(sleep_time)
    wait_queue.put("waiting")

    # wait for permission to pass
    message = task_queue.get()

    print "%s got task: %s" % (threading.current_thread(), message)
    # unregister current thread waiting
    wait_queue.get()

    if message == "proceed":
        do_stuff()
        # kill size-1 threads waiting
        for _ in range(wait_queue.qsize() - 1):
            task_queue.put("die")
        # release last
        task_queue.put("proceed")

    if message == "die":
        print "%s died without doing stuff" % threading.current_thread()
        pass

t1 = threading.Thread(target=queue_thread, args=(1, ))
t2 = threading.Thread(target=queue_thread, args=(2, ))
t3 = threading.Thread(target=queue_thread, args=(3, ))
t4 = threading.Thread(target=queue_thread, args=(4, ))

# allow first thread to pass
task_queue.put("proceed")

t1.start()
t2.start()
t3.start()
t4.start()

rawDescription: Mutual exclusion thread locking, with dropping of queued functions upon mutex/lock release, in Python?


code:
import rpyc
conn = rpyc.classic.connect("hostname")  # assuming a classic server is running on 'hostname'

print conn.modules.sys.path
conn.modules.sys.path.append("lucy")
print conn.modules.sys.path[-1]

# a version of 'ls' that runs remotely
def remote_ls(path):
    ros = conn.modules.os
    for filename in ros.listdir(path):
        stats = ros.stat(ros.path.join(path, filename))
        print "%d\t%d\t%s" % (stats.st_size, stats.st_uid, filename)

remote_ls("/usr/bin")

# and exceptions...
try:
     f = conn.builtin.open("/non/existent/file/name")
except IOError:
     pass

rawDescription: Task Scheduling Across a Network?

description: Task Scheduling Across a Network


code:
board = [[0]*10 for i in range(10)] # Generate 10 lists filled with 10 0's each.

for x in board:
    print x # Print out each [0,0,...0,0] on a new line.

rawDescription: New line sign in list Python

description: New line sign in list Python


code:
import maya.cmds

def myRotateCallback():
    print 'do something'

maya.cmds.scriptJob( attributeChange=['pCube1.rotateX', myRotateCallback] )

rawDescription: In MAYA 2009, is it possible to capture the cube rotate event?


code:
file2_dict = {}
with open("file2.txt") as fo:
    file_contents = fo.read()
    for line in file_contents.split("\n"):
        if not line.strip():
            continue
        key, value = line.strip().split(" ", 1)
        if key not in file2_dict:
            file2_dict[key] = []
        file2_dict[key].append(int(value))

output_string = ""
with open("file1.txt") as fo:
    file_contents = fo.read()
    for line in file_contents.split("\n"):
        if not line.strip():
            continue
        name, id, min, max = line.strip().split(" ")
        for value in file2_dict.get(id, []):
            if int(min) < value < int(max):
                output_string += line.replace(id, "%s %d" % (id, value))
                output_string += "\n"

print output_string

rawDescription: Finding a value between two integers in python


code:
@staticmethod
def match(location):
    query = "SELECT id FROM rate WHERE {0} LIKE CONCAT(area.code, '%%')  ORDER BY LENGTH(area.code) DESC LIMIT 1".format(location)
    try:
        res = db.engine.execute(query).first()
        return None if not res else Area.query.get(res[0])
    except SQLAlchemyError as e:
        print "Unable to get location for for location code {0}, reason: {1}".format(location, str(e))
    return None

rawDescription: Flask-SQLAlchemy query where substring is in database against a whole string


code:
blacklist = ['a', 'b', 'c']
cleaned = []
for item in ['foo', 'bar', 'baz']:
    clean = True
    for exclude in blacklist:
        if item.find(exclude) != -1:
            clean = False
            break
    if clean:
        cleaned.append(item)
print cleaned # --> ['foo']

rawDescription: python, cleaning a list

description: Python, cleaning a list


code:
import zipfile

z = zipfile.ZipFile("1.docx")

#print list of valid attributes for ZipFile object
print dir(z)

#print all files in zip archive
all_files = z.namelist()
print all_files

#get all files in word/media/ directory
images = filter(lambda x: x.startswith('word/media/'), all_files)
print images

#open an image and save it
image1 = z.open('word/media/image1.jpeg').read()
f = open('image1.jpeg','wb')
f.write(image1)

#Extract file
z.extract('word/media/image1.jpeg', r'path_to_dir')

rawDescription: Finding image present docx file using python


code:
data = api.rate_limit_status()

print data['resources']['statuses']['/statuses/home_timeline']
print data['resources']['users']['/users/lookup']

rawDescription: Python 2.7 - Tweepy - How to get rate_limit_status()?


code:
try:
    print data[1]['i']['a']
except KeyError:
    print "Not found!"

rawDescription: Python safe dict navigation, The Right Way