code:
import codecs

def getisocodes_dict(data_path):
    # Provide a map from ISO code (both bibliographic and terminologic)
    # in ISO 639-2 to a dict with the two letter ISO 639-2 codes (alpha2)
    # English and french names
    #
    # "bibliographic" iso codes are derived from English word for the language
    # "terminologic" iso codes are derived from the pronunciation in the target
    # language (if different to the bibliographic code)

    D = {}
    f = codecs.open(data_path, 'rb', 'utf-8')
    for line in f:
        iD = {}
        iD['bibliographic'], iD['terminologic'], iD['alpha2'], \
            iD['english'], iD['french'] = line.strip().split('|')
        D[iD['bibliographic']] = iD

        if iD['terminologic']:
            D[iD['terminologic']] = iD

        if iD['alpha2']:
            D[iD['alpha2']] = iD

        for k in iD:
            # Assign `None` when columns not available from the data
            iD[k] = iD[k] or None
    f.close()
    return D

if __name__ == '__main__':
    D = getisocodes_dict('ISO-639-2_utf-8.txt')
    print D['eng']
    print D['fr']

    # Print my current locale
    import locale
    print D[locale.getdefaultlocale()[0].split('_')[0].lower()]

rawDescription: Get system language in ISO 639 (3-letter codes) in Python


code:
def iTunesRunning():
    import win32ui
    # may need FindWindow("iTunes", None) or FindWindow(None, "iTunes")
    # or something similar
    if FindWindow("iTunes", "iTunes"):
        print "Found an iTunes window"
        return True

rawDescription: Python check if a process is running or not


code:
def run_macro(fName, path=os.getcwd()):
    print(path)
    fName = os.path.join(path, fName)
    print(fName)
    xlApp = win32com.client.Dispatch("Excel.Application")
    fTest = xlApp.Workbooks.Open(fName)

    for i in fTest.VBProject.VBComponents:
        print i.Name

        num_lines = i.CodeModule.CountOfLines

        for j in range(1, num_lines+1):

            if 'Sub' in i.CodeModule.Lines(j, 1) and not 'End Sub' in i.CodeModule.Lines(j, 1):
                print i.CodeModule.Lines(j, 1)

    xlApp.Application.Quit()
    del xlApp

if __name__ == "__main__":
    run_macro("Testing1.xlsm")

rawDescription: how to list all Excel Macro Names using Python

description: List all Excel Macro Names using Python


code:
class ClassInit(object):
    def __init__(self, configFile):
        f = open(configFile)
        config = f.read()
        f.close()
        config_dic = { '__builtins__': None}
        exec 'a = 4' in config_dic
        for key, value in config_dic.iteritems():
            if key != '__builtins__':
                setattr(self, key, value)

rawDescription: How to initialize a class with data from a python file

description: Initialize a class with data from a python file


code:
import zmq
import signal, time
import threading as th
from contextlib import contextmanager
@contextmanager
def ZMQErrorOnAlarm():
    def handler(signum, frame):
        raise Exception('ALARM detected')
    signal.signal(signal.SIGALRM, handler)
    yield
ctx = zmq.Context()
sock = ctx.socket(zmq.ROUTER)
sock.bind('tcp://*:6000')
def shutdown():
    signal.alarm(5)
    print 'closing'
t = th.Thread(target=shutdown)
t.start()
with ZMQErrorOnAlarm():
    try:
        sock.send('x')
        sock.recv()
    except:
        print 'closed'

rawDescription: closing hanging zmq socket from another thread

description: Closing hanging zmq socket from another thread


code:
s = "a123b"
for i in s:
    print i, i.isalpha()

rawDescription: How to check if character in string is a word character ? Python

description: Check if character in string is a word character ? Python


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

# prepare some pretty plots
stuff_to_plot = []
for i in range(10):
    stuff_to_plot.append(np.random.rand(10))

fig = plt.figure()
ax = fig.add_subplot(111)

coord_index = 0
plot_index = 0
def onclick(event):
    # get the index variables at global scope
    global coord_index
    if coord_index != 6:
        print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
            event.button, event.x, event.y, event.xdata, event.ydata)
        coord_index += 1
    else:
        coord_index = 0
        global plot_index
        # if we have more to plot, clear the plot and plot something new
        if plot_index < len(stuff_to_plot):
            plt.cla()
            ax.plot(stuff_to_plot[plot_index])
            plt.draw()
            plot_index += 1

cid = fig.canvas.mpl_connect('button_press_event', onclick)

# plot something before fist click
ax.plot(stuff_to_plot[plot_index])
plot_index += 1
plt.show()

rawDescription: Continue in parent for loop

description: Continue in parent for loop


code:
import poplib

M = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server
try:
    M.user(raw_input("username: ")) #Get the username from the standar input
    M.pass_(raw_input("password: ")) #Get the password from the standar input
except:
    print "username or password incorrect"
else:
    print "Successful login"

rawDescription: Python Hotmail login

description: Python Hotmail login


code:
with tempfile.TemporaryFile(suffix=".png") as tmpfile:
    fig.savefig(tmpfile, format="png") # File position is at the end of the file.
    tmpfile.seek(0) # Rewind the file. (0: the beginning of the file)
    print b64encode(tmpfile.read())

rawDescription: Can't use read-write files with matplotlib's savefig()?


code:
class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.combo = QtGui.QComboBox(self)
        self.combo.installEventFilter(self)
        self.show()

    def eventFilter(self,target,event):
        if target == self.combo and event.type() == QtCore.QEvent.MouseButtonPress:
            print "Button press"
            self.fillComboBox()

        return False

    def fillComboBox(self):
        self.combo.addItem('foo')
        self.combo.addItem('bar')

rawDescription: Populate QComboBox on drop down or user click


code:
import csv
with open('test.csv','r') as csvFile:
    reader=csv.reader(csvFile,delimiter=',')
    reader.next()   # if you don't want to print columns headers
    for row in reader:
         print row[2]
csvFile.close()

rawDescription: Reading specific Column Values from CSV and pass it to urllib as parameter in Python


code:
orig_list = ['"jason","hello1,hello2,hello3","somegroup2","bundle1","loc1"', '"ruby","hello","somegroup","bundle2","loc2"', '"sam","hello3,hello2","somegroup3,somegroup4","bundle2","loc3"']
id1 = id(orig_list)

orig_list[:] = [w for w in orig_list]
orig_list[:] = [g.replace('"', "'") for g in orig_list]
orig_list[:] = [h.split("',") for h in orig_list]
orig_list[:] = [[j.replace("'", '') for j in k] for k in orig_list]

id2 = id(orig_list)
print id1 == id2  # True
print orig_list  # [['jason', 'hello1,hello2,hello3', 'somegroup2', 'bundle1', 'loc1'], ['ruby', 'hello', 'somegroup', 'bundle2', 'loc2'], ['sam', 'hello3,hello2', 'somegroup3,somegroup4', 'bundle2', 'loc3']]

rawDescription: inplace removal of quotes from list of list

description: Inplace removal of quotes from list of list


code:
import StringIO
import sys

sys.stdout = StringIO.StringIO()
print "foo", "bar", "baz"
s = sys.stdout.getvalue()

rawDescription: Python file interface for strings

description: Python file interface for strings


code:
import resource

rsrc = resource.RLIMIT_DATA
soft, hard = resource.getrlimit(rsrc)
print 'Soft limit starts as  :', soft

resource.setrlimit(rsrc, (1024, hard)) #limit to one kilobyte

soft, hard = resource.getrlimit(rsrc)
print 'Soft limit changed to :', soft

rawDescription: How to limit Python heap size?

description: Limit Python heap size


code:
#!/usr/bin/python
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        print "Got it!"

if __name__ == "__main__":
    event_handler = MyHandler()
    observer = Observer()
    observer.schedule(event_handler, path='.', recursive=False)
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

rawDescription: python watchdog monitoring file for changes

description: Python watchdog monitoring file for changes


code:
import re

l = ['Job Id: 49361.tyrone-cluster', 'resources_used.cput = 14:32:14', 'resources_used.mem = 13955852kb', 'resources_used.vmem = 14199016kb', 'resources_used.walltime = 05:23:02', 'job_state = R', 'queue = qp32', 'Job Id: 49362.tyrone-cluster', 'job_state = Q', 'queue = batch', 'comment = Not Running: Queue not an execution queue.', 'Job Id: 49395.tyrone-cluster', 'resources_used.cput = 31:20:32', 'resources_used.mem = 19179712kb', 'resources_used.vmem = 158305072kb', 'resources_used.walltime = 01:57:34', 'job_state = R', 'queue = idqueue', 'Job Id: 49396.tyrone-cluster', 'resources_used.cput = 46:26:45', 'resources_used.mem = 5347092kb', 'resources_used.vmem = 7588024kb', 'resources_used.walltime = 01:44:50', 'job_state = R', 'queue = qp32', 'Job Id: 49408.tyrone-cluster', 'job_state = Q', 'queue = qp32']

job_elements = [i for (i,e) in enumerate(l) if re.match(r'Job Id: (\d+)', e)] + [len(l) + 1]

rows = []
for (s,e) in zip(job_elements[:-1], job_elements[1:]):
    row = []
    for line in l[s:e]:
        mat = re.match(r'Job Id: (\d+)', line)
        if mat:
            row.append(mat.group(1).strip())
            continue
        mat = re.match(r'.* = (.*)', line)
        if mat:
            row.append(mat.group(1).strip())
            continue

    rows.append(' '.join(row))

# Print output :
for r in rows:
    print r

# Or write to file:
with open('output.txt', 'w') as f:
    for r in rows:
        f.write(r)        # You could write these two lines as f.write(r + '\n')
        f.write('\n')     #   if you didn't care about creating a string unnecessarily

rawDescription: extract elements in a list

description: Extract elements in a list


code:
class Foo(object):

    def regular_method(self):
        print 'ok'

    def _soft_private(self):
        print 'ok'

    def __mangled_private(self):
        print 'ok'

f = Foo()
f.regular_method()
# prints ok
f._soft_private()
# print ok
f.__mangled_private()
# error! no such attribute. BUT:
f._Foo__mangled_private()
# prints ok

rawDescription: What's the difference between _b and b in this place


code:
# taking every 3rd element moves the start by 3
print list(islice(window(idlist, n=2),None,None,3))

rawDescription: Sliding Window with Variable Increment - Python


code:
from lxml import etree
f = etree.fromstring('<document></document>')
ele = etree.Element('br')   # the element to be inserted
f.insert(0, ele)
print etree.tostring(f, pretty_print=True)

rawDescription: python & lxml: adding empty element into an etree

description: Python & lxml: adding empty element into an etree


code:
import sys, select, os

i = 0
while True:
    os.system('cls' if os.name == 'nt' else 'clear')
    print "I'm doing stuff. Press Enter to stop me!"
    print i
    if sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
        line = raw_input()
        break
    i += 1

rawDescription: Exiting while loop by pressing enter without blocking. How can I improve this method?


code:
with open('rules_test1Fold0w4_sample00ll1.dat') as fileobj:
    lines = fileobj.readlines()
actualrules=''
for index in sortrule:
    print lines[index]

rawDescription: getting data from a file and putting it into an array

description: Getting data from a file and putting it into an array


code:
import SocketServer

class Server(SocketServer.ThreadingTCPServer):
    __slots__ = ("loaded")

class Handler(SocketServer.StreamRequestHandler):
    def handle(self):
        self.server.loaded = not self.server.loaded
        print self.server.loaded # Alternates value at each new request now.

server = Server(('localhost',5000),Handler)
server.loaded = False

while True:
    server.handle_request()

rawDescription: How do I modify variables in the SocketServer server instance from within a RequestHandler handler instance in Python?

description: Modify variables in the SocketServer server instance from within a RequestHandler handler instance in Python


code:
from pip.operations import freeze
x = freeze.freeze()
for p in x:
    print p

rawDescription: How to retrieve pip requirements (freeze) within Python?

description: Retrieve pip requirements (freeze) within Python


code:
import gtk

win  = gtk.Window ()
box  = gtk.EventBox ()
area = gtk.DrawingArea ()

def onclick (box, event):
   print event.x, event.y

box.connect ('button-press-event', onclick)

box.add (area)
win.add (box)
win.show_all ()
win.connect ('destroy', lambda *x: gtk.main_quit ())

gtk.main ()

rawDescription: How to get mouse position at mouse click - Python Gtk

description: Get mouse position at mouse click - Python Gtk


code:
import ctypes, os
try:
 is_admin = os.getuid() == 0
except AttributeError:
 is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0

print is_admin

rawDescription: Cross-platform way to check admin rights in a Python script under Windows?


code:
from mako.template import Template
from mako.lookup import TemplateLookup

base = """
  <html>
    <body>

      <div class="header">
        <%block name="header"/>
      </div>

      ${self.body()}

    </body>
  </html>
"""

index = """
  <%inherit file="base.html"/>

  <%block name="header">
    this is some header content
  </%block>

  this is the body content.
"""

lookup = TemplateLookup()
lookup.put_string("base.html", base)
lookup.put_string("index.html", index)

index_template = lookup.get_template("index.html")

print index_template.render()

rawDescription: How to use inheritance with mako using non-file based templates?

description: Use inheritance with mako using non-file based templates


code:
import wx
import wx.lib.mixins.listctrl as listmix

########################################################################
class SortableListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.ListCtrl.__init__(self, parent, size=(-1,100),
                             style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        listmix.ListCtrlAutoWidthMixin.__init__(self)

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

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

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

        data = {1: ["Dr appointment", "05/06/2014", ""],
                2: ["Meeting", "08/22/2013", "Board Room"]
                }

        self.list_ctrl = SortableListCtrl(panel)
        self.list_ctrl.InsertColumn(0, 'Subject')
        self.list_ctrl.InsertColumn(1, 'Due')
        self.list_ctrl.InsertColumn(2, 'Location', width=125)

        index = 0
        for d in data:
            print data[d][0]
            print data[d][1]
            self.list_ctrl.InsertStringItem(index, data[d][0]),
            self.list_ctrl.SetStringItem(index, 1, data[d][1])
            self.list_ctrl.SetStringItem(index, 2, data[d][2])
            index += 1

        btn = wx.Button(panel, label="Update")
        btn.Bind(wx.EVT_BUTTON, self.updateListCtrl)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.list_ctrl, 1, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.CENTER|wx.ALL, 5)
        panel.SetSizer(sizer)

    #----------------------------------------------------------------------
    def updateListCtrl(self, event):
        """
        """
        self.list_ctrl.DeleteAllItems()
        data = {1: ["Tennis", "09/06/2013", ""],
                2: ["Lunch", "08/15/2013", "Chick-fil-a"]
                }

        index = 0
        for d in data:
            print data[d][0]
            print data[d][1]
            self.list_ctrl.InsertStringItem(index, data[d][0]),
            self.list_ctrl.SetStringItem(index, 1, data[d][1])
            self.list_ctrl.SetStringItem(index, 2, data[d][2])
            index += 1

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

rawDescription: Sort items using listctrl in Python and wxpython


code:
import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.entry = tk.Entry(self)
        self.entry.pack(fill="x")

        self.entry.bind("<w><d>", self.onWD)
        # alternatively: self.entry.bind("wd", self.onWD)

    def onWD(self, event):
        print "boom!"

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

rawDescription: python Tkinter bind 2.7.9 w-d

description: Python Tkinter bind 2.7.9 w-d


code:
import urllib2
import re
data = urllib2.urlopen('http://marvel.wikia.com/api.php?action=query&prop=revisions&titles=All-New%20X-Men%20Vol%201%201&rvprop=content')
regex = re.compile('(Writer1_1|Penciler1_1)')
for line in data.read().split('|'):
    if regex.search(line):
        #assume everything after = is the full name
        print ' '.join(line.split()[2:])

rawDescription: Extract template arguments in Python from MediaWiki's API wikitext


code:
from datetime import datetime, timedelta

t0 = datetime(1981, 1, 1)
seconds = 1000000
dt = t0 + timedelta(seconds=seconds)
print dt
# 1981-01-12 13:46:40

rawDescription: Seconds since date (not epoch!) to date

description: Seconds since date (not epoch!) to date


code:
import Tkinter as tk
root = tk.Tk()

def myfunction(event):
    print buttons[event.widget]

buttons = {}
for i in range(10):
    b = tk.Button(root, text='button' + str(i))
    buttons[b] = i # save button, index as key-value pair
    b.bind("<Button-1>", myfunction)
    b.place(x=10,y=(10+(25*i)))
root.mainloop()

rawDescription: Python Tkinter button callback

description: Python Tkinter button callback


rawDescription: how to convert an adjacency matrix into a list of links

description: Convert an adjacency matrix into a list of links


code:
import subprocess

# My bash is at /user/local/bin/bash, your mileage may vary.
command = 'ssh user@host /usr/local/bin/bash'
heredoc = ('<< EOF \n'
           'cd Downloads \n'
           'touch test.txt \n'
           'EOF')

command = command.split()
command.append(heredoc)
print command

try:
     p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except Exception as e:
     print e

rawDescription: ssh + here-document syntax with Python

description: Ssh + here-document syntax with Python


code:
s = u'если'
print repr(s)

rawDescription: Print string in a form of Unicode codes

description: Print string in a form of Unicode codes


code:
import gtk

# Even I am ashamed by this
# Set up a one-time signal handler to detect size changes
def _on_size_req(win, req):
    x, y, w, h = win.get_allocation()
    print x, y, w, h   # just to prove to you its working
    win.disconnect(win.connection_id)
    win.unmaximize()
    win.window.move_resize(x, y, w, h)

# Create the window, connect the signal, then maximise it
w = gtk.Window()
w.show_all()
w.connection_id = w.connect('size-request', _on_size_req)
# Maximizing will fire the signal handler just once,
# unmaximize, and then resize to the previously set size for maximization.
w.maximize()

# run this monstrosity
gtk.main()

rawDescription: Finding the workspace size (screen size less the taskbar) using GTK


code:
d = u's\xe1bado'
d
print d

rawDescription: Converting datetime.ctime() values to Unicode


code:
def setUp(self):
    self.client = Client()
    self.user = create_test_user()
    self.password = 'test_user'
    self.user.set_password(self.password)
    self.user.save()
    self.client.login(
        username=self.user.username,
        password=self.password)
    print 'AUTHENTICATED? ', self.user.is_authenticated() #PRINTS TRUE

rawDescription: Make my test_user pass is_authenticated

description: Make my test_user pass is_authenticated


code:
class MyEnum:
  VAL1, VAL2, VAL3 = range(3)
  @classmethod
  def tostring(cls, val):
    for k,v in vars(cls).iteritems():
        if v==val:
            return k

  @classmethod
  def fromstring(cls, str):
      return getattr(cls, str.upper(), None)

print MyEnum.fromstring('Val1')
print MyEnum.tostring(2)

rawDescription: Python Enum class (with tostring fromstring)


code:
flattened_data = (
    {
        'name': record['name'],
        'date': grade['date'],
        'grade': grade['grade'],
        'score': grade.get('score')
    }
    for record in cursor2
    for grade in record['grades']
)
result = pd.DataFrame(flattened_data)[['name', 'date', 'grade', 'score']]
print result.head(100)

rawDescription: JSON column is nested values

description: JSON column is nested values


code:
from nltk.tokenize import word_tokenize
example = ['Mary had a little lamb' ,
           'Jack went up the hill' ,
           'Jill followed suit' ,
           'i woke up suddenly' ,
           'it was a really bad dream...']
tokenized_sents = [word_tokenize(i) for i in example]
for i in tokenized_sents:
    print i

rawDescription: Tokenize words in a list of sentences Python


code:
L_0 = [[7], [3,8], [8,1,0], [2,7,4,4], [4,5,2,6,5]]
L_1 = [[11], [7,32], [14,14,14], [0,1,2,3], [5,99,1,2,7],
       [0,25,9,45, 54,1], [99,88,77,66,55,44,33]]

def create_paths(rows):
    new_paths = []
    paths = [[0]]
    for row in xrange(rows):
        for path in paths:
            new_paths.append(path+[path[-1]])
            new_paths.append(path+[path[-1]+1])
        paths = new_paths
        new_paths = []
    return paths

def max_cost(tree):
    costs = []
    paths = create_paths(len(tree)-1)
    for path in paths:
        costs.append(sum([tree[i][j] for i, j in enumerate(path)]))
    return max(costs)

print max_cost(L_0)
print max_cost(L_1)

#output:
30
270

rawDescription: Python max benefit

description: Python max benefit


code:
for g in t[0].groups.all():
    print g.title

rawDescription: Checking for group membership (Many to Many in Django)


code:
for row in c.execute('SELECT * from table if time = "14:00:00"').fetchall():
    print row

rawDescription: How to traverse a query result in python?

description: Traverse a query result in python


code:
xmax, ymax = a.max(axis=0)
print xmax, ymax

rawDescription: How to find max value in a numpy array column?

description: Find max value in a numpy array column


code:
import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 1234))
    sock.listen(100)
    while True:
        print 'listening...'
        csock, address = sock.accept()
        tick+=1
        print 'connection!'
        handshake(csock, tick)
        print 'handshaken'
        while True:
            interact(csock, tick)
            tick+=1

def send_data(client, str):
    #_write(request, '\x00' + message.encode('utf-8') + '\xff')
    str = '\x00' + str.encode('utf-8') + '\xff'
    return client.send(str)
def recv_data(client, count):
    data = client.recv(count)
    return data.decode('utf-8', 'ignore')

def handshake(client, tick):
    our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:     WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
    shake = recv_data(client, 255)
    print shake
    #We want to send this without any encoding
    client.send(our_handshake)

def interact(client, tick):
    data = recv_data(client, 255)
    print 'got:%s' %(data)
    send_data(client, "clock ! tick%d" % (tick))
    send_data(client, "out ! %s" %(data))

if __name__ == '__main__':
    start_server()

rawDescription: Python example of Joe's Erlang websocket example


code:
import re
fname = raw_input("Enter the file name: ")
numbers = []
with open(fname) as lines:
    for line in lines:
        numbers.extend(re.findall('[0-9]+', line))
print numbers
print sum(map(int, numbers))

rawDescription: Trying to extract numbers from a .txt document and sum it using regular expressions in python2.7


code: print open(__file__).read()

rawDescription: Print out all code in a Python script

description: Print out all code in a Python script


code:
print [t.string for t in div.findAll(match_only_non_divs)]
# [u'Navigate!', None, u'Main navigation', None, None, u'Home', None, u'About']

print [t.string for t in div.findAll(match_only_non_divs) if t.string]
# [u'Navigate!', u'Main navigation', u'Home', u'About']

rawDescription: how to obtain HTML-free contents for non-div tags

description: Obtain HTML-free contents for non-div tags


code:
f=open("file")
for line in f:
    if len(line)==81:
       n=f.next()
       line=line.rstrip()+n
    print line.rstrip()
f.close()

rawDescription: Remove linebreak at specific position in textfile


code:
with open('07362853300091_trade_turquoise_errNo_031.csv', 'rb') as file:
     for row in csv.reader(file):
         print "\n".join(row)

rawDescription: Printing unicode string works in python 3.2 but not in 2.7


code:
from threading import Thread
import Queue
import os
import time
import sys

# declare some global items
# queue that an author thread can write line items to a csv
write_q = Queue.Queue()

# queue filled with files to parse
read_q = Queue.Queue()

# queue filled with files that have size change during read. Can
# preload this queue to optimize however program should handle any
# file that changes during operation
moving_q = Queue.Queue()

# given csv labels
labels = ['date', 'message', 'action', 'details']

# global for writer thread so it knows when to close
files_to_parse = True

# parsing function for any number of threads
def file_parser():
    # Each parser thread will run until the read_q is empty
    while True:
        moving = False
        # Test for a file from the read queue or moving queue
        try:
            if not moving_q.empty():
                try:
                    f_path = moving_q.get(False)
                    moving = True
                # if the moving queue is empty after trying to read
                # might have been snatched by different thread. Ignore error
                except Queue.Empty:
                    pass
            else:
                # No items left in moving queue so grab non moving file
                f_path = read_q.get(False)
        # all files have been dealt with
        except Queue.Empty:
            print "Done Parsing"
            sys.exit()

        # Following will parse a file and test that the file is not being
        # modified during the read
        with open(f_path, 'r') as f:
            # csv reader setup
            reader = csv.DictReader(f, labels, delimiter=' ', restkey='rest')

            # initillized file size (when we started reading)
            pre = os.path.getsize(f_path)

            # store output items in a list so if file is updated during read
            # we can just ignore those items and read file later
            line_items = []

            # parse the file line by line
            for line in reader:
                # Check that file hasn't been updated
                post = os.path.getsize(f_path)
                if pre != post:
                    # if file has changed put the file back on the queue and clear the output lines
                    moving_q.put(f_path)
                    line_items = None
                    break
                # parse the line and add it to output list
                else:
                    if line.get('rest'):
                        line['details'] += ' %s' % (' '.join(line['rest']))
                        line_items.append(','.join([infile,line['date'], line['message'], line['action'], line['details']]) + '\n')

            # don't want to do reading and writing in same thread. Push
            # all line items onto the write thread for the author to deal with
            if line_items and moving:
                write_q.put(line_items)
                moving_q.task_done()
            elif line_items and not moving:
                write_q.put(line_items)
                read_q.task_done()

# author thread that will write items to a file as other threads complete
# tasks. Should help speed up IO bound processing
def file_author(out_file):
    with open(out_file,'w') as f:
        # parse files until all the parser threads are running
        while files_to_parse or not read_q.empty():
            # only one writer thread so write as items are put into thread
            if not read_q.empty():
                line_items = write_q.get(False)
                for line_item in line_items:
                    f.write(line_item)
                write_q.task_done()
            # sleep in the downtime so we dont overload PC
            else:
                time.sleep(.1)
    print "Done writting"

if __name__ == "__main__":
    # list of file names as you had before
    listing = []
    outfile = "MyNewCSVfile.csv"

    # You can optimize parsing by adding known "moving files" directly
    # to the moving_queue, however program should handle either way
    for infile in listing:
        _path2 = _path + infile
        write_q.put(_path2)

    # make a writer thread
    t = Thread(target = file_author, args = (outfile,))
    t.daemon = True
    t.start()

    # make some parse threads
    for i in range(10):
        t = Thread(target = file_parser)
        t.daemon = True
        t.start()

    # wait for parser threads to finish work
    read_q.join()
    moving_q.join()

    # close author
    files_to_parse = False
    time.sleep(.1)
    print "Complete"

rawDescription: Python File Parsing

description: Python File Parsing


code:
import string

sentence = 'Test this sentence'

if not any([symbol in sentence for symbol in string.punctuation]):
    # If no punctuations, continue ...

    # Ask word and get word ... , for example:
    word = 'this'

    position = sentence.find(word) # Returns -1 if word not in sentence.

    if position > -1:
        print position

rawDescription: Print and Find The Position Of A Word And Words In A List


code:
triv_vec = np.vectorize(triv, otypes=[np.int,np.int])
print triv_vec([1,2],[3,4])
(array([1, 2]), array([3, 4]))

rawDescription: Numpy vectorize and atomic vectors

description: Numpy vectorize and atomic vectors


code:
__author__ = 'rcope'
from Tkinter import Tk, Frame, BOTH, Menu, Label, SUNKEN, X, BOTTOM

class Application(Frame):
   def __init__(self, parent):
      Frame.__init__(self, parent, background = "white")
      parent.configure(bg = "black")
      self.pack(fill = BOTH, expand = True, padx = 20, pady = 20)

      self.parent = parent

      # Maximize window
      self.screenWidth = self.parent.winfo_screenwidth() - 5
      self.screenHeight = self.parent.winfo_screenheight() - 110
      self.parent.geometry('%dx%d+%d+%d' % (self.screenWidth, self.screenHeight, 0, 0))
      self.parent.resizable(0, 0)

      # Status bar
      self.statusBar = StatusBar(self.parent)
      self.statusBar.pack(side = BOTTOM, fill = X)

      # Menu bar
      menubar = Menu(self.parent)
      self.parent.config(menu = menubar)

      self.commandMenu = Menu(menubar, tearoff = 0)
      self.commandMenu.add_command(label = "Rename", command = self.onRename)
      menubar.add_cascade(label = "Command", menu = self.commandMenu)

      self.helpMenu = Menu(menubar, tearoff = 0)
      self.helpMenu.add_command(label = "About", command = self.onAbout)
      menubar.add_cascade(label = "Help", menu = self.helpMenu)
      self.helpMenu.bind("<<MenuSelect>>", self.statusBarUpdate)

   def onRename(self):
      pass
   def onAbout(self):
      pass

   def statusBarUpdate(self, event=None):
       print "Status Bar Update Called"
       if self.parent.call(event.widget, "index", "active") == 0:
           self.statusBar.set("About This Application")
       else:
           self.statusBar.set("")

class StatusBar(Frame):
   def __init__(self, master):
      Frame.__init__(self, master)
      self.label = Label(self, bd = 1, relief = SUNKEN, anchor = "w")
      self.label.pack(fill=X)
   def set(self, format0, *args):
      self.label.config(text = format0 % args)
      self.label.update_idletasks()
   def clear(self):
      self.label.config(text="")
      self.label.update_idletasks()

def main():
   root = Tk()
   Application(root)
   root.mainloop()

if __name__ == '__main__':
   main()

rawDescription: Python Tkinter: Display description in status bar


code:
import tornado.websocket
import tornado.autoreload
import tornado.web

class LobbyWS(tornado.websocket.WebSocketHandler):
    clients = []

    def open(self, *args):
        self.stream.set_nodelay(True)
        LobbyWS.clients.append(self)
        print 'opening %s' % self

    def on_pong(self, data):
        print 'got pong', data

    def on_message(self, message):
        print "Client %s received a message : %s" % (self, message)
        for client in LobbyWS.clients:
            client.write_message(message)

    def on_close(self):
        print "Client %s closed." % self

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("""

""")

application = tornado.web.Application([
    (r"/", MainHandler),
    (r"/lobby", LobbyWS),
    ])

if __name__ == "__main__":
    application.listen(8888)
    tornado.autoreload.start()
    tornado.ioloop.IOLoop.instance().start()

rawDescription: Tornado Websockets doesn't call on_message without ping


code:
import json
from pprint import pprint

file = 'data.json'

with open(file) as data_file:
    data = json.load(data_file)

for host in data:
    print host["host"]

    for version, proto in host["protocols"].iteritems():
        print version
        print proto
        print proto["data"]

rawDescription: JSON iterating an array

description: JSON iterating an array


code:
import os
import time

from pyfiglet import Figlet

f = Figlet(font='slant')
word = 'HELLO'
curr_word = ''
for char in word:
    os.system('reset') #assuming the platform is linux, clears the screen
    curr_word += char;
    print f.renderText(curr_word)
    time.sleep(1)

rawDescription: Print BIG Words on Console - using python, bit by bit


code:
def getSum(piece):
    if len(piece)==0:
        return 0
    else:
        return piece[0] + getSum(piece[1:])
print getSum([1, 3, 4, 2, 5])

rawDescription: Python, get sum of a list of numbers with recursion


code:
import re

ciph = "hello there"
result = ""
for c in ciph:
    result += str((hex(ord(c))))[2:]

# reverse it
orig = ""
for code in re.findall('..', result):
    orig += chr(int(code, 16))
print orig  # "hello there"

rawDescription: Decrypt String in Python That Was Encrypted in Java


code:
ids = title.select('//meta/@content').extract()
print [id for id in ids if id.isdigit()]

rawDescription: Scrapy: Extract value from meta tag

description: Scrapy: Extract value from meta tag


code:
import heapq
import itertools
import operator

def increasing(fn, left, right):
    """
    Given two never decreasing iterators produce another iterator
    resulting from passing the value from left and right to fn.
    This iterator should also be never decreasing.
    """
    # Imagine an infinite 2D-grid.
    # Each column corresponds to an entry from right
    # Each row corresponds to an entry from left
    # Each cell correspond to apply fn to those two values

    # If the number of columns were finite, then we could easily solve
    # this problem by keeping track of our current position in each column
    # in each iteration, we'd take the smallest value report it, and then
    # move down in that column. This works because the values must increase
    # as we move down the column. That means the current set of values
    # under consideration must include the lowest value not yet reported

    # To extend this to infinite columns, at any point we always track a finite
    # number of columns. The last column current tracked is always in the top row
    # if it moves down from the top row, we add a new column which starts at the top row
    # because the values are increasing as we move to the right, we know that
    # this last column is always lower then any columns that come after it

    # Due to infinities, we need to keep track of all
    # items we've ever seen. So we put them in this list
    # The list contains the first part of the incoming iterators that
    # we have explored
    left_items = [next(left)]
    right_items = [next(right)]

    # we use a heap data structure, it allows us to efficiently
    # find the lowest of all value under consideration
    heap = []

    def add_value(left_index, right_index):
        """
        Add the value result from combining the indexed attributes
        from the two iterators. Assumes that the values have already
        been copied into the lists
        """
        value = fn( left_items[left_index], right_items[right_index] )
        # the value on the heap has the index and value.
        # since the value is first, low values will be "first" on the heap
        heapq.heappush( heap, (value, left_index, right_index) )

    # we know that every other value must be larger then
    # this one.
    add_value(0,0)

    # I assume the incoming iterators are infinite
    while True:
        # fetch the lowest of all values under consideration
        value, left_index, right_index = heapq.heappop(heap)

        # produce it
        yield value

        # add moving down the column
        if left_index + 1 == len(left_items):
            left_items.append(next(left))

        add_value(left_index+1, right_index)

        # if this was the first row in this column, add another column
        if left_index == 0:
            right_items.append( next(right) )
            add_value(0, right_index+1)

def fib():
    a = 1
    b = 1
    while True:
        yield a
        a,b = b,a+b

r = increasing(operator.add, fib(), itertools.count() )
for x in range(100):
    print next(r)

rawDescription: "sorted 1-d iterator" based on "2-d iterator" (Cartesian product of iterators)


code:
stack = [(my_list, -1)]
while stack:
    item, level = stack.pop()

    if isinstance(item, list):
        for i in reversed(item):
            stack.append((i, level+1))
    else:
        print "\t" * level, item

rawDescription: Non-recursive means of printing a list in Python


code:
for line in file:
    if "AP" in line:
        i = line.find('AP')
        splitLine = line[i+2:].split('|')
        val = splitLine[0]
        print val,
    elif "Num of clients" in line:
        splitLine = line.split(':')
        num = splitLine[1]
        print '- ' + num

rawDescription: Parsing text in python issue

description: Parsing text in python issue


code:
argparser = argparse.ArgumentParser()
argparser.add_argument("-H", action="store_true")
subparser = argparser.add_subparsers(dest='sp')
cmd = subparser.add_parser("cmd")
cmd.add_argument("-H", dest='cmd_H')

print argparser.parse_args()

rawDescription: Nested ArgumentParser

description: Nested ArgumentParser


code:
# define expressions to match leading and trailing
# html tags, and just suppress the leading or trailing whitespace
opener = White().suppress() + Literal("<html>")
closer = Literal("</html>") + White().suppress()

# define a single expression to match either opener
# or closer - have to add leaveWhitespace() call so that
# we catch the leading whitespace in opener
either = opener|closer
either.leaveWhitespace()

print either.transformString(insource)

# alternative, if you know what the tag will look like:
# match 'info=<some double quoted string>', and use a parse
# action to extract the contents within the quoted string,
# call strip() to remove leading and trailing whitespace,
# and then restore the original '"' characters (which are
# auto-stripped by the QuotedString class by default)
infovalue = QuotedString('"', multiline=True)
infovalue.setParseAction(lambda t: '"' + t[0].strip() + '"')
infoattr = "info=" + infovalue

print infoattr.transformString(insource)

rawDescription: How to construct a "leading" equivalent to FollowedBy subclass in PyParsing

description: Construct a "leading" equivalent to FollowedBy subclass in PyParsing


code:
class BigClass(Object):

  def Logger(self):
     print 'foo has been called'

  @log(Logger)
  def Foo(self):
     print 'calling foo'

a = BigClass()
a.foo()

rawDescription: creating method signals to automatically fire up on an event in python

description: Creating method signals to automatically fire up on an event in python


code:
import os
import fnmatch

for root, _, filenames in os.walk('/tmp'):
    print root, filenames
    for filename in fnmatch.filter(filenames, '*.py'):
        filename = os.path.join(root, filename)

        # `filename` now unambiguously refers to a file that
        # exists. Open it, delete it, xlrd.open it, whatever.
        # For example:
        if os.access(filename, os.R_OK):
            print "%s can be accessed"  % filename
        else:
            print "%s cannot be accessed"  % filename

rawDescription: Python os.walk/fnmatch.filter does not find file when in file's current directory


code:
def print_style(widget):
    style = widget.get_style()
    for i in range(5):
        print i, gdkColorToRgb(style.bg[i])
gdkColorToRgb = lambda gc: (gc.red//257, gc.green//257, gc.blue//257)
widget = gtk.HBox() ## for example
widget.connect('realize', print_style)

rawDescription: Get colors of current gtk style

description: Get colors of current gtk style


code:
data = [
    ["a_key_1", "a_key_2", "a_value_1", "a_value_2"],
    ["b_key_1", "b_key_2", "b_value_1", "b_value_2"],
    ["a_key_1", "a_key_2", "a_value_3", "a_value_4"],
    ["a_key_1", "a_key_3", "a_value_5", "a_value_6"],
]

from itertools import groupby
keyfunc = lambda row: (row[0], row[1])
print [
    list(key) + [list(zipped) for zipped in zip(*group)[2:]]
    for key, group
    in groupby(sorted(data, key=keyfunc), keyfunc)
]

# => [['a_key_1', 'a_key_2', ['a_value_1', 'a_value_3'], ['a_value_2', 'a_value_4']],
#     ['a_key_1', 'a_key_3', ['a_value_5'], ['a_value_6']],
#     ['b_key_1', 'b_key_2', ['b_value_1'], ['b_value_2']]]

rawDescription: python: flatten list while preserving nested structure for certain indexes

description: Python: flatten list while preserving nested structure for certain indexes


code:
data_form = {}

for fieldname, value in request.form.items():
    data_form[fieldname] = value

data_form.pop("csrf_token", None)
data_form.pop("type", None)

print data_form

rawDescription: Access dynamic input name in ImmutableMultiDict


code:
for i in range(0,len(dl)):
  for j in range(0,3):
    a_elem = i*3 + j
    print dl[i] + " " + a[a_elem] + " " + str(dr[a_elem])

rawDescription: issues with a triple loop regrading setting the ranges for the loop

description: Issues with a triple loop regrading setting the ranges for the loop


code:
class MigrationUi(QtGui.QWidget):

    ### Some stuff

    def editSelected(self):
        selected_item = self.ui.treeWidget_migrateAnmg.currentItem()

        if selected_item:
            inputWin = PublishInfoUI(selected_item)
            ############################
            # Here is the core modifications
            if inputWin.exec_(): #Triggered if we press "OK"
                #Solution 1:
                inputWin.getValues()
                #End solution 1

                #Solution 2:
                returnedAsset = inputWin.getValues()
                print "Returned asset: "
                print returnedAsset.text(0)
                print returnedAsset.text(1)
                print returnedAsset.text(2)
                print returnedAsset.text(3)
                print returnedAsset.text(4)
                print returnedAsset.text(5)
                print returnedAsset.text(6)
                print returnedAsset.text(7)
                print returnedAsset.text(8)
                selected_item.setText(6, returnedAsset.text(6) )
                selected_item.setText(7, returnedAsset.text(7) )
                selected_item.setText(8, returnedAsset.text(8) )
                #End solution 2
            else:
                print "Canceled/closed operation"
            ############################
        else:
            cmds.warning("Please select an item in ANMG field")

    def slotCancel(self):
        self.close()

class PublishInfoUI(QtGui.QDialog):

    def __init__(self, asset, parent = None, modal = False):
        QtGui.QWidget.__init__(self, parent, modal = modal)

        self.ui = publishInfoUI_test.Ui_PublishInfo()
        self.ui.setupUi(self)
        self.assets = asset

        self.fill_details()

    def fill_details(self):
        self.ui.lineEdit_rigUsed.setText(self.assets.text(0))
        self.ui.lineEdit_anmLocation.setText(self.assets.text(5))
        self.ui.textEdit_comment.setText(self.assets.text(7))

    def getValues(self):
        #Solution 1:
        #Do the modification here
        self.assets.setText(6, self.ui.lineEdit_description.text() )
        self.assets.setText(7, self.ui.textEdit_comment.toPlainText() )
        self.assets.setText(8, self.ui.textEdit_Vcomment.toPlainText() )
        #End solution 1

        #Solution2:
        #Return a new asset and do the modification in MigrationUi class
        assetToReturn = QtGui.QTreeWidgetItem()
        assetToReturn.setText(6, self.ui.lineEdit_description.text() ) #Feel free to add more infos if necessary
        assetToReturn.setText(7, self.ui.textEdit_comment.toPlainText() )
        assetToReturn.setText(8, self.ui.textEdit_Vcomment.toPlainText() )
        return assetToReturn
        #End solution 2

rawDescription: Map values from an UI into QTreeWidget Column


code:
import datetime

class Field(object):
    def __init__(self):
        self.val = datetime.date.today()

    def __get__(self, instance=None, type=None):
        if instance is not None:
            return self.val
        return self

class Container(object):
    value_1 = Field()
    value_2 = Field()

c = Container()
print c.value_1.isoformat() # 2016-03-03
print c.value_2.isoformat() # 2016-03-03

rawDescription: How can I represent an instance as a typed value

description: Represent an instance as a typed value


code:
import numpy as np
a = [[[ ['green', 'blue', 'red' ] ]]]
print np.squeeze(a)

rawDescription: Simplest way to return an array that is nested in multiple arrays


code:
import xml.etree.ElementTree as et

xml1="""<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>ok</status>
<response>ok</response>
<jobid>[integer]</jobid>
</content>
"""

xml2="""<?xml version="1.0" encoding="UTF-8"?>
<content>
<status>fail</status>
<error>missingdata</error>
</content>
"""

def get_elements(xml_input):
    ret = {}
    tree = et.fromstring(xml_input)

    for el in tree:
        ret[el.tag] = el.text

    return ret

print get_elements(xml1)
print get_elements(xml2)

rawDescription: Parse XML segment when elements may not always exist


code:
import httplib2
from BeautifulSoup import BeautifulSoup, SoupStrainer

http = httplib2.Http()
status, response = http.request('http://www.nytimes.com')

for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')):
    if link.has_key('href'):
        print link['href']

rawDescription: Using scrapy to get links in Python?

description: Using scrapy to get links in Python


code:
if re.match(r'\S{7}\d$', text):
    print 'all conditions match'

rawDescription: Checking string for conditions

description: Checking string for conditions


code:
from itertools import groupby

L = [
    { "key" : { "subkey" : "red", "value" : 1 }  },
    { "key" : { "subkey" : "red", "value" : 2 }  },
    { "key" : { "subkey" : "blue", "value" : 1 }  },
    { "key" : { "subkey" : "yellow", "value" : 3 }  },
    { "key" : { "subkey" : "blue", "value" : 5 }  },
    { "key" : { "subkey" : "blue", "value" : 8 }  },
    { "key" : { "subkey" : "red", "value" : 2 }  },
    { "key" : { "subkey" : "red", "value" : 3 }  },
    { "key" : { "subkey" : "red", "value" : 6 }  },
]

def some_event(*args):
    print args, sum(args)

for k, g in groupby(L, key=lambda x:x["key"]["subkey"]):
    g = list(g)
    if len(g) > 1:
        some_event(*(i["key"]["value"] for i in g))

rawDescription: iterate through dicts of lists + add up values of identical keys

description: Iterate through dicts of lists + add up values of identical keys


code:
line_list=[]
for line in fr:
    cols = line.split('|')
    key = cols[0]
    cols[0]=d[key]
    line_list.append('|'.join(cols))

print line_list

rawDescription: Python dictionary and loop confusion during lookup of dictionary


code:
import re
import os
import glob

source_dir=os.getcwd()
target_dir="clean"
source_files = [fname for fname in glob.glob(os.path.join(source_dir,"*.seq"))]

# check if target directory exists... if not, create it.
if not os.path.exists(target_dir):
    os.makedirs(target_dir)

for source_file in source_files:
   target_file = os.path.join(target_dir,os.path.basename(source_file)+".clean")
   with open(source_file,'r') as sfile:
      with open(target_file,'w') as tfile:
         lines = sfile.readlines()
         # do the replacement in the second line.
         # (remember that arrays are zero indexed)
         lines[1]=re.sub("K|Y|W|M|R|S",'N',lines[1])
         tfile.writelines(lines)

print "DONE"

rawDescription: Replace string in a specific line using python


code:
from django.db.signals import post_save
from django.dispatch import receiver
from django.db.models import Model

class SearchModel(Model):
    class Meta:
        abstract = True

    def on_post_save(self):
        print "%s.on_post_save()" % self

# NB `SearchModel` already inherits from `Model`
class Book(SearchModel):
    book_id = django_models.BigIntegerField(null=True, blank=True)

    class Meta:
        db_table = 'book'

@receiver(post_save)
def search_on_post_save(sender, instance, **kwargs):
    if issubclass(sender, SearchModel):
         instance.on_post_save()

rawDescription: Initiate post save on Django abstract parent model, when child model saved


code:
a= 'href=\"http:\/\/'
a
repr(a)
str(a)
print a

rawDescription: Disable auto escape in python

description: Disable auto escape in python


code:
import matplotlib.pyplot as plt
from sklearn import datasets

iris=datasets.load_iris()
plt.figure(figsize=(8,8))

for target in set(iris.target):
    print target
    x = [iris.data[i,0] for i in range(len(iris.target)) if iris.target[i]==target]
    y = [iris.data[i,2] for i in range(len(iris.target)) if iris.target[i]==target]
    plt.scatter(x, y, color=['red', 'blue', 'green'][target], label=iris.target_names[target])
plt.xlabel(iris.feature_names[0])
plt.ylabel(iris.feature_names[2])
plt.title('Iris Dataset')
plt.legend(iris.target_names, loc='lower right')
plt.show()

rawDescription: Missing labels in matplotlib scatter plot legend


code:
def detect(image):
 image_faces = []
 bitmap = cv.fromarray(image)
 faces = cv.HaarDetectObjects(bitmap, cascade, cv.CreateMemStorage(0))
 if faces:
  for (x,y,w,h),n in faces:
   image_faces.append(image[y:(y+h), x:(x+w)])
   #cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,255),3)
 return image_faces

if __name__ == "__main__":
    cam = cv2.VideoCapture(0)
    while 1:
        _,frame =cam.read()
        image_faces = []
        image_faces = detect(frame)
        for i, face in enumerate(image_faces):
           cv2.imwrite("face-" + str(i) + ".jpg", face)

        #cv2.imshow("features", frame)
        if cv2.waitKey(1) == 0x1b: # ESC
            print 'ESC pressed. Exiting ...'
            break

rawDescription: Python+OpenCV: cv2.imwrite

description: Python+OpenCV: cv2.imwrite


code:
def Pythag(league):
    total_runs_scored = 0
    for team, scores in league.iteritems():
        # other stuff
        total_runs_scored += scores[0]
        # other stuff
        # runs scored by all teams up to this point
        print 'League Running Total of Runs Scored: %f' % (total_runs_scored,)
    # outside the loop, so total runs scored in the league.
    # will be the same as the last one in the loop
    print 'League Total Runs Scored: %f' % (total_runs_scored,)

rawDescription: Summing Only First Values When in Dictionary When Keys Correspond To Lists (Python)


code:
from multiprocessing import Process, Queue
import time

def queuer(q):
    while True:
        q.put("JOB")
        print "Adding JOB"
        time.sleep(1)

def worker(q):
    while True:
        if not q.empty():
            item = q.get()
            print "Running", item
        else:
            print "No jobs"
            time.sleep(1)

if __name__ == '__main__':
    q = Queue()
    a = Process(target=queuer, args=(q,))
    b = Process(target=worker, args=(q,))
    a.start()
    b.start()

rawDescription: How do you share a variable between two processes in Python?

description: Share a variable between two processes in Python


code:
from Tkinter import *
import tkMessageBox
from scipy import stats
import csv

sample=[]

def load_file():

    fname = askopenfilename(filetypes = (("Text Files", ".txt"),
                            ("HTML Files", "*.html;*.htm"),
                                ("All Files", "*.*")))
    global sample
    if fname:
        print "uploading file...",fname
        try:
            print("""Here comes the file""")
        except:
            showerror("Open Source File", "Failed to read file\n'%s'" % fname)
        ifile  = open(fname, "rb")

        data = csv.reader(ifile,delimiter=' ')

        rownum = 0

        for row in data:
            colnum = 0
            d=[]
            for col in row:
                print '%s' % col
                d.append(float(col))
                colnum += 1
            sample.append(d)
            rownum += 1

        ifile.close()
        print sample

def choose():

        global sample
        if q1.get() == 1 and q2.get() == 1 and q3.get() == 1 and q4.get() == 2 and  q5.get() == 1 and q6.get() == 2 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2 :

            tkMessageBox.showinfo('t-test', 't-value is %s, p=%s' %stats.ttest_1samp(sample[0], 0.50 ))
            #will do t-test here
            print sample
            # now sample list contains the data for doing t-test,
            #you just need to call t-test to get the results and show it on message box

        elif q1.get() == 1 and q2.get() == 2 and q3.get() == 1 and q4.get() == 1 and q5.get() == 2 and q6.get() == 2 and q7.get() == 2 and  q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('z-test', 'test statistic is %s, p=%s'%stats.ttest_1samp(sample[0], 5.0))

        elif q1.get() == 1 and q2.get() == 1 and q3.get() == 2 and q4.get() == 2 and q5.get() == 1 and q6.get() == 2 and q7.get() == 1 and q8.get() == 2 and q9.get() == 2:
            tkMessageBox.showinfo('Paired t-test', 'test statistic is %s, p=%s'%stats.ttest_rel(sample[0], sample[1]))

        elif q1.get() == 2 and q2.get() == 1 and q3.get() == 2 and q4.get() == 2 and q5.get() == 1 and q6.get() == 1 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('Two sample t-test', 'test statistic is %s, p=%s'%stats.ttest_ind(sample[0], sample[1]))

        elif q1.get() == 2 and q2.get() == 2 and q3.get() == 1 and q4.get() == 1 and q5.get() == 2 and q6.get() == 1 and q7.get() == 2 and q8.get() == 2 and q9.get() == 2:
             tkMessageBox.showinfo('Two sample z-test', 'test statistic is %s, p=%s'%stats.ttest_ind(sample[0], sample[1]))

        else:
            tkMessageBox.showinfo('decision', 'You have either incorrectly answered a question about your data or none of the available tests are appropriate.')
            #root.destroy()
root = Tk()

q1 = IntVar()

Label(root,
      text="""How many samples do you have?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text="One",
            padx = 20,
            variable=q1,
            value=1).pack(anchor=N)
Radiobutton(root,
            text="Two",
            padx = 20,
            variable=q1,
            value=2).pack(anchor=N)

q2 = IntVar()

Label(root,
      text="""Which choice most closely fits your sample size?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Less than 30""",
            padx = 20,
            variable=q2,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """Greater than or equal to 30""",
            padx = 20,
            variable=q2,
            value = 2).pack(anchor=N)

q3 = IntVar()

Label(root,
      text="""Is the population mean known?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q3,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q3,
            value = 2).pack(anchor=N)

q4 = IntVar()

Label(root,
      text="""Is the standard deviation of your data known?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q4,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q4,
            value = 2).pack(anchor=N)

q5 = IntVar()

Label(root,
      text="""Do you wish to compare two groups?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q5,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q5,
            value = 2).pack(anchor=N)

q6 = IntVar()

Label(root,
      text="""Do you want to compare two sample means?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q6,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q6,
            value = 2).pack(anchor=N)

q7 = IntVar()

Label(root,
      text="""Is your data paired (E.g. before and after data)?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q7,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q7,
            value = 2).pack(anchor=N)

q8 = IntVar()

Label(root,
      text="""Are you testing proportions?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q8,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q8,
            value = 2).pack(anchor=N)

q9 = IntVar()

Label(root,
      text="""Do you wish to test for a difference between observed and expected data?""",
      justify = LEFT,
      padx = 20).pack(anchor=W)

Radiobutton(root,
            text = """Yes""",
            padx = 20,
            variable=q9,
            value = 1).pack(anchor=N)

Radiobutton(root,
            text = """No""",
            padx = 20,
            variable=q9,
            value = 2).pack(anchor=N)

from tkFileDialog import askopenfilename
from tkMessageBox import showerror

Button(root,text = "Browse files", command = load_file, width = 10).pack()

Button(root, text = "Submit", command=choose).pack()

root.mainloop()

rawDescription: Tkinter FileDialog

description: Tkinter FileDialog


code:
while True:
    cpu_time = psutil.cpu_percent(interval=1,percpu=False)
    if cpu_time < 10:
        print "CPU usage: "+str(cpu_time)
    elif cpu_time <=25:
        print "CPU usage: "+str(cpu_time)

rawDescription: Python: Creating a Loop to update data

description: Python: Creating a Loop to update data


code:
import collections

data = '''name: john, jane
car: db9, m5
food: pizza, lasagne
'''

personal_list = collections.defaultdict(dict)

for line in data.splitlines():
  key, _, info = line.partition(':')
  infos = info.split(',')
  key = key.strip().title()
  for i, item in enumerate(infos):
    item = item.strip().title()
    personal_list[i][key] = item

for i in personal_list:
  print personal_list[i]

rawDescription: Sorting Lists of List of Dictionaries

description: Sorting Lists of List of Dictionaries


code:
A = [2005, [4, 5, 6, 7], 2006, [4, 5, 6, 4]]
B = [7, 6]
global_max = max(B)

for year, year_list, max_year in zip(A[::2], A[1::2], B):
    print year
    for year_val in year_list:
        if year_val == global_max:
             print year_val, "(local max) (global max)"
        elif year_val == max_year:
             print year_val, "(local max)"
        else:
             print year_val

rawDescription: Managing integers in a list

description: Managing integers in a list


code:
N = len(CashFlow)
for j in range(N):
    NPV = 0
    for i in range(j, N):
        NPV += CashFlow[i] / (1+MonthlyInterest) ** (i - j)
        NPV = round (NPV, 0)
    ListNPV.append(NPV)

print 'CashFlow = ', CashFlow
print 'ListNPV =', ListNPV

rawDescription: Why is my loop in a loop only printing half the list length?


code:
lines=0
wordCount=0
mostWordsInLine = 0
fileHandler=getInputFile()

for lineOfText in fileHandler.readlines():
    lines += 1
    print(str(lines),str(lineOfText))
    f1=lineOfText.split()
    wordCount=wordCount+len(f1)
    if len(f1) > mostWordsInLine:
        mostWordsInLine = len(f1)
    print ("Word count:" +str(wordCount))

print "Average words per line: {}".format(wordCount / lines)
print "Most words in a single line: {}".format(mostWordsInLine)

rawDescription: Counting words per line from text file in Python


code:
import urllib
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

profile_link = 'http://hassanmehmood.com'

class TitleScrapper(object):

    def __init__(self):

        fp = webdriver.FirefoxProfile()
        fp.set_preference("browser.startup.homepage_override.mstone", "ignore") #Avoid startup screen
        fp.set_preference("startup.homepage_welcome_url.additional",  "about:blank")

        self.driver = webdriver.Firefox(firefox_profile=fp)
        self.driver.set_window_size(1120, 550)

    def scrape_profile(self):
        self.driver.get(profile_link)
        print self.driver.title
        self.driver.close()

    def scrape(self):
        self.scrape_profile()

if __name__ == '__main__':
    scraper = TitleScrapper()
    scraper.scrape()

rawDescription: How can a scraped HTML be different from the source code?


code:
from itertools import groupby
s = "HL1110/1110R/1112/1112R/MFC1810/1810R/1815/1815R/DCP1510/1510R/1512/1512R"
for item in s.split("/"):
    print ["".join(g) for k,g in groupby(item, str.isdigit)]

rawDescription: How do I separate letters and digits in python?

description: Separate letters and digits in python


code:
from urlparse import urlparse, parse_qs

url = "http://localhost:8069/web#view_type=form&model=system.audit&menu_id=221&action=229"
uri = urlparse(url)
qs = uri.fragment
print parse_qs(qs).get('model', None)
# ['system.audit']

rawDescription: how to get current model name from url odoo

description: Get current model name from url odoo


code:
usernames = ["jBlame", "jannet"]
queries = ["j", "jm", "jbl", "JB"]

def build_index(usernames):
    """Build an index by given usernames

    :returns: index dict
    """
    result = {}
    # Sort should ignore cases
    for username in sorted(usernames, key=lambda x: x.lower()):
        for i in range(len(username)):
            # TODO: If you only want the first result matched, modify this line to
            # let the index consumes less space
            result.setdefault(username[:(i + 1)].lower(), []).append(username)
    return result

def find_name(query, index):
    """return the matched username by given query and index
    """
    if not query:
        return ''
    result = index.get(query.lower())
    return result[0] if result else -1

index = build_index(usernames)
for query in queries:
    print find_name(query, index)

rawDescription: Algorithm to match query to known list, presenting result based on lexicographic order


code:
myNumber = 1.2e-8
myBase = 1e-12
mantissa = myNumber/myBase
print "{0:1}{1:s}".format(mantissa, "{0:1}".format(myBase)[1:])

rawDescription: Express floating point numbers in non standard notation in python?


code:
import urllib2
import datetime

from bs4 import BeautifulSoup as soup

url = "http://app2.nea.gov.sg/anti-pollution-radiation-protection/air-pollution/psi/psi-readings-over-the-last-24-hours"
web_soup = soup(urllib2.urlopen(url))

table = web_soup.find(name="div", attrs={'class': 'c1'}).find_all(name="div")[2].find_all('table')[0]

data = {}
bold_time = ''
cur_time = datetime.datetime.strptime("12AM", "%I%p")
for tr_index, tr in enumerate(table.find_all('tr')):
    if 'Time' in tr.text:
        continue
    for td_index, td in enumerate(tr.find_all('td')):
        if not td_index:
            continue
        data[cur_time] = td.text.strip()
        if td.find('strong'):
            bold_time = cur_time
        cur_time += datetime.timedelta(hours=1)

print data.get(bold_time)  # bold
print data.get(bold_time - datetime.timedelta(hours=1))  # before bold
print data.get(bold_time - datetime.timedelta(hours=2))  # before before bold

rawDescription: python beautifulsoup extracting text

description: Python beautifulsoup extracting text


code:
import csv
from collections import defaultdict

d = {100002: "['Apple', 'M', 'R', '500001', 'Fruit', '90']", 100004: "['Banana', 'M', 'Y', '500001', 'Fruit', '75']"}
dd = defaultdict(lambda: defaultdict(int))
with open('test.csv') as fin:
    csvin = csv.reader(fin)
    for row in csvin:
        dd[int(row[0])][row[1]] += 1

for key in (key for key in dd if key in d):
    counts = [0] * 4
    for idx, val in dd[key].iteritems():
        counts[int(idx) - 1] = int(val)
    print key, d[key], counts

# 100002 ['Apple', 'M', 'R', '500001', 'Fruit', '90'] [2, 0, 2, 0]
# 100004 ['Banana', 'M', 'Y', '500001', 'Fruit', '75'] [0, 1, 2, 0]

rawDescription: Counting Based on Occurrences of a Dictionary's Key in a .CSV File


code:
nums, my_str = {num:key for key in nums for num in nums[key]}, "xx111xxx200x222x"
import re
print nums
# {'200': 'two hundreds', '100': 'one hundreds', '111': 'one hundreds', '222': 'two hundreds'}
print [nums[item] for item in re.split('x+', my_str) if nums.get(item, "")]
# ['one hundreds', 'two hundreds', 'two hundreds']

rawDescription: How to find all element from str

description: Find all element from str


code:
import thread

try:
    from msvcrt import getch
except ImportError:
    def getch():
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        except:
            pass
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch

char = None
def keypress():
    global char
    char = getch()

thread.start_new_thread(keypress, ())

i=1
while True:
    if char is not None:
        print "Key pressed is {} -- exiting loop".format(char)
        break
    print "Loop has run {} times".format(i)
    i+=1

rawDescription: Manage loops outside it

description: Manage loops outside it


code:
from itertools import product
for item in product(['a', 'b', 'c'], repeat=3):
    print item

rawDescription: How to use itertools to compute all combinations with repeating elements?

description: Use itertools to compute all combinations with repeating elements


code:
tickme=""
with open('a.txt','r') as file:

    for line in file:
        if 'tickme' in line:
            tickme=line
        if line.startswith(' '):
            if 'dynamic' in line:
                print searchstring
                print line

rawDescription: python search file within specified range

description: Python search file within specified range


code:
'''
Rotate Image
'''
import pyexiv2
import wx
import cStringIO
import os

def rotateImage(infile, device):
    try:
        # Read Metadata from the image
        metadata = pyexiv2.metadata.ImageMetadata(infile)
        metadata.read();

        # Let's get the orientation
        orientation = metadata.__getitem__("Exif.Image.Orientation")
        orientation = int(str(orientation).split("=")[1][1:-1])

        # Extract thumbnail
        thumb = metadata.exif_thumbnail

        angle = 0

        # Check the orientation field in EXIF and rotate image accordingly
        if device == "iPhone" or device == "iPad":
            # Landscape Left : Do nothing
            if orientation == ORIENTATION_NORMAL:
                angle = 0
            # Portrait Normal : Rotate Right
            elif orientation == ORIENTATION_LEFT:
                angle = -90
            # Landscape Right : Rotate Right Twice
            elif orientation == ORIENTATION_DOWN:
                angle = 180
            # Portrait Upside Down : Rotate Left
            elif orientation == ORIENTATION_RIGHT:
                angle = 90

            # Resetting Exif field to normal
            print "Resetting exif..."
            orientation = 1
            metadata.__setitem__("Exif.Image.Orientation", orientation)

        # Rotate
        if angle != 0:
            # Just rotating the image based on the angle
            print "Rotating image..."
            angle = math.radians(angle)
            img = wx.Image(infile, wx.BITMAP_TYPE_ANY)
            img_centre = wx.Point( img.GetWidth()/2, img.GetHeight()/2 )
            img = img.Rotate( angle, img_centre, True )
            img.SaveFile(infile, wx.BITMAP_TYPE_JPEG)

            # Create a stream out of the thumbnail and rotate it using wx
            # Save the rotated image to a temporary file
            print "Rotating thumbnail..."
            t = wx.EmptyImage(100, 100)
            thumbStream = cStringIO.StringIO(thumb.data)
            t.LoadStream(thumbStream, wx.BITMAP_TYPE_ANY)
            t_centre = wx.Point( t.GetWidth()/2, t.GetHeight()/2 )
            t = t.Rotate( angle, t_centre, True )
            t.SaveFile(infile + ".jpg", wx.BITMAP_TYPE_JPEG)
            thumbStream.close()

            # Read the rotated thumbnail and put it back in the rotated image
            thumb.data = open(infile + ".jpg", "rb").read();
            # Remove temporary file
            os.remove(infile + ".jpg")

        # Write back metadata
        metadata.write();

    except Exception, e:
        print "Error rotating image... : " + str(e)

rawDescription: Rotating an image with orientation specified in EXIF using Python without PIL including the thumbnail


code:
import praw

user_agent = praw.Reddit("my_cool_user_agent")
link = "http://www.reddit.com/r/AdviceAnimals/comments/" + \
        "1adu71/apparently_people_still_need_to_hear_this/"
submission = user_agent.get_submission(link)
print submission.url

rawDescription: Help getting URL linked to by Reddit post With PRAW


code:
# The submission test must be a function.
def test_code(code, CC, error, code_vars):

    # unpack the student code namespace into the globals()
    globs = globals()
    for var, val in code_vars.items():
        globs[var] = val

    # Use information from errors in student code
    if error:
        return "Yada yada %s" %error

    # Use information in the raw student code
    if len(code.split("\n")) is not 2:
        return "This should be accomplished in 2 lines"

    # Have direct access to variables from the student code
    # I'd like to avoid params['y'] if possible.
    try:
        y
    except NameError:
        return "Please use the variable y"

    if y is not 8:
        return "Wrong! Check stuff"

    # Use information from print output
    if str(y) not in CC:
        return "Remember to print your variable!"

    return True

# Read in student code
student_code = """y = 8
                  print y
                  potato"""

# Catch print output
CC = StringIO.StringIO()
sys.stdout = CC

# create the namespace for the student code
code_vars = {}

# Execute student code and catch errors
try:
    # execute the student code in the created namespace
    exec student_code in code_vars
except Exception as e:
    error = e

# Start outputting to the terminal again
sys.stdout = sys.__stdout__

# Run the submission test
submission_test = test_code(student_code, CC.split("\n"), error, code_vars)

# Output the result of the submission test
if submission_test is True:
    print("Well done!")
elif submission_test is False:
    print("Oops! You failed... Try again!")
else:
    print(submission_test)

rawDescription: How to take arbitrary variables loaded by exec, and use them in a function

description: Take arbitrary variables loaded by exec, and use them in a function


code:
file_data = open("/root/.ssh/authorized_keys").read()
"""manipulate the contents of the file such that the variable new_key contains
   the data you want to append to the file via ssh"""

#I'll assume the data to be abcd
new_key = "abcd"
#Constructing the command to pass via ssh
cmd = 'echo "' + new_key + '">>/root/.ssh/authorized_keys'
#note that I've used >> and not >, the former will append while the later will overwrite the file
#also the path given in the above command is the one on the remote ssh server and not your local machine
s.sendline(cmd)
s.prompt()
print s.before
#voilà your key is appended to the file on the remote server
#you can check that
s.sendline("cat /root/.ssh/authorized_keys")
s.prompt()
print s.before

rawDescription: Python / create ssh connection and change a file


code:
def copy(self):
    for i in range(0, self.myTable.rowCount()):
        checkbox = self.myTable.cellWidget(i, 0)
        print "Check box " + str(i+1) + " is :",
        print 'checked' if checkbox.isChecked() else 'not checked'

rawDescription: check state checkBox in table (pySide)

description: Check state checkBox in table (pySide)


code:
#!/usr/bin/python                   #on windows change to your path to the python exe

print "Content-Type: text/html"     # HTML is following
print                               # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "
This is my first CGI script
"
print "Hello, world!"

rawDescription: How do I create a webpage with buttons that invoke various Python scripts on the system serving the webpage?

description: Create a webpage with buttons that invoke various Python scripts on the system serving the webpage


code:
import random
from retrying import retry

@retry
def do_something_unreliable():
    if random.randint(0, 10) > 1:
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print do_something_unreliable()

rawDescription: commands execution in python script

description: Commands execution in python script


code:
class FluentList(list):
    def append(self, value):
        super(FluentList,self).append(value)
        return self

    def extend(self, iterable):
        super(FluentList,self).extend(iterable)
        return self

    def remove(self, value):
        super(FluentList,self).remove(value)
        return self

    def insert(self, index, value):
        super(FluentList,self).insert(index, value)
        return self

    def reverse(self):
        super(FluentList,self).reverse()
        return self

    def sort(self, cmp=None, key=None, reverse=False):
        super(FluentList,self).sort(cmp, key, reverse)
        return self

li = FluentList()
li.extend([1,4,6]).remove(4).append(7).insert(1,10).reverse().sort(key=lambda x:x%2)
print li

rawDescription: Why does [].append() not work in python?

description: Why does [].append() not work in python


code:
class Dummy(object):
  def __init__(self, klass, path):
    self.klass = klass
    self.path = path

  def __getattr__(self, attr):
    return Dummy(self.klass, self.path + [attr])

  def __call__(self, *args, **kw):
    self.klass._dummy_func(self.path, *args, **kw)

class Klass(object):
  def __getattr__(self, attr):
    return Dummy(self, [attr])

  def _dummy_func(self, path, *args, **kw):
    print "Dummy function %s called with %d arguments!" \
                      % ('.'.join(path), len(args))

Klass().foo.bar.func(1, 2)
# => "Dummy function foo.bar.func called with 2 arguments!"

rawDescription: How to catch the lookup of non-existent nested variables on a Python class?

description: Catch the lookup of non-existent nested variables on a Python class


code:
SPLIT="SV="
line=">tr|A0A024RAP8|A0A024RAP8_HUMAN HCG2009644, isoform CRA_b OS=Homo sapiens GN=KLRC4-KLRK1 PE=4 SV=1MGWIRGRRSRHSWEMSEFHNYNLDLKKSDFSTRWQ"
print line.split(SPLIT)[0] + SPLIT + line.split(SPLIT)[1][0]

rawDescription: splitting strings after certain characters

description: Splitting strings after certain characters


code:
import csv

header = None
idfld = None
sourcefld = None

record_table = {}

with open('input.csv', 'rb') as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        row = [x.strip() for x in row]

        if header is None:
            header = row
            for i, fld in enumerate(header):
                if fld == 'ID':
                    idfld = i
                elif fld == 'Source':
                    sourcefld = i
            continue

        key = row[idfld]
        sourcename = row[sourcefld]

        if key not in record_table:
            record_table[key] = {sourcename: row, "all_rows": [row]}
        else:
            if sourcename in record_table[key]:
                cur_row = record_table[key][sourcename]
                for i, fld in enumerate(row):
                    if cur_row[i] == '':
                        record_table[key][sourcename][i] = fld
            else:
                record_table[key][sourcename] = row
            record_table[key]["all_rows"].append(row)

print ', '.join(header) + ', duplicate_flag'

for recordid in record_table:
    rowdict = record_table[recordid]

    final_row = [''] * len(header)

    rowcount = len(rowdict)

    for sourcetype in ['NY Times', 'ESPN', 'Wall Street Journal']:
        if sourcetype in rowdict:
            row = rowdict[sourcetype]
            for i, fld in enumerate(row):
                if final_row[i] != '':
                    continue
                if fld != '':
                    final_row[i] = fld

    if rowcount > 1:
        for row in rowdict["all_rows"]:
            print ', '.join(row) + ', duplicate'

    print ', '.join(final_row) + ', not_duplicate'

rawDescription: Python Duplicate Removal

description: Python Duplicate Removal


code:
import os
import glob
import time

class Purge(object):

    removable_extensions = ['ts', 'm3u8']

    def __init__(self, basedir, channel_pattern, debits,
                 older_than_days, test_mode=False):
        self.basedir = basedir
        self.channel_pattern = channel_pattern
        self.debits = debits
        self.older_than_secs = time.time() - 24*60*60*older_than_days
        self.test_mode = test_mode  # If `True`, do not delete files.

    def delete_file(self, filepath):
        try:
            os.remove(filepath)
        except OSError:
            pass

    def file_for_deletion(self, filepath):
        # Return `True` if a file meets all conditions for deletion.
        filename, ext = os.path.splitext(os.path.basename(filepath))
        condition_ext = ext[1:] in self.removable_extensions
        condition_old = os.stat(filepath).st_mtime <= self.older_than_secs
        condition_deb = any(
            '_{}_'.format(d) in filename or filename.endswith(d)
            for d in self.debits
            )
        return all((condition_ext, condition_old, condition_deb))

    def purge_channel(self, channel_dir):
        for root, dirs, files in os.walk(channel_dir):
            for name in files:
                filepath = os.path.join(root, name)
                if self.file_for_deletion(filepath):
                    print filepath
                    if not self.test_mode:
                        self.delete_file(filepath)
            #TODO: delete empty directories here.

    def purge(self):
        channels = glob.glob(os.path.join(self.basedir, self.channel_pattern))
        for channel_dir in channels:
            self.purge_channel(channel_dir)

if __name__ == '__main__':

    purge_job_info = dict(
        basedir=r'path/to/data',  # All channel folders live here.
        channel_pattern='channel*',  # `glob` pattern.
        debits=['400', '1800'],
        older_than_days=7,
        )

    p = Purge(**purge_job_info)
    p.test_mode = True
    p.purge()

rawDescription: Purge script with mutltiple conditions

description: Purge script with mutltiple conditions


code:
def player_sign():
    choice = raw_input("Do you want to be X or O?:  ").lower()
    while choice != 'x' and choice != 'o':
        print "Error!\n Wrong input!"
        choice = raw_input("Do you want to be X or Y?:  ").lower()
    if choice == 'x':
        print "X is yours!"
        player = 'X'
        computer = 'O'
        return player, computer
    elif choice == 'o':
        print "You've chosen O!"
        player = 'O'
        computer = 'X'
        return player, computer
    else:
        print "Error!\n Wrong input!"
        return None, None

player_choice, computer_choice = player_sign()

rawDescription: Function won't return new value Python 2.7


code:
from collections import defaultdict

def get_request_data(form):
    '''
    return dict list with data from request.form

    :param form: MultiDict from `request.form`
    :rtype: {id1: {field1:val1, ...}, ...} [fieldn and valn are strings]
    '''

    # request.form comes in multidict [('data[id][field]',value), ...]

    # fill in id field automatically
    data = defaultdict(lambda: {})

    # fill in data[id][field] = value
    for formkey in form.keys():
        if formkey == 'action': continue
        datapart,idpart,fieldpart = formkey.split('[')
        if datapart != 'data': raise ParameterError, "invalid input in request: {}".format(formkey)

        idvalue = int(idpart[0:-1])
        fieldname = fieldpart[0:-1]

        data[idvalue][fieldname] = form[formkey]

    # return decoded result
    return data

rawDescription: How to decode dataTables Editor form in python flask?

description: Decode dataTables Editor form in python flask


code:
import subprocess
args = ['/path/to/python', 'bar.py']
process = subprocess.Popen(args, shell=True, creationflags=subprocess.CREATE_NEW_CONSOLE) # you can also set only shell=True
print process.pid

rawDescription: Python inter-process communication, getting pid of an independent process (not child) started from another Python process


code:
def batch(size):
    num_lines = sum(1 for line in open('File1.txt'))
    if num_lines >= size:
        with open('File1.txt', 'r+') as File1:
            head = [next(File1) for x in xrange(5)]
            File2 = open('File2.txt', "w")
            for i in head:
                File2.write(i)

            lines = open('File1.txt').readlines()
            open('File1.txt', 'w').writelines(lines[size:])

            print 'Batch created.'

    else:
        print 'Nothing ready in follow file.'

rawDescription: Copy N lines from File1 to File2, then delete copied lines in File1


code:
import sys, select, os, datetime
from multiprocessing import Process, Value
from time import time

def func_control(fileno, switch):
    sys.stdin = os.fdopen(fileno)  #open stdin in this process

    print "I'm doing stuff. Press Enter to stop me!"
    some_str = raw_input("> ")
    switch.value = 0
    return

def func_1(i1, switch):
    i1.value = 0
    while switch.value != 0:
        i1.value = i1.value+1
    return 0

def func_2(i2, switch):
    i2.value = 0
    while switch.value != 0:
        i2.value = i2.value+1
    return 0

def start_fcontrol(start_time, fn, switch):
    while time() < start_time.value: pass
    func_control(fn, switch)

def start_f1(i1, start_time, switch):
    while time() < start_time.value: pass
    func_1(i1, switch)

def start_f2(i2, start_time, switch):
    while time() < start_time.value: pass
    func_2(i2, switch)

switch = Value('i', 1)
i1 = Value('i', 0)
i2 = Value('i', 0)
start_time = Value('d', time() + 1)

fn = sys.stdin.fileno() #get original file descriptor

procs = []
procs.append(Process(target=start_fcontrol, args=(start_time, fn, switch)))
procs.append(Process(target=start_f1, args=(i1, start_time, switch)))
procs.append(Process(target=start_f2, args=(i2, start_time, switch)))
map(lambda x: x.start(), procs)
map(lambda x: x.join(), procs)
print "i1 = ", i1.value
print "i2 = ", i2.value

rawDescription: Terminating multiple processes containing while loops upon user input


code:
import socket, struct

def atod(a): # ascii_to_decimal
    return struct.unpack("!L",socket.inet_aton(a))[0]

def dtoa(d): # decimal_to_ascii
    return socket.inet_ntoa(struct.pack("!L", d))

net,_,mask = sys.argv[1].partition('/')
mask = int(mask)
net = atod(net)

for host in (dtoa(net+n) for n in range(0, 1<<32-mask)):
    print host

rawDescription: Scanning a Class C network Python

description: Scanning a Class C network Python


code:
course_name
print re.sub('\([A-Z]+\)\s*', '', course_name)
print re.search('\(([A-Z]+)\)\s*', course_name).groups()[0]

rawDescription: Extract only first match using python regular expression


code:
soup = BeautifulSoup("""

cats

they meow

they have fur

turtles

they don't make noises

they have shells

""")

animals = []
attributes = {}

for p in soup.findAll('p'):
    if (p['class'] == 'animal'):
        animals.append(p.string)
    elif (p['class'] == 'attribute'):
        if animals[-1] not in attributes.keys():
            attributes[animals[-1]] = [p.string]
        else:
            attributes[animals[-1]].append(p.string)

print animals
print attributes

rawDescription: Separating HTML into groups using BeautifulSoup when groups are all in the same element


code:
from docutils.core import publish_string
print publish_string(rst)

rawDescription: How to print a reStructuredText node tree?

description: Print a reStructuredText node tree


code:
import time
while True:
    print "fetching"
    time.sleep(10)

rawDescription: Python: Perform ations repeatedly over a time interval, as long as a condition is true


code:
import re
import os
import sys
import numpy as np
import numpy.linalg as LA
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer

trainingdataDir = 'C:\\data\\Training data'

tfidf_vectorizer = TfidfVectorizer()

transformer = TfidfTransformer()
def readfile(trainingdataDir):
    train_set = []
    for file in os.listdir(trainingdataDir):
        trainingfiles = os.path.join(trainingdataDir, file)
        if os.path.isfile(trainingfiles):
            data = open(trainingfiles, 'r')
            data_set=str.decode(data.read())
            train_set.append(data_set)
    return train_set

tfidf_matrix_train = tfidf_vectorizer.fit_transform(readfile(trainingdataDir))
print 'Fit Vectorizer to train set',tfidf_matrix_train
print "cosine scores ==> ",cosine_similarity(tfidf_matrix_train[0:1], tfidf_matrix_train)

rawDescription: Vectorizing Files using sklearn

description: Vectorizing Files using sklearn


code:
import re

text = """
  & $\Ultraspherical{\lambda}{n}@{x}$
  & $(-1,1)$
  & $(1 - x^2)^{\lambda-\frac{1}{2}}$
  & $\dfrac{2^{1-2\lambda} \pi \EulerGamma@{n+2\lambda}}
           {(n+\lambda) \left( \EulerGamma@{\lambda} \right)^2 n!}$
  & $\dfrac{2^n \pochhammer{\lambda}{n}}{n!}$ & $0$
  & $\lambda > -\tfrac{1}{2}, \lambda \ne 0 $

"""

print(len(re.findall('^\s*&', text, re.M)))

rawDescription: Using Python Regex with Line Count

description: Using Python Regex with Line Count


code:
str = """A/file1
A/B/C/D/file3
A/B/file1
A/B/file2
A/B/C/D/file1
A/file2
A/W/X/Y/Z/file1
A/W/file1
A/W/X/file1
A/file3
A/B/C/file1
A/W/X/Y/file1
A/B/file2"""

import string
files = string.split(str, "\n")

import os.path
std = sorted(files, key=lambda file: (os.path.dirname(file), os.path.basename(file)))

print std[0]
for i in range(1,len(std)):
    if os.path.dirname(std[i]) != os.path.dirname(std[i-1]):
        print ""
    print std[i]

rawDescription: File paths hierarchial sort in python

description: File paths hierarchial sort in python


code:
def test(thing=None):
    if thing is None:
        thing = StringIO.StringIO()
    thing.write("test")
    print thing.getvalue()

rawDescription: What are the rules about default parameters in Python?


code:
from lxml.html import fromstring

html = '''

 Summary:

 According to the complaint filed January 04, 2011, over a six-week period in December 2007 and January 2008, six healthcare related hedge funds managed by Defendant FrontPoint Partners LLC ("FrontPoint") sold more than six million shares of Human Genome Sciences, Inc. ("HGSI") common stock while their portfolio manager possessed material negative non-public information concerning the HGSI's clinical trial for the drug Albumin Interferon Alfa 2-a.

On March 2, 2011, the plaintiffs filed a First Amended Class Action Complaint, amending the named defendants and securities violations.  On March 22, 2011, a motion for appointment as lead plaintiff and for approval of selection of lead counsel was filed.  The defendants responded to the First Amended Complaint by filing a motion to dismiss on March 28, 2011.

INDUSTRY CLASSIFICATION:

 SIC Code:

0000

 Sector:

N/A

 Industry:

N/A

'''

htmlElement = fromstring(html)
textContent = htmlElement.text_content()
result = textContent.split('\n\n Summary:\n\n')[1].split('\n\nINDUSTRY CLASSIFICATION:\n\n')[0]

print result

rawDescription: python - How to transform a html file into a human-readable txt file?

description: Python - How to transform a html file into a human-readable txt file


code:
app = Tk()
app.title("My GUI")
app.iconbitmap(app, "C:\Program Files (x86)\Notepad++\Files\icon.ico")
app.resizable(0,0)

app.mainloop()

rawDescription: Python Tkinter small window pops up momentarily before main window


code:
s = 'somestring'
names = ['file1.sql', 'file2.sql']
for n in names:
    f = open(n)
    lines = f.readlines()
    for i, l in enumerate(lines):
        if s in l:
            print 'line %d' % (i)

rawDescription: Need a python script to search specific keywords in a collection of sql files returning the index of where each keyword was found


code:
url = "https://something_api.p.mashape.com/q=whateverquery",
headers={"X-Mashape-Key": "ABCDEFG12345"}
test = fetchMashape(url, headers)

def fetchMashape(url, headers):
    cached = cache.get(url)
    content = ""
    if not cached:
        response = unirest.get(url,
              headers=headers)
        print "not cached"
        if response is not None:
            cache.set(url, response.raw_body)
            content = response.raw_body
        else:
            content = None
    else:
        content = cached
        print "already cached"

    dictionary = json.loads(content)

    return dictionary

rawDescription: Caching Mashape API calls in Python

description: Caching Mashape API calls in Python


code:
abb_dict = {
    'lol': 'laughing out loud',
    'bfn': 'bye for now',
    'cuz': 'because',
    'omw': 'on my way',
    'tbh':'to be honest',
    'afk':'away from keyboard',
    'brb':'be right back',
    'afaik':'as far as i know',
    'np':'no problem',
    'rofl':'rolling on floor laughing',
    'asap': 'as soon as possible',
}

user_wants_to_play = True
while user_wants_to_play:
    check = raw_input('Do you want to play? y/n:')
    if check == 'y':
        tweet_str = raw_input('Enter a statement with an abbreviation: ')
        words = tweet_str.split(' ')
        rtn_sentence = []
        for word in words:
            try:
                replacement = abb_dict[word]
                word = replacement
                rtn_sentence.append(word)
            except:
                rtn_sentence.append(word)
        print ' '.join(rtn_sentence)
    else:
        user_wants_to_play= False

rawDescription: Understanding why my loop is not restarting


code:
ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

print "Please enter three letters"
x = list(raw_input("> ").upper())

length = len(ALPHABET)

first_letter = ALPHABET.index(x[0])
first_letter = (first_letter + 0) % length

x[0] = ALPHABET[first_letter]

second_letter = ALPHABET.index(x[1])
second_letter = (second_letter + 2) % length

x[1] = ALPHABET[second_letter]

third_letter = ALPHABET.index(x[2])
third_letter = (third_letter + 19) % length

x[2] = ALPHABET[third_letter]

print ''.join(x)

rawDescription: letter shifting program in python

description: Letter shifting program in python


code:
for s,x in itertools.product(range(65),ml):
    t = s * x
    if t == 65:
        print s
        print x
        break

rawDescription: Multiply all numbers in a generated list by an increasing number to reach a value python


code:
array = [1,2,3,4,5]
number = 10
if all(number % x == 0 for x in array):
    print "%s is dividable by %s" % (str(number), str(array))

rawDescription: nested if statements based on values in an array

description: Nested if statements based on values in an array


code: print "%4s %4s %4s %4s %4s" % tuple(Lst[0] for Lst in tableau_list[0:5])

rawDescription: Python 2.7.8 format printing - list of lists


code:
import gevent.monkey
from gevent.queue import Queue
from gevent import sleep
gevent.monkey.patch_all()

MAX_REQUESTS = 10

requests = Queue(MAX_REQUESTS)
articles = Queue()

mock_responses = range(100)
mock_responses.reverse()

def request():
    print "worker started"
    while True:
        print "request %s" % requests.get()
        sleep(1)

        try:
            articles.put('article response %s' % mock_responses.pop())
        except IndexError:
            articles.put(StopIteration)
            break

def run():
    print "run"

    i = 1
    while True:
        requests.put(i)
        i += 1

if __name__ == '__main__':
    for worker in range(MAX_REQUESTS):
        gevent.spawn(request)

    gevent.spawn(run)
    for article in articles:
        print "Got article: %s" % article

rawDescription: speed up a HTTP request python and 500 error

description: Speed up a HTTP request python and 500 error


code:
import tkFileDialog
import os
import os.path
import re

directory = tkFileDialog.askdirectory()

for dirpath, dirnames, filenames in os.walk(directory):
    for filename in filenames:
        path = os.path.join(dirpath, filename)

        with open(path) as file:
            contents = file.read()

            print path[:30], contents.count('Tmp'), re.findall('Tmp\d{5}', contents)

rawDescription: Counting "something" in multiple text files in one folder


code:
a = [(1, 2), (1, 8), (2, 3), (2, 7), (2, 8), (2, 9), (3, 1), (3, 2), (3, 5), (3, 6),  (3, 7), (3, 7), (3, 9)]

x1=None  # here we keep track of the last x we saw
ys=None  # here we keep track of all ys we've seen for this x1

result = []

for x,y in a:
    if x != x1:  # this is an x we haven't seen before
        if ys:   # do we have results for the last x?
            result.append( ys )
        ys = [ x, '', y ] # initialize the next set of results
        x1 = x
    else:
        ys.append( y ) # add this to the results we are buliding

if ys:
    result.append( ys )  # add the last set of results

print result

rawDescription: Python, format this list

description: Python, format this list


code:
import os

def show_and_append(dir):
    lst = []
    # assume that dir exists.
    for i in os.walk(dir):
        print i[0]
        lst.append(i[0])
    return lst

children = show_and_append('/tmp/')

rawDescription: Print a file/directory name as it finds it


code:
from ctypes import *

class ArrayProxy(object):
    def __init__(self, array, struct):
        self.array = array
        self.struct = struct

    def __setitem__(self, i, val):
        self.array[i] = val
        self.struct.attributes_updated = True

    def __getitem__(self, i):
        item = self.array[i]
        if issubclass(type(item), Array):
            # handle multidimensional arrays
            return ArrayProxy(item, self.struct)
        return item

class Comm(Structure):
    def __init__(self):
        self.attributes_updated = False

    def __setattr__(self, name, value):
        super(Comm, self).__setattr__('attributes_updated', True)
        super(Comm, self).__setattr__(name, value)

    def __getattribute__(self, name):
        attr = super(Comm, self).__getattribute__(name)
        if issubclass(type(attr), Array):
            return ArrayProxy(attr, self)
        return attr

class MyCStruct(Comm):
    _fields_ = [('number', c_int),
                ('array', c_int*5),
                ('multiarray', c_int*2*1),]

    def __init__(self):
        Comm.__init__(self)

s = MyCStruct()
print s.array
# <__main__.ArrayProxy object at 0x1b1f3d0>
print s.attributes_updated
# False
s.array[0] = 1
print s.attributes_updated
# True

s2 = MyCStruct()
s2.multiarray[0][0] = 1
print s2.attributes_updated
# True

rawDescription: How does one override the __setitem__ method for (possibly multidimensional) arrays created via ctypes _fields_?

description: Override the __setitem__ method for (possibly multidimensional) arrays created via ctypes _fields_


code:
import time
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
yesterday = yesterday.strftime('%Y/%m/%d')
yesterday = "/%s/%s" % ( time.tzname[0], yesterday )
print yesterday

rawDescription: Getting yesterdays date with time zone

description: Getting yesterdays date with time zone


rawDescription: Search a variable for an address

description: Search a variable for an address


code:
import fileinput

for line in fileinput.input("myfile.txt", inplace = 1):
    if line == "$menu_items['gojo_project']                    => array('http://www.gojo.net/community/plugin-inventory/ops-gojo/gojo', 'gojo',3),":
        line = '//' + line
    print line,

rawDescription: Python: match a long string with special characters and white spaces and then prepend two characters to the beginning


code: print int(current_program.total_seconds() / 60)

rawDescription: Converts from timedelta into minutes

description: Converts from timedelta into minutes