code:
from inspect import ismethod

def call_all(obj, *args, **kwargs):
    for name in dir(obj):
        attribute = getattr(obj, name)
        if ismethod(attribute):
            attribute(*args, **kwargs)

class Test():
    def a(self, input):
        print "a: " + input
    def b(self, input):
        print "b: " + input
    def c(self, input):
        print "c: " + input

call_all(Test(), 'my input')

rawDescription: Python: Call all methods of an object with a given set of arguments


code: print outputdict['/Title'] + ", " + outputdict['/Subject']

rawDescription: Avoiding special characters with regex

description: Avoiding special characters with regex


code:
def test(n, l, store, alias):
    if alias:
        len_ = len
        len_l = len_(l)
    else:
        len_l = len(l)
    for _ in range(n):
        if store:
            _ = len_l
        elif alias:
            _ = len_(l)
        else:
            _ = len(l)

if __name__ == '__main__':
    from itertools import product
    from timeit import timeit
    setup = 'from __main__ import test, l'
    for n, l, store, alias in product(
        (1, 10, 100),
        ([None]*10,),
        (True, False),
        (True, False),
    ):
        test_case = 'test({!r}, l, {!r}, {!r})'.format(n, store, alias)
        print test_case, len(l),
        print timeit(test_case, setup=setup)

rawDescription: perfomance of len(List) vs reading a variable

description: Perfomance of len(List) vs reading a variable


code:
import multiprocessing

def crawl(result_queue):
    import urllib2
    data = urllib2.urlopen("http://news.ycombinator.com/").read()

    print "Requested..."

    if "result found (for example)":
        result_queue.put("result!")

    print "Read site."

processs = []
result_queue = multiprocessing.Queue()

for n in range(4): # start 4 processes crawling for the result
    process = multiprocessing.Process(target=crawl, args=[result_queue])
    process.start()
    processs.append(process)

print "Waiting for result..."

result = result_queue.get() # waits until any of the proccess have `.put()` a result

for process in processs: # then kill them all off
    process.terminate()

print "Got result:", result

rawDescription: Terminate multiple threads when any thread completes a task


code:
import pprint

temp = [
(41, 1, "2015-1-22 12:37:58.631670", 'Pending'),
(37, 1, "2015-1-21 13:56:3.632057", 'Delivered'),
(41, 14, "2015-1-22 12:37:58.631670", 'Delivered'),
(36, 1, "2015-1-21 13:22:52.705818", 'Delivered'),
(40, 2, "2015-1-22 12:37:58.631670", 'Delivered'),
(38, 1, "2015-1-21 14:4:10.206100", 'Delivered')
]

output = {}
for i in temp:
    id = i[0]
    count = i[1]
    date_v = i[2]
    status = i[3]
    p_count = 0
    d_count = 0
    f_count = 0
    if status=="Pending":
        p_count = count
    elif status=="Delivered":
        d_count = count
    elif status=="Failed":
        f_count= count

    try:
        output[i[0]]["Pending"] = output[i[0]]["Pending"]+p_count
        output[i[0]]["Failed"] = output[i[0]]["Failed"]+f_count
        output[i[0]]["Delivered"] = output[i[0]]["Delivered"]+d_count
        output[i[0]]["total"] = output[i[0]]["Pending"]+count
        output[i[0]]["date time"] = date_v
    except KeyError, e:
        total = count
        output[i[0]] = {'id':id, 'Pending':p_count, 'Failed':f_count,\
                        'Delivered':d_count, 'total':total, 'date time':date_v}

pprint.pprint(output)

rawDescription: Filter out dictionary removing duplicates


rawDescription: typing e exponent in python

description: Typing e exponent in python


code:
from nltk.tree import ParentedTree
ptree = ParentedTree.fromstring('(ROOT (S (NP (JJ Congressional) \
    (NNS representatives)) (VP (VBP are) (VP (VBN motivated) \
    (PP (IN by) (NP (NP (ADJ shiny) (NNS money))))))) (. .))')

def traverse(t):
    try:
        t.label()
    except AttributeError:
        return
    else:

        if t.height() == 2:   #child nodes
            print t.parent()
            return

        for child in t:
            traverse(child)

traverse(ptree)

rawDescription: NLTK tree data structure, finding a node, it's parent or children


code:
for i in dir(foo):
    attr = getattr(foo, i)
    print type(attr)

rawDescription: Finding out type within dir()

description: Finding out type within dir()


code:
inp_check=["A|1" , "D|41", "F|34"]
inp_master=["A","B", "C", "D", "E", "F"]
print [next((value2 for value2 in inp_check if value in value2 ) ,"null") for value in inp_master]

rawDescription: Comparing two lists and inserting Nulls where necessary


code:
lst = ['A', 'B', 'C']
index = 7
for i in range(index + 1):
    print i, lst[i % len(lst)]

rawDescription: Relooping the list

description: Relooping the list


code:
class A(object):
    def __init__(self, value):
        self.value = value

a = A(1)
print a.value
a.value = 5
print a.value

rawDescription: What is the proper way to split a python module into submodules?


code:
from itertools import chain

x = [('Edgar',), ('Robert',)]

# list is to materialize the entire sequence.
# Normally you would use this in a for loop with no `list()` call.
print list(chain.from_iterable(x))

rawDescription: Python list of tuples, need to unpack and clean up


code:
class MyClassWithAQueue(object):
    def __init__(self):
        self.q = multiprocessing.Queue()

myclass = MyClassWithAQueue()
myclass.q = Queue.Queue()
print myclass.q

rawDescription: Monkey Patching an attribute within a class?


code:
filename = 'bob_20090216.txt'

regexps = {'bob': 'bob_[0-9]+.txt',
           'fred': 'fred_[0-9]+.txt',
           'paul': 'paul_[0-9]+.txt'}

for filetype, regexp in regexps.items():
    m = re.match(regexp, filename)
    if m != None:
        print '%s is of type %s' % (filename, filetype)

rawDescription: Parameterised regular expression in Python


code:
class MyBrowser(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,
                   style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)
        self.transparency = 255
        self.dialog = wx.TextEntryDialog(None, message="Enter stuff")
        self.dialog.ShowModal()
        print self.dialog.GetValue()
        self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

rawDescription: Get user input from a dialog before showing the mainframe


code:
from collections import defaultdict

LofD = [{'name': 'johnny', 'surname': 'smith', 'age': 53},
 {'name': 'johnny', 'surname': 'ryan', 'age': 13},
 {'name': 'jakob', 'surname': 'smith', 'age': 27},
 {'name': 'aaron', 'surname': 'specter', 'age': 22},
 {'name': 'max', 'surname': 'headroom', 'age': 108},
]

def counters():
  return defaultdict(int)

def freqs(LofD):
  r = defaultdict(counters)
  for d in LofD:
    for k, v in d.items():
      r[k][v] += 1
  return dict((k, dict(v)) for k, v in r.items())

print freqs(LofD)

rawDescription: item frequency in a python list of dictionaries

description: Item frequency in a python list of dictionaries


code:
import itertools
import operator
import sys

lines = (line.split() for line in sys.stdin)
prev_name, prev_month = '', '99999999'
month_view = 0
for (name,date), grp in itertools.groupby(lines, key=operator.itemgetter(0,1)):
    view = sum(int(row[-1]) for row in grp)
    if prev_name == name and date.startswith(prev_month):
        month_view += view
    else:
        prev_name = name
        prev_month = date[:6]
        month_view = view
    print '{}\t{}\t{}\t{}'.format(name, date, view, month_view)

rawDescription: Python script not working as required

description: Python script not working as required


code:
import unittest

class Test(unittest.TestCase):
    def setUp(self):
        print 'Setting Up'

    def test1(self):
        print 'In test1'
        self.test2()

    def test2(self):
        print 'In test2'

    def tearDown(self):
        print 'Tearing Down'

if __name__ == '__main__':
    unittest.main()

rawDescription: python unittest methods

description: Python unittest methods


rawDescription: Python 3: check if the entire list is inside another list


code:
file1=open('a.txt','r')
file1=file1.readlines()
file1={i.split()[0]:i.split()[1] for i in file1}
print file1
#{'1': 'a|c', '3': 'a|b|c', '2': 'a|b|c|d|e', '7': 'a|b|c', '6': 'a|b|c', '9': 'x|y', '8': 'a|b|c'}

file2=open('b.txt','r')
file2=file2.readlines()
file2={i.split()[0]:i.split()[1] for i in file2}
print file2
#{'1': 'b|c', '9': 'a|b|c', '3': 'a|b|c', '2': 'a|b|d', '4': 'a|b'}

keys=set(file1.keys()+file2.keys())

for i in sorted(keys):
    if i in file1 and i in file2:
        file1_values=file1[i].split('|')
        file2_values=file2[i].split('|')
        intersec=set(file1_values)&set(file2_values)
        if len(intersec)>0:
            print i,'|'.join(intersec)
        else:print i, 'missing values'
    else:
        print i,'empty'

rawDescription: Python comparing two dictionaries with multiple values per key and retrieve common values


code:
from lxml import etree
context = etree.parse(handle)
nsmap = context.getroot().nsmap.copy()
nsmap['xmlns'] = nsmap.pop(None)
print next(x.text for x in context.xpath('.//xmlns:servlet-class', namespaces=nsmap))

rawDescription: parse xml with lxml including namespace

description: Parse xml with lxml including namespace


code:
if CompanyIndex in companies:
    name, founding_year, number_of_employee = companies[CompanyIndex]
    # print stuff
else:
    print 'Your input is not correct..'

rawDescription: python saving information from text data

description: Python saving information from text data


code:
for k in occurrences_dict:
    print k, occurrences_dict[k]

rawDescription: Parsing fasta file with biopython to count number sequence reads belonging to each ID


code:
foo = expr.parseString("x + y + z")
foo.var
print foo.dump()

rawDescription: pyparsing: named results?

description: Pyparsing: named results


code:
import lxml.etree as ET
from lxml.etree import XMLParser

def worthy(elem):
    for child in elem.iterchildren():
        if (child.tag == 't') and (child.text is not None):
            return True
    return False

def dump(elem):
    for child in elem.iterchildren():
        print "Good", child.tag, child.text

parser = XMLParser(ns_clean=True, recover=True)
etree = ET.parse('regex_trial.xml', parser)
for thing in etree.findall("//"):
    if thing.tag == 'tc' and worthy(thing):
        dump(thing)

rawDescription: Regular Expressions to parse template tags in XML


code:
import sys, re

first_re = re.compile(r'^\d{3}$')

if len(sys.argv) > 1:

    if first_re.match(sys.argv[1]):
        print "Primary argument is : ", sys.argv[1]
    else:
        raise ValueError("First argument should be ...")

    args = sys.argv[2:]

else:

    args = ()

# ... anywhere in code ...

if 'debug' in args:
    print 'debug flag'

if 'xls' in args:
    print 'xls flag'

rawDescription: How can I process command line arguments in Python?

description: Process command line arguments in Python


rawDescription: How do you remove a sublist if a certain element is not found at a specific position across all sublists?

description: Remove a sublist if a certain element is not found at a specific position across all sublists


code:
from pylab import *
import netCDF4

f = netCDF4.MFDataset('/usgs/data2/rsignell/models/ncep/narr/air.2m.19??.nc')
# print variables
f.variables.keys()

atemp = f.variables['air']
print atemp

ntimes, ny, nx = shape(atemp)
cold_days = zeros((ny,nx),dtype=int)

for i in xrange(ntimes):
    cold_days += atemp[i,:,:].data-273.15 < 0

pcolormesh(cold_days)
colorbar()

rawDescription: Loop through netcdf files and run calculations - Python or R


code:
for i in xrange(1,6,2):
    print i

rawDescription: Python analogue of Matlab's interval numbers like 1:2:5


code: print '<img src="%s">' * len(foo) % tuple(foo)

rawDescription: Python: Print list of unknown length?

description: Python: Print list of unknown length


code:
def OnSelectMustHave(self,event):
    info = event.GetData()
    InfoList.append(info)
    def do_select():
          map(lambda x:self.list_ctrl.Select(x,1) ,set(InfoList))

    wx.CallLater(100,do_select)
    print info,InfoList
    return

rawDescription: Python and multiple select with a ListCtrl


code:
from time import sleep

import random

@celery.task
def power(value, expo):
    sleep(random.randint(10, 1000) / 1000.0) # sleep for 10-1000ms
    return value ** expo

@celery.task
def amass(results, tasks):
    completed_tasks = []
    for task in tasks:
        if task.ready():
            completed_tasks.append(task)
            results.append(task.get())

    # remove completed tasks
    tasks = list(set(tasks) - set(completed_tasks))

    if len(tasks) > 0:
        # resend the task to execute at least 1 second from now
        amass.delay(results, tasks, countdown=1)
    else:
        # we done
        print results

rawDescription: Running a task after all tasks have been completed


code:
folder = "C:/Documents and Settings/DuffA/Bureaublad/shortcuts projects/klic01/11G008689_1"

import glob, os
for filename in glob.glob(os.path.join(folder, "*+*")):
    print "I rename '+' to '_' in\n"+filename
    os.rename(filename, filename.replace('+','_'))

print '\n\n---- Now, there after, are the filenames in \n     '+folder

for root, dirs, filenames in os.walk(folder): # returms root, dirs, and files
    for filename in filenames:
        print '\nfilename==',filename
        filename_split = os.path.splitext(filename) # filename and extension name (extension in [1])
        filename_zero = filename_split[0]
        extension = str.upper(filename_split[1])
        print 'filename_zero==',filename_zero
        print 'os.path.join(filename_zero, "*.*")==',os.path.join(filename_zero, "*.*")
        print 'glob.glob(os.path.join(filename_zero, "*.*"))==',glob.glob(os.path.join(filename_zero, "*.*"))
        for filename_zero in glob.glob(os.path.join(filename_zero, "*.*")):
            print '  filename_zero in glob.glob(os.path.join(filename_zero, "*.*")) ==',filename_zero
            os.rename(filename_zero, filename_zero.replace('.','_'))

rawDescription: find and replace within a filename

description: Find and replace within a filename


code:
# create train and test folds from our labels:
cv_by_hand = [(np.where(labels != label)[0], np.where(labels == label)[0])
               for label in np.unique(labels)]

# We check this against our existing cv by converting the latter to a list
cv_to_list = list(cv)

print cv_by_hand
print cv_to_list

# Check equality
for (train1, test1), (train2, test2) in zip(cv_by_hand, cv_to_list):
    assert (train1 == train2).all() and (test1 == test2).all()

# Use the created cv_by_hand in cross validation
scores2 = cross_val_score(lr, data, target, cv=cv_by_hand)

# assert equality again
assert (scores == scores2).all()

rawDescription: Custom cross validation split sklearn

description: Custom cross validation split sklearn


code:
keys = [th.text.strip() for th in rows[0].find_all('th')]
for row in rows[1:]:
    data = {key: td.text.strip() for key, td in zip(keys, row.find_all('td'))}
    print data

rawDescription: python beautifulsoup4 parsing google finance data

description: Python beautifulsoup4 parsing google finance data


code:
import csv
from itertools import chain

def get_mulList(*args):
    return map(list,zip(*args))

csv_data = open('logger_data.csv','r')
data = list(csv.reader(csv_data))
ind_dict = dict(zip(data[0],get_mulList(*data[1:])))
print ind_dict

rawDescription: Read CSV file as a dictionary containing first row as headers (keys)


code:
def nfruits2(dictionary, string):
    i = 0
    string = sorted(string)

    for idx, char in enumerate(string):
        # Update the fruit Python ate
        dictionary[char] -= 1
        # update others he bought, skip this on the last step
        if idx < len(string) - 1:
            for key in dictionary:
                if key != char:
                    dictionary[key] += 1
        print dictionary
    return dictionary[max(dictionary, key = dictionary.get)]

rawDescription: Change values in dictionary except for one key


rawDescription: List length versus dictionary length in Python


code:
import cProfile

def test():
    sols = [i for i in matrix_path(5)]
    print len(sols), "paths found"

cProfile.run("test()")

rawDescription: All possible combinations of numbers of length 5 in a 4X4 matrix


code:
import logging
import subprocess
import shlex
import select
import fcntl
import os
import errno
import contextlib

logger = logging.getLogger(__name__)

def make_async(fd):
    '''add the O_NONBLOCK flag to a file descriptor'''
    fcntl.fcntl(fd, fcntl.F_SETFL, fcntl.fcntl(fd, fcntl.F_GETFL) | os.O_NONBLOCK)

def read_async(fd):
    '''read some data from a file descriptor, ignoring EAGAIN errors'''
    try:
        return fd.read()
    except IOError, e:
        if e.errno != errno.EAGAIN:
            raise e
        else:
            return ''

def log_fds(fds):
    for fd in fds:
        out = read_async(fd)
        if out:
            logger.info(out)

@contextlib.contextmanager
def plain_logger():
    root = logging.getLogger()
    hdlr = root.handlers[0]
    formatter_orig = hdlr.formatter
    hdlr.setFormatter(logging.Formatter('%(message)s'))
    yield
    hdlr.setFormatter(formatter_orig)

def main():
    # fmt = '%(name)-12s: %(levelname)-8s %(message)s'
    logging.basicConfig(filename = '/tmp/test.log', mode = 'w',
                        level = logging.DEBUG)

    logger.info("Started")
    cmdStr = 'ls -laR /tmp'

    with plain_logger():
        proc = subprocess.Popen(shlex.split(cmdStr),
                                stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        # without `make_async`, `fd.read` in `read_async` blocks.
        make_async(proc.stdout)
        make_async(proc.stderr)
        while True:
            # Wait for data to become available
            rlist, wlist, xlist = select.select([proc.stdout, proc.stderr], [], [])
            log_fds(rlist)
            if proc.poll() is not None:
                # Corner case: check if more output was created
                # between the last call to read_async and now
                log_fds([proc.stdout, proc.stderr])
                break

    logger.info("Done")

if __name__ == '__main__':
    main()

rawDescription: Python logging and subprocess ouput and error stream


code:
import appscript
iTunes = appscript.app("iTunes")
lib = iTunes.playlists['Library']
for trk in lib.tracks():
    if re.search("test", trk.name()):
        print trk.name()

rawDescription: Searching for a track on iTunes

description: Searching for a track on iTunes


code:
import datetime
import calendar
now = datetime.datetime.now()
print calendar.monthrange(now.year, now.month)[1]

rawDescription: How to get month interval using "datetime" in python?

description: Get month interval using "datetime" in python


code:
import sys
import difflib
from bs4 import BeautifulSoup

text = [[],[]]
files = []
soups = []

for i, arg in enumerate(sys.argv[1:]):
  files.append(open(arg, "r").read())
  soups.append(BeautifulSoup(files[i], 'xml'))

  for tag_text in soups[i].find_all('OCRCharacters'):
    text[i].append(''.join(tag_text))

for first_string, second_string in zip(text[0], text[1]):
    d = difflib.Differ()
    diff = d.compare(first_string.splitlines(), second_string.splitlines())
    print '\n'.join(diff)

rawDescription: Using diff with beautiful soup objects

description: Using diff with beautiful soup objects


code:
# We can import a sibling module like this.
from app.package_b.module_b import bbb
def aaa(s, t):
    return '{0} {1}'.format(s, bbb(t))

# We can also run module_a.py directly, using Python's -m option, which
# allows you to run a module like a script.
#
#    python -m app.package_a.module_a
if __name__ == '__main__':
    print aaa(111, 222)
    print bbb(333)

rawDescription: Optimal file structure organization of Python module unittests?


code:
brownGrowth = growPOP(brownPOP, totalPOP, rt(ca, cb, month), carryK)
while month <= 2:
    print "Month ", month
    print "Grown brown fish: ", brownGrowth
    brownPOP = endbrownPOP
    goldPOP = endgoldPOP
    totalPOP = endtotalPOP
    carryK = newcarryK
    month = month + 1

rawDescription: Efficient coding for loops with names for variables


code:
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse

from uploadfile.models import Document
from uploadfile.forms import DocumentForm

def imageupload(request):
    # Handle file upload
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            newdoc = Document(docfile = request.FILES['docfile'])
            newdoc.save()

            # Redirect to the document list after POST
            return HttpResponseRedirect(reverse('uploadfile.views.list'))
    else:
        form = DocumentForm() # A empty, unbound form

    # Load documents for the list page
    doc_list = []
    documents = Document.objects.all()
    for document in documents:
        doc_list.append(document.docfile.name)
    print doc_list[-1]

    # Render list page with the documents and the form
    return render_to_response('imageupload.html',
    {'documents': documents,'form': form},
    context_instance=RequestContext(request))

def imageuploadresult(request):

    #here I used PyImgur package to upload image and get public url. Use sbi to get the Google BestGuess. What I return to this page is the uploaded image and google best guess. I will skip this part of code.
    return render_to_response(
        'imageuploadresult.html',
        {'query': q, 'imagepath':path2},
        context_instance=RequestContext(request))

rawDescription: Django Upload Image can not find uploaded image


code:
configdict = {
        'tasks': {
            'entries': [
                { 'id': 1, 'date': 736366 },
                { 'id': 2, 'date': 735444 },
                { 'id': 3, 'date': 735796 }
                ]
            }
        }

tasks = sorted([ t for t in configdict['tasks']['entries'] ], key=lambda task: task['date'])

print repr(tasks)

rawDescription: sorting list of objects in Python

description: Sorting list of objects in Python


code:
res = iter_rows(active)
keys = next(res)
for new in res:
    print dict(zip(keys, new))

rawDescription: openpyxl Fetch value from excel and store in key value pair

description: Openpyxl Fetch value from excel and store in key value pair


code:
try:
  import simplejson as json
except:
  import json
out = {'key': 'value', 'key2': 4}
print json.dumps(out)

rawDescription: Send data from Python to Javascript (JSON)


code:
class A:
   def __init__(self):
      self.name = 'hi'

x = 3
a = A()

def foo():
   a.name = 'Bedevere'
   x = 9

foo()
print x, a.name #outputs 3 Bedevere

rawDescription: Should I use a class in this: Reading a XML file using lxml


code:
import sys

# since we're reading the csv file, we'll want to use the `csv` module
# to help us parse it
import csv

if len(sys.argv) < 2:
    print "usage: {} <workout_file>".format(sys.argv[0])
    sys.exit(1)

filename = sys.argv[1]

# now that we're reading the file, we'll use `r`
with open(filename, 'r') as data_file:
    # to use `csv`, you need to create a csv-reader object by
    # passing in the `data_file` `file` object
    reader = csv.reader(data_file)

    # now reader contains a parsed iterable version of the file
    for row in reader:
        # here's where you'll want to investigate different plotting
        # libraries and such, where you'll be accessing the various
        # points in each line as follows:
        workout_name = row[0]
        num_sets = row[1]
        num_reps = row[2]
        weight = row[3]
        workout_time = row[4]
        # optionally, if your csv file contains headers (as in the example
        # above), you can access fields in each row using:
        #
        # row['weight'] or row['workout'], etc.

rawDescription: python: write input to file and save it

description: Python: write input to file and save it


code:
import bisect
import random
import timeit

def bisect_solution(size=10000):
    lst = []
    for n in xrange(size):
        value = random.randint(1, 1000000)
        bisect.insort_left(lst, value)
    return lst

# Cut out of the bisect module to be used in bisect_solution2()
def insort_left(a, x, lo=0, hi=None):
    """Insert item x in list a, and keep it sorted assuming a is sorted.

    If x is already in a, insert it to the left of the leftmost x.

    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x: lo = mid+1
        else: hi = mid
    a.insert(lo, x)

def bisect_solution2(size=10000):
    lst = []
    for n in xrange(size):
        value = random.randint(1, 1000000)
        insort_left(lst, value)
    return lst

def sort_solution(size=10000):
    lst = []
    for n in xrange(size):
        value = random.randint(1, 1000000)
        lst.append(value)
        lst.sort()
    return lst

t = timeit.timeit('bisect_solution()', 'from __main__ import bisect_solution', number = 10)
print "bisect_solution: ", t

t = timeit.timeit('bisect_solution2()', 'from __main__ import bisect_solution2', number = 10)
print "bisect_solution2: ", t

t = timeit.timeit('sort_solution()', 'from __main__ import sort_solution', number = 10)
print "sort_solution: ", t

rawDescription: Fastest way to sort list of dates when adding to the list


code:
def load_data(self, column, cell, model, iter):
    # load data for the given model row
    print 'load data ' + model.get_value(iter, 0)

rawDescription: PyGTK how to "load-on-demand" data to TreeView


code: df1 = df1.reset_index(); print df1

rawDescription: Avoiding double counting in pandas merge


code: print zip(*data)

rawDescription: python list manipulation nesting vertically, making it look like a matrix

description: Python list manipulation nesting vertically, making it look like a matrix


code:
def robot_print():
    pybot_stdout = sys.stdout
    sys.stdout = sys.__stdout__
    print "this is a testlib log for robot test - Test.robot"
    sys.stdout = pybot_stdout

rawDescription: Pybot - redirect test library print statements to the console


code:
def foo(): print 'foo'

bar = foo
bar()

rawDescription: higher order function python walkthrough

description: Higher order function python walkthrough


code:
from sys import argv

script, input = argv

import sys
print sys.path

rawDescription: difference between various import statements in python

description: Difference between various import statements in python


code:
def fun():
    x = 42
    print "local id in fun:", id(locals())

import types
def no_locals(fn):
    func_code = fn.func_code
    return types.FunctionType(
        types.CodeType(
            func_code.co_argcount,
            func_code.co_nlocals,
            func_code.co_stacksize,
            func_code.co_flags & ~2, # disable newlocals
            func_code.co_code,
            func_code.co_consts,
            func_code.co_names,
            func_code.co_varnames,
            func_code.co_filename,
            func_code.co_name,
            func_code.co_firstlineno,
            func_code.co_lnotab),
        globals())

id(globals())
assert('x' not in locals())
fun()
assert('x' not in locals())
fun = no_locals(fun) # disable newlocals flag for fun
assert('x' not in locals())
fun()
assert('x' in locals())
x

rawDescription: PyEval_GetLocals returns globals?

description: PyEval_GetLocals returns globals


code:
cols = ['family','id','mum','dad']+list(B)
result = A[cols]

for line in [cols]+list(result):
  print '\t'.join([str(col) for col in line])

rawDescription: Numpy: order array as other array - by string order


rawDescription: Selecting some elements based on conditions


code:
import StringIO
from xml.etree import ElementTree

def get_index(root, element):
    for idx, child in enumerate(root):
        if element == child:
            return idx
    else:
        raise ValueError("No '%s' tag found in '%s' children" %
                         (element.tag, root.tag))

xml_str = """<root><something></something><application></application></root>"""
tree = ElementTree.parse(StringIO.StringIO(xml_str))

# print our tree pre-insertion
print ElementTree.tostring(tree.getroot())

permission_str = "<permission></permission>"
perm_element = ElementTree.fromstring(permission_str)
app_element = tree.getroot().find('./application')
app_index = get_index(tree.getroot(), app_element)
tree.getroot().insert(app_index, perm_element)

# print our tree post-insertion
print ElementTree.tostring(tree.getroot())

rawDescription: Python ElementTree insertBefore command

description: Python ElementTree insertBefore command


code:
from ctypes import windll
import time
dc= windll.user32.GetDC(0)
count = 0
t1 = time.time()
while count < 1000:
    a= windll.gdi32.GetPixel(dc,x,y)
    count += 1
t2 = time.time()
tf = t2-t1
print int(count/tf)

rawDescription: Most efficient/quickest way to parse pixel data with Python?


code:
graph = {'A':{'B','C'}, 'B':{'D'}, 'C':{'D'}}
for path in find_all_paths('A','D', graph):
  print path

rawDescription: Python: How to find more than one pathway in a recursive loop when multiple child nodes refers back to the parent?


code:
import json
j = json.loads("""{
"AED" : "united Arab Emirates Dirham",
"AFN" : "Afghan Afghani",
"ZWL" : "Zimbabwean Dollar"
}""")
print json.dumps(j, indent=4)

rawDescription: Start a new line after every comma

description: Start a new line after every comma


code:
list1 = [1, 2, 3]
list2 = [7, 8, 9]

def add_list(a,b):
    carry = 0
    res_list = []
    for i,j in zip(a[::-1],b[::-1]):   # Iterate through the lists in reverse
        val = (i+j+carry)%10           # Store the sum in val
        carry = (i+j+carry)//10        # Store the carry
        res_list.append(val)           # Append to the returning list
    return res_list[::-1]              # Return the list

print add_list(list1,list2)

rawDescription: Adding to a specific variable in a list

description: Adding to a specific variable in a list


code:
from termcolor import colored

print colored('hello', 'red'), colored('world', 'green')

rawDescription: How do I print colored output to the terminal in Python?

description: Print colored output to the terminal in Python


code:
numpy2ri.deactivate()
corr = fit.rx2('correlation')
print corr

rawDescription: Extract correlation matrix from R's factanal() via RPy


code:
class Location:
     name="a place"
     description = "A dark place.  there are exits to the North and East"
     exits = "North","East"
     def __str__(self):
         return "%s\n%s"%(self.name,self.description)

class Player:
     current_location = "Home"
     inventory = ["Blue Key","Magic Thumbtacks"]
     health = 100
     name = "Unknown"
     def __init__(self,name):
         self.name = name

player = Player("Player 1")
loc = Location()
print loc
x = input("Input:")

rawDescription: Avoid using global without confusing new programming students in Python?


code: print "i is equal to %d." % i

rawDescription: how do you put a period right next to a variable in python?

description: Put a period right next to a variable in python


code:
import json

D = {'foo':1, 'baz': 2}

print json.dumps(D)

rawDescription: Passing a Python list to php

description: Passing a Python list to php


code:
huffTree = makeHuffTree(exampleData)
tot = printHuffTree2(freqs, huffTree)
print 'Sum = ', tot

rawDescription: Python, SUM of multiply

description: Python, SUM of multiply


code:
result = []
for remainder in all_remainders(34, 5):
  for nested_remainder in all_remainders(remainder, 3):
    result.append(nested_remainder)

print result

rawDescription: How do i get Python to show all possible remainders instead of just the one that occurs with the largest multiple of the number?

description: Get Python to show all possible remainders instead of just the one that occurs with the largest multiple of the number


code:
fh  = open('Documents/import.txt').read()

for line in fh.split('\n'):
    print line.strip()
    splits = line.split()
    if  len(splits) ==1 and splits[0]== line.strip():
        splits = [item for item in line.strip().split(',') if item]
    print splits

rawDescription: Python Data Import

description: Python Data Import


code:
for stock in stocks:
    print stocks[stock]

rawDescription: What is the correct way to store data N with associated properties if I've 100s of N to track?


code:
from itertools import takewhile, imap, repeat
for x in takewhile(bool, imap(lambda f: f(), repeat(foo))):
    print x

rawDescription: "Iterate" a function's return values

description: "Iterate" a function's return values


code:
import re

lines = [
    "Yes, I'd say so. Nov 08, 2014 UTC",
    "Hell yes! Oct 01, 2014 UTC"
]

for ln in lines:
    print re.sub(r'(\w+\s\d{2}, \d{4} UTC)$', r'\t\1', ln)

rawDescription: Splitting string before name of month with regex


code:
import json
data = [(1, None),
            (4, 1),
            (15, 1),
            (6, 1),
            (2, 1),
            (7, 1),
                (12, 7),
                (13, 7),
                    (17, 13),
                        (18, 17),
                (14, 7),
            (8, 1),
            (9, 1),
            (10, 1),
            (11, 1),
            (19, 1),
        (16, None)]

def convert(input):
    loading_struct = {} #load your tuples into a dict object (because I like dict)
    alignment_struct = {} #structure to hold the output
    seen_embedded_keys = {} #keep track of items we have seen before to remove them
    for line in input: #iterating your input of a list of tuples
        loading_struct[line[0]] = line[1] #unloading them unto a dictionary
        alignment_struct[line[0]] = {} #creating a blank result dictionary with your proposed output
    for node_id, parent_node_id in loading_struct.items(): #iterating the loading struct
        if parent_node_id: #if it doesnt have a parent, we dont need to do anything
            alignment_struct[parent_node_id][node_id] = alignment_struct[node_id]
            seen_embedded_keys[node_id] = True
    for node_id in seen_embedded_keys: #cleanup time remove the keys that are embedded somewhere else
        del alignment_struct[node_id]
    return alignment_struct

output = json.dumps(convert(data)).replace('{}', '""') #your requirement to have nodes with no children to have values of ''
print output

rawDescription: Python list of (node_id, parent_node_id) to JSON


code:
# based on idea from http://groups.google.com/group/comp.lang.python/msg/b1c5bb87a3ff5e34

try:
    import _md5 as md5
except ImportError:
    # python 2.4
    import md5
import ctypes

def md5_getstate(md):
    if type(md) is not md5.MD5Type:
        raise TypeError, 'not an MD5Type instance'
    return ctypes.string_at(id(md) + object.__basicsize__,
                            md5.MD5Type.__basicsize__ - object.__basicsize__)

def md5_continue(state):
    md = md5.new()
    assert len(state) == md5.MD5Type.__basicsize__ - object.__basicsize__, \
           'invalid state'
    ctypes.memmove(id(md) + object.__basicsize__,
                   ctypes.c_char_p(state),
                   len(state))
    return md

m1 = md5.new()
m1.update("hello, ")
state = md5_getstate(m1)
m2 = md5_continue(state)
m2.update("world!")
print m2.hexdigest()

m = md5.new()
m.update("hello, world!")
print m.hexdigest()

rawDescription: Hash algorithm for dynamic growing/streaming data?


code:
row_idx =0
while row_idx < sheet.nrows:
    col_idx = 0
    row_data =[]
    while col_idx < sheet.ncols:
        new_value = remove_returns1(row_idx, col_idx)
        col_idx += 1
        row_data.append(new_value)

    a= ' '.join(row_data)
    print a
    row_idx += 1

rawDescription: How do I iterate through more than just one variable in a loop in Python?

description: Iterate through more than just one variable in a loop in Python


code: print textblock

rawDescription: Acessing a tuple value in a list (trying for hours now :( )


code:
import re
test = '{sq-a_ foo}{sq-b_\nbar}\n{sq-c_ gluck}'
print re.sub(r'(\{*[_a-zA-Z0-9-])\n',r'\1 ',test)

rawDescription: Python 2.6.1 (OS X 10.6.8) re.sub() underscore behavior in groups?


code:
from PyQt4 import QtCore, QtGui
import datetime

def getDatetime():
    return '%s'%datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d %H:%M:%S')

class CustomEvent(QtCore.QEvent):
    _type = QtCore.QEvent.registerEventType()

    def __init__(self, name=None):
        QtCore.QEvent.__init__(self, CustomEvent._type)

class label(QtGui.QLabel):
    def __init__(self, parent=None):
        super(label, self).__init__(parent)

    def customEvent(self, event):
        print '....customEvent %s if event.type(): %s == CustomEvent._type: %s'%(event, event.type(), CustomEvent._type)
        if event.type() == CustomEvent._type:
            self.setText(getDatetime() )

class button(QtGui.QPushButton):
    def __init__(self, parent=None):
        super(button, self).__init__(parent)
        self.clicked.connect(self.onClick)
        self.show()

    def onClick(self):
        event = CustomEvent(name = 'Event Name')
        QtCore.QCoreApplication.postEvent(lbl, event)

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    btn = button()
    btn.show()
    lbl = label()
    lbl.show()
    sys.exit(app.exec_())

rawDescription: When Signal Does Not Find its Slot

description: When Signal Does Not Find its Slot


code:
from collections import Counter

l = ['foobar', 'foofoo', 'foobar', 'barfoo']
D = Counter((v[:3], v[3:]) for v in l)
print D.most_common() # [(('foo', 'bar'), 2), (('foo', 'foo'), 1), (('bar', 'foo'), 1)]

rawDescription: Ordering a nested dictionary by the frequency of the nested value


code:
from ctypes import byref, cdll, c_int

mult = cdll.LoadLibrary('./mult.so')
add = cdll.LoadLibrary('./add.so')
a = c_int(2)
b = c_int(4)
print mult.multiply_(byref(a), byref(b))
print add.addtwo_(byref(a), byref(b))

rawDescription: call functions from a shared fortran library in python

description: Call functions from a shared fortran library in python


code: print hours(i)

rawDescription: How can I reference a list element in this function?

description: Reference a list element in this function


code:
import json
x = "{'FileID': 'a3333.txt','Timestamp': '2014-12-05T02:01:28.271Z','SuccessList':'a,b,c,d,e'}"
x = x.replace("'", '"')
j = json.loads(x)
print j

rawDescription: Unable to remove additional double quotes in json string added by python


code:
def func(tup):
    print """
    Start {0[0]}
    End {0[0]}
    """.format(tup)

rawDescription: Loop through list of tuples, call elements independently


code:
from texttable import Texttable

# ----------------------------------------------------------------
# Imagine data to be generated by Scrapy, for each record:
# a dictionary of three items. The first set ot functions
# generate the data for use in the texttable function

def process_item(item):
    # This massages each record in preparation for writing to csv
    item['Title'] = item['Title'].encode('utf-8') + ','
    item['Release Date'] = item['Release Date'].encode('utf-8') + ','
    item['Director'] = item['Director'].encode('utf-8')
    return item

def initialise_dataset():
    data = [{'Title' : 'Title',
         'Release Date' : 'Release Date',
         'Director' : 'Director'
         }, # first item holds the table header
            {'Title' : 'And Now For Something Completely Different',
         'Release Date' : '1971',
         'Director' : 'Ian MacNaughton'
         },
        {'Title' : 'Monty Python And The Holy Grail',
         'Release Date' : '1975',
         'Director' : 'Terry Gilliam and Terry Jones'
         },
        {'Title' : "Monty Python's Life Of Brian",
         'Release Date' : '1979',
         'Director' : 'Terry Jones'
         }
        ]

    data = [ process_item(item) for item in data ]
    return data

def records(data):
    for item in data:
        yield [item['Title'], item['Release Date'], item['Director'] ]

# this ends the data simulation part
# --------------------------------------------------------

def create_table(data):
    # Create the table
    table = Texttable(max_width=0)
    table.set_deco(Texttable.HEADER)
    table.set_cols_align(["l", "c", "c"])
    table.add_rows( records(data) )

    # split, remove the underlining below the header
    # and pull together again. Many ways of cleaning this...
    tt = table.draw().split('\n')
    del tt[1] # remove the line under the header
    tt = '\n'.join(tt)
    return tt

if __name__ == '__main__':
    data = initialise_dataset()
    table = create_table(data)
    print table

rawDescription: Writing to separate columns instead of comma seperated for csv files in scrapy


code:
import nltk
feature_vectors =[
({'drama': True, 'mystery': True, 'horror': True, 'psychological': True}, 'relevant'),
({'drama': True, 'fantasy': True, 'romance': True, 'adventure': True, 'science fiction': True}, 'unrelevant')]
classifier = nltk.classify.naivebayes.NaiveBayesClassifier.train(feature_vectors)

doc1 = {'drama': True, 'mystery': True, 'horror': True, 'psychological': True}
prob_dist = classifier.prob_classify(doc1)
print "relevant " + str(prob_dist.prob("relevant"))
print "unrelevant " + str(prob_dist.prob("unrelevant"))
print '----'
caps_doc1 = {'Drama': True, 'Mystery': True, 'Horror': True, 'Psychological':True }
prob_dist = classifier.prob_classify(caps_doc1)
print "relevant " + str(prob_dist.prob("relevant"))
print "unrelevant " + str(prob_dist.prob("unrelevant"))
print '----'

rawDescription: trouble with nltk python NaiveBayesClassifier, I keep getting same probabilities inputs correct?

description: Trouble with nltk python NaiveBayesClassifier, I keep getting same probabilities inputs correct


code:
import os, subprocess, fnmatch

def run_command(command):
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    out, err = p.communicate()
    print out

program = 'C:/Python27/Scripts/lizard.bat'
path = 'path_to_code'
matches = []
for root, dirs, filenames in os.walk(path):
    for filename in fnmatch.filter(filenames, '*.cpp'):
         matches.append(os.path.join(root, filename))

for path1 in matches:
    command = [program, path1]
    run_command(command)

rawDescription: Unable to get output of shell script run from python


code:
import numpy as np
a = np.random.randint(0, 100, 10)
b = np.random.rand(10, 3)

v = np.hstack((a[:,None],b))

import io

t = io.BytesIO()

np.savetxt(t, v, fmt="%g", delimiter=",")
print t.getvalue()

rawDescription: Numpy savetxt heterogenous data

description: Numpy savetxt heterogenous data


code:
with open("myfile.fa","rt") as infile:
    matrix = [list(line.strip()) for line in infile.readlines()]
    print matrix

rawDescription: How to open a text file as a character matrix in python?

description: Open a text file as a character matrix in python


code:
col = [['customer_account', 'CounterPartyID'],
       ['customer_account', 'CounterPartyID', 'symbol'],
       ['customer_account', 'CounterPartyID', 'Providers', 'symbol'],
       ['Providers', 'customer_account'],
       ['Providers', 'symbol']]

for i, col in enumerate(col):
    print col
    print i
    #grouped = df.groupby(col)

    sheetname = 'All Trades-' + str(i)
    print sheetname

#['customer_account', 'CounterPartyID']
#0
#All Trades-0
#['customer_account', 'CounterPartyID', 'symbol']
#1
#All Trades-1
#['customer_account', 'CounterPartyID', 'Providers', 'symbol']
#2
#All Trades-2
#['Providers', 'customer_account']
#3
#All Trades-3
#['Providers', 'symbol']
#4
#All Trades-4

rawDescription: Loop through a pandas dataframe with multiple groupby functions and write to excel


code:
from datetime import date
print date.today().year + 1

rawDescription: How to Print next year from current year in Python


code:
from bisect import bisect_left
from itertools import combinations
from time import time

def loadvars():
  f = open('anadict.txt','r')
  anadict = f.read().split('\n')
  f.close()
  return anadict

scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2,
         "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3,
         "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1,
         "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4,
         "x": 8, "z": 10}

def score_word(word):
  return sum([scores[c] for c in word])

def findwords(rack, anadict):
  rack = ''.join(sorted(rack))
  foundwords = []
  for i in xrange(2,len(rack)+1):
    for comb in combinations(rack,i):
      ana = ''.join(comb)
      j = bisect_left(anadict, ana)
      if j == len(anadict):
        continue
      words = anadict[j].split()
      if words[0] == ana:
        foundwords.extend(words[1:])
  return foundwords

if __name__ == "__main__":
  import sys
  if len(sys.argv) == 2:
    rack = sys.argv[1].strip()
  else:
    print """Usage: python cheat_at_scrabble.py <yourrack>"""
    exit()
  t = time()
  anadict = loadvars()
  print "Dictionary loading time:",(time()-t)
  t = time()
  foundwords = set(findwords(rack, anadict))
  scored = [(score_word(word), word) for word in foundwords]
  scored.sort()
  for score, word in scored:
    print "%d\t%s" % (score,word)
  print "Time elapsed:", (time()-t)

rawDescription: How can this Python Scrabble word finder be made faster?


code:
d = {}
for s,i in seq:
    d.setdefault(i, []).append(s)
print d

rawDescription: Python Get row in a list by one of its attributes


code:
from collections import deque

prices = [7.0, 9.0, 5.0, 1.0, 3.0]
moving_average = deque()
total = 0.0
days = 3
averages = []

for price in prices:
    moving_average.append(price)
    total += price

    if len(moving_average) > days:      # Length of moving average
        total -= moving_average.popleft()

    averages.append(total / float(len(moving_average)))

print averages

rawDescription: moving average of 3 elements by C or Python

description: Moving average of 3 elements by C or Python


code:
#main.py
import os
import config

print config.__file__
print os.path.basename(config.__file__)
print os.path.dirname(config.__file__)

rawDescription: Getting all hard-coded paths out of the code, how to handle the config file?


code:
from string import ascii_lowercase
import itertools

def iter_all_strings():
    size = 1
    while True:
        for s in itertools.product(ascii_lowercase, repeat=size):
            yield "".join(s)
        size +=1

for s in iter_all_strings():
    print s
    if s == 'bb':
        break

rawDescription: How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

description: Make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)


code:
class Meta(type):
    def __str__(self):
        return "Klass"

class A(object):
    __metaclass__ = Meta

    def __str__(self):
        return "instance"

print A
print A()

rawDescription: Python overriding class (not instance) special methods


code: print map(id,[ [] ]*10)

rawDescription: 2D Python list will have random results

description: 2D Python list will have random results


code:
from collections import Counter
from itertools import chain

dicts = [
    {1: 'url1', 3: 'url2', 7: 'url3', 5: 'url4'},
    {1: 'url1', 7: 'url3'},
    {5: 'url4', 10: 'url5'},
]

result = Counter(chain.from_iterable(d.items() for d in dicts))
result = {k:[n,v] for ((k,v),n) in result.items()}

print dict(result)

rawDescription: How to count number of repeated keys in several dictionaries?

description: Count number of repeated keys in several dictionaries


code:
print np.vstack((a, b)).shape
# (6, 2)

rawDescription: understanding numpy's dstack function

description: Understanding numpy's dstack function


code:
class allnames(ast.NodeVisitor):
  def visit_Module(self, node):
    self.names = set()
    self.generic_visit(node)
    print sorted(self.names)
  def visit_Name(self, node):
    self.names.add(node.id)

allnames().visit(t)

rawDescription: Simple example of how to use ast.NodeVisitor?


code:
l = range(1,16+1)
l.append(l.pop(0))
print l

rawDescription: Python - loop through list in specific order


code:
self.sshobj = paramiko.SSHClient()
self.sshobj.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    self.sshobj.connect("192.168.0.1", port=22, look_for_keys=False, timeout=None, banner_timeout=5)
except paramiko.ssh_exception.SSHException, se:
    # paramiko raises SSHException('No authentication methods available',) since we did not specify any auth methods. socket stays open.
    pass
ts_start = time.time()
try:
    self.channel = self.sshobj.invoke_shell()
except EOFError, e:
    # EOFError is raised when peer terminates session.
    pass
print time.time()-ts_start

rawDescription: Paramiko check login timeout of SSH Server


code:
num = raw_input("Please enter number: ")
for i in range(num):
   print "*" * i

rawDescription: I am new to python programming and I have an assignment in which I have to print a certain pattern of stars using a function


code:
def random_grid(file):
    grid = []
    num_rows = raw_input("How many raws would you like in your grid? ")
    num_columns = raw_input("How many columns would you like in your grid? ")
    min_range = raw_input("What is the minimum number you would like in your grid? ")
    max_range = raw_input("what is the maximum number you would like in your grid? ")
    for row in range(int(num_rows)):
        grid.append([])
        for column in range(int(num_columns)):
            grid[row].append(random.randint((int(min_range)),(int(max_range))))
    x = ""
    for row in grid:
        x += (' '.join([str(x) for x in row])) + "\n"
        print x

    with open(r"test.txt", 'w') as text_file:
        text_file.write(x)

rawDescription: Python list to txt

description: Python list to txt


code:
class EarlyExit(Exception):
    def __init__(self, result):
        Exception.__init__(self)
        self.result = result

class CustomDispatcher:
    def __init__(self, pool):
        from collections import deque
        self.pool = pool
        self.q = deque()

    def queue_work(self, *args):
        while len(self.q) > NUM_PROCESSES * 3:
            # provided the workers have significant work to do,
            # it will "take a long time" to finish the work
            # already queued.  Rather than swamp the mp machinery
            # with even more pending tasks, wait for some to
            # finish first.
            self.unqueue()
        self.q.append(self.pool.apply_async(string_gen, args))

    def unqueue(self):
        if self.q:
            # note:  the main program spends most of its time
            # blocked on the .get(); that's because it can
            # generate prefixes far faster than workers can
            # process them
            result = self.q.popleft().get()
            print ".",
            if result is not None:
                print
                raise EarlyExit(result)

    def drain(self):
        while self.q:
            self.unqueue()

def do_work(dispatch, strings_per_chunk=10000000):
    # What's the most chars we can cycle through without
    # exceeding strings_per_chunk?
    N = len(CHARSET)
    suffix_len = 1
    while N**suffix_len <= strings_per_chunk:
        suffix_len += 1
    suffix_len -= 1
    print "workers will cycle through the last", suffix_len, "chars"
    print "so each dot represents", \
          format(len(CHARSET)**suffix_len, ","), "strings"

    # There's no point to splitting up very short strings.
    max_short_len = min(suffix_len, MAX_LENGTH)
    for length in range(1, max_short_len + 1):
        dispatch.queue_work("", suffix_len, length)
    # And now the longer strings.
    for length in range(max_short_len + 1, MAX_LENGTH + 1):
        for t in product(*make_bases(length - suffix_len)):
            dispatch.queue_work("".join(t), suffix_len, length)
    dispatch.drain()  # check remaining tasks for a winner

if __name__ == "__main__":
    import multiprocessing
    pool = multiprocessing.Pool(NUM_PROCESSES)
    dispatch = CustomDispatcher(pool)
    try:
        do_work(dispatch)
    except EarlyExit as e:
        print "the plaintext is", repr(e.result)
    else:
        print "didn't crack it!"
    pool.close()
    pool.terminate() # stop all workers! we're done
    pool.join()

rawDescription: Python Parallel hash bruteforce

description: Python Parallel hash bruteforce


code:
import csv
import networkx as nx
g = nx.DiGraph()
data_csv = open('Data.csv')
csv_f = csv.reader(data_csv,delimiter=';')
prev_orderID = -1
prev_machineID = -1
for row in csv_f:
    if row[0] == prev_orderID:
        g.add_edge(prev_machineID,row[1])
    prev_orderID = row[0]
    prev_machineID = row[1]
print g.edges()

rawDescription: Create edges in networkx out of csv-File


code:
from itertools import product
nested = product(product(Colour, Type), repeat=2)
print list(nested)
# [(('Black', 'Car'), ('Black', 'Car')),
#  (('Black', 'Car'), ('Black', 'Bike')),
#  ...
# ]

rawDescription: Listing out binary combinations and mapping to data


code:
TeamA_All_Scores = [] # list for all scores
TeamB_All_Scores = [] # list for all scores

TeamAWins = 0
TeamBWins = 0
NoWins = 0

for i in range(100):
    LuckA = random.randrange(0,25)
    LuckB = random.randrange(0,25)

    ScoreA = (PowerRankA + WinPercA + LuckA) / 3
    ScoreB = (PowerRankB + WinPercB + LuckB + HomeBonus) / 3

    TeamA_All_Scores.append(ScoreA)
    TeamB_All_Scores.append(ScoreB)

    if ScoreA > ScoreB:
        TeamAWins += 1
    elif ScoreA < ScoreB:
        TeamBWins += 1
    else:
        NoWins += 1

TeamAScore = sum(TeamA_All_Scores) / 100.0
TeamBScore = sum(TeamB_All_Scores) / 100.0

print "Avg. Score for TeamA:", TeamAScore, " number of Wins:", TeamAWin
print "Avg. Score for TeamB:", TeamBScore, " number of Wins:", TeamBWin
print "No Wins:", NoWins
print "TeamA All Scores:", ",".join(TeamA_All_Scores)
print "TeamB All Scores:", ",".join(TeamB_All_Scores)

rawDescription: Python loop while collecting data

description: Python loop while collecting data


code:
print dict((k, len(v)) for k, v in counts.items())
# prints {1: 1, 2: 2}

rawDescription: Number of different values assoicated with a key in a list of dicts


code:
def is_((pbc1, pbc2)):
    thistype = pairtype(SomePBC, SomePBC)
    s = super(thistype, pair(pbc1, pbc2)).is_()
    if not s.is_constant():
        if not pbc1.can_be_None or not pbc2.can_be_None:
            for desc in pbc1.descriptions:
                if desc in pbc2.descriptions:
                    break
            else:
                s.const = False    # no common desc in the two sets
    return s

rawDescription: Pythonic ways to use 'else' in a for loop


code: re.sub(ur'[\W_]+', u'', u'a_b A_Z \x80\xFF \u0404', flags=re.UNICODE)

rawDescription: Python remove anything that is not a letter or number


code:
class A:
    def foo(self):
        print self

a = A()
b = a
c = b
a,b,c    #all of them point to the same instance object

rawDescription: How to get the object name from within the class?

description: Get the object name from within the class


code:
import sys
print sys.argv[0] # prints python_script.py
print sys.argv[1] # prints var1
print sys.argv[2] # prints var2

rawDescription: Call Python script from bash with argument


code:
for fileName in os.listdir('.'):
    if not fnmatch.fnmatch(fileName, '*.py'):
        print fileName

rawDescription: Any equivalent of * style in Bash for acting on mutliple files NOT matching the same pattern


code: print s[::-1].idxmax()

rawDescription: Find index of last true value in pandas Series or DataFrame


code:
for mapping in AttributesMapping.objects.get(product=product):
    print mapping.attribute.name, mapping.value

rawDescription: Django ManyToMany relation QuerySet using custom intermediate table


code:
from bs4 import BeautifulSoup
import re

txt = """
Hi Mark, how are you?, Don't forget meeting on sunday, ok?
"""
soup = BeautifulSoup(txt)
for p in soup.find_all('p'):
    p.string = ''.join(
        [str(t).upper()
            if not re.match(r'<[^>]+>', str(t))
            else str(t)
            for t in p.contents])

print soup.prettify(formatter=None)

rawDescription: Change content only to parent element text, in Beautifulsoup


code:
class Runner(object):
    def __init__(self, start):
        self.start = start
        print start.__doc__
        print start.code

rawDescription: Trouble printing a variable

description: Trouble printing a variable


code:
from functools import update_wrapper
import types

class class_or_instance(object):
    # having optional func in case is passed something that doesn't have a correct __name__
    # (like a lambda function)
    def __init__(self, name_or_func):
        self.fn = fn
        self.name = fn.__name__

    def __get__(self, obj, cls):
        print "GET!!!"
        if obj is not None:
            f = lambda *args, **kwds: self.fn(obj, *args, **kwds)
            update_wrapper(f, self.fn)
            setattr(obj, self.name, types.MethodType(f, obj, obj.__class__))
        else:
            f = lambda *args, **kwds: self.fn(cls, *args, **kwds)
            update_wrapper(f, self.fn)
        return f

rawDescription: Python - using decorator.py to preserve a method docstring


code:
def get_section_line(f):
  iterator = enumerate(f)
  for lno, line in iterator:
    if line.startswith("/*!"):
      yield lno, line.replace("/*!", "", 1)
      for lno, line in iterator:
        if line.startswith('*'):
          break
        yield lno, line

with open('inputfile.txt') as f:
  for lno, line in get_section_line(f):
    print "%04d %s"%(lno,line.rstrip('\n'))

rawDescription: peak next character in a file

description: Peak next character in a file


code:
from xml.etree import ElementTree
import re

def proper_SN(elem):
    if re.search('\d{6}', elem.text):
        return True
    return False

tree = ElementTree.parse('data.xml')
rows = tree.getiterator('SerialNumber')
for row in rows:
    print "SerialNumber: %s Passed = %s" % (row.text, proper_SN(row))

rawDescription: Checking XML Nodes with Script using REGEX


code:
import subprocess
subprocess.Popen("sleep 4s && echo right thar, right thar",shell=True);
print 'i like it when you put it'

rawDescription: Run command pipes with subprocess.Popen

description: Run command pipes with subprocess.Popen


code:
word = 'COMPUTER'
print sum([ord(c) for c in word])

rawDescription: Adding ASCII Values together. Jython/Python


code:
import inspect
def foo(a='spam', b='foo', *args):
    print a, b, args

defaults = inspect.getargspec(foo).defaults
foo(*(defaults + (1,2,3)))

rawDescription: Is there a way to access varargs without specifying default value arguments in Python


code:
import random
a = range(10)
b = scrambled(a)
print a, b

rawDescription: Scramble Python List

description: Scramble Python List


code:
class ReturnTypeWrapper(type):
    def __new__(mcs, name, bases, dct):
        cls = type.__new__(mcs, name, bases, dct)
        for attr, obj in cls.wrapped_base.__dict__.items():
            # skip 'member descriptor's and overridden methods
            if type(obj) == type(complex.real) or attr in dct:
                continue
            if getattr(obj, '__objclass__', None) is cls.wrapped_base:
                setattr(cls, attr, cls.return_wrapper(obj))
        return cls

    def return_wrapper(cls, obj):
        def convert(value):
            return cls(value) if type(value) is cls.wrapped_base else value
        def wrapper(*args, **kwargs):
            return convert(obj(*args, **kwargs))
        wrapper.__name__ = obj.__name__
        return wrapper

class Complex(complex):
    __metaclass__ = ReturnTypeWrapper
    wrapped_base = complex
    def __str__(self):
        return '({0}, {1})'.format(self.real, self.imag)
    def __repr__(self):
        return '{0}({1!r}, {2!r})'.format(self.__class__.__name__,
                                          self.real, self.imag)

a = Complex(1+1j)
b = Complex(2+2j)

print type(a + b)

rawDescription: Using metaclasses to override methods of complex builtin


code:
block = re.compile(r'^\s*define_block.*?^\s*end_block[^$]*', re.I | re.M | re.S)
input = block.sub('', input)
for line in input.split('\n'):
    if line.lower().startswith('variable'):
        print line

rawDescription: How to use Python Lepl to parse a subset of a domain specific language?

description: Use Python Lepl to parse a subset of a domain specific language


code: print ax.get_children()

rawDescription: Change Marker Style/Color in Python Probplot


code:
import win32com.shell.shell as shell
import win32event
#fMask = SEE_MASK_NOASYNC(0x00000100) = 256 + SEE_MASK_NOCLOSEPROCESS(0x00000040) = 64
dict = shell.ShellExecuteEx(fMask = 256 + 64, lpFile='Notepad.exe', lpParameters='Notes.txt')
hh = dict['hProcess']
print hh
ret = win32event.WaitForSingleObject(hh, -1)
print ret

rawDescription: How to determine if win32api.ShellExecute was successful using hinstance?

description: Determine if win32api.ShellExecute was successful using hinstance


code:
if __name__=='__main__':
    # these variables would normally be called in from a config file
    job_type = 'job1'
    uni_var = 10

    job_select = superClass.optionFactory(job_type, uni_var)
    job_select.job()

class superClass(object):
    def __init__(self, job_type, uni_var):
        self.job_type = job_type
        self.uni_var = uni_var

    # This is the definition called by the main function it then
    # redirects the request to the appropriate job sub class
    def job(self):
        raise NotImplementedError()

    @staticmethod
    def optionFactory(job_type, uni_var):
        "Return an instance of superClass based on job_type and uni_var."
        if job_type == "job1":
            return option1(job_type, uni_var)
        else:
            return option2(job_type, uni_var)

class option1(superClass):
    def __init__(self, job_type, uni_var):
        super(option1, self).__init__(job_type, uni_var)

    def job(self):
        print 'job option 1'
        print uni_var

class option2(superClass):
    def __init__(self, job_type, uni_var):
        super(option2, self).__init__(job_type, uni_var)

    def job(self):
        print 'job option 2'
        print uni_var

rawDescription: correct class inheritance

description: Correct class inheritance


code:
# ZSCAN: Iterate over all members in the set in batches of about 10.
# This will be unordered list.
# zscan_iter returns tuples (member, score)
batchSize = 10
for memberTuple in r.zscan_iter(key, match = None, count = batchSize):
    member = memberTuple[0]
    score = memberTuple[1]
    print str(member) + ' --> ' + str(score)

# ZRANGE: Get all members in the set, ordered by score.
# Here there maxRank=-1 means "no max".
minRank = 0
maxRank = -1
for memberTuple in r.zrange(key, minRank, maxRank, desc = False, withscores = True):
    member = memberTuple[0]
    score = memberTuple[1]
    print str(member) + ' --> ' + str(score)

rawDescription: Redis as a Queue - Bulk Retrieval

description: Redis as a Queue - Bulk Retrieval


code:
list = [u'ABC', u'DEF', u'GHI']
list = str(map(str, list))

list = list.replace('[', '{').replace(']', '}').replace('\'', '\"')

query = '''update "aTable" SET "Test" = '%s\'''' %(list)
print query

rawDescription: Python List to PostgreSQL Array

description: Python List to PostgreSQL Array


code:
param = ["selection_Size", "selection_Loc", "selection_Mov", "error"]
for par in param:
    print par, placement.__dict__.get(par, None)

    # this should work too
    # print par, getattr(placement, par, None)

rawDescription: Retrieving Values from Module by Name?

description: Retrieving Values from Module by Name


code:
import subprocess
from threading import Thread
import time

linebuffer=[]
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)

def reader(f,buffer):
   while True:
     line=f.readline()
     if line:
        buffer.append(line)
     else:
        break

t=Thread(target=reader,args=(x.stdout,linebuffer))
t.daemon=True
t.start()

while True:
  if linebuffer:
     print linebuffer.pop()
  else:
     print "nothing here"
     time.sleep(1)

rawDescription: Python: How to read stdout of subprocess in a nonblocking way


code:
print x.parent
print x.parent.parent
print x.parent.parent.parent

rawDescription: Extracting content of div with BeautifulSoup


code:
def foo():
    print 'foo'

rawDescription: Finding a module's directory

description: Finding a module's directory


code:
#!/usr/bin/python
print "Hello"

rawDescription: Setting alternative version of python in script


code:
def sumPair(theList, n):
    for index, e in enumerate(theList):     # to know the index for the slicing below
        complement = n - e                  # we are searching for the complement
        if complement in theList[index+1:]: # only the rest is searched
            return e, complement

print sumPair([6,3,6,8,3,2,8,3,2], 11)

rawDescription: One loop? python

description: One loop? python


code:
import itertools
import json
import re
import urllib2

text = urllib2.urlopen('http://dcsd.nutrislice.com/menu/meadow-view/lunch/').read()
menus = json.loads(re.search(r"bootstrapData\['menuMonthWeeks'\]\s*=\s*(.*);", text).group(1))

days = itertools.chain.from_iterable(menu['days'] for menu in menus)

day = next(itertools.dropwhile(lambda day: day['date'] != '2014-01-13', days), None)

if day:
    print '\n'.join(item['food']['description'] for item in day['menu_items'])
else:
    print 'Day not found.'

rawDescription: Parse the JavaScript returned from BeautifulSoup


code:
headers = next(reader)
print row[0], row[1], row[9], row[4], row[6], row[5], row[19]

for row in reader:
    if row[1]=="7/1/2000" or row[1]=="7/1/2010":
        print row[0], row[1], row[9], row[4], row[6], row[5], row[19]

rawDescription: manipulating TSV file

description: Manipulating TSV file


code:
versions = reversion.get_for_object(MyModel)

for version in versions:
    print '%s made this revision' % version.revision.user.username

rawDescription: django-reversion how to get user-id

description: Django-reversion how to get user-id


rawDescription: Composite a numpy array/matrix based on column values and variables?


code:
if hasattr(self, s):
    print getattr(self, s)

rawDescription: In Python, how do I get a variable via a string representation of the name of the variable?


code:
class Filter(QtCore.QObject):
    def eventFilter(self, widget, event):
        # FocusOut event
        if event.type() == QtCore.QEvent.FocusOut:
            # do custom stuff
            print 'focus out'
            # return False so that the widget will also handle the event
            # otherwise it won't focus out
            return False
        else:
            # we don't care about other events
            return False

rawDescription: How to connect QLineEdit focusOutEvent

description: Connect QLineEdit focusOutEvent


code:
from string import Template
the_template = Template("
First name: $first
")
print the_template.substitute(section_id="anID", first="Sarah")

rawDescription: Python inline of XML or ASCII string/template?


code:
pdfname = "statementConcentrator"
lineNum = 0
count = 0
for line in open("line56.good").readlines():
    lineNum = lineNum + 1
    if pdfname in line:
        print "#%5d: %s"%(lineNum, line[:-1])
        count = count + 1
print "Found on %d line(s)."%(count)

rawDescription: find a matching line in a txt with python

description: Find a matching line in a txt with python


code:
class callonce(object):

    def __init__(self, f):
        self.f = f
        self.called = False

    def __call__(self, *args, **kwargs):
        if not self.called:
            self.called = True
            return self.f(*args, **kwargs)
        print 'Function already called once.'

rawDescription: How to make a few lines of function execute only once in python? preferably using lambda?

description: Make a few lines of function execute only once in python? preferably using lambda


code:
def play(self):
    next_room_name = self.start

    while True:
        print "\n--------"
        room = getattr(self, next_room_name)
        next_room_name = room()

rawDescription: Please help understanding how the 'return' statement is used in this example


code:
def main():
    print "Hello world, says myscript!"
    # wait for input from the user
    raw_input()

if __name__ == '__main__':
    main()

rawDescription: debianzing a python program to get a .deb

description: Debianzing a python program to get a .deb


code:
for line in b0rken:
    print line.rstrip('\r\n').decode('unicode_escape')

rawDescription: Read Unicode from CSV

description: Read Unicode from CSV


code:
import time
def main():
    print [i for i in range(0,100)]

start_time = time.clock()
main()
print time.clock() - start_time, "seconds"

rawDescription: Counting Time of code running Python

description: Counting Time of code running Python


code:
import threading
import time

def print_num(dummyid, n):
    print "From %s: %d" % (dummyid, n)
def dummy_run(dummyid):
    for i in xrange(5):
        from_dummy_thread(lambda: print_num(dummyid, i))
        time.sleep(0.5)

threading.Thread(target=dummy_run, args=("a",)).start()
threading.Thread(target=dummy_run, args=("b",)).start()

while True:
    from_main_thread_blocking()

rawDescription: Execute Python function in Main thread from call in Dummy thread


code:
import pystache
template = u'''
  {{greeting}}
   Property 1 is {{information.property1}}
   Property 2 is {{information.property2}}
'''

context={
    'greeting':'Hello',
    'information':{
         'property1':'bob',
         'property2':42
    }
}
print pystache.render(template,context)

rawDescription: mustache/pystache: Rendering complex objects

description: Mustache/pystache: Rendering complex objects


code:
@classmethod
def meth(cls): print cls

rawDescription: How can I get the name of a child class?

description: Get the name of a child class


code: print dict(zip(keys, [list(i) for i in zip(*data)]))

rawDescription: Add items to a dictionary of lists

description: Add items to a dictionary of lists


code:
def print_matrix(matrix):
    print "-" + "------------"*len(matrix[0])
    for i in range(len(matrix)):
        print "|",
        for j in range(len(matrix[i])):
            print "{0:9} |".format(matrix[i][j]),
        print
    print "-" + "------------"*len(matrix[0])

rawDescription: Populating a matrix with random numbers and computing sums of those numbers


code:
F = compose(f1, f2, f3)
print F('x')

rawDescription: Can't figure this function composition code


code:
import httplib2
from bs4 import BeautifulSoup

http = httplib2.Http()
with open('test_1.txt', 'r') as f:
    for mapcalc in f:
        status, response = http.request(mapcalc)
        for leg in BeautifulSoup(response):
            duration = leg.duration.text.strip()
            distance = leg.distance.text.strip()
            start = leg.start_address.text.strip()
            end = leg.end_address.text.strip()
            print duration
            print distance
            print start
            print end

f.close()

rawDescription: Iterating through list of URLs in Python - bs4


code:
lastSize = 0
lastLineIndex = 0
while True:
    wx.Yield()
    fileSize = os.path.getsize(logPath)
    if fileSize > lastSize:
        lines = infile.readlines()
        newLines = 0
        for line in lines[lastLineIndex:]:
            newLines += 1
            print line.rstrip()
        lastLineIndex += newLines

        if "DBG-X: Returning 1" in line:
            break
        if "DBG-X: Returning 0" in line:
            break
    fileSize1 = fileSize
    infile.seek(0)

rawDescription: Wait for text file to be written, read changes line by line and print


code:
print repr(unicode('a'))
unicode('a')

rawDescription: Suppress the u'prefix indicating unicode' in python strings


code:
vl_list = []
conn=boto.ec2.connect_to_region("us-west-1")
result=collections.defaultdict(list)
reservations = conn.get_all_instances()
for res in reservations:
        for inst in res.instances:
                if 'clusters' in inst.tags:
                        values = inst.tags['clusters']
                        vl_list.extend(values.split(","))
                        print values

rawDescription: Split values and store into a list

description: Split values and store into a list


code: print "{:g}".format(1.0)

rawDescription: Python and Floats - Print only the Whole Number


code:
readNames()
print readNames()

rawDescription: Loop will not break

description: Loop will not break


code:
import argparse

class MyHelpFormatter(argparse.HelpFormatter):
    def __init__(self, *args, **kwargs):
        super(MyHelpFormatter, self).__init__(*args, **kwargs)

    def _format_usage(self, usage, actions, groups, prefix):
        return super(MyHelpFormatter, self)._format_usage(
            usage, actions, groups, prefix if prefix else "bla: ")

class MyArgumentParser(argparse.ArgumentParser):
    def __init__(self, *args, **kwargs):
        kwargs['formatter_class']=MyHelpFormatter
        super(MyArgumentParser, self).__init__(*args, **kwargs)

p = MyArgumentParser(description='Foo')
p.add_argument('foo', type=str)
print p.parse_args()

rawDescription: Translate argparse's internal strings

description: Translate argparse's internal strings


code:
list=[[{'identityb32': 'qa46imyg4njipjsgpudn7vnn2zub2tec', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '18.238.2.85', 'orport': '443', 'identityhash': '8039e43306e35287a6467d06dfd5add6681d4c82', 'nick': 'Onions', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x809\xe43\x06\xe3R\x87\xa6F}\x06\xdf\xd5\xad\xd6h\x1dL\x82', 'digest': 'lh2LprFLd0bdvtIfPkz58//mgZk', 'pubtime': '09:37:21'}, {'identityb32': 'qbwfsrn3y5mmcnshotg77clvxjufngso', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '109.120.148.60', 'orport': '443', 'identityhash': '806c5945bbc758c1364774cdff8975ba68569a4e', 'nick': 'Reactor', 'version': 'Tor 0.2.4.22', 'flags': ['Exit', 'Fast', 'HSDir', 'Running', 'V2Dir', 'Valid'], 'identity': '\x80lYE\xbb\xc7X\xc16Gt\xcd\xff\x89u\xbahV\x9aN', 'digest': 'bDAS/RN7q4injGzkIXobcsSwS6c', 'pubtime': '08:55:57'}, {'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '195.154.12.66', 'orport': '9001', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'pubtime': '01:09:22'}], [{'identityb32': 'rzjbswyebloufopae2j46a6272wrwlae', 'pubdate': '2014-07-07', 'dirport': '80', 'ip': '77.247.181.164', 'orport': '443', 'identityhash': '8e52195b040add42b9e02693cf03dafead1b2c04', 'nick': 'rainbowwarrior', 'version': 'Tor 0.2.5.3-alpha', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8eR\x19[\x04\n\xddB\xb9\xe0&\x93\xcf\x03\xda\xfe\xad\x1b,\x04', 'digest': '67A4paMg3mQUqHei8jNSWR2QWsg', 'pubtime': '08:29:31'}, {'identityb32': 'rzniakcq6yj3xdbk766e4yuspkhm2bq2', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '212.116.188.158', 'orport': '9001', 'identityhash': '8e5a802850f613bb8c2affbc4e62927a8ecd061a', 'nick': '7sYiz', 'version': 'Tor 0.2.3.25', 'flags': ['Exit', 'Fast', 'Guard', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x8eZ\x80(P\xf6\x13\xbb\x8c*\xff\xbcNb\x92z\x8e\xcd\x06\x1a', 'digest': '5+O3G9AMJujmUVxO3Fhz7MSPpHI', 'pubtime': '12:34:28'}, {'identityb32': 'rzs3murcnwhdeyj6blsfj7ipltbpmn44', 'pubdate': '2014-07-07', 'dirport': '9030', 'ip': '176.31.122.216', 'orport': '9001', 'identityhash': '8e65b652226d8e32613e0ae454fd0f5cc2f6379c', 'nick': 'somewhere', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Running', 'Stable', 'Unnamed', 'V2Dir', 'Valid'], 'identity': '\x8ee\xb6R"m\x8e2a>\n\xe4T\xfd\x0f\\\xc2\xf67\x9c', 'digest': 'lEgmmiJcOvzsib9fZjgaxaSG6to', 'pubtime': '03:27:26'}]]
d = {}
for dic in l[0]:
    d.update(dic)
print d
{'identityb32': 'qcrds2ps562vf3ctnuoob4lgtmvt4zau', 'pubdate': '2014-07-07', 'ip': '195.154.12.66', 'orport': '9001', 'digest': 'FoPXNW58WS4eoCWB/eIK7j5yS4E', 'dirport': '9030', 'identityhash': '80a23969f2efb552ec536d1ce0f1669b2b3e6414', 'nick': 'han1', 'version': 'Tor 0.2.4.22', 'flags': ['Fast', 'HSDir', 'Named', 'Running', 'Stable', 'V2Dir', 'Valid'], 'identity': '\x80\xa29i\xf2\xef\xb5R\xecSm\x1c\xe0\xf1f\x9b+>d\x14', 'pubtime': '01:09:22'}

rawDescription: Extracting data from a list

description: Extracting data from a list


code:
def partition_list(a, k):
    if k <= 1: return [a]
    if k >= len(a): return [[x] for x in a]
    partition_between = [(i+1)*len(a)/k for i in range(k-1)]
    average_height = float(sum(a))/k
    best_score = None
    best_partitions = None
    count = 0

    while True:
        starts = [0]+partition_between
        ends = partition_between+[len(a)]
        partitions = [a[starts[i]:ends[i]] for i in range(k)]
        heights = map(sum, partitions)

        abs_height_diffs = map(lambda x: abs(average_height - x), heights)
        worst_partition_index = abs_height_diffs.index(max(abs_height_diffs))
        worst_height_diff = average_height - heights[worst_partition_index]

        if best_score is None or abs(worst_height_diff) < best_score:
            best_score = abs(worst_height_diff)
            best_partitions = partitions
            no_improvements_count = 0
        else:
            no_improvements_count += 1

        if worst_height_diff == 0 or no_improvements_count > 5 or count > 100:
            return best_partitions
        count += 1

        move = -1 if worst_height_diff < 0 else 1
        bound_to_move = 0 if worst_partition_index == 0\
                        else k-2 if worst_partition_index == k-1\
                        else worst_partition_index-1 if (worst_height_diff < 0) ^ (heights[worst_partition_index-1] > heights[worst_partition_index+1])\
                        else worst_partition_index
        direction = -1 if bound_to_move < worst_partition_index else 1
        partition_between[bound_to_move] += move * direction

def print_best_partition(a, k):
    print 'Partitioning {0} into {1} partitions'.format(a, k)
    p = partition_list(a, k)
    print 'The best partitioning is {0}\n    With heights {1}\n'.format(p, map(sum, p))

a = [1, 6, 2, 3, 4, 1, 7, 6, 4]
print_best_partition(a, 1)
print_best_partition(a, 2)
print_best_partition(a, 3)
print_best_partition(a, 4)

b = [1, 10, 10, 1]
print_best_partition(b, 2)

import random
c = [random.randint(0,20) for x in range(100)]
print_best_partition(c, 10)

d = [95, 15, 75, 25, 85, 5]
print_best_partition(d, 3)

rawDescription: Split a list of numbers into n chunks such that the chunks have (close to) equal sums and keep the original order


code:
def pathToDagNode( fullPath ):
    if not cmds.objExists(fullPath):
        return None
    else:
        selectionList = om.MSelectionList()
        selectionList.add( fullPath )
        dagPath = om.MDagPath()
        selectionList.getDagPath( 0, dagPath )
        return dagPath

dag_node = pathToDagNode( '|your|node|full|path' )
print pathToDagNode.isInstanced()

rawDescription: Maya find if an object is instanced?

description: Maya find if an object is instanced


code:
pMerge = subprocess.Popen(mp4merge, stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
stdout, stderr = pMerge.communicate()
print stdout
print stderr

rawDescription: Python subprocess + mencoder not working, same command works in terminal


code:
import os.path
import tornado.httpserver
import tornado.websocket
import tornado.ioloop
import tornado.web

# websocket
class FaviconHandler(tornado.web.RequestHandler):
    def get(self):
        self.redirect('/static/favicon.ico')

class WebHandler(tornado.web.RequestHandler):
    def get(self):
        self.render("websockets.html")

class WSHandler(tornado.websocket.WebSocketHandler):
    def open(self):
        print 'new connection'
        self.write_message("Hi, client: connection is made ...")
        tornado.ioloop.IOLoop.instance().add_timeout(datetime.timedelta(seconds=5), self.test)

    def on_message(self, message):
        print 'message received: \"%s\"' % message
        self.write_message("Echo: \"" + message + "\"")
        if (message == "green"):
            self.write_message("green!")

    def on_close(self):
        print 'connection closed'

    def test(self):
        self.write_message("scheduled!")

handlers = [
    (r"/favicon.ico", FaviconHandler),
    (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
    (r'/', WebHandler),
    (r'/ws', WSHandler),
]

settings = dict(
    template_path=os.path.join(os.path.dirname(__file__), "static"),
)

application = tornado.web.Application(handlers, **settings)

import datetime

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

rawDescription: How to run functions outside websocket loop in python (tornado)

description: Run functions outside websocket loop in python (tornado)


code:
import itertools
for l1, l2 in itertools.combinations(list_a[0], 2):
    for e1, e2 in itertools.product(l1, l2):
        print e1, e2

rawDescription: Finding matches in a Python multi-dimensional list


code:
for line in f:
    if not line: continue
    print line
    print testing.searchString(line).asList()
    print

rawDescription: PyParsing : how to use SkipTo and OR(^) operator


code:
allInstancesOfFoo = []

class foo(object):
    def __init__(self):
        allInstancesOfFoo.append(self)

bar1=foo()
bar2=foo()
bar3=foo()
print bar1 is allInstancesOfFoo[0]  #True

rawDescription: Will adding an instance of a class to a list copy the instance or just 'reference' it?


code:
import ast
with open('foo.txt', 'r') as myfile:
    fileoutput = myfile.read() # Assuming the whole file is just the list

original = ast.literal_eval(fileoutput)
for i in original:
    print i

rawDescription: Is there an easy way to parse a list of tuples generated by python's print, using Python?


code:
#! /usr/bin/env python

''' Unscramble a string of text by brute force

From http://stackoverflow.com/questions/26248379/influence-of-choosing-string-as-seed-of-random-on-the-output

'''

import sys
from random import seed, shuffle
from itertools import product
from string import ascii_letters

def scramble(seq, key):
    seed(key)
    msg = seq[:]
    shuffle(msg)
    return msg

def scramble_old(seq, key):
    seed(key)
    r = range(len(seq))
    shuffle(r)
    return [seq[i] for i in r]

def unscramble(seq, key):
    seed(key)
    r = range(len(seq))
    shuffle(r)
    newseq = len(seq) * [None]
    for i, j in enumerate(r):
        newseq[j] = seq[i]
    return newseq

def test():
    key = 'zap'
    #orig = 'quickbrownfox'
    orig = '01234'

    print 'orig:       ', orig
    shuf = scramble(list(orig), key)
    print 'shuffled:   ', ''.join(shuf)
    unshuf = unscramble(shuf, key)
    print 'unshuffled: ', ''.join(unshuf)

def decode(seq, begin):
    count = 0
    begin_len = len(begin)
    for k in product(*3*[ascii_letters]):
        key = ''.join(k)
        dup = seq[:]
        newseq = unscramble(dup, key)
        if newseq[:begin_len] == begin:
            count += 1
            print '%s: [%s] %s' % (key, ''.join(newseq), count)
            #print '     [%s]\n' % ''.join(scramble(newseq, key))

def main():
    original_str = 'euTtSa:0 kty1h a0  nlradstara  atlot 5wtic'.lower()
    original_list = list(original_str)
    print '     [%s], %d\n' % (original_str, len(original_str))

    decode(original_list, begin=list('the'))

if __name__ == '__main__':
    #test()
    main()

rawDescription: Influence of choosing string as seed of random on the output


code:
for i in range(27):
    if (i > 11) and (i < 16):
        data.next() # keeps data moving forward even when i == 12, 13, 14, 15
    else:
        print "   value %d = %02X" % (i, data.next()) # only prints when i == 0...11, 16...27

rawDescription: How to skip ahead a certain number with .next()

description: Skip ahead a certain number with .next()


code:
c = setValue(derived(), [L1(), L2()])
print c.value

rawDescription: Assigning classes as variables

description: Assigning classes as variables


code:
line = 'a but tuba!'
line = line.replace('!', '').split(' ')
print line

rawDescription: How do I split multiple characters without using any imported libraries?

description: Split multiple characters without using any imported libraries


code:
with open('textA.txt') as f:
    print f.readlines()

rawDescription: .txt file does not print after being written with a join lines command in python


code:
class Scene1(Scene):
    def enter(self):
        print "Some text describing the scene bla bla"
        raw = raw_input("Guess the number")
        if raw != "42":
            return 'Scene1'
        return 'Scene2'

class Engine:
    def __init__(self):
        self.scenes = {'Scene1':Scene1(),'Scene2':Scene2(),'Scene3':Scene3()}
    def run(self):
        next = 'Scene1'
        while True:
            if next == 'finished':
                break;
            next = self.scenes.get(next).enter()

Engine().run()

rawDescription: OOP how to get the script (a game) to run


code:
class A(object):
    def __init__(self):
        print 'A.__init__()'
        super(A, self).__init__()

class B(object):
    def __init__(self):
        print 'B.__init__()'
        super(B, self).__init__()

class C(A, B):
    def __init__(self):
        print 'C.__init__()'
        super(C, self).__init__()

C()

rawDescription: Python super usage example

description: Python super usage example


code:
data = "Library\tSSHClient    with name\tnode"

# replace tabs with 2 spaces
data = data.replace('\t', '  ')

print linedefn.parseString(data)

rawDescription: pyparsing whitespace match issues

description: Pyparsing whitespace match issues


code: print file_path, file_hash

rawDescription: filter list of values in a dictionary

description: Filter list of values in a dictionary


code: for tree in enum_unordered(("a","b","c","d")): print tree

rawDescription: Enumerate all full (labeled) binary tree


code:
def main():
    items = 'shp_folders.txt'

    with open(items, 'r') as f:
        for line in f:
            pathname = line.rstrip()
            print "Zipping %r" % pathname

            # Call function to zip files in pathname
            b = zipShapefile(pathname)
            if b:
                print "Zipping done!"
            else:
                print "An error occurred during zipping."

rawDescription: loop through path list held in txt

description: Loop through path list held in txt


code:
import cPickle
import json
try:
    import cjson  # not Python standard library
except ImportError:
    cjson = False
from collections import defaultdict

dd1 = defaultdict(list)
dd2 = defaultdict(list)

for i in xrange(1000000):
    dd1[str(i % 10)].append(str(i))
    dd2[str(i % 100)].append(str(i))

dt1 = dict(dd1)
dt2 = dict(dd2)

from timeit import timeit

def testdict(dd, dt):
    def serialize_defaultdict():
        with open('defaultdict.pickle', 'w') as f:
            cPickle.dump(dd, f)

    def serialize_p2_defaultdict():
        with open('defaultdict.pickle2', 'w') as f:
            cPickle.dump(dd, f, -1)

    def serialize_dict():
        with open('dict.pickle', 'w') as f:
            cPickle.dump(dt, f)

    def serialize_p2_dict():
        with open('dict.pickle2', 'w') as f:
            cPickle.dump(dt, f, -1)

    def serialize_json():
        with open('dict.json', 'w') as f:
            json.dump(dt, f)

    if cjson:
        def serialize_cjson():
            with open('dict.cjson', 'w') as f:
                f.write(cjson.encode(dt))

    def serialize_flat():
        with open('dict.flat', 'w') as f:
            f.write('\n'.join([' '.join([k] + v) for k, v in dt.iteritems()]))

    def serialize_eval():
        with open('dict.eval', 'w') as f:
            f.write('\n'.join([k + '\t' + repr(v) for k, v in dt.iteritems()]))

    def unserialize_defaultdict():
        with open('defaultdict.pickle') as f:
            assert cPickle.load(f) == dd

    def unserialize_p2_defaultdict():
        with open('defaultdict.pickle2') as f:
            assert cPickle.load(f) == dd

    def unserialize_dict():
        with open('dict.pickle') as f:
            assert cPickle.load(f) == dt

    def unserialize_p2_dict():
        with open('dict.pickle2') as f:
            assert cPickle.load(f) == dt

    def unserialize_json():
        with open('dict.json') as f:
            assert json.load(f) == dt

    if cjson:
        def unserialize_cjson():
            with open('dict.cjson') as f:
                assert cjson.decode(f.read()) == dt

    def unserialize_flat():
        with open('dict.flat') as f:
            dtx = {}
            for line in f:
                vals = line.split()
                dtx[vals[0]] = vals[1:]
            assert dtx == dt

    def unserialize_eval():
        with open('dict.eval') as f:
            dtx = {}
            for line in f:
                vals = line.split('\t')
                dtx[vals[0]] = eval(vals[1])
            assert dtx == dt

    print 'serialize flat:', timeit(serialize_flat, number=10)
    print 'serialize eval:', timeit(serialize_eval, number=10)
    print 'serialize defaultdict:', timeit(serialize_defaultdict, number=10)
    print 'serialize dict:', timeit(serialize_dict, number=10)
    print 'serialize defaultdict new pickle:', timeit(serialize_p2_defaultdict, number=10)
    print 'serialize dict new pickle:', timeit(serialize_p2_dict, number=10)
    print 'serialize json:', timeit(serialize_json, number=10)
    if cjson:
        print 'serialize cjson:', timeit(serialize_cjson, number=10)
    print 'unserialize flat:', timeit(unserialize_flat, number=10)
    print 'unserialize eval:', timeit(unserialize_eval, number=10)
    print 'unserialize defaultdict:', timeit(unserialize_defaultdict, number=10)
    print 'unserialize dict:', timeit(unserialize_dict, number=10)
    print 'unserialize defaultdict new pickle:', timeit(unserialize_p2_defaultdict, number=10)
    print 'unserialize dict new pickle:', timeit(unserialize_p2_dict, number=10)
    print 'unserialize json:', timeit(unserialize_json, number=10)
    if cjson:
        print 'unserialize cjson:', timeit(unserialize_cjson, number=10)

print 'testing with 10 keys...'
testdict(dd1, dt1)

print 'testing with 100 keys...'
testdict(dd2, dt2)

rawDescription: Python defaultdict(list) de/serialization performance


code:
import matplotlib.pyplot as plt
ax = plt.subplot(111)
li = ax.plot([1,2,3,4],[5,6,7,4], 'o-', color='b')
print li[0].get_data()

rawDescription: Gettings point coordinates from matplotlib.pyplot.plot


code:
class Foo:
    info_pat = re.compile("{{persondata(.*)}}",
                                  re.IGNORECASE|re.MULTILINE|re.UNICODE)
    def findPersonInfo(self):
        result = None
        if self.isPerson:
            result = self.info_pat.search(self._rawPage)
        if result:
            print 'Match found: ', result.group()

rawDescription: Python Regex not working to extract information from a wiki page


code:
for p, x in eachWithPrefix(x.split('\\') for x in v):
  s = '\\'.join(x)
  c = '\\'.join(x[:p])
  print ' '*len(c) + s[len(c):]

rawDescription: How to group strings with similar beginnings in a sorted list?

description: Group strings with similar beginnings in a sorted list


code:
# simple demonstration of this wrapper
xkb = SimpleXkbWrapper()
# initialise wrapper for the X Keyboard Extension (v1.0) and
# open connection to default X display
display_name = None
major_in_out = 1
minor_in_out = 0
try:
    ret = xkb.XkbOpenDisplay(display_name, major_in_out, minor_in_out)
except OSError, error:
    print
    print '  Error: %s' % error
    print
    exit(1)
# ... get modifier state of core keyboard ...
display_handle = ret['display_handle']
device_spec = xkb.XkbUseCoreKbd
xkbstaterec = xkb.XkbGetState(display_handle, device_spec)
# ... and extract and the information we need
print xkb.ExtractLocks(xkbstaterec)

rawDescription: Check if key is pressed using python (a daemon in the background)


code:
def metric_block_iter(lines):
  start = find_next_separator(lines, find_header(lines)+1)
  while True:
    end = find_next_separator(lines, start+1)
    if end is None: break
    yield (start, end)
    start = end

if __name__ == '__main__':
  totals = []
  lines = open('so.txt').readlines()

  for (start, end) in metric_block_iter(lines):
    # Pull out block, after line of dashes.
    metrics_block = lines[start+1:end]

    # Pull out 2nd column from 1st line of metrics.
    eng_total = metrics_block[0].split()[1]
    totals.append(eng_total)

  print totals

rawDescription: How to extract data from an irregularly formatted data file in python

description: Extract data from an irregularly formatted data file in python


code:
import sys

rounds = int(sys.stdin.readline())

score = [0,0]
leads = [0,0]
while rounds > 0:
    results = map(int, sys.stdin.readline().split())
    score[0] += results[0]
    score[1] += results[1]
    lead = score[0] - score[1]
    if (lead < 0 and leads[1] < -lead): leads[1] = -lead
    if (lead > 0 and leads[0] < lead): leads[0] = lead
    rounds -= 1

if (leads[0] > leads[1]): print 1, leads[0]
else: print 2, leads[1]

rawDescription: Improving the speed of a Python solution to the Lead Game exercise


code: print type(o)

rawDescription: python __repr__

description: Python __repr__


code:
import pandas as pd

df = pd.DataFrame({
    "col_a":["Larry","Lee"],
    "col_b":["Larry","Lee"],
    "col_a1":["Peter","Jeremy"],
    "col_b1":["Peter","Ilia"]
    })

print df
df["flag"] = df.eval("col_a==col_b and col_a1==col_b1")
print df

rawDescription: Compare multiple sets of columns to produce boolean result column


code:
for key, value in d.items():
    print key, value

rawDescription: Extract a pair with same values in a list of dictionary in python


code:
with open("53903") as fp:
    for message in msgreader(fp):
        print message

rawDescription: pulling subset of lines from files

description: Pulling subset of lines from files


code:
def __init__():
    self.tabClicked.connect(self.addTab)

def addTab():
    # this function is executed ever time you emit signal from `self.tabClicked()`
    print "addTab?"

rawDescription: PyQt4: How/when to emit custom signal from subclass of QTabWidget?


code:
import numpy as np
import scipy.ndimage as ndimage

x = np.array([[1, 0, 1, 2, 2],
              [1, 0, 1, 2, 2],
              [3, 0, 3, 4, 4],
              [3, 0, 3, 4, 4]])

for i, item in enumerate(ndimage.find_objects(x), start=1):
    print i, item

rawDescription: How to select all locations of unique elements in numpy 2d array with bounding box around them?

description: Select all locations of unique elements in numpy 2d array with bounding box around them


code:
from xml.dom import minidom

doc = minidom.parseString(text)
for question in doc.getElementsByTagName('question'):
    for answer in question.getElementsByTagName('answer'):
        if answer.childNodes[0].nodeValue.strip() == '99':
            question.parentNode.removeChild(question)

print doc.toxml()

rawDescription: BeautifulSoup/LXML.html: delete tag and its children if child looks like x


code:
def test(*args):
    for a in args:
        print a,
        yield a

g = test(1,1,1,1,1,1,1)
print all(a == x for a in g for x in g)
g = test(1,1,1,2,1,1,1)
print all(a == x for a in g for x in g)

rawDescription: Lazy way to check if all generated elements are equal


code:
def print_both(file, *args):
    toprint = ' '.join([arg for arg in args])
    print toprint
    file.write(toprint)

rawDescription: Python output on both console and file

description: Python output on both console and file


code:
import urllib2
from lxml import etree
from lxml.cssselect import CSSSelector

url =  "http://www.custojusto.pt/Lisboa?ca=14_s&th=1&q=macbook&cg=0&w=1"
response = urllib2.urlopen(url)
htmlparser = etree.HTMLParser()
tree = etree.parse(response, htmlparser)

my_products = []
# Here, we harvet all the results into a list of dictionaries, containing the items we want.
for product_result in CSSSelector(u'div.lista')(tree):
    # Now, we can select the children element of each div.lista.
    this_product = {
        u'name': product_result.xpath('div[2]/a/h2'),  # first h2 of the second child div
        u'category': product_result.xpath('div[2]/span[1]'),  # first span of the second child div
        u'price': product_result.xpath('div[3]/span/ul/li[1]/span'),  # Third div, span, ul, first li, span tag.
    }
    print this_product.get(u'name')[0].text
    my_products.append(this_product)

# Let's inspect a product result now:
for product in my_products:
    print u'Product Name: "{0}", costs: "{1}"'.format(
        product.get(u'name')[0].text.replace(u'Procura:', u'').strip() if product.get(u'name') else 'NONAME!',
        product.get(u'price')[0].text.strip() if product.get(u'price') else u'NO PRICE!',
    )

rawDescription: python parse html elements while scraping

description: Python parse html elements while scraping


code:
for date, task in load_list('todo.txt'):
    print 'On ' + date.strftime(DATE_FORMAT) + ', do task: ' + task

rawDescription: .split() and .append information from .txt file as tuples to a new list


code:
# only modified function print_solution(n) and solve(n)

def print_solution(n):
    ans = []
    while f(n)[1] != -1:
        ans.append(n)
        n = f(n)[1]
    ans.append(1)
    ans.reverse()
    for x in ans:
        print x,

def solve(n):
    for i in range(1, n):
        f(i)[0]
    print_solution(n)
    print ''

solve(96234) # 1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234

rawDescription: Dynamic programming for primitive calculator


code:
from collections import defaultdict
from urllib import quote
from urlparse import parse_qsl, urlparse

urls = defaultdict(list)
with open('links.txt') as f:
    for url in f:
        parsed_url = urlparse(url.strip())
        params = parse_qsl(parsed_url.query, keep_blank_values=True)
        for key, value in params:
            urls[parsed_url.path].append("%s=%s" % (key, quote(value)))

# printing results
for url, params in urls.iteritems():
    print url
    for param in params:
        print param

rawDescription: Group values with common domain and page values