code:
import os
from os.path import join, isdir, islink, isfile

def mywalk(top, topdown=True, onerror=None, ignore_list=('.ignore',)):
    try:
        # Note that listdir and error are globals in this module due
        # to earlier import-*.
        names = os.listdir(top)
    except Exception, err:
        if onerror is not None:
            onerror(err)
        return
    if len([1 for x in names if x in ignore_list]):
        return
    dirs, nondirs = [], []
    for name in names:
        if isdir(join(top, name)):
            dirs.append(name)
        else:
            nondirs.append(name)

    if topdown:
        yield top, dirs, nondirs
    for name in dirs:
        path = join(top, name)
        if not islink(path):
            for x in mywalk(path, topdown, onerror, ignore_list):
                yield x
    if not topdown:
        yield top, dirs, nondirs

rawDescription: A Python walker that can ignore directories


code:
import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None)
        self.resized = False # the dirty flag
        self.Bind(wx.EVT_SIZE,self.OnSize)
        self.Bind(wx.EVT_IDLE,self.OnIdle)

    def OnSize(self,event):
        self.resized = True # set dirty

    def OnIdle(self,event):
        if self.resized:
            # take action if the dirty flag is set
            print "New size:", self.GetSize()
            self.resized = False # reset the flag

app = wx.PySimpleApp()
frame = Frame().Show()
app.MainLoop()

rawDescription: What is the simplest way of monitoring when a wxPython frame has been resized?


code:
import hashlib
from base64 import b64decode, b64encode

def utf16tobin(s):
  return s.encode('hex')[4:].decode('hex')

b64salt = "kDP0Py2QwEdJYtUX9cJABg=="
b64hash = "OJF6H4KdxFLgLu+oTDNFodCEfMA="
binsalt = b64decode(b64salt)
password_string = 'password'.encode("utf16")
password_string = utf16tobin(password_string)

m1 = hashlib.sha1()
# Pass in salt
m1.update(binsalt + password_string)
# Pass in password
# B64 encode the binary digest
if b64encode(m1.digest()) == b64hash:
    print "Logged in!"
else:
    print "Didn't match"
    print b64hash
    print b64encode(m1.digest())

rawDescription: Using Python to authenticate against raw username, hash, salt in DB created by ASP.NET roles/membership


code:
def test(length):
    result = []
    dict = {}
    _test(length, result, dict)
    return result, dict

def _test(length, result, dict):
    if length == 10:
        return
    else:
        result.append(length)
        dict[length] = length + 1
        _test(length + 1, result, dict)

x, y = test(0)
print x, y

rawDescription: How to preserve a list or a dict in python recursive function?

description: Preserve a list or a dict in python recursive function


code:
def greedy_bag(items,limit):
    weighted_items = sorted(items,key=lambda x:x["cost"],reverse = True)
    items_in_bag =  []
    for item in weighted_items:
         if item["cost"] < limit:
            items_in_bag.append(item)
            limit -= item["cost"]
    return items_in_bag
disiplines = "ABCDE"
costs = [5,4,3,2,1]
items = [{"item":d,"cost":c} for d,c in zip(disiplines,costs)]
schedule = []
while items:
    bagged_items = greedy_bag(items,5)
    if not bagged_items:
        break
    for item in bagged_items:
          items.remove(item)
    schedule.append(bagged_items)

print schedule

rawDescription: Generate random blocks Timetable python

description: Generate random blocks Timetable python


code:
s=r'''<data source="file:\\c:\main\data.txt"/>'''
soup = BeautifulSoup(s)
t = soup.find("data")
t['source']
t['source'][7:]
t['source'] = t['source'][7:]
print str(soup)

rawDescription: Python: What is the best approach to find and replace attribute values for an XML tag type?


code:
import urllib, urllib2

##Download SocksiPy - A Python SOCKS client module. ( http://code.google.com/p/socksipy-branch/downloads/list )
##Simply copy the file "socks.py" to your Python's lib/site-packages directory, and initiate a socks socket like this.
## NOTE: you must use socks before urllib2.
import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050)
socket.socket = socks.socksocket

url = 'http://ifconfig.me/ip'
request = urllib2.Request(url)
request.add_header('Cache-Control','max-age=0')
response = urllib2.urlopen(request)
print response.read()

rawDescription: How to make HTTP request through a (tor) socks proxy using python?

description: Make HTTP request through a (tor) socks proxy using python


code:
import pandas as pd

class pFrame(pd.DataFrame):
    def __init__(self):
        super(pFrame, self).__init__()

class pFrameA(pFrame):
    def __init__(self):
        super(pFrameA, self).__init__()
        self.units = ('msec', 'psi')
        self.numFormat = (int, float)
        self._gcHeaderStr = "          Time          Cell"

class pFrameB(pFrame):
    def __init__(self):
        super(pFrameB, self).__init__()
        self.units = ('in', 'in')
        self.numFormat  = (float, float)
        self._gcHeaderStr = "Disp 1        Disp 2"

p = pFrameA()
print p._gcHeaderStr    # To prove something is happening

rawDescription: Python nested class definition results in endless recursion... what did I do wrong here?


rawDescription: I have a dictionary with a value that is as a list of unicode strings, I would like to get them as a string?


code:
import gettext
import babel

messagefiles = gettext.find('OurAppName',
    languages=babel.Locale('en').languages.keys(),
    all=True)
messagefiles.sort()

languages = [path.split('/')[-3] for path in messagefiles]
langlist = zip(languages,
    [babel.Locale.parse(lang).display_name for lang in languages])

print langlist

rawDescription: List available languages for PyGTK UI strings


code:
module = sys.modules.get('x')
if module is not None:
    print module.__file__

rawDescription: discover path of python module

description: Discover path of python module


code:
for n1, n2 in izip(num, reversed(num)):
    print n1, '\t', n2

rawDescription: Reverse loop inside a loop with same list


code:
import spur

shell = spur.SshShell(hostname="remoteserver", username="bob", password="password1")
with shell:
    result = shell.run(["find", "/", "-name", "*.nc"])
    print result.output

rawDescription: Iterate through directory structure over ssh using python


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('AIDNIndustrySearchAll.txt', 'r'))

# create a generator that returns actual href entries
links = (x.get('href') for x in soup.find_all('a'))

# filter the links to only those that contain "V" and store it as a
# set to remove duplicates
selected = set(a for a in links if "V" in a)

# build output string using selected links
output = "\n".join('http://www.aidn.org.au/{0}'.format(a) for a in selected)

# write the string to file
with open('AIDNurl.txt', 'w') as f:
  f.write(output)

print output
print len(selected)  # print number of selected links

rawDescription: Why is this loop returning twice?

description: Why is this loop returning twice


code:
for row in datafile:
    dstip = row[2]
    for key, value in subnet_dict.iteritems():
        if dstip in key:
            print dstip, value + ' was FOUND in the dictionary'
            break
    else:
        print dstip + ' was not found'

rawDescription: Python searching dictionary for matching key


code:
import string

class BlankFormatter(string.Formatter):
    def __init__(self, default=''):
        self.default=default

    def get_value(self, key, args, kwds):
        if isinstance(key, str):
            return kwds.get(key, self.default)
        else:
            Formatter.get_value(key, args, kwds)

kwargs = {"name": "mark", "adj": "mad"}
fmt=BlankFormatter()
print fmt.format("My name is {name} and I'm really {adj}.", **kwargs)
# My name is mark and I'm really mad.
print fmt.format("My name is {name} and I'm really {adjective}.", **kwargs)
# My name is mark and I'm really .

rawDescription: Leaving values blank if not passed in str.format


code:
import urllib2
import json
response = urllib2.urlopen('https://dl.dropboxusercontent.com/u/153071/test.json')
content_dict = json.loads(response.read())
print json.dumps(content_dict, indent=4)

rawDescription: Json, urlib2 and pprint

description: Json, urlib2 and pprint


code:
import json
from shapely import shape, Point
# depending on your version, use: from shapely.geometry import shape, Point

# load GeoJSON file containing sectors
with ('sectors.json', 'r') as f:
    js = json.load(f)

# construct point based on lat/long returned by geocoder
point = Point(45.4519896, -122.7924463)

# check each polygon to see if it contains the point
for feature in js['features']:
    polygon = shape(feature['geometry'])
    if polygon.contains(point):
        print 'Found containing polygon:', feature

rawDescription: Point in Polygon with geoJSON in Python

description: Point in Polygon with geoJSON in Python


code:
user_choices = {}
i = 1
print '\nPhone listings:\n'
for record in pBook:
    key = i
    user_choices[key] = record
    i += 1
    print str(key) + ')', record + ':', 'Home', pBook[record]['home'] + ',', 'Cell', pBook[record]['cell']
print ''

rawDescription: extracting a dictionary key and assigning it to a variable in python

description: Extracting a dictionary key and assigning it to a variable in python


code:
def meta_class_synthesize(name, bases, attrmap):
    seen = set()
    seen_add = seen.add
    metas = [type(base) for base in bases]
    metas = tuple([
        meta for meta in metas
        if meta is not type and meta not in seen and not seen_add(meta)])
    if not metas:
        return type(name, bases, attrmap)
    elif len(metas) == 1:
        return metas[0](name, bases, attrmap)
    newmeta_name = "__".join(meta.__name__ for meta in metas)
    newmeta = type(newmeta_name, metas, {})
    return newmeta(name, bases, attrmap)

class M_A(type):
    pass

class M_B(type):
    pass

class A:
    __metaclass__ = M_A

class B:
    __metaclass__ = M_B

class C(A, B):
    __metaclass__ = meta_class_synthesize

print type(C)  # prints "<class '__main__.M_A__M_B'>"

rawDescription: Access class object builtin values from class (not class member) scope


code:
# list containing the number of lines with 0,1,2,3... occurrances of query words.
wordcount = [0,0,0,0,0]
for line in file("raw_dataset_1").readlines():
    # loop over each query word, see if it occurs in the given line, and just count them.
    # The bracket inside will create a list of elements (query_word) from your query word list (query)
    # but add only those words which occur in the line (if  query_word in line). [See list comprehension]
    # E.g. if your line contain three query words those three will be in the list.
    # You are not interested in what those words are, so you just take the length of the list (len).
    # Finally, number_query_words_found is the number of query words present in the current line of text.
    number_query_words_found = len([query_word for query_word in query if query_word in line])
    if number_query_words_found<5:
        # increase the line-number by one. The index corresponds to the number of query-words present
        wordcount[number_query_words_found] += 1

print "Number of lines with 2 query words: ", wordcount[2]
print "Number of lines with 3 query words: ", wordcount[3]

rawDescription: finding common occurence of given words in a line

description: Finding common occurence of given words in a line


code:
# increment
p = (x, y)
counts[p] = counts.get(p, 0) + 1

# list
for (p, count) in counts.iteritems():
    x, y = p
    print x, y, count

rawDescription: Python data structure for sparse list of x/y co-ordinates


code:
import re

url = '/path/eds/vs/accescontrol.dat/d=12520/file1.dat'
match = re.search('^(.+/([^/]+\.dat))[^$]', url)

print match.group(1)
# Outputs /path/eds/vs/accescontrol.dat
print match.group(2)
# Outputs accescontrol.dat

rawDescription: Get specific parts of a file path in Python


code:
from PyQt4 import QtCore, QtGui
from mainwindow_ui import Ui_MainWindow

class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.setupUi(self)
        self.buildingTree(
            "default,default:cluster1,default:cluster1:clusterA,"
            "default:cluster2,default:cluster2:clusterA"
            )
        self.treeWidget.viewport().installEventFilter(self)

    def eventFilter(self, target, event):
        if (event.type() == QtCore.QEvent.ContextMenu and
            target is self.treeWidget.viewport()):
            item = self.treeWidget.itemAt(event.pos())
            if item is not None:
                menu = QtGui.QMenu()
                menu.addAction(
                    "New", lambda: self.new_cluster(item))
                menu.addAction(
                    "Rename", lambda: self.rename_cluster(item))
                menu.addSeparator()
                menu.addAction(
                    "Delete", lambda: self.delete_cluster(item))
                menu.exec_(event.globalPos())
            return True
        return super(MainWindow, self).eventFilter(target, event)

    def new_cluster(self, item):
        print "New Cluster", item.text(0)

    def rename_cluster(self, item):
        print "Rename cluster", item.text(0)

    def delete_cluster(self, item):
        print "Delete cluster", item.text(0)

    def buildingTree(self, string):
        arrTree = []
        arrPath = []
        arrString = []
        arrString = string.split(",")
        root = QtGui.QTreeWidgetItem(self.treeWidget)
        root.setText(0,arrString[0])
        arrTree.append(root)
        arrString.pop(0)
        for path in arrString:
            arrPath = path.split(":")
            nameOfFather = arrPath[len(arrPath)-2]
            arrTree.reverse()
            for node in arrTree:
                nameOfNode = node.text(0)
                if nameOfNode == nameOfFather:
                    sonNode = QtGui.QTreeWidgetItem(node)
                    sonNode.setText(0,arrPath[len(arrPath)-1])
                    arrTree.insert(0,sonNode)
                    arrTree.reverse()
                    break
        self.treeWidget.setSortingEnabled(False)

if __name__ == "__main__":

    import sys
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

rawDescription: sending QTreeWidgetItem to a function with python

description: Sending QTreeWidgetItem to a function with python


code:
s = u'àaeëß35+{}"´'
print ''.join(c for c in s if c.isalnum())

rawDescription: How can I remove all non-letter (all languages) and non-numeric characters from a string?

description: Remove all non-letter (all languages) and non-numeric characters from a string


code:
import re
import inspect
print inspect.getsource(re.compile)

rawDescription: Can Python print a function definition?

description: Can Python print a function definition


code:
import urllib2
ret = urllib2.urlopen('http://hostname/directory/file.jpg')
if ret.code == 200:
    print "Exists!"

rawDescription: How do I check if a file on http exists, using Python/django?

description: Check if a file on http exists, using Python/django


code:
html = r"""

    9.0

"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(html)
elem = soup.find('div')

print elem['id'], 'is the id'
print elem.text, 'is the value' #9.0

rawDescription: Using ScraperWiki to get information from a div element


code:
# Prime Factorization by trial division

from math import ceil,sqrt
from collections import deque

# Global list of primes (strictly we should use a class variable not a global)
#primes = deque()
primes = []

def is_prime(n):
    """Test whether n is divisible by any prime known so far"""
    global primes
    for p in primes:
         if n%p == 0:
             return False #  n was divisible by p
    return True # either n is prime, or divisible by some p larger than our list
def generate_primes(a):
    """Generate sieved list of primes (up to sqrt(a)) as we go"""
    global primes
    primes_upper_limit = int(sqrt(a))
    # We get huge speedup by using xrange() instead of range(), so we have to seed the list with 2
    primes.append(2)
    print "Generating sieved list of primes up to", primes_upper_limit, "...",
    # Consider prime candidates 2,3,5,7... in increasing increments of 2
    #for number in [2] + range(3,primes_upper_limit+2,2):
    for number in xrange(3,primes_upper_limit+2,2):
        if is_prime(number): # use global 'primes'
            #print "Found new prime", number
            primes.append(number) # Found a new prime larger than our list
    print "done"
def find_largest_prime_factor(x, debug=False):
    """Find all prime factors of x, and return the largest."""
    global primes
    # First we need the list of all primes <= sqrt(x)
    generate_primes(x)
    to_factor = x # running value of the remaining quantity we need to factor
    largest_prime_factor = None
    for p in primes:
        if debug: print "Testing divisibility by", p
        if to_factor%p != 0:
            continue
        if debug: print "...yes it is"
        largest_prime_factor = p
        # Divide out all factors of p in x (may have multiplicity)
        while to_factor%p == 0:
            to_factor /= p
        # Stop when all factors have been found
        if to_factor==1:
            break
    else:
        print "Tested all primes up to sqrt(a), remaining factor must be a single prime > sqrt(a) :", to_factor
    print "\nLargest prime factor of x is", largest_prime_factor
    return largest_prime_factor

rawDescription: Handling memory usage for big calculation in python


rawDescription: Generate SQL statements from a Pandas Dataframre


code:
import xml.etree.cElementTree as ET
tree = ET.parse('test.xml')
for elem in tree.getiterator():
    if elem.tag=='{http://www.namespace.co.uk}AssetEquipment':
        output={}
        for elem1 in list(elem):
            if elem1.tag=='{http://www.namespace.co.uk}Active':
                output['Active']=elem1.text
            if elem1.tag=='{http://www.namespace.co.uk}Direction':
                output['Direction']=elem1.text
            if elem1.tag=='{http://www.namespace.co.uk}Location':
                for elem2 in list(elem1):
                    if elem2.tag=='{http://www.namespace.co.uk}RoomLocation':
                        for elem3 in list(elem2):
                            if elem3.tag=='{http://www.namespace.co.uk}Room':
                                output['Room']=elem3.text
        print output

rawDescription: Extracting Specific Lines of XML with Python ElementTree


code:
import HTML
import html2
from html2 import *
#print a
#print b
file = open('out.html', 'w')
table_data = [
['S.No',   'Testcase - ID',   'Result'],
['1',       a,         b],
['2',       c,         d],

]
htmlcode = HTML.table(table_data)
c=htmlcode
print htmlcode

rawDescription: How to call python global command inside Procedure of another file

description: Call python global command inside Procedure of another file


code:
def CheckConstraints(self, Selections):
    'Validate the stored constraints'
    Good = True
    env = {}
    ## Undefined options default to False
    for i in self.Options:
        env[i] = False
    ## Set defined Options to True
    for i in Selections:
        env[i] = True
    for i in self.Constraints:
        if not eval(i, env):
            Good = False
            print "Constraint Check Failure:", i, Selections
        else:
            print "Constraint Check OK:", i, Selections
    return Good

rawDescription: How can I clean up use of exec, can’t get setattr to do the job?

description: Clean up use of exec, can’t get setattr to do the job


code:
import numpy as np
from sklearn.datasets import make_classification
from sklearn import ensemble
from sklearn.calibration import CalibratedClassifierCV
from sklearn.metrics import log_loss
from sklearn import cross_validation

X, y = make_classification(n_samples=1000,
                           n_features=100,
                           n_informative=30,
                           n_redundant=0,
                           n_repeated=0,
                           n_classes=9,
                           random_state=0,
                           shuffle=False)

skf = cross_validation.StratifiedShuffleSplit(y, 5)

for train, test in skf:

    X_train, X_test = X[train], X[test]
    y_train, y_test = y[train], y[test]

    clf = ensemble.GradientBoostingClassifier(n_estimators=100)
    clf_cv = CalibratedClassifierCV(clf, cv=3, method='isotonic')
    clf_cv.fit(X_train, y_train)
    probas_cv = clf_cv.predict_proba(X_test)
    cv_score = log_loss(y_test, probas_cv)
    print 'calibrated score (3-fold):', cv_score

    clf = ensemble.GradientBoostingClassifier(n_estimators=100)
    clf_cv = CalibratedClassifierCV(clf, cv=10, method='isotonic')
    clf_cv.fit(X_train, y_train)
    probas_cv = clf_cv.predict_proba(X_test)
    cv_score = log_loss(y_test, probas_cv)
    print 'calibrated score (10-fold:)', cv_score

    #Train 3 classifiers and take average probability
    skf2 = cross_validation.StratifiedKFold(y_test, 3)
    probas_list = []
    for sub_train, sub_test in skf2:
        X_sub_train, X_sub_test = X_train[sub_train], X_train[sub_test]
        y_sub_train, y_sub_test = y_train[sub_train], y_train[sub_test]
        clf = ensemble.GradientBoostingClassifier(n_estimators=100)
        clf.fit(X_sub_train, y_sub_train)
        probas_list.append(clf.predict_proba(X_test))
    probas = np.mean(probas_list, axis=0)
    clf_ensemble_score = log_loss(y_test, probas)
    print 'uncalibrated ensemble clf (3-fold) score:', clf_ensemble_score

    clf = ensemble.GradientBoostingClassifier(n_estimators=100)
    clf.fit(X_train, y_train)
    probas = clf.predict_proba(X_test)
    score = log_loss(y_test, probas)
    print 'direct clf score:', score
    print

rawDescription: Why does CalibratedClassifierCV underperform a direct classifer?


code:
import sys
import os

external = set()
exempt = set()
paths = (os.path.abspath(p) for p in sys.path)
stdlib = {p for p in paths
          if p.startswith((sys.prefix, sys.real_prefix))
          and 'site-packages' not in p}
for name, module in sorted(sys.modules.items()):
    if not module or name in sys.builtin_module_names or not hasattr(module, '__file__'):
        # an import sentinel, built-in module or not a real module, really
        exempt.add(name)
        continue

    fname = module.__file__
    if fname.endswith(('__init__.py', '__init__.pyc', '__init__.pyo')):
        fname = os.path.dirname(fname)

    if os.path.dirname(fname) in stdlib:
        # stdlib path, skip
        exempt.add(name)
        continue

    parts = name.split('.')
    for i, part in enumerate(parts):
        partial = '.'.join(parts[:i] + [part])
        if partial in external or partial in exempt:
            # already listed or exempted
            break
        if partial in sys.modules and sys.modules[partial]:
            # just list the parent name and be done with it
            external.add(partial)
            break

for name in external:
    print name, sys.modules[name].__file__

rawDescription: How to check if a module/library/package is part of the python standard library?

description: Check if a module/library/package is part of the python standard library


code:
import re

from scrapy.http import FormRequest
from scrapy.spider import BaseSpider

HEADERS = {
    'X-MicrosoftAjax': 'Delta=true',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36'
}
URL = 'http://exitrealty.com/agent_list.aspx?firstName=&lastName=&country=USA&state=NY'

class ExitRealtySpider(BaseSpider):
    name = "exit_realty"

    allowed_domains = ["exitrealty.com"]
    start_urls = [URL]

    def parse(self, response):
        # submit a form (first page)
        self.data = {}
        for form_input in response.css('form#aspnetForm input'):
            name = form_input.xpath('@name').extract()[0]
            try:
                value = form_input.xpath('@value').extract()[0]
            except IndexError:
                value = ""
            self.data[name] = value

        self.data['ctl00$MainContent$ScriptManager1'] = 'ctl00$MainContent$UpdatePanel1|ctl00$MainContent$agentList'
        self.data['__EVENTTARGET'] = 'ctl00$MainContent$List'
        self.data['__EVENTARGUMENT'] = 'Page$1'

        return FormRequest(url=URL,
                           method='POST',
                           callback=self.parse_page,
                           formdata=self.data,
                           meta={'page': 1},
                           dont_filter=True,
                           headers=HEADERS)

    def parse_page(self, response):
        current_page = response.meta['page'] + 1

        # parse agents (TODO: yield items instead of printing)
        for agent in response.xpath('//a[@class="regtext"]/text()'):
            print agent.extract()
        print "------"

        # request the next page
        data = {
            '__EVENTARGUMENT': 'Page$%d' % current_page,
            '__EVENTVALIDATION': re.search(r"__EVENTVALIDATION\|(.*?)\|", response.body, re.MULTILINE).group(1),
            '__VIEWSTATE': re.search(r"__VIEWSTATE\|(.*?)\|", response.body, re.MULTILINE).group(1),
            '__ASYNCPOST': 'true',
            '__EVENTTARGET': 'ctl00$MainContent$agentList',
            'ctl00$MainContent$ScriptManager1': 'ctl00$MainContent$UpdatePanel1|ctl00$MainContent$agentList',
            '': ''
        }

        return FormRequest(url=URL,
                           method='POST',
                           formdata=data,
                           callback=self.parse_page,
                           meta={'page': current_page},
                           dont_filter=True,
                           headers=HEADERS)

rawDescription: Crawling through pages with PostBack data javascript Python Scrapy


code:
import sys

from PyQt4 import QtGui
from PyQt4 import QtCore

class MainWindow(QtGui.QWidget):
    def __init__(self):
        super(MainWindow, self).__init__()
        main_layout = QtGui.QVBoxLayout()

        # Create the model
        self.model = MyModel()

        # Create a slider and link it to the model
        self.slider1 = QtGui.QSlider()
        self.model.add_slider(self.slider1)
        main_layout.addWidget(self.slider1)

        # Add a lineEdit and button to force update the model
        # Note that the LineEdit is not linked to the model, so won't update with the slider
        self.edit = QtGui.QLineEdit()
        button = QtGui.QPushButton('update model')
        button.clicked.connect(self.on_clicked)
        main_layout.addWidget(self.edit)
        main_layout.addWidget(button)

        self.setLayout(main_layout)

    def on_clicked(self):
        self.model.update_model(int(self.edit.text()),self.slider1)

class MyModel(QtGui.QStandardItemModel):
    def __init__(self,*args,**kwargs):
        super(MyModel,self).__init__(*args,**kwargs)
        self._slider_list = {}
        self.itemChanged.connect(self.on_item_changed)

    def add_slider(self,slider):
        if slider in self._slider_list:
            raise Exception('You cannot link a slider to the model twice')

        item = QtGui.QStandardItem(str(slider.value()))
        self._slider_list[slider] = item
        self.appendRow(item)
        slider.valueChanged.connect(lambda value: self.update_model(value,slider))

    def update_model(self,value,slider):
        if str(value) != self._slider_list[slider].text():
            self._slider_list[slider].setText(str(value))
            print 'update_model: %d'%value

    def on_item_changed(self,item):
        slider = self._slider_list.keys()[self._slider_list.values().index(item)]
        if slider.value() != int(item.text()):
            slider.setValue(int(item.text()))
            print 'on_item_changed: %s'%item.text()

app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()

sys.exit(app.exec_())

rawDescription: Creating a model/view interface with sliders using PyQt


code:
import numpy as np
y = np.array([1,2,3,4,5])
print np.sum(np.square(y[1:] - 2*y[:-1]))
# 14

rawDescription: Numpy: vectorize sum of lagged differences


code:
#!/usr/bin/env python
import sys
from PyQt4 import QtCore, QtGui

class CustomWidget(QtGui.QLabel):
    signal_hided = QtCore.pyqtSignal()
    signal_shown = QtCore.pyqtSignal()
    def hideEvent(self, event):
        print 'hideEvent'
        super(CustomWidget, self).hideEvent(event)
        self.signal_hided.emit()

    def showEvent(self, event):
        print 'showEvent'
        super(CustomWidget, self).showEvent(event)
        self.signal_shown.emit()

class MainWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.widget1 = CustomWidget('Widget1')
        self.widget2 = CustomWidget('Widget2')

        # connect signals, so if one widget is hidden then other is shown
        self.widget1.signal_hided.connect(self.widget2.show)
        self.widget2.signal_hided.connect(self.widget1.show)
        self.widget2.signal_shown.connect(self.widget1.hide)
        self.widget1.signal_shown.connect(self.widget2.hide)

        # some test code
        self.button = QtGui.QPushButton('test')
        layout = QtGui.QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.widget1)
        layout.addWidget(self.widget2)
        self.setLayout(layout)
        self.button.clicked.connect(self.do_test)

    def do_test(self):
        if self.widget1.isHidden():
            self.widget1.show()
        else:
            self.widget2.show()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    widget = MainWidget()
    widget.resize(640, 480)
    widget.show()

    sys.exit(app.exec_())

rawDescription: PyQt : How to set up a widget hidding an other widget if it's visible?


code:
from itertools import permutations, product, chain

d = {'A': [1,2,3], 'B': [4,5], 'C': [6]}
# You don't need to materialise permutations here, but this matches your existing dict
p = {k:list(permutations(v)) for k, v in d.iteritems()}

for blah in product(*map(p.get, sorted(p))):
    print list(chain.from_iterable(blah)) # or use tuple instead of list

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 5, 4, 6]
[1, 3, 2, 4, 5, 6]
[1, 3, 2, 5, 4, 6]
[2, 1, 3, 4, 5, 6]
[2, 1, 3, 5, 4, 6]
[2, 3, 1, 4, 5, 6]
[2, 3, 1, 5, 4, 6]
[3, 1, 2, 4, 5, 6]
[3, 1, 2, 5, 4, 6]
[3, 2, 1, 4, 5, 6]
[3, 2, 1, 5, 4, 6]

rawDescription: Generate combinations (in specific order) of all permutations of the dict of lists


code:
class exampleClass(object):

    def doSomething(self,number):
        return number + 1

class exampleClass2(exampleClass):

    def __init__(self):
        self.member1 = exampleClass()

    def callDefDoSomething(self):
        print self.member1.doSomething(5)

object2 = exampleClass2()
object2.callDefDoSomething()

rawDescription: Communicating between objects in Python

description: Communicating between objects in Python


code:
import ast, _ast

ops = {_ast.Mult: "*", _ast.Add: "+", _ast.BitXor: "pow", _ast.Div: "/"}

def rec(n):
    if isinstance(n, _ast.Expr):
        return rec(n.value)
    elif isinstance(n, _ast.BinOp):
        if isinstance(n.op, _ast.BitXor):
            return "{}({}, {})".format(ops[type(n.op)], rec(n.left),rec(n.right))
        else:
            return "({} {} {})".format(rec(n.left), ops[type(n.op)],rec(n.right))
    elif isinstance(n, _ast.Name):
        return n.id
    elif isinstance(n, _ast.Num):
        return n.n

print rec(ast.parse("(((a+b)^2) + ((c+d)^(1/2))) * e").body[0])
# ((pow((a + b), 2) + pow((c + d), (1 / 2))) * e)

rawDescription: Convert Equation from One Language to Another


code:
import httplib, urllib
params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
print params
params = urllib.urlencode({'number': 12524, 'type': 'issue', 'action': 'show'})
print params

rawDescription: Python urllib.encode @ symbol?

description: Python urllib.encode @ symbol


code:
def square(n):
    return n * n

if __name__ == "__main__":
    import multiprocessing
    multiprocessing.freeze_support()

    pool = multiprocessing.Pool(processes=5)

    print pool.map(square, range(10))

rawDescription: Python multiprocessing fails for example code


code:
import socket
import os

class ForkingServer:
    def serve_forever(self):
        self.s = socket.socket()
        try:
            self.s.bind(("", 9000))
            self.s.listen(1)
        except socket.error, (value,message):
            print "error:", message
            if self.s:
                self.s.close()
            return

        while True:
            client,address = self.s.accept()
            pid = os.fork()
            # You should read the documentation for how fork() works if you don't
            # know it already
            # The short version is that at this point in the code, there are 2 processes
            # completely identical to each other which are simulatenously executing
            # The only difference is that the parent process gets the pid of the child
            # returned from fork() and the child process gets a value of 0 returned
            if pid == 0:
                # only the newly spawned process will execute this
                self.handle_client_connection(client, address)
                break
            # In the meantime the parent process will continue on to here
            # thus it will go back to the beginning of the loop and accept a new connection

    def handle_client_connection(self, client,address):
        #simple echo server
        print "Got a connection from:", address
        while True:
            data = client.recv(5)
            if not data:
                # client closed the connection
                break
            client.send(data)
        print "Connection from", address, "closed"

server = ForkingServer()
server.serve_forever()

rawDescription: Using Processes as Threads with Networking in Python


code:
m = re.search("\d+", string)
print m.group(0)

rawDescription: How to store number from string in python

description: Store number from string in python


code:
from collections import defaultdict

d1 = { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3' }
d2 = { 'key1': 'value4', 'key2': 'value5', 'key3': 'value6' }

dout = defaultdict(list)
for item in d1:
    dout[item].append(d1[item])
for item in d2:
    dout[item].append(d2[item])
print dout

rawDescription: Python: Reducing a List in a Dictionary

description: Python: Reducing a List in a Dictionary


code:
import png
import sys

KERNING = 3

def find_rows(pixels,width, height):
    "find all rows that are purely white"
    white_rows = []
    is_white = False
    for y in range(height):
        if sum(sum( pixels[(y*4*width)+x*4+p] for p in range(3)) for x in range(width)) >= width*3*254:
            if not is_white:
                white_rows.append(y)
            is_white = True
        else:
            is_white = False
    return white_rows

def find_words_in_image(blob, tolerance=30):
    n = 0
    r = png.Reader(bytes=blob)
    (width,height,pixels_rows,meta) = r.asRGBA8()
    pixels = []
    for row in pixels_rows:
        for px in row:
            pixels.append(px)
    # find each horizontal line
    white_rows = find_rows(pixels,width,height)
    # for each line try to find a white vertical gap
    for i,y in enumerate(white_rows):
        if y >= len(white_rows):
            continue
        y2 = white_rows[i+1]
        height_of_row = y2 - y
        is_white = False
        white_cols = []
        last_black = -100
        for x in range(width-4):
            s = y*4*width+x*4
            if sum(pixels[s+y3*4*width] + pixels[s+y3*4*width+1] + pixels[s+y3*4*width+2] for y3 in range(height_of_row)) >= height_of_row*3*240:
                if not is_white:
                    if len(white_cols)>0 and x-last_black < KERNING:
                        continue
                    white_cols.append(x)
                is_white = True
            else:
                is_white = False
                last_black = x
        # now we have a list of x,y co-oords for all the words on this row
        for j,x in enumerate(white_cols):
            if j >= len(white_cols)-1:
                continue
            wordpx = []
            new_width = white_cols[j+1]-x
            new_height = y2-y
            x_offset = x*4
            for h in range(new_height):
                y_offset = (y+h)*4*width
                start = x_offset+y_offset
                wordpx.append( pixels[start:start+(new_width*4)] )
            n += 1
            with open('word%s.png' % n, 'w') as f:
                w = png.Writer(
                    width=new_width,
                    height=new_height,
                    alpha=True
                    )
                w.write(f,wordpx)
    return n

if __name__ == "__main__":
    #
    # USAGE: python png2words.py yourpic.png
    #
    # OUTPUT: [word1.png...word2.png...wordN.png]
    #
    n = find_words_in_image( open(sys.argv[1]).read() )
    print "found %s words" % n

rawDescription: how to perform word by word slicing of a scanned page with python?

description: Perform word by word slicing of a scanned page with python


code:
import urllib2
import re, sys
from HTMLParser import HTMLParser

# EDIT 1: import the packag
from BeautifulSoup import BeautifulSoup

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
    def handle_data(self, d):
        self.fed.append(d)
    def get_data(self):
        return ''.join(self.fed)

def stripHTMLTags(html):
    html = re.sub(r'<{1}br{1}>', '\n', html)
    s = MLStripper()
    s.feed(html)
    text = s.get_data()
    if "External links" in text:
        text, sep, tail = text.partition('External links')
    if "External Links" in text:
        text, sep, tail = text.partition('External Links')
    text = text = text.replace("See also","\n\n See Also - \n")
    text = text.replace("*","- ")
    text = text.replace(".", ". ")
    text = text.replace("  "," ")
    text = text.replace("""   /
 / ""","")
    return text

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
infile = opener.open('http://en.wikipedia.org/w/index.php?title=Albert_Einstein&printable=yes')
page = infile.read()

# EDIT 2: convert the page and extract text from the first <p> tag
soup = BeautifulSoup(page)
para = soup.findAll("p", limit=1)[0].text

print stripHTMLTags(para)

rawDescription: retrieving main paragraphs from python wikipedia page output

description: Retrieving main paragraphs from python wikipedia page output


code:
import Tkinter
import ttk
import time
import threading
import sys

def task(root):
    ft = ttk.Frame()
    ft.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
    pb_hD = ttk.Progressbar(ft, orient='horizontal', mode='indeterminate')
    pb_hD.pack(expand=True, fill=Tkinter.BOTH, side=Tkinter.TOP)
    pb_hD.start(10)
    root.mainloop()

def process_of_unkown_duration(root):
    time.sleep(5)
    print 'Done'
    root.quit()

def Main():
    root = Tkinter.Tk()
    t1=threading.Thread(target=process_of_unkown_duration, args=(root,))
    t1.start()
    task(root)  # This will block while the mainloop runs
    t1.join()

if __name__ == '__main__':
    Main()

rawDescription: How do I make one thread Kill another?

description: Make one thread Kill another


code:
import ctypes
from ctypes import *

winmm= windll.winmm
print 'waveInGetNumDevs=',winmm.waveInGetNumDevs()

rawDescription: How to see if there is one microphone active using python?

description: See if there is one microphone active using python


code:
import operator
data = {0: [31, 32, 58, 59], 1: [31, 32, 12, 13, 37, 38], 2: [12, 13]}

# the accumulator is the new dict with renumbered values combined with a list of renumbered numbers so far
# item is a (key, value) element out of the original dict
def reductor(acc, item):
   (out, renumbered) = acc
   (key, values)     = item
   def remapper(v):
       try:
           x = renumbered.index(v)
       except ValueError:
           x = len(renumbered)
           renumbered.append(v)
       return x
   # transform current values to renumbered values
   out[key] = map(remapper, values)
   # return output and updated list of renumbered values
   return (out, renumbered)

# now reduce the original data
print reduce(reductor, sorted(data.iteritems(), key=operator.itemgetter(0)), ({}, []))

rawDescription: Python: Renumerate elements in multiple lists


code:
with open('MyText.txt', 'r') as f:
  lineArr=f.read().split('\n')
  if 'Sample Text' in lineArr:
    timeTaken = [s for s in lineArr if "Time Taken" in s]
    print timeTaken[0]

rawDescription: Text File Reading and Printing Data

description: Text File Reading and Printing Data


code:
import numpy
def adj(attr):
    if attr==None:
        return attr
    else:
        return attr-1
def adjust_slice(x):
    if isinstance(x,int):
        return x-1
    elif isinstance(x,slice):
        return slice(*[adj(attrib) for attrib in (x.start,x.stop,x.step)])
    elif isinstance(x,list):
        return slice(x[0]-1,x[-1]-1,1)
    else:
        raise Exception("Expected slice, list, or int.")
class array_starting_at_one(list):
    def __init__(self,np_array):
        self.np_array = numpy.array(np_array)
    def __getitem__(self,i):
        if isinstance(i,int):
            i=i-1
        elif isinstance(i,tuple):
            i = tuple([adjust_slice(x) for x in i])
        else:
            return array_starting_at_one(self.np_array[adjust_slice(x)])
        return self.np_array[i]
    def __setitem__(self,i,y):
        if isinstance(i,int):
            self.np_array[i-1] = y
        elif isinstance(i,tuple):
            self.np_array[tuple([adjust_slice(x) for x in i])] = y
        else:
            self.np_array[adjust_slice(x)] = y
    def __getslice__(self,i,j):
        return array_starting_at_one(self.np_array[(i-1):(j-1)])
    def __setslice__(self,i,j,y):
        self.np_array[i-1:j-1]=y
    def __repr__(self):
        print self.np_array
    def __str__(self):
        return str(self.np_array)

rawDescription: python: change numpy.array access method to start at 1 instead of 0

description: Python: change numpy.array access method to start at 1 instead of 0


code:
a = [1,1,2,3,5,8,13,21]
import itertools
for x in itertools.takewhile(lambda val: val != 8, a):
    print x

rawDescription: slice (unsorted) array at value in python

description: Slice (unsorted) array at value in python


code:
def adds_dynamic_z_decorator(f):
  def replacement(*arg,**karg):
    # create a new 'z' binding in globals, saving previous
    if 'z' in globals():
      oldZ = (globals()['z'],)
    else:
      oldZ = None
    try:
      globals()['z'] = None
      #invoke the original function
      res = f(*arg, **karg)
    finally:
      #restore any old bindings
      if oldZ:
        globals()['z'] = oldZ[0]
      else:
        del(globals()['z'])
    return res
  return replacement

@adds_dynamic_z_decorator
def func(x,y):
  print z

def other_recurse(x):
  global z
  print 'x=%s, z=%s' %(x,z)
  recurse(x+1)
  print 'x=%s, z=%s' %(x,z)

@adds_dynamic_z_decorator
def recurse(x=0):
  global z
  z = x
  if x < 3:
    other_recurse(x)

print 'calling func(1,2)'
func(1,2)

print 'calling recurse()'
recurse()

rawDescription: Can I use a decorator to mutate the local scope of a function in Python?

description: Use a decorator to mutate the local scope of a function in Python


code:
def grep(filename, needle):
    with open(filename) as f_in:
        matches = ((i, line.find(needle), line) for i, line in enumerate(f_in))
        return [match for match in matches if match[0] != -1]

def main(filename, needle):
    matches = grep(filename, needle)
    if matches:
        print "{0} found on {1} lines in {2}".format(needle, len(matches), filename)
        for line in matches:
            print "{0}:{1}:{2}".format(*line)
        return 1
    else:
        return -1

if __name__=='__main__':
    import sys
    filename = sys.argv[1]
    needle = sys.argv[2]
    return sys.exit(main(filename, needle))

rawDescription: Python; reading file and finding desired text


code:
import nltk
grammar = nltk.parse_cfg("""
S -> NP VP
VP -> V PUNCT
PUNCT -> '.'
V -> 'eat'
NP -> 'I'
""")

sentence = "I eat .".split()
cp = nltk.ChartParser(grammar)
for tree in cp.nbest_parse(sentence):
    print tree

rawDescription: How to parse the special character in Context Free Grammar?

description: Parse the special character in Context Free Grammar


code:
css = "the contents of your enormous css file"
rules = re.findall(r'[^\{]+\{[^\}]*\}', css, re.MULTILINE)

print 'Number of rules: ', len(rules)

with open('output1.css', 'w') as f:
    f.write('\n'.join(rules[:4096]))
if len(rules) > 4096:
    with open('output2.css', 'w') as f:
        f.write('\n'.join(rules[4096:]))

rawDescription: How can I split large CSS files with python?

description: Split large CSS files with python


code:
#!/usr/bin/python -u
import libxml2
import sys

# Memory debug specific
libxml2.debugMemory(1)

dtd="""<!ELEMENT foo EMPTY>"""
instance="""<?xml version="1.0"?>
<foo></foo>"""

dtd = libxml2.parseDTD(None, 'test.dtd')
ctxt = libxml2.newValidCtxt()
doc = libxml2.parseDoc(instance)
ret = doc.validateDtd(ctxt, dtd)
if ret != 1:
    print "error doing DTD validation"
    sys.exit(1)

doc.freeDoc()
dtd.freeDtd()
del dtd
del ctxt

rawDescription: DTD Validation With Python?

description: DTD Validation With Python


code:
import csv

with open('myfile', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        # count non-empty cells, but ignore the first
        nonempty = [cell for cell in row[1:] if cell]
        print len(nonempty)

rawDescription: Finding length of line

description: Finding length of line


code:
from itertools import izip_longest
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

with open('z.t') as f:
    d = grouper(f, 4)
    for x in d:
            print ','.join(y.rstrip() for y in x)

a,b,c,d,e,f,g,h
i,j,k,l,m,n,o,p

rawDescription: Reading data file with varying number of columns python


code:
def result_none(choice):
    mydict = {}
    if choice == 'a':
        mydict['x']  = 100
        mydict['y']  = 1000
        return mydict
    else:
        return None

def result_dict(choice):
    mydict = {}
    if choice == 'a':
        mydict['x']  = 100
        mydict['y']  = 1000
    return mydict

test_dict = result_dict('b')
if test_dict.get('x'):
    print 'Got x'
else:
    print 'No x'

test_none = result_none('b')
if test_none.get('x'):
    print 'Got x'
else:
    print 'No x'

rawDescription: Should I return an empty dict instead of None?


code:
def natural_numbers():
    n = 0
    while True:
        n += 1
        yield n

def find_nth_character(n):
    numbers = ""
    gen = natural_numbers()

    while len(numbers) < n:
        numbers += str(gen.next())

    print numbers
    return numbers[n-1]

def main():
    print find_nth_character(15)

if __name__ == '__main__':
    main()

rawDescription: Make an infinite list of natural numbers pasted together and find index python


code:
import time
import datetime

timestamp = int(time.time())

# date timestamp
print int(
    time.mktime(
        datetime.date.fromtimestamp(timestamp).timetuple()
    )
)

# iso format date string
print datetime.date.fromtimestamp(timestamp).isoformat()

rawDescription: Query Unix time by day in Python

description: Query Unix time by day in Python


code:
from BeautifulSoup import BeautifulSoup, Tag
soup = BeautifulSoup()
mem_attr = ['Description', 'PhysicalID', 'Slot', 'Size', 'Width']
html = Tag(soup, "html")
table = Tag(soup, "table")
tr = Tag(soup, "tr")
soup.append(html)
html.append(table)
table.append(tr)
for attr in mem_attr:
    th = Tag(soup, "th")
    tr.append(th)
    th.append(attr)

print soup.prettify()

rawDescription: Issue in creating a html file from beautiful soup


code:
import xml.etree.cElementTree as ET

file_path = "/path/to/your/test.xml"
context = ET.iterparse(file_path, events=("start", "end")) #Probably we could use only the start tag
# turn it into an iterator
context = iter(context)
on_members_tag = False

for event, elem in context:
    tag = elem.tag
    value = elem.text
    if value :
        value = value.encode('utf-8').strip()
    if event == 'start' :
        if tag == "row" :
            attribs = elem.attrib
            print "This is the campaignID %s and this is the adGroupID" % (attribs['campaignID'] , attribs['adGroupID'])

    elem.clear() #Save memory!

rawDescription: How to process xml files in python

description: Process xml files in python


code:
import ast

l = [['dir/path', 'other/path'],['path/path', 'blah/xyz']]
with open('a.txt', 'a') as f:
    f.write(str(l))

with open('a.txt') as fread:
    s = fread.read()
    l2 = ast.literal_eval(s)

print type(l2)
for i in l2:
    print i

rawDescription: Read python list from a file

description: Read python list from a file


code:
from sys import stdout
from time import sleep
for i in range(400):
    print"\r"+str(i),
    stdout.flush()
    sleep(0.5)

rawDescription: How can I print over the current line in Python 2.7?

description: Print over the current line in Python 2.7


code:
import os
import signal

def create_handler(obj):
    def _handler(signum, frame):
        print "obj is availiable here!"
        print obj
        signal.signal(signum, signal.SIG_DFL)
        os.kill(os.getpid(), signum) # Rethrow signal, this time without catching it
    return _handler

def mymethod(*params):
  obj = MyObj(params)
  signal.signal(signal.SIGTSTP, create_handler(obj))
  obj.do_some_long_stuff()

rawDescription: "Proper" way to handle signals other than SIGINT in Python?


code:
def on_status(self, status):
    print status.text
    self.n += 1
    if self.n < 20:
        return True
    else:
        print 'maxnum = '+str(self.n)
        return False

rawDescription: How to trigger keyboard interrupt

description: Trigger keyboard interrupt


code:
from webtest import TestApp
from nose.tools import eq_

class TestAuthentication(object):
    """
    Tests for the default authentication setup.

    If your application changes how the authentication layer is configured
    those tests should be updated accordingly
    """

    application_under_test = 'main'

    def setUp(self):
        """Method called by nose before running each test"""
        # Loading the application:
        conf_dir = config.here
        wsgiapp = loadapp('config:test.ini#%s' % self.application_under_test,
                          relative_to=conf_dir)
        self.app = TestApp(wsgiapp)
        # Setting it up:
        test_file = path.join(conf_dir, 'test.ini')
        cmd = SetupCommand('setup-app')
        cmd.run([test_file])

    def tearDown(self):
        """Method called by nose after running each test"""
        # Cleaning up the database:
        model.DBSession.remove()
        teardown_db()

    def test_forced_login(self):
        """Anonymous users are forced to login

        Test that anonymous users are automatically redirected to the login
        form when authorization is denied. Next, upon successful login they
        should be redirected to the initially requested page.

        """
        # Requesting a protected area
        resp = self.app.get('/secc/', status=302)
        assert resp.location.startswith('http://localhost/login')
        # Getting the login form:
        resp = resp.follow(status=200)
        print resp.forms
        form = resp.forms
        # Submitting the login form:
        form['login'] = u'manager'
        form['password'] = 'managepass'
        post_login = form.submit(status=302)
        # Being redirected to the initially requested page:
        assert post_login.location.startswith('http://localhost/post_login')
        initial_page = post_login.follow(status=302)
        assert 'authtkt' in initial_page.request.cookies, \
               "Session cookie wasn't defined: %s" % initial_page.request.cookies
        assert initial_page.location.startswith('http://localhost/secc/'), \
               initial_page.location

rawDescription: Testing cherrypy WSGI application using Webtest and or nose?


code:
for i, j in itertools.combinations(range(len(matrix.T)), 2):
    c = matrix[:, [i,j]]
    combined_counts = collections.Counter(map(tuple,c))
    first_column_counts = collections.Counter(c[:,0])
    second_column_counts = collections.Counter(c[:,1])
    print 'columns {} and {}'.format(i,j)
    for AB in combined_counts:
      freq_AB = float(float(combined_counts[AB])/len(c))
      print 'Frequency of {} = {}'.format(AB, freq_AB)
      freq_A = float(first_column_counts[AB[0]])/len(c)
      print "  Freq of '{}' in column {} = {}".format(AB[0], i, freq_A)
      freq_B = float(second_column_counts[AB[1]])/len(c)
      print "  Freq of '{}' in column {} = {}".format(AB[1], i, freq_B)
    print

rawDescription: calculate individual frequencies for both elements of a pair of this 2 elements from a numpy matrix in python

description: Calculate individual frequencies for both elements of a pair of this 2 elements from a numpy matrix in python


code:
if __name__ == "__main__":
    start = time.time()
    data = np.fromfile('d:\mov.mp4', dtype=np.uint8, count=-1)
    end = time.time()
    print("Original file read: ")
    print end - start
    encoded, table = huffman_encode(data)

rawDescription: The fastest way to read binary files by bytes in Python


code:
if __debug__:
  print "If this prints, you're not running python -O."
else:
  print "If this prints, you are running python -O!"

rawDescription: How would you do the equivalent of preprocessor directives in Python?


code:
from mutagen.mp3 import MP3
audio = MP3("example.mp3")
print audio.info.length

rawDescription: Finding the length of an mp3 file

description: Finding the length of an mp3 file


code:
from threading import Thread
from threading import Event
from time import sleep
import signal

stop = Event()

def handler(signum, frame):
    y = raw_input("Are you sure you want to quit? (y/n): ").lower()
    if y == 'y':
        stop.set()

def main():
    t = 0
    while not stop.isSet():
        print "The count is {0}".format(t)
        t +=1
        sleep(1)

if __name__ == "__main__":
    signal.signal(signal.SIGINT, handler)

    mainthread = Thread(target = main)
    mainthread.start()

    while mainthread.is_alive():
        try:
            mainthread.join(timeout = 0.1)
        except IOError:
            pass #Gets thrown when we interrupt the join

rawDescription: Python threading script is not starting/working properly


code:
from collections import defaultdict

ratings= {
    'Shane': {'127 Hours': 5.0, 'Avatar': 4.5},
    'Jaycee': {'127 Hours': 4.8, 'Avatar': 3.5, 'Mad Max: Fury Road': 4.9}
}

# this creates a dictionary that sets a default value of an empty list on
# access, which makes the aggregation a bit nicer since no
# initialization is needed
by_movie = defaultdict(list)

# aggregate to the default dict
for name, mrs in ratings.items():
    for movie, rating in mrs.items():
        by_movie[movie].append(rating)

# calculate averages
averages = {movie: sum(movie_ratings) / len(movie_ratings) for movie, movie_ratings in by_movie.items()}

print averages

rawDescription: Python - Average all key values with same key to dictionary


code:
from __future__ import division
sentence = raw_input ("Please write a sentence for it to be counted and averaged:")

words = sentence.split(" ") #You should split words by space

nbrChar, average=0, .0

for word in words:
    nbrChar+= len(word)

average= nbrChar // len(words)

print 'The average amount of words in the sentence is' ,average, 'words'

rawDescription: Average of words in a sentence?

description: Average of words in a sentence


code:
import re
import json

def parse(self, response):

    playerdata = re.search(re.escape("DataStore.prime('stage-player-stat', defaultTeamPlayerStatsConfigParams.defaultParams , ") + '(\[.*\])' + re.escape(");"), response.body).group(1)

    for player in json.loads(playerdata):
        print player['FirstName']

rawDescription: Using Scrapy to obtain data from HTML datastore


code:
a = 'subteam3=zzz&comments3=good&subteam9=yyy&comments9=bad'
a = a.split('&')
b = dict()
c = dict()

for i in range(0,len(a)):
    if 'subteam' in a[i]:
        temp,temp1 = a[i].strip('subteam').split("=")
        b[temp]=temp1
    else:
        temp,temp1 = a[i].strip('comments').split("=")
        c[temp]=temp1

print b,c

rawDescription: how to convert a string into dictionary in python?

description: Convert a string into dictionary in python


code:
def makeboard(rows, cols):
    board = []
    for r in range(rows):
        brow = []
        for c in range(cols):
            if r == c == 0:
                 brow.append(' ')
            elif r == 0:
                 brow.append(str(c-1))
            elif c == 0:
                 brow.append(str(r-1))
            else:
                 brow.append('*')
        board.append(brow)
    return board

b = makeboard(4,3)

for row in b:
    print ' '.join(row)

rawDescription: Creating a board in Python

description: Creating a board in Python


code:
def createTable():
    ok = False
    while not ok:
        try:
            verifyTable = test.get_table(raw_input("Please enter the name for table: "))
            ok = True
        except BotoServerError:
            print "Table already exist"

rawDescription: When table name already exist I want to show a message but also ask again for a new table name


code:
#!/usr/bin/python

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtSvg import *

class Main(QWidget):

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

        hbox = QHBoxLayout()

        self.setLayout(hbox)
        self.view = MyView(self)
        self.scene = QGraphicsScene()
        self.view.setScene(self.scene)

        hbox.addWidget(self.view)

class MyView(QGraphicsView):

    def __init__(self, parent):
        super(MyView, self).__init__(parent)
        self.parent = parent

    def mousePressEvent(self, event):
        super(MyView, self).mousePressEvent(event)
        test = MySvg()
        self.parent.scene.addItem(test)

class MySvg(QGraphicsSvgItem):

    def __init__(self):
        super(MySvg, self).__init__('ubuntu.svg')

        self.setFlags(QGraphicsItem.ItemIsSelectable|
                      QGraphicsItem.ItemIsMovable)

        self.setAcceptsHoverEvents(True)

    def hoverEnterEvent(self, event):
        print 'Enter'

    def hoverLeaveEvent(self, event):
        print 'Leave'

    def hoverMoveEvent(self, event):
        print 'Moving'

def runMain():

    app = QApplication(sys.argv)
    ex = Main()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    runMain()

rawDescription: pyQt Hover event with Svg image

description: PyQt Hover event with Svg image


code:
with open(csv_file, 'r') as csvfile:
    readcsv = csv.reader(csvfile, delimiter=';')
    flatlist = (list(readcsv))[0]

print flatlist

rawDescription: Python CSV to list (1-dimensional list)

description: Python CSV to list (1-dimensional list)


code:
from flask import g

# ...

def require_auth(func):

    # ...

    for instance in session.query(SQLTypes.User).filter(SQLTypes.User.token==token):
        g.auth_user = instance.username
    try:
        g.auth_user
        print 'authenticated!'
    except AttributeError:
        abort(401)

    # ...

rawDescription: custom authentication decorator

description: Custom authentication decorator


code:
events = service.events().list(calendarId='resource@email-address.com').execute()

while True:
  for event in events.get('items', []):
    print event['summary']
  page_token = events.get('nextPageToken')
  if page_token:
    events = service.events().list(calendarId='resource@email-address.com', pageToken=page_token).execute()
  else:
    break

rawDescription: Getting All Entries from a Multi-User Google Calendar


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

import matplotlib.dates as mdates

def on_click(event):
    if event.inaxes is not None:
        print mdates.num2date(event.xdata)

t = pd.date_range('2015-11-01', '2016-01-06', freq='H')
y = np.random.normal(0, 1, t.size).cumsum()

df = pd.DataFrame({'Y':y}, index=t)

fig, ax = plt.subplots()
df.plot(ax=ax)
fig.canvas.mpl_connect('button_press_event', on_click)
plt.show()

rawDescription: How to get data in DateTime format from plot with Events in python?

description: Get data in DateTime format from plot with Events in python


code:
import subprocess
print subprocess.check_output(['ls', '/etc'])

rawDescription: How to use python to extract data that is pushed to stdout?

description: Use python to extract data that is pushed to stdout


code:
#!/usr/bin/python
from functools import partial
import types

class MyClass(object):

    def __init__(self, val):
        self.val = val

    @classmethod
    def generateMethods(self):
        def dummy(conf1, self):
            print "conf1:", conf1
            print "self.val:", self.val
            print

        for s in ('dynamic_1', 'dynamic_2'):
            closed = partial(dummy, s)
            setattr(self, "test_{0}".format(s), closed)

    def __getattribute__(self, attr):
        # Here we do have access to the much need instance(self)
        obj = object.__getattribute__(self, attr)
        if isinstance(obj, partial):
            return types.MethodType(obj, self, type(self))
        else:
            return obj

if __name__ == '__main__':
    MyClass.generateMethods()

    x = MyClass('FOO')

    x.test_dynamic_1()
    x.test_dynamic_2()

rawDescription: Setting a functools.partial as an instance method in Python


code:
if __name__ == '__main__':
    last_minute = -1

    while True:
        try:
            r = json.read(rqst_command())
            print r
            command = r.get('cmd')
            interval = r.get('intvl')
            collect_time = r.get('c_time')

            tempTest = True

            while tempTest == True:
                start_time = datetime.now()
                s_minute = start_time.minute

                if(((s_minute % interval) == 0) and (s_minute != last_minute)):
                    tempTest = False
                    c_t = datetime(start_time.year, start_time.month, start_time.day, start_time.hour, start_time.minute, 0)
                    last_minute = c_t.minute

                    send_stats(cmd_nodes(command, collect_time), c_t)
                    time.sleep(1)
                else:
                    time.sleep(1)
        except Exception, e:
            print e
            print "**Top Level Exception"
            pass

rawDescription: How to execute program at specific times

description: Execute program at specific times


code:
def create_or_fail(graph_db, start_node, end_node, relationship):
    if len(list(graph_db.match(start_node=start_node, end_node=end_node, rel_type=relationship))) > 0:
        print "Relationship already exists"
        return None
    return graph_db.create((start_node, relationship, end_node))

rawDescription: (py2neo) How to check if a relationship exists?


code:
from datetime import datetime, timedelta

# This does not check for crossing from Sunday to Monday
def convert_dt(start_dt, journey):
    begin_weekday, begin_hour, begin_minute = journey[0]
    end_weekday, end_hour, end_minute = journey[1]

    begin_dt = start_dt + timedelta(days=begin_weekday)
    begin_dt += timedelta(hours=begin_hour, minutes=begin_minute)

    end_dt = start_dt + timedelta(days=end_weekday)
    end_dt += timedelta(hours=end_hour,minutes=end_minute)
    return (begin_dt, end_dt)

def get_slot_journeys(start_dt, journeys):
    next_dt = start_dt
    slot_count =  60/5 * 24 * 7
    slot_dict = {}

    journey_dts = []
    #convert journey begin and end to datetimes
    for index in range(len(journeys['begin_weekday'])):
        next_journey = [(journeys['begin_weekday'][index],
                         journeys['begin_hour'][index],
                         journeys['begin_minute'][index],),
                        (journeys['end_weekday'][index],
                         journeys['end_hour'][index],
                         journeys['end_minute'][index],)
                       ]
        journey_dts.append(convert_dt(start_dt, next_journey))

    for slot in range(slot_count):
        slot_dict[next_dt] = 0
        for journey_start, journey_end in journey_dts:
            if next_dt >= journey_start and next_dt <= journey_end:
                slot_dict[next_dt] = slot_dict[next_dt] + 1

        next_dt += timedelta(minutes=(5))

    return slot_dict

if __name__ == "__main__":
    start_dt = datetime(2012, 1, 2, 0, 0)

    journeys = {'begin_weekday': [0, 0],
                'begin_hour': [14, 18],
                'begin_minute': [20, 30],
                'end_weekday': [0, 1],
                'end_hour': [19, 12],
                'end_minute': [15, 55],
               }
    slot_dict = get_slot_journeys(start_dt, journeys)
    slot_keys = slot_dict.keys()
    slot_keys.sort()

    for key in slot_keys:
        if slot_dict[key]:
            print key, slot_dict[key]

rawDescription: Looping over weekday in python time object


code:
import re

def get_output_parameters(serial_output):
    p = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)S(\d*)C(\d*\.?\d*)(.*)') # here is the regular expressions i created from this link http://regex101.com/r/dP6fE1/1
    p2 = re.compile(ur'^BASE_RAD: NEW,(.*)#T(\d*)R-(\d*),(.*):T(\d*)R-(\d*),(.*);Q(\d*)V(\d*\.?\d*)D(\d*)(.*)')

    Serial_Results = re.match(p, serial_output)
    digital_out = False
    if not Serial_Results:
        Serial_Results = re.match(p2, serial_output)
        digital_out = True

    # Assigning variables to the array values
    Base_ID = Serial_Results.group(1)
    Base_Time_Stamp = Serial_Results.group(2)
    Base_Signal = Serial_Results.group(3)
    Repeater_ID = Serial_Results.group(4)
    Repeater_Time_Stamp = Serial_Results.group(5)
    Repeater_Signal = Serial_Results.group(6)
    Sensor_ID = Serial_Results.group(7)
    Sensor_Sequence = Serial_Results.group(8)
    Sensor_Input_Voltage = Serial_Results.group(9)
    Sensor_Wind_Speed = Serial_Results.group(10)
    Sensor_Temperature = Serial_Results.group(11)
    if not digital_out:
        Checksum = Serial_Results.group(12)

    print Sensor_Temperature

Serial_Output = "BASE_RAD: NEW,ATC001#T1412010472R-77,ATC005:T1412010460R-70,SU0003;Q6V8.9S0C11.5*xx"
Serial_Output2 = "BASE_RAD: NEW,ATC001#T1413824282R-102,ATC003:T1413824274R-98,SU001G;Q0V14.0D00*x"

get_output_parameters(Serial_Output)
get_output_parameters(Serial_Output2)

rawDescription: python regular expression for multiple string values

description: Python regular expression for multiple string values


code:
import xml.etree.ElementTree as ET
def test():
    tree = ET.parse('test2.xml')
    root = tree.getroot()
    temp = root.findall("./Step")
    for item in temp:
        values = item.text.strip('\n').splitlines()
    values_out = process_step_data(values)
    print values_out

def process_step_data(output_step):
    step_result = []
    for i in range(len(output_step)):
        for num_str in output_step[i].splitlines():
            x = [float(j) for j in num_str.split()]
            step_result = step_result + x
    return step_result

rawDescription: Python - Reading space separated numbers in a string


code:
with ZipFile('foo.zip', 'r') as f:
    names = f.namelist()
print names
# ['file1', 'folder1/file2', ...]

rawDescription: Using Python, getting the name of files in a zip archive


code:
class Employee(object):
  locations = []
  def __init__(self, ldap, name, location, salary, status):
    self.ldap = ldap
    self.name = name
    self.location = location
    self.locations.append(location)
    self.salary = salary
    self.status = status

employee1 = Employee('axlr', 'Axl Rose', 'Dublin', 50000, 'active')
employee2 = Employee('slash', 'Slash', 'Dublin', 50000, 'active')
employee3 = Employee('peterp', 'Peter Pan', 'New York', 50000, 'active')
print Employee.locations

rawDescription: Python - Iterate through class

description: Python - Iterate through class


code:
if "a" in args:
    print "x"
elif "b" in args:
    print "y"
else:
    raise ValueError("must specify at least a or b")

rawDescription: Do action based on parameters received

description: Do action based on parameters received


code:
def retrieveFileData():
    """Retrieve Location/Upstream data from files"""
    lines = set()
    for line in open(LOCATION_FILE):
        lines.add(line.strip())
    return lines

def add_line(line):
    """Add new entry to file"""
    f = open(LOCATION_FILE, 'a')
    lines = retrieveFileData()
    print lines
    if line not in lines:
        f.write(line + '\n')
        lines.add(line)
        f.close()
    else:
        print "entry already exists"

if __name__ == "__main__":
    while True:
        line = raw_input("Enter line manually: ")
        add_line(line)
        if line == 'quit':
            break

rawDescription: how access to a file concurrently to add/edit/delete the data?

description: How access to a file concurrently to add/edit/delete the data


code:
from collections import Counter
chrome_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
             12, 13, 14, 15, 16, 17, 18, 19, 20,
             21, 22, "x", "y"]
for i in chrome_list:
    file_ = open('{}.fa'.format(i), 'r')
    broken_file = file_.read().split('\n\n')
    for line in broken_file:
        print Counter(line.split('\n')[1])
    file_.close()

rawDescription: Count GC content of fasta using python without error


code:
with open('Program File', 'r') as f: # open file
    for line in f: # get each line
        row = line.split('\t') # split line into in list by tabs
        print row[3] # print fourth value

rawDescription: Python Filing - need help (beginner stuff)


code:
uneven = [[1], [47, 17, 2, 3], [3], [12, 5, 75, 33]]

from itertools import repeat

print zip(*(repeat(*x) if len(x)==1 else x for x in uneven))

rawDescription: Python list transpose and fill

description: Python list transpose and fill


code:
import gevent
import random

def _get_status(greenlets):
    total = 0
    running = 0
    completed = 0
    successed = 0
    yet_to_run = 0
    failed = 0

    for g in greenlets:
        total += 1
        if bool(g):
            running += 1
        else:
            if g.ready():
                completed += 1
                if g.successful():
                    successed += 1
                else:
                    failed += 1
            else:
                yet_to_run += 1

    assert yet_to_run == total - completed - running
    assert failed == completed - successed

    return dict(total=total,
                running=running,
                completed=completed,
                successed=successed,
                yet_to_run=yet_to_run,
                failed=failed)

def get_greenlet_status(greenlets):
    while True:
        status = _get_status(greenlets)
        print status
        if status['total'] == status['completed']:
            return
        gevent.sleep(5)

def crawl(url):
    r = random.randint(0, 10)
    gevent.sleep(r)

    err = random.randint(0, 4)
    if err == 0:
        raise Exception

greenlets = [gevent.spawn(crawl, each) for each in xrange(100)]

get_greenlet_status(greenlets)

rawDescription: Get total number of running greenlets in Gevent?


code:
import getenv
print getenv("REMOTE_ADDR")

rawDescription: Python thinks I'm a different IP

description: Python thinks I'm a different IP


code:
from itertools import product
from collections import namedtuple

param_list = []

Parameter = namedtuple("Parameter", ['bad', 'good'])
param1 = Parameter(["peanut", "gluten"], ["bacon", "pickle"])
param_list.append(param1)
param2 = Parameter([0], [1, 22])
param_list.append(param2)
param3 = Parameter([0, 1], [2, 3, 4, 9])
param_list.append(param3)
param4 = Parameter(["football"], ["running", "yoga"])
param_list.append(param4)

# Pulling one item ahead of loop so as to avoid the last item.
iterable = product(*param_list)
prev_prod = next(iterable)

for prod in iterable:
    for sub_prod in product(*prev_prod):  # Using 'prev_prod' instead of 'prod'
        prod_str = [str(x) if type(x) is not str else x for x in sub_prod]
        print ", ".join(prod_str)

    prev_prod = prod

rawDescription: How to stop iterating over an iterator at the penultimate item?

description: Stop iterating over an iterator at the penultimate item


code:
def createSubElement(text, tagText = ""):
    elem = etree.SubElement(root, text)
    elem.text = tagText

createSubElement('firstChild', 'first one')
createSubElement('SecondChild', 'second one')

print etree.tostring(root)

rawDescription: Confused as to use a class or a function: Writing XML files using lxml and Python


code:
class UserPickAuthorization(Authorization):
    # Checks that the records' owner is either None or the logged in user
    def authorize_user(self, bundle):
        print 'Authorize User'

        if bundle.request.user.is_superuser:
            return True
        if bundle.request.user == bundle.obj.user:
            return True

        return False

    def user(self, bundle):
        print 'User'
        return User.objects.get(pk=bundle.request.pk)

    def read_list(self, object_list, bundle):
        print 'Read List'
        return object_list.filter(Q(user = self.user(bundle)) | Q(user = None))

    def read_detail(self, object_list, bundle):
        print 'Read Detail'
        return self.authorize_user(bundle)

    def create_list(self, object_list, bundle):
        print 'Create List'
        return object_list

    def create_detail(self, object_list, bundle):
        print 'Create Detail'
        return self.authorize_user(bundle)

    def update_list(self, object_list, bundle):
        print 'Update List'
        allowed = []
        for obj in object_list:
            print "User is superuser %s"%(bundle.request.user.is_superuser)
            print "User owns obj %s"%(bundle.request.user == bundle.obj.user)

            if bundle.request.user.is_superuser or bundle.request.user == bundle.obj.user:
                allowed.append(obj)

        return allowed

class UserPickResource(ModelResource):
    pick = fields.ToOneField(TeamResource, 'pick', full=True)
    user = fields.ToOneField(UserResource, 'user', full=True)
    league = fields.ToOneField(LeagueResource, 'league', full=True)

    class Meta:
        queryset = UserPick.objects.all()
        resource_name = 'userpick'
        authentication = SessionAuthentication()
        authorization = UserPickAuthorization()
        list_allowed_methods = ['get', 'post','put', 'patch', 'delete']
        always_return_data = True
        filtering = {
            'pick': ALL_WITH_RELATIONS,
            'league': ALL_WITH_RELATIONS,
            'user': ALL_WITH_RELATIONS,
            'week' : ALL
        }

rawDescription: Allow a user to only delete his own comments with tastypie


code:
from requests import Session

session = Session()

# HEAD requests ask for *just* the headers, which is all you need to grab the
# session cookie
session.head('http://sportsbeta.ladbrokes.com/football')

response = session.post(
    url='http://sportsbeta.ladbrokes.com/view/EventDetailPageComponentController',
    data={
        'N': '4294966750',
        'form-trigger': 'moreId',
        'moreId': '156#327',
        'pageType': 'EventClass'
    },
    headers={
        'Referer': 'http://sportsbeta.ladbrokes.com/football'
    }
)

print response.text

rawDescription: Simulating ajax request with python using requests lib


rawDescription: Find Synonyms for multi-word phrases

description: Find Synonyms for multi-word phrases


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

app = QApplication(sys.argv)

web = QWebView()
web.load(QUrl("http://www.google.com"))
#web.show()

printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("file.pdf")

def convertIt():
    web.print_(printer)
    print "Pdf generated"
    QApplication.exit()

QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)

sys.exit(app.exec_())

rawDescription: PIL - are there a any PIL solutions that will allow you to take a screenshot of a specified web page?


code:
students = int(raw_input())
student_inf = {}
student_marks = []
for i in range(0,students):
    student_name = raw_input()
    student_inf[student_name] = {}
    student_marks = [int(x) for x in raw_input().split()]
    for entry in student_marks:
       student_inf[student_name][entry] = entry
    print str(sum(student_inf[student_name].values())/3.0)

rawDescription: When I'm trying to enter marks in dictionary I couldnt do and the loop exits without it initiating


code:
texts = [ "The lion (Panthera leo) ...", "Panthera ...", "..." ]
keywords  = ['cat', 'lion', 'panthera', 'family']

# gives the count of `word in text`
def matches(text):
    return sum(word in text.lower() for word in keywords)

# or inline that helper function as a lambda:
# matches = lambda text:sum(word in text.lower() for word in keywords)

# print the one with the highest count of matches
print max(texts, key=matches)

rawDescription: Python: How to find the string with most matches in a list of strings


code:
def printTwos(n):
   if n % 4 != 0:
      if n % 2 == 0:
         # n is divisible by 2 so add two at the beginning of string
         result = printTwos(n/2)
         return "2 * " + result
      else:
         # n is odd so return it as string
         return str(n)
   else:
      result = printTwos(n/4)
      return "2 * %s * 2" % result

print printTwos(4)
print printTwos(8)
print printTwos(32)
print printTwos(80)

rawDescription: Python recursive function: Takes even Num (n), returns odd num multiplied by 2s such that the final value is equal to n


code:
from bs4 import BeautifulSoup
import re

html = r"""

Completion Information

                Well Status Code

Spud Date

Drilling Completed

Surface Casing Date

                W - Final Completion

12/08/2011

02/14/2012

12/09/2011

                Field Name
                Completed Well Type

Completed Date

Validated Date

                   WOLFBONE (TREND AREA)

Oil

02/14/2012

06/04/2013

"""
soup = BeautifulSoup(html)
tab_box = soup.findAll('table', {'class': 'TabBox'})

for var in tab_box:
    print var

rawDescription: Python, Soup, and grabbing tables

description: Python, Soup, and grabbing tables


code:
import random

meat = ['pork', 'beef']*5
fruit = ['grape', 'banana', 'apple']*30
random.shuffle(meat)
random.shuffle(fruit)

result = fruit[-5:]
del fruit[-5:]

result.append(meat.pop())
while meat:
    result.extend(fruit[-3:])
    del fruit[-3:]
    result.append(meat.pop())

resultlength = len(result)
while fruit:
    insertion_location = random.randint(0,resultlength)
    result.insert(insertion_location,fruit.pop())
    resultlength += 1

print result

rawDescription: Shuffling the order of list with multiple constraints using Python


code:
def quote(string):
    text = ''
    stack = []
    for token in iter_tokes(string):
        if is_quote(token):
            if stack and stack[-1] == token: # closing
                text += '}'
                stack.pop()
            else: # opening
                text += '\\quotation{'
                stack.append(token)
        else:
            text += token
    return text

def iter_tokes(string):
    i = find_quote(string)
    if i is None:
        yield string
    else:
        if i > 0:
            yield string[:i]
        yield string[i]
        for q in iter_tokes(string[i+1:]):
            yield q

def find_quote(string):
    for i, char in enumerate(string):
        if is_quote(char):
            return i
    return None

def is_quote(char):
    return char in '\'\"'

def main():
    quoted = None
    with open('input.txt') as fh:
        quoted = quote(fh.read())
    print quoted

main()

rawDescription: Parse text to replace quotes and nested quotes


code:
import sys

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.QtWebKit import QWebPage, QWebView

app = QApplication(sys.argv)
web_view = QWebView()
web_view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
def url_changed(url):  print 'url changed: ', url
def link_clicked(url):  print 'link clicked: ', url
def load_started():  print 'load started'
def load_finished(ok):  print 'load finished, ok: ', ok
web_view.connect(web_view, SIGNAL("urlChanged(const QUrl&)"), url_changed)
web_view.connect(web_view, SIGNAL("linkClicked(const QUrl&)"), link_clicked)
web_view.connect(web_view, SIGNAL('loadStarted()'), load_started)
web_view.connect(web_view, SIGNAL('loadFinished(bool)'), load_finished)
web_view.load(QUrl('http://google.com'))
web_view.show()
sys.exit(app.exec_())

rawDescription: QWebKit linkClicked signal never fires

description: QWebKit linkClicked signal never fires


code:
link = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', link)
# pay attention on iteration over set(links) and not links
for link in set(links):
    print link

rawDescription: Cleaning scrapped url in python

description: Cleaning scrapped url in python


code:
from time import time

# your code here

tic = time()
cursor.execute("select * from db.customer")
toc = time()
print toc - tic

rawDescription: How to calculate a MSSQL query execution time using python

description: Calculate a MSSQL query execution time using python


code:
def veggie_r_ip(foods, curIndex):
    if(curIndex >= len(foods)):
        return
    curFood = foods[curIndex]
    is_veggie = curFood.split('|')[2]
    if is_veggie == "False":
        foods.remove(curFood)
        veggie_r_ip(foods, curIndex)
    else:
        veggie_r_ip(foods, curIndex + 1)

def main():
    foods =['Spicy Vegetable Moo Shu|1|True|140', 'BBQ Pork|1|False|920', 'Chicken Breast|1|False|920', 'Salad|1|True|920']
    veggie_r_ip(foods, 0)
    print foods

rawDescription: Removes non-vegetarian foods from a list of foods.


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

# pass 1: create nodes dictionary
nodes = {}
for i in a:
    id, parent_id = i
    nodes[id] = { 'id': id }

# pass 2: create trees and parent-child relations
forest = []
for i in a:
    id, parent_id = i
    node = nodes[id]

    # either make the node a new tree or link it to its parent
    if id == parent_id:
        # start a new tree in the forest
        forest.append(node)
    else:
        # add new_node as child to parent
        parent = nodes[parent_id]
        if not 'children' in parent:
            # ensure parent has a 'children' field
            parent['children'] = []
        children = parent['children']
        children.append(node)

print forest

rawDescription: Python - Generate a dictionary(tree) from a list of tuples


code:
L = [1, 2, 3]
for i in L:
    L.append(4)
    print L
    if len(L) > 30:
        break

rawDescription: why the list behaves differently in python and Java?

description: Why the list behaves differently in python and Java


code:
args = sys.argv[:]  # Copy so don't destroy original
while len(args) > 0:
    current_arg = args[0]

    if current_arg == '-f':
        foo = args[1]
        args = args[2:]
    elif current_arg == '-b':
        bar = args[1]
        args = args[2:]
    elif current_arg == '-z':
        baz = args[1]
        args = args[2:]
    else:
        print 'Unknown argument: %r' % args[0]
        args = args[1:]

rawDescription: Parsing command line options in Jython 2.1


code:
if path.exists(path.expanduser('~/Desktop/AZDif.png')):
    print 'Yes'

rawDescription: How to know a file exists in the Desktop using Python on Mac OS?

description: Know a file exists in the Desktop using Python on Mac OS


code:
import mechanize

store_url = 'http://store.nike.com'
cart_url = 'http://store.nike.com/us/services/jcartService?callback=nike_Cart_hanleJCartResponse&action=addItem&lang_locale=en_US&country=US&catalogId=1&productId=463712&price=00.0&siteId=null&line1=Nike+Air+Max+1+Ultra+Moire&line2=Men%27s+Shoe&passcode=null&sizeType=null&skuAndSize=10661133%3A10&qty=1&rt=json&view=3&skuId=10661133&displaySize=14&_=142655682313'

br = mechanize.Browser()
response = br.open(store_url)
response = br.open(cart_url)
data = response.read()
print data

rawDescription: Scraping with Mechanize

description: Scraping with Mechanize


code:
import collections
thing = collections.defaultdict(list)
thing[0].append('spam')
thing[1].append('eggs')
print thing
thing[0]
thing[69]

rawDescription: Adding values to a list in a list in Python


code:
class A(object):
    def __hash__(self):
        print '__hash__'
        return 42

    def __cmp__(self):
        print '__cmp__'
        return object.__cmp__(self)

    def __eq__(self, rhs):
        print '__eq__'
        return True

a1 = A()
a2 = A()
print a1 in set([a1])
print a1 in set([a2])

rawDescription: Python set "in" operator: uses equality or identity?


code:
# store values
tot = {}
# the last date
date0 = Dict1[-1]['date']

# easier to work from back, i found
for line in Dict1[-1::-1]:
    date, name, qty = [line[x] for x in 'date', 'name', 'qty']

    # add the value to all subsequent days
    for d in range(date, date0+1):
        tot.setdefault(d, {}).setdefault(name, [0])[0] += qty

# i was putting value into array, and i put it out into a scalar
tot = dict((k, dict((kk,vv[0]) for kk,vv in v.items())) for k,v in tot.items())
print tot

rawDescription: Running total for list of dict

description: Running total for list of dict


code:
import numpy as np
import pandas as pd

# Read in your file as a pandas.DataFrame
# using 'any number of whitespace' as the seperator
df = pd.read_csv("snow.txt", sep='\s*', names=["year", "month", "day", "snow_depth"])

# Show the first 5 rows of the DataFrame
print df.head()

# Group data first by year, then by month
g = df.groupby(["year", "month"])

# For each group, calculate the average of only the snow_depth column
monthly_averages = g.aggregate({"snow_depth":np.mean})

rawDescription: Monthly Averages Using Daily Data Using Python Pandas


code:
class My_Thread(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.process = None

    def run(self):
        print "Starting " + self.name
        cmd = [ "bash", 'process.sh']
        self.process = p = subprocess.Popen(cmd,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT)
        for line in iter(p.stdout.readline, b''):
            print ("-- " + line.rstrip())
        print "Exiting " + self.name

    def stop(self):
        print "Trying to stop thread "
        if self.process is not None:
            self.process.terminate()
            self.process = None

thr = My_Thread()
thr.start()
time.sleep(30)
thr.stop()
thr.join()

rawDescription: How terminate Python thread without checking flag continuously


code:
import string

s = "@John, It's a fantastic #week-end%, How about () you"
for c in "!@#%&*()[]{}/?<>":
    s = string.replace(s, c, "")

print s

rawDescription: Remove extra characters in the string in Python


code:
dd = {}
referrer = None

for line in open('hosts.txt'):
    if line.startswith('Host'):
        host = line.split(': ')[1].strip('\n')
    elif line.startswith('Referer'):
        referrer = line.split(': ')[1].strip('\n')

    if referrer is not None:
        dd.setdefault(referrer, set()).add(host)
        referrer = None

for k, v in dd.iteritems():
    print k, len(v)

rawDescription: Relating two consecutive lines in a file


code:
from bs4 import BeautifulSoup
import collections
import glob
import re
import os

path = r'mypath'
file_stats = []

search_list = ['some text I want', 'some other text']
search_list = [phrase.lower() for phrase in search_list]    # Ensure list is all lowercase

for filename in glob.glob(os.path.join(path, '*.html')):
    with open(filename, 'r') as f_input:
        html = f_input.read()

    soup = BeautifulSoup(html, 'html.parser')

    # Remove style and script sections from the HTML
    for script in soup(["style", "script"]):
        script.extract()

    # Extract all text
    text = soup.get_text().encode('utf-8')

    # Create a word list in lowercase
    word_list = [word.lower() for word in re.sub("[^\w]", " ",  text).split()]

    # Search for matching phrases
    phrase_counts = dict()
    text = ' '.join(word_list)

    for search in search_list:
        phrase_counts[search] = text.count(search)

    # Calculate the word counts
    word_counts = collections.Counter(word_list)

    # Filter unique words
    unique_words = sorted(word for word, count in word_counts.items() if count == 1)

    # Create a list of unique words and phrase matches for each file
    file_stats.append([filename, unique_words, phrase_counts])

# Display the results for all files
for filename, unique_words, phrase_counts in file_stats:
    print '{:30} {}'.format(filename, unique_words)
    for phrase, count in phrase_counts.items():
        print '  {} : {}'.format(phrase, count)

rawDescription: How to track number of occurrences with set.union()

description: Track number of occurrences with set.union()


code:
from copy import deepcopy

class guarded_property(object):
    # self.name attribute set here by the metaclass
    def __set__(self, instance, value):
        setattr(instance, "_" + self.name, value)
        setattr(instance, "_" + self.name + "_copy", deepcopy(value))
        setattr(instance, "_" +  self.name + "_dirty", True)

    def __get__(self, instance, owner):
        return getattr (instance, "_" + self.name)

    def clear(self, instance):
        setattr(instance, "_" +  self.name + "_dirty", False)
        setattr(instance, "_" + self.name + "_copy", deepcopy(self.__get__(instance, None)))

    def dirty(self, instance):
        return getattr(instance, "_" +  self.name + "_dirty") or   not (getattr(instance, "_" + self.name + "_copy") == self.__get__(instance, None))

class Guarded(type):
    def __new__(cls, name, bases, dict_):
        for key, val in dict_.items():
            if isinstance(val, guarded_property):
                val.name = key
                dict_[key + "_clear"] = (lambda v: lambda self: v.clear(self))(val)
                dict_[key + "_dirty"] = (lambda v: property(lambda self: v.dirty(self)))(val)
        return type.__new__(cls, name, bases, dict_)

if __name__ == "__main__":
    class Example(object):
        __metaclass__ = Guarded
        a = guarded_property()

    g =  Example()
    g.a = []
    g.a_clear()
    print g.a_dirty
    g.a.append(5)
    print g.a_dirty
    g.a_clear()
    print g.a_dirty

rawDescription: Logging access to elements in data members of python objects


code:
from collections import defaultdict, Counter
from itertools import ifilter
from random import choice, randrange

def pairwise(iterable):
    it = iter(iterable)
    last = next(it)
    for curr in it:
        yield last, curr
        last = curr

valid = set('abcdefghijklmnopqrstuvwxyz ')

def valid_pair((last, curr)):
    return last in valid and curr in valid

def make_markov(text):
    markov = defaultdict(Counter)
    lowercased = (c.lower() for c in text)
    for p, q in ifilter(valid_pair, pairwise(lowercased)):
        markov[p][q] += 1
    return markov

def genrandom(model, n):
    curr = choice(list(model))
    for i in xrange(n):
        yield curr
        if curr not in model:   # handle case where there is no known successor
            curr = choice(list(model))
        d = model[curr]
        target = randrange(sum(d.values()))
        cumulative = 0
        for curr, cnt in d.items():
            cumulative += cnt
            if cumulative > target:
                break

model = make_markov('The qui_.ck brown fox')
print ''.join(genrandom(model, 20))

rawDescription: Markov chain on letter scale and random text


code:
#!/usr/bin/python

lines = open('CVE.txt').readlines()
nexpose = set([l.split(':')[1].strip() for l in lines if l.startswith('NeXpose')])
snort   = set([l.split(':')[1].strip() for l in lines if l.startswith('Snort')])

# print 'Nexpose: ', ', '.join(nexpose)
# print 'Snort  : ', ', '.join(snort)

print 'CVEs both in Nexpose and Snort  : ', ', '.join(snort.intersection(nexpose))

rawDescription: Matching entries using python

description: Matching entries using python


code:
from threading import Thread, Lock  # <----

def f(l, i, n):
    l.acquire()
    i.append(n)
    l.release()
    print "in:", i

if __name__ == '__main__':
    lock = Lock()

    i = []
    for num in range(10):
        p = Thread(target=f, args=(lock, i, num))  # <----
        p.start()
        p.join()

    print "out:", i

rawDescription: Pass-by-reference mutable variable in a thread


code:
from lxml import etree

xml = etree.XML('''

hi there

2nd paragraph

                   ''')

# If you want to visit all of the descendants
for element in xml.iter():
    print element.tag

# Or, if you want to have a list of all the descendents
all_elements = list(xml.iter())
print [element.tag for element in all_elements]

rawDescription: lxml - get a flat list of elements

description: Lxml - get a flat list of elements


code:
from datetime import datetime, timedelta

this_month_start = datetime.now().date().replace(day=1)
prev_month_start = (this_month_start - timedelta(days=1)).replace(day=1)

pages = blog.get_page_list()
last_month_pages = [
  p for p in pages
  if prev_month_start <= p['dateCreated'] < this_month_start]
last_month_approved_comment_count = sum(
  blog.get_comment_count(page['page_id'])['approved']
  for page in last_month_pages)

print "number of last month's pages:", len(last_month_pages)
print "number of approved comments for last month's pages:",
print last_month_approved_comment_count

rawDescription: Cutting down a list based on events being in between two datetimes


code:
from operator import itemgetter
with open("f") as f:
    pairs = [line.split(" ") for line in f.readlines()]
    asc_pairs = [(min([int(x),int(y)]), max([int(x), int(y)]))
            for x,y in pairs]
    print "\n".join([" ".join([str(l) for l in k])
        for k in sorted(asc_pairs, key=itemgetter(0,1))])

rawDescription: Sorting output across multiple lines - python


code:
i = 0
count = 0
while i < len(S):
    if S[i] == 'a':
        count += 1
    i += 1
print count

rawDescription: How to replace the for with a while loop

description: Replace the for with a while loop


rawDescription: Python: Summing the array elements returned by a loop


code:
for i in range(0, xMax):
    temp_variable = ''
    for j in range(0, yMax):
        temp_variable += arena_shape[ j + i*yMax]
    print temp_variable

rawDescription: Python: print array of strings to stdout line by line


code:
import subprocess
proc = subprocess.Popen(
    'python',stdout=subprocess.PIPE,
    stdin=subprocess.PIPE)
proc.stdin.write('import antigravity\n')
proc.stdin.write('print "something"\n')
proc.stdin.close()
result = proc.stdout.read()
print result

rawDescription: Input python commands on the python command line


code:
import re
referrer = "http://myapp.com/other/page/aaa"
m = re.match("^http://myapp.com/other/page/(.*)", referrer)
if m:
    print m.group(1)

rawDescription: Match referer by regex

description: Match referer by regex


code:
import subprocess, sys
from Queue import Queue, Empty
from threading import Thread

def process_line(line):

    if line == "stop\n": # lines have trailing new line characters
        print "SERVER SHUTDOWN PREVENTED"
        return None
    elif line == "quit\n":
        return "stop\n"
    elif line == "l\n":
        return "list\n"

    return line

s = subprocess.Popen("java -jar minecraft_server.jar nogui", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

def read_lines(stream, queue):
    while True:
        queue.put(stream.readline())

# terminal reading thread
q = Queue()
t = Thread(target=read_lines, args=(sys.stdin, q))
t.daemon = True
t.start()

# server reading thread
qs = Queue()
ts = Thread(target=read_lines, args=(s.stdout, qs))
ts.daemon = True
ts.start()

while s.poll() == None: # loop while the server process is running

    # get a user entered line and send it to the server
    try:
        line = q.get_nowait()
    except Empty:
        pass
    else:
        line = process_line(line) # do something with the user entered line
        if line != None:
            s.stdin.write(line)
            s.stdin.flush()

    # just pass-through data from the server to the terminal output
    try:
        line = qs.get_nowait()
    except Empty:
        pass
    else:
        sys.stdout.write(line)
        sys.stdout.flush()

rawDescription: A Python Wrapper or Handler for A Minecraft Server


rawDescription: How to add seconds(can go upto 10E12) to a datetime variable in python

description: Add seconds(can go upto 10E12) to a datetime variable in python


code:
import subprocess
import multiprocessing

def callback(result):
    # do something with result
    pid, returncode = result
    print pid, returncode

def call_process(cmd):
    p = subprocess.Popen(cmd)
    p.wait()
    return p.pid, p.returncode

def main():
    pool = multiprocessing.Pool()
    pool.apply_async(call_process, [["/bin/sleep", "2"]], callback=callback)
    pool.apply_async(call_process, [["/bin/sleep", "5"]], callback=callback)
    pool.close()
    pool.join()

main()

rawDescription: python subprocess: find out in the signal handler which child terminated

description: Python subprocess: find out in the signal handler which child terminated


code:
for key, value in {'one':1, 'two':2, 'three':3}.iteritems():
    print key, ":", value

rawDescription: looping a unnamed dictionary in python

description: Looping a unnamed dictionary in python


code:
from collections import defaultdict

a = defaultdict(lambda:None)
print a[100]

rawDescription: how to rewrite python dicts to get default values

description: Rewrite python dicts to get default values