code:
import xml.etree.ElementTree as ET

xml = '''<user name="John Doe" title="Manager">
  <group title="USA">
    <column name="Inventory">
    Inventory of the products
    </column>

    <column name="Sells">
    Sells of the products
    </column>
  </group>
</user>'''

root = ET.fromstring(xml)

inventory = root.findall('.//column[@name="Inventory"]')
print inventory[0].text.strip()

sells = root.findall('.//column[@name="Sells"]')
print sells[0].text.strip()

rawDescription: Reading XML file using xml.dom.minidom or elementtree - Python


code:
#!/usr/bin/env python

import antlr3
from antlr3 import *
from antlr3.tree import *
from CppLexer import *
from CppParser import *

def print_level_order(tree, indent):
  print '{0}{1}'.format('   '*indent, tree.text)
  for child in tree.getChildren():
    print_level_order(child, indent+1)

input = """
  int main (){
    int a = 4;
    return a;
  }
"""
char_stream = antlr3.ANTLRStringStream(input)
lexer = CppLexer(char_stream)
tokens = antlr3.CommonTokenStream(lexer)
parser = CppParser(tokens)
tree = parser.parse().tree
print_level_order(tree, 0)

rawDescription: draw syntax tree with antlr3 generated parser for the Python

description: Draw syntax tree with antlr3 generated parser for the Python


code: print pattern.findall(test3)

rawDescription: Python Regex Skipping Optional Groups

description: Python Regex Skipping Optional Groups


code:
import networkx as nx
G = nx.MultiDiGraph()
G.add_nodes_from(['A', 'B', 'C', 'D', 'E'])

G.add_edge('A', 'B', weight=5)
G.add_edge('B', 'C', weight=4)
G.add_edge('C', 'D', weight=8)
G.add_edge('D', 'C', weight=8)
G.add_edge('D', 'E', weight=6)
G.add_edge('A', 'D', weight=5)
G.add_edge('C', 'E', weight=2)
G.add_edge('E', 'B', weight=3)
G.add_edge('A', 'E', weight=7)

print nx.dijkstra_path_length(G, source = 'A', target = 'C')

rawDescription: calculating graph weight in python with NetworkX

description: Calculating graph weight in python with NetworkX


code:
from random import shuffle

def merge(events, actors, index=None):
    """Returns list of unique pairs of events and actors, none of which may on index"""
    if len(events) == 0:
        return []
    if index is None:
        index = set()

    merged = None
    tried_pairs = set()
    for event in events:
        for i, actor in enumerate(actors):
            pair = (event, actor)
            if pair not in index and pair not in tried_pairs:
                new_index = index.union([pair])
                rest = merge(events[1:], actors[:i]+actors[i+1:], new_index)

                if rest is not None:
                    # Found! Done.
                    merged = [pair] + rest
                    break
                else:
                    tried_pairs.add(pair)
        if merged is not None:
            break

    return merged

if __name__ == '__main__':
    _events = ['P1','P1','P1','P2','P2','P2','P3','P3','P3','P4','P5','P6','P7','P7']
    _actors = ['IE','IE','ID','ID','IA','IA','IA','IC','IB','IF','IG','IH','IH','IA']
    shuffle(_events)
    edgelist = merge(_events, _actors)

    if edgelist is not None:
        print 'Solution obtained: '
        for item in edgelist:
            print item
    else:
        print 'No possible solution with these events and actors.'

rawDescription: How to merge two elements of two lists in python at random and ensure resulting entries are all unique?

description: Merge two elements of two lists in python at random and ensure resulting entries are all unique


code:
lines = "Foo\nBar\nBaz"

for line in lines.splitlines():
    print "%s Hello" % line

rawDescription: How do I put string at the end of every line?

description: Put string at the end of every line


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

D = 12.96
n = np.arange(1,7)

F0 = 1.0
x = np.linspace(0.001,4,2000)
Q = 2*np.pi*n/D
I = (11.159, 43.857, 26.302, 2.047, 0.513, 0.998)
phase = np.genfromtxt('3_phases.txt')

D2 = 12.3
I2 = (9.4, 38.6, 8.4, 3.25, 0, 0.37)
Q2 = 2*np.pi*n/D2
n2 = np.arange(1,7)

for row in phase:

    F = (np.sqrt(np.square(n)*I/sum(I)))*row
    d = sum(i*(np.sin(x*D/2+np.pi*j)/(x*D/2+np.pi*j))for i,j in zip(F,n))
    e = sum(i*(np.sin(x*D/2-np.pi*j)/(x*D/2-np.pi*j))for i,j in zip(F,n))
    f_0 = F0*(np.sin(x*D/2)/(x*D/2))
    F_cont = np.array(d) + np.array(e) + np.array(f_0)
    plt.plot(x,F_cont,'r')
    plt.clf()

    F2 = (np.sqrt(np.square(n2)*I2/sum(I2)))*row
    plt.plot(Q2,F2,'o')
    F_data = F2
    Q_data = Q2
    I_data = np.around(2000*Q2/(4-0.001))
    I_data = np.array(map(int,I_data))
    F_fit = F_cont[I_data]
    print F_fit
    R2 = (1-(sum(np.square(F_data-F_fit))/sum(np.square(F_data-np.mean(F_data)))))

rawDescription: Python Numpy Array indexing

description: Python Numpy Array indexing


code:
class A:
  def meth(self):
    print "In method."
a = A()
a.meth
fn = A.__dict__.pop('meth')
hasattr(a, 'meth')
a.meth
fn()
fn(a)

rawDescription: How do you hide from hasattr?

description: Hide from hasattr


code: print 'Hello dear user, your name is %(first_name)s %(last_name)s' % params

rawDescription: Arguments in the middle of a string to be localized


code:
import lxml.etree as ET
et = ET.parse('file.xml')
output = StringIO.StringIO()
et.write_c14n(output)
print output.getvalue()

rawDescription: Python Exclusive XML Canonicalization (xml-exc-c14n)


code:
def test(check):
    try:
        for i in L:
            if i == check:
                print 'True'
            else:
                print 'False'
            # print cmp(i,check)
    except:
        pass
test('c')

rawDescription: List validation one by one

description: List validation one by one


code:
import sys
from testdbtableform import *
from PyQt4 import QtSql, QtGui, QtCore, QtSql

def createConnection():
    db = QtSql.QSqlDatabase.addDatabase('QSQLITE')
    db.setDatabaseName('demomap.db')
    if db.open():
        return True
    else:
        print db.lastError().text()
        return False

class MyForm(QtGui.QDialog):

    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_Dialog()
        self.ui.setupUi(self)
        self.model = QtSql.QSqlTableModel(self)
        self.model.setTable("userlist")
        self.model.setEditStrategy(2)
        self.model.select()
        self.ui.tableView.setModel(self.model)
        self.ui.Submit.clicked.connect(self.dbinput)

    def dbinput(self):
        self.model.insertRow(-1)
        text = self.ui.lineEdit.text()
        if self.model.setData(self.model.index(-1, 0), text):
            self.model.submitAll()
        else:
            print "There was a problem setting the data."

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

rawDescription: Use QlineEdit to udpate SQLite database in PyQt


rawDescription: Compare a string to an integer

description: Compare a string to an integer


code:
import glob2
import os
bam_dirs = {os.path.dirname(p) for p in glob2.glob('/data2/**/*.bam')}
print bam_dirs

rawDescription: How can I recursively find the *directories* containing a file of a certain type?

description: Recursively find the *directories* containing a file of a certain type


code:
import re
atomic_wt = {'Na':22.99, 'Cl':35.45, 'H':1.008, 'O':16}
input = raw_input()
elementList = (re.findall('[A-Z][^A-Z]*', input))
wt_list = []

for element_withcoeff in elementList:
    #Separate the coefficients
    coeffList = re.findall(r'\d+', element_withcoeff)
    if len(coeffList) < 1:
        coeff = 1 #If no coefficent is defined use 1 as default
    else:
        coeff = int(coeffList[0])

    #Separate the element name for dictionary lookup
    element = filter(lambda c: not c.isdigit(), element_withcoeff)
    print coeff, element

    #Calculate using coeff
    elem_wt = coeff * atomic_wt[element]
    wt_list.append(elem_wt)

print "%sg" % sum(wt_list)

rawDescription: How do I separate coefficients from their variables in a list?

description: Separate coefficients from their variables in a list


code:
import time

def abc():
    print 'Hi'
    print 'Hello'
    print 'Hai'

for i in xrange(3):
    time.sleep(1)
    abc()

rawDescription: Python timer countdown

description: Python timer countdown


code:
from datetime import datetime
class Venue(object):
    """Collection of events"""
    def __init__(self):
        self.events_list = []

    def add_events(self,*args):
        for e in args:
            self.events_list.append(e)

    def sort_list(self):
        self.events_list.sort(key=lambda e: e.start_date)

    def find_spare_times(self):
        self.sort_list()
        for event in self.events_list:
            yield event.start_date
            yield event.end_date

    def add_spare_events(self):
        l = list(self.find_spare_times())
        print l
        new_start,new_end = None, None
        for e in l[1:]:
            if new_start:
                new_end = e
                self.add_events(Event(new_start,new_end,res=False))
                new_start = None
            else:
                new_start = e
                new_end = None

    def get_events(self):
        return self.events_list

class Event(object):
    """Holds the data for an individual event"""
    def __init__(self,start_date, end_date,res=True):
        self.start_date = start_date
        self.end_date = end_date
        self.reserved = res

# create a couple of events with some spare time between them...
def main():
    ev1 = Event(datetime(2011,1,1,5),datetime(2011,1,1,6))
    ev2 = Event(datetime(2011,1,1,8),datetime(2011,1,1,9))

    v = Venue()
    v.add_events(ev1,ev2)
    print v.get_events()
    v.add_spare_events()
    print v.get_events()

if __name__ == "__main__":
    main()

rawDescription: Find 'spare' time in list of python datetime objects


code:
def isPrime(n):
    if n == 2: return True
    if n % 2 == 0 or n < 2: return False
    for i in range(3, int(n**0.5)+1, 2):
        if n % i == 0: return False
    return True
if __name__ == "__main__":
    n = count = 1
    while count < 10001:
        n += 2
        if isPrime(n): count += 1
    print n

rawDescription: prime number generator taking too much time

description: Prime number generator taking too much time


code:
import spynner

if __name__=='__main__':
    url = "http://angular.github.com/angular-phonecat/step-10/app/#/phones"
    browser = spynner.Browser()
    browser.create_webview(True)
    browser.load(url, load_timeout=60)
    print browser._get_html()
    # ^ Can pipe this to a file, POST it to my server or return it as a string
    browser.close()

rawDescription: Render JavaScript to HTML in Python?

description: Render JavaScript to HTML in Python


rawDescription: What is the equivalent python script of Linux command "strings"?


code:
import requests
import os
import wx
import wx.lib.scrolledpanel as scrolled

from threading import Thread
from wx.lib.pubsub import pub

########################################################################
class DownloadThread(Thread):
    """Downloading thread"""

    #----------------------------------------------------------------------
    def __init__(self, gnum, url, fsize):
        """Constructor"""
        Thread.__init__(self)
        self.fsize = fsize
        self.gnum = gnum
        self.url = url
        self.start()

    #----------------------------------------------------------------------
    def run(self):
        """
        Run the worker thread
        """
        local_fname = os.path.basename(self.url)
        count = 1
        while True:
            if os.path.exists(local_fname):
                tmp, ext = os.path.splitext(local_fname)
                cnt = "(%s)" % count
                local_fname = tmp + cnt + ext
                count += 1
            else:
                break
        req = requests.get(self.url, stream=True)
        total_size = 0
        print local_fname
        with open(local_fname, "wb") as fh:
            for byte in req.iter_content(chunk_size=1024):
                if byte:
                    fh.write(byte)
                    fh.flush()
                total_size += 1024
                if total_size < self.fsize:
                    wx.CallAfter(pub.sendMessage,
                                 "update_%s" % self.gnum,
                                 msg=total_size)
        print "DONE!"
        wx.CallAfter(pub.sendMessage,
                     "update_%s" % self.gnum,
                     msg=self.fsize)

########################################################################
class MyGauge(wx.Gauge):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, range, num):
        """Constructor"""
        wx.Gauge.__init__(self, parent, range=range)

        pub.subscribe(self.updateProgress, "update_%s" % num)

    #----------------------------------------------------------------------
    def updateProgress(self, msg):
        """"""
        self.SetValue(msg)

########################################################################
class MyPanel(scrolled.ScrolledPanel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        scrolled.ScrolledPanel.__init__(self, parent)

        self.data = []
        self.download_number = 1

        # create the sizers
        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
        dl_sizer = wx.BoxSizer(wx.HORIZONTAL)

        # create the widgets
        lbl = wx.StaticText(self, label="Download URL:")
        self.dl_txt = wx.TextCtrl(self)
        btn = wx.Button(self, label="Download")
        btn.Bind(wx.EVT_BUTTON, self.onDownload)

        # layout the widgets
        dl_sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5)
        dl_sizer.Add(self.dl_txt, 1, wx.EXPAND|wx.ALL, 5)
        dl_sizer.Add(btn, 0, wx.ALL, 5)
        self.main_sizer.Add(dl_sizer, 0, wx.EXPAND)

        self.SetSizer(self.main_sizer)
        self.SetAutoLayout(1)
        self.SetupScrolling()

    #----------------------------------------------------------------------
    def onDownload(self, event):
        """
        Update display with downloading gauges
        """
        url = self.dl_txt.GetValue()
        try:
            header = requests.head(url)
            fsize = int(header.headers["content-length"]) / 1024

            sizer = wx.BoxSizer(wx.HORIZONTAL)
            fname = os.path.basename(url)
            lbl = wx.StaticText(self, label="Downloading %s" % fname)
            gauge = MyGauge(self, fsize, self.download_number)

            sizer.Add(lbl, 0, wx.ALL|wx.CENTER, 5)
            sizer.Add(gauge, 0, wx.ALL|wx.EXPAND, 5)
            self.main_sizer.Add(sizer, 0, wx.EXPAND)

            self.Layout()

            # start thread
            DownloadThread(self.download_number, url, fsize)

            self.download_number += 1
        except Exception, e:
            print "Error: ", e

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

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Downloader", size=(800, 400))
        panel = MyPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = DownloaderFrame()
    app.MainLoop()

rawDescription: wxPython threading downloads

description: WxPython threading downloads


rawDescription: Removing Duplicates From Dictionary

description: Removing Duplicates From Dictionary


code:
def dataHeader(columns_no):
    columns = []
    for i in range(columns_no):
        text = raw_input("Enter column #%d name> " % i)
        columns.append(text)
    return columns

def dataBody(columns_no):
    segmentLines = []
    for line in fileData.readlines():
        segmentLine = line.split(',')
        segmentLines.append(segmentLine[:columns_no])
    return segmentLines

def dataPrinter():
    columns_no = 6
    gen1 = dataHeader(columns_no)
    gen2 = dataBody(columns_no)
    print ','.join(gen1)
    for row in gen2:
        print ','.join(row)

dataPrinter()

rawDescription: Inserting column name headers into a Python list


code:
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason

rawDescription: How To Capture Output of Curl from Python script


code:
from itertools import groupby

money = money_tx.objects.order_by('-tx_date')
plans = investment_plan.objects.select_related()
result = []

for date, group in groupby(money, lambda x: x.tx_date):
    plans_for_date = set(x.investment_plan_id for x in group)
    for plan in plans:
        data = {'ORG': plan.organization.organization_name,
                'INV_PLAN': plan.plan_code,
                'DATE': date,
                'PRESENT': (plan.id in plans_for_date)}
        result.append(data)

print result

rawDescription: pickup "unique item" of a field in a django model using python

description: Pickup "unique item" of a field in a django model using python


code: print u'Hello, 你好, bye.'.encode('unicode-escape')

rawDescription: Python convert file content to unicode form


code:
s = "Hello I am the input string"
columns = 5
seq = [s[i::columns] for i in range(columns)]
print seq
print "\n".join(seq)

rawDescription: Create a string with every nth character of another string in Python


code:
from datetime import datetime
dt = datetime.strptime('2015-04-08T07:52:00Z', '%Y-%m-%dT%H:%M:%SZ')
print dt.strftime('%d/%m/%Y')

rawDescription: How I do convert from timestamp to date in python?


code:
import requests

URL = 'http://www.famjia.com/portal/intranet/famjiaPaper/'
LOGIN_URL = 'http://www.famjia.com/portal/login'

session = requests.session()

login_data = {'initialURI': '/portal',
              'loginname': '',
              'loginpassword': '',
              'username': 'YOUR USERNAME HERE',
              'password': 'YOUR PASSWORD HERE'}
session.post(LOGIN_URL, data=login_data)

req = session.get(URL)
print req.content

rawDescription: python get html page after login

description: Python get html page after login


code:
def deleteitem():
  showlist()

  with open("todo.txt") as f:
    lines = f.readlines()
  if len(lines) == 0:  # completely empty file
    return  # handle appropriately
  prompt = "Enter number to delete (1-%d), or 0 to abort: " % len(lines)
  while True:
    input = raw_input(prompt)
    try:
      input = int(input, 10)
    except ValueError:
      print "Invalid input."
    else:
      if 0 <= input <= len(lines):
        break
      print "Input out of range."
  if input == 0:
    return
  input -= 1  # adjust from [1,len] to [0,len)

  #del lines[input]  # if you want to remove that line completely
  lines[input] = "\n"  # or just make that line blank (what you had)

  with open("todo.txt", "w") as f:
    f.writelines(lines)

  showlist()

rawDescription: Python: list assignment out of range

description: Python: list assignment out of range


code:
def example(arg_list, res=[]):
    if arg_list == []:
        print res
        return
    for i in arg_list[0]:
        example(arg_list[1:], res+[i])

rawDescription: nesting to a dynamic depth in Python

description: Nesting to a dynamic depth in Python


code:
#!/usr/bin/env python

import sys
import getpass
import telnetlib

HOST = "192.168.1.5"

def main():
    ports = ['6023']
    if len(sys.argv) > 1:
        ports = sys.argv[1].split(',')

    for port in ports:
        get_stats(port)

def get_stats(port):
    tn = telnetlib.Telnet(HOST, port)
    tn.read_until('>>>')
    tn.write("spider.name\n")
    print tn.read_until('>>>')
    tn.close()

if __name__ == '__main__':
    main()

rawDescription: Python telnetlib to connect to Scrapy Telnet to read stats


rawDescription: Python/Pandas - Convert type from pandas period to string


code:
# Use hivex to pull out a registry key.
h = hivex.Hivex ("/tmp/ntuser.dat")

key = h.root ()
key = h.node_get_child (key, "Software")
key = h.node_get_child (key, "Microsoft")
key = h.node_get_child (key, "Internet Explorer")
key = h.node_get_child (key, "Main")

val = h.node_get_value (key, "Start Page")
start_page = h.value_value (val)
#print start_page

# The registry key is encoded as UTF-16LE, so reencode it.
start_page = start_page[1].decode ('utf-16le').encode ('utf-8')

print "User %s's IE home page is %s" % (username, start_page)

rawDescription: Is there a pure Python library for parsing a Windows Registry file?


code:
for tweet in tweets:
    a = re.findall(r"(?<=@)\w+", str(tweet[1]))
    for i in a:
        print tweet[0]+',', '@'+i

rawDescription: split multi-valued list and fill down

description: Split multi-valued list and fill down


rawDescription: Satisfying a Condition Among List of Lists of Arbitrary Length


code:
from pyparsing import *

text = 'abc123xyz456def789'
foo = Word(alphas)
bar = Word(nums)
fun = OneOrMore(Group(foo.setResultsName("foo") + bar.setResultsName("bar")))

results = fun.parseString(text)

#Print all foo
print [r.foo for r in results]
#Print all bar
print [r.bar for r in results]

rawDescription: acccumulate using pyparsing

description: Acccumulate using pyparsing


code:
import pandas as pd
import random
import numpy as np

#created a dump data as you didn't provide one
df = pd.DataFrame({'col1':  [random.getrandbits(1) for i in range(10)], 'col2':  [random.getrandbits(1) for i in range(10)], 'col3': [1]*10})

#You can select the value directly by using ix[] operator
row_indexer,column_indexer=3,1
print df.ix[row_indexer,column_indexer]

#You can filter the data of a specific column this way
print df[df['col1']==1]
print df[df['col2']==1]

#df.iloc to select by postion .loc to  Selection by Label

#want to be able to select rows from a list of values and at the same time select for any column in which each of the selected rows has a value of one.
print df[(df.T == 1).any()]
# if you wanna filter a specific columns with a condition on rows
print df[(df['col1']==1)|(df['col2']==1)]
#To make it more complex I also want to select rows from a list of values where all values in a column for these rows is zero.
print df[(df.T == 0).all()]
# if you wanna filter a specific columns with a condition on rows
print df[(df['col1']==0) & (df['col2']==0)]

rawDescription: How to search pandas data frame by index value and value in any column

description: Search pandas data frame by index value and value in any column


code:
data = """University of example1
Assistent professor, hello, University of example2
Hello, University of example3"""

data = data.replace("\n",",")   #All one line, split with commas
slices = data.split(",")        #Get each slice
for slice in slices:            #Go through each slice
    if "University" in slice:   #check for magic word
        print slice.strip()     #print out what matches, remove trailing and leading white space

rawDescription: Python dynamically select values from comma separated values


code:
if all( x in 'hello world' for x in ['hel','ell','orl'] ):
    print "Found all of them"

rawDescription: find multiple string occurrences in Python

description: Find multiple string occurrences in Python


code:
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('Hello %s!' % self.client_address[0])
        return

def main():
    try:
        server = HTTPServer(('', 80), MyHandler)
        print 'started http server'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

rawDescription: How to create a custom webpage?

description: Create a custom webpage


code:
import re

a = dict(int='111', b=r'aaabbb#int##int#*')
expected_a = {'b': 'aaabbb(111)(111)*', 'int': '111'}
a['b'] = re.sub(r'#.*?#', '(%s)' % a['int'], a['b'])

print a == expected_a # True

rawDescription: how to use re.compile and re.sub in python?

description: Use re.compile and re.sub in python


code:
import re

from bs4 import BeautifulSoup
import requests

response = requests.get('http://www.europarl.europa.eu/oeil/search/search.do?searchTab=y')
soup = BeautifulSoup(response.content)

# get the results count
num_results = soup.find('span', class_=re.compile('resultNum')).text
num_results = int(re.search('(\d+)', num_results).group(1))
print "Results found: " + str(num_results)

results_per_page = 50
base_url = "http://www.europarl.europa.eu/oeil/search/result.do?page={page}&rows=%s&sort=d&searchTab=y&sortTab=y&x=1411566719001" % results_per_page

links = []
for page in xrange(1, num_results/results_per_page + 1):
    print "Current page: " + str(page)

    url = base_url.format(page=page)
    response = requests.get(url)

    soup = BeautifulSoup(response.content)
    links += [a.attrs.get('href') for a in soup.select('div.procedure_title a')]

print links

rawDescription: Display all search results when web scraping with Python


code:
df_stack = df.set_index('gene').stack()
df_top = df_stack.loc[df_stack.rank('min', ascending=False) <= 5]
print df_top.reset_index(0).groupby('gene').groups

rawDescription: How to identify row and column based on top-K values in Pandas data frame

description: Identify row and column based on top-K values in Pandas data frame


code:
import sys
from PyQt4 import QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.button = QtGui.QPushButton("Make child")
        self.button.clicked.connect(self.openChildWindow)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def openChildWindow(self):
        self.childWindow = ChildWindow(self)
        self.childWindow.setGeometry(650, 350, 200, 300)
        self.childWindow.show()

    def printResult(self, value):
        print value

class ChildWindow(Window):
    def __init__(self, parentWindow):
        super(ChildWindow, self).__init__()
        self.parentWindow = parentWindow
        self.parentWindow.printResult("I'm in child init")
        self.button.setDisabled(1)

    def closeEvent(self, event):
        self.parentWindow.printResult("Closing child, this text is returned to parent")

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 300, 200, 300)
    window.show()
    sys.exit(app.exec_())

rawDescription: Value in MainWindow returned from childWindow


code:
target_list =['apples 1', 'oranges 1', 'bananas 2', 'apples 3', 'oranges 2','mango 3', 'apples 2']
source_list =  ['A apples', 'B mango', 'C apples', 'D bananas', 'E oranges','F apples', 'G oranges']

from collections import defaultdict

# you want each target fruit to be a group, so use them as keys in a dict
# use a defaultdict list so whenever you access a key that doesn't exist
# it creates an empty list at that key
td = defaultdict(list)

for item in target_list:
    key, value = item.split()
    # the value for each fruit is a list of the numbers associated with it
    td[key].append(value)

# for each source item find a match and pop a number from the list
# so that each pair gets a different number
for item in source_list:
    letter, key = item.split()
    if key in td:
        print key, letter, td[key].pop()

rawDescription: Match elements from one list to another, skip already-matched elements, Python


code:
from imapclient import IMAPClient

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

server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)

server.select_folder('INBOX')
messages = server.search(['X-GM-RAW has:attachment'])

print '%d messages with attachments' % (len(messages),)
print
print 'UIDs of messages with attachments:'
print messages

rawDescription: Search for messages with attachments with GMail IMAP


code:
import os

output = []

for _, _, files in os.walk(directory):
    for f in files:
        fullpath = os.path.join(directory, os.path.basename(f))
        output.append(subprocess.check_output([fullpath]))

print '\n'.join(output)

rawDescription: How to write the results of a batch file called in a Python script to a file

description: Write the results of a batch file called in a Python script to a file


code:
import itertools

d = {1:2, 2:3, 3:4}

for k0, k1 in itertools.combinations(d,2):
    print 'compare', k0, k1

rawDescription: Efficient comparison of all elements of python dict


code:
import urllib
import urllib2

url = 'http://example.com/...'
values = { 'productslug': 'bar','qty': 'bar' }
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
print result

rawDescription: python urllib2 post request

description: Python urllib2 post request


code:
import re

def hasDigit(s):
    return not not re.search("\d", s)

def Main():
    while True:
        Class_A_Input = raw_input('Enter Class A tickets sold: ')
        Class_B_Input = raw_input('Enter Class B tickets sold: ')
        Class_C_Input = raw_input('Enter Class C tickets sold: ')

        if all([hasDigit(Input) for Input in [Class_A_Input, Class_B_Input, Class_C_Input]]):
            break
        else:
            print 'Wrong'

rawDescription: Python - Check Multiple String Value For Chars


code:
class pattern_class(object):
    generic_attr = "bla"

def declare(classname):
    return type(classname, (pattern_class,), {'__tablename__' : classname+"_daily_history"})

class_list_names = ['rak', 'bak', 'sak', 'mak']
for name in class_list_names:
    cls = declare(name)
    globals()[name + '_entry'] = cls()

print rak_entry.__tablename__, bak_entry.__tablename__, sak_entry.__tablename__, mak_entry.__tablename__

rawDescription: Dynamic class declaration, using variable to name class and variable reference to dynamic class


code:
dt_start_time = "2014-09-23 11:00:00"
dt_end_time = "2014-09-24 23:00:00"

format = "%Y-%m-%d %H:%M:%S"

from datetime import datetime as dt

st = dt.strptime(dt_start_time, format)
end = dt.strptime(dt_end_time, format)

count = (end - st).days
if st.hour < 21 and end.hour >= 21:
    count += 1
print count

rawDescription: Calculate 9:00 PM in between two dates in python


code: print df.drop('Total', axis=1).divide(df.Total, axis=0)

rawDescription: Best Way to Calculate Column Percentage of Total Pandas


code:
#!/usr/bin/env python2

import os
import sys
import app
from models import YourModel as Model
from flask.ext.whooshalchemy import whoosh_index

sys.stdout  = os.fdopen(sys.stdout.fileno(), 'w', 0)
atatime     = 512

with app.app_context():
    index       = whoosh_index(app, Model)
    searchable  = Model.__searchable__
    print 'counting rows...'
    total       = int(Model.query.order_by(None).count())
    done        = 0
    print 'total rows: {}'.format(total)
    writer = index.writer(limitmb=10000, procs=16, multisegment=True)
    for p in Model.query.yield_per( atatime ):
        record = dict([(s, p.__dict__[s]) for s in searchable])
        record.update({'id' : unicode(p.id)}) # id is mandatory, or whoosh won't work
        writer.add_document(**record)
        done += 1
        if done % atatime == 0:
            print 'c {}/{} ({}%)'.format(done, total, round((float(done)/total)*100,2) ),

    print '{}/{} ({}%)'.format(done, total, round((float(done)/total)*100,2) )
    writer.commit()

rawDescription: Flask-WhooshAlchemy with existing database


code:
from time import time

prev = time()
while True:
    now = time()
    if now - prev > 3:
        print 'report'
        prev = now
    else:
        pass
        # runs

rawDescription: Does time.sleep() stop all executions?

description: Does time.sleep() stop all executions


code:
def mymkdir(*args):
        print "mkdir", args

os.mkdir = mymkdir # monkey patching os
os.mkdir("toto")

rawDescription: How can I conditionally intercept I/O done via os.path, shutil and subprocess modules?

description: Conditionally intercept I/O done via os.path, shutil and subprocess modules


code:
def fib_seq(n):
    if n == 0:
        return n
    elif n == 1:
        return n
    else:
        return fib_seq(n-1) + fib_seq(n-2)

def memoize(fn, arg):
 memo = {}
 if arg not in memo:
  memo[arg] = fn(arg)
  return memo[arg]

fibm = memoize(fib_seq,27)
print fibm

rawDescription: Why Wont Python Wont Do a Lot of Recursion?


code:
import re, os

def grep(regex, base_dir):
    compiled_regex = re.compile(regex, re.DOTALL)
    matches = list()
    for filename in os.listdir(base_dir):
        full_filename = os.path.join(base_dir, filename)
        if not os.path.isfile(full_filename):
            continue
        with open(os.path.join(base_dir, filename)) as fh:
            content = fh.read()
            if compiled_regex.search(content):
                matches.append(full_filename)
    return matches

print grep(" dojo.require(..*) ", ".")

rawDescription: Search directory for string

description: Search directory for string


code:
def mp_factorizer(nums):
    out_q = multiprocessing.Queue()

    def worker(num, out_q):
        outdict = {}
        outdict[num] = num * 2
        print outdict
        out_q.put(outdict)

    procs = []

    for num in nums:
        p = multiprocessing.Process(
                target=worker,
                args=(num, out_q)
        )
        procs.append(p)
        p.start()

    for p in procs:
        p.join()

    resultdict = {}
    for i in range(len(nums)):
        if not out_q.empty():
            resultdict.update(out_q.get())

    return resultdict

rawDescription: What am I doing wrong, in following Python program, as I'm getting global queue empty?


code:
files = ['text1.txt', 'text2.txt']

for (id, file) in enumerate(files, start=1):
    with open(file, 'r') as f:
        for line in f.xreadlines():
            print line.strip() + str(id)

rawDescription: print same ID for all the elements in text1 and then icrementing the ID and priting for text2. -Python

description: Print same ID for all the elements in text1 and then icrementing the ID and priting for text2. -Python


code:
import paho.mqtt.client as mqtt
import json
import datetime as dt

time_interval = dt.timedelta(minutes=5)

dic1 = {}

class EPC():
    def __init__(self,when):
        self.first_seen = when
        self.last_print = when
        self.last_seen = when

def on_message(client, userdata, msg):
    payloadjson = json.loads(msg.payload.decode('utf-8'))
    line = payloadjson["value"].split(',')
    epc =  line[1]
    rfid_input = "EPC_NO.: " + epc[11:35]
    when = dt.datetime.today()

    if rfid_input not in dic1:
        dic1[rfid_input] = EPC(when)
        print_all = True

    else:
        dic1[rfid_input].last_seen = when
        print_all = when-dic1[rfid_input].last_print > time_interval

    if print_all:
        for (key, value) in dic1.items():
            first_str = "%04d-%02d-%02d %02d:%02d:%02d"%(value.first_seen.year,value.first_seen.month,value.first_seen.day,value.first_seen.hour,value.first_seen.minute,value.first_seen.second)
            last_str = "%04d-%02d-%02d %02d:%02d:%02d"%(value.last_seen.year,value.last_seen.month,value.last_seen.day,value.last_seen.hour,value.last_seen.minute,value.last_seen.second)

            print "EPC_NO.: %06d    FIRST SEEN AT %s    LAST SEEN AT %s"%(key, first_str,last_str)
            value.last_print = when

rawDescription: How to implement a real time tracking mechanism in Python?

description: Implement a real time tracking mechanism in Python


code:
class MyList(list):

    def __getitem__(self, index):
        try:
            return super(MyList, self).__getitem__(index)
        except IndexError:
            return None

mylist = MyList([1,2,3,4,5])
print mylist[4] #prints 5
print mylist[6] is None #prints True

rawDescription: Python: How to avoid 'list assignment index out of range'


code:
def removeUnique(player,f_Type,num_seconds,ts):
   curTime = timestampToEpoch(ts)
   for e in uniqueFishes[player].copy():
      if e[1] >= curTime - num_seconds:
         uniqueFishes[player].remove(e)
   print uniqueFishes[player]

rawDescription: Python: Comparing values in a defaultdict(set)


code:
import numpy as np
import pandas as pd

# set seed
np.random.seed(42)

df = pd.DataFrame(np.random.random(12).reshape(-1,3))
print('Original')
print(df)

df['log'] = df[1].apply(np.log)
print('Modified')
print df

rawDescription: How to check the progress of a process in pandas?

description: Check the progress of a process in pandas


code:
import turtle

def position(x, y):
    print x, y

wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
tess.color("blue")
tess.shape("turtle")

wn.onscreenclick(position)
turtle.done()

rawDescription: Turtle: Call to onscreenclick not listening in Pycharm


code:
import wx

def toggle(window):
    print window.ToggleWindowStyle(wx.CLOSE_BOX)
    window.Refresh()
    wx.CallLater(2000, toggle, window)

def main():
    app = wx.App(None)
    window = wx.Dialog(None)
    toggle(window)
    window.ShowModal()
    window.Destroy()
    app.MainLoop()

if __name__ == '__main__':
    main()

rawDescription: Dynamically changing wx.Dialog wx.CLOSE_BOX style


code:
import psycopg2

query = """
    select *
    from driving_distance ($$
        select
            gid as id,
            start_id::int4 as source,
            end_id::int4 as target,
            shape_leng::double precision as cost
        from network
        $$, %s, %s, %s, %s
    )
;"""

conn = psycopg2.connect("dbname=cpn")
cur = conn.cursor()
cur.execute(query, (10, 1000000, False, False))
rs = cur.fetchall()
conn.close()
print rs

rawDescription: How to execute sql lines in postgresql using python？

description: Execute sql lines in postgresql using python？


code:
s = '45_cat_fish'
print s.split('_', 1)[1]
# >>> cat_fish

rawDescription: What's the most Pythonic way to remove a number from start of a string?


code:
import getopt, math, random, sys, time, types, wx, subprocess

from multiprocessing import Process, Queue, cpu_count, current_process, freeze_support
from Queue import Empty

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.Point(700, 500), wx.Size(300, 200))

        self.panel = wx.Panel(self, wx.ID_ANY)

        #widgets
        self.start_bt = wx.Button(self.panel, wx.ID_ANY, "Start")
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.start_bt)

        self.output_tc = wx.TextCtrl(self.panel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.TE_READONLY)

        # sizer
        self.sizer = wx.GridBagSizer(5, 5)
        self.sizer.Add(self.start_bt, (0, 0), flag=wx.ALIGN_CENTER|wx.LEFT|wx.TOP|wx.RIGHT, border=5)
        self.sizer.Add(self.output_tc, (1, 0), flag=wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM, border=5)
        self.sizer.AddGrowableCol(0)
        self.sizer.AddGrowableRow(1)
        self.panel.SetSizer(self.sizer)

        # Set some program flags
        self.keepgoing = True
        self.i = 0
        self.j = 0

    def OnButton(self, event):
        self.start_bt.Enable(False)

        self.numtasks = 4
        self.numproc = 2
        #self.numproc = cpu_count()
        self.output_tc.AppendText('Number of processes = %d\n' % self.numproc)

        # Create the queues
        self.taskQueue = Queue()
        self.outputQueue = Queue()

        # Create the task list
        self.Tasks = range(self.numtasks)

        # The worker processes...
        for n in range(self.numproc):
            process = Process(target=self.worker, args=(self.taskQueue, self.outputQueue))
            process.start()

        # Start processing tasks
        self.processTasks(self.update)

        if (self.keepgoing):
            self.start_bt.Enable(True)

    def processTasks(self, resfunc=None):
        self.keepgoing = True

        # Submit first set of tasks
        numprocstart = min(self.numproc, self.numtasks)
        for self.i in range(numprocstart):
            self.taskQueue.put(self.Tasks[self.i])

        self.j = -1 # done queue index
        self.i = numprocstart - 1 # task queue index
        while (self.j < self.i):
            # Get and print results
            self.j += 1
            output = None
            while output != 'STOP!':
                try:
                    output = self.outputQueue.get()
                    if output != 'STOP!':
                        resfunc(output)
                except Empty:
                    break

            if ((self.keepgoing) and (self.i + 1 < self.numtasks)):
                # Submit another task
                self.i += 1
                self.taskQueue.put(self.Tasks[self.i])

    def update(self, output):
        self.output_tc.AppendText('%s PID=%d Task=%d : %s\n' % output)
        wx.YieldIfNeeded()

    def worker(self, inputq, outputq):
        while True:
            try:
                tasknum = inputq.get()
                print '## Now Running: ', tasknum #this goes to terminal/console. Add it to outputq if you'd like it on the TextCtrl.
                command = ['./script.sh']
                p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                while True:
                    r = p.stdout.readline()
                    if not r:
                        outputq.put('STOP!')
                        break
                    outputq.put(( current_process().name, current_process().pid, tasknum, r.rstrip()))
            except Empty:
                break

    # The worker must not require any existing object for execution!
    worker = classmethod(worker)

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, -1, 'stdout to GUI using multiprocessing')
        self.frame.Show(True)
        self.frame.Center()
        return True

if __name__ == '__main__':
    freeze_support()
    app = MyApp(0)
    app.MainLoop()

rawDescription: How to capture output of a shell script running in a separate process, in a wxPython TextCtrl?

description: Capture output of a shell script running in a separate process, in a wxPython TextCtrl


code:
import sys
log = "".join([i for i in sys.stdin])
print log

rawDescription: How to read from STDIN in python from a piped grep output

description: Read from STDIN in python from a piped grep output


code:
import re
s1 = "thishasadigit4here"
m = re.search("\d", s1)
if m:
    print "Digit found at position %d" % m.start()
else:
    print "No digit in that string"

rawDescription: Python: find index of first digit in string?


code:
from socket import *
import threading
import time

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

    def run (self):
        for i in range(10):
          print 'start thread'
          cs = socket(AF_INET, SOCK_DGRAM)
          cs.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
          cs.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
          cs.sendto('This is a test', ('192.168.1.3', 4499))
          time.sleep(1)

a = PingerThread()
a.start()

cs = socket(AF_INET, SOCK_DGRAM)
try:
    cs.bind(('192.168.1.3', 4499))
except:
    print 'failed to bind'
    cs.close()
    raise
    cs.blocking(0)
data = cs.recvfrom(1024) # <-- waiting forever
print data

rawDescription: Broadcasting and receiving data with Python


code:
from djcelery.models import TaskMeta
task = TaskMeta.objects.get(task_id=task_id)
print task.status

rawDescription: Set / get objects with memcached

description: Set / get objects with memcached


code:
class FINISH_PROCESSING_SIGNAL(Exception): pass

def coroutine(func):
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start

# Sink
@coroutine
def list_builder():
    # accepts objects and adds them to a list
    _list = []
    try:
        while True:
            data = (yield)
            _list.append(data)
    except FINISH_PROCESSING_SIGNAL:
        yield _list

# Filter
@coroutine
def user_data_filter(target=list_builder()):
    header = "-+-"
    footer = "Transfer Packets"
    username = "User Name"
    fullname = "Full Name"
    note = "Description"
    try:
        while True:
            user = {}
            data = (yield)
            line = data
            if header in line:
                while True:
                    line = (yield)
                    if footer in line:
                        target.send(user)
                        break
                    elif username in line:
                        user["username"] = line.split('|')[1]
                    elif fullname in line:
                        user["fullname"] = line.split('|')[1]
                    elif note in line:
                        user["note"] = line.split('|')[1]
    except FINISH_PROCESSING_SIGNAL as err:
        # Pass along the Exception to the target, and yield its result back
        # to the caller
        yield target.throw(err)

# Producer
def process_users_table(table, target=user_data_filter()):
    lines = table.split('\r\n')
    for line in lines:
        target.send(line)
    processed_data = target.throw(FINISH_PROCESSING_SIGNAL)
    # processed_data = target.close()
    return processed_data

if __name__ == '__main__':

    test_users_table = \
    """
    Item            |Value\r\n
    ----------------+-----------------------\r\n
    User Name       |alice\r\n
    Full Name       |Alice Doe\r\n
    Description     |\r\n
    Transfer Packets|0\r\n
    ----------------+-----------------------\r\n
    User Name       |bob\r\n
    Full Name       |Bob Tables\r\n
    Description     |\r\n
    Transfer Packets|0\r\n
    """

    users = process_users_table(test_users_table)
    print users

rawDescription: Is there a standard approach to returning values from coroutine endpoints


code:
def _bootstrap(self):
    from . import util
    global _current_process

    try:
        self._children = set()
        self._counter = itertools.count(1)
        try:
            sys.stdin.close()
            sys.stdin = open(os.devnull)
        except (OSError, ValueError):
            pass
        _current_process = self
        util._finalizer_registry.clear()
        util._run_after_forkers()
        util.info('child process calling self.run()')
        try:
            self.run()
            exitcode = 0
        finally:
            util._exit_function()
    except SystemExit, e:
        if not e.args:
            exitcode = 1
        elif type(e.args[0]) is int:
            exitcode = e.args[0]
        else:
            sys.stderr.write(e.args[0] + '\n')
            sys.stderr.flush()
            exitcode = 1
    except:
        exitcode = 1
        import traceback
        sys.stderr.write('Process %s:\n' % self.name)
        sys.stderr.flush()
        traceback.print_exc()

    util.info('process exiting with exitcode %d' % exitcode)
    return exitcode

rawDescription: python process._sbootstrap() / .start()

description: Python process._sbootstrap() / .start()


code:
from requests import async
# If using requests > v0.13.0, use
# from grequests import async

urls = [
    'http://python-requests.org',
    'http://httpbin.org',
    'http://python-guide.org',
    'http://kennethreitz.com'
]

# A simple task to do to each response object
def do_something(response):
    print response.url

# A list to hold our things to do via async
async_list = []

for u in urls:
    # The "hooks = {..." part is where you define what you want to do
    #
    # Note the lack of parentheses following do_something, this is
    # because the response will be used as the first argument automatically
    action_item = async.get(u, hooks = {'response' : do_something})

    # Add the task to our list of things to do via async
    async_list.append(action_item)

# Do our list of things to do via async
async.map(async_list)

rawDescription: Asynchronous Requests with Python requests


code:
from flask_security import current_user, auth_token_required

@app.route('/')
@auth_token_required
def view():
    print current_user.id

rawDescription: flask security auth_token_required - token authentication

description: Flask security auth_token_required - token authentication


code:
import argparse
# Define the parser
parser = argparse.ArgumentParser(description='Short sample app')
# Declare an argument (`--algo`), telling that the corresponding value should be stored in the `algo` field, and using a default value if the argument isn't given
parser.add_argument('--algo', action="store", dest='algo', default=0)
# Now, parse the command line arguments and store the values in the `args` variable
args = parser.parse_args()
# Individual arguments can be accessed as attributes...
print args.algo

rawDescription: Pass arguments from cmd to python script


code:
import urllib2

try:
    response = urllib2.urlopen('https://example.com')
    print 'response headers: "%s"' % response.info()
except IOError, e:
    if hasattr(e, 'code'): # HTTPError
        print 'http error code: ', e.code
    elif hasattr(e, 'reason'): # URLError
        print "can't connect, reason: ", e.reason
    else:
        raise

rawDescription: Does urllib encrypt authentication data?

description: Does urllib encrypt authentication data


rawDescription: Find previous calendar day in python

description: Find previous calendar day in python


code:
import paramiko
client=paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
com="ls ~/desktop"
client.connect('MyIPAddress',MyPortNumber, username='username', password='password')
output=""
stdin, stdout, stderr = client.exec_command(com)

print "ssh succuessful. Closing connection"
stdout=stdout.readlines()
client.close()
print "Connection closed"

print stdout
print com
for line in stdout:
    output=output+line
if output!="":
    print output
else:
    print "There was no output for this command"

rawDescription: Paramiko not outputting stdout

description: Paramiko not outputting stdout


code:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome(desired_capabilities=d)
# load some site
driver.get('http://foo.com')
# print messages
for entry in driver.get_log('browser'):
    print entry

rawDescription: Getting console.log output from Chrome with Selenium Python API bindings


code:
import time
import subprocess
from multiprocessing import Process

cmd1 = ['nice', 'time', 'java', '-Xmx6G', '-jar', '/comparison/old_picard/MarkDuplicates.jar', 'I=/comparison/old.bam', 'O=/comparison/old_picard/markdups/old.dupsFlagged.bam', 'M=/comparison/old_picard/markdups/old.metrics.txt', 'TMP_DIR=/comparison', 'VALIDATION_STRINGENCY=LENIENT', 'ASSUME_SORTED=true']
cmd2 = ['nice', 'time', 'java', '-Xmx6G', '-jar', '/comparison/new_picard/MarkDuplicates.jar', 'I=/comparison/new.bam', 'O=/comparison/new_picard/markdups/new.dupsFlagged.bam', 'M=/comparison/new_picard/markdups/new.metrics.txt', 'TMP_DIR=/comparison', 'VALIDATION_STRINGENCY=LENIENT', 'ASSUME_SORTED=true']

def timeit(cmd):
    print cmd
    past = time.time()
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
    results = [p.communicate()]
    present = time.time()
    total = present - past
    results.append(total)
    return results

p1 = Process(target=timeit, args=(cmd1,))
p2 = Process(target=timeit, args=(cmd2,))

for p in (p1, p2):
    p.start()
for p in (p1, p2):
    p.join()

rawDescription: Multiprocessing and Multithreading

description: Multiprocessing and Multithreading


code:
from random import *

legislators = { "Tsang Yok-sing" : "DAB", "Albert Ho" :
"Democratic", "Lee Cheuk-yan" : "Labour", "James To" :
"Democratic", "Chan Kam-lam" : "DAB", "Lau Wong-fat" :
"Economic Synergy", "Emily Lau" : "Democratic" }

names = list(legislators.keys())

parties = list(legislators.values())

numberCorrect = 0

for i in range(0, 5):

    name = names[randrange(len(names))]

    party = parties[randrange(len(parties))]

    ans = raw_input("Does "+name+" belong to "+party+ " Y/N?")

    if ans == 'Y':

        if legislators[name] == party:

            print 'correct'

        else:
            print 'error'

    elif ans == 'N':

        if legislators[name] == party:

            print 'error'
        else:

            print 'correct'

rawDescription: Need to check if an answer in a quiz is correct (Python)


code:
d = {'a': 1, 'b': {'sa': 11, 'sb': 22, 'sc': {'ssa': 111, 'ssb': 222}}, 'c': 3}

def dotter(d, key, dots):

    if isinstance(d, dict):
        for k in d:
            dotter(d[k], key + '.' + k if key else k, dots)
    else:
        dots.append(key)

    return dots

print dotter(d, '', [])

rawDescription: How can I get a list of nested dictionary keys as dot separated strings?

description: Get a list of nested dictionary keys as dot separated strings


code:
#!/usr/bin/env python
import threading
import socket
import time
import errno

class StoppableThread(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def stop(self):
        if self.isAlive() == True:
            # set event to signal thread to terminate
            self.stop_event.set()
            # block calling thread until thread really has terminated
            self.join()

class Accept(StoppableThread):
    def __init__(self, port):
        StoppableThread.__init__(self)
        self.port = port
        self.threads = []

    def run(self):
        # handle connection acception
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.bind(('', self.port ))
        conn.listen(5)
        # set socket timeout to ~10ms
        conn.settimeout(0.01)
        while self.stop_event.is_set() == False:
            try:
                csock, caddr = conn.accept()
                # spawn a new thread to handle the client connection
                listen_thread = Listen(csock, caddr)
                self.threads.append(listen_thread)
                listen_thread.start()
            except socket.timeout:
                # socket operation timeout
                # clear all terminated threads from thread list
                for thread in self.threads:
                    if thread.isAlive() == False:
                        self.threads.remove(thread)

        self.stop_threads()

    def stop_threads(self):
        # stop all running threads
        for listen_thread in self.threads:
            if listen_thread.isAlive() == True:
                listen_thread.stop()
        self.threads = []

class Listen(StoppableThread):
    def __init__(self, csock, caddr):
        StoppableThread.__init__(self)
        self.csock = csock
        self.caddr = caddr
        self.csock.setblocking(False)

    def run(self):
        while self.stop_event.is_set() == False:
            try:
                recv_data = self.csock.recv(250)
                if len(recv_data) > 0:
                    print str(self.caddr)+": " + recv_data
                    self.csock.send("got it")
                else:
                    # connection was closed by foreign host
                    self.stop_event.set()
            except socket.error as (sock_errno, sock_errstr):
                if (sock_errno == errno.EWOULDBLOCK):
                    # socket would block - sleep sometime
                    time.sleep(0.1)
                else:
                    # unexpected / unhandled error - terminate thread
                    self.stop_event.set()
        channel.close()

class Shout(StoppableThread):
    def __init__(self, sport):
        StoppableThread.__init__(self)
        self.sport = sport

    def run(self):
        while self.stop_event.is_set() == False:
            try:
                address = raw_input("who u talking to? ")
                conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                conn.connect((address, self.sport))
                break
            except socket.error:
                # handle connection problems
                print "can't connect to "+ str(address)
            except:
                # exit thread in case of an unexpected error
                self.stop_event.set()

        while self.stop_event.is_set() == False:
            try:
                # chat loop: send messages to remote host
                print "what to send? :",
                msg = raw_input()
                # beware: send() function may block indefinitly here and it might not send all bytes as expected !!
                conn.send(msg)
            except:
                # exit thread in case of an unexpected error
                self.stop_event.set()
        # close socket before thread terminates
        conn.close()

def main():
    do_exit = False
    server_port = 2727

    # start server socket thread
    accept = Accept(server_port)
    accept.start()

    # start transmitting client socket thread
    shout = Shout(server_port)
    shout.start()

    while do_exit == False:
        try:
            # sleep some time
            time.sleep(0.1)
        except KeyboardInterrupt:
            # Ctrl+C was hit - exit program
            do_exit = True

    # stop all running threads
    shout.stop()
    accept.stop()

    # exit main program after all threads were terminated gracefully

if __name__ == "__main__":
    main()

rawDescription: How to exit a multithreaded program?

description: Exit a multithreaded program


code:
import re

s = 'abaabbab'
print re.sub('[ab]', lambda L: {'a': 'b', 'b': 'a'}[L.group()], s)
# babbaaba

rawDescription: Switch chars with regex

description: Switch chars with regex


code:
lead = False

for line in file_to_use:
    if re.search ("LEAD", line):
        lead = True
    if re.search ("To the Editor:", line):
        lead = False

    if lead == False:
        print line

rawDescription: Searching between targets in python

description: Searching between targets in python


code:
tup1 = ( ('AAA', 2), ('BBB', 3) )
tup2 = ('AAA', 'BBB', 'CCC', 'DDD')
dic = dict( tup1 )
for tri in tup2:
    dic[tri] = dic.get(tri,0)

print tuple(dic.items())
#(('AAA', 2), ('BBB', 3), ('CCC', 0), ('DDD', 0))

rawDescription: How to compare these 2 python tuple of tuples of unequal length and output another tuple based on the comparison?

description: Compare these 2 python tuple of tuples of unequal length and output another tuple based on the comparison


rawDescription: How to check if elements of one list/dict present in another list/dict in python

description: Check if elements of one list/dict present in another list/dict in python


code:
import pandas as pd
import sqlalchemy as sql
import sqlalchemy.orm as orm

sh = pd.ExcelFile("ExcelFileName").parse("SheetName")
db=sql.create_engine('DBtype://username:password@localhost:postnumber/dbname')
metadata = sql.schema.MetaData(bind=db,reflect=True)

table = sql.Table('TableinDB', metadata, autoload=True)

class Row(object):
 pass
rowmapper = orm.Mapper(Row,table)
print table.columns

Sess = orm.sessionmaker(bind = db)
session = Sess()

for x in range(len(sh.values)):
 row1 = Row()
 row1.field1 = sh.values[x][0]
 row1.field2 = sh.values[x][1]
 #Add as many fields as you want
session.add(row1)
#forloop ends
session.commit()

rawDescription: Iteratively insert rows into a postgreSQL database using SQLAlchemy in python


code:
from bs4 import BeautifulSoup
import urllib2
text = urllib2.urlopen("http://yourwebpage.com/").read()
soup = BeautifulSoup(text)
img = soup.find_all('img',{'id':'next'})
if img:
    a_tag = img[0].parent
    href = a_tag.get('href')
    print href

rawDescription: Click an image which has a specific name


code:
import networkx as nx
edges = """A1 - B1
A2 - B2
A2 - B1
A3 - B1
A4 - B2
A5 - B3
A6 - B3
A7 - B3
A7 - B3
A8 - B4
A9 - B3""".split('\n')
G = nx.parse_edgelist(edges,delimiter=' - ')
for component in nx.connected_components(G):
    print [n for n in component if n.startswith('A')]

rawDescription: Searching for individual bipartite networks


code:
process = subprocess.Popen(cmd, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in iter(process.stdout.readline, ''):
    print line,
    sys.stdout.flush() # please see comments regarding the necessity of this line
process.wait()
errcode = process.returncode

rawDescription: Output of subprocess both to PIPE and directly to stdout


rawDescription: Merge two Python list of tuples

description: Merge two Python list of tuples


code:
from scapy.all import *

def ip_proto(pkt):
    proto_field = pkt.get_field('proto')
    return proto_field.i2s[pkt.proto]

x = IP() / TCP()
print ip_proto(x)

rawDescription: IP proto field number to string

description: IP proto field number to string


code:
import datetime
print datetime.timedelta(minutes=594.15)

rawDescription: Python converting integer to hours

description: Python converting integer to hours


code:
print "WELCOME TO HANGMAN!"

print '                                            '

print '------------------------------------------'

print '                                            '

import random #for random.choice
dictionary=['CODING', 'GENEPEEKS', 'PYTHON', 'FREE TOM BRADY', 'BOSTON']
word=random.choice(dictionary)
original=list(word)
temp=list(word)
guess=[] #null list
lettersguessed=[]
trial=int(0) #for keeping track of guessess
userinput=''
counter=int(0) #keeping track of position of element in list (if found)

for i in range(len(original)): #creating the '_ _**....' list
    if (original[i]==' '):
        guess.append(" ") #(whitespace) for vowels
    else:
        guess.append("_") #_ for all other alphabets

print guess

while trial<15:
    userinput=str.upper(raw_input('Input : '))

    if userinput in lettersguessed:  # test presence
        print "This letter has been used already!"
        continue
    else:
        lettersguessed.append(userinput) # remember as used

    if len(userinput)>1: #checking for multiple characters
        print 'Error : Input only a single character'
        trial -= 1
        continue

    if userinput in original:
        while userinput in temp: #loop for checking redundant characters
            counter=temp.index(userinput)
            guess[counter]=userinput
            temp.remove(userinput)
            temp.insert(counter,'_')

        counter=0

        for i in range(0,len(temp)): #checking for final guess match with original
            if temp[i]=='_':
                counter+=1

        if counter==len(original): #if guess matches original
            print 'Correct\t', guess
            print 'You Win !'
            trial=10
            break

        print 'Correct\t' , guess , '\tTrials left: ', (9-trial)

    else:
        trial+=1
        print 'Incorrect', '\tTrials left: ', (9-trial)
else:
    print 'You Lose !'
    print 'Correct answer was\t', original

rawDescription: What is the correct way to use a .join() function?


code:
string = ''

for i in range(len(contacts.items())):
    string += contacts.items()[i][0] + ': '
    string += ' '.join([str(j) for j in contacts.items()[i][1].values()])

    if i % 2 == 0:
        string += '\t'

    if i % 2 != 0:
        string += '\n'

print string # prints in two columns

rawDescription: Another Dictionary, Another Format Output


rawDescription: what number row does 'x' appear on

description: What number row does 'x' appear on


code:
import BeautifulSoup
import urllib2
html = urllib2.urlopen('http://wolk.vlan77.be/~gerben')
soup = BeautifulSoup.BeautifulSoup(html)
print soup.find('td', text=lambda data: data.string and 'lucas' in data.string)

rawDescription: Beautiful Soup line matching

description: Beautiful Soup line matching


code:
from random import randint

CARD_FACES = {1: "Ace", 2: "2", 3: "3", 4: "4", 5: "5", 6: "6", 7: "7", 8: "8",
              9: "9", 10: "10", 11: "Jack", 12: "Queen", 13: "King"}

def deal():
    """Deal a card - returns a value indicating a card with the Ace
       represented by 1 and the Jack, Queen and King by 11, 12, 13
       respectively.
    """
    return randint(1, 13)

def _get_hand_value(cards):
    """Get the value of a hand based on the rules for Black Jack."""
    val = 0
    for card in cards:
        if 1 < card <= 10:
            val += card # 2 thru 10 are worth their face values
        elif card > 10:
            val += 10 # Jack, Queen and King are worth 10

    # Deal with the Ace if present.  Worth 11 if total remains 21 or lower
    # otherwise it's worth 1.
    if 1 in cards and val + 11 <= 21:
        return val + 11
    elif 1 in cards:
        return val + 1
    else:
        return val

def show_hand(name, cards):
    """Print a message showing the contents and value of a hand."""
    faces = [CARD_FACES[card] for card in cards]
    val = _get_hand_value(cards)

    if val == 21:
        note = "BLACK JACK!"
    else:
        note = ""

    print "%s's hand: %s, %s : %s %s" % (name, faces[0], faces[1], val, note)

# Deal 2 cards to both the dealer and a player and show their hands
for name in ("Dealer", "Player"):
    cards = (deal(), deal())
    show_hand(name, cards)

rawDescription: Python - Blackjack

description: Python - Blackjack


code:
import re
s = "If you have vacation or wedding plans for Miami, Jacksonville, Macon, Charlotte, or Charleston, expect a couple systems"
p = re.compile(r"If you have vacation or wedding plans (in|for) ((\w+, )+)or (\w+)")
m = p.match(s)
print m.group(2) # output: Miami, Jacksonville, Macon, Charlotte,
cities = m.group(2).split(", ") # cities = ['Miami', 'Jacksonville', 'Macon', 'Charlotte', '']
cities[-1] = m.group(4) # add the city after or
print cities # cities = ['Miami', 'Jacksonville', 'Macon', 'Charlotte', 'Charleston']

rawDescription: Python regex to capture a comma-delimited list of items


code:
import urllib
foo = '%23%C5%9Een%C5%9EakrakTakiple%C5%9FelimYine'
print urllib.unquote(foo).decode('utf-8')

rawDescription: Python character encoding for '%C5%9' and similar


code:
from pyswitch import Switch   # pyswitch can be found on PyPI

myswitch = Switch()

@myswitch.case(42)
def case42(value):
    print "I got 42!"

@myswitch.case(range(10))
def caseRange10(value):
    print "I got a number from 0-9, and it was %d!" % value

@myswitch.caseIn('lo')
def caseLo(value):
    print "I got a string with 'lo' in it; it was '%s'" % value

@myswitch.caseRegEx(r'\b([Pp]y\w)\b')
def caseReExPy(matchOb):
    print r"I got a string that matched the regex '\b[Pp]y\w\b', and the match was '%s'" % matchOb.group(1)

@myswitch.default
def caseDefault(value):
    print "Hey, default handler here, with a value of %r." % value

myswitch(5)  # prints: I got a number from 0-9, and it was 5!
myswitch('foobar')  # prints: Hey, default handler here, with a value of foobar.
myswitch('The word is Python')  # prints: I got a string that matched the regex '\b[Pp]y\w\b', and the match was 'Python'

rawDescription: Is there any value to a Switch / Case implementation in Python?


code:
d = {'element':[0,1]}
print d['element'][1]

rawDescription: Python dictionary, Sub element access

description: Python dictionary, Sub element access


code:
import multiprocessing as mp
import random

workers_count = 5
# Probability of failure, change to simulate failures
fail_init_p = 0.5
fail_job_p = 0.4

#========= Worker =========
def do_work(job_state, arg):
    if random.random() < fail_job_p:
        raise Exception("Job failed")
    return "job %d processed %d" % (job_state, arg)

def init(args):
    if random.random() < fail_init_p:
        raise Exception("Worker init failed")
    return args

def worker_function(args, jobs_queue, result_queue):
    # INIT
    # What to do when init() fails?
    try:
        state = init(args)
    except:
        print "!Worker %d init fail" % args
        result_queue.put('init failed')
        return
    # DO WORK
    # Process data in the jobs queue
    for job in iter(jobs_queue.get, None):
        try:
            # Can throw an exception!
            result = do_work(state, job)
            result_queue.put(result)
        except:
            print "!Job %d failed, skip..." % job
            result_queue.put('job failed')

#========= Parent =========
jobs = mp.Queue()
results = mp.Queue()
for i in range(workers_count):
    mp.Process(target=worker_function, args=(i, jobs, results)).start()

# Populate jobs queue
results_to_expect = 0
for j in range(30):
    jobs.put(j)
    results_to_expect += 1

init_failures = 0
job_failures = 0
successes = 0
while job_failures + successes < 30 and init_failures < workers_count:
    result = results.get()
    init_failures += int(result == 'init failed')
    job_failures += int(result == 'job failed')
    successes += int(result != 'init failed' and result != 'job failed')
    #print init_failures, job_failures, successes

for ii in range(workers_count):
    jobs.put(None)

rawDescription: Python multiprocessing and handling exceptions in workers


code:
L_rects = []
R_rects = []
for i in changeMatrix:
    count_j = 1
    for j in i:
        if j <> 0 and count_i <> count_j:
            textitem_L = scene.addText(L[count_j-1])
            textitem_R = scene.addText(L[count_i-1])
            textitem_L.setPos(0, count_valid*50)
            textitem_R.setPos(300, count_valid*50)
            rect_L = scene.addRect(50, count_valid*50, 25, 25, pen, brush)
            rect_R = scene.addRect(250, count_valid*50, 25, 25, pen, brush)
            rect_L.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsFocusable | QtGui.QGraphicsItem.ItemSendsGeometryChanges)
            rect_R.setFlags(QtGui.QGraphicsItem.ItemIsMovable | QtGui.QGraphicsItem.ItemIsSelectable | QtGui.QGraphicsItem.ItemIsFocusable | QtGui.QGraphicsItem.ItemSendsGeometryChanges)
            rect_L.setAcceptsHoverEvents(True)
            rect_R.setAcceptsHoverEvents(True)
            L_rects.append(rect_L)
            R_rects.append(rect_R)
            count_valid += 1
        count_j += 1
    count_i += 1

rawDescription: Using scene.addRect in for loop. How do I add a variable number to the rectangle's name?


code:
import csv
import re

data = [ ['9-1', '9-2', '9-3'] ]

def quoteCsvData(data):
    for x, row in enumerate(data):
        for y, item in enumerate(row):
             if data[x][y] != '':
                 data[x][y] = r'="' + data[x][y] + r'"'
    return data

def unquoteCsvData(data):
    for x, row in enumerate(data):
        for y, item in enumerate(row):
            if data[x][y] != '':
                m = re.match(r'="([^"]*)"',data[x][y])
                if m:
                    data[x][y] =  m.group(1)
    return data

fd = open('test2.csv', 'wb')
data = quoteCsvData(data)
try:
    writer = csv.writer(fd, delimiter=',',
                            quotechar='|',
                            quoting=csv.QUOTE_NONE,
                            escapechar='\\')
    writer.writerows(data)

finally:
    fd.close()

fd = open('test2.csv', 'rb')
try:
    reader = csv.reader(fd, delimiter=',',
                            quotechar='|',
                            quoting=csv.QUOTE_NONE,
                            escapechar='\\')
    data = []
    for row in reader:
        data.append(row)

finally:
    fd.close()

data = unquoteCsvData(data)

print data

rawDescription: Create excel-compatable CSV file with python?


code:
friends = facebook.users.getInfo(friends[0:], ['name', 'birthday','installed'])
for friend in friends:
    if 'birthday' in friend:
        if 'installed' in friend:
            print friend

rawDescription: How to retrieve friends currently using an app using PyFacebook?

description: Retrieve friends currently using an app using PyFacebook


code:
import ConfigParser
config = ConfigParser.ConfigParser()

config.read('config_num.ini')
sample = config.get('num_group','file')
sample = open(sample, 'r').read()
print sample

rawDescription: python configparser to parse the data saved in a text file

description: Python configparser to parse the data saved in a text file


code:
mydict = {
'1' : ['G', 'GA', 'GAA', 'GAAA', 'GAAAA', 'GAAAAA', 'GAAAAAG', 'GAAAAAGU', 'GAAAAAGUA', 'GAAAAAGUAU', 'GAAAAAGUAUG', 'GAAAAAGUAUGC', 'GAAAAAGUAUGCA', 'GAAAAAGUAUGCAA', 'GAAAAAGUAUGCAAG', 'GAAAAAGUAUGCAAGA', 'GAAAAAGUAUGCAAGAA', 'GAAAAAGUAUGCAAGAAC'],

'2' : ['G', 'GA', 'GAG', 'GAGA', 'GAGAG', 'GAGAGA', 'GAGAGAG', 'GAGAGAGA', 'GAGAGAGAC', 'GAGAGAGACA', 'GAGAGAGACAU', 'GAGAGAGACAUA', 'GAGAGAGACAUAG', 'GAGAGAGACAUAGA', 'GAGAGAGACAUAGAG', 'GAGAGAGACAUAGAGG'],

'3' : ['G', 'GU', 'GUC', 'GUCU', 'GUCUU', 'GUCUUU', 'GUCUUUG', 'GUCUUUGU', 'GUCUUUGU"', 'GUCUUUGU"G', 'GUCUUUGU"GU', 'GUCUUUGU"GUA', 'GUCUUUGU"GUAC', 'GUCUUUGU"GUACA', 'GUCUUUGU"GUACAU', 'GUCUUUGU"GUACAUC']
}

# build a dict of values and the keys that hold them
tmpdict = defaultdict(list)
for key, values in mydict.items():
    for value in values:
        tmpdict[value].append(key)

# now remove offenders from the original list
for value, inkeys in tmpdict.items():
    if len(inkeys) > 1:
        for key in inkeys:
            mydict[key].remove(value)

print mydict

rawDescription: Removing values from dictionary that multiple keys share


code:
import win32com.client

def find_process(name):
    objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
    objSWbemServices = objWMIService.ConnectServer(".", "root\cimv2")
    colItems = objSWbemServices.ExecQuery(
         "Select * from Win32_Process where Caption = '{0}'".format(name))
    return len(colItems)

print find_process("SciTE.exe")

rawDescription: Windows process management using Python

description: Windows process management using Python


code:
import re
data = "{{ a }} {{ func() }} {{var.func()}} {{ a+ab+b*+2/100 }} {{ var.atr}} "

def substitute(val):
    content = val.group(1)
    if re.match('^[\w\.]+$', content):
        return "{{ %s }}" % content
    else:
        return "{%% print %s %%}" % content
    pass

print re.sub("{{\s*(.*?)\s*}}", substitute, data)

rawDescription: RegEx Python - I Want to write RegEx by which I can replace { expression } into {% print expression %}


code:
import lxml.html.soupparser
MainPage = urllib2.urlopen(url).read()
HtmlDoc = lxml.html.soupparser.fromstring(MainPage)
Elements = HmtlDoc.xpath('//div[@id="bd-cross"]//li[@class=""]')
if len(Elements) > 0:
    print 'Yes'
else:
    print 'No'

rawDescription: Check for a tag presense on the page with Python


code:
import Tkinter as tk

data = {"Field 1": 1,
        "Field 2": 2,
        "Field 3": 3,
        "Field 4": 4,
}

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.variables ={}
        for label in sorted(data.keys()):
            self.variables[label] = tk.IntVar()
            cb = tk.Checkbutton(self, text=label,
                                onvalue=data[label], offvalue=0,
                                variable=self.variables[label])
            cb.pack(side="top", fill="x")

        button = tk.Button(self, text="Submit", command=self.OnSubmit)
        button.pack()

    def OnSubmit(self):
        for field in sorted(data.keys()):
            print "Value for %s: %s" % (field, self.variables[field].get())

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

rawDescription: Tkinter-Create Buttons according to dictionary keys


code:
import matplotlib.patches as patches
from matplotlib.nxutils import points_inside_poly
import matplotlib.pyplot as plt
import numpy as np

# generate some data
X, Y = np.meshgrid(np.arange(-3.0, 3.0, 0.025), np.arange(-3.0, 3.0, 0.025))
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)

fig, axs = plt.subplots(1,2, figsize=(12,6), subplot_kw={'xticks': [], 'yticks': [], 'frameon': False})

# create a normal contour plot
axs[0].set_title('Standard contour plot')
im = axs[0].imshow(Z, cmap=plt.cm.Greys_r)
cs = axs[0].contour(Z, np.arange(-3, 4, .5), linewidths=2, colors='red', linestyles='solid')

# get the path from 1 of the contour lines
verts = cs.collections[7].get_paths()[0]

# highlight the selected contour with yellow
axs[0].add_patch(patches.PathPatch(verts, facecolor='none', ec='yellow', lw=2, zorder=50))

# make a mask from it with the dimensions of Z
mask = verts.contains_points(list(np.ndindex(Z.shape)))
mask = mask.reshape(Z.shape).T

axs[1].set_title('Mask of everything within one contour line')
axs[1].imshow(mask, cmap=plt.cm.Greys_r, interpolation='none')

# get the sum of everything within the contour
# the mask is inverted because everything within the contour should not be masked
print np.ma.MaskedArray(Z, mask=~mask).sum()

rawDescription: How to go from a contour to an image mask in with Matplotlib

description: Go from a contour to an image mask in with Matplotlib


code:
import re
uni = 'Привет, как дела?'.decode('utf-8')
re.findall(ur'(?u)\w+', uni)

rawDescription: Russian symbols in re (Python)

description: Russian symbols in re (Python)


code:
from bs4 import BeautifulSoup

var = '''
 Energy

Coal

Oil

Carbon

Oxygen
'''

soup = BeautifulSoup(var)

for a in soup.find_all('a'):
  print a.string

rawDescription: Efficient way to extract text from between tags


code:
import re
m = re.search("Your number is (\d+)",
      "xxx Your number is 123  fdjsk")
if m:
    print m.groups()[0]

rawDescription: Python Regular Expression example

description: Python Regular Expression example


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

    def show(self):
        print self.name

ob1=test('object 1')
ob2=test('object 2')

l=[]
l.append(ob1)
l.append(ob2)

l[0].show()
l[1].show()

rawDescription: Array of objects in Python

description: Array of objects in Python


code:
for row in table:
    print '{0} - {1} - {2}'.format(*[x.name for x in row])

rawDescription: Python: Expanding List of Class Objects

description: Python: Expanding List of Class Objects


code:
from email.utils import parsedate_tz
print parsedate_tz('Fri, 15 May 2009 17:58:28 +0700')

rawDescription: How to parse a RFC 2822 date/time into a Python datetime?

description: Parse a RFC 2822 date/time into a Python datetime


code:
mylist = [1, 2, 2, 4, 2, 3]
for i, j in enumerate(mylist[:-1]):
    if j  == mylist[i+1]:
        mylist[i] = "foo"
        mylist[i+1] = "foo"
print mylist
[1, 'foo', 'foo', 4, 2, 3]

rawDescription: compare current item to next item in a python list

description: Compare current item to next item in a python list


rawDescription: Converting JSON String to Dictionary in (Python)


code:
import re

s = "my 'cat' is 'white'"

print re.sub("'([^']+)'", r"'\1\1'", s) # prints my 'catcat' is 'whitewhite'

rawDescription: programmatically find and replace content dynamically in a string in python

description: Programmatically find and replace content dynamically in a string in python


code:
from collections import defaultdict
d = defaultdict(str)
for i, j in zip(X, Y):
    d[i] += j

print d
print d.items()
X = d.keys()
Y = d.values()
print X
print Y

rawDescription: Have one list combine elements from another list based on index values


code:
import os

exts = {}
my_exts = ('pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx')

for file in os.listdir("C:\\Users\\joey\\Desktop\\school\\ICOMMH"):
    ext = os.path.splitext(file)[1]
    if ext and ext[1:] in my_exts:
        exts[ext] = exts.get(ext, 0) + 1

print sorted(exts.items(), key=lambda x: x[1], reverse=True)

rawDescription: python count files and display max

description: Python count files and display max


code:
id_hex = binascii.unhexlify(id_hex)
challenge = binascii.unhexlify(challenge)
result = id_hex+password+challenge
print result
response = hashlib.md5(result).hexdigest()

rawDescription: Unable to compute FREERADIUS CHAP response based on ID + Password + Challenge in Python


code:
geo = soup.find('geo')
print geo.lat.get_text(strip=True)
print geo.lng.get_text(strip=True)

rawDescription: BeautifulSoup: Parse children by tag name


code:
mygen = slow_generator_of_giant_list()
try:
    while True: stats = update(stats, mygen.next())
except StopIteration:
    print stats

rawDescription: Python REPL: issuing commands in advance to execute after block


code:
from multiprocessing import Process

class MyClass:
  def __init__(self, params):
    pass

  def launch(self):
    print "method launch is called"

def spawn(params):
  an_instance = MyClass(params)
  an_instance.launch()

my_list_of_parameters  = ['1', '2']

for params in my_list_of_parameters:
   Process(target=spawn, args=(params)).start()

rawDescription: python subprocess Process target function won't instantiate classes

description: Python subprocess Process target function won't instantiate classes


code:
def merge(lst):
    def j(sq):
        return ",".join(sq)
    def m(sl):
        dic = {}
        for ssl in sl:
            k = tuple(ssl[0:2])
            try:
                v = dic[k]
            except KeyError:
                dic[k] = v = (set(), set())
            v[0].update( set(ssl[2].split(',')) )
            v[0].discard('')
            v[1].update( set(ssl[3].split(',')) )
            v[1].discard('')
        return [ list(k) + [j(v[0])] + [j(v[1])] for k, v in sorted(dic.iteritems()) ]
    return [ m(sl) for sl in lst ]

for sl in merge(l):
    print sl

rawDescription: How to combine sub-sublists only if they share a common value?

description: Combine sub-sublists only if they share a common value


code:
#!/usr/bin/env python

from signal import signal, SIGTERM
from sys import exit
import atexit

def cleanup():
    print "Cleanup"

if __name__ == "__main__":
    from time import sleep
    atexit.register(cleanup)

    # Normal exit when killed
    signal(SIGTERM, lambda signum, stack_frame: exit(1))

    sleep(10)

rawDescription: proper way to stop a daemon process

description: Proper way to stop a daemon process


code:
previous = None
for i, (low,high) in enumerate(exons[name]):
    if low -3 <= ppos1 <= high + 6:
        exonnumber = i+1
        print previous
        previous = low, high

rawDescription: Enumerate, print previous

description: Enumerate, print previous


code:
import re
s = "{NOKIA}_B {Lumia 640 XL}_M {LTE}_O {8GB}_S"
matches = re.findall(r"\{([0-9a-zA-Z ]*)\}", s)
print matches

rawDescription: Python catch text inside {}

description: Python catch text inside {}


code:
f = open("foo", "w+")  # file does not exist
f.write("samething")
f.seek(1)
f.write("o")
f.seek(0)
print f.read() # prints "something"

rawDescription: Replace a character by another in a file


code:
def vectorize_key(key,word_size=32):
    return (reduce(lambda p, (e,f) : p | (e << f),((x[i],i*8) for i in range(word_size/8)),0) for x in split((ord(k) for k in key),word_size/8))

rawDescription: How to pack bits into variable length words in python?

description: Pack bits into variable length words in python


code:
import time
import multiprocessing

def slowfunc(value):
    time.sleep(0.01)
    return value**2 + 0.3*value + 1

counter_a = counter_b = counter_c = 0
def add_to_counter(res):
    global counter_a, counter_b, counter_c
    counter_a += res
    counter_b -= (res - 10)**2
    counter_c += (int(res) % 2)

pool = multiprocessing.Pool(50)
results = []

for value in range(100000):
    r = pool.apply_async(slowfunc, [value])
    results.append(r)

    # don't let the queue grow too long
    if len(results) == 1000:
        results[0].wait()

    while results and results[0].ready():
        r = results.pop(0)
        add_to_counter(r.get())

for r in results:
    r.wait()
    add_to_counter(r.get())

print counter_a, counter_b, counter_c

rawDescription: Python, process a large text file in parallel


code:
with open("config.txt") as cfile:
    words = cfile.readlines()
words = filter(None, words) # here we get rid of all empty lines, should there be any

with open("bintext-result.txt") as ifile:
    for line in ifile:
        if not(any([w in line for w in words])):
            print line.strip()

rawDescription: Filtering text in a file based from keywords in another file/dictionary


code:
import os
import glob
import logging
import logging.handlers
import time

LOG_FILENAME = 'logging_rotatingfile_example.out'

# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)

# Check if log exists and should therefore be rolled
needRoll = os.path.isfile(LOG_FILENAME)

# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(LOG_FILENAME, backupCount=50)

my_logger.addHandler(handler)

# This is a stale log, so roll it
if needRoll:
    # Add timestamp
    my_logger.debug('\n---------\nLog closed on %s.\n---------\n' % time.asctime())

    # Roll over on application start
    my_logger.handlers[0].doRollover()

# Add timestamp
my_logger.debug('\n---------\nLog started on %s.\n---------\n' % time.asctime())

# Log some messages
for i in xrange(20):
    my_logger.debug('i = %d' % i)

# See what files are created
logfiles = glob.glob('%s*' % LOG_FILENAME)

print '\n'.join(logfiles)

rawDescription: Rotate logfiles each time the application is started (Python)


code:
import requests

var = requests.get(r'http://www.google.com/search?q="stackoverflow"&btnI')
print var.url

rawDescription: Python - get redirected URL from Google's "I feel lucky" and Duckduckgo's "I'm feeling ducky"


code:
import math
def subset_avg(numbers, target, partial=[]):
    if len(partial) > 0:
        s = math.ceil(sum(partial)/float(len(partial))) # calculate the average for each subset
    else:
        s = math.ceil(sum(partial)/float(1)) # calculate the average for first empty subset which is 0

    # check if the partial sum is equals to target
    if s == target:
        print "sum(%s)=%s" % (partial, target)
    if s >= target:
        return  # if we reach the number why bother to continue

    for i in range(len(numbers)):
        n = numbers[i]
        remaining = numbers[i+1:]
        subset_avg(remaining, target, partial + [n])

if __name__ == "__main__":
    l1 = [1,2,3,4,5,6,7,8,9,10]
    l1_sum = 0
    for each_i in l1:
        l1_sum += each_i # calculate the sum here
    l1_avg = math.ceil(l1_sum/float(len(l1))) # calculate the average here which becomes out target
    subset_avg(l1,l1_avg)

rawDescription: A simple matchmaking service in Python

description: A simple matchmaking service in Python


code:
import requests
response = requests.get("http://www.google.com")
print response.content
print response.content == response.text

rawDescription: reading the response in python requests

description: Reading the response in python requests


code:
import Tkinter

class App:
    def __init__(self, root):
        f = Tkinter.Frame(width=100, height=100, background="bisque")
        f.pack(padx=100, pady=100)
        f.bind("<1>", self.OnMouseDown)

    def OnMouseDown(self, event):
        print "frame coordinates: %s/%s" % (event.x, event.y)
        print "root coordinates: %s/%s" % (event.x_root, event.y_root)

root=Tkinter.Tk()
app = App(root)
root.mainloop()

rawDescription: How do I get mouse position relative to the parent widget in tkinter?

description: Get mouse position relative to the parent widget in tkinter


code:
import numpy as np
X = np.array([1, 3, 0, 8, 1, 4, 6, 0, 2, 0, 7, 8])
indicies_we_want = np.where(X > 0) # Results in an array containing the indicies of X we want to keep
result = np.take(X, indicies_we_want) # Filter by these indicies
result = result.reshape(3, 3) # Reshape to desired result
print result

rawDescription: Maintaining shape of output as of input after Boolean indexing in python


code:
from jinja2 import *

def show_all_attrs(value):
    res = []
    for k in dir(value):
        res.append('%r %r\n' % (k, getattr(value, k)))
    return '\n'.join(res)

env = Environment()
env.filters['show_all_attrs'] = show_all_attrs

# using the filter
tmpl = env.from_string('''{{v|show_all_attrs}}''')
class Myobj(object):
    a = 1
    b = 2

print tmpl.render(v=Myobj())

rawDescription: Display all jinja object attributes

description: Display all jinja object attributes


code:
new_img = resize(img, (30,60))
print new_img.shape

rawDescription: resizing images in python not actually resizing

description: Resizing images in python not actually resizing


code:
import cgi

def html_escape(func):
    def wrapped(*args, **kwargs):
        return cgi.escape(func(*args, **kwargs))
    return wrapped

@html_escape
def index():
    return "<html></html>"

print index()

rawDescription: Flask HTML Escape Decorator

description: Flask HTML Escape Decorator


code:
def swap(lista, idx1, idx2):
    temp = lista[idx1]
    lista[idx1] = lista[idx2]
    lista[idx2] = temp

def valid():
    return True

def permutare(lista, start):
    if start >= len(lista):
        if valid():
            return [list(lista)]

    output = []
    for idx in xrange(start, len(lista)):
        swap(lista, start, idx)
        output.extend(permutare(lista, start + 1))
        swap(lista, start, idx)  # backtrack
    return output

print len(permutare(['a','b','c'], 0))

rawDescription: Python permutations with condition (backtracking)


code:
def descAscii(words):
    print ''.join(str(ord(letter)) for letter in words)

rawDescription: How can I concatenate elements from a lists into one variable in Python?

description: Concatenate elements from a lists into one variable in Python


code:
ticketdata = pickle.load(open("tickets.p", "rb"))

for i in ticketdata:
    for j in ticketdata[i]['events']:
        if 'closed' in j:
            print i, ticketdata[i]['events']

rawDescription: Filtering a dictionary + Tuple

description: Filtering a dictionary + Tuple


code:
from xml.etree import ElementTree as et

class XMLCombiner(object):
    def __init__(self, filenames):
        assert len(filenames) > 0, 'No filenames!'
        # save all the roots, in order, to be processed later
        self.roots = [et.parse(f).getroot() for f in filenames]

    def combine(self):
        for r in self.roots[1:]:
            # combine each element with the first one, and update that
            self.combine_element(self.roots[0], r)
        # return the string representation
        return et.tostring(self.roots[0])

    def combine_element(self, one, other):
        """
        This function recursively updates either the text or the children
        of an element if another element is found in `one`, or adds it
        from `other` if not found.
        """
        # Create a mapping from tag name to element, as that's what we are fltering with
        mapping = {el.tag: el for el in one}
        for el in other:
            if len(el) == 0:
                # Not nested
                try:
                    # Update the text
                    mapping[el.tag].text = el.text
                except KeyError:
                    # An element with this name is not in the mapping
                    mapping[el.tag] = el
                    # Add it
                    one.append(el)
            else:
                try:
                    # Recursively process the element, and update it in the same way
                    self.combine_element(mapping[el.tag], el)
                except KeyError:
                    # Not in the mapping
                    mapping[el.tag] = el
                    # Just add it
                    one.append(el)

if __name__ == '__main__':
    r = XMLCombiner(('sample1.xml', 'sample2.xml')).combine()
    print '-'*20
    print r

rawDescription: Merge xml files with nested elements without external libraries


code:
import threading
import os

commands = ['ping www.google.com', 'ping www.yahoo.com', 'ping www.hotmail.com']

def worker_func():
    while commands:   # Checks if the the list is not-empty. Loop exits when list is becomes empty
        com = commands.pop(0)
        print "Start execute commands.."
        os.system(com)
        count += 1
        print "[OK] command "+str(count)+" runing successfully."

workers = [threading.Thread(target=worker_func, args=tuple(), name='thread_'+str(i))  for i in range(5) ]  # Create 5 workers (consumers)
[worker.start() for worker in workers]  # Start working
[worker.join() for worker in workers]   # Wait for all workers to finish

rawDescription: execute multiple commands in Linux using python in the same time

description: Execute multiple commands in Linux using python in the same time


code:
import signal

class TimeoutError(Exception): pass

# define the timeout handler
def handler(signum, frame):
    raise TimeoutError()

# set the timeout handler and the signal duration
signal.signal(signal.SIGALRM, handler)
signal.alarm(1)
try:
    asyncore.loop()
except TimeoutError as exc:
    print "timeout"
finally:
    signal.alarm(0)

rawDescription: asyncore.loop doesn't terminate when there are no more connections

description: Asyncore.loop doesn't terminate when there are no more connections


code:
infile = open('entrada', 'r')

animals = 0
safe_animals = 0
dangerous_animals = 0

for line in infile:
    line_components = line.strip().split()
    if line_components:
        animals += 1

        if line_components[3] == 'dangerous':
            dangerous_animals += 1
        elif line_components[3] == 'safe' and line_components[1] == 'large':
            safe_animals += 1

print "%i animals" % animals
print "%i safe animals" % safe_animals
print "%i dangerous animals" % dangerous_animals

rawDescription: Reading and writing files in python 3

description: Reading and writing files in python 3


code:
a="foofoo\tBergen, Norway\tAffluent\tDonkey"

word= a.rsplit('\t',2)
if len(word)>2:
     print word[-2]

#output =Affluent

rawDescription: Getting substring between nth and mth occurence of some sequence


code:
# create an empty list object
myListofNA = []
for d in range(0,2):
    datei = filenames[d]
    aMolecule = openfiles(datei)
    myNA = aMolecule[3]
    print myNA
    # each time append a new entry to the list
    myListofNA.append(myNa)

# search the maximum element of the list
print max(myListofNA)

rawDescription: Put several intengers into a list to get the maximum number


code:
import re
import sys

# I will write each table to different results-file.
# dictionary to map columns (numbers) to opened file objects:
resultfiles = {}

def get_result_file(column):
    # helper to easily access results file.
    if column not in resultfiles:
        resultfiles[column] = open('result-column-%d.dat' % column, 'w')
    return resultfiles[column]

# iterate over data:
for line in open('input.dat'):
    try:
        # str.split(separator, maxsplit)
        # with `maxsplit`=2 it is more fail-proof:
        no, score, seq = line.split(None, 2)

        # from your example I guess that white-spaces in sequence are meaningless,
        # however in your example one sequence contains white-space, so I remove it:
        seq = re.sub('\s+', '', seq)

        # data validation will help to spot problems early:
        assert int(no), no
        assert float(score), score
        assert len(seq) == 65, seq

    except Exception, e:
        # print the error and continue to process data:
        print >> sys.stderr, 'Error %s in line: %s.' % (e, line)
        continue  # jump to next iteration of for loop.

    # int(), float() will rise ValueError if no or score aren't numbers
    # assert <condition> will rise AssertionError if condition is False.

    # iterate over each character in amino sequance:
    for column, char in enumerate(seq, 1):
        f = get_result_file(column)
        f.write('%s    %s    %s\n' % (no, score, char))

# close all opened result files:
for f in resultfiles.values():
    f.close()

rawDescription: Parsing the result using python

description: Parsing the result using python


code:
commands = ['pqr', '123', '456', '789']
inp = []
user_inp = None
non_repeat = "abc"
while user_inp != "789":
    user_inp = raw_input()
    # if it is the first entry and input == non_repeat
    if not inp and user_inp == non_repeat:
        inp.append(user_inp)
    # else if all entries so far are == non_repeat and currnet input == non_repeat
    elif user_inp == non_repeat and all(x == non_repeat for x in inp):
        inp.append(user_inp)
     # else if it is a valid command, append
    elif inp and user_inp in commands:
        inp.append(user_inp)
    # else we tried adding non_repeat after other elements or a command not in the list
    else:
        print ">>> Invalid Input"

rawDescription: Python list remove item in the loop once


code:
import pandas as pd
import numpy as np
import io

temp=u"""end,start,value
2000-01-04,2000-01-02,6
2000-01-05,2000-01-03,9"""

df = pd.read_csv(io.StringIO(temp), parse_dates = [0,1])
print df
#change ordering for filling date from start to end
df = df[['start', 'end', 'value']]

#value divided difference of start and end, but it cant count first day, so has to be added
df['value'] = df['value']/(df['end'] + pd.Timedelta('1 days')- df['start']).astype('timedelta64[D]')

df['Id'] = df.index
#reshape datetimes from rows to columns
df = pd.melt(df, id_vars=[ 'value','Id'], var_name=['D'], value_name='Date')
#remove unnecessary column D
del df['D']
print df
#   value  Id       Date
#0      2   0 2000-01-02
#1      3   1 2000-01-03
#2      2   0 2000-01-04
#3      3   1 2000-01-05

#set multiindex
df = df.set_index(['Id', 'Date' ])

#fill gap between start and end dates
f = lambda df: df.asfreq("D", method='ffill')
df = df.reset_index(level=0).groupby('Id').apply(f)

del df['Id']
df = df.reset_index()
print df
#   Id       Date  value
#0   0 2000-01-02      2
#1   0 2000-01-03      2
#2   0 2000-01-04      2
#3   1 2000-01-03      3
#4   1 2000-01-04      3
#5   1 2000-01-05      3

#sum column value to column rate
df['rate'] = df.groupby('Date')['value'].transform('sum')
#delete unnecessary columns
df = df.drop(['Id', 'value'], axis=1 )
#drop duplicity
df = df.drop_duplicates()
print df
#
#        Date  rate
#0 2000-01-02     2
#1 2000-01-03     5
#2 2000-01-04     5
#5 2000-01-05     3

rawDescription: How to transform date range stored as two columns (start, end) to create new row index and create accumulated rate for values?

description: Transform date range stored as two columns (start, end) to create new row index and create accumulated rate for values


code:
dic = {}
with open("ABC.txt",'r') as f:
    for l in f:
        key,value = l.split() #Set two temporary variables for easy access to data
        dic[key] = int(value) #Set the appropriate key of the list to its respective value
print dic

rawDescription: Dictionary out of strings with blanks as the key/value separator