code:
from joblib import Parallel, delayed
import numpy as np
import os
import tempfile
import shutil

def main():

    print "Nested loop array assignment:"
    regular()

    print "Parallel nested loop assignment using numpy's memmap:"
    par3(4)

def regular():
    # Define variables
    a = [0,1,2,3,4]
    b = [0,1,2,3,4]

    # Set array variable to global and define size and shape
    global ab
    ab = np.zeros((2,np.size(a),np.size(b)))

    # Iterate to populate array
    for i in range(0,np.size(a)):
        for j in range(0,np.size(b)):
            func(i,j,a,b)

    # Show array output
    print ab

def par3(process):

    # Creat a temporary directory and define the array path
    path = tempfile.mkdtemp()
    ab3path = os.path.join(path,'ab3.mmap')

    # Define variables
    a3 = [0,1,2,3,4]
    b3 = [0,1,2,3,4]

    # Create the array using numpy's memmap
    ab3 = np.memmap(ab3path, dtype=float, shape=(2,np.size(a3),np.size(b3)), mode='w+')

    # Parallel process in order to populate array
    Parallel(n_jobs=process)(delayed(func3)(i,a3,b3,ab3) for i in xrange(0,np.size(a3)))

    # Show array output
    print ab3

    # Delete the temporary directory and contents
    try:
        shutil.rmtree(path)
    except:
        print "Couldn't delete folder: "+str(path)

def func(i,j,a,b):
    # Populate array
    ab[0,i,j] = a[i]+b[j]
    ab[1,i,j] = a[i]*b[j]

def func3(i,a3,b3,ab3):
    # Populate array
    for j in range(0,np.size(b3)):
        ab3[0,i,j] = a3[i]+b3[j]
        ab3[1,i,j] = a3[i]*b3[j]

# Run script
main()

rawDescription: Accessing and altering a global array using python joblib


code:
from lxml.html import html5parser
doc = html5parser.fromstring(body)
print doc.xpath("string()")

rawDescription: Use html5lib to convert an HTML fragment to plain text


code:
import os, string, arcpy, json
arcpy.env.overwriteOutput = True

layer = "C:\\Other\\Shapefiles\\Geo500K_JSON\\GEOLOGY_500K_kn.shp"

output = "C:\\Other\\Shapefiles\\Geo500K_JSON\\"

outfile = output + "Geo500K_knTest_C2.json"

idfield = "ORIG_LABEL"
shape_field = arcpy.Describe(layer).shapeFieldName

features = []
geojson = {
    'type': 'FeatureCollection',
    'features': features
    }

rows = arcpy.SearchCursor(layer,"","","",idfield + " A")
for row in rows:
    geostring = '' #for each lat/lng pt
    geolist = [] # array for storing individual geostrings
    ringList = [] #array for storing geolist array with geostrings separated by commas
    partList = [] #array for storing partlist, final array used
    shapeString = ''

    extent = row.Shape.extent
    centroid = row.getValue(shape_field).centroid

    coordinates = []

    feature = {
        'type': 'Feature',
        'bbox': [ extent.XMin, extent.YMin, extent.XMax, extent.YMax ],
        'properties': {
            'label': str(row.getValue(idfield)),
            'name': str(row.getValue("FM_NAME")),
            'lithType': str(row.getValue("LithType")),
            'rank': str(row.getValue("Rank")),
            'lithName': str(row.getValue("LithName")),
            'ageType': str(row.getValue("AgeType")),
            'minAge': str(row.getValue("MinAge")),
            'maxAge': str(row.getValue("MaxAge")),
            'center': [ centroid.X, centroid.Y ],
            'centroid': [ centroid.X, centroid.Y ]
        },
        'geometry': {
            'type': 'MultiPolygon',
            'coordinates': coordinates
        }
    }

    feat = row.shape
    for p in range(feat.partCount):
        part = feat.getPart(p)
        pt = part.next()
        while pt:
            lat = str(round(pt.Y,6))
            lon = str(round(pt.X,6))

            geostring = '[' + lon + ',' + lat + ']'
            geolist.append(geostring)

            pt = part.next()

            #if no following point go to the next part which should be an interior ring.
            if not pt:
                ringList.append(',' .join(geolist))
                geostring = ''
                geolist = []
                pt = part.next()
                if pt:
                    print 'Interior Ring: '

        partList.append('],[' .join(ringList))
        ringList = []

    features.append( feature )
    shapeString = ']],[[' .join(partList)
    coordinates.append(shapeString)

with open(outfile,'wb') as jsonFile:
    json.dump( geojson, jsonFile )

rawDescription: Can polygonzo for google maps handle interior rings?


code:
dicts = {1: {'age': 1, 'cost': 14, 'score': 0.3, 'sum_cost': 0},
         2: {'age': 1, 'cost': 9, 'score': 0.5, 'sum_cost': 0},
         3: {'age': 13, 'cost': 7, 'score': 0.4, 'sum_cost': 0}}

sum_addition = 0

for key, values in sorted(dicts.items(), key=lambda x: x[1].get('score', None)):
    if dicts[key].get('score') is not None: #By default gives None when key is not available
        dicts[key]['sum_cost'] = sum_addition
        sum_addition += dicts[key]['cost']
        print key, dicts[key]

rawDescription: Python dictionary: Add to the key sum of values fulfilling given condition


code: print item + str(i)

rawDescription: String concatenation acting crazy

description: String concatenation acting crazy


code:
import csv
from itertools import groupby
from operator import itemgetter
with open('word.txt','rb') as f1:
        reader = csv.reader(f1, delimiter='\t')
        for group, rows in groupby(reader, itemgetter(0)):
            best = sorted(rows, key=lambda r: (float(r[3]), float(r[2])),reverse=True)[:3]
            for a in best:
                print a
            print "\n"

rawDescription: How to print 3 lines with the highest values

description: Print 3 lines with the highest values


code:
from scapy.all import *
from random import randint
import threading
import time
import multiprocessing
from itertools import count

# Generate packet
pkts = IP(src="10.0.0.1",dst="10.0.0.2")/TCP()/Raw(RandString(size=120))
#print pkts
pkts[TCP].flags = "UFP"

pktList = []
for pktNum in range(0,5):
    pktList.extend(pkts)
    pktList[pktNum][TCP].dport = randint(1,65535) # Pkt has Ran PortNo.
    print pktList[pktNum].summary()
    #print len(pktList[pktNum])
    wrpcap('tcp-packets.pcap',pktList[pktNum])

# Send the list of packets send(pktList)
def send_p(queue):
    for run_number in count(): # this will run indefinitely, same as while True, must be killed to stop.
        start_time=time.time()
        send(pktList)
        totalTime = time.time()-start_time
        totalBytes=(50*120)/totalTime
        queue.put((run_number, totalTime, totalBytes))

def create_p2p_traffic():
    pass # do stuff

q = multiprocessing.Queue()
t = multiprocessing.Process(target=send_p, args=(q, ))
t.start()

time.sleep(10)

### Runs without P2P traffic

rates = []

while True: # This loop will pull all items out of the queue and display them.
    run = q.get()
    if not run: # When we reach the end of the queue, exit
        break
print "Average rate of {0:.1f} B/s without p2p traffic".format(sum(rates)/float(len(rates)))

### Runs with P2P traffic

p = multiprocessing.Process(target=create_p2p_traffic)
p.start()

time.sleep(10) # "Some interval of time"

if t.is_alive():
    t.terminate()

if p.is_alive():
    p.terminate()

p2prates = []
while True: # This loop will pull all items out of the queue and display them.
    run = q.get()
    if not run: # When we reach the end of the queue, exit
        break
    run_number, total_time, total_bytes = run
    print "Run {run_number} took a total of {total_time}\
at an average rate of {total_bytes:.1f} B/s".format(run_number=run_number,
                                                    total_time=total_time,
                                                    total_bytes=total_bytes)
    p2prates.append(total_bytes)

print "Average rate of {0:.1f} B/s after p2p started".format(sum(p2prates)/float(len(p2prates)))

rawDescription: How to stop a timer thread

description: Stop a timer thread


code:
def add(a, b):
    return a + b

def subtract(a, b):
    return a - b

a = 20
b = 10
print eval('add(a, b)')
print eval('subtract(a, b)')

rawDescription: Math operations from string

description: Math operations from string


code:
class PropagateOuter(type):
    def __init__(cls, name, bases, dct):
        type.__init__(cls, name, bases, dct)
        if "Serialiser" in dct:
            cls.Serialiser.outer = cls

class ModelBase(object):
    __metaclass__ = PropagateOuter

# Python 3 version of the above
# class ModelBase(metaclass=PropagateOuter):
#     pass

class SerialiserBase(LibrarySerialiser):
    def __init__(self, *args, **kwargs):
        print self.outer.origin
        super(SerialiserBase, self).__init__(*args, **kwargs)

class ComponentModel(ModelBase):
    origin = 'supermarket'

    class Serialiser(SerialiserBase):
        pass

ser = ComponentModel.Serialiser()

rawDescription: Accessing a parent variable from a nested child class in Python


code:
def nth(n):
    return str(n) + nth.ext[int(n)%10]
nth.ext = ['th', 'st', 'nd', 'rd'] + ['th']*6

print nth(t.tm_mday) + time.strftime(' %B %Y %H:%M', t)

rawDescription: Format datestamp in python

description: Format datestamp in python


code:
values = []
for select in playVal:
    for option in select.find_all('option'):
        values.append(option.get('value'))
print values

rawDescription: Option Value in loop

description: Option Value in loop


rawDescription: How to use regex with optional characters in python?

description: Use regex with optional characters in python


code:
from bs4 import BeautifulSoup

soup = BeautifulSoup("""

""")

for div in soup.find_all('div', class_='foo'):
    pos = div.attrs['class'].index('foo')
    div.attrs['class'][pos] = 'bar'

print soup

rawDescription: How to Rename a Class in Beautiful Soup

description: Rename a Class in Beautiful Soup


code:
from cStringIO import StringIO
from csv import reader

file_like_object = StringIO("1,,2,'3,4'")
csv_reader = reader(file_like_object, quotechar="'")
for row in csv_reader:
    print row

rawDescription: Transform comma separated string into a list but ignore comma in quotes


code:
import xlrd
import os, fnmatch

start_dir = 'C:/eclipse/TST-folder'

def excelfiles(pattern):
    file_list = []
    for root, dirs, files in os.walk(start_dir):
        for filename in files:
            if fnmatch.fnmatch(filename.lower(), pattern):
                if filename.endswith(".xls") or filename.endswith(".xlsx") or filename.endswith(".xlsm"):
                    file_list.append(os.path.join(root, filename))
    return file_list

file_list = excelfiles('*tst*')     # only accept docs hwom title includes tst
for i in file_list: print i

'''Location of each val from the excel spreadsheet'''

def locate_vals():
    val_dict = {}
    for filename in file_list:
        wb = xlrd.open_workbook(os.path.join(start_dir, filename))
        sheet = wb.sheet_by_index(0)

        # problem 2: extract these values once per sheet
        weightvalue = sheet.cell(3, 3).value
        lengthvalue = sheet.cell(3, 2).value
        speedvalue = sheet.cell(3, 4).value

        # problem 3: store them in a dictionary, keyed on filename
        val_dict[filename] = [weightvalue, lengthvalue, speedvalue]

    # dictionary keyed on filename, with value a list of the extracted vals
    return val_dict

print locate_vals()

rawDescription: Read files from list of files

description: Read files from list of files


code:
import ast, re
def dataType(str):
    str=str.strip()
    if len(str) == 0: return 'BLANK'
    try:
        t=ast.literal_eval(str)

    except ValueError:
        return 'TEXT'
    except SyntaxError:
        return 'TEXT'

    else:
        if type(t) in [int, long, float, bool]:
            if t in set((True,False)):
                return 'BIT'
            if type(t) is int or type(t) is long:
                return 'INT'
            if type(t) is float:
                return 'FLOAT'
        else:
            return 'TEXT'

testSet=['   1  ', ' 0 ', 'True', 'False',   #should all be BIT
         '12', '34l', '-3','03',              #should all be INT
         '1.2', '-20.4', '1e66', '35.','-   .2','-.2e6',      #should all be FLOAT
         '10-1', 'def', '10,2', '[1,2]','35.9.6','35..','.']

for t in testSet:
    print "{:10}:{}".format(t,dataType(t))

rawDescription: determine "type of value" from a string in python

description: Determine "type of value" from a string in python


code:
nums = []

for url in short_urls:
    query_params = {
         'access_token': ACCESS_TOKEN,
         'link': url.values()}

    endpoint = "https://api-ssl.bitly.com/v3/link/clicks"
    response = requests.get(endpoint, params = query_params)

    data = json.loads(response.content)

    print "link_clicks:" + " " + str(data['data']['link_clicks'])
    nums.append(data['data']['link_clicks'])

print 'total link_clicks:', sum(nums)

rawDescription: Adding all the integers returned from a for loop?


code:
from datetime import datetime
datestring = "2008-12-12 19:21:10"
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
print dt.year, dt.month, dt.day

rawDescription: in Python, How can I convert a string into a date object and get year, month and day separately?

description: In Python, How can I convert a string into a date object and get year, month and day separately


code:
with open('1.txt') as f1, open('2.txt') as f2, open('3.txt') as f3:
    for line in zip(f1,f2,f3):
        print ' '.join(e.strip() for e in line)

rawDescription: Python - read - rewrite on a text file (Driving me crazy)


code:
import sys

print sys.argv[1] # first parameter
print sys.argv[2] # second parameter
print sys.argv[3] # third parameter

rawDescription: How to pass multiple variable from php to python script

description: Pass multiple variable from php to python script


code:
import cmd
import urlparse
import types

# function decorator to add parse_qs to individual functions
def parse_qs_f(f):
    def f2(self, arg):
        return f(self, urlparse.parse_qs(arg))
    return f2

# class decorator to iterate over all attributes of a class and apply
# the parse_qs_f decorator to all do_* methods
def parse_qs(cls):
    for attr_name in dir(cls):
        attr = getattr(cls, attr_name)
        if attr_name.startswith('do_') and type(attr) == types.MethodType:
            setattr(cls, attr_name, parse_qs_f(attr))
    return cls

@parse_qs
class myCmd(cmd.Cmd):
    def do_foo(self, args):
        print args

my_cmd = myCmd()
my_cmd.cmdloop()

rawDescription: Python cmd module - parsing values from line


code:
import re
import collections

def cleandup(fh):
    for line in fh:
        for word in re.split('[^a-z]+', line.lower()):
            yield word

with open ('DQ.txt') as doc:
    wordlist = collections.Counter(cleanedup(doc))
    print wordlist.most_commond(5)

rawDescription: Python program: foreign language word-frequency dictionary


code:
class UserRegistration(object):
    username_and_password = {}
    def __init__(self):
        self.username = ""
    def register_name(self):
        self.username = raw_input("Choose Username> ")
        self.register_pass()

    def register_pass(self):
        password = getpass.getpass("Choose Password> ")
        confirm_password = getpass.getpass("Confirm Password> ")
        if password == confirm_password:
            self.username_and_password[self.username] = password
        else:
            print "Passwords didn't match"
            self.register_pass()

rawDescription: if True: update, if False: restart

description: If True: update, if False: restart


code:
if(sections == 'EQUAL'):
    print "Equal"
    for name in config.items('EQUAL'):
        s = str(name[0]) + ':' + str(name[1])
        print s
        for each_file in list_of_files:
            if each_file.endswith('_abc.TXT'):
                f = open(each_file)
                for line in f:
                    if s in line: #checks for the presence of a substring in a string
                        print line

rawDescription: How to read the string from an .INI file and then read that string from a text file and print the entire line using Python?

description: Read the string from an .INI file and then read that string from a text file and print the entire line using Python


code:
import regex
print(regex.findall(r"[^[:punct:] ]+|[[:punct:]]", 'help؛'))
print(regex.findall(ur"[^[:punct:] ]+|[[:punct:]]", u'help؛'))

rawDescription: How to split string by space and treat special characters as a separate word in Python?

description: Split string by space and treat special characters as a separate word in Python


code:
def sem1Sort(semester1, selectionSEM1):
    print '\n'.join([cls for cls in semester1['1'] if cls in selectionSEM1])

rawDescription: Indexing in Python

description: Indexing in Python


code:
for path, dirs, files in os.walk(root_dir, topdown=False):
        if path.split('/')[-1] == 'bin':
            print path

rawDescription: Picking a particular directory path using os path in Python


code:
import mechanize
import re

b = mechanize.Browser()

# Visit search page
url = 'http://www.ebi.ac.uk/intact'
resp = b.open(url)
page = resp.read()

# Fill out and submit form
b.select_form(name="intactForm")
b["queryTxt"] = 'Q9SUE8'
resp = b.submit()
page = resp.read()

# Parse Page
matches = re.findall('
(.*?)', page)

linksB = matches[0]
print linksB

rawDescription: How to get value from a html table?

description: Get value from a html table


code:
import inspect, os
print inspect.getfile(inspect.currentframe()) # script filename (usually with path)
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory

rawDescription: How do I get the path and name of the file that is currently executing?

description: Get the path and name of the file that is currently executing


code:
import re

def number_suffix(s):
    """Return the number from the end of the string. """
    match = re.search(r"\d+$", s)
    if match:
        num = int(match.group(0))
    else:
        num = 0
    return num

print number_suffix("AS1001")    # 1001
print number_suffix("AS1")       # 1
print number_suffix("AS")        # 0

rawDescription: Auto increment of Concatenated alpha-numeric. (Python)


rawDescription: Walk through each column in a numpy matrix efficiently in Python


code:
PER_KM_PRICE = 100

class Pack(object):
    def __init__(self, name, weight):
        self.name = name
        self.weight = weight

class CommercialPack(Pack):
    def __init__(self, pack):
        self.__dict__ = pack.__dict__

    def get_delivery_price(self, distance):
        return self.weight * PER_KM_PRICE * distance

def get_pack():
    return Pack("pack", 20)

cp = CommercialPack(get_pack())
print cp.get_delivery_price(3)

rawDescription: Object type casting in Python (design suggestion)


code:
import collections

descriptions = {
    'player_joined_clan' : '%(player)s joined clan %(clan)s',
    'player_left' : '%(player)s left',
    'player_hit_player' : '%(player)s (of %(clan)s) hit %(player2)s (of %(clan2)s)',
    }

data = [
    {'player': 'PlayerA'},
    {'player': 'PlayerB', 'clan' : 'ClanB'},
    {'clan' : 'ClanC'},
    {'clan' : 'ClanDA', 'player2': 'PlayerDB'},
    ]

for item in data:
    print item
    item = collections.defaultdict(lambda : '"<unknown>"', **item)
    for key in descriptions:
        print '  %s: %s' % (key, descriptions[key] % item)
    print

rawDescription: Python "switch statement" and string formatting


code:
while zombie1.hp > 0:
    user_input = raw_input("What will you do?: ")
    acted = False
    if weapon == "knife" and user_input == 'stab':
        attack(zombie1)
        acted = True
    if weapon == "9mm" and user_input == 'shoot':
        attack1(zombie1)
        acted = True
    if weapon == "shovel" and user_input == "smack":
        attack2(zombie2)
        acted = True
    if weapon == "axe" and user_input == "chop":
        attack3(zombie3)
        acted = True
    if weapon == "pickaxe" and user_input == "mine":
        attack4(zombie4)
        acted = True
    if weapon == "lighter" and user_input == "burn":
        attack5(zombie5)
        acted = True
    if not acted:
        print "What, are you NUTS? That won't do anything."

rawDescription: How do you call something in Python?

description: Call something in Python


code:
import sympy
from sympy.abc import x
import numpy

A = sympy.Matrix([[x,0],[0,x]])
B = numpy.eye(2)
print A + B # Prints "Matrix([[x + 1.0, 0], [0, x + 1.0]])"

rawDescription: Summing several equal size matrices by elements


code:
import re

ss =  '''Princess Maria Amelia of Brazil (1831–1853)

was the daughter of Dom Pedro I,
founder of Brazil's independence and its first emperor,

and Amelie of Leuchtenberg.

The only child from her father's second marriage,
Maria Amelia was born in France
following Pedro I's 1831 abdication in favor of his son Dom Pedro II.

Before Maria Amelia was a month old, Pedro I left for Portugal
to restore its crown to his eldest daughter Dona Maria II.
He defeated his brother Miguel I (who had usurped Maria II's throne),
only to die a few months later of tuberculosis.

'''

def select_lines(input,regx = re.compile('((?:^.+\n)+)',re.MULTILINE)):
    return [x.splitlines() for x in regx.findall(input)]

for sl in  select_lines(ss):
    print sl
    print

rawDescription: Split lines into paragraphs

description: Split lines into paragraphs


code:
import re
samples = ('0032487365060', '32487365060', '+32487365060', '0487365060')
pattern = re.compile(r'^(?:\+|00)?(\d+)$')
for sample in samples:
    match = pattern.search(sample)
    if match is not None:
        print 'matched:', match.group(1)
    else:
        print 'Sample {} did not match'.format(sample)

rawDescription: Python - how to parse this with regex correctly? its parsing all the E.164 but except the local format


code:
def myfunc(*iterables):
    print iterables

myfunc('abc', 'abc', 'abc')   # 3 Arguments
# ('abc', 'abc', 'abc')
myfunc(['abc'] * 3)           # 1 Argument with 3 items in it
# (['abc', 'abc', 'abc'],)
myfunc(*['abc'] * 3)          # Unpack the 3 element list, to pass 3 arguments
# ('abc', 'abc', 'abc')

rawDescription: Inputing an identical string multiple times to a function


code:
import multiprocessing

def processWorker(input, result):
    work = input.get()
    print work
    result.put(work*work)

input  = multiprocessing.Queue()
result = multiprocessing.Queue()

p = multiprocessing.Process(target = processWorker, args = (input, result))
p.start()

input.put(2)
res = result.get(block = True)
print res

rawDescription: Calling os.fsync on the stdout/stderr file descriptors kills a subprocess


code:
from ctypes import *

PyObject_HEAD = [
    ('ob_refcnt', c_size_t),
    ('ob_type', c_void_p),
]

class XRangeType(Structure):
    _fields_ = PyObject_HEAD + [
        ('start', c_long),
        ('step', c_long),
        ('len', c_long),
    ]

range_obj = xrange(1, 11, 2)

c_range_obj = cast(c_void_p(id(range_obj)), POINTER(XRangeType)).contents
print c_range_obj.start, c_range_obj.step, c_range_obj.len

rawDescription: Accessing xrange internal structure

description: Accessing xrange internal structure


rawDescription: Flask-Admin: route to models view with filter applied


code:
soup = BeautifulSoup(data)
for inventory in soup.select('div.inventory'):
    number = inventory.find('span', class_='item-number').text
    link = inventory.find('span', class_='cost').a.get('href')
    print number, link

rawDescription: How to print the text inside of a child tag and the href of a grandchild element with a single BeautifulSoup Object?

description: Print the text inside of a child tag and the href of a grandchild element with a single BeautifulSoup Object


code:
def transaction(f):
    def pre():
        print "pre transaction"
    def post():
        print "post transaction"

    def wrapped(*args):
        pre()
        f(*args)
        post()

    return wrapped

class Foo(object):
    def __init__(self):
        print "instantiating"

    def doFoo(self):
        print "doing foo"

    @transaction
    def doBar(self, value):
        print "doing bar "+str(value)

@transaction
def foofunc():
    print "hello"

foofunc()

f=Foo()
f.doFoo()
f.doBar(5)

rawDescription: Is there a __enter__ and __exit__ equivalent for all functions in a class?


code:
#!/usr/bin/python

"""
This script would scrape and download files using the anchor links.
"""

#Imports

import os, re, sys
import urllib, urllib2

#Config
base_url = "http://www.google.com/"
destination_directory = "downloads"

def _usage():
    """
    This method simply prints out the Usage information.
    """

    print "USAGE: %s <url>" %sys.argv[0]

def _create_url_list(url):
    """
    This method would create a list of downloads, using the anchor links
    found on the URL passed.
    """

    raw_data = urllib2.urlopen(url).read()
    raw_list = re.findall('<a style="display:inline; position:relative;" href="(.+?)"', raw_data)
    url_list = [base_url + x for x in raw_list]
    return url_list

def _get_file_name(url):
    """
    This method will return the filename extracted from a passed URL
    """

    parts = url.split('/')
    return parts[len(parts) - 1]

def _download_file(url, filename):
    """
    Given a URL and a filename, this method will save a file locally to the»
    destination_directory path.
    """
    if not os.path.exists(destination_directory):
        print 'Directory [%s] does not exist, Creating directory...' % destination_directory
        os.makedirs(destination_directory)
    try:
        urllib.urlretrieve(url, os.path.join(destination_directory, filename))
        print 'Downloading File [%s]' % (filename)
    except:
        print 'Error Downloading File [%s]' % (filename)

def _download_all(main_url):
    """
    Given a URL list, this method will download each file in the destination
    directory.
    """

    url_list = _create_url_list(main_url)
    for url in url_list:
        _download_file(url, _get_file_name(url))

def main(argv):
    """
    This is the script's launcher method.
    """

    if len(argv) != 1:
        _usage()
        sys.exit(1)
    _download_all(sys.argv[1])
    print 'Finished Downloading.'

if __name__ == '__main__':
    main(sys.argv[1:])

rawDescription: downloading files from Filetype fields?

description: Downloading files from Filetype fields


code:
for word, count in c.most_common():
    print word,count

rawDescription: A program that opens a text file, counts the number of words and reports the top N words ordered by the number of times they appear in the file?


code:
import re
from random import randint

text = 'this is some example text'
for i in xrange(5):
    print re.sub(' ', lambda m: m.group() * randint(1, 3), text)

rawDescription: Iterate over a string in Python and add some new characters


code:
mystr = "Higher id: 38"
mylist = mystr.split(' ')
mylist[-1] = "55" # t[-1] accesses the last element in the list
print ' '.join(mylist)

rawDescription: Replace string element with split method


code:
def cipher_attack(partial_alphabetic,ciphertext):
    var = ""
    for i in xrange(len(ciphertext)):
        if ciphertext[i] in partial_alphabetic:
            var += alphabet[partial_alphabetic.index(ciphertext[i]) % 26]
        else:
            var += "!"
    print var

cipher_attack("!wertyuiopasdfghjklzxcvbnm","rqr")

rawDescription: printing "!" in the missing letter in python

description: Printing "!" in the missing letter in python


code:
from file1 import x
print x

rawDescription: Include / extend modules in python

description: Include / extend modules in python


code:
def ingest_row(self, row):
    try:
        self.cur.execute(query, row)
        self.mydb.commit()
    except:
        print query
        quit()

rawDescription: Python/MySQLdb import from CSV with data encapsulation


code:
from BeautifulSoup import BeautifulSoup
print BeautifulSoup(html).find("h1").text

rawDescription: Python regex match to parse html

description: Python regex match to parse html


code:
for ch in number_string:
    print ch # will print each digit in order

rawDescription: Split an integer into digits to compute an ISBN checksum


code:
import inspect
print inspect.getsource(numpy.column_stack)

rawDescription: Is zip() the most efficient way to combine arrays with respect to memory in numpy?


code:
from distutils.version import StrictVersion

print StrictVersion('1.2.2') > StrictVersion('1.2.1')

rawDescription: How to raise an exception on the version number of a module

description: Raise an exception on the version number of a module


code:
print "path count"
for path, count in first.iteritems():
    print "%s %d" % (path, count)

rawDescription: How to approach IIS log parsing in a Pythonic way?

description: Approach IIS log parsing in a Pythonic way


code:
import ast
class Visit(ast.NodeVisitor):
    def visit_ClassDef(self, node):
        print [n.id for n in node.bases]

text = open('test.py').read()
tree = ast.parse(text)
Visit().visit(tree)

rawDescription: How to get an inheritance tree from AST?

description: Get an inheritance tree from AST


code:
class Dev(threading.Thread):

    def __init__(self, workQueue, queueLock, count):
        super(Dev, self).__init__()
        self.workQueue = workQueue
        self.queueLock= queueLock
        self.count = count

    def set_count(self, value):
        self.count = value

    def run(self):
        data = ''
        while 1:
            with self.queueLock:
                if not self.workQueue.empty():
                    data = self.workQueue.get()
                    print data
                    print self.count

            if data == 'quit':
                break

rawDescription: Threading and information passing -- how to


code:
for i1 in cursor1.fetchall():
    print 'hello'
    for i in cursor.fetchall():
        print 'hello'

        if i in i1:
             print i
        else:
             print i1

rawDescription: python retrieve all data from mysql table after if statement

description: Python retrieve all data from mysql table after if statement


code:
a = 1
b = 0
n = 1
while len(str(a)) != 1000:
    a, b = a+b, a
    n = n + 1
print "%d has 1000 digits, n = %d" % (a, n)

rawDescription: Non Brute Force Solution to Project Euler 25


code:
x = 0
def f():
    x = 1
f()
print x #=>0

rawDescription: Defining lists as global variables in Python


code:
t1=timeit.Timer("n = math.sqrt(numpy.dot(A,A.conj()))","from __main__ import A,math,numpy")
t2=timeit.Timer("n = math.sqrt(sum(abs(A)**2))","from __main__ import A,math")
t3=timeit.Timer("numpy.linalg.norm(A)","from __main__ import A,numpy")
print "%.2f usec/pass" % (1000000 * t1.timeit(number=100000)/100000)
print "%.2f usec/pass" % (1000000 * t2.timeit(number=100000)/100000)
print "%.2f usec/pass" % (1000000 * t3.timeit(number=100000)/100000)

rawDescription: multiplying two nparray together python

description: Multiplying two nparray together python


code:
df=pd.DataFrame(li, columns=['Phone'])
pat=re.compile(r'^\d{3}-\d{3}-\d{4}$')
print df
print df[df['Phone'].apply(lambda s: bool(pat.search(s)))]

rawDescription: Removing cells/rows that don't contain regex match with Python


code:
import json

# shamelessly ignoring PEP8 for the sake of space
data = '''
[{"files": [{"files": [{"files": [{"node_type": "file", "last_accessed": "0000-00-00 00:00:00", "last_updated": "2014-08-14 08:51:42",
"name": "messages.po", "created": "2014-08-14 08:51:41"}], "node_type": "directory", "name": "letters"}], "node_type": "directory",
"name": "source1"}, {"files": [], "node_type": "directory", "name": "source2"}], "node_type": "directory", "name": "main"}, {"files":
[{"node_type": "file", "last_accessed": "0000-00-00 00:00:00", "last_updated": "2014-08-14 08:11:53", "name": "prefs.js", "created":
"2014-08-14 08:11:53"}], "node_type": "directory", "name": "New Directory"}, {"files": [{"files": [{"files": [{"node_type": "file",
"last_accessed": "0000-00-00 00:00:00", "last_updated": "2014-08-14 08:51:30", "name": "cli.mo", "created": "2014-08-14 08:51:30"}],
"node_type": "directory", "name": "333"}], "node_type": "directory", "name": "222"}], "node_type": "directory", "name": "111"}]
'''

for item in parse(json.loads(data)):
    print item

rawDescription: Python. How to get directory path from JSON?


code:
def line():
    print 'Line selected!'

def square():
    print 'Square selected'

def triangle():
    print 'Triangle selected'

globals()[shape.strip().lower()]()

rawDescription: Using If-elif-else statements with Python


code:
mylist = []

with open('test.csv') as f:
    keys = f.readline()
    keys = keys.split()

    for line in f:
        a = line.split()
        mylist.append(a[1:])

for idx1, item in enumerate(mylist):
    for idx2, number in enumerate(item):
        if float(number) > 0.2:
            print "%7s, %7s  --> %7s" %(keys[idx1], keys[idx2], number)

rawDescription: Extracting pair of values from matrix using python


code:
i = { "a" : { "x": 1, "y": 2 },
      "b" : { "x": 3, "z": 4 } }

d = {}
for key, value in i.iteritems():
    for ikey, ivalue in value.iteritems():
        d.setdefault(ikey,{})[key] = ivalue
print d

rawDescription: how to create a pivot nested dictionary in python

description: Create a pivot nested dictionary in python


code:
def click_link(self, event, text):
    print "you clicked '%s'" % event.widget["text"]

rawDescription: return label text when clicked

description: Return label text when clicked


code:
def varibleChanges(circle, square):
    #skip to the end..
    return circle, square

while loopcounter <=2:
    print 'this is the begining of the loop'
    circle, square = varibleChanges(circle, square)
    loopcounter +=1
    print "this is the end of the loop\n"

rawDescription: save variables on a while loop

description: Save variables on a while loop


code:
from itertools import chain

replacements = {
    'replace_this': [2, 3],
    4: [7, 8, 9]
}

a = [ 1, 'replace_this', 4 ]
print list(chain.from_iterable(replacements.get(el, [el]) for el in a))
# [1, 2, 3, 7, 8, 9]

rawDescription: Replace element with a list in python

description: Replace element with a list in python


code:
p = []
for n in range(3):
    p.append(Poly(a[n+1][0:3]))
print p

rawDescription: assign values executed to array in python

description: Assign values executed to array in python


code:
import hashlib
salt = "-WOu@p.Za,>W+6&`A63/"
user_password = "password"
sha1 = hashlib.sha1()
sha1.update(user_password + salt)
encrypted = sha1.hexdigest()
print encrypted
# 476dc4076d1c7eb43152b78e9dc20d892f660f24

rawDescription: How to create a user login section in Python with Tkinter module

description: Create a user login section in Python with Tkinter module


code:
import csv
import re
with open('input.txt','r') as f:
    reader = csv.reader(f)
    thelist = list(reader)
print [re.split('\s+', line[0])[1] for line in thelist][1:] # the CHILDREN_NAME column

rawDescription: Python - read table with empty fields from file


code:
import theano.tensor as T
import time
import numpy as np
class neuralNet(object):
    def __init__(self, examples, num_features, num_classes):
        self.w = shared(np.random.random((16384, 5000)).astype(T.config.floatX), borrow = True, name = 'w')
        self.w2 = shared(np.random.random((5000, 3000)).astype(T.config.floatX), borrow = True, name = 'w2')
        self.w3 = shared(np.random.random((3000, 512)).astype(T.config.floatX), borrow = True, name = 'w3')
        self.w4 = shared(np.random.random((512, 40)).astype(T.config.floatX), borrow = True, name = 'w4')
        self.b = shared(np.ones(5000, dtype=T.config.floatX), borrow = True, name = 'b')
        self.b2 = shared(np.ones(3000, dtype=T.config.floatX), borrow = True, name = 'b2')
        self.b3 = shared(np.ones(512, dtype=T.config.floatX), borrow = True, name = 'b3')
        self.b4 = shared(np.ones(40, dtype=T.config.floatX), borrow = True, name = 'b4')
        self.x = examples

        L1 = T.nnet.sigmoid(T.dot(self.x, self.w) + self.b)
        L2 = T.nnet.sigmoid(T.dot(L1, self.w2) + self.b2)
        L3 = T.nnet.sigmoid(T.dot(L2, self.w3) + self.b3)
        L4 = T.dot(L3, self.w4) + self.b4
        self.forwardProp = T.nnet.softmax(L4)
        self.predict = T.argmax(self.forwardProp, axis = 1)

    def loss(self, y):
        return -T.mean(T.log(self.forwardProp)[T.arange(y.shape[0]), y])

x = T.matrix('x')
y = T.ivector('y')

nnet = neuralNet(x)
loss = nnet.loss(y)

diffrentiationTime = []
for i in range(100):
    t1 = time.time()
    gw, gw2, gw3, gw4, gb, gb2, gb3, gb4 = T.grad(loss, [nnet.w, nnet.w2, logReg.w3, nnet.w4, nnet.b, nnet.b2, nnet.b3, nnet.b4])
    diffrentiationTime.append(time.time() - t1)
print 'Efficient Method: Took %f seconds with std %f' % (np.mean(diffrentiationTime), np.std(diffrentiationTime))

diffrentiationTime = []
for i in range(100):
    t1 = time.time()
    gw = T.grad(loss, [nnet.w])
    gw2 = T.grad(loss, [nnet.w2])
    gw3 = T.grad(loss, [nnet.w3])
    gw4 = T.grad(loss, [nnet.w4])
    gb = T.grad(loss, [nnet.b])
    gb2 = T.grad(loss, [nnet.b2])
    gb3 = T.grad(loss, [nnet.b3])
    gb4 = T.grad(loss, [nnet.b4])
    diffrentiationTime.append(time.time() - t1)
print 'Inefficient Method: Took %f seconds with std %f' % (np.mean(diffrentiationTime), np.std(diffrentiationTime))

rawDescription: How efficient/intelligent is Theano in computing gradients?


code:
@MyDecorator("some other func!")
def some_other_function():
    print "in some other function!"

rawDescription: Python Class Based Decorator with parameters that can decorate a method or a function


code:
import numpy as np
a = np.arange(10)
print a
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.arange(10)
print b
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print np.allclose(a, b)
#True
b[1] = 10
#array([ 0, 10,  2,  3,  4,  5,  6,  7,  8,  9])
print  np.allclose(a, b)
#False

rawDescription: How to compare list of arrays in python?

description: Compare list of arrays in python


code:
if (1 == 1   # cond 1
and 2 == 2   # cond 2
and 3 == 3): # cond 3
    print True

rawDescription: Commenting with line continuation

description: Commenting with line continuation


code:
s="WE THE PEOPLE OF THE UNITED STATES, IN ORDER TO FORM A MORE PERFECT UNION, ESTABLISH JUSTICE, INSURE DOMESTIC TRANQUILITY, PROVIDE FOR THE COMMON DEFENSE, PROMOTE THE GENERAL WELFARE, AND SECURE THE BLESSINGS OF LIBERTY TO OURSELVES AND OUR POSTERITY, DO ORDAIN AND ESTABLISH THIS CONSTITUTION FOR THE UNITED STATES OF AMERICA."

import textwrap
print "\n".join(textwrap.wrap(s, 80))

rawDescription: What is the Python code to split up a string so that it prints out normally in an 80-character window without wrapping?


code:
def merger((l,r,v,o)):
    return l+r

for m in mergelist:
         print merger(m)

rawDescription: How to use a map with *args to unpack a tuple in a python function call

description: Use a map with *args to unpack a tuple in a python function call


code:
for h in range(max(numbers), 0, -1):
    print ' '.join('*' if x >= h else ' ' for x in numbers)

rawDescription: Can't figure out how to print * & whitespace vertically instead of hoirzontally


code:
class TreeBuilder(object):
    _PATTERN = re.compile('^[-]+')

    def __init__(self, source):
        self.tree = {}
        self.source = source
        self._recurse_tree()

    def _recurse_tree(self, parent=None, depth=0):
         last_line = self.source.readline().rstrip()
         while last_line:
             if last_line.startswith('-'):
                 tabs = len( self._PATTERN.match(last_line).group() )
             else:
                 tabs = 0
             if tabs < depth:
                 break
             node = self._PATTERN.sub('', last_line.strip())
             if tabs >= depth:
                 if parent is not None:
                     print "%s: %s" %(parent, node)
                     if parent in self.tree:
                         self.tree[parent].append(node)
                     else:
                         self.tree[parent] = [ node, ]
                 last_line = self._recurse_tree(node, tabs+1)
         return last_line

rawDescription: Refactoring to eliminate global variable in recursive function


code:
#!/usr/bin/python

import subprocess

command = ('./joe.sh',)
task = subprocess.Popen(command, stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
stdout, stderr = task.communicate()
print 'stdout == %r\nstderr == %r' % (stdout, stderr)

rawDescription: Subprocess.Popen behaves differently in interpreter, executable scripts


code:
from bs4 import BeautifulSoup

data = """Yesterday<person>Peter Smith</person>drove to<location>New York</location>"""

soup = BeautifulSoup(data, 'html.parser')
for item in soup:
    print item

rawDescription: How to parse sentences into tokens with either regex or toolkits

description: Parse sentences into tokens with either regex or toolkits


code:
import random

def make_hint(chars, frequency):
    hint_chars = [char if random.random() < frequency else "_" for char in chars]
    return "".join(hint_chars)

answer = "I am just an example"

print make_hint(answer, 0.3)
print make_hint(answer, 0.5)

rawDescription: string replacement of fixed positions in python

description: String replacement of fixed positions in python


rawDescription: Add a ranking ordered column to a Pandas Dataframe


code:
import subprocess, threading

class Command(object):
    def __init__(self, cmd):
        self.cmd = cmd
        self.process = None

    def run(self, timeout):
        def target():
            print 'Thread started'
            self.process = subprocess.Popen(self.cmd, shell=True)
            self.process.communicate()
            print 'Thread finished'

        thread = threading.Thread(target=target)
        thread.start()

        thread.join(timeout)
        if thread.is_alive():
            print 'Terminating process'
            self.process.terminate()
            thread.join()
        print self.process.returncode

#This will run one command for 5 seconds:
command = Command("ping www.google.com")
command.run(timeout=5)

rawDescription: Is it possible to output text from a Python script to the terminal as an executable command?


rawDescription: Building a list with a parent-child dependency


code: print green + 'Hello' + native

rawDescription: python in terminal - how to get current colour?

description: Python in terminal - how to get current colour


code:
delta = dateTo - dateFrom

for delta_day in range(0, delta.days+1): # Or use xrange in Python 2.x
    print dateFrom + datetime.timedelta(delta_day)

rawDescription: python, datetime.date: difference between two days

description: Python, datetime.date: difference between two days


code:
for i in range(5):
    i = 2 ** i
    print i

rawDescription: applying for loop such that counters are multiplied rather than being added in python

description: Applying for loop such that counters are multiplied rather than being added in python


code:
def Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD, CHOICE):
    os.system('cls')
    CHOICE()
    print(HANGMANPICS[len(MISSEDLETTERS)])
    print

    print 'Missed letters:',
    for letter in MISSEDLETTERS:
        print letter,
    print

    blanks = '_' * len(SECRETWORD)

    for i in range(len(SECRETWORD)):
        if SECRETWORD[i] in CORRECTLETTERS:
            blanks = blanks[:i] + SECRETWORD[i] + blanks[i+1:]

    for letter in blanks:
        print letter
    print

def getGuess(alreadyguessed):
    while True:
        print 'Guess a letter'
        guess = raw_input()
        guess = guess.lower()
        if len(guess) != 1:
            print 'Please enter single letters'
        elif guess in alreadyguessed:
            print 'This letter has already been guessed, please guess again'
        elif guess not in 'abcdefghijklmnopqrstuvwxyz':
            print 'You did not guess a letter, please guess a letter'
        else:
            return guess

def playAgain():
    print ('Do you wanna play again? (Yes or No)')
    return raw_input().lower().startswith('y')

print ('Welcome to Hangman! By Aaron Taylor')
print (HANGMANPICS[6])
words = Welcome()
if words == ANIMALS:
    CHOICE = Animals
elif words == CITIES:
    CHOICE = Cities
elif words == COUNTRIES:
    CHOICE = Countries
else:
    CHOICE = Random

MISSEDLETTERS = ''
CORRECTLETTERS = ''
SECRETWORD = getRandomword(words)

done = False

while True:
    Display(HANGMANPICS,MISSEDLETTERS,CORRECTLETTERS,SECRETWORD,CHOICE)
    guess = getGuess(MISSEDLETTERS + CORRECTLETTERS)

rawDescription: Hangman program

description: Hangman program


code:
class Foo(object):
    def __getitem__(self,items):
        print items

f = Foo()
f[1,2,3]
f[1:3]
f[1,1:3,2:3]

rawDescription: Numpy subclass attribute slicing

description: Numpy subclass attribute slicing


code:
for key, data in graph_table.scan(filter="SingleColumnValueFilter('cf', 'id', <, 'binary:%s', true, false)" % struct.pack(">q", 1000)):
     print key, data

rawDescription: Filtering integers with HBase + Python

description: Filtering integers with HBase + Python


code:
combos = [list(x) + [x[0]] for x in permutations(airports)]
for x in combos: print x

rawDescription: How to append the first item of list to the end of all permutations of that list?

description: Append the first item of list to the end of all permutations of that list


code:
import sys  #for argv[]
import binascii #for hexlify repr() spits out ugly mix like
        #'\xe8y\xff\' instead of  '\xe8\x79\xff' :(
from ctypes import *
from capstone import *
class SYMBOL_INFO(Structure):
    _fields_ = [
    ( 'SizeOfStruct', c_ulong ),
    ( 'TypeIndex', c_ulong ),
    ( 'Reserved', c_ulonglong * 2 ),
    ( 'Index', c_ulong ),
    ( 'Size', c_ulong ),
    ( 'ModBase', c_ulonglong ),
    ( 'Flags', c_ulong ),
    ( 'Value', c_ulonglong ),
    ( 'Address', c_ulonglong ),
    ( 'Register', c_ulong ),
    ( 'Scope', c_ulong ),
    ( 'Tag' , c_ulong ),
    ( 'NameLen', c_ulong ),
    ( 'MaxNameLen', c_ulong ),
    ( 'Name', c_char * 2001 )
]
modname = sys.argv[1]
offset  = long(sys.argv[2],16)
sympath = "xxx:\\yyyyy"  # substitute actual path
base    = windll.LoadLibrary(modname)._handle
symaddr = c_ulonglong(base + offset)
print "Module name = %s\nModule Base = %s\nSymFromAddr = %s" % \
    (modname,hex(base),hex(symaddr.value))
dbghelp = windll.dbghelp
k32 =     windll.kernel32
hproc = k32.GetCurrentProcess()
dbghelp.SymInitialize(hproc,sympath,1)
sinfo = SYMBOL_INFO()
sinfo.SizeOfStruct = sizeof(SYMBOL_INFO) - 2000
sinfo.MaxNameLen = 2000
Displacement = c_ulonglong()
dbghelp.SymFromAddr(hproc,symaddr,addressof(Displacement),addressof(sinfo))
print "Sym At Addr = %s + %s" % (sinfo.Name,str(hex(Displacement.value)))
opcodebuff     = create_string_buffer(16)
memmove(opcodebuff,symaddr.value,16)
for i in range(0,16,1):
    print binascii.hexlify(opcodebuff.raw[i]),
print
MyDisasm = Cs(CS_ARCH_X86, CS_MODE_32)
for i in MyDisasm.disasm(opcodebuff, symaddr,1):
    print "0x%x: %s %s  %s" %  ( i.address ,binascii.hexlify(i.bytes),
     i.mnemonic, i.op_str)
    if(i.mnemonic == 'call'):
        try:
            symaddr = c_ulonglong(long(i.op_str,16))
            dbghelp.SymFromAddr(hproc,symaddr,addressof(Displacement),addressof(sinfo))
            print "(%s+%s)" % (sinfo.Name,str(hex(Displacement.value))),
            print "(%s+0x%X)"% (modname ,long(i.op_str,16)-base)
        except:
            print "Indirect/register Calls Not Handled Yet"
            pass

rawDescription: Parse PDB Symbol and Resolve Address

description: Parse PDB Symbol and Resolve Address


code:
import itertools

d = [{'id':1,'key1':'value1','key2':'value2'},{'id':1,'key1':'value3','key2':'value4'}, \
        {'id':1,'key4':'value5'}, {'id':2,'key1':'value3','key2':'value4'}]

for id, group in itertools.groupby(d, lambda x : x['id']):
    key_line = 'id'
    values_line = str(id)
    for g in group:
        for key in sorted(g.keys()):
            if key == 'id':
                continue
            key_line += ',{0}'.format(key)
            values_line += ',{0}'.format(g[key])

    print key_line
    print values_line

rawDescription: Selecting Certain Keys,Values From List of Python Dictionaries to CSV


code: print matrix

rawDescription: Convert a deafultdict to numpy matrix or a CSV of 2D Matrix


code:
import re
import urllib
from bs4 import BeautifulSoup

tmp = re.compile("1-(..)")
url ='http://www.brothersoft.com/windows/photo_image/'
pageUrl = urllib.urlopen(url)
soup = BeautifulSoup(pageUrl)

for a in soup.select('div.coLeft.cate.mBottom dd a[href]'):
        suburl = 'http://www.brothersoft.com'+a['href'].encode('utf-8','repalce')
        print suburl

        content = urllib.urlopen(suburl+'2.html')
        soup1 = BeautifulSoup(content)
        pages = soup1.find("div", {"class":"freemenu coLeft Menubox"})
        page = pages.text
        allPages =  int(re.search(r'of ([\d]+) ', page).group(1)) / 20 + 1
        print allPages
        for page in range(1, allPages+1):
                content = urllib.urlopen(suburl+'{}.html'.format(page))
                soup = BeautifulSoup(content)
                for a in soup.select('div.freeText dl a[href]'):
                        print 'http://www.brothersoft.com'+a['href'].encode('utf-8','repalce')

rawDescription: How to iterate link inside unknown total page number?

description: Iterate link inside unknown total page number


code:
import re

nmap = """Host: 127.0.0.1 ()      Status: Up
Host: 127.0.0.1 ()      Ports: 22/open/tcp//ssh///, 80/open/tcp//http///, 443/open/tcp//https///        Ignored State: closed (65532)
Host: 127.0.0.2 ()      Status: Up
Host: 127.0.0.2 ()      Ports: 21/open/tcp//ftp///, 22/open/tcp//ssh///, 25/open/tcp//smtp///, 53/filtered/tcp//domain///, 80/open/tcp//http///, 110/open/tcp//pop3///, 143/open/tcp//imap///, 443/open/tcp//https///, 465/filtered/tcp//smtps///, 993/open/tcp//imaps///, 995/open/tcp//pop3s///, 5222/filtered/tcp//xmpp-client///    Ignored State: closed (65523)
# Nmap done at Sat Nov  7 10:40:36 2015 -- 2 IP addresses (2 hosts up) scanned in 32.07 seconds"""

entries = []

for line in nmap.split('\n'):
    re_host = re.match(r'Host\: ([0-9.]+?)\s+', line)
    if re_host:
        host = re_host.group(1)
        ports = re.findall('(\d+)\/open', line)

        if len(ports):
            entries.append((host,  ports))

for host, ports in entries:
    print '{:16} {}'.format(host, ports)

rawDescription: Python Parse file with Regex

description: Python Parse file with Regex


code:
import numpy
import time
x = (1,2)
y = (1,2)
plt.ion()
plt.plot(x,y, 'r-')
plt.draw()

for i in range(100):
   print i
   time.sleep(1)
   plt.cla()
   y = (numpy.random.normal(), numpy.random.normal())
   plt.plot(x,y, 'r-')
   plt.draw()

rawDescription: Matplotlib updating axis

description: Matplotlib updating axis


code:
import re
from HTMLParser import HTMLParser
from htmlentitydefs import entitydefs

class MLStripper(HTMLParser):
    def __init__(self):
        self.reset()
        self.fed = []
        self.entityref = re.compile('&[a-zA-Z][-.a-zA-Z0-9]*[^a-zA-Z0-9]')

    def handle_data(self, d):
        self.fed.append(d)

    def handle_starttag(self, tag, attrs):
        self.fed.append(' ')

    def handle_endtag(self, tag):
        self.fed.append(' ')

    def handle_entityref(self, name):
        if entitydefs.get(name) is None:
            m = self.entityref.match(self.rawdata.splitlines()[self.lineno-1][self.offset:])
            entity = m.group()
            # semicolon is consumed, other chars are not.
            if entity[-1] != ';':
                entity = entity[:-1]
            self.fed.append(entity)
        else:
            self.fed.append(' ')

    def get_data(self):
        self.close()    # N.B. ensure all buffered data has been processed
        return ''.join(self.fed)

def strip_tags(html):
    s = MLStripper()
    s.feed(html)
    return s.get_data()

print strip_tags('html &zzz; tags<p>&zzz &zz: will be&replaced</p>with space. NOT this &abc')

rawDescription: python remove html tags including html entities but not normal text with a '&' prefix

description: Python remove html tags including html entities but not normal text with a '&' prefix


code:
for dates in movie.xpath('.//div[@class="showstimes"]/div[@class="date"]'):
    date = dates.xpath('normalize-space()')
    for times in dates.xpath('following-sibling::span[preceding-sibling::div[1]=$current]', current=dates):
        time = times.xpath('a/text()')[0]
        url = times.xpath('a/@href')[0]
        format_type = times.xpath('span/text()')[0]
        print date, time, url, format_type

rawDescription: No nested nodes. How to get one piece of information and then to get additional info respectively?


code:
soup = BeautifulSoup(txt)
title= soup.findAll('h1', { "class" : "title" })[0].string.encode('utf-8')
print repr(title)

rawDescription: Encoding issue when scraping chinese text into csv with python


code:
def f():
   """Indented docstrings look good.

   If you'll peek at f.__doc__, you'll see leading whitespace here.
   But we don't care because pydoc, doctest and other docstring-parsing
   tools are smart enough to strip it.
   """
   if X:
       print """\
Here I'm writing flush-left to avoid leading whitespace.
But this breaks the visual flow in a horrible way!
"""
   print "where am I now? how did I get here?"
   print textwrap.dedent("""\
       This is much better!  I can have the cake and eat it.
       Unlike docstrings, don't start the first line after quotes.
       """)

rawDescription: Usage of """ instead of " on variables with long text inside


code:
from bs4 import BeautifulSoup
soup = BeautifulSoup('''\

My name is james bond

007

''')
soup.body
for tag in soup.body or soup:  # prefer the body tag if exists
    print tag

rawDescription: Beautifulsoup showing duplicates

description: Beautifulsoup showing duplicates


code:
import pandas as pd
import io
from Crypto import Random
from Crypto.Cipher import AES

def pad(s):
    n = AES.block_size - len(s) % AES.block_size
    return s + n * chr(n)

def unpad(s):
    return s[:-ord(s[-1])]

def encrypt(message, key, key_size=256):
    message = pad(message)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    return iv + cipher.encrypt(message)

def decrypt(ciphertext, key):
    iv = ciphertext[:AES.block_size]
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext[AES.block_size:])
    return unpad(plaintext)

def encrypt_file(file_name, key):
    with open(file_name, 'rb') as fo:
        plaintext = fo.read()
    enc = encrypt(plaintext, key)
    with open(file_name + ".enc", 'wb') as fo:
        fo.write(enc)

def decrypt_stream(file_name, key):
    with open(file_name, 'rb') as fo:
        ciphertext = fo.read()
    dec = decrypt(ciphertext, key)
    cipherbyte = io.BytesIO()
    cipherbyte.write(dec)
    cipherbyte.seek(0)
    return cipherbyte

key = 'this is an example key'[:16]

df = pd.DataFrame({
    'x': [1,2],
    'y': [3,4]
})

df.to_stata('test.dta')

encrypt_file('test.dta', key)

print pd.read_stata(decrypt_stream('test.dta.enc', key))

rawDescription: Decrypting a file to a stream and reading the stream into pandas (hdf or stata)


code:
rng = pd.date_range('1/1/2011 01:00:00', periods=48, freq='H')
df = pd.DataFrame(np.random.randn(48, 1), index=rng, columns=['A'])

grouped = df.groupby(lambda x: x.day)
data = [group['A'].values.tolist() for day, group in grouped]
print json.dumps(data, indent=4)

rawDescription: Pandas Dataframe output to JSON

description: Pandas Dataframe output to JSON


code:
import itertools

x = '''[This] is some text with [some [blocks that are nested [in a [variety]
of ways]]] and some [which are not], and [any [with nesting] must go] away.'''

def nonest(txt):
  pieces = []
  d = 0
  level = []
  for c in txt:
    if c == '[': d += 1
    level.append(d)
    if c == ']': d -= 1
  for k, g in itertools.groupby(zip(txt, level), lambda x: x[1]>0):
    block = list(g)
    if max(d for c, d in block) > 1: continue
    pieces.append(''.join(c for c, d in block))
  print ''.join(pieces)

nonest(x)

rawDescription: Remove all nested blocks, whilst leaving non-nested blocks alone via python


code:
f = open("mustang.txt", "r")
count = f.read().count('.')
f.close()
print count

rawDescription: Count how many full stops a text file contains in Python


code:
data = """
ID: ID/123456-00000003c
Value2: 1382386949.12
Value3: 00420903237127
Value4: 21

ID: ID/654431-0000000a
Value2: 1382386943.1032
Value3: 004989353474713
Value4: 33"""

myData = {}
for line in data.split("\n"):
    if line:
        key, value = line.split(": ")
        myData.setdefault(key, [])
        myData[key].append(value)

def get_values_of(actualKey):
    return myData[actualKey]

print get_values_of("ID")
print get_values_of("Value2")
print get_values_of("Value3")
print get_values_of("Value4")

rawDescription: Multiline string parsing

description: Multiline string parsing


code:
for c in theString:
    if c == '"':
        print 'Aha!'

rawDescription: Check if a character equals quotes in python


code:
userchoice = raw_input('What do you want to calculate?\nmass, acceleration or force')

def calculator():
    if userchoice == 'mass':
        acceleration = int(raw_input('What will be the acceleration?'))
        force = int(raw_input('And what it will be the force?'))
        return float(force) / float(acceleration)

    elif userchoice == 'force':
        acceleration = int(raw_input('What will be the acceleration?'))
        mass = int(raw_input('And what it will be the mass?'))
        return float(mass) * float(acceleration)

    elif userchoice == 'acceleration':
        force = int(raw_input('And what it will be the force?'))
        mass = int(raw_input('And what it will be the mass?'))
        return float(force)/float(mass)

    else:
        return "You didn't choose any of the available options"

print calculator()

rawDescription: simple calculator function return 0 instead of the operation (e.g. mass * acceleration)

description: Simple calculator function return 0 instead of the operation (e.g. mass * acceleration)


code:
from Tkinter import *

class GraphicsInterface:

    def __init__(self):
        self.window = Tk()
        self.window.geometry("720x500")
        self.clicked=[]
        self.buttons = []

        for n in range(1, 3):
            btn = Button(text="Button " + str(n))
            btn.configure(command=lambda btn=btn, n=n: self.chooseDice(btn, n))
            btn.pack()
            self.buttons.append(btn)

        btn = Button(text="Go!", command=self.go)
        btn.pack()
        self.window.mainloop()

    def go(self):
        print "buttons:", self.clicked
        self.reset()

    def reset(self):
        '''Reset all the buttons'''
        self.clicked = []
        for button in self.buttons:
            button.configure(state="normal")

    def chooseDice(self, widget, number):
        self.clicked.append(number)
        widget.configure(state="disabled")

app = GraphicsInterface()

rawDescription: accessing the return value of function that was bound to an event (python/tkinter)

description: Accessing the return value of function that was bound to an event (python/tkinter)


code:
def datameans(df, avggrouper1, avggrouper2, startdate, enddate):
    df_hour_means = df[startdate:enddate]
    df_hour_means = df_hour_means.groupby(
        lambda x: (getattr(x,avggrouper1), getattr(x,avggrouper2))).mean()
    print df_hour_means.to_string()
    df_hour_means.plot()

rawDescription: Python Pandas groupby function with dynamic parameters for monthly, hourly means


code:
if True and ("," in g):
    print "yay"
if True and True and ("," in g):
    print "nay"

rawDescription: File Inout Output multiple delimiters

description: File Inout Output multiple delimiters


code:
from collections import Counter
D = Counter()

for line in open('filename', 'r'):
    k = line.split()[-1]
    D[k] += 1

print 'VALUE    NO TIMES REPEATED'
print '-----    -----------------'
for value, count in D.items():
    print '%-6s           %d'  % (value, count)

rawDescription: count for number of repetition of values in a list and generate outfile

description: Count for number of repetition of values in a list and generate outfile


code:
a_names = {record.username for record in List_A}
b_names = {record.username for record in List_B}
for name in a_names & b_names:
    print "Duplicate username: {0}".format(name)

rawDescription: Python 2.6: How can I compare two lists of same object types on one particular field, efficiently?


code:
import numpy

M = numpy.array([[1,2,3], [4,5,6]])

def my_func(M, col, f=lambda x: x+1):
    copy = numpy.copy(M)
    copy[:, col] = f(copy[:, col])
    return copy

print my_func(M, 1)

rawDescription: What's the numpy/pythonic way to non-destructively replace a column x of a matrix with f(x)?


code:
import codecs

with codecs.open('C:/in_file.ini', encoding='latin-1') as f_in, codecs.open('C:/out_file.ini', "w", encoding='latin-1') as f_out:
    print "Leyendo... " + str(f_in.name)
    generator = (l.split('"')[1].strip() for l in f_in.readlines() if l.strip())
    for i, line in enumerate(generator):
        f_out.write('alm_siem_alarm%d=%s\n' % (i, line.upper()))

rawDescription: Reading a file and uppercasing non-ascii characters before saving the content to other file


code:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.random_geometric_graph(1000, 0.1)

# position is stored as node attribute data for random_geometric_graph
pos = nx.get_node_attributes(G, 'pos')

# find node near center (0.5,0.5)
ncenter, _ = min(pos.items(), key = lambda (node, (x, y)): (x-0.5)**2+(y-0.5)**2)

# color by path length from node near center
p = {node:length
     for node, length in nx.single_source_shortest_path_length(G, ncenter).items()
     if length < 5}

plt.figure(figsize = (8, 8))
node_color = p.values()
H = G.subgraph(p.keys())
nx.draw_networkx_edges(H, pos, alpha = 0.4)
nx.draw_networkx_nodes(H, pos, node_size = 80, node_color = node_color,
                       cmap = plt.get_cmap('Reds_r'))

plt.xlim(-0.05, 1.05)
plt.ylim(-0.05, 1.05)
plt.axis('off')
plt.savefig('random_geometric_graph.png')
plt.show()

rawDescription: Networkx Random Geometric Graph limit nodes within radius r


code:
import pandas as pd, numpy as np
pd.set_option('display.float_format',lambda x: '{:,.1f}'.format(x))
np.random.seed(1)

dates  = pd.date_range('20140101',periods=10,freq='1D')
others = np.arange(0,5)
index  = [(d,o) for o in others for d in dates]
index  = pd.MultiIndex.from_tuples(index, names=['DATES','LAT'])
data   = np.random.randint(0,20,(50,10))

df = pd.DataFrame(data=data,index=index,columns=np.arange(0,10))
df.columns.names = ['LON']

# answer
df = df.stack()
df= df.groupby(level=['LAT','LON']).mean()
print df.unstack(level=['LON'])

rawDescription: Resampling multilevel index, or averaging along third dimension of a matrix/array


code:
def makeDecoratingParseAction(marker):
    def parse_action_impl(s,l,t):
        return (marker, t[0])
    return parse_action_impl

A = Word(nums).setParseAction(makeDecoratingParseAction("A"))
B = Word(alphas).setParseAction(makeDecoratingParseAction("B"))
expr = OneOrMore(A | B)

result = expr.parseString("123 abc 456 7 d")
print result.asList()

rawDescription: Distinguish matches in pyparsing

description: Distinguish matches in pyparsing


code:
abacus = [0,0,0,0]
print abacus
m(abacus, 2)
print abacus
m(abacus, 4)
print abacus
m(abacus, 3)
print abacus
m(abacus, 3)
print abacus
m(abacus, 7)
print abacus

rawDescription: python list controlled increment

description: Python list controlled increment


code:
jsonobj = json.loads(text)
print jsonobj['data']

rawDescription: Parse a json file and add the strings to a URL


code: a = 21; print a*2

rawDescription: Semicolon alternative in python

description: Semicolon alternative in python


code:
# Foreground stuff
value = raw_input("Please enter a value: ")

import os

pid = os.fork()
if pid == 0:
    # Child
    os.setsid()  # This creates a new session
    print "In background:", os.getpid()

    # Fun stuff to run in background

else:
    # Parent
    print "In foreground:", os.getpid()
    exit()

rawDescription: Move python code to background after running a part of it


code:
import sys

# somehow modnames should be a list of strings that are the names of config files
#
# you can do this more dynamically depending on what you're doing
modnames = ['bar']

for modname in modnames:
  exec('import %s' % modname)

for modname in modnames:
  mod = sys.modules[modname]
  for k in mod.__dict__:
    if k[:2] != '__':
      print modname, k, mod.__dict__[k]

rawDescription: load python code at runtime

description: Load python code at runtime


code:
def calledfunction(var="Created within the function"):
    print var

calledfunction()  # Created within the function
calledfunction(var="Created by the caller")  # Created by the caller

rawDescription: Python: checking for the existence of a variable in globals() makes it invisible in the local context


code:
import itertools

for x in itertools.product('abc', repeat=4):
    print x

rawDescription: Generating all possible permutations and then combining every possible result?


code:
n = len(list_a)
newList = []
for i in range(0,n, 6):
    newList.append(list_a[i:i+6] )

    newTuple1 = (newList[-1][1], newList[i][0])
    newList.append(newTuple1)
    try:
        newTuple2 = (newTuple1[0] + 1, list_a[i+6][0])
        newList.append(newTuple2)
    except IndexError:
        print "There was no next tuple"

print newList

rawDescription: Insert values in lists following a pattern


code:
if place == False:
    blah = combo[:pos+1]
    listx = [x for x in listx if not x[:pos+1] == combo[:pos+1]]
    print 'Fail', combo, count
    solve(listx)

rawDescription: Recursion results in unwanted looping

description: Recursion results in unwanted looping


code:
if __name__=="__main__":
        mydict = {}
        reader = csv.reader(open("file.csv", "rb"))
        for i, rows in enumerate(reader):
            if i == 0: continue
            k = rows[0]
            v = rows[1]
            if not k in mydict:
                mydict[k] = [v]
            else:
                mydict[k].append(v)

        print mydict

rawDescription: make dictionary from csv file columns

description: Make dictionary from csv file columns


code:
import re

def multiple_replace(dict, text):
  # Create a regular expression  from the dictionary keys
  regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))

  # For each match, look-up corresponding value in dictionary
  return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)

if __name__ == "__main__":

  text = "Larry Wall is the creator of Perl"

  dict = {
    "Larry Wall" : "Guido van Rossum",
    "creator" : "Benevolent Dictator for Life",
    "Perl" : "Python",
  }

  print multiple_replace(dict, text)

rawDescription: How can I do multiple substitutions using regex in python?

description: Do multiple substitutions using regex in python


code:
import sys
import urllib2
from bs4 import BeautifulSoup

problem_url  = "https://projecteuler.net/problem={}".format(sys.argv[1])
problem_page = urllib2.urlopen(problem_url)
soup = BeautifulSoup(problem_page.read())

problem_text = soup.find("div", {"class": "problem_content"}).text
print problem_text

rawDescription: parsing html information with beautifulsoup4 in python

description: Parsing html information with beautifulsoup4 in python


code:
def my_callback():
    print 'Hello'
server = ThreadedTCPServer((HOST, PORT), lambda *args, **keys: ThreadedTCPRequestHandler(my_callback, *args, **keys))

rawDescription: Proper way to add functionality to handle() in a python TCP Server?


code:
class Tournament(models.Model):
    title = models.CharField(max_length = 100, verbose_name = u"Tournament's title")
    players = models.ManyToManyField(Player,verbose_name = u"Tournament's players")

    def __unicode__(self):
        return self.title

    def save(self, **kwargs):
        Tournament(title = self.title)
        all_players = Player.objects.all()
        for member in all_players:
            member_of_tournament = member.tournament_set.filter(title = self.title)
            for j in member_of_tournament:
                print member.tournament_set.filter(title = self.title)
                self.players.add(member)

        super(Tournament, self).save(**kwargs)

rawDescription: Save() in ManyToMany relation

description: Save() in ManyToMany relation


code:
easy_text_file = open("number.txt", "rU")

#directly iterate each line of easy_text_file, convert it to int and create list
initial = [int(line) for line in easy_text_file]

#Based your requirement : next value should be equal to previous + 1
#So we create list with starting point from first value to length of initial list
compare = range(initial[0], len(initial)+1)

#Using zip function to create a match tuple between those list
#Output of zip(initial, compare) => [(1,1),(2,2),(3,3),(5,4), ....]
#Then we compare between each values
for i, j in zip(initial, compare):
    if i!=j:
        print 'This line does not satisfy the condition at line: ', j
        break

easy_text_file.close()

rawDescription: How to check if the next element in a text file is 1 more than the previous element?

description: Check if the next element in a text file is 1 more than the previous element


code:
for items in line:
    breakup= items.split()
    number, salary, position, first, des1, des2, last = breakup
    data.append(tuple([last, first + ' ' + des1 + ' ' + des2, number, position, salary]))

print data

rawDescription: List position changing

description: List position changing


code:
found = False
for line in open('Gin.txt'):    #Will be corrected later..
    if Q.lower() in line.lower():
        found = True
        print line

if not found:
    print "Found nothing. Did you spell it correct?"

rawDescription: Python Getting mixed results.

description: Python Getting mixed results.


code:
class Parent(object):
    def __init__(self):
        print "Parent initialized"

    @classmethod
    def resource_setup(cls):
        cls.name = "P"
        print "parent method"

    def parent_method(self):
        print self.name

Parent.resource_setup()
a = Parent()
a.parent_method()
print a.name

rawDescription: Accessing class method vairable in instance method


rawDescription: Parse simple array and save it on a file


code:
print df['item_list'].apply(pd.Series).stack().drop_duplicates().tolist()
['beer', 'apple', 'pear', 'rice', 'egg', 'banana', 'oil', 'noodle', 'tomato', 'milk']

print list(set(df['item_list'].apply(pd.Series).stack().tolist()))
['tomato', 'oil', 'apple', 'pear', 'milk', 'beer', 'noodle', 'rice', 'egg', 'banana']

rawDescription: Python Pandas dataframe: Collect values of a column


code:
from Tkinter import *

class App(object):
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.optionvalue = IntVar(master)
        self.optionvalue.set(2)
        self.optionvalue.trace("w", self.callbackfunc)
        self.optionmenu = OptionMenu(master, self.optionvalue, 1, 2, 3, 4)
        self.optionmenu.pack()
        self.state = []

    def callbackfunc(self, *args):
        self.state.append(args)
        print self.state
        print self.optionvalue.get()

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

rawDescription: What are the limitations of callback functions associated with Tkinter traces?


code:
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Error: You must specify exactly one function: dothis or dothat"
        sys.exit(1)

    try:
        getattr(DoThisOrThat, sys.argv[1])()
    except AttributeError:
        print "I don't know how to " + sys.argv[1]

rawDescription: What is an elegant way to call one of several (possibly static) member functions by sending arguments to a Python script?


code:
from google.appengine.ext import db

class SuppSet(db.Model):
    name = db.StringProperty()

class Supp(db.Model):
    suppset = db.ReferenceProperty(SuppSet, collection_name='supp_list')

suppSet0, suppSet1 = SuppSet(name = '0'), SuppSet(name = '1')
suppSet0.put()
suppSet1.put()

supp = Supp(suppset=suppSet0)
supp.put()

print 'suppSet0.supp_list: %r' % list(suppSet0.supp_list)
print 'suppSet1.supp_list: %r' % list(suppSet1.supp_list)
print 'suppset for sup: %s' % supp.suppset.name

supp.suppset = suppSet1
supp.put()

print 'suppSet0.supp_list: %r' % list(suppSet0.supp_list)
print 'suppSet1.supp_list: %r' % list(suppSet1.supp_list)
print 'suppset for sup: %s' % supp.suppset.name

rawDescription: GAE python - how to change the "one" that a "many" object points to?


code:
class First(object):
    @property
    def text(self):
        "get text from self.second"
        return self.second.text

    def __init__(self):
        self.second = Second("This is a text in First class")
        print self.text

class Second:
    def __init__(self, text):
        self.text = text
        print "How to show self.text from First?"
        print self.text

rawDescription: Python: Calling a method from the importing class


code:
import re
with open("input") as f:
    for line in f:
        mo=re.match(r'[^\d]*(\d+).*?(\S*)\s*(tons|feet|lbs)', line)
        if mo: print "'{}' '{}' '{}'".format(mo.group(1), mo.group(2),
                       mo.group(3))

rawDescription: Matching Regex in Python

description: Matching Regex in Python


code:
import numpy as np

# In this case, X should be a matrix rather than vector
# and we compute w.r.t P rather than merely its element [i][j]
path = []

def path_prob2(p, psi, x2):  # X a sequence giving the path
    path.append(np.dot(psi, x2[:, 0]))  # first step
    prob = psi  # initial distribution is an row vector
    for t in range(1, x2.shape[1]):  # .shape[1] grasp # of columns
        prob = np.dot(prob, p)  # prob[t]: marginal distribution at period t
        path.append(np.prod(np.dot(prob, t)))
    return path, prob

# p must to be a 2-D numpy array
p = np.array([[0.971, 0.029, 0],
              [0.145, 0.778, 0.077],
              [0, 0.508, 0.492]])

psi = np.array([0.2, 0.2, 0.6])
x2 = np.array([[0, 0, 0],
               [1, 1, 1],
               [1, 1, 1]])

print path_prob2(p, psi, x2)

rawDescription: Two problems on writing a script to compute markov joint distribution (in python)


code:
import email.utils
from time import mktime
from datetime import datetime

example_date = "Sat, 02 Mar 2011 15:00:00"
date_parsed = email.utils.parsedate(example_date)
dt = datetime.fromtimestamp(mktime(date_parsed))

today = datetime.today()
diff_date = today - dt  # timedelta object

print "%s days, %s hours ago" \
    % (diff_date.days, diff_date.seconds / 3600)

rawDescription: How can I convert an RFC 822 timestamp into a human readable format in Python?

description: Convert an RFC 822 timestamp into a human readable format in Python


code:
import sys
def go():
  sys.stdout = None
  print "Hello!"

go()
print "Here"

rawDescription: Finding a print statement in Python

description: Finding a print statement in Python


code:
from itertools import groupby,count
a = [12,11,5,7,2,21,32,13,6,42,1,8,9,0,32,38]
l = [idx for idx,value in enumerate(a) if value<10]
print [list(g) for _,g in groupby(l,key=lambda n,c=count():n-next(c))]

rawDescription: How to get the index range of a list that the values satisfy some criterion in Python?

description: Get the index range of a list that the values satisfy some criterion in Python


code:
class CandleFlags:
      DOJI= 0x01
      HAMMER=0x02
      SOMETHING=0x04

print CandleValue & CandleFlags.DOJI # doji bit is set
print CandleValue & CandleFlags.SOMETHING #something bit is set
print CandleValue & ~CandleFlags.HAMMER # Hammer bit is not set

rawDescription: How to present a flag in python?

description: Present a flag in python


code:
from datetime import datetime, timedelta
def date():
    dates = []
    while True:
        start = raw_input("Please put start date with format yy.mm.dd or 'end': ")
        if start == "end":
            break
        end = raw_input("Please put end date with format yy.mm.dd or 'end': ")
        try:
            start =  datetime.strptime(start,"%y.%m.%d")
            end =  datetime.strptime(end,"%y.%m.%d")
            for _ in xrange((end-start).days+1):
                dates.append(start)
                start += timedelta(days=1)
        except ValueError:
            print "Use yy.mm.dd format or write 'end'"
    print 'Dates:'
    return dates

rawDescription: How to input a range of dates and turn it into a list?

description: Input a range of dates and turn it into a list


code:
for c, n in l:
    print c * n

rawDescription: Python - print "x" "y" times when passed in a list of [x,y]


code:
int(1L)
long(1)

rawDescription: convert long date to strings

description: Convert long date to strings


code:
all={
    1:{ ('a',123,145):20, ('a',155,170):12, ('b',234,345): 34},
    2:{ ('a',121,135):10, ('a',155,175):28, ('b',230,345): 16},
    3:{ ('a',130,140):20, ('a',150,170):10, ('b',234,345): 30}
}

from collections import defaultdict
summer = defaultdict(int)
mini, maxi = 0,0
for d in all.values():
    for (name, start, stop), value in d.iteritems():
        # im completely ignoring the `name` here, not sure if that's what you want
        # else just separate the data before doing this ...
        if mini == 0:
            mini = start
        mini, maxi = min(mini, start), max(maxi, stop)
        for i in range(start, stop+1):
            summer[i]+=value

# now we have the values at each point, very redundant but very fast so  far
print summer

# now we can find the intervals:
def get_intervals(points, start, stop):
    cstart = start
    for i in range(start, stop+1):
        if points[cstart] != points[i]: # did the value change ?
            yield cstart, i-1, points[cstart]
            cstart = i

    if cstart != i:
        yield cstart, i, points[cstart]

print list(get_intervals(summer, mini, maxi))

rawDescription: combining values for a large number of overlapping intervals of dictionary keys

description: Combining values for a large number of overlapping intervals of dictionary keys


code:
import csv
with open('file.csv') as f:
    csvreader = csv.reader(f)
    for row in csvreader:
        print row[0]

rawDescription: Getting the first element in string/list


code:
#
#   Hello World client in Python
#   Connects REQ socket to tcp://localhost:5555
#   Sends "Hello" to server, expects "World" back
#
import zmq

context = zmq.Context()

#  Socket to talk to server
print "Connecting to hello world server…"
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print "Sending request %s …" % request
    socket.send("Hello")

    #  Get the reply.
    message = socket.recv()
    print "Received reply %s [ %s ]" % (request, message)

rawDescription: Sending data to Python Script from PHP

description: Sending data to Python Script from PHP


code:
from xml.dom import minidom

def getChild(n,v):
    for child in n.childNodes:
        if child.localName==v:
            yield child

def getValue(n, val):
    res = None
    for n in mynode:
        rv = getChild(n,val)
        for v in rv:
            var = v.childNodes[0].nodeValue
            res = var
    if not res:
        for n in mynode:
            attr = n.getAttributeNode(val)
            if attr:
                res = attr.nodeValue.strip()

    return res

xmldoc = minidom.parse('file.xml')
mynode = xmldoc.getElementsByTagName('mynode')
print getValue(mynode,'text')
print getValue(mynode,'position')

rawDescription: minidom.parse reading short XML?

description: Minidom.parse reading short XML


code:
# befile = 'filename'
# with open(bedfile) as infile:
#    data = load(infile)

txt = """\
A, 1,4,a
A, 4,8,b
B, 3,5,a
B, 5,10,c
"""
data = load(txt.splitlines())
print data
# defaultdict(<class '__main__.Chrom'>, {
#   'A': A(a, 1:4); A(b, 4:8),
#   'B': B(a, 3:5); B(c, 5:10)})

print 3, data['A'].find(3)   # a gene
print 9, data['B'].find(9)   # c gene
print 11,data['B'].find(11)   # none

rawDescription: cython: reducing the size of a class, reduce memory use, improve speed

description: Cython: reducing the size of a class, reduce memory use, improve speed


code:
def perform(self, data):
    for key in ['true-property', 'false-property']:
        val = data['test'][key]
        newval = bool(val)

        print '%s type: %s' % (key, type(val))
        print 'is dbus.Boolean: %s' % isinstance(val, dbus.Boolean)
        print 'Python:', newval
        print '  Dbus:', dbus.Boolean(newval)
    return data

rawDescription: dbus Variant: How to preserve boolean datatype in Python?

description: Dbus Variant: How to preserve boolean datatype in Python


code:
#!/usr/bin/python
import re
import mechanize
br = mechanize.Browser()
r = br.open("http://www.htmlcodetutorial.com/forms/_INPUT_TYPE_RADIO.html")
form = mechanize.ParseResponse(r, backwards_compat=False)[2]
options = [item.name for item in form.find_control('pizzasize').items]
print options

rawDescription: How to print Radio Control information

description: Print Radio Control information


code:
import jellyfish
target = 'take'
list = ['teak','fake','bake','sake','rake','sale']
outlist = [x for x in list if jellyfish.levenshtein_distance(x,target) == 1]

print outlist
['fake', 'bake', 'sake', 'rake']

rawDescription: Letter combinations between a string in a list


code:
# raster dimensions, 10 million points
N, M = 3200, 3200
# circle center and its radius in index space
i0, j0, r = 70, 20, 12.3

raster = fromfunction(lambda i,j: 100+10*i+j, (N, M), dtype=int)
print "raster is ready"
print raster

pts_iterator = points_in_circle((i0,j0,r), raster) # very quick, do not extract points yet
pts = array(list(pts_iterator)) # actually extract all points
print pts.size, "points extracted, sum = ", sum(pts)

rawDescription: Extract points within a shape from a raster


code:
from itertools import groupby
from operator import itemgetter
sample = ['aaxbb', 'aayybb', 'aaaaaaabb', 'ccabcavabb', 'yyaaadbb', 'yyaabb', 'a']
for key, group in groupby(sample, lambda v: (v[:2], v[-2:])):
    print list(group)

for key, group in groupby(sample, itemgetter(slice(2), slice(-2, None))):
    print list(group)

rawDescription: Groupby complex function

description: Groupby complex function


code:
named_fields = et.xpath('//field[@name="count"]')

for x in named_fields:
    print x.text

rawDescription: How to extract text from Field using lxml, if field exists?

description: Extract text from Field using lxml, if field exists


code:
def update_group_remove_user(self, group_name, user):
    remove_user = self.session.query(Group).\
    filter(Group.group_name == group_name).\
    filter(Group.user == user).one()
    if remove_user:
        r = remove_user.user
        print 'test r : ', r
        g = Group() # does not work
        g.remove(r)
        self.session.commit()

rawDescription: Removing/Adding an Object from a Collection in SQLAlchemy


code:
import datetime

today = datetime.date.today()
weekday = today.weekday()
start_delta = datetime.timedelta(days=weekday)
start_of_week = today - start_delta
week_dates = [start_of_week + datetime.timedelta(days=i) for i in range(7)]
print week_dates

rawDescription: How can I find "week" in django's calendar app?

description: Find "week" in django's calendar app


code:
from nltk.tree import Tree
bracket_parse = "(S (VP (VB get) (NP (PRP me)) (ADVP (RB now))))"
ptree = Tree.fromstring(bracket_parse)
ptree
for subtree in ptree.subtrees():
    print subtree

rawDescription: Grammar rule extraction from parsed result


code:
import xml.etree.ElementTree as ET
response_data = '''<ListOrdersRequest>
    <ListOrdersResult>
        <Orders>
            <Order>
                ...
                <ElementIWant>value_I_want</ElementIWant>
            </Order>
            <Order>
                ...
                <ElementIWant>value_I_want</ElementIWant>
            </Order>
            ...
        </Orders>
    </ListOrdersResult>
</ListOrdersRequest>'''
for i in ET.fromstring(response_data).findall(".//ElementIWant"):
    print i.text

rawDescription: Parsing XML in Python with ElementTree - findall


code:
class MyDescriptor(object):
    def __init__(self, name, func):
        self.func = func
        self.attr_name = '__' + name
    def __get__(self, instance, owner):
        return getattr(self, self.attr_name)
    def __set__(self, instance, value):
        setattr(self, self.attr_name, value)
        self.func(self.attr_name)

def postprocess(attr_name):
    print 'postprocess called after setting', attr_name

class Example(object):
    prop1 = MyDescriptor('prop1', postprocess)
    prop2 = MyDescriptor('prop2', postprocess)

obj = Example()
obj.prop1 = 'answer'  # prints 'postprocess called after setting __prop1'
obj.prop2 = 42  # prints 'postprocess called after setting __prop2'

rawDescription: Implementing Python persistent properties


code: print template.format(*table_headers)

rawDescription: Python tabulate, appending element to current table


code:
b.mask = np.zeros(500)
b.mask[498] = True
t = timeit.Timer('c=b.view(np.ndarray)[~b.mask[:,0]]; np.apply_along_axis(test, 1, c)','from __main__ import test, b, ma, np')
print round(t.timeit(100), 6)

rawDescription: Python/Numpy - Masked Arrays are Very Slow


code:
from lxml import html
from lxml.html import clean

s ='<marquee>67.51</marquee>'
doc = html.fromstring(s)
cleaner = clean.Cleaner(safe_attrs=html.defs.safe_attrs | set(['style']))

print html.tostring(cleaner.clean_html(doc))

rawDescription: How to preserve inline CSS style with lxml.html.clean.Cleaner() in Python?

description: Preserve inline CSS style with lxml.html.clean.Cleaner() in Python


code:
import re
import array

listcdtitles = ["""    Liszt, Hungarian Rhapsody #6 {'Pesther Carneval'}; 2 Episodes from Lenau's 'Faust'; 'Hunnenschlacht' Symphonic Poem. (NW German Phil./ Kulka)   """,
""" Puccini, Verdi, Gounod, Bizet: Arias & Duets from Butterfly, Tosca, Boheme, Turandot, I Vespri, Faust, Carmen. (Fiamma Izzo d'Amico & Peter Dvorsky w.Berlin Radio Symph./Paternostro)  """,
""" Tchaikovsky, 'The Tempest' Fantasy. Liszt, Symphonic Poem #1. (London Symph./Butt)  """,
""" Duffy, John: 'Heritage: Civilization and the Jews'- Fanfare & Chorale, Symphonic Dances + Orchestral Suite. Bernstein, 'On the Town' Dance Episodes. (Royal Phil./R.Williams)   """,
""" Lilien, Ignace {1897-1963}: Songs, 1920-1935. (Anja van Wijk, mezzo & Frans van Ruth, piano)    """,
""" Hindemith, Trauermusik. Purcell, 'Fairy Queen' Suite. Rossini, String Sonata #6. Petrov, 'Creation of the World' Ballet Suite. Bartok, Romanian Folkdances Sz 56. Tartini, Flute Concerto in G {w.A.Maiorov} (Leningrad Orch.for Ancient & Modern Music/ Serov) """,
""" Bizet, Verdi, Massenet, Puccini: Arias from Carmen, Rigoletto, Werther, Manon Lescaut, Tosca, Turandot + Songs by Lara, Di Capua et al. (Peter Dvorsky, tenor w.Bratislava Orch./Lenard {Also performing 'Carmen' Overt.& 'Thais' Meditation}. Rec.Live, 10/87) """,
""" Fantini, Rauch, C.Straus, Priuli, Bertali: 'Festival Mass at the Imperial Court of Vienna, 1648' (Yorkshire Bach Choir & Baroque Soloists + Baroque Brass of London/Seymour)    """,
""" Vinci, Leonardo {c.1690-1730}: Arias from Semiramide Riconosciuta, Didone Abbandonata, La Caduta dei Decemviri, Lo Cecato Fauzo, La Festa de Bacco, Catone in Utica. (Maria Angeles Peters sop. w.M.Carraro conducting) """,
""" Gluck, Mozart, Beethoven, Weber, Verdi, Wagner, Ponchielli, Mascagni, Puccini: Arias from Alceste, Don Giovanni, Fidelio, Oberon, Ballo, Tristan, Walkure, Siegfried, Gotterdammerung, Gioconda, Cavalleria, Tosca. (Helene Wildbrunn. Rec.1919-24) """,
""" Stanley, Wesley, Stubley, Boyce, Handel, Heron, Russell, Hook: '18th Century Organ Music on Period Instruments' (Same instruments and artist as above)  """,
""" Reimann, 'Unrevealed' for Baritone & String Quartet to Texts by Lord Byron {R.Salter w.Kreuzberger Quartet}; Variations for Piano (David Levine)    """,
""" Bruckner, Symphony #9. (Berlin Philharmonic/ Jochum. Rec. 'live', 11/28/77) """,
""" Bruckner, Symphony #5. (Haas Edition. BBC Symph./ Horenstein. Rec.9/71) """]

entryDictionary = {}
i=0
for entry in listcdtitles:
    #remove unnecessary characters from the string
    entry=re.sub(r'[^\w ]', '', entry.lower(), flags=re.IGNORECASE)
    #split the entry into words and store it in the
    entryDictionary[i]=entry.split(" ")
    i=i+1
# print the dictionary
print("Entries")
print(entryDictionary)

# define a score matrix, compare the words in each entry and if
# a word is same in both entries, that is one point
scoreMatrix = []
for k in range(i):
    scoreMatrix.append([])
    for j in range (i):
        if j>k:
            scoreMatrix[k].append(0)
        else:
            scoreMatrix[k].append("-")
k=0
j=0

for k in range(i-1):
    entry1 = entryDictionary[k]
    for j in range(k+1,i):
        entry2 = entryDictionary[j]
        for kk in range(len(entry1)):
            for jj in range(len(entry2)):
                if entry1[kk] != "" and entry1[kk] == entry2[jj]:
                    scoreMatrix[k][j] = scoreMatrix[k][j] + 1

print "Score Matrix (Higher numbers denote heigher similarity between two entries"

print repr("").rjust(10),
for k in range(i-1):
    print repr("Entry " + str(k)).rjust(10),
print repr("Entry " + str(i-1)).rjust(10)

for k in range(i):
    scoreMatrix.append([])
    print repr("Entry " + str(k)).rjust(10),
    for j in range (i-1):
        print repr(scoreMatrix[k][j]).rjust(10),
    print repr(scoreMatrix[k][i-1]).rjust(10)

rawDescription: python: complex string algorithm

description: Python: complex string algorithm


code:
# keys = yourDict.keys(), e.g.
keys = ['low', 'el', 'helloworld', 'something', 'ellow', 'thing', 'blah', 'thingy']

# flt is [[key, is_substring],...] sorted by key length reversed
flt = [[x, 0] for x in sorted(keys, key=len, reverse=True)]

for i in range(len(flt)):
    p = flt[i]
    if p[1]:  # already removed
        continue
    for j in range(i + 1, len(flt)): # iterate over shorter strings
        q = flt[j]
        if not q[1] and q[0] in p[0]: # if not already removed and is substring
            q[1] = 1  # remove

goodkeys = set(x[0] for x in flt if not x[1])
print goodkeys # e.g ['helloworld', 'something', 'thingy', 'blah']

rawDescription: Remove a dictionary key if it is a substring in any other key


code:
for x in target.xpath('/root/targetroot/targetcontainer/target'):
    print x.text

rawDescription: Quickest/Best way to traverse XML with lxml in Python


code:
def f():
    return 1
    print 'hi'
def g():
    return 1

rawDescription: does return(value) stop a python script

description: Does return(value) stop a python script


rawDescription: Exclude matching the period character in a [\W\d]+ regex


code:
import feedparser

result = feedparser.parse('https://gdata.youtube.com/feeds/api/videos/Ej4_G-E1cAM/comments')
for entry in result.entries:
    print entry.author

rawDescription: Single out tags in an xml document?

description: Single out tags in an xml document


code:
def run():

    from collection import difaultdict

    with open(stopwordfile, 'r') as f:
        stopwords = '|'.join(f.read().lower().split())

    regex = re.compile(r'.*
(.*?)
.*', re.DOTALL)
    reg123 = re.compile(r'(</?[ap][^>]*>|"|\W+|'+stopwords+')')

    totalfreq = defaultdict(int)
    filewords = {}

    for filename in (fn for fn in os.listdir(path) if fn[-5:] == '.html'):
        with open(path + filename, 'r') as f:
            ch = regex.findall(f.read().lower())[0]
            ch = reg123.sub(' ', ch)
            words = ch.strip().split()

        freq = defaultdict(int)
        for w in words:
            totalfreq[w] += 1
            freq[w] += 1
        filewords[filename] = freq

    print totalfreq

rawDescription: Counting frequency of words in documents using python regex


code:
from bs4 import BeautifulSoup

document_a = """

Some Name

"""

document_b = """

Some Name

"""
def get_author(document):
    soup = BeautifulSoup(document_a)
    p = soup.find(class_="author-name")
    if not p:
        p = soup.find(rel="author-name")
        if not p:
            return "No Author Found"
    return p.text

print "author in first document:", get_author(document_a)
print "author in second document:", get_author(document_b)

rawDescription: Using lxml or ??? to extract information from webpages


code:
import argparse
class MyAction(argparse.Action):
    def __call__(self,parser,namespace,values,option_string=None):
        if not values:
            setattr(namespace,self.dest,[0])
        else:
            setattr(namespace,self.dest,values)

parser = argparse.ArgumentParser()
parser.add_argument('-a',default=[None],action=MyAction,type=int,nargs='*')
print parser.parse_args(''.split())
print parser.parse_args('-a'.split())
print parser.parse_args('-a 1 2 3'.split())

rawDescription: Python argparse: combine multiple-value argument with default and const


code:
import requests
import argparse

parser = argparse.ArgumentParser(description='Fetch wikipedia extracts.')
parser.add_argument('word', help='word to define')
args = parser.parse_args()

proxies = {
    # See http://www.mediawiki.org/wiki/API:Main_page#API_etiquette
    # "http": "http://localhost:3128",
}

headers = {
    # http://www.mediawiki.org/wiki/API:Main_page#Identifying_your_client
    "User-Agent": "Definitions/1.0 (Contact rob@example.com for info.)"
}

params = {
    'action':'query',
    'prop':'extracts',
    'format':'json',
    'exintro':1,
    'explaintext':1,
    'generator':'search',
    'gsrsearch':args.word,
    'gsrlimit':1,
    'continue':''
}

r = requests.get('http://en.wikipedia.org/w/api.php',
                 params=params,
                 headers=headers,
                 proxies=proxies)
json = r.json()
if "query" in json:
    result = json["query"]["pages"].items()[0][1]["extract"]
    print result.encode('utf-8')
else:
    print "No definition."

rawDescription: Interested in searching wikipedia xml dump for only medically-related terms


code:
def eformat(f, prec, exp_digits):
    s = "%.*e"%(prec, f)
    mantissa, exp = s.split('e')
    # add 1 to digits as 1 is taken by sign +/-
    return "%se%+0*d"%(mantissa, exp_digits+1, int(exp))

print eformat(0.0000870927939438012, 14, 3)
print eformat(1.0000870927939438012e5, 14, 3)
print eformat(1.1e123, 4, 4)
print eformat(1.1e-123, 4, 4)

rawDescription: Python - number of digits in exponent

description: Python - number of digits in exponent


code:
from collections import defaultdict

d = defaultdict(int)

with open("data.txt") as f:
    for line in f:
        tokens = [t.strip() for t in line.split(",")]
        try:
            sid = int(tokens[3])
            mark = int(tokens[4])
        except ValueError:
            continue
        d[sid] += mark

print d

rawDescription: Python CSV - Need to sum up values in a column grouped by value in another column


code:
class NamedModel(db.Model):
    """A Model subclass for entities which automatically generate their own key
    names on creation. See documentation for _generate_key function for
    requirements."""

    def __init__(self, *args, **kwargs):
        kwargs['key_name'] = _generate_key(self, kwargs)
        super(NamedModel, self).__init__(*args, **kwargs)

def _generate_key(entity, kwargs):
    """Generates a key name for the given entity, which was constructed with
    the given keyword args.  The entity must have a KEY_NAME property, which
    can either be a string or a callable.

    If KEY_NAME is a string, the keyword args are interpolated into it.  If
    it's a callable, it is called, with the keyword args passed to it as a
    single dict."""

    # Make sure the class has its KEY_NAME property set
    if not hasattr(entity, 'KEY_NAME'):
        raise RuntimeError, '%s entity missing KEY_NAME property' % (
            entity.entity_type())

    # Make a copy of the kwargs dict, so any modifications down the line don't
    # hurt anything
    kwargs = dict(kwargs)

    # The KEY_NAME must either be a callable or a string.  If it's a callable,
    # we call it with the given keyword args.
    if callable(entity.KEY_NAME):
        return entity.KEY_NAME(kwargs)

    # If it's a string, we just interpolate the keyword args into the string,
    # ensuring that this results in a different string.
    elif isinstance(entity.KEY_NAME, basestring):
        # Try to create the key name, catching any key errors arising from the
        # string interpolation
        try:
            key_name = entity.KEY_NAME % kwargs
        except KeyError:
            raise RuntimeError, 'Missing keys required by %s entity\'s KEY_NAME '\
                'property (got %r)' % (entity.entity_type(), kwargs)

        # Make sure the generated key name is actually different from the
        # template
        if key_name == entity.KEY_NAME:
            raise RuntimeError, 'Key name generated for %s entity is same as '\
                'KEY_NAME template' % entity.entity_type()

        return key_name

    # Otherwise, the KEY_NAME is invalid
    else:
        raise TypeError, 'KEY_NAME of %s must be a string or callable' % (
            entity.entity_type())

rawDescription: Custom keys for Google App Engine models (Python)


code:
from bs4 import BeautifulSoup, Comment

data = """

        Bold text - some text here

        lorem ipsum here

         Author Name

    """
soup = BeautifulSoup(data, 'lxml')
get_class = soup.find("div", {"class" : "gt-read"})
comments = get_class.find_all(text=lambda text:isinstance(text, Comment))
[comment.extract() for comment in comments]

get_class.find("div").extract()
text = get_class.encode_contents().strip()

print text

rawDescription: Get the content of div within div using BeautifulSoup?


code:
w = 600

integral = np.random.randint(0, 255, (w, w))
bitmap = np.zeros_like(integral)

lines = np.random.randint(0, w, (10000, 6))

def computeSumInboundingbox(bitmap, cameraTable, integral):
   for line in cameraTable:
        minX, minY, maxX, maxY = line[2], line[3], line[4], line[5]
        sumBox = integral[(minY,minX)] + integral[(maxY, maxX)] - integral[(maxY,minX)] - integral[(minY,maxX)]
        if sumBox != 0:
            bitmap[line[0],line[1]] = float(sumBox) / ((maxX - minX) * (maxY - minY))

def computeSumInboundingbox2(bitmap, line, integral):
    tx, ty, minx, miny, maxx, maxy = line.T
    sumBox = integral[miny, minx] + integral[maxy, maxx] - integral[maxy, minx] - integral[miny, maxx]
    mask = sumBox != 0
    bitmap[tx[mask], ty[mask]] = (sumBox.astype(float)[mask] / ((maxx - minx) * (maxy - miny))[mask])

bitmap1 = np.zeros_like(integral)
bitmap2 = np.zeros_like(integral)
computeSumInboundingbox(bitmap1, lines, integral)
computeSumInboundingbox2(bitmap2, lines, integral)
print np.allclose(bitmap1, bitmap2)

rawDescription: Python performance using Boost Python doesn't improve