code:
import urllib2, httplib, socket
from suds.client import Client
from suds.transport.http import HttpTransport, Reply, TransportError

class HTTPSClientAuthHandler(urllib2.HTTPSHandler):
    def __init__(self, key, cert):
        urllib2.HTTPSHandler.__init__(self)
        self.key = key
        self.cert = cert

    def https_open(self, req):
        #Rather than pass in a reference to a connection class, we pass in
        # a reference to a function which, for all intents and purposes,
        # will behave as a constructor
        return self.do_open(self.getConnection, req)

    def getConnection(self, host, timeout=300):
        return httplib.HTTPSConnection(host,
                                       key_file=self.key,
                                       cert_file=self.cert)

class HTTPSClientCertTransport(HttpTransport):
    def __init__(self, key, cert, *args, **kwargs):
        HttpTransport.__init__(self, *args, **kwargs)
        self.key = key
        self.cert = cert

    def u2open(self, u2request):
        """
        Open a connection.
        @param u2request: A urllib2 request.
        @type u2request: urllib2.Requet.
        @return: The opened file-like urllib2 object.
        @rtype: fp
        """
        tm = self.options.timeout
        url = urllib2.build_opener(HTTPSClientAuthHandler(self.key, self.cert))
        if self.u2ver() < 2.6:
            socket.setdefaulttimeout(tm)
            return url.open(u2request)
        else:
            return url.open(u2request, timeout=tm)

# These lines enable debug logging; remove them once everything works.
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)
logging.getLogger('suds.transport').setLevel(logging.DEBUG)

c = Client('https://YOUR_URL_HERE',
    transport = HTTPSClientCertTransport('YOUR_KEY_AND_CERT.pem',
                                         'YOUR_KEY_AND_CERT.pem'))
print c

rawDescription: Suds over https with cert

description: Suds over https with cert


code:
import time, random, sys
#from PySide.QtCore import *
#from PySide.QtGui import *

from PyQt4 import QtCore
from PyQt4 import QtGui

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class ApplicationWindow(QtGui.QMainWindow):
    get_data = QtCore.pyqtSignal()

    def __init__(self, parent = None):

        QtGui.QMainWindow.__init__(self, parent)

        self.thread = QtCore.QThread(parent=self)
        self.worker = Worker(parent=None)
        self.worker.moveToThread(self.thread)

        self.create_main_frame()
        self.create_status_bar()

        self.startButton.clicked.connect(self.start_acquisition)
        self.stopButton.clicked.connect(self.stop_acquisition)
        self.worker.pixel_list.connect(self.update_figure)
        self.worker.done.connect(self.update_UI)

        self.get_data.connect(self.worker.get_data)

        self.thread.start()

    def create_main_frame(self):
        self.main_frame = QtGui.QWidget()

        self.dpi = 100
        self.width = 10
        self.height = 8
        self.fig = Figure(figsize=(self.width, self.height), dpi=self.dpi)
        self.axes = self.fig.add_subplot(111)
        self.axes.axis((0,512,0,120))

        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)
        self.canvas.updateGeometry()
        self.canvas.draw()
        self.background = None

        self.lE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)
        self.hE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)

        self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

        self.startButton = QtGui.QPushButton(self.tr("&Start"))
        self.stopButton = QtGui.QPushButton(self.tr("&Stop"))

        layout = QtGui.QGridLayout()
        layout.addWidget(self.canvas, 0, 0)
        layout.addWidget(self.mpl_toolbar, 1, 0)
        layout.addWidget(self.startButton, 2, 0)
        layout.addWidget(self.stopButton, 2, 1)

        self.main_frame.setLayout(layout)
        self.setCentralWidget(self.main_frame)

        self.setWindowTitle(self.tr("XRTdev Interface"))

    def create_status_bar(self):
        self.status_text = QtGui.QLabel("I am a status bar.  I need a status to show!")
        self.statusBar().addWidget(self.status_text, 1)

    def start_acquisition(self):
        self.worker.exiting = False
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)
        self.get_data.emit()

    def stop_acquisition(self):
        self.worker.exiting = True
        self.startButton.setEnabled(True)
        self.stopButton.setEnabled(False)
        self.cleanup_UI()

    def update_figure(self, lE, hE):
        if self.background == None:
            self.background = self.canvas.copy_from_bbox(self.axes.bbox)
        self.canvas.restore_region(self.background)
        self.lE_line.set_ydata(lE)
        self.hE_line.set_ydata(hE)
        self.axes.draw_artist(self.lE_line)
        self.axes.draw_artist(self.hE_line)
        self.canvas.blit(self.axes.bbox)

    def update_UI(self):
        self.startButton.setEnabled(True)
        self.stopButton.setEnabled(False)
        self.cleanup_UI()

    def cleanup_UI(self):
        self.background = None
        self.axes.clear()
        self.canvas.draw()

class Worker(QtCore.QObject):

    pixel_list = QtCore.pyqtSignal(list, list)
    done = QtCore.pyqtSignal()

    def __init__(self, parent = None):
        QtCore.QObject.__init__(self, parent)
        self.exiting = True

    @QtCore.pyqtSlot()
    def get_data(self):
        # simulate I/O
        print 'data_start'
        n = random.randrange(100,200)
        while not self.exiting and n > 0:
            lE = [random.randrange(5,16) for i in xrange(512)]
            hE = [random.randrange(80,121) for i in xrange(512)]
            self.pixel_list.emit(lE, hE)
            time.sleep(0.05)
            n -= 1
        print 'n: ', n
        self.done.emit()

rawDescription: time.sleep() required to keep QThread responsive?

description: Time.sleep() required to keep QThread responsive


code:
twitterStream=Stream(OAuthObject,listenerFunction())
while True:
    if twitterStream.running is True:
        twitterStream.disconnect()
    keywords=getKeywordsFromDb() # return string of keywords seaprated by comma
    if keywords=='':
        print 'no keywords to listen to'
    else:
        twitterStream.filter(track=[keywords],async=True) # Open the stream to work on asynchronously on a different thread
    time.sleep(3600) # sleep for one hour

rawDescription: how can i refresh twitter filter stream thread in tweepy?

description: Refresh twitter filter stream thread in tweepy


rawDescription: Group one list of string elements based on another list


code:
from apiclient import discovery

# Auth goes here if necessary. Create authorized http object...
client = discovery.build('storage', 'v1beta2') # add http=whatever param if auth
request = client.objects().list(
    bucket="mybucket",
    prefix="abc/myfolder")
while request is not None:
  response = request.execute()
  print json.dumps(response, indent=2)
  request = request.list_next(request, response)

rawDescription: Google Cloud Storage + Python : Any way to list obj in certain folder in GCS?


code:
from xml.etree.ElementTree import ElementTree
doc = ElementTree(file='myfile.xml')

for e in doc.findall('/Hit_accession'):
    print e.get('Hsp_qseq').text

rawDescription: Python regex too greedy, misses first occurence in XML


code:
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import Selector
from scrapy.http import Request

class people(CrawlSpider):
  name="people"
  allowed_domains=["http://blog.sina.com.cn/"]
  start_urls=["http://blog.sina.com.cn/s/blog_53d7b5ce0100e7y0.html"]
  rules=(Rule(SgmlLinkExtractor(allow=("http://blog.sina.com.cn/",)),  callback='parse_item', follow=True),
#restrict the crawling in the articalContent section only
  Rule(SgmlLinkExtractor(restrict_xpaths=('//div[contains(@class, "articalContent")]'))),
  )

  def parse(self,response):
      links = Selector(text=response.body).xpath('//div[contains(@class, "articalContent")]//a//text()')
      for link in links:
          print link.extract()

rawDescription: 1-level down crawling using scrapy

description: 1-level down crawling using scrapy


code:
##################################### Method 1
import mechanize
import cookielib
from BeautifulSoup import BeautifulSoup
import html2text

# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

br.addheaders = [('User-agent', 'Chrome')]

# The site we will navigate into, handling it's session
br.open('https://github.com/login')

# View available forms
for f in br.forms():
    print f

# Select the second (index one) form (the first form is a search query box)
br.select_form(nr=1)

# User credentials
br.form['login'] = 'mylogin'
br.form['password'] = 'mypass'

# Login
br.submit()

print(br.open('https://github.com/settings/emails').read())

rawDescription: How to scrape a website that requires login first with Python

description: Scrape a website that requires login first with Python


code:
import os
import glob
import sys
import os.path as path

try:
    directory,file = path.split( sys.argv[1] )
    def sort_func(fname):
        """
        Russian directories , english directories, russian files then english files
        although, honestly I don't know how russian files will actually be sorted ...
        """
        fullname = path.join(directory,fname)
        isRussian = any(ord(x) > 127 for x in fullname)
        isDirectory = path.isdir(fullname)
        return ( not isDirectory, not isRussian, fullname)

    files = sorted( os.listdir(directory), key=sort_func)
    print ( files.index(file) + 1 )

except IndexError:
    print "oops, no commandline arguments"

rawDescription: How to get sequence number of the file in the folder?

description: Get sequence number of the file in the folder


code:
def handler(signum, frame):
    print 'do it'

# This will fire an ITIMER_REAL signal every 0.01 seconds
signal.setitimer(signal.ITIMER_REAL, 0, 0.01)

# Tell the signal module to execute handler when upon signal
# ITIMER_REAL
signal.signal(signal.ITIMER_REAL, handler)

rawDescription: Best way to do a high frequency loop in python on Linux?

description: Do a high frequency loop in python on Linux


code:
import xml.etree.ElementTree as ET
import sqlite3

conn = sqlite3.connect('trackdb.sqlite')
cur = conn.cursor()

# Make some fresh tables using executescript()
cur.executescript('''
DROP TABLE IF EXISTS Artist;
DROP TABLE IF EXISTS Album;
DROP TABLE IF EXISTS Genre;
DROP TABLE IF EXISTS Track;

CREATE TABLE Artist (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name    TEXT UNIQUE
);

CREATE TABLE Album (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    artist_id  INTEGER,
    title   TEXT UNIQUE
);

CREATE TABLE Genre (
    id  INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
    name   TEXT UNIQUE
);

CREATE TABLE Track (
    id  INTEGER NOT NULL PRIMARY KEY
        AUTOINCREMENT UNIQUE,
    title TEXT  UNIQUE,
    album_id  INTEGER,
    genre_id  INTEGER,
    len INTEGER, rating INTEGER, count INTEGER
);
''')

fname = raw_input('Enter file name: ')
if ( len(fname) < 1 ) : fname = 'Library.xml'

# <key>Track ID</key><integer>369</integer>
# <key>Name</key><string>Another One Bites The Dust</string>
# <key>Artist</key><string>Queen</string>
def lookup(d, key):
    found = False
    for child in d:
        if found : return child.text
        if child.tag == 'key' and child.text == key :
            found = True
    return None

stuff = ET.parse(fname)
all = stuff.findall('dict/dict/dict')
print 'Dict count:', len(all)
for entry in all:
    if ( lookup(entry, 'Track ID') is None ) : continue

    name = lookup(entry, 'Name')
    artist = lookup(entry, 'Artist')
    album = lookup(entry, 'Album')
    genre = lookup(entry, 'Genre')
    count = lookup(entry, 'Play Count')
    rating = lookup(entry, 'Rating')
    length = lookup(entry, 'Total Time')

    if name is None or artist is None or genre is None or album is None:
        continue

    print name, artist, album, genre, count, rating, length

    cur.execute('''INSERT OR IGNORE INTO Artist (name)
        VALUES ( ? )''', ( artist, ) )
    cur.execute('SELECT id FROM Artist WHERE name = ? ', (artist, ))
    artist_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Album (title, artist_id)
        VALUES ( ?, ? )''', ( album, artist_id ) )
    cur.execute('SELECT id FROM Album WHERE title = ? ', (album, ))
    album_id = cur.fetchone()[0]

    cur.execute('''INSERT OR IGNORE INTO Genre (name)
    VALUES ( ? )''', ( genre, ) )
    cur.execute('SELECT id FROM Genre WHERE name = ? ', (genre, ))
    genre_id = cur.fetchone()[0]

    cur.execute('''INSERT OR REPLACE INTO Track
        (title, album_id, genre_id, len, rating, count)
        VALUES ( ?, ?, ?, ?, ?, ? )''',
        ( name, album_id, genre_id, length, rating, count ) )

    conn.commit()

rawDescription: Musical Track Database

description: Musical Track Database


code:
def process_form_data(form_list):
    form_data = [form.cleaned_data for form in form_list]

    print form_data[0]
    print form_data[1]

    return form_data
class ReviewWizard(SessionWizardView):
    template_name = "review_form.html"

    def done(self, form_list, **kwargs):
        form_data = process_form_data(form_list)

        return render("done.html", {"form_data": form_data})

    def get_form_kwargs(self, step):
        kwargs = super(ReviewWizard, self).get_form_kwargs(step)
        kwargs['request'] = self.request
        return kwargs

rawDescription: Django: How can I make Form Wizard take in a request object when it creates the forms?


code:
def findNodesInSpheres(sList,nList,nx,ny,nz):
    print "Running findNodesInSpheres"
    filteredList = []
    for a in sList:
            rad = a.radius
            minx = (a.x) - (rad) if (a.x - rad > 0) else 0
            maxx = (a.x) + (rad) if (a.x + rad < nx ) else nx
            miny = (a.y) - (rad) if (a.y - rad > 0) else 0
            maxy = (a.y) + (rad) if (a.y + rad < ny ) else ny
            minz = (a.z) - (rad) if (a.z - rad > 0) else 0
            maxz = (a.z) + (rad) if (a.z + rad < nz ) else nz
            boundingBox = set([ (i + j * (nx + 1) + k * (nx + 1) * (ny + 1)) for i in range (int(minx),int(maxx)+1)
                            for j in range (int(miny),int(maxy)+1) for k in range(int(minz),int(maxz)+1) ])

            for b in sorted(boundingBox):
                    if findRadius(a,nList[b]) <= rad:
                            filteredList.append(nList[b].num)
    return filteredList

rawDescription: Python Optimized Comparison Between List of Dict


code:
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Element(Base):
    __tablename__ = 'element'

    path = Column(String, primary_key=True)

    related = relationship('Element',
                           primaryjoin=
                                remote(foreign(path)).like(
                                        path.concat('/%')),
                           viewonly=True,
                           order_by=path)

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

sess = Session(e)
sess.add_all([
    Element(path="/foo"),
    Element(path="/foo/bar1"),
    Element(path="/foo/bar2"),
    Element(path="/foo/bar2/bat1"),
    Element(path="/foo/bar2/bat2"),
    Element(path="/foo/bar3"),
    Element(path="/bar"),
    Element(path="/bar/bat1")
])

e1 = sess.query(Element).filter_by(path="/foo/bar2").first()
print [e.path for e in e1.related]

rawDescription: Materialized path relationship in declarative SQLAlchemy


code:
from operator import mul
print sorted([(3,2,3), (2,2,2), (6,4,1)], key=lambda tup: reduce(mul, tup), reverse=True)

rawDescription: Sort an array of tuples by product in python


code:
import random
import os

if __name__=='__main__':

    books = ['Genesis', 'Exodus', 'Leviticus', 'Numbers', 'Deuteronomy',
             'Joshua', 'Judges', 'Ruth', 'I Samuel', 'II Samuel', 'I Kings',
             'II Kings', 'I Chronicles', 'II Chronicles', 'Ezra', 'Nehemiah',
             'Esther', 'Job', 'Psalms', 'Proverbs', 'Ecclesiastes',
             'Song of Solomon', 'Isaiah', 'Jeremiah', 'Lamentations',
             'Ezekiel', 'Daniel', 'Hosea', 'Joel', 'Amos', 'Obadiah', 'Jonah',
             'Micah', 'Nahum', 'Habakkuk', 'Zephaniah', 'Haggai', 'Zechariah',
             'Malachi', 'Matthew', 'Mark', 'Luke', 'John', 'Acts', 'Romans',
             'I Corinthians', 'II Corinthians', 'Galatians', 'Ephesians',
             'Philippians', 'Colossians', 'I Thessalonians',
             'II Thessalonians', 'I Timothy', 'II Timothy', 'Titus',
             'Philemon', 'Hebrews', 'James', 'I Peter', 'II Peter', 'I John',
             'II John', 'III John', 'Jude', 'Revelation']

    mydict_book = dict((bname,i) for i,bname in enumerate(books,start=1))

while True:
    seleCtion = raw_input(" The Bible has a number of %s books.\n" % len(books) \
                          + "Select Next to see them below:\n" \
                          + "{0:^78}".format("[1]Print All books   [2]Start Quiz\n"))

    if seleCtion == '1':
        print '\n'.join('{0:3d}. {1}'.format(i, bname) for i,bname in mydict_book.iteritems())
        break

    elif seleCtion == '2':
        print "{0:^78}\nLet's start the quiz:\n\n".format("Welcome to the Bible book quiz!\n\n")
        break

    else:
        print 'You must select 1 or 2'

while 1:
    try:
        num_ques = int(raw_input('\nEnter the number of questions you wanna be asked: '))
        sampler  = random.sample(books, num_ques)
        if not sampler:  exit(0)

        num_sampler = map(lambda bname: mydict_book[bname] , sampler)
        print 'num_sampler==',num_sampler

        print ('\n\n\nHere are the first %s books in the quiz: \n%s'
               '\nMatch the correct numeric positions below :\n%s') \
               % (num_ques,  str(sampler)[1:-1],  str(random.sample(num_sampler,num_ques))[1:-1])

        samp = [ int(raw_input('\nWhich number is %s : ' % books[i-1])) for i in num_sampler ]

        your = zip(samp,sampler)
        your.sort()
        print '\nYour Answers yield:\n' + '\n'.join('%2s. %s' % (n,bk) for n,bk in your)

        correct = zip(num_sampler,sampler)
        correct.sort()
        print "\nThe Correct Answers are:\n" + '\n'.join('%2s - %s' % (n,bk) for n,bk in correct)

        continue

    except ValueError:
        break

rawDescription: What is a straight forward approach to matching and enumerating the results of a quiz using Python


code:
import threading

def printit():
  threading.Timer(5.0, printit).start()
  print "Hello, World!"

printit()

# continue with the rest of your code

rawDescription: Run certain code every n seconds

description: Run certain code every n seconds


code:
for Mvalue in M:
#iterate over all values
    result=0.0
    for i in range(1,int(Mvalue)):
        #calculate the sum (**2 is squared in python)
        result=result+(y[i+1]-y[i])**2
    #divide result
    result=result/(2*(Mvalue-1))
    print result

rawDescription: Python, calculation of non-overlapping AVAR


code:
a = 3

def foo(param):
        if param == 1:
            print 1
            return None
        print param
        foo(param - 1)

foo(a)

rawDescription: Alternate to global variable in Python

description: Alternate to global variable in Python


code:
en_freq="ETAOINSHDLUCMFYWGPBVKXQJZ" #from http://www.math.cornell.edu/~mec/2003-2004/cryptography/subs/frequencies.html
encoded_text =open("encrypted.txt").read().upper()#normalize to uppercase
sorted_my_frequency = Counter(encoded_text).most_frequent(len(en_freq)) #we want the 25 most frequent characters (sorted
my_frequency=join(sorted_my_frequency)[:len(en_freq)]

translation_table = string.maketrans(my_frequency,en_freq) #map our most frequent  to expected english frequencies

print encoded_text.translate(translation_table) #apply the translation_table
#note that you need a fairly large ammount of text for this to work very well ... and you will likely still need to manually translate some parts

rawDescription: Subtitute all letters in a string

description: Subtitute all letters in a string


code:
from optparse import OptionParser

parser = OptionParser()

parser.add_option("-t","--TIMEOUT", dest="timeout", type="int",  help="timeout in seconds")

options, args = parser.parse_args()
if options.timeout is not None and options.timeout <= 0:
    print "Timeout if specified must be greater than zero"
    sys.exit(CLI_ERROR)

rawDescription: python OptionParser.has_option error

description: Python OptionParser.has_option error


code:
try:
    beanstalk = beanstalkc.Connection(host="my.host.com")
    beanstalk.watch("update_queue")
except:
    print "Error connecting to beanstalk"

while True:
    job = beanstalk.reserve()
    do_something_with_job(job)

rawDescription: Python message to other applications

description: Python message to other applications


code:
class C:
    def my_method(self):
        print "I am a C"

c = C()
c.my_method()  # Prints "I am a C"

rawDescription: What is a "method" in Python?

description: What is a "method" in Python


code:
import os, sys, time

r,w=os.pipe()
r,w=os.fdopen(r,'r',0), os.fdopen(w,'w',0)

pid = os.fork()
if pid:          # Parent
    w.close()
    while 1:
        data=r.readline()
        if not data: break
        print "parent read: " + data.strip()
else:           # Child
    r.close()
    for i in range(10):
        print >>w, "line %s" % i
        w.flush()
        time.sleep(1)

rawDescription: Python program using os.pipe and os.fork() issue


rawDescription: How to force specific Yaml list type

description: Force specific Yaml list type


code:
#passRandomDraw.py
import numpy
from mpi4py import MPI
comm = MPI.COMM_WORLD
rank = comm.Get_rank()

randNum = numpy.zeros(1)
rnk = -1 # EDIT

if rank == 1:
    randNum = numpy.random.random_sample(1)
    print "Process", rank, "drew the number", randNum[0]
    comm.Send(randNum, dest=0, tag=rank) # EDIT

if rank == 0:
    print "Process", rank, "before receiving has the number", randNum[0]
    print "Sender rank:", rnk
    status = MPI.Status()
    comm.Recv(randNum, source=MPI.ANY_SOURCE, tag=MPI.ANY_TAG, status=status) # EDIT
    rnk = status.Get_source()
    print "Process", rank, "received the number", randNum[0]
    print "Sender rank:", rnk # EDIT

rawDescription: mpi4py Send/Recv with tag

description: Mpi4py Send/Recv with tag


code:
s = ""
for i in range(3):
    s+= 'Hi'
print s

rawDescription: How to print strings in a for loop without space in one line?

description: Print strings in a for loop without space in one line


code:
from bs4 import BeautifulSoup

html = '''Example'''

soup = BeautifulSoup(html)

for a in soup.find_all('a', href=True):
    print "Found the URL:", a['href']

rawDescription: How do I scrape the link from an <a> string in one line?

description: Scrape the link from an <a> string in one line


code:
keys =['(Lag)=(\d+\.?\d*)',
    '(Autocorrelation Index): (\d+\.?\d*)',
    '(Semivariance): (\d+\.?\d*)']

import re
string1 = ''.join(open("dummy.txt").readlines())

sections = string1.split('Spatial Statistics')

output = []
heads = []

for isec, sec in enumerate(sections):
    found = []
    output.append([])
    for key in keys:
        found.extend(re.findall(key, sec))
    for result in found:
        print '%s  =  %s' % (result[0],result[1])
        output[-1].append(result[1])
    if len(found) > 0 & len(heads) == 0:
        heads = [result[0] for result in found]

fout = open('output.csv', 'w')
wrt = csv.writer(fout)
wrt.writerow(heads)
wrt.writerows(outputs)
fout.close()

rawDescription: How to format an output in Python?

description: Format an output in Python


code:
def demo(displayHeartbeat):
    response = connect_to_stream()
    if response.status_code != 200:
        print response.text
        return
    for line in response.iter_lines(1):
        if line:
            try:
                msg = json.loads(line)
            except Exception as e:
                print "Caught exception when converting message into json\n" + str(e)
                return

            if displayHeartbeat:
                print line
            else:
                if msg.has_key("instrument") or msg.has_key("tick"):
                    print msg["tick"]["ask"] - .001
                    instrument = msg["tick"]["instrument"]
                    time = msg["tick"]["time"]
                    bid = msg["tick"]["bid"]
                    ask = msg["tick"]["ask"]

rawDescription: Parse JSON output in Python using Requests and its sessions


code:
from Tkinter import *
from tkFileDialog import askopenfilename

value_list = []

def callback():
    name = askopenfilename()
    value_list.append(name)
    print name

Button(text='File Open', command=callback).pack(fill=X)
mainloop()

# value_list[0] is filename

rawDescription: Passing a variable from Tkinter to main program


code:
pokemons = {
    'geodude': Pokemon("Geodude", 100, "Ground",
                  attacks =
                  {"Tackle":["Tackle",30,"Normal","35"],
                   "Rollout":["Rollout",50,"Rock","20"]
                  }),

    'pikachu': Pokemon("Pikachu", 100, "Lightning",
                  attacks =
                  {"Thunder Shock":["Thunder Shock",40,"Electric"],
                   "Quick Attack":["Quick Attack",40,"Normal"]
                  }),
}

def fight():
    current_pokemon_name = raw_input("pikachu or geodude? > ")
    current_pokemon = pokemons[current_pokemon_name]
    print current_pokemon.name
    print current_pokemon.attacks

rawDescription: Python 'str' object has no attribute 'name' when using a function variable to call a class instance


code:
import types

class A(object):
    def do_computing(self):
        print "do_computing"

methods = {name:value for name, value in A.__dict__.iteritems()
                        if not name.startswith('_')}

instance_class = type('B', (object,), {'a': '#A', 'b': '#B'})

for name, func in methods.iteritems():
    new_method = types.MethodType(func, None, instance_class)
    setattr(instance_class, name, new_method)

instance_class().do_computing()

rawDescription: How to share same method on two different classes in python

description: Share same method on two different classes in python


code:
def block_reverse(lst, blocksize):
    for i in range(blocksize-1, len(lst), blocksize):
        if i<blocksize:
            lst[:blocksize]=lst[blocksize-1::-1]
        else:
            lst[i-blocksize+1:i+1] = lst[i:i-blocksize:-1]

m=[[4,4,4],[5,5,5],[6,6,6],[7,7,7]]
block_reverse(m, 2)
print m

rawDescription: How to invert a list for a specific range for entire list?

description: Invert a list for a specific range for entire list


code:
import xlrd

def processExcel(excelFile):
    excelData = xlrd.open_workbook(excelFile, on_demand=True)
    sheets = excelData.sheet_names()
    print sheets

rawDescription: Xlrd very slow opening excel file

description: Xlrd very slow opening excel file


code:
content = si.RetrieveContent()
host = content.searchIndex.FindByDnsName(dnsName="DC0_C0_H0", vmSearch=False)
print host.summary.quickStats.uptime

description: Pyvmomi retrieving vim.host.Summary.QuickStats


code:
import numpy as np
from functools import partial
from multiprocessing import Process, Pool
import multiprocessing.managers

x = 3;
y = 3;
z = 10;

class MyManager(multiprocessing.managers.BaseManager):
    pass
MyManager.register('np_zeros', np.zeros, multiprocessing.managers.ArrayProxy)

para = []
process = []
def local_func(ar, section):
    print "section %s" % str(section)
    ar[2,2,section] = 255
    print "value set %d", ar[2,2,section]

if __name__ == "__main__":
    m = MyManager()
    m.start()
    ar = m.np_zeros((x,y,z))

    pool = Pool(1)

    run_list = range(0,10)
    func = partial(local_func, ar)
    list_of_results = pool.map(func, run_list)

    print ar

rawDescription: Trying to use multithreading to fill an array in python


code:
#!/usr/bin/env python
def func():
    print 'func called'

class MyClass(object):
    FUNC = staticmethod(func)

    def call_func(self):
        MyClass.FUNC()

instance = MyClass()
instance.call_func()

rawDescription: Class variable pointing to a method behaves different


code:
class Tree(object):

  def __init__(self, entry):
      self.entry = entry
      self.left = None
      self.right = None

  def __str__(self):
      return '<%s, %d, %s>' % (self.left, self.entry, self.right)

def insert(item, tree):
    if item < tree.entry:
        if tree.left is not None:
            insert(item, tree.left)
        else:
            tree.left = Tree(item)
    else:
        if tree.right is not None:
            insert(item, tree.right)
        else:
            tree.right = Tree(item)

root = Tree(1)
insert(0, root)
insert(2, root)
print root

rawDescription: How to build a binary search tree with python without pointers?

description: Build a binary search tree with python without pointers


code:
import itertools
print [i for i in itertools.permutations([1,2,3,4,5])]

rawDescription: How to make a loop to try all sorting possibilities of x items in Python

description: Make a loop to try all sorting possibilities of x items in Python


code:
from django.db import connection
q = A.objects.filter(NAME__istartswith='z').order_by('ORDER').select_related('b', 'c')
for obj in q:
   obj = obj.b or obj.c or obj
   print repr(obj), obj.__dict__ # (to prove the subclass-specific attributes exist)
print "query count:", len(connection.queries)

rawDescription: Fetching inherited model objects in django


code:
import libvirt
import time
import threading

def callback(conn, dom, event, detail, opaque):
    print "EVENT: Domain %s(%s) %s %s" % (dom.name(),
                                          dom.ID(),
                                          event,
                                          detail)

eventLoopThread = None

def virEventLoopNativeRun():
    while True:
        libvirt.virEventRunDefaultImpl()

def virEventLoopNativeStart():
    global eventLoopThread
    libvirt.virEventRegisterDefaultImpl()
    eventLoopThread = threading.Thread(target=virEventLoopNativeRun,
                                       name="libvirtEventLoop")
    eventLoopThread.setDaemon(True)
    eventLoopThread.start()

if __name__ == '__main__':

    virEventLoopNativeStart()

    conn = libvirt.openReadOnly('qemu:///system')

    conn.domainEventRegister(callback, None)
    conn.setKeepAlive(5, 3)

    while conn.isAlive() == 1:
        time.sleep(1)

rawDescription: Can I register event callbacks using the libvirt Python module with a QEMU backend?

description: Register event callbacks using the libvirt Python module with a QEMU backend


code:
import re
import sys
import time

# Tested with PySide 1.0.9, changing imports to PyQt should work identically
from PySide.QtCore import Qt, QUrl
from PySide.QtGui import QApplication, QImage, QPainter
from PySide.QtWebKit import QWebPage, QWebSettings

def os_safe_name(url):
    url = re.sub("[^a-zA-Z0-9_-]+", "_", url)
    url = re.sub("_{2,}", "_", url)
    return url

class Render(QWebPage):
    def __init__(self, url):
        QWebPage.__init__(self)

        self.url = url
        self.finished = False

        # Settings
        s = self.settings()
        #s.setAttribute(QWebSettings.AutoLoadImages, False)
        s.setAttribute(QWebSettings.JavascriptCanOpenWindows, False)
        s.setAttribute(QWebSettings.PluginsEnabled, True)

        #self.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
        self.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)

        # When page is loaded, callback saves image to file
        self.loadFinished.connect(self._loadFinished)

        self.mainFrame().load(QUrl(url))

    def _loadFinished(self, result):
        frame = self.mainFrame()
        size = frame.contentsSize()
        size.setWidth(1366)
        self.setViewportSize(size)

        image = QImage(self.viewportSize(), QImage.Format_ARGB32)

        painter = QPainter(image)
        frame.render(painter)
        painter.end()

        self.filepath = "output/%s.png" % os_safe_name(self.url)
        image.save(self.filepath)

        self.finished = True

def run(url, app = None):
    if app is None:
        app = QApplication(sys.argv)

    r = Render(url)

    while not r.finished:
        app.processEvents()
        time.sleep(0.01)

    return r.filepath

if __name__ == '__main__':
    app = QApplication(sys.argv)

    print run("http://stackoverflow.com", app=app)
    print run("http://google.com", app=app)

rawDescription: Screenshot of multiple webpages in a headerless QtWebKit browser in Python


code:
p = raw_input(("plz enter a password to check it's strength"))

upper_case = 0
lower_case = 0
number = 0
symbol = 0

for i in p:
    if i.isupper():
        upper_case += 1
    elif i.islower():
        lower_case += 1
    elif i.isdigit():
        number += 1
    else:
        symbol += 1

if len (p) <= 6:
    print"This is a weak password  "
elif upper_case > 0 and lower_case > 0 and number > 0 and symbol > 0:
    print "Good"
else:
    print "Too Weak"

rawDescription: python password checker: numbers and symbols

description: Python password checker: numbers and symbols


code:
print "
Review 	<input type=\"textarea\" name=\"Review\" cols=\"50\" rows=\"5\" />"
print """
    Enter some text...
    </textarea>

"""

rawDescription: HTML Python Correct Syntax

description: HTML Python Correct Syntax


code:
"""
Distributes a number N of queries over a set of M user accounts
"""
from itertools import izip, cycle, groupby
from threading import Thread

def run_query(account, queries):
    """Run a number of queries under the same account"""
    user = account[0]
    passw = account[1]
    for query in queries:
        print 'The executed query will use: ' + user + ' ' + passw + ' ' + ' ' + query + '\n'

def main():
    """Distributes the queries and then runs them in threads"""
    accounts = [('user1', 'pass1'), ('user2', 'pass2'), ('userM', 'passM')]
    queries = ['query1', 'query2', 'query3', 'queryN']

    assignments = list(izip(cycle(accounts), queries))
    assignments = sorted(assignments, key=lambda (account, query): account)
    # [(('user1', 'pass1'), 'query1'), (('user1', 'pass1'), 'queryN'),
    #  (('user2', 'pass2'), 'query2'),
    #  (('userM', 'passM'), 'query3')]

    for account, assigned in groupby(assignments, lambda (account, query): account):
        queries = [item[1] for item in list(assigned)]
        Thread(target=run_query, args=(account, queries)).start()

if __name__ == '__main__':
    main()

rawDescription: Python multithreading using a different setting per thread


code:
def PrintDescription():
    print 'This program encrypts and descrypts messages using multiple \
encryption methods.\nInput files must be in the same directory as this program.\
\nOutput files will be created in this same directory.'

def GetChoice(acceptable_answers):
    while True:
        user_choice = raw_input('')
        if user_choice in acceptable_answers:
            return user_choice
        else:
            print 'Please try:', ', '.join(acceptable_answers)

def StartMenu():
    print 'Do you wish to encrypt or decrypt?'
    print '<e>ncrypt'
    print '<d>ecrypt'
    print '<q>uit'
    user_choice = GetChoice('edq')
    return user_choice

def MethodMenu():
    print 'Which method would you like to use?'
    print '<c>aesarian fixed offset'
    print '<p>seudo-random offset'
    print '<s>ubstitution cipher'
    user_choice = GetChoice('cps')
    return user_choice

def main():
    alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.?! \t\n\r"
    PrintDescription()

    while True:
        user_choice = StartMenu()
        if user_choice in ('e', 'd'):
            user_choice = MethodMenu()
            # Do something based on the user_choice
        if user_choice == 'q':
            break

main()

rawDescription: Need Assistance with my Encryption Program


code:
from nltk.corpus import brown
from nltk import batch_ne_chunk as bnc
from nltk.tree import Tree
sentences = brown.tagged_sents()[0:5]
chunk_sents = bnc(sentences)

for sent in chunk_sents:
    for i,j in zip(sent[:-1], sent[1:]):
            if type(j) is Tree and i[1].startswith("JJ"):
                    print i,j

rawDescription: Adjectives used with named entities

description: Adjectives used with named entities


code:
class Vehicle(object):
    def __init__(self, year):
        self.year = year

class Car(Vehicle):
    def __init__(self, name, year):
        super(Car, self).__init__(year)
        self.name = name

a = Vehicle(1995)
print a.year
b = Car("test", 1992)
print b.name
print b.year

rawDescription: Understand classes in Python?

description: Understand classes in Python


code:
import StringIO
line = StringIO.StringIO()
line.write("Hello World")
line.seek(0)
print line.getvalue()
line.seek(1)
print line.getvalue()
line.seek(1)
print next(line)
line.seek(1)
print line.read()

rawDescription: How can i remove the first character from a StringIO object?

description: Remove the first character from a StringIO object


code: print range(20) # doctest:+ELLIPSIS

rawDescription: Can python doctest ignore some output lines?


code:
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)

rawDescription: Python Time Seconds to h:m:s

description: Python Time Seconds to h:m:s


rawDescription: List coordinates between a set of coordinates


code:
from jinja2 import Environment
Environment(auto_reload=False, loader = moduleloader.FileSystemModuleLoader(package))

class ModuleLoader(object):
    """Base mixin class for loaders that use pre-parsed Jinja2 templates stored
    as Python code.
    """
    def get_module(self, environment, template):
        raise TemplateNotFound(template)

    def load(self, environment, filename, j_globals=None):
        """Loads a pre-compiled template, stored as Python code in a template
        module.
        """
        if j_globals is None: j_globals = {'environment' : environment}

        t = object.__new__(environment.template_class)

        module = self.get_module(environment, filename)
        name, blocks, root, debug_info = module.run(environment, t)   # module run function

        t.environment = environment
        t.globals = j_globals
        t.name = name
        t.filename = filename
        t.blocks = blocks
        # render function and module
        t.root_render_func = root
        t._module = None

        # debug and loader helpers
        t._debug_info = debug_info
        t._uptodate = lambda: True
        return t

class FileSystemModuleLoader(ModuleLoader):

    def __init__(self, package = None):   # load module from package or datastore
        self.package = package            # package = "compiled" or package = None

    def get_module(self, environment, template):
        # Convert the path to a module name
        name = template.replace('.html', '').replace('.txt','').replace('/', '.')   # NO extensions
        module = None

        if self.package == None :
            if name in sys.modules : return sys.modules[name]
            logging.info('load module : ' + name)      # load module from runtimes db
            try :
                runtime = models.Runtimes.get_by_key_name(template)
                module_code = db.Text(runtime.compiled)
                module = imp.new_module(name)
                exec module_code in module.__dict__
                sys.modules[name] = module             # add to sys modules, so no import
                return module
            except (ImportError, AttributeError):
                logging.error('load failed : ' + name)

        else :                                         # load module from package
            logging.info('load module : ' + name + ' from package : ' + self.package)
            try:
                mod_import = __import__(self.package, globals(), None, [str(name)])
                module = getattr(mod_import, name)
                return module
            except (ImportError, AttributeError):
                logging.error('load failed : ' + name)

        raise TemplateNotFound(template)

rawDescription: loading Jinja templates from python modules (pre-compiled templates)

description: Loading Jinja templates from python modules (pre-compiled templates)


code:
cat = []
with open(file) as f2:
    for lines in f2:
        if "cat /etc/passwd" in lines:
            for i in range(3):
                cat.append(f2.readline())

print cat

rawDescription: returning value outside a for loop

description: Returning value outside a for loop


rawDescription: split list of tuples in lists of list of tuples

description: Split list of tuples in lists of list of tuples


code:
filename = raw_input("Please enter filename: ") #This is already a string
if len(filename.split('.')[-1])==3:
   print "Valid extension"
else:
   print "Invalid extension!"

rawDescription: How do I look up for a specific part / extension of file name?

description: Look up for a specific part / extension of file name


code:
from my_projects.tools import resolve_to_name
print resolve_to_name('/some/url')
print resolve_to_name('/some/other/url')

rawDescription: Django 'resolve' : get the url name instead of the view_function


code:
for value in sorted(a.values(), reverse=True):
    print value

rawDescription: Using dictionaries in loop

description: Using dictionaries in loop


code:
datafile = open("temp.txt", "r")
record = datafile.readline()

while record != '':
    d1 = record.strip("\n").split(",")
    print d1[0],float (d1[1])
    record = datafile.readline()

datafile.close()

rawDescription: python text reading

description: Python text reading


code:
for item in folderContents:
    Beginn = int(item.Beginn.strftime('%Y'))
    Ende = int(item.Ende.strftime('%Y'))

    for year in range(Beginn, Ende + 1):
        print year

rawDescription: show years between begin and end

description: Show years between begin and end


code:
for date, cell in zip(dates, datecells):
    self.driver.execute_script("arguments[0].scrollIntoView(true);", cell)
    print "'{0}', '{1}'".format(date, cell.text)
    self.assertEqual(date, cell.text)

rawDescription: Issues with Python Selenium WebElements and .text


code:
def myfunc():
  exec 'myvar="boooh!"' in globals()

myfunc()
myvar

rawDescription: Cannot change global variables in a function through an exec() statement?


code:
a = range(1,21)
num_row = 5

a = map(str, a)
max_length = len(max(a, key=len))
for i, x in enumerate(a, 1):
    print x.rjust(max_length),
    if i % num_row == 0:
        print

rawDescription: Thinking Python

description: Thinking Python


rawDescription: Unicode values in strings are escaped when dumping to JSON in Python


code:
A = np.argwhere(bins<data)
print A[np.r_[A[1:,0] != A[:-1,0], True]]

rawDescription: How to pythonically get the max of a numpy argwhere function

description: Pythonically get the max of a numpy argwhere function


code:
import os
myfiles =[]
for root, dirnames, filenames in os.walk("./"):
    for filename in filenames:
        if(filename.endswith(".txt")):
            myfiles.append("%s/%s" % (root, filename))
print myfiles

rawDescription: Use glob.glob to search recursively for *.txt files, from current directory down


code: print max([test[i] for i in foo])

rawDescription: Python - max in an list within another list


code:
a = [ 1, 2, 3]
def myfunc(a, b, c):
    return a + b + c

print myfunc(*a)

rawDescription: Passing arguments to functions in Python using inspect


rawDescription: Iterating through a List of Lists and calling variables explicitly


code:
def calc_change(n, coins):
    cache = {}
    c = max(coins)
    nums = calc_ways(n, coins, cache, c)
    return nums

def calc_ways(n, coins, cache, current_coin):
    if n < 0:
        return 0
    if n == 0:
        return 1
    if (n,current_coin) not in cache:
        cache[(n,current_coin)] = sum(calc_ways(n - c, coins, cache, c) for c in coins if c <= current_coin)
    return cache[(n,current_coin)]

answer = calc_change(n, coins)
print answer

rawDescription: Why this solution isn't working for the coin change algorithm?


code:
import os

pid = os.getppid()
with open("/proc/%s/cmdline" % pid) as f:
    print f.readline()

rawDescription: Amarok 1.4 script: knowing who's running you


code:
file1 = open("data.txt","r")
file2=open("newdata.txt","w")
lines = [ line.rstrip() for line in file1.readlines() ]

firstIndex = lines.index("#02textline2")
secondIndex = lines.index("#03textline3")

print firstIndex, secondIndex
file2.write("\n".join(lines[firstIndex  + 1 : secondIndex]))

file1.close()
file2.close()

rawDescription: Read specific number of lines in python

description: Read specific number of lines in python


code:
import wx

class Frame(wx.Frame):
    def __init__(self):
        super(Frame, self).__init__(None)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
    def OnEraseBackground(self, event):
        pass # do nothing
    def OnLeftDown(self, event):
        print event.GetPosition()
    def OnKeyDown(self, event):
        if event.GetKeyCode() == wx.WXK_ESCAPE:
            self.Close()
        else:
            event.Skip()
    def OnPaint(self, event):
        w, h = self.GetSize()
        dc = wx.PaintDC(self)
        region = wx.RegionFromPoints([(0, 0), (w, 0), (w, h), (0, h)])
        box = wx.RegionFromPoints([(100, 100), (500, 100), (500, 500), (100, 500)])
        region.SubtractRegion(box)
        dc.SetClippingRegionAsRegion(region)
        dc.DrawRectangle(0, 0, w, h)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame()
    frame.ShowFullScreen(True)
    app.MainLoop()

rawDescription: How to draw a transparent section within an opaque area

description: Draw a transparent section within an opaque area


code:
def AddCheckButton(self, button):
    button = gtk.CheckButton("CheckButton")
    self.vBox.pack_start(button, True, True, 1)
    button.show()
    print "adding checkbox..."

rawDescription: update a gtk.VBox dynamically

description: Update a gtk.VBox dynamically


code:
data = dict()
data['venue1'] = dict()
data['venue1']['time1'] = 'something'
print data

rawDescription: Python and multidimensional dictionaries


code:
x="""...-1AG.,., should be transformed as ...G.,.,
..,-1A,.,., should be transformed as ..,,.,.,
...-2GTC,., should be transformed as ...C,.,
..,-2GT.,., should be transformed as ..,.,.,
...+3TAGT,, should be transformed as ...T,,
..,+3TAG.,. should be transformed as ..,.,."""

def repl(matchobj):
    return matchobj.group(2)[int(matchobj.group(1)):]

print re.sub(r"[+-](\d+)([a-zA-Z]+)",repl,x)

rawDescription: Substituion of variable pattern with RegEx in Python


code:
def findGrid(n):   #This function takes one parameter.
    row = n + 1
    col = row ** 2

    return  row, col                  #Return two values

def calculate(a, b):
    a = a + 2
    b = a // 2

    return a, b

def main():
    x = int(input("Enter a number: "))
    y,z = findGrid(x)
    a,b = calculate(y, z)
    print a
    print b

main()

rawDescription: How to use two returned values from a function in another function? MAIN()

description: Use two returned values from a function in another function? MAIN()


code:
filesInChannel = [
"https://www.youtube.com/watch?v=PYuNBFdwK7k",
"https://www.youtube.com/watch?v=-Ox9MvottBI"
]

def getStats(link):
    page = requests.get(link)
    likes = re.search("with (\d*.\d*.\d*)", page.text).group(1)
    title = re.search("property=\"og:title\" content=\"([^\n]*)", page.text).group(1)
    return (likes, title)

for link in filesInChannel:
    stats = getStats(link)
    print stats[0].encode("utf-8") + " " + stats[1].encode("utf-8")

rawDescription: How to retrieve like/dislike/view count from Youtube?

description: Retrieve like/dislike/view count from Youtube


code:
ruler = dict((1<<i, i+1) for i in xrange(63))

for i in xrange(1, 20):
    print ruler[i & ~(i-1)],

rawDescription: iterative implementation of the ruler function (1,2,1,3,1,2,1,4,1,2,1,3,...)

description: Iterative implementation of the ruler function (1,2,1,3,1,2,1,4,1,2,1,3,...)


code: print query_params

rawDescription: Why does urllib.urlencode add square brackets and single quotes to my dict values?


code:
from collections import Counter

with open('input.tsv') as infile:
    counts = Counter(infile.read())

for c in 'SF':
    print '{}: {}'.format(c, counts.get(c))

rawDescription: python 3 Acess file and check number of time phrase comes up

description: Python 3 Acess file and check number of time phrase comes up


code:
d = {24:[1, 45],25:[4, 584]}

def ReturnListRef():
    a = 24
    return a

def foo():
    var = d[ReturnListRef()]
    print var
print  foo()
[1, 45]

rawDescription: Python: Using a returned value to reference another variable?


code:
import Tkinter as tk

def print_non_empty():
    for i, entry in enumerate(entries):
        output = entry.get()
        if output:
            print 'Entry {}: {}'.format(i, output)
    print ''

root = tk.Tk()
entries=[]

for i in range(5):
    entries.append(tk.Entry(root))
    entries[-1].pack()

tk.Button(root, text='show non-empty entries', command=print_non_empty).pack()

root.mainloop()

rawDescription: Insert only populated entry python tkinter


code:
try:
    if 0 < float(input) < 1:
        raise ValueError("invalid input value")
    print "Valid input"
except (TypeError, ValueError):
    print "Invalid input"

rawDescription: Better way to fail a try in python if result is valid but not wanted


code:
prefixes = 'JKLMNOPQ'
prefixes_taking_u = 'OQ'
suffix = 'ack'
for p in prefixes:
    u_part = 'u' if p in prefixes_taking_u else ''
    print p + u_part + suffix

rawDescription: Using "or" in if statement conditions

description: Using "or" in if statement conditions


code:
# define a sorting function based on the first numer, assuimg columns are
# separated by space or tab
f = lambda line: float(line.split()[0])
# opened file in Python is an iterator, so could be served to max() directly
with open('your_input_file') as inf:
    line_with_max_num = max(inf, key=f)
# turn the other four numbers into a list and print them to the screen
# or do whatever you like with them
print [float(_) for _ in line_with_max_num.split()[1:]]

rawDescription: python: findimg maximum number in first column of a file and considering other numbers on the same line in different columns

description: Python: findimg maximum number in first column of a file and considering other numbers on the same line in different columns


code:
(X, Y) = read_data()
F = langrange_polynomial(X, Y)
print F(user_defined_x)

rawDescription: How to calculate f(x) of a 6th degree function

description: Calculate f(x) of a 6th degree function


code:
a = [("one","two"), ("bad","good")]
print "\n".join(j for i in a for j in i)
for i in a:
 print "\n".join(i)

rawDescription: Python: Nested Loop

description: Python: Nested Loop


code:
class Node(object):
    def __init__(self, data):
        self.data = data

if __name__ == "__main__":
    a = Node(5)
    a = a.__dict__ #whoops, now I lost the object, I just have the dict
    b = Node.__new__(Node) # make a new empty object
    b.__dict__ = a.copy() # copy in the dict
    # copy because there might still a reference to the object elsewhere
    # if you want changes to b to affect a, then don't copy just assign
    print b.__dict__
    print b

rawDescription: How to turn object.__dict__ into the object itself again?

description: Turn object.__dict__ into the object itself again


code:
import re
match = re.search('^[^\s]+,\s*([a-zA-Z]{2})$', my_string)
if match:
    print match.groups()[0]

rawDescription: Referencing a RegEx Variable

description: Referencing a RegEx Variable


code:
import nltk

text = 'Son, if you really want something in this life,\
        you have to work for it. Now quiet! They are about\
        to announce the lottery numbers.'

stopwords = ['in', 'to', 'for', 'the']

sents = []

for sent in nltk.sent_tokenize(text):

    tokens = nltk.word_tokenize(sent)
    sents.append(' '.join([w for w in tokens if w not in stopwords]))

print sents

rawDescription: filtering stopwords near punctuation

description: Filtering stopwords near punctuation


code:
import dns.name
import dns.message
import dns.query
import dns.flags

domain = 'google.com'
name_server = '8.8.8.8'
ADDITIONAL_RDCLASS = 65535

domain = dns.name.from_text(domain)
if not domain.is_absolute():
    domain = domain.concatenate(dns.name.root)

request = dns.message.make_query(domain, dns.rdatatype.ANY)
request.flags |= dns.flags.AD
request.find_rrset(request.additional, dns.name.root, ADDITIONAL_RDCLASS,
                   dns.rdatatype.OPT, create=True, force_unique=True)
response = dns.query.udp(request, name_server)

print response.answer
print response.additional
print response.authority

rawDescription: How make dns queries in dns-python as dig (with aditional records section)?


code:
class loginwindow(object):
    def __init__(self, parent):
        self.parent=parent
        self.data=None #Default value, to say its not been set yet
        self.root=Toplevel(self.parent)
        self.root.transient(self.parent)
        self.username=Entry(self.root); self.username.pack()
        self.password=Entry(self.root, show="*"); self.password.pack()
        self.ok=Button(self.root, text="Continue", command=self.checkPass); self.ok.pack()
        self.cancel=Button(self.root, text="Cancel", command=self.cancelPass); self.cancel.pack()

    def checkPass(self):
        self.data=(self.username.get(), self.password.get())
        self.root.destroy()

    def cancelPass(self):
        self.data=False
        self.root.destroy()

def popup():
    passWindow=loginwindow(parent)
    passWindow.parent.wait_window(passWindow.root)
    print passWindow.data

parent=Tk()
Button(parent, text='popup', command=popup).pack()
parent.mainloop()

rawDescription: Transient input window

description: Transient input window


code:
import pylibmc

client=pylibmc.Client(["127.0.0.1:21201"]) # port for memcachedb

print "return value  " + str(client.set("key","value"))

print "get result  " + str(client.get("key"))

rawDescription: How to connect to memcachedb and use API

description: Connect to memcachedb and use API


code:
import re

def clean_split(string_input):
    """
    Split a string into its component tokens and return as list
    Treat spaces and punctuations, including in-word apostrophes as separate tokens

    >>> clean_split("it's a good day today!")
    ["it", "'", "s", " ", "a", " ", "good", " ", "day", " ", "today", "!"]
    """
    return re.findall(r"[\w]+|[^\w]", string_input)

with open('replace.txt' , 'r') as myr:
    replacements = dict(tuple(line.split()) for line in myr)

with open('mytext.txt', 'r') as myf:
    for line in myf:
        print ''.join(replacements.get(word, word) for word in clean_split(line)),

rawDescription: Replace words with word-substitutions from another file


code:
for line in code.readlines():
    for sign in '({[<+-=*/%;>]})':
        line = line.replace(sign, ' %s ' % sign)
    for word in line.split():
        print word

rawDescription: How to split code string properly

description: Split code string properly


code:
for i in soup.findAll('tr'):
    for tdi in i.findAll('td'):
            print tdi.text.strip(),
    print

rawDescription: How to parse name and value from html files

description: Parse name and value from html files


code:
import threading, time

def printCount(lock, stop):
    with lock:
        print "Thread B started"
    x = 0
    while not stop.is_set():
        time.sleep(1)
        x = x + 1
        with lock:
            print x

class ThreadHandler():
    STEP = 0.2

    def __init__(self, target):
        self.lock = threading.Lock()
        self.stop = threading.Event()
        args = (self.lock, self.stop)
        self.thread = threading.Thread(target=target, args=args)

    def start(self):
        self.thread.start()

    def join(self):
        while self.thread.is_alive():
            try:
                self.thread.join(self.STEP)
            except KeyboardInterrupt:
                self.stop.set()

## User Code ##
print "begin!"
handler = ThreadHandler(target=printCount)
handler.start()
with handler.lock:
    print "woop!"
handler.join()

rawDescription: Python Threading - Managing thread termination and the main thread


code:
import weakref
class Cursor: pass

_ = None # suppress special _ variable
a = Cursor()
r = weakref.ref(a)
print r()
del a
print r()

rawDescription: Python: How do I keep track of whether an object is still in 'use'?


rawDescription: How to force Python JSON output to exclude 'L' from longs

description: Force Python JSON output to exclude 'L' from longs


code:
import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, width=400,  height=400,
                                background="bisque")
        self.canvas.pack(fill="both", expand=True)

        graphic1 = GraphicObject(10,10,100,100, name="graphic1")
        graphic2 = GraphicObject(110,110,200,200, name="graphic2")

        graphic1.draw(self.canvas)
        graphic2.draw(self.canvas)

class GraphicObject(object):
    def __init__(self, x0,y0,x1,y1, name=None):
        self.coords = (x0,y0,x1,y1)
        self.name = name

    def draw(self, canvas, outline="black", fill="white"):
        item = canvas.create_oval(self.coords, outline=outline, fill=fill)
        canvas.tag_bind(item, "<1>", self.mouse_click)

    def mouse_click(self, event):
        print "I got a mouse click (%s)" % self.name

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

rawDescription: Python; tkinter; Canvas objects and events


code:
#Rajiv Ravishankar
#rravisha
#21-301 Assignment #1, Qns 4

from numpy import matrix
import itertools

def eligCheck(m):
    #We know certain properties of magic squares that can help identify if a 3x3 matrix is a magic square or not
    #These properties include the following checks listed below.
    #
    #
    #The main purpose of this function is to check is a 3x3 matrix is a magic square without having to add all the
    #rows, columns and diagonals.
    flag=0
    #Check 1 if the matrix is indeed 4x4
    if (len(m)==4 and len(m[0])==4 and len(m[1])==4 and len(m[2])==4):
        flag=flag+1
    #Check 2 if the 2nd diagonal adds up
    if (m[0][3] + m[1][2] + m[2][1] + m[3][0] == 34):
        flag=flag+1
    #Checks 2 if the first diagonal adds up
    if (m[0][0] + m[1][1] + m[2][2] + m[3][3] == 34):
        flag=flag+1
    #To save resources and increase efficency, only if all three checks return true will we add the rows and columns to check.
    if (flag==3):
        return True
    else:
        return False

def elementAdder(m):
    #This function is to be called only AFTER eligCheck() returns TRUE for a given matrix.  Since a 4x4 matrix that satisfies the checks
    #in eligCheck() does not mean that it is a magic square, we add each row, each column and both diagonals an see if the sum
    #is equal to 15.  Splitting into two function save processing power.
    #
    #
    #Checking if all rows add up to 15
    flag=0
    #Check 1 if row 1 adds up
    if (m[0][0]+m[0][1]+m[0][2]+m[0][3] == 34):
        flag=flag+1
    else:
        return False
    #Check 2 if row 2 adds up
    if (m[1][0]+m[1][1]+m[1][2]+m[1][3] == 34):
        flag=flag+1
    else:
        return False
    #Check 3 if row 3 adds up
    if (m[2][0]+m[2][1]+m[2][2]+m[2][3] == 34):
        flag=flag+1
    else:
        return False
    #Check if row 4 adds up
    if (m[3][0]+m[3][1]+m[3][2]+m[3][3] == 34):
        flag=flag+1
    else:
        return False
    #Check 4 if column 1 adds up
    if (m[0][0]+m[1][0]+m[2][0]+m[3][0] == 34):
        flag=flag+1
    else:
        return False
    #Check 5 if column 2 adds up
    if (m[0][1]+m[1][1]+m[2][1]+m[3][1] == 34):
        flag=flag+1
    else:
        return False
    #Check 6 if column 3 adds up
    if (m[0][2]+m[1][2]+m[2][2]+m[3][2] == 34):
        flag=flag+1
    else:
        return False
    #Check 7 if column 4 adds up
    if (m[0][3]+m[1][3]+m[2][3]+m[3][3] == 34):
        flag=flag+1
    else:
        return False
    #Note that diagonal checks have already been verified in eligCheck() represents the diagonal from left to right

    #The strategy here is to set flag as zero initially before the additiong checks and then run each check one after the other.  If a
    #check fails, the matrix is not a magic square.  For every check that passes, flag is incremented by 1.  Therefore, at the end of
    #all the check, if flag == 8, it is added proof that the matrix is a magic square.  This step is redundant as the program has been
    #written to stop checks as soon as a failed check is encountered as all checks need to be true for a magic square.
    if flag==8:
        print m
        return True
    else:
        print "**** FLAG ERROR: elementAdder(): Line 84 ***"
        print m

def dimensionScaler(n, lst):
    #This function converts and returns a 1-D list to a 2-D list based on the order.  #Square matrixes only.
    #n is the order here and lst is a 1-D list.
    i=0
    j=0
    x=0
    #mat = [[]*n for x in xrange(n)]
    mat=[]
    for i in range (0,n):
        mat.append([])
        for j in range (0,n):
            if (j*n+i<len(lst)):
                mat[i].append(lst[i*n+j])
    return mat

#mtrx=[]

def matrixGen():
#Brute forcing all possible 4x4 matrices according to the previous method will require 16!*32*16 bits or 1.07e6 GB of memory to be allocated in the RAM (impossible today)./, we
#use an alternative methos to solve this problem.
#
#
#We know that for the sums of the diagonals will be 34 in magic squares of order 4, so we can make some assumtions of the corner element values
#and also the middle 4 elements.  That is, the values of the diagonals.
#The strategy here is to assign one set of opposite corner elements as say 1 and 16 and the second as 13 and 4
#The remaining elements can be brute forced for combinations untill 5 magic squares are found.
    setPerms=itertools.permutations([2,3,5,6,7,8,9,10,11,12,14,15],12)
    final=[0]*16
    count=0
    #print final
    for i in setPerms:
        perm=list(i)
        setCorners=list(itertools.permutations([1,4,13,16],4))

        for j in range(0,len(setCorners)):
            final[0]=setCorners[j][0]
            final[1]=perm[0]
            final[2]=perm[1]
            final[3]=setCorners[j][1]
            final[4]=perm[2]
            final[5]=perm[3]
            final[6]=perm[4]
            final[7]=perm[5]
            final[8]=perm[6]
            final[9]=perm[7]
            final[10]=perm[8]
            final[11]=perm[9]
            final[12]=setCorners[j][2]
            final[13]=perm[10]
            final[14]=perm[11]
            final[15]=setCorners[j][3]
            if eligCheck(dimensionScaler(4,final))==True:
                elementAdder(dimensionScaler(4,final))

matrixGen()

rawDescription: How would I go about finding all possible permutations of a 4x4 matrix with static corner elements?


code:
import re

def matchCity(city_name):
    if re.search('kyo|par|omba', city_name):
        print "{} is a valid city name".format(city_name)

matchCity('tokyo') # tokyo is a valid city name
matchCity('paris') # paris is a valid city name
matchCity('bombay') # bombay is a valid city name
matchCity('chicago') # Doesn't print anything

rawDescription: Python: Comparing string variable against multiple sub-strings


code:
longest_word = ''
for word in filenames:
    with open(word) as w:
        x = w.read()
        y = max(x.split(), key = len)
    if len(y) > len(longest_word):
        longest_word = y
print '\nLongest Word:', longest_word

rawDescription: How to Compare two Files and Find the Lasgest Word between


code:
from collections import OrderedDict
number=input()

occurrences = OrderedDict()
for i in range(1,number+1):
    s = raw_input("")
    occurrences[s] = occurrences.get(s, 0) + 1
number_of_unique_occurence=len(occurrences)
print number_of_unique_occurence
print '%s' %' '.join(map(str, occurrences.values()))

rawDescription: can any one help me to optimize my code?

description: Can any one help me to optimize my code


code:
from ij import IJ
from ij.gui import GenericDialog

def getOptions():

    dir1 = IJ.getDirectory("Choose first directory")
    dir2 = IJ.getDirectory("Choose second directory")

    gd = GenericDialog("Directories")
    gd.addStringField("FA Name", "File name")
    gd.showDialog()

    if gd.wasCanceled():
        print "User canceled. Exiting...."
        return
    fileName = gd.getNextString()
    return dir1, dir2, fileName

options = getOptions()
if options is not None:
    dir01, dir02, name = options

    print dir01, dir02, name

rawDescription: Assign value from getDirectory after click on Button using jython


code:
page = requests.get('http://finance.sina.com.cn/chanjing/cyxw/2015-12-17/doc-ifxmttcn4893506.shtml')
print page.text
soup = BeautifulSoup(page.text)
soup.prettify()
print soup

rawDescription: Can't not get content from URL with python


code:
import HTMLParser
data = '''

yes
no

'''
class TableParser(HTMLParser.HTMLParser):
    def __init__(self):
        HTMLParser.HTMLParser.__init__(self)
        self.in_td = False

    def handle_starttag(self, tag, attrs):
        if tag == 'td':
            self.in_td = True

    def handle_data(self, data):
        if self.in_td:
            print data

    def handle_endtag(self, tag):
        self.in_td = False

p = TableParser()
p.feed(data)

rawDescription: Parse/extract table data using python

description: Parse/extract table data using python


code:
import numpy as np
from numpy.lib.stride_tricks import as_strided

def sliding_window(arr, window_size):
    """ Construct a sliding window view of the array"""
    arr = np.asarray(arr)
    window_size = int(window_size)
    if arr.ndim != 2:
        raise ValueError("need 2-D input")
    if not (window_size > 0):
        raise ValueError("need a positive window size")
    shape = (arr.shape[0] - window_size + 1,
             arr.shape[1] - window_size + 1,
             window_size, window_size)
    if shape[0] <= 0:
        shape = (1, shape[1], arr.shape[0], shape[3])
    if shape[1] <= 0:
        shape = (shape[0], 1, shape[2], arr.shape[1])
    strides = (arr.shape[1]*arr.itemsize, arr.itemsize,
               arr.shape[1]*arr.itemsize, arr.itemsize)
    return as_strided(arr, shape=shape, strides=strides)

def cell_neighbors(arr, i, j, d):
    """Return d-th neighbors of cell (i, j)"""
    w = sliding_window(arr, 2*d+1)

    ix = np.clip(i - d, 0, w.shape[0]-1)
    jx = np.clip(j - d, 0, w.shape[1]-1)

    i0 = max(0, i - d - ix)
    j0 = max(0, j - d - jx)
    i1 = w.shape[2] - max(0, d - i + ix)
    j1 = w.shape[3] - max(0, d - j + jx)

    return w[ix, jx][i0:i1,j0:j1].ravel()

x = np.arange(8*8).reshape(8, 8)
print x

for d in [1, 2]:
    for p in [(0,0), (0,1), (6,6), (8,8)]:
        print "-- d=%d, %r" % (d, p)
        print cell_neighbors(x, p[0], p[1], d=d)

rawDescription: Pixel neighbors in 2d array (image) using Python


code:
list_t = ['a','b','a','c','d','b']
dictionary = {'a':1,'b':2,'c':3,'d':4}
lis=[]
for i in list_t:
   for key in dictionary:
      if (i==key):
         lis.append(dictionary[i])
print lis

rawDescription: Replacing List items with Dictionary Items?


code:
from sqlalchemy import *

db_target = create_engine('sqlite:///C:\\Users\\asit\\workspace\\forum1\\src\\sqlite.db')
target_metadata = MetaData(db_target)
src_master_table = Table('forum_forum', src_metadata, autoload=True )
print src_master_table.columns._data

rawDescription: column details in sqlite3

description: Column details in sqlite3


code:
#!/usr/bin/env python

import datetime

def hrdd(d1, d2):
    """
    Human-readable date difference.
    """
    _d1 = datetime.datetime.strptime(d1, "%a %b %d %H:%M:%S %Y")
    _d2 = datetime.datetime.strptime(d2, "%a %b %d %H:%M:%S %Y")
    diff = _d2 - _d1
    return diff.days # <-- alternatively: diff.seconds

if __name__ == '__main__':
    d1 = "Mon Jun 28 10:51:07 2010"
    d2 = "Fri Jun 18 10:18:43 2010"
    d3 = "Wed Dec 15 09:18:43 2010"

    print hrdd(d1, d2)
    # ==> -11
    print hrdd(d2, d1)
    # ==> 10
    print hrdd(d1, d3)
    # ==> 169
    # ...

rawDescription: Python human readable date difference

description: Python human readable date difference


code:
from tiffile import TiffFile

with TiffFile('test.tiff') as tif:
    for page in tif:
        image = page.asarray(memmap=True)
        print image[0,:,:]

rawDescription: Is there any way to read one image row/column into an array in Python?


code:
class Polymorph(object):
    def __new__(cls, value):
        if isinstance(value, float):
            return MyFloat(value)
        elif isinstance(value, int):
            return MyInt(value)
        elif isinstance(value, str):
            return MyStr(value)
        else:
            raise TypeError, 'Unknown type'

rawDescription: Polymorphic class in Python that returns an instance of another class


code:
s="abcdefgh"
print "".join(b+a for a,b in zip(s[::2],s[1::2]))

rawDescription: Python switch order of elements

description: Python switch order of elements


code:
import re
s = 'Lorem ipsum dolor [mytag]something[/mytag]sit amet, ipsum [mytag]something else[/mytag]a laoreet ultricies'
print re.sub(r'\[mytag\](.+?)\[/mytag\]', r'<img src="\1">', s)

rawDescription: Howto replace different values with tags in string?


code:
def Powerball():
    result = []
    for i in range(5):
        number = r.randint(1,69)
        while (number in result):
            number = r.randint(1,69)
        result.append(number)
    result.sort()
    result.append(r.randint(1,26))
    return result

if __name__ == '__main__':
    result = Powerball()
    print result

rawDescription: python code for power ball lottery

description: Python code for power ball lottery


code:
import threading

def task1():
  urllib2.urlopen(blabla)

class bla:
  def disconnectButton(self, w):
    print "Window Resize"
    self.buttononTop()
    #urllib2.urlopen(disconnect_url).read()
    t1 = threading.Thread(target=task1)
    subprocess.call("/var/tmp/restartMe.sh", shell=True)

rawDescription: Python - why the button sometime works and sometime fails to execute all the logic assigned?


code:
str1 = "string1"
str2 = "string2"
results = [["string1 ", " string2"]]
for row in results:
  if((row[0].strip() == str1) and (row[1].strip() == str2)):
     print "We found the match....."

rawDescription: == operator not working on two matching strings


code:
from collections import Counter
from string import punctuation

counter = Counter()
with open('/tmp/content_data') as f:
  for line in f:
    counter.update(word.strip(punctuation) for word in line.split())

result = dict(counter)

# note: because we have
#   isinstance(counter, dict)
# you may as well leave the result as a Counter object

print result

rawDescription: Find the count of the words in the text file using python


code:
import inspect
def bubble(*args, **kwargs):
    def find_class_by_code_object(back_self, method_name, code):
        for cls in inspect.getmro(type(back_self)):
            if method_name in cls.__dict__:
                method_fun = getattr(cls, method_name)
                if method_fun.im_func.func_code is code:
                    return cls

    frame = inspect.currentframe().f_back
    back_self = frame.f_locals['self']
    method_name = frame.f_code.co_name

    for _ in xrange(5):
        code = frame.f_code
        cls = find_class_by_code_object(back_self, method_name, code)
        if cls:
            super_ = super(cls, back_self)
            return getattr(super_, method_name)(*args, **kwargs)
        try:
            frame = frame.f_back
        except:
            return

class A(object):
    def test(self):
        print "A.test()"

class B(A):
    def test(self):
        # instead of "super(B, self).test()" we can do
        bubble()

class C(B):
    pass

c = C()
c.test() # works!

b = B()
b.test() # works!

rawDescription: super() in Python 2.x without args

description: Super() in Python 2.x without args


code:
bash_script_name = cwd + "/myscript.sh"
bash_script_file_obj = open(bash_script_name, 'w')
bash_script_file_obj.write(text_of_myscript)
bash_script_file_obj.close()
os.chmod(bash_script_name, 0755)

subprocess.call([another_tool, bash_script_name])

os.remove(bash_script_name)

rawDescription: python distutils include shell scripts in module directory

description: Python distutils include shell scripts in module directory


code:
x = [1,2,3,4]
for i in xrange(len(x)):
    for j in xrange(i + 1, len(x)):
        y = x[:]
        y[i], y[j] = y[j], y[i]
        print y

rawDescription: Get a list of the 1-element swap from an array in Python


code:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))

retval = -1
def onclick(event):
    print 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(event.button, event.x, event.y, event.xdata, event.ydata)
    # Record the x location of the user's click in the global variable and close the figure
    global retval
    retval = event.xdata
    plt.close()

cid = fig.canvas.mpl_connect('button_press_event', onclick)
# Bring up the figure (and wait)
plt.show()

print 'User selected point {0}'.format( retval )

rawDescription: python return ydata from matplotlib click

description: Python return ydata from matplotlib click


code:
jsonData = {
    "a": 1,
    "b": 2,
    "c": [{
        "d": 4,
        "e": 5,
        "f": {
            "g": 6
        }
    }]
}

def get_simple_keys(data):
    result = []
    for key in data.keys():
        if type(data[key]) != dict:
            result.append(key)
        else:
            result += get_simple_keys(data[key])
    return result

print get_simple_keys(jsonData['c'][0])

rawDescription: Get keys from json in python

description: Get keys from json in python


code:
#!/usr/bin/python

import requests
from time import sleep
import smtplib

def get_data_count(url):
    try:
        req = requests.get(url)
    except requests.ConnectionError:
        return 'could not get page'

    try:
        return int(req.json()['stats']['highPriorityQueueDepth'])
    except TypeError:
        return 'field not found'
    except ValueError:
        return 'not an integer'

def send_mail(data):
    sender = 'fromuser@host.com'
    receivers = ['touser@host.com']

    message = """From: fromuser@host.com
    To: touser@host.com
    Subject: Send mail from python!!

    """
    body = ''
    for item in data:
        body = body + '{name} - {res}\n'.format(name=item['name'], res=item['res'])

    message = message + body

    try:
       smtpObj = smtplib.SMTP('corp.host.com' )
       smtpObj.sendmail(sender, receivers, message)
       print "Mail sent"
    except smtplib.SMTPException:
       print "You can't spam. Mail sending failed!"

def main():
    urls = [
        ('hostnameA', 'http://hostnameA:1234/Service/statistics?%24format=json'),
        ('hostnameB', 'http://hostnameB:1234/Service/statistics?%24format=json')
    ]

    keepData = []
    while True:
        data = []
        sendMail = False
        print('')

        for name, url in urls:
            res = get_data_count(url)
            print('{name} - {res}'.format(name=name, res=res))
            data.append({'name':name, 'res':res})

        keepData.append([item['res'] for item in data])

        if len(keepData) >= 3:
            count = 0
            for nn in range(len(urls)):
                for item in keepData:
                    if item[nn] >= 100:
                        count = count + 1
                    if count > 2:
                        sendMail = True
                        count = 0
        if len(keepData) >= 3: keepData = keepData[1::]
        if sendMail == True: send_mail(data)
        sleep(100.)

if __name__=="__main__":
    main()

rawDescription: How to send out an email using Python after retrying three times while scanning the URL?

description: Send out an email using Python after retrying three times while scanning the URL


code:
for (i, (x1, x2)) in enumerate(zip(listone,listtwo)):
    if i != 3:
        print "The number is {0} the element is {1}".format(x1, x2)

rawDescription: Print all except the fourth item from a five item list for two lists next to each other Python


code:
class _Reflection(object):
    def __init__(self, source, reflection, name=None):
        self.source = source
        self.reflection = reflection
        self.name = name

    def __getattr__(self, attribute):
        self.attribute = attribute
        return _Reflection(self.source.__getattribute__(attribute), self.reflection.__getattribute__(attribute), name=attribute)

    def __call__(self, *args, **kwargs):
        source_output = self.source(*args, **kwargs)

        if self.name == 'add_argument':
            # if the method being called is 'add_argument',
            # over-ride the 'default' argument's value to 'None' in our secondary argparser.
            kwargs['default'] = None

        reflection_output = self.reflection(*args, **kwargs)
        return _Reflection(source_output, reflection_output)

class ReflectionArgumentParser(object):
    def create(self, *args, **kwargs):
        self.parser = argparse.ArgumentParser(*args, **kwargs)
        self._mirror = argparse.ArgumentParser(*args, **kwargs)
        return _Reflection(self.parser, self._mirror)

    def parse_args(self, *args, **kwargs):
        return self.parser.parse_args(*args, **kwargs)

    def filter_defaults(self, *args, **kwargs):
        return self._mirror.parse_args(*args, **kwargs)

mirrorParser = ReflectionArgumentParser()
parser = mirrorParser.create()

parser.add_argument('-f', '--foo', default=False, action="store_true")
parser.add_argument('-b', '--baz', default=0, action="store_const", const=10)
parser.add_argument('bar', nargs='*', default='bar')

print mirrorParser.parse_args([])
# Outputs: Namespace(bar='bar', baz=0, foo=False)

print mirrorParser.filter_defaults([])
# Outputs: Namespace(bar=[], baz=None, foo=None)

print mirrorParser.filter_defaults('--foo -b lorem ipsum'.split())
# Outputs: Namespace(bar=['lorem', 'ipsum'], baz=10, foo=True)

rawDescription: How do I get the user specified list of optional command line args not containing the defaults using argparse in python?

description: Get the user specified list of optional command line args not containing the defaults using argparse in python


code:
class FilterableList(list):

    def __init__(self, column_names, initial_data=None):
        if initial_data is not None:
            list.__init__(self, initial_data)
        else:
            list.__init__(self)
        self.column_names = column_names

    def __setitem__(self, index, value):
        if len(value) != len(self.column_names):
            raise ValueError('Rows must have the correct number of columns.')
        list.__setitem__(self, index, value)

    def append(self, value):
        if len(value) != len(self.column_names):
            raise ValueError('Rows must have the correct number of columns.')
        list.append(self, value)

    def extend(self, values):
        for value in values:
            self.append(value)

    def filtered_by(self, **filters):
        try:
            filters = [(self.column_names.index(filter[0]), filter[1]) for filter in filters.iteritems()]
        except ValueError:
            raise ValueError('Tried to apply filter with invalid column name.')
        for row in self:
            for index, value in filters:
                if row[index] != value:
                    continue
                yield row

data = FilterableList(('a', 'b', 'c', 'd'))
data.extend(((1,2,3,4), (2,2,5,6), (3,4,7,8)))
for row in data.filtered_by(b=2, c=5):
    print row

rawDescription: Filtering though a dataset with a dynamic definition


code:
import decimal
cents = 999
dollars = decimal.Decimal(cents) / 100
print dollars

rawDescription: How to use Python string formatting to convert an integer representing cents to a float representing dollars?

description: Use Python string formatting to convert an integer representing cents to a float representing dollars


code:
# The destination object resource is entirely optional. If empty, we use
# the source object's metadata.
if reuse_metadata:
    destination_object_resource = {}
else:
    destination_object_resource = {
            'contentLanguage': 'en',
            'metadata': {'my-key': 'my-value'},
    }
req = client.objects().copy(
        sourceBucket=bucket_name,
        sourceObject=old_object,
        destinationBucket=bucket_name,
        destinationObject=new_object,
        body=destination_object_resource)
resp = req.execute()
print json.dumps(resp, indent=2)

rawDescription: Programmatically emulating "gsutil mv" on appengine cloudstorage in python


code:
from gi.repository import Gtk

class TestWindow(Gtk.Window):

    def __init__(self, *args, **kwargs):
        Gtk.Window.__init__(self, *args, **kwargs)
        self.connect("destroy", Gtk.main_quit)
        self.is_allowed = True
        self.create_widgets()
        self.show_all()

    def create_widgets(self):
        box = Gtk.HBox()
        self.entry = Gtk.Entry()
        self.handler_id = self.entry.connect("changed", self.on_entry_changed)
        box.pack_start(self.entry, True, True, 0)
        button = Gtk.Button("Toggle")
        button.connect("clicked", self.on_button_clicked)
        box.pack_start(button, True, True, 0)
        self.add(box)

    def on_entry_changed(self, *args):
        print "entry has changed"

    def on_button_clicked(self, *args):
        if self.is_allowed:
            self.entry.handler_block(self.handler_id)
            print "now blocking"
        else:
            self.entry.handler_unblock(self.handler_id)
            print "now unblocking"
        self.is_allowed = not self.is_allowed

TestWindow()
Gtk.main()

rawDescription: Manually toggling of widgets ability to emit signals in pygtk


code:
import sys
from PySide import  QtGui

class Frame(QtGui.QFrame):

    def __init__(self, (width, height)):
        super(Frame, self).__init__()

        # NOTE1: indicate if mouse was clicked
        self.clicked= False

        self.setGeometry(0, 0, width, height)
        self.setWindowTitle('Frame')
        self.show()
        self.loop()

    def loop(self):
        while 1:
            self.update()
            QtGui.QApplication.processEvents()

    def mouseMoveEvent(self, event):
        # Get the current position of the mouse
        x, y = event.x(), event.y()
        print (x, y)

    def mousePressEvent(self, event):
        # NOTE 2: track clicks, not movements
        if self.clicked:
            # calculate vector
            vector = ((self.x, self.y,), (event.x(), event.y(),))
            print "vector: %s" % str(vector)
            self.clicked = False
            return vector
        # set vector start
        self.x = event.x()
        self.y = event.y()
        self.clicked = True
        print "vector set to start from: (%d,%d)" % (self.x, self.y)
        return None

def main():
    app = QtGui.QApplication(sys.argv)
    Frame((500, 500))
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

rawDescription: PySide : Compute the amount of movement in both x and y axis since the previous call to the mouseMoveEvent function


rawDescription: How do I create a grid in python with different values?

description: Create a grid in python with different values


code:
for zip_name in glob.glob('[0-9].zip'):
# the zip file name one numeric digit only.
    z=zipfile.ZipFile(zip_name)
    subdir = zip_name[:-4]    # the glob pattern ensures that the file name ends with ".zip", so strip off the extension
    with z.open('{}/atextfile.txt'.format(subdir)) as f:
        for line in f:
            for word in line:
                print word

rawDescription: How to read a file inside a folder which itself is inside a zip file

description: Read a file inside a folder which itself is inside a zip file


code:
import os
from os.path import join

lookfor = "python.exe"
for root, dirs, files in os.walk('C:\\'):
    print "searching", root
    if lookfor in files:
        print "found: %s" % join(root, lookfor)
        break

rawDescription: Python: How to do a system wide search for a file when just the filename (not path) is available


code:
import sys
from urllib import unquote_plus

from PyQt4 import QtCore, QtGui, QtWebKit

class MyWebPage(QtWebKit.QWebPage):
    formSubmitted = QtCore.pyqtSignal(object)

    def acceptNavigationRequest(self, frame, req, nav_type):
        if nav_type == QtWebKit.QWebPage.NavigationTypeFormSubmitted:
            elements = {}
            for key, value in req.url().encodedQueryItems():
                key = unquote_plus(bytes(key)).decode('utf8')
                value = unquote_plus(bytes(value)).decode('utf8')
                elements[key] = value
            self.formSubmitted.emit(elements)
        return super(MyWebPage, self).acceptNavigationRequest(frame, req, nav_type)

class PsyTML(QtGui.QDialog):
    def __init__(self):
        super(PsyTML, self).__init__()
        self.elements = {}
        self.view = QtWebKit.QWebView(self)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.view)
        layout.setContentsMargins(0, 0, 0, 0)
        self.view.setPage(MyWebPage())
        self.view.page().formSubmitted.connect(self.handleFormSubmitted)

    def viewPsyTML(self, html):
        self.view.setHtml(html)
        self.exec_()

    def handleFormSubmitted(self, elements):
        self.elements = elements
        self.accept()

# setup the html form
html = """
<form action="" method="get">
Like it?
<input type="radio" name="like" value="yes"/> Yes
<input type="radio" name="like" value="no" /> No

<input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""

html2 = """
<form action="" method="get">
Choose wisely?
<input type="radio" name="choose" value="A"/> Yes
<input type="radio" name="choose" value="B" /> No

<input type="text" name="text" value="" />
<input type="submit" name="submit" value="Send"/>
</form>
"""

def main():
    app = QtGui.QApplication(sys.argv)

    intro = PsyTML()
    intro.viewPsyTML(html)

    # do stuff with elements...
    for item in intro.elements.iteritems():
        print '"%s" = "%s"' % item

    intro = PsyTML()
    intro.viewPsyTML(html2)

if __name__ == "__main__":
    main()

rawDescription: PyQt4: Restarting the main loop?

description: PyQt4: Restarting the main loop


code:
class SimpleEcho(WebSocket):

    def handleMessage(self):
        if self.data is None:
            self.data = ''

        for client in self.server.connections.itervalues():
            client.sendMessage(str(self.address[0]) + ' - ' + str(self.data))

        #echo message back to client
        #self.sendMessage(str(self.data))

    def handleConnected(self):
        print self.address, 'connected'

    def handleClose(self):
        print self.address, 'closed'

rawDescription: WebSocket broadcast to all clients using Python


code:
#!/usr/bin/env python

"""
Test program.

Usage:
    test.py (-v ...)

Options:
    -v  Flag that should be counted
"""

import docopt
print docopt.docopt(__doc__)

rawDescription: Option multiplicity with docopt

description: Option multiplicity with docopt


code:
desc = []
with open('football.txt') as txt:
    footballFixtures = [cut.strip().split(':') for cut in txt.readlines()]
    print footballFixtures
for typeoffixture,descriptionoffixture,dateoffixture,resultoffixture in footballFixtures:
    desc.append(descriptionoffixture)
first = StringVar()
first.set("Select a Fixture")
OptionMenu(root,first, *desc).place(x=90, y=150)

rawDescription: Python OptionMenu only displaying one option when used with a for statement


code:
import os
import django
from myapp import models

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
django.setup()

print models.MyModel.objects.get(pk=1)

rawDescription: "Rake" or my customs tasks in Django

description: "Rake" or my customs tasks in Django


code:
import numpy as np
import pathos.multiprocessing as multiprocessing

class LikelihoodTest:
      def __init__(self,Xgal,Ygal):
          self.x=Xgal
          self.y=Ygal
          self.objPosition=gal_pos
          self.beta_s=beta
          self.RhoCrit_SigmaC=rho_c_over_sigma_c
          self.AngularDiameter=DA
          self.RhoCrit=rho_crit
          self.Reducedshear=observed_g
          self.ShearError=g_err
          #The 2D function
      def like2d(self,posx, posy):
          stuff=[self.objPosition, self.beta_s, self.RhoCrit_SigmaC , self.AngularDiameter, self.RhoCrit]
          m=4.447e14
          c=7.16
          param=[posx, posy, m, c]
          return reduced_shear( param, stuff, self.Reducedshear, self.ShearError)
      def ShearLikelihood(self,r):
          return [float(self.like2d(self.x[j],r)) for j in range(len(self.x))]
      def run(self):
          try:
              print "processing to estimate likelihood in 2D grids......!!!"
              start = time.time()
              pool = multiprocessing.Pool(processes=10)
              seq=[ self.y[i] for i in range( self.y.shape[0])]
              results=np.array( pool.map(self.ShearLikelihood, seq ))
              end = time.time()
              print "process time:\n",end - start
              pool.close()
          except ValueError:
              print "Oops! value error ....!"
          return results
      def plotLikelihood(self,shared_array):
          #plotting on a mesh the likelihood function in order to see whether you have defined the inputs correctly and you can observe the maximum likelihood in 2D
          # Set up a regular grid of interpolation points
          xi, yi = np.linspace(self.x.min(), self.x.max(), 100), np.linspace(self.y.min(), self.y.max(), 100)
          # Interpolate
          rbf = scipy.interpolate.interp2d(self.x, self.y,shared_array , kind='linear')
          zi = rbf(xi, yi)
          fig, ax = plt.subplots()
          divider = make_axes_locatable(ax)
          im = ax.imshow(zi, vmin=shared_array.min(), vmax=shared_array.max(), origin='lower',
                        extent=[self.x.min(), self.x.max(), self.y.min(),self.y.max()])
          ax.set_xlabel(r"$Xpos$")
          ax.set_ylabel(r"$Ypos$")
          ax.xaxis.set_label_position('top')
          ax.xaxis.set_tick_params(labeltop='on')
          cax = divider.append_axes("right", size="5%", pad=0.05)
          cbar = fig.colorbar(im,cax=cax, ticks=list(np.linspace(shared_array.max(), shared_array.min(),20)),format='$%.2f$')
          cbar.ax.tick_params(labelsize=8)
          plt.savefig('/users/Desktop/MassRecons/Likelihood2d_XY_coordinate.pdf', transparent=True, bbox_inches='tight', pad_inches=0)
          plt.close()

if __name__ == '__main__':
     Xgal = np.linspace(Xgalaxy.min(), Xgalaxy.max(), 1000)
     Ygal = np.linspace(Ygalaxy.min(), Ygalaxy.max(), 1000)
     Test=LikelihoodTest(Xgal,Ygal)
     x=Test.run()
     Test.plotLikelihood(x)

rawDescription: Using multiprocessing in a class

description: Using multiprocessing in a class


code:
import re
testinput = '#define ATTR_A          (HELLO +1L)     /*FSDSDF*/'
r = re.split(r'#define (\w*)(\s*)(\(.*\))', testinput)
print '%s = %s' %(r[1], r[3])

rawDescription: Converting format of the text in a file by using python


code:
import datetime as dt

d = {0: "(oh)",
     1: "one",
     2: "two",
     3: "three",
     4: "four",
     5: "five",
     6: "six",
     7: "seven",
     8: "eight",
     9: "nine",
     10: "ten",
     11: "eleven",
     12: "twelve",
     13: "thirteen",
     14: "fourteen",
     15: "fifteen",
     16: "sixteen",
     17: "seventeen",
     18: "eighteen",
     19: "nineteen",
     20: "twenty",
     30: "thirty",
     40: "forty",
     50: "fifty",
     60: "sixty"}

def display_time(t):
    Hour = d[int( t[0:2])] if t[0:2] != "00" else d[12]
    Suffix = 'a.m.' if d[int( t[7:9])] == Hour else 'p.m.'

    if  t[3] == "0":
        if  t[4] == "0":
            Minute = ""
        else:
            Minute = d[0] + " " + d[int(t[4])]
    else:
        Minute = d[int(t[3])*10] + '-' + d[int(t[4])]
    print 'The time is', Hour, Minute, Suffix

display_time(dt.datetime.now().strftime('%I %M %H'))
display_time("07 45 19")
display_time("11 06 23")
display_time("00 00 00")

rawDescription: Python Current Time in English Sentence

description: Python Current Time in English Sentence


code:
s1 = [11, 12, 13, 14, 15, 16, 17, 18]
s2 = [21, 22, 23, 24, 25, 25]

l1 = len(s1)
l2 = len(s2)
l = min(l1,l2)+1

k = l2/2;

for i in range(k,l2+1):
    print s1[:i]
    print s2[-i:]
    print

for i in range(1,l1-l2):
    print s1[i:i+l2]
    print s2[:]
    print

for i in reversed(range(k,l)):
    print s1[-i:]
    print s2[:i]
    print

rawDescription: Moving a list half its size against other list to get the common elements


code:
def run_strategy(smaPeriod,inst):

        # Download the bars.
    feed = yahoofinance.build_feed([inst], 2011, 2013, ".")

        # Evaluate the strategy with the feed's bars.
    myStrategy = MyStrategy(feed, inst, smaPeriod)
    myStrategy.run()
    print "Final portfolio value: $%.2f" % myStrategy.getBroker().getEquity()
def main():
    instruments = ["AAPL","EBAY", "NFLX", "BBY"]
    for inst in instruments:
            run_strategy(10,inst)
if __name__ == '__main__':
        main()

rawDescription: Backtesting multiple stocks using Python


code:
import collections
sorted_event_types = collections.OrderedDict(sorted(event_types.items()))
print sorted_event_types

rawDescription: Sort a dictionary

description: Sort a dictionary


code:
#!/usr/bin/env python

token_list = ['a', 'b', 'c']

contradictions = {
    'a': set(['b']),
    'b': set(['a']),
    'c': set()
}

result = []

while token_list:
    token = token_list.pop()
    new = [set([token])]
    for r in result:
        if token not in contradictions or not r & contradictions[token]:
            new.append(r | set([token]))
    result.extend(new)

print result

rawDescription: Find "best" complete subgraphs

description: Find "best" complete subgraphs


code:
def info(title):
    print(title)
    print('module name:', __name__)
    if hasattr(os, 'getppid'):  # only available on Unix
        print('parent process:', os.getppid())
    print('process id:', os.getpid())

def startServer(inDebug, port):
    print "Starting Main Mock Server"
    app.run(debug=inDebug, port=port)

def startProxyServer(inDebug, port):
    print "Starting JSONP Mock Server Proxy"
    appJsonp.run(debug=inDebug, port=jsonpPort)

if __name__ == '__main__':
    info('Main Line Starting')
    p = mp.Process(target=startServer, args=(False, port))
    p.deamon = True
    p.start()
    p1 = mp.Process(target=startProxyServer, args=(False, port))
    p1.deamon = True
    p1.start()
    p.join()
    p1.join()

rawDescription: How to run Flask in two Port or threads

description: Run Flask in two Port or threads


code:
import boto

# Helper function. This is more or less the code
# I added to devolop branch
def resubmit(batch, prev):
    # Empty (re-use) the batch
    del batch[:]

    # The batch answer contains the list of
    # unprocessed keys grouped by tables
    if 'UnprocessedKeys' in prev:
        unprocessed = res['UnprocessedKeys']
    else:
        return None

    # Load the unprocessed keys
    for table_name, table_req in unprocessed.iteritems():
        table_keys = table_req['Keys']
        table = batch.layer2.get_table(table_name)

        keys = []
        for key in table_keys:
            h = key['HashKeyElement']
            r = None
            if 'RangeKeyElement' in key:
                r = key['RangeKeyElement']
            keys.append((h, r))

        attributes_to_get = None
        if 'AttributesToGet' in table_req:
            attributes_to_get = table_req['AttributesToGet']

        batch.add_batch(table, keys, attributes_to_get=attributes_to_get)

    return batch.submit()

# Main
db = boto.connect_dynamodb()
table = db.get_table('MyTable')
batch = db.new_batch_list()

keys = range (100) # Get items from 0 to 99

batch.add_batch(table, keys)

res = batch.submit()

while res:
    print res # Do some usefull work here
    res = resubmit(batch, res)

# The END

rawDescription: Simple example of retrieving 500 items from dynamodb using Python


code:
@inlineCallbacks
def onJoin(self, ign):

    @inlineCallbacks
    def method(details):
        session = yield self.call('wamp.session.get', details.caller)
        peer = session['transport']['peer']
        print "peer's address", peer

        headers = session['transport']['http_headers_received']
        print "headers:"
        print '\n'.join(['{}: {}'.format(k, v) for (k, v) in headers.items()])

    yield self.register(
        method, 'some_method',
        types.RegisterOptions(details_arg='details'),
    )

rawDescription: Accessing RPC caller's IP and HTTP connect headers inside ApplicationSession's registered endpoint


code:
import random

def shuffled(seq):
  copy = list(seq)
  random.shuffle(copy)
  return copy

def rand_repeat(seq):
  while True:
    for el in shuffled(seq):
      yield el

list1 = ['tom', 'mary', 'frank', 'joe', 'john', 'barry']
list2 = [1, 2, 3, 4]

print zip(shuffled(list1), rand_repeat(list2))

rawDescription: Randomize 2 lists for a for loop

description: Randomize 2 lists for a for loop


code:
def find_element(p,n):
    return [i for i,x in enumerate(p) if x == n]

print find_element(['1', '2', '1', '2', '2', '1', '2'],'2')
#[1, 3, 4, 6]

rawDescription: How does the return function work within a loop and procedure?