code:
import sys, shlex, readline, os, string
List, assign, call, add, sub, div, Pow, mul, mod, fac, duf, read,\
kill, clr, STO, RET, fib, curs = {}, "set", "get", "+", "-", "/", "^", "*",\
"%", "fact", "func", "read", "kill", "clear", ">", "@", "fib", "vars"
def fact(num):
    if num == 1: return 1
    else: return num*fact(num-1)
def builtin_op(op, stack):
    global List
    if op == mul: stack.append(float(stack.pop())*float(stack.pop()))
    elif op == div: stack.append(float(stack.pop())/float(stack.pop()))
    elif op == sub: stack.append(float(stack.pop())-float(stack.pop()))
    elif op == add: stack.append(float(stack.pop())+float(stack.pop()))
    elif op == Pow: stack.append(float(stack.pop())**float(stack.pop()))
    elif op == assign: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == call: stack.append(List[stack.pop()])
    elif op == fac: stack.append(fact(stack.pop()))
    elif op == duf: stack.append("%s %s %s" % (duf, stack.pop(), stack.pop()))
    elif op == mod: stack.append(float(stack.pop())%float(stack.pop()))
    elif op == kill: del List[stack.pop()]
    elif op == clr: os.system("clear")
    elif op == STO: val = List[stack.pop()] = stack.pop(); stack.append(val)
    elif op == RET: stack.append(List[stack.pop()])
    elif op == curs: stack.append(List)
    elif op == read: prompt = stack.pop(); List[prompt] = Eval(raw_input("%s "%prompt)); stack.append(List[prompt])
def Eval(expr):
    ops = "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s"%(mul, add, sub, div, Pow, assign, call, fac, duf, mod, read, kill, clr, STO, RET, curs)
    stack, expr, ops = [], shlex.split(string.lower(expr)), ops.split()
    for i in expr:
        if i[0] != ';':
            if i not in ops: stack.append(i)
            elif i in ops: builtin_op(i, stack)
        else: stack.append("ok")
    return stack[0]
def shell():
    try:
        x = ""
        while x != "quit":
            x = raw_input("star>   ")
            try: l = Eval(x)
            except KeyError: l = "does not exist"
            except: l = "parse error!"
            if l != None: print "   =>",l,"\n"
    except (EOFError, KeyboardInterrupt): print
if len(sys.argv) > 1:
    x = open(sys.argv[1], 'r'); l = x.readlines(); x.close()
    for i in l:
        if i[0] != ";":
            i = ' '.join(i.split())
            x = Eval(i)
            if x != None: print i,"\n   =>",x,"\n"
        else: pass
    shell()
else: shell()

rawDescription: small language in python

description: Small language in python


code:
import re
objectnames = 'hername: [485]', 'myname: [42]', 'myname: [81]'
name = 'myname'
for objectname in objectnames:
    if re.search(r'^{}: \[\d+\]$'.format(re.escape(name)), objectname):
        print objectname, 'matches'

for objectname in objectnames:
    if objectname.startswith('{}: ['.format(name)):
        print objectname, 'matches'

for objectname in objectnames:
    if objectname.partition(':')[0] == name:
        print objectname, 'matches'

rawDescription: check string but skip number inside brackets?

description: Check string but skip number inside brackets


code:
user = json.loads('{"__type__": "User", "name": "John Smith", "username": "jsmith"}')
print user['name']
print user['username']

rawDescription: How to convert JSON data into a Python object

description: Convert JSON data into a Python object


code:
# the variable 'stdout' is now a tuple as returned by string.split('\n'), right?

# if you need a loop index, use enumerate() instead of this
for i in range (len(stdout)):

    # this if statement checks for membership in the tuple 'stdout' which is
    # going to have the same result every time - did you mean to check the
    # elements of the tuple you're iterating over for something instead?
    if 'no rows selected' not in stdout:

        # indexing into an iterable with a loop index is usually wrong in python
        # not always, but usually
        sql_output=stdout[i].split(',')

        # do you need to split this thing up into so many components when they're
        # all getting passed anyway?
        client_info=sql_output[0]
        sid=sql_output[1]
        serial=sql_output[2]
        program=sql_output[3]
        last_call=sql_output[4]
        process=sql_output[5]
        machine=sql_output[6]

        t = Thread(target=kill_it, args=(client_info,sid,serial,program,last_call,process,machine,))
        print t
        t.start()
        # threads and processes should ALWAYS be joined

rawDescription: Child thread doesn`t really spawn or has some issue in python?


code:
for line in reversed(open("filename").readlines()):
    print line.rstrip()

rawDescription: Read a file in reverse order using python


code:
def sighandler(signum, frame):
    print 'signal handler called with signal: %s ' % signum
    global shutdown_flag
    shutdown_flag = True
    sys.exit() # make sure you add this so the main thread exits as well.

if __name__ == '__main__':
    main(sys.argv)
    while 1:  # this will force your main thread to live until you terminate it.
       time.sleep(1)

rawDescription: Why is signal.SIGTERM not dealt with properly in my main thread?


code:
import pprint
import itertools
l = [
  {'word': u'live', 'sequence': 1L, 'part': 1L},
  {'word': u'school', 'sequence': 2L, 'part': 1L},
  {'word': u'job', 'sequence': 1L, 'part': 2L},
  {'word': u'house', 'sequence': 2L, 'part': 2L},
]
l2 = [sorted(list(g), key=lambda x:x["sequence"])
      for k, g in itertools.groupby(l, key=lambda x:x["part"])]
pprint.pprint(l2)

rawDescription: Python - List of dictionary to list of list of dictionary


code:
import re

msg = "[something]" + random.randint(1,5)*str(random.randint(1,1000000000)) + "[something]"

print msg[msg.index('['):msg.index(']')+1]
print re.search("(\[.*?\])", msg).group(1)

rawDescription: Python 2.7 string

description: Python 2.7 string


code:
from scrapy.item import Item, Field
from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.selector import Selector
import re
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from urlparse import urljoin

class MmobySpider(CrawlSpider):
    name = "mmoby2"
    allowed_domains = ["mymobile.ge"]
    start_urls = [
        "http://mymobile.ge/new/v2.php?cat=69&pnum=1"
    ]

    rules = (
        Rule(SgmlLinkExtractor(
                allow=("new/v2\.php\?cat=69&pnum=\d*",),
            )
            , callback="parse_items", follow=False),)

    def parse_items(self, response):
        sel = Selector(response)
        print response.url

rawDescription: Next pages and scrapy crawler doesn't work


code:
from types import FunctionType

class MetaTerms(type):
    """
    This metaclass will let us create a Terms class.
    Every subclass of the terms class will have its
    methods auto-wrapped as static methods, and
    will be added to the terms directory.
    """
    def __new__(cls, name, bases, attr):
        # Auto-wrap all methods as static methods
        for key, value in attr.items():
            if isinstance(value, FunctionType):
                attr[key] = staticmethod(value)
        # call types.__new__ to finish the job
        return super(MetaTerms, cls).__new__(cls, name, bases, attr)

    def __init__(cls, name, bases, attr):
        # At __init__ time, the class has already been
        # built, so any changes to the bases or attr
        # will not be reflected in the cls.
        # Call types.__init__ to finish the job
        super(MetaTerms, cls).__init__(name, bases, attr)
        # Add the class into the termsets.
        if name != 'Terms':
            cls.termsets[cls.shortname] = cls

    def __getattr__(cls, name):
        return cls.termsets[name]

class Terms(object):
    __metaclass__ = MetaTerms
    termsets = {} # Keep track of sets

class SqrtTerms(Terms):
    display = 'Square Roots' # user-readable name
    shortname = 'sqrt'  # Used to find in Terms.termsets

    def size(d):
        """Number of terms based on input columns"""
        return d

    def make(X):
        """Make the terms from the input data"""
        return numpy.sqrt(X)

    def labels(columns):
        """List of term labels based off of data column labels"""
        return ['sqrt(%s)' % c for c in columns]

class PairwiseProductTerms(Terms):
    display = 'Pairwise Products'
    shortname = 'pairprod'

    def size(d):
        return (d * (d-1)) / 2

    def make(X):
        pass

    def labels(columns):
        # Technically a one-liner but also more complicated
        return ['(%s) * (%s)' % (columns[c1], columns[c2])
            for c1 in range(len(columns)) for c2 in range(len(columns))
            if c2 > c1]

print Terms.termsets
print Terms.sqrt
print Terms.pairprod
print Terms.pairprod.size(5)

rawDescription: Purely static classes in Python - Use metaclass, class decorator, or something else?


code:
match = re.finditer('BB', 'ABBACDA')
for m in match:
    print m.start(), m.end(), m.group(0)

rawDescription: Finding character position from a RegEx'd string


code:
rows, cols = 100, 100
im1 = np.random.rand(rows, cols)
im2 = np.random.rand(rows, cols)
ans1 = lucas_kanade_op(im1, im2)
ans2 = lucas_kanade_np(im1, im2)
np.testing.assert_almost_equal(ans1,ans2)

import timeit
print 'op\'s time:', timeit.timeit('lucas_kanade_op(im1, im2)',
                                   'from __main__ import lucas_kanade_op, im1, im2',
                                   number=1)
print 'np\'s time:', timeit.timeit('lucas_kanade_np(im1, im2)',
                                   'from __main__ import lucas_kanade_np, im1, im2',
                                   number=1)

rawDescription: Lucas Kanade python numpy implementation uses enormous amount of memory


code:
def __getattr__(self, name):
    # if __getattr__() is being called, locals() were already checked
    d = sys._getframe(1).f_locals
    if name in d:
        return d[name]

    d = globals()
    if name in d:
        return d[name]

    mesg = "name '%s' not found in monad, locals, or globals" % name
    raise NameError, mesg

rawDescription: python late binding - dynamically put locals in scope

description: Python late binding - dynamically put locals in scope


code:
from collections import Counter

counts = Counter(d)
for key, freq in counts.most_common(30):
    print key, freq

rawDescription: How to find to 30 biggest numbers from 1000 in dict?

description: Find to 30 biggest numbers from 1000 in dict


code:
value, TimeStamp = 41, '8\r,15-08-2015 20:28:47'
print "insert into Log(ComponentId, Value, TimeStamp) OUTPUT INSERTED.ID VALUES(8,{0},{1})".format(value, TimeStamp)
TimeStamp = TimeStamp.replace('\r', '')
print "insert into Log(ComponentId, Value, TimeStamp) OUTPUT INSERTED.ID VALUES(8,{0},{1})".format(value, TimeStamp)

rawDescription: String appending is acting really odd Python


code:
text = thought1 + " " + thought2 # To handle both strings
result = [a for a in text.split() for b in foods if b in a]

print result

rawDescription: Checking if string contains a word that is subset of a word in a list


code:
def get_protein_chunks(filepath):
    chunk = []
    last_indented = False
    with open(filepath) as f:
        for line in f:
            if not line.startswith(" "):
                current_indented = False
            else:
                current_indented = True
            if last_indented and not current_indented:
                yield chunk
                chunk = []
            chunk.append(line.strip())
            last_indented = current_indented

look_for_proteins = set(line.strip() for line in open('file2.txt'))

for p in get_protein_chunks("input.txt"):
    proteinname = p[0].split()[0]
    proteindata = p[1:]
    if proteinname not in look_for_proteins:
        continue
    print "Protein: %s" % proteinname
    golines = [l for l in proteindata if l.startswith("GO:")]
    for g in golines:
        print g

rawDescription: Finding common elements between two files


code:
import re
import sre_parse

def ld(a,b):
    "Calculates the Levenshtein distance between a and b."
    n, m = len(a), len(b)
    if n > m:
        # Make sure n <= m, to use O(min(n,m)) space
        a,b = b,a
        n,m = m,n

    current = range(n+1)
    for i in range(1,m+1):
        previous, current = current, [i]+[0]*n
        for j in range(1,n+1):
            add, delete = previous[j]+1, current[j-1]+1
            change = previous[j-1]
            if a[j-1] != b[i-1]:
                change = change + 1
            current[j] = min(add, delete, change)
    return current[n]

s='Mary had a little lamb'
d={}
regs=[r'.*', r'Mary', r'lamb', r'little lamb', r'.*little lamb',r'\b\w+mb',
        r'Mary.*little lamb',r'.*[lL]ittle [Ll]amb',r'\blittle\b',s,r'little',
        r'^.*lamb',r'.*.*.*b',r'.*?.*',r'.*\b[lL]ittle\b \b[Ll]amb',
        r'.*\blittle\b \blamb$','^'+s+'$']

for reg in regs:
    m=re.search(reg,s)
    if m:
        ld1=ld(reg,m.group(0))
        ld2=ld(m.group(0),s)
        score=max(ld1,ld2)
        for t, v in sre_parse.parse(reg):
            if t=='at':      # anchor...
                if v=='at_beginning' or 'at_end':
                    score-=1   # ^ or $, adj 1 edit

                if v=='at_boundary': # all other anchors are 2 char
                    score-=2

        d[reg]=score
    else:
        print "'%s' does not match '%s'" % (reg, s)

print
print "   ===== %s =====    === %s ===" % ('RegEx'.center(15),'Score'.center(10))

for key, value in sorted(d.iteritems(), key=lambda (k,v): (v,k)):
    print "   %27s        %5s" % (key, value)

rawDescription: Matching 2 regular expressions in Python


code:
f = open('file')
with f:
    print >> f, "Open"

print f.closed  # True

rawDescription: Can I use a with statement on a file that already exists?

description: Use a with statement on a file that already exists


code:
def find_all_paths(graph, start, end, path=[]):
        path = path + [start]
        if start == end:
            return [path]
        if start not in graph:
            return []
        paths = []
        for node in graph[start]:
            if node not in path:
                newpaths = find_all_paths(graph, node, end, path)
                for newpath in newpaths:
                    paths.append(newpath)
        return paths

def min_path(graph, start, end):
    paths=find_all_paths(graph,start,end)
    mt=10**99
    mpath=[]
    print '\tAll paths:',paths
    for path in paths:
        t=sum(graph[i][j] for i,j in zip(path,path[1::]))
        print '\t\tevaluating:',path, t
        if t<mt:
            mt=t
            mpath=path

    e1=' '.join('{}->{}:{}'.format(i,j,graph[i][j]) for i,j in zip(mpath,mpath[1::]))
    e2=str(sum(graph[i][j] for i,j in zip(mpath,mpath[1::])))
    print 'Best path: '+e1+'   Total: '+e2+'\n'

if __name__ == "__main__":
    graph = {'A': {'B':5, 'C':4},
             'B': {'C':3, 'D':10},
             'C': {'D':12},
             'D': {'C':5, 'E':9},
             'E': {'F':8},
             'F': {'C':7}}
    min_path(graph,'A','E')
    min_path(graph,'A','D')
    min_path(graph,'A','F')

rawDescription: Dictionary best data structure for train routes?


code:
from random import shuffle
L = ["Anne", "Beth", "Cath", "Dave", "Emma"]
shuffle(L, int=lambda n: int(n - 1))
print L

rawDescription: oneliner scramble program

description: Oneliner scramble program


code:
import numpy as np

def do_complicated_math(r, g, b):
    rgb = np.array([r, g, b])

    # create a transposed version of the m_2 array
    m_2 = np.zeros((r.size,3,3))
    for ii,ar in enumerate(rgb):
        m_2[:,ii][:,ii][:] = ar
    m_1 = np.ones((3, 3))
    m_3 = np.ones((3, 3))

    rgb_transformed = m_1.dot(m_2).dot(m_3).dot(rgb)

    print rgb_transformed
    return np.arctan2(rgb_transformed, rgb_transformed)

x = np.ones(6)
do_complicated_math(x, x, x)

r = np.array([0.2,0.3,0.1])
g = np.array([1.0,1.0,0.2])
b = np.array([0.3,0.3,0.3])
do_complicated_math(r, g, b)

rawDescription: Numpy arctan2 of multidimensional array

description: Numpy arctan2 of multidimensional array


code:
f = open('some_file.txt')
f.seek(0, os.SEEK_END)
seek_newline_backwards(f)
print f.tell(), repr(f.readline())

rawDescription: Two simple questions about python

description: Two simple questions about python


code:
data = {'John': {'Maths': 40, 'Eng':50, 'Phy': 67, 'Chem': 78},
        'Kate': {'Maths': 98, 'Chem': 83},
        'Julia': {'Phy': 76, 'Eng': 67, 'Maths': 56, 'Bio': 78},
        'Sam': {'Phy': 23, 'Eng': 67, 'Chem': 98, 'Maths': 56}}

print "name\tmark\n------------"
for name, marks in data.iteritems():
    if not 'Bio' in marks:
        print "{name}\t{mark}".format(name=name, mark=marks.get('Eng', '---'))

rawDescription: making arrays/lists of selected items from a dictionary in python

description: Making arrays/lists of selected items from a dictionary in python


code:
def f(x):
    print "pid: ", os.getpid(), " deal with ", x
    sys.stdout.flush()

if __name__ == '__main__':
    pool = Pool(processes=4, maxtasksperchild=2)
    keys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    result = pool.map(f, keys)

rawDescription: How to limit the number of concurrent workers?

description: Limit the number of concurrent workers


code:
cursor.execute("select CONCAT(nameid, '_', cityid, '.txt') from mydata")
present = set([r[0] for r in cursor])

for pdffile in (set(os.listdir(filepath)) - present):
    nameid, cityid = map(int, pdffile.rsplit('.', 1)[0].split('_'))
    print nameid, cityid, "Found"

rawDescription: How to do this lookup more elegantly in Python and MySQL?

description: Do this lookup more elegantly in Python and MySQL


code:
from datetime import datetime

dates = [20010101.0, 20010101.25, 20010101.5, 20010101.75, 20020102.0]

for a_date in dates:
    date, hour = divmod(a_date, 1)
    print datetime.strptime('{} {}'.format(int(date), int(24.0 * hour)), "%Y%m%d %H")

rawDescription: Convert date string with days as a float to datetime object


rawDescription: Findall regex in python

description: Findall regex in python


code:
import csvmapper

mapper = csvmapper.JSONMapper('mapper.json')
parser = csvmapper.CSVParser('data.csv', mapper)

for item in parser.buildObject():
    print item.Job # Job of the item
    print "will be %d after 2 years" %(item.Age+2) # item.Age is an int

rawDescription: Mapping CSV using json file

description: Mapping CSV using json file


code:
Types = ['integer', 'varchar(12)', 'integer', 'double', 'date']
pyTypes = [get_py_type(item) for item in Types]
print pyTypes

rawDescription: Python: Changing Types while writing in a MS-Access DB


code:
print {item[0]:item[1] for item in set1 & set2}
# {'text': '5'}

rawDescription: Python Check Duplicate in Array

description: Python Check Duplicate in Array


code:
from mako.template import Template
mytemplate = Template(filename='/docs/mytmpl.txt', module_directory='/tmp/mako_modules')
print mytemplate.render()

rawDescription: Python functions in Mako templates (not in module-level blocks)


code:
#!/usr/bin/python

import os
import subprocess
import tarfile

print 'Adding files to archive.'
with tarfile.open("archive.tar.gz", "w:gz") as f:
    for name in ["file1", "file2"]:
        f.add(name)

print 'Adding archive to commit.'
subprocess.call(['git', 'add', 'archive.tar.gz'])

rawDescription: Use git hooks to create an archive of files


code:
print res
type(res)

rawDescription: How can I select a certain value based on 2(or more) other values in a pandas dataframe

description: Select a certain value based on 2(or more) other values in a pandas dataframe


code:
from flask import Flask, request
app = Flask(__name__)

@app.route('/get_phone/', methods=['POST', 'GET'])
def get_phone():
    if request.method == 'POST':
        print 'First name:', request.form['firstname']
        print 'Phone:', request.form['lastname']

    return 'Take a look at your terminal!'

if __name__ == '__main__':
    app.run()

rawDescription: Simple form submit with python

description: Simple form submit with python


code:
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
    while 1:
        ok = raw_input(prompt)
        if ok in ('y', 'ye', 'yes'): return 1
        if ok in ('n', 'no', 'nop', 'nope'): return 0
        retries = retries - 1
        if retries < 0: raise IOError, 'refusenik user'
        print complaint

rawDescription: Passing in arguments to command line (PYTHON)


code:
import urllib2
f = urllib2.urlopen('http://89.238.146.142:7030')
for i, line in enumerate(f):
  if line.startswith('icy-name') or i > 20: break

if i > 20: print 'failed to find station name'
else: print 'station name is', line.replace('icy-name:', '')

rawDescription: Python: Get name of shoutcast/internet radio station from url


rawDescription: Parsing NOAAPORT feed with RegEx

description: Parsing NOAAPORT feed with RegEx


code:
import datetime
import re
months = {}
for date in dates:
    month = date[:7]
    time = re.match(r"(\d+):(\d+):(\d+)", dates[date])
    seconds = int(time.group(1))*3600 + int(time.group(2))*60 + int(time.group(3))
    if month in months:
        months[month] += seconds
    else:
        months[month] = seconds
for month in sorted(months.keys()):
    print "Times for " + month + ": " + str(datetime.timedelta(seconds=months[month]))

rawDescription: Add timestamps in python month wise

description: Add timestamps in python month wise


rawDescription: How to print a board in python?

description: Print a board in python


code:
def custom_split(data):
    element, opened, result = "", 0, []
    for char in data:
        # Immediately following if..elif can be shortened like this
        # opened += (char == ")") - (char == "(")
        # Wrote like that for readability

        if char == ")":
            opened -= 1
        elif char == "(":
            opened += 1

        if char == "." and opened == 0 and element != "":
            result.append(element)
            element = ""
        else:
            element += char

    if element != "":
        result.append(element)
    return result

print custom_split('(1.2).(2.1)')
print custom_split('(1.2).2')
print custom_split('2.2')
print custom_split('(2.2)')
print custom_split('2')

rawDescription: split a string at a certain index

description: Split a string at a certain index


code:
f = open("input2.txt", "r").read().split()
w = open("input1.txt", "r").readlines()

for timestamp in w:
    for letter in f:
        print timestamp.rstrip(), letter

rawDescription: Processing two lists, iterating line by line and join values


code:
class SetVariables(dict):
    'Transform a variable lookup into a membership test'
    def __getitem__(self, var):
        s = dict.__getitem__(self, var)
        return self.x in s

def set_eval(expr, **sets):
    'Evaluation a set expression for the given sets'
    universe = {x for s in sets.values() for x in s}
    expr = compile(expr, '', 'eval')
    variables = SetVariables(sets)
    results = set()
    for x in universe:
        variables.x = x
        if eval(expr, {}, variables):
            results.add(x)
    return results

if __name__ == '__main__':
    print set_eval(expr = '(A or B) and not(A and C)',
                   A = {0, 1},
                   B = {0, 2},
                   C = {1, 3}
                  )

rawDescription: Parsing and computing boolean set definitions


code:
proxies = {'http': 'http://myproxy.example.com:1234'}
print "Using HTTP proxy %s" % proxies['http']
urllib.urlopen("http://www.google.com", proxies=proxies)

rawDescription: How can I open a website with urllib via proxy in Python?

description: Open a website with urllib via proxy in Python


code:
import sqlite3
from scrapy.exceptions import IgnoreRequest

class IgnoreDuplicates():

    def __init__(self):
        self.crawled_urls = set()

        with sqlite3.connect('C:\dev\scrapy.db') as conn:
            cur = conn.cursor()
            cur.execute("""SELECT url FROM CrawledURLs""")
            self.crawled_urls.update(x[0] for x in cur.fetchall())

        print self.crawled_urls

    def process_request(self, request, spider):
        if request.url in self.crawled_urls:
            raise IgnoreRequest()
        else:
            return None

rawDescription: Using Middleware to ignore duplicates in Scrapy


code:
import numpy as np

data = np.array([[[1704, 1240],
                  [1745, 1244],
                  [1972, 1290],
                  [2129, 1395],
                  [1989, 1332]],

                 [[1712, 1246],
                  [1750, 1246],
                  [1964, 1286],
                  [2138, 1399],
                  [1989, 1333]],

                 [[1721, 1249],
                  [1756, 1249],
                  [1955, 1283],
                  [2145, 1399],
                  [1990, 1333]]])

point = [1989, 1332]

#-- Calculate distance ------------
# The reshape is to make it a single, Nx2 array to make calling `hypot` easier
dist = data.reshape((-1,2)) - point
dist = np.hypot(*dist.T)

# We can then reshape it back to AxBx1 array, similar to the original shape
dist = dist.reshape(data.shape[0], data.shape[1], 1)

#-- Remove closest point ---------------------
mask = np.squeeze(dist) != dist.min(axis=1)
filtered = data[mask]

# Once again, let's reshape things back to the original shape...
filtered = filtered.reshape(data.shape[0], -1, data.shape[2])

print filtered

rawDescription: NumPy: Execute function over each ndarray element


code: print repr(a)

rawDescription: Is print the only way to display something in .py file


rawDescription: Get the nth element from the inner list of a list of lists in Python


code:
import pandas as pd
print pd.read_csv('test.csv',sep='|')

rawDescription: Reading a CSV-file with uncomplete headers


code:
import datetime    # <- LOOK HERE, same as in your example

date_select = datetime.datetime.strptime('2011-12-1', '%Y-%m-%d')
delta = datetime.timedelta(days=1)
target_date = date_select + delta
print target_date

rawDescription: how to convert string to datetime.timedelta()?

description: Convert string to datetime.timedelta()


code:
import getpass
import pickle
import hashlib
from os import path

def Encryption(data):
    return hashlib.sha224(data).hexdigest()

## First we check if the database exists.
if path.isfile('database.db'):
    fh = open('database.db', 'rb')
    db = pickle.load(fh)
    fh.close()
## If it doesn't, we will create one.
else:
    ## First we create the desired variable.
    db = {'torxed' : Encryption('wham'), 'someoneelse' : Encryption('pass')}
    ## Then we open a filehandle to it.
    fh = open('database.db', 'wb')
    ## And then we dump the variable into the filehandle.
    ## This will keep the variable intact between sessions,
    ## meaning the next time you start your script, the variable will look the same.
    pickle.dump(db, fh)
    fh.close()

## Then we ask the user for his/hers credentials.
user = raw_input('Username: ')
_pass = getpass.getpass('Password: ')

## If the user exists in the "db" and the decoded password
## Matches the logged in user, it's a-ok :)
if user in db and db[user] == Encryption(_pass):
    print 'You logged in'

rawDescription: Password Protection Python

description: Password Protection Python


code: print "test"

rawDescription: How to get input in Python 2.7?

description: Get input in Python 2.7


code:
import itertools, operator, csv
with open("test.csv") as f:
    reader = csv.reader(f, delimiter=",")
    next(reader) # skip header line
    for key, group in itertools.groupby(reader, key=operator.itemgetter(0)):
        print key, ' '.join(g[1] for g in group)

rawDescription: combine multiple values in Python

description: Combine multiple values in Python


code:
import random
N = 5
random.seed(1024)
lists = [sorted(random.sample(range(10*N), 2*N-1)) for _ in xrange(N)]
for p in find_smallest_sum_permutations(lists):
    print p, sum(p)

rawDescription: Find the permutations that sums to the three smallest numbers


code:
import string
block = ["damn", "shit"]
block_char_list = [ list(i) for i in block ]

def  getbBlockListFlag(msg):
    #Remove punctuation
    tmp = msg.translate(string.maketrans("",""), string.punctuation)
    tmp1 = []
    for word in tmp.split():
        j = []
        for k in list(word):
            if k not in j:
                j.append(k)
            elif k != j[-1]:
                j.append(k)

        # word in block list
        if j in block_char_list:
            return True, j

    return False, []

msg = "True test case ? daaaammmmnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

msg = "True test case ? normal damn.,"
status, bloak_char = getbBlockListFlag(msg)
if status:
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

msg = "False test case ? nothing when sequance aaammdddnnn.."
status, bloak_char = getbBlockListFlag(msg)
if status:
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

msg = "False test case ? nothing.."
status, bloak_char = getbBlockListFlag(msg)
if status:
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(bloak_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)
msg = "True test case ? Handle  daaaammaaaammnnnnn.,"
status, block_char = getbBlockListFlag(msg)
if status:
    print "The word `%s` from block list is presnt in input '%s'"%(''.join(block_char), msg)
else:
    print "No word from block list is presnt in input '%s'"%(msg)

rawDescription: Find similar word for word in the list

description: Find similar word for word in the list


code:
import numpy
from scipy import signal

# Set up the inputs
a = numpy.random.randn(100, 200)
a[a<0] = 0
a[a>0] = 255

b = numpy.random.randn(20, 20)
b[b<0] = 0
b[b>0] = 255

# put b somewhere in a
a[37:37+b.shape[0], 84:84+b.shape[1]] = b

# Now the actual solution...

# Set the black values to -1
a[a==0] = -1
b[b==0] = -1

# and the white values to 1
a[a==255] = 1
b[b==255] = 1

max_peak = numpy.prod(b.shape)

# c will contain max_peak where the overlap is perfect
c = signal.correlate(a, b, 'valid')

overlaps = numpy.where(c == max_peak)

print overlaps

rawDescription: Finding matching submatrices inside a matrix


rawDescription: How to add matrices in python

description: Add matrices in python


code:
pressed = pygame.get_keypressed()

if all( (pressed[x] for x in (K_h, K_e, K_l, K_o)) ):
    print "all keys are pressed: h, e, l, o"

if any( (pressed[x] for x in (K_h, K_e, K_l, K_o)) ):
    print "at least one key is pressed: h, e, l, o"

rawDescription: How to process pygame.get_pressed() to receive input

description: Process pygame.get_pressed() to receive input


code:
def main(folder_path_to_search, files_to_compare_to, source_image_path):
    #capture_img = "/Users/Me/home1.png"
    capture_img = input('enter path of the file from database')
    #img_to_compare = "/Users/Me/Documents/python programs/compare/img2.jpg"
    take_and_save_picture(capture_img)

    images_to_compare = [ os.path.join(folder_path_to_search,file_path) for file_path in os.listdir(folder_path_to_search) if file_path.endswith(files_to_compare_to) ]

    for comp_image in get_images_to_compare():
        diff = compute_edges_diff(source_image_path, comp_image)
        print "Difference (percentage):", diff, "(", source_image_path, ":", comp_image, ")"

if __name__ == '__main__':
  folder_path_to_search = raw_input("Enter folder path to search")
  files_to_compare_to   = raw_input("enter file extention to glob ex: '.jpg'")
  source_image_path     = raw_input("enter full file path of source image")
  main(folder_path_to_search, files_to_compare_to, source_image_path)

rawDescription: compare a single image with 10 or more images and find the matching one

description: Compare a single image with 10 or more images and find the matching one


code:
connections = cmds.listConnections()
for destination, source in zip(*[iter(connections)]*2):
    print source, destination

rawDescription: Way to iterate two items at a time in a list?


code:
import re

listitems = ['Adams<\/a>\n', 'Donovan Smith<\/a>\n']

PATTERN = re.compile(r'(.*?)<\\/a>')

names = []
for item in listitems:
  n = re.search(PATTERN, item).group(1)
  names.append(n)

print names
['Adams', 'Donovan Smith']

rawDescription: Is accepting special characters for a range within a regex pattern possible?


code:
from itertools import product, izip
def Cc(s):
    s = s.lower()
    for p in product(*[(0,1)]*len(s)):
      yield ''.join( c.upper() if t else c for t,c in izip(p,s))

print list(Cc("Dan"))

rawDescription: Combination of all possible cases of a string


code:
import sys
class SetTrace(object):
    def __init__(self, func):
        self.func = func

    def __enter__(self):
        sys.settrace(self.func)
        return self

    def __exit__(self, ext_type, exc_value, traceback):
        sys.settrace(None)

def monitor(frame, event, arg):
    if event == "line":
        print('hello')
        # print(frame.f_globals)
        # print(frame.f_locals)
    return monitor

def foo():
   print 'bar'
   print 'barbar'
   print 'barbarbar'

with SetTrace(monitor):
    foo()

rawDescription: Python : Adding a code routine at each line of a block of code


code:
def get_user_choice(prompt, choices):
    while True:
        choice = raw_input(prompt)
        if choice in choices:
            return choice
        else:
            print 'choice must be in: {}'.format(choices)

rawDescription: python: If an entry is not an acceptable parameter for a prompt, how to re-ask

description: Python: If an entry is not an acceptable parameter for a prompt, how to re-ask


code:
import os
import time

print time.ctime(max(os.stat(root).st_mtime for root,_,_ in os.walk('/tmp/x')))

rawDescription: Get the last modified date of a directory (including subdirectories) using Python?


code:
a, a = 1, 2
print a, a

rawDescription: Two Basic Python Dictionary Questions

description: Two Basic Python Dictionary Questions


code:
for index, element in enumerate(l):
    print index, element

rawDescription: Dynamic ordered python structure?

description: Dynamic ordered python structure


code:
def depth(L) :
    nesting = []
    for c in L:
        if type(c)  == type(nesting) :
            print 'nesting before append', nesting ###
            nesting.append(depth(c))
            print 'nesting after append', nesting ###
    if len(nesting)  > 0:
        return 1 + max(nesting)
    return 1

rawDescription: having trouble understanding this code

description: Having trouble understanding this code


code:
import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
root = tree.getroot()

a = root.find('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(b, 'c')
c.text = 'text3'

print ET.tostring(root)

rawDescription: python xml.etree.ElementTree append to subelement

description: Python xml.etree.ElementTree append to subelement


code:
import io
with open('/tmp/test.txt', 'wb') as outfh:
    outfh.write('cat\tdog\tfish\nchips\t4.50\r\nsnake\tegg\tspam\nham\t42.38\r\n')
with io.open('/tmp/test.txt', newline='\r\n') as infh:
    for line in infh:
        row = line.strip().split('\t')
        print row

rawDescription: Python: Read a file separated strictly on 0x0a, not '\n' string


code:
from datetime import datetime

with open(input_filename) as inf:
    # generator expression
    datetimes = (datetime.strptime(line.strip(), '%d-%m-%Y %H:%M:%S') for line in inf)
    for mindt, maxdt in min_max_per_hour(datetimes):
        print mindt, maxdt

rawDescription: Minimum and maximum timestamps per hour bucket


rawDescription: How to print a string of variables without spaces in Python (minimal coding!)

description: Print a string of variables without spaces in Python (minimal coding!)


code:
def difft2(start,end):
    a,b,c,d = start.hour, start.minute, start.second, start.microsecond
    w,x,y,z = end.hour, end.minute, end.second, end.microsecond
    delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000.
    if delt < 0:
        delt += 1440

    hh,rem = divmod(delt,60)
    hh = int(hh)
    mm = int(rem)
    rem = (rem - mm)*60
    ss = int(rem)
    ms = (rem - ss)*1000000
    ms = int(ms)

    SS = '%sh %smn %ss %sms - %sh %smn %ss %sms == %sh %smn %ss %sms'
    return SS % (w,x,y,z,a,b,c,d,hh,mm,ss,ms)

print difft2(time(11,30,0),time(12,35,45,478129))
print difft2(time(11,30,45,11),time(10,35,45,12))
print
print difft2(time(20,40,0),time(22,41,0))
print difft2(time(20,40,0),time(18,41,0))

rawDescription: how to substract two datetime.time values in django template,and how to format a duration as hour,minutes

description: Substract two datetime.time values in django template,and how to format a duration as hour,minutes


code:
for child in get_buttons_hbox(a).get_children():
    print child.get_label()

rawDescription: How do I change the built-in button labels on a gtk.Assistant?

description: Change the built-in button labels on a gtk.Assistant


code:
def convert(unplanned, planned, emerg):
    chain = (unplanned, planned, emerg)
    names = map(lambda lst: [d['service__name'] for d in lst], chain)
    sums = map(lambda lst: [d['service_sum'] for d in lst], chain)
    ds = [dict(zip(n, s)) for n,s in zip(names, sums)]
    unique_names = set([])
    unique_names = reduce(unique_names.union,names)
    results = []
    for n in unique_names:
        s = []
        for i in range(3):
            s.append(ds[i].get(n,0))
        results.append((n, tuple(s)))

    return results

print convert(unplanned, planned, emerg)

rawDescription: Python Compare List to Dict Value

description: Python Compare List to Dict Value


code:
from ctypes import CFUNCTYPE
def pyfn():
    print "Called the Python function"

CWRAPPER = CFUNCTYPE(None)
wrapped_py_func = CWRAPPER(pyfn)
main.mainfunction(wrapped_py_func)

rawDescription: How to pass a function pointer to an external program in Cython

description: Pass a function pointer to an external program in Cython


code:
from collections import defaultdict
my_list = [{'Table': 'A', 'Column': 'C1', 'DataType': 'int'},
           {'Table': 'A', 'Column': 'C2', 'DataType': 'varchar'},
           {'Table': 'B', 'Column': 'C3', 'DataType': 'int'}]

position_list = [{'Table': 10}, {'Column': 15}, {'DataType': 10}]
positions = defaultdict(lambda : 10)
for d in position_list:
    positions.update(d)

print [{k + ' ' * positions[k] : v + ' ' * positions[v] for k,v in d.items()} for d in my_list]

rawDescription: How to append space at the end of Key and Values in the list of dictionary in python

description: Append space at the end of Key and Values in the list of dictionary in python


code:
def print_to_screen(df):
    print df

def save_to_file(df, filename):
    df.to_csv(filename)

rawDescription: How to use a functional in this case?

description: Use a functional in this case


code:
listOfStuff =([a,b], [c,d], [e,f], [f,g])

for item in listOfStuff[1:3]:
    print item

rawDescription: python: iterate a specific range in a list

description: Python: iterate a specific range in a list


code: print pp(T.grad(lr._y_given_x,x)) #might need syntax checkng

rawDescription: What is the prupose/meaning of passing "input" to a function in Theano?


code:
for i in location_hw_map: # Loops through every key in the dictionary
    if i in testString: # If the key is in the test string (if "123456789" is in "uyrfr..."
        print location_hw_map[i] # Print the value of the key
        break # We break out of the loop incase of multiple keys that are in the test string

rawDescription: searching dictionary for string and returning other value

description: Searching dictionary for string and returning other value


code:
for m in regex.finditer(r'(?:'+probe+'){s<'+str(mismatches+1)+'}', sequence, regex.BESTMATCH, overlapped=True):
    print '%d: %s' % (m.start(), m.group())

rawDescription: Python regex : overlapping sequences position


code:
import types

ARGS_FLAG = 4   #If memory serves, this can be found in code.h in the Python source.
KWARGS_FLAG = 8

def valid(f, args, kwargs):

    def dummy():
        pass

    dummy_code = dummy.func_code
    real_code = f.func_code

    args_flag = real_code.co_flags & ARGS_FLAG
    kwargs_flag = real_code.co_flags & KWARGS_FLAG

    # help(types.CodeType) for details
    test_code = types.CodeType(real_code.co_argcount,
                               real_code.co_nlocals,
                               dummy_code.co_stacksize,
                               args_flag | kwargs_flag,
                               dummy_code.co_code,
                               dummy_code.co_consts,
                               dummy_code.co_names,
                               real_code.co_varnames,
                               "<test>", "", 0, "", ())

    # help(types.FunctionType) for details
    test_func = types.FunctionType(test_code, {}, "test", f.func_defaults)

    try:
        test_func(*args, **kwargs)
    except TypeError:
        return False
    else:
        return True

def x(a, b, c): pass
def y(a, b=1, c=2): pass
def z(a=1, b=2, c=3): pass

print valid(x, ("hello", "goodbye", "what?"), {}) # => True
print valid(x, ("hello", "goodbye"), {}) # => False
print valid(y, ("hello", "goodbye", "what?"), {}) # => True
print valid(y, (), {"a": "hello", "b": "goodbye", "c": "what"}) #=> True
print valid(y, ("hello", "goodbye"), {"c": "what?"}) #=> True

rawDescription: check for valid arguments

description: Check for valid arguments


code:
#Imports
import decimal
#variables
neweq = "neweq"
on = 1
#loop
#equasion function
def eq ():
        global b
        b = input("Please enter an equation (Example: 10*(3*a)==4*(7*a), or 3.0/7.0). Unfortunately however, you can only use the variable 'a'. Also, you can type 'exit' to quit:  ")
        if b == exit:
             exit ("Thank you for using me :)")
        else:
            b = float(b)        ## input converted into float.
            b = '%.3f'%(b)      ## after this b would be of type string
            b = float(b)        ## again converting into float to match with "a" in `inp()`
#input funcution
def inp ():
        a = input("Enter numeral (Example: 1, or 1.5) to proceed, 'exit' to quit, or 'neweq' to enter a new equasion:  ")
        if a == exit:
            exit ("Thank you for using me :)")
        if a == neweq:
            a = 0
            eq ()
            inp()
        if b == a:          ## if a == b should work now.
            print "Yes, the answer is", a
            eq ()
        else:
            print "No, the answer is not", a
            print "test line", b
            inp ()
#function calls
eq()
inp ()

rawDescription: What is wrong in my python script?

description: What is wrong in my python script


rawDescription: What is the best way to implement nested dictionaries in Python?


code:
import math
print range(math.floor(min(y)), math.ceil(max(y))+1)

rawDescription: Force the Y axis to only use integers

description: Force the Y axis to only use integers


code:
# pseudo code
class MyClass:
   def __init__(self, caller):
        self.caller = caller

   def display_info(self):
        print self.caller

my_obj = MyClass(caller='caller')
my_obj.display_info()

my_obj.caller = 'new_caller'
my_obj.display_info()

rawDescription: How to get a function's name from a object passed as that function's parameter in Python?

description: Get a function's name from a object passed as that function's parameter in Python


code: print str.__doc__

rawDescription: Python Get Docstring Without Going into Interactive Mode


code:
class Test:
    def __init__(self, *args, **kwargs):
        pass
    def start(self):
        pass

def func():
    print 'this is a func and not a method!!!'

t = Test()

module = __import__(t.start.__module__)

print vars(module)['Test'].start
print vars(module)['func']
print vars(module)['t'].start

rawDescription: Obtaining object reference for a method using getattr


code:
t = Tristate(True)
t == True           # True
t != False          # True
t in (True, False)  # True
bool(t)             # Exception!
if t: print "woo"   # Exception!

rawDescription: Booleans have two possible values. Are there types that have three possible values?


code:
def name():
    print 'Jack'

def do_n(fo, x):
    if x <= 0:
        return
    fo()
    do_n(fo, x - 1)

rawDescription: Calling a function n times with two parameters


code:
for line in ins:
    s  = list(line.decode("utf-8"))
    for character in s:
       if ord( character ) > 10000:
          print repr(line)
          # Found it in the line, move onto the next line
          break

rawDescription: Python script printing lines twice

description: Python script printing lines twice


code:
# uses https://pypi.python.org/pypi/bitarray
from bitarray import bitarray as BitArray

class PackedIntArray(object):
    """ Packed array of unsigned fixed-bit-width integer values. """
    def __init__(self, array_size, item_bit_width, initializer=None):
        self.array_size = array_size
        self.item_bit_width = item_bit_width
        self.bitarray = BitArray(array_size * item_bit_width)
        if initializer is not None:
            try:
                iter(initializer)
            except TypeError:  # not iterable
                self.bitarray.setall(initializer) # set all to bool(initializer)
            else:
                for i in xrange(array_size):
                    self[i] = initializer[i]  # must be same length as array

    def __getitem__(self, index):
        offset = index * self.item_bit_width
        bits = self.bitarray[offset: offset+self.item_bit_width]
        return reduce(lambda x, y: (x << 1) | y, bits, 0)

    def __setitem__(self, index, value):
        bits = BitArray('{:0{}b}'.format(value, self.item_bit_width))
        offset = index * self.item_bit_width
        self.bitarray[offset: offset+self.item_bit_width] = bits

    def __len__(self):
        """ Return the number of items stored in the packed array.. """
        return self.array_size

    def length(self):
        """ Return the number of bits stored in the bitarray.. """
        return self.bitarray.length()

    def __repr__(self):
        return('PackedIntArray({}, {}, ('.format(self.array_size,
                                                 self.item_bit_width) +
               ', '.join((str(self[i]) for i in xrange(self.array_size))) +
               '))')

if __name__ == '__main__':
    from random import randrange

    # hash function configuration
    BW = 8, 8, 4  # bit widths of each integer
    HW = sum(BW)  # total hash bit width

    def myhash(a, b, c):
        return (((((a & (2**BW[0]-1)) << BW[1]) |
                    b & (2**BW[1]-1)) << BW[2]) |
                    c & (2**BW[2]-1))

    hashes = PackedIntArray(3, HW)

    print 'length of hashes array: {:,} bits'.format(hashes.length())
    print ''
    print 'populate hashes array:'
    for i in xrange(len(hashes)):
        hashed = myhash(*(randrange(2**bit_width) for bit_width in BW))
        print '  hashes[{}] <- {:,} (0b{:0{}b})'.format(i, hashed, hashed, HW)
        hashes[i] = hashed
    print ''
    print 'contents of hashes array:'
    for i in xrange(len(hashes)):
        print ('  hashes[{}]: {:,} '
                '(0b{:0{}b})'.format(i, hashes[i], hashes[i], HW))

rawDescription: How to create Python objects of arbitrary memory size?

description: Create Python objects of arbitrary memory size


code:
def changeLetter(item_list):
    result_list = []
    for word in item_list:
        new_word = ''
        for char in word:
            if char == 't':
                new_word += 'T'
            else:
                new_word += char
        result_list.append(new_word)
    print result_list

changeLetter(["test","iteration","123","train"])

rawDescription: Change all specific letters in a list in python


code: print df.index.values.tolist()

rawDescription: how to convert Index into list?

description: Convert Index into list


code:
#!/usr/bin/env python
import datetime, fileinput, re

for line in fileinput.input():
    if 'FILED AS OF DATE' in line:
       line = line.rstrip()
       dt = datetime.datetime.strptime(line, 'FILED AS OF DATE:       %Y%m%d')

       # or with regex
       date_str, = re.findall(r'\d+', line)
       dt = datetime.datetime.strptime(date_str, '%Y%m%d')

       print dt.date()

rawDescription: read a text field in Python using regular expressions

description: Read a text field in Python using regular expressions


code: print data,

rawDescription: Python, code to csv. Troubles

description: Python, code to csv. Troubles


code:
import enchant
print enchant.list_languages()

rawDescription: How to add dictionary to PyEnchant?

description: Add dictionary to PyEnchant


code: print re.findall(r'#[^#]*#|\S+', 'mark bill #special# #special method# johni')

rawDescription: Regex does not remove the special chars in split function


code: print u'Trentem\u00f8ller'

rawDescription: Unicode being re-unicoded

description: Unicode being re-unicoded


rawDescription: sort names in alphabitical order in mysql using python

description: Sort names in alphabitical order in mysql using python


code:
# column name in your case
columns = ['h' + str(i+1) for i in range(20)]

print next(subtract_group(df1, columns))

rawDescription: python pandas groupby and subtract columns from different groups

description: Python pandas groupby and subtract columns from different groups


rawDescription: Is it possible to scrap images from a webpage, convert them to numbers and save them to a file?


code:
for li in cases:
    print '{}\n\t->{}'.format(li, [w for w in li if w[0].isalpha()])

rawDescription: regex to remove non alpha-words A-Z a-z from a list (exceptions)

description: Regex to remove non alpha-words A-Z a-z from a list (exceptions)


rawDescription: How to compare values in tuples in a list

description: Compare values in tuples in a list


code:
test = {0: {0: 12.33334444, 1: 208.76062847194152, 2: 34.713109915419565}, 1: {0: 84.463009655114703, 1: 20.83266665599966, 2: 237.6299644405141}, 2: {0: 27.018512172212592, 1: 104.38390680559911, 2: 137.70257804413103}}

sorted_index_map = {}

for key, value in test.iteritems():
    sorted_index_map[key] = sorted(value, key=lambda k: value[k])

index_of_min_index_map = {key: 0 for key in test}

need_to_check_duplicate = True

while need_to_check_duplicate:
    need_to_check_duplicate = False
    min_index_map = {key: sorted_index_map[key][i] for key, i in index_of_min_index_map.iteritems()}
    index_set = list(min_index_map.itervalues())
    for key, index in min_index_map.iteritems():
        if index_set.count(index) == 1:
            continue
        else:
            for key_to_check, index_to_check in min_index_map.iteritems():
                if key != key_to_check and index == index_to_check:
                    if test[key][index] > test[key_to_check][index_to_check]:
                        index_of_min_index_map[key] += 1
                        need_to_check_duplicate = True
                        break

result = {key: sorted_index_map[key][i] for key, i in index_of_min_index_map.iteritems()}
print result

rawDescription: python find minimum value in dictionary iteratively

description: Python find minimum value in dictionary iteratively


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

class Teacher(object):
    def __init__(self, id, name):
        self.id = id
        self.name = name

class Classroom(object):
    def __init__(self, children, teachers):
        self.children = IndexedList(children, ('id','name'))
        self.teachers = IndexedList(teachers, ('id','name'))

classroom = Classroom([Child('389','pete')], [Teacher('829','bob')])

print classroom.children[0].name               # -> pete

classroom.children.append(Child('2344','joe'))
print len(classroom.children)                  # -> 2
print classroom.children[1].name               # -> joe
print classroom.children['joe'].id             # -> 2344
print classroom.children['2344'].name          # -> joe

p = classroom.children.pop('pete')
print p.name                                   # -> pete
print len(classroom.children)                  # -> 1

rawDescription: python list/dict property best practice

description: Python list/dict property best practice


code:
rows = [-1] + np.where(df['col1']=='NewEntry')[0].tolist() + [len(df.index)]
[-1, 3L, 5]

rawDescription: Split pandas dataframe by String

description: Split pandas dataframe by String


code:
for column in inspect(Dog).columns:
    print column.primary_key
# True
# False

rawDescription: SQLAlchemy determine whether an Integer attribute is a Foreign Key or not using inspector


code:
data = [1,2,3,4,5,8,4,6,9]
myfunc = test_dll.AddBuffer
myfunc.argtypes = (ctypes.POINTER(ctypes.c_uint8),ctypes.c_uint8)
data_array = ctypes.c_uint8 * len(data)
arg = data_array(*data)
result = myfunc(arg,ctypes.sizeof(arg))
print result
for i in range (0,len(arg)):
     print  arg[i]

rawDescription: Wrapped C library in Python, but returned array is unchanged


code:
for i in lines:
    print i.rstrip('\n')

rawDescription: removing the "\n" for every line except for the last line

description: Removing the "\n" for every line except for the last line


code:
def both_ends(s):
    return s[:2] + s[-2:] if len(s) > 4 else s

lst = ('apple', 'pizza', 'x', 'joke')
print map(both_ends, lst)

rawDescription: How do I loop through each string in my list?

description: Loop through each string in my list


code:
s="['LOCATION', 'NAME', 'JOB', 'AREA')]"
print '\n'.join(tok.strip("[]()' ") for tok in s.split(','))

rawDescription: Python Parseing String by Single Quotes

description: Python Parseing String by Single Quotes


code:
class CallableValue(object):
    def __init__(self, name):
        self.name = name
    def __call__(self, *args, **kwargs):
        print "Lo, {} was called!".format(self.name)

class Bar(object):
    def __getattr__(self, name):
        return CallableValue(name)

rawDescription: Determine if __getattr__ is method or attribute call


code:
f = open('data.txt')
data = []

pattern = "\d+|[IVX]+"
regex = re.compile(pattern)

for line in f:
    matches = re.findall(regex, line)
    data.append(matches)

f.close()

print data
transposed_data = zip(*data)
print transposed_data

for atuple in transposed_data:
    val = atuple[0]

    if all([num==val for num in atuple]):
        next
    else:
        print atuple
        break

rawDescription: Detect and extract changing numbers in list of strings


code:
from lxml import etree
f = etree.parse('foo.xml')  # path to XML file
root = f.getroot() # get the root element
for tags in root.iter(): # iter through the root element
    print tags.tag       # print all the tags

rawDescription: Using lxml to extract data where all elements are not known in advance


code:
a = [b"abc\xf3\x9c\xc6", b"xyz"]
serialised = json.dumps([s.decode("latin1") for s in a])
print [s.encode("latin1") for s in json.loads(serialised)]

rawDescription: Serializing binary data in Python

description: Serializing binary data in Python


code:
def get_response():
    x = TestAsyncHttp()
    ret = IOLoop.current().run_sync(lambda: x.get_response(request_json))
    body = ret.body
    print body['value']

rawDescription: How to get content from tornado future object

description: Get content from tornado future object


code:
t = time.time()
palette = []
for i in xrange(256):
    if i == 127:
        palette.append((255, 255, 255))
    elif i > 127:
        palette.append((0,0,255-i))
    else:
        palette.append((i, 0, 0))
palette = np.array(palette, np.uint8)

h2 = palette[gray]

print time.time() - t

print np.all(h==h2)

rawDescription: Performance comparison of OpenCV-Python interfaces, cv and cv2


code:
line = '....xooo......aaaa...'
import re
print [whole for whole, _ in re.findall("(([^.])\\2+)", line)]

rawDescription: Regex: How to match sequence of SAME characters?


code:
# Adapted from example on http://codespeak.net/lxml/tutorial.html
import lxml.etree as et
import StringIO
s = """<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "cheese">
<!ENTITY eacute "é"> ]>
<root>
<a>&tasty; soufflé</a>
</root>
"""
tree = et.parse(StringIO.StringIO(s))
print et.tostring(tree, xml_declaration=True, encoding="utf-8")

rawDescription: Creating a doctype with lxml's etree

description: Creating a doctype with lxml's etree


code:
import pickle

s = {1,2,3,4}
with open("s.pick","wb") as f: # pickle it to file
    pickle.dump(s,f)

with open("s.pick","rb") as f1:
    un_p = pickle.loads(f1.read()) # unpickle and use

print un_p

set([1, 2, 3, 4])

rawDescription: Python Improve List Compare Speed

description: Python Improve List Compare Speed


code:
import random

def animal():
    print "rawr I'm a tiger"

def vegetable():
    print "Woof, I'm a carrot"

def mineral():
    print "Rumble, I'm a rock"

sections = {
    'animal': animal,
    'vegetable': vegetable,
    'mineral': mineral,
}

section = sections[random.choice(sections.keys())]
section()

rawDescription: I can select randomly from a list... but how do I use this as a variable for different commands? Python


code:
for i in range(8592, 8602):
    print unichr(i),

rawDescription: Python random non alphabetical character


code:
for row in A:
   print fmt%tuple(row)

rawDescription: How to realize a very big int matrix with 5 string columns in numpy (python3)?

description: Realize a very big int matrix with 5 string columns in numpy (python3)


code:
import md5
import base64

def obfuscate(s):
    return base64.b64encode( md5.new(s).digest())

def obfuscate2(s):
    return base64.b16encode( md5.new(s).digest())

# returns alphanumeric string but strings can also include slash, plus or equal i.e. /+=
print obfuscate('Tag 1')
print obfuscate('Tag 2')
print obfuscate('Tag 3')

# return hex string
print obfuscate2('Tag 1')

rawDescription: Encrypt a string in Python. Restrict the characters used to only alphanumeric


code:
l = [[1,2,3,4],[5,6,7,8,9,10],[11,12,13]]

# e.g.: find the index of the list containing 12
# This returns the first match (i.e. using index 0), if you want all matches
# simply remove the `[0]`
print [i for i, lst in enumerate(l) if 12 in lst][0]

rawDescription: Finding the index of an item in a list of lists


code:
for i in range(1, len(data) - 1):
    backward_angle = np.arctan2(data[i][1] - data[i - 1][1], data[i][0] - data[i - 1][0])
    forward_angle = np.arctan2(data[i + 1][1] - data[i][1], data[i + 1][0] - data[i][0])
    angle_difference = (backward_angle - forward_angle + np.pi) % (2 * np.pi) - np.pi
    if np.absolute(angle_difference) > np.pi / 2:
        print "Outlier:", data[i]

rawDescription: python numpy detect and eliminate undesired value

description: Python numpy detect and eliminate undesired value


code:
class X:
    def __init__(self, incoming_list):
        self.data = incoming_list # to keep them for later, if needed
        print incoming_list

X([x for x in range(10)])

rawDescription: How can I call the metaclass's __call__?

description: Call the metaclass's __call__


code:
'\n'.join(lst)
print '\n'.join(lst)

rawDescription: How to add new line as a separate element on a list?

description: Add new line as a separate element on a list


code:
for elt, items in groupby(x, itemgetter(1)):
    print elt, items
    for i in items:
        print i

rawDescription: Sorting and Grouping Nested Lists in Python


code:
strs = "qui8ecc),0*21qxxcd4))"
re.search(r'.*?,0\*[0-9A-F]{2}', strs).group()
m = r.search("kdjrnnj,0*B3;,w0l44")
if m : print m.group()

rawDescription: Python using a regex to find start position of a substring


code:
line = "1,2 3\t4"
print line.replace(',', ' ').split()

rawDescription: Serial data logger, parse on the fly

description: Serial data logger, parse on the fly


code:
from urllib2 import urlopen
from bs4 import BeautifulSoup

url = 'http://hobbyking.com/hobbyking/store/__39036__Turnigy_Multistar_2213_980Kv_14Pole_Multi_Rotor_Outrunner.html'
soup = BeautifulSoup(urlopen(url), 'html5lib')

for element in soup.find(id='productDetails').find_all():
    print element.text

rawDescription: Python and lxml.html get_element_by_id output questions


code:
from wsgiref.handlers import format_date_time
from datetime import datetime
from time import mktime

now = datetime.now()
stamp = mktime(now.timetuple())
print format_date_time(stamp) #--> Wed, 22 Oct 2008 10:52:40 GMT

rawDescription: RFC 1123 Date Representation in Python?

description: RFC 1123 Date Representation in Python


code:
import socket
import select

SERVER_SOCKADDR = ("", 424242)

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.setblocking(0)      # <------------------
server.bind(SERVER_SOCKADDR)
server.listen(5)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.setblocking(0)

result = client.connect_ex(SERVER_SOCKADDR)

rfds, wfds, xfds = select.select([server], [client], [], 1)
if server in rfds:
    print "Server socket: accept does not block"
    sockfd, addr = server.accept()    # sockfd.send() and sockfd.recv() to
                                      # write and read the stream...
    sockfd.setblocking(0)
    print sockfd, addr
else:
    print "Server socket: accept blocks"
if client in wfds:
    print "Client socket: write does not block"
else:
    print "Client socket: write blocks"

server.close()
client.close()

rawDescription: How to launch a process that connects to a socket you're listening on?

description: Launch a process that connects to a socket you're listening on


code:
#!/usr/bin/env python

def check(f, *args, **kwargs):
    def inner(*args, **kwargs):
        print 'checking...'
        return f(*args, **kwargs)
    return inner

class Example(object):

    @check
    def hello(self):
        print 'inside hello'

    @check
    def hi(self):
        print 'inside hi'

if __name__ == '__main__':
    example = Example()
    example.hello()
    example.hi()

rawDescription: what design pattern for pre-do in class function

description: What design pattern for pre-do in class function


code:
from itertools import imap,islice

li = ['sea','mountain','desert',
      'Emma','Cathy','Kate',
      'HH','VVVVVVV','AAA',
      'round','flat','sharp',
      'nut','banana','apple',
      'heeeeeeeeeeen','tiger','snake'
      'red','purple','white',
      'atlantic','pacific','antarctic',
      'Bellini']

print 'len of li == %d\n' % len(li)

def cols_print(li,nc):
    maxel = tuple(max(imap(len,islice(li,st,None,nc)))
                  for st in xrange(nc))

    nblines,tail = divmod(len(li),nc)
    stakes = (nc-1)*['%%-%ds\t'] + ['%%-%ds']
    form = ''.join(stakes) % maxel

    itn = iter(li).next

    print '\n'.join(form % tuple(itn() for g in xrange(nc))
                  for i in xrange(nblines))
    if tail:
        print ''.join(stakes[nc-tail:]) % maxel[0:tail] % tuple(li[-tail:]) + '\n'
    else:
        print

for nc in xrange(3,8):
    cols_print(li,nc)
    print '-----------------------------------------------------------'

rawDescription: Print python list in groups of 3

description: Print python list in groups of 3


code:
rx = '(\w+)=(\S+)|(\S.+?)(?=\w+=|$)'
d = {
    a or 'theSentence': (b or c).strip()
    for a, b, c in re.findall(rx, s)
}
print d

rawDescription: Python tokenize sentence with optional key/val pairs


code:
from asynctimer import AsyncTimer
import time

def run():
    atimer = AsyncTimer()

    atimer.start()

    print 'initial count: ', atimer.get_seconds();

    print 'Now we wait this process,'
    print 'While the child process keeps counting'

    time.sleep(3)

    print '3 seconds later:', atimer.get_seconds();

    atimer.stop()

if __name__ == '__main__':
    run()

rawDescription: While loop is breaking passing of values to a variable within a python process


rawDescription: How to read packet saved in a file with Python?

description: Read packet saved in a file with Python


code:
import numpy as np
import itertools

def polyfit2d(x, y, z, order=3):
    ncols = (order + 1)**2
    G = np.zeros((x.size, ncols))
    ij = itertools.product(range(order+1), range(order+1))
    for k, (i,j) in enumerate(ij):
        G[:,k] = x**i * y**j
    m, _, _, _ = np.linalg.lstsq(G, z)
    return m

def fmt1(x,i):
  if i == 0:
    return ""
  elif i == 1:
    return x
  else:
    return x + '^' + str(i)

def fmt2(i,j):
  if i == 0:
    return fmt1('y',j)
  elif j == 0:
    return fmt1('x',i)
  else:
    return fmt1('x',i) + fmt1('y',j)

def fmtpoly2(m, order):
  for (i,j), c in zip(itertools.product(range(order+1), range(order+1)), m):
    yield ("%f %s" % (c, fmt2(i,j)))

xs = np.array([ 0, 1, 2, 3] )
ys = np.array([ 0, 1, 2, 3] )

zx = np.array([ 0, 2, 6, 12])
zy = np.array([ 1, 3, 5, 7])

mx = polyfit2d(xs, ys, zx, 2)
print "x-component(x,y) = ", ' + '.join(fmtpoly2(mx,2))

my = polyfit2d(xs, ys, zy, 2)
print "y-component(x,y) = ", ' + '.join(fmtpoly2(my,2))

rawDescription: Fitting a polynomial function for a vector field in python


code:
import web

db = web.database(dbn='postgres', db='mydata', user='dbuser', pw='')

results = db.query("SELECT COUNT(*) AS total_users FROM users")
print results[0].total_users # -> prints number of entries in 'users' table

rawDescription: How can I get int result from web python sql query?

description: Get int result from web python sql query


code:
number_list = [919865588453, 919613851649, 919869863504, 123456789012]
number_map = {91986 : 'Area 1', 9196138 : 'Area 2', 9198698 : 'Area 3'}

for number in number_list:
    for prefix, area in sorted(number_map.items(), key=lambda x: len(x[1])):
        if str(number).startswith(str(prefix)):
            print "%d is in %s" % (number, area)
            break
    else:
        print "%d is in no area" % number

rawDescription: Looping a list of numbers against dictionary


code:
#!/home/python
import cgi
import cgitb
cgitb.enable()

print 'Content-type: text/html\r\n\r'
print ''
print '
Please enter a keyword of your choice
'
print '<form action="next.cgi" method="get">'
print 'Keyword: <input type="text" name="keyword">
'
print '<input type="submit" value="Submit" />'
print '</form>'
print ''

rawDescription: CGI form submit button using python

description: CGI form submit button using python


code:
dct_keys = ['one', 'two', 'three', 'four']
dict1 = dict.fromkeys(dct_keys)
print dict1

rawDescription: How to create unique key(keys defined in some list) for dictionary and assign value to it in for loop in python 2.7?

description: Create unique key(keys defined in some list) for dictionary and assign value to it in for loop in python 2.7


code:
class Dummy(object) :
    pass

def getPrice(obj) :
    return obj.price

d0 = Dummy()
d0.price = 56.
d1 = Dummy()
d1.price=16.

d2 = Dummy()
d2.price=786.

d3 = Dummy()
d3.price=5.5

elements = [d0, d1, d2, d3]

print 'Pre-sorting:'
for elem in elements :
    print elem.price

sortedElements = sorted(elements, key=getPrice)

print 'Post-sorting:'
for elem in sortedElements :
    print elem.price

rawDescription: How to sort by a particular attribute of a class?

description: Sort by a particular attribute of a class


code:
from multiprocessing import Process, Queue, current_process
import time

def f(q):
    name = current_process().name
    config = q.get()
    print "%s got config: %s" % (name, config)
    print "%s beginning processing at %s" % (name, time.asctime())
    time.sleep(5)
    print "%s completing processing at %s" % (name, time.asctime())

if __name__ == '__main__':
    q = Queue()
    processes = []
    cfg = { 'my' : 'config', 'data' : 'here' }
    for i in range(3):
        p = Process(target=f, args=(q,))
        processes.append(p)
        p.start()
        q.put(cfg)

    for p in processes:
        p.join()

rawDescription: python multiprocessing manager

description: Python multiprocessing manager


code:
import sys
sys.stdout = open("myLog.log","w")
sys.stderr = open("myLogErr.log","w")
print "stdout test"

rawDescription: Python script from Cron

description: Python script from Cron


code:
for key, group in itertools.groupby(mylist, lambda item: item["mc_no"]):
    print key, sum([item["tmst"] for item in group])

rawDescription: Group dictionary key values in python

description: Group dictionary key values in python


code:
Mapper.py
import subprocess
import sys
from Process import match

for line in sys.stdin:
    key, value = line.split()
    if match(key):
        print value

rawDescription: MapReduce how to allow Mapper to read an xml file for lookup


code:
import sys
import nose
from cStringIO import StringIO

def basic_test():
    print "hello"

if __name__=="__main__":
    module_name = sys.modules[__name__].__file__

    old_stdout = sys.stdout
    sys.stdout = mystdout = StringIO()
    result = nose.run(argv=[sys.argv[0],
                            module_name,
                            '-s'])
    sys.stdout = old_stdout
    print mystdout.getvalue()

rawDescription: Trying to capture verbose stdout output with Nose


code: print most_common(['goose', 'duck', 'duck', 'goose'])

rawDescription: Python most common element in a list

description: Python most common element in a list


code:
from bs4 import BeautifulSoup

data = """

                              **Regular Board Meeting**                      7:00 pm

"""
soup = BeautifulSoup(data)

td = soup.body.td  # or soup.find('td', id='aisd_calendar-2014-04-28-0')
print td['date'].strip('*')

link = soup.find('div', {'class': 'contents'}).a
print link['href']

rawDescription: Navigating an HTML tree in Python

description: Navigating an HTML tree in Python


code:
import requests
from bs4 import BeautifulSoup

markup_str = requests.get("https://gitter.im/numenta/public/archives/2015/08/19").content
soup = BeautifulSoup(markup_str, 'html.parser')

chat_container = soup.find(id='chat-container')
chat_items = chat_container.find_all(class_='chat-item')

for chat_item in chat_items:
    msg = chat_item.find(class_='chat-item__text').get_text().strip()
    poster = chat_item.find(class_='chat-item__from').get_text().strip()
    posted_at = chat_item.find(class_='chat-item__time').get_text().strip()
    print "({}) {}: {}".format(posted_at, poster, msg)

rawDescription: Parsing discussion from gitter

description: Parsing discussion from gitter


code:
import re

for line in st.splitlines():
    m=re.search(r'(^.*,5$)',line)
    if m: print m.group(0)

rawDescription: Python regular expression issues

description: Python regular expression issues


code:
def identity(x):
    return x

def f(n):
    return [i for i in range(1, 10) if (n % i == 0)]

def s(factors):
    return (len(factors) == 2)

def foo(func, helper=identity):
    def innerfunc(n):
        return func(helper(n))
    return innerfunc

a = foo(f)
print a(6)
# [1, 2, 3, 6]
b = foo(s, a)
print b(5)
# True

rawDescription: Python generic method scope

description: Python generic method scope


code:
for k, li in data.items():
    print k, min(li, key=min)

rawDescription: Finding min() and max() of Python dictionary key-value pair complexes


code:
def f(x):
    print x

rawDescription: Running python script in terminal, nothing prints or shows up - why?


code:
import numpy as np
import time

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a[a>0.5] = np.nan
t2 = time.time()
print 'adv. idx.: ',t2-t1

t1 = time.time()
for i in xrange(1000):
        a = np.random.rand(10)
        a = np.where(a>0.5,np.nan,a)
t2 = time.time()
print 'np.where: ',t2-t1

rawDescription: Difference np.where and this work-around?


code:
dates="""\
16th February 2011
4th April 2009
31st December 2007"""

import re
import time

for date in dates.splitlines():
    p=re.findall(r'(\d+)\w\w (\w+) (\d\d\d\d)',date)
    d=time.strptime(' '.join(p[0]),'%d %B %Y')
    iso=time.strftime('%Y%m%d',d)
    print "{0:>20} =>{1:>30} =>{2:>15}".format(date,p,iso)

rawDescription: Change a buch of dates, regex and python


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

def main():
    app = QApplication(sys.argv)
    w = MyWindow()
    w.show()
    sys.exit(app.exec_())

class MyWindow(QWidget):
    def __init__(self, *args):
        QWidget.__init__(self, *args)

        # create table
        list_data = [1,2,3,4]
        lm = MyListModel(list_data, self)
        lv = MyListView()
        lv.setModel(lm)
        lv.clicked.connect(self.item_clicked)
        self.lv = lv

        # layout
        layout = QVBoxLayout()
        layout.addWidget(lv)
        self.setLayout(layout)

    def item_clicked(self, index):
        print "row=", index.row(), "button=", self.lv._mouse_button

class MyListView(QListView):
    def mousePressEvent(self, event):
        self._mouse_button = event.button()
        super(MyListView, self).mousePressEvent(event)

class MyListModel(QAbstractListModel):
    def __init__(self, datain, parent=None, *args):
        """ datain: a list where each item is a row
        """
        QAbstractListModel.__init__(self, parent, *args)
        self.listdata = datain

    def rowCount(self, parent=QModelIndex()):
        return len(self.listdata)

    def data(self, index, role):
        if index.isValid() and role == Qt.DisplayRole:
            return QVariant(self.listdata[index.row()])
        else:
            return QVariant()

if __name__ == "__main__":
    main()

rawDescription: Catch which mousebutton is pressed on item


code:
result = []
for k in d:
    temp = tuple()
    fir, sec = zip(*d[k])
    fir, sec = sum(map(float, fir)), reduce(lambda i,j: float(i)-float(j), sec)
    result.append((k,) + (fir, sec))
print result

rawDescription: Adding values corresponding to one key in python dictionary


code:
while True:
    files = dict ([(f, None) for f in os.listdir(path_to_watch)])
    if len(files) > 0:
        print "You have %s new file/s!" % len(files)
    time.sleep(20)

rawDescription: How much CPU should a Python time.sleep(n) call use


code:
class Date:
    month = int
    day = int
    year = int
    def setDate(self, month, day, year):
        HIGH_MONTH = 12
        HIGHEST_DAYS = [None,31,29,31,30,31,30,31,31,30,31,30,31]
        if month > HIGH_MONTH:
            month = HIGH_MONTH
        elif month < 1:
            month = 1
        else:
            month = month
        if day > HIGHEST_DAYS[month]:
            day = HIGHEST_DAYS[month]
        elif day < 1:
            day = 1
        else:
            day = day
        self.year = year
        self.month = month
        self.day = day
    def showDate(self):
        print 'Date:',self.month,'/',self.day,'/',self.year

rawDescription: Passing an object of a class to a method in the class


code:
try:
    testCase()
except noIdentifier as e:
    print e.message # or use print str(e)

rawDescription: Exit a Python Program (From a Function) with a Successful Run Result


code:
result = []
for j in range(len(xs[0])):
    result.append( [] )
    for i in range(len(xs)):
        result[j].append(xs[i][j])

print result

rawDescription: List Comprehension Running Sequence

description: List Comprehension Running Sequence


code:
for item in data:
    if item['id'] == 'mike':
        print item['licenses']
        break

rawDescription: Python: Retrieve result from inner dictionary


code:
fr = {}
inp = open("file.txt",'r').read().strip().split('\n')
for line in map(str.split,inp):
    k = int(line[0])
    l = float(line[-1])
    key = abs(k-1) / 10 * 10

    if fr.has_key(key):
        fr[key] += l
    else:
        fr[key] = l

for k in sorted(fr.keys()):
    sum = fr[k]
    print '%d-%d\t%f' % (k+1 if k else 0, k+10, sum)

rawDescription: how to group a data in python

description: Group a data in python


code:
class Loader(type):
    def __new__(cls, name, bases, attrs):
        if attrs.get('__init__'):
            attrs['_init'] = attrs['__init__']
            del attrs['__init__']
        if attrs.get('_init_'):
            attrs['__init__'] = lambda self: self._init_()
            attrs['_init'] = lambda self: None
        return super(Loader, cls).__new__(cls, name, bases, attrs)

class Super(object):
    __metaclass__ = Loader
    def _init_(self):
        print 'init super'
        self._init()
        self.load()

    def load(self):
        print 'load super'

class Sub(Super):
    def __init__(self):
        print 'init sub'

    def load(self):
        super(Sub, self).load()
        print 'load sub'

sub = Sub()
sup = Super()

rawDescription: Calling load functions in correct order from constructors


rawDescription: Python: Replace ith occurence of x with ith element in list


code:
print 1 == True
# True
print 0 == False
# True

rawDescription: How to shorten this if and elif code in Python

description: Shorten this if and elif code in Python


code:
import balanced

key = balanced.APIKey().save()
balanced.configure(key.secret)
balanced.Marketplace().save()

buyer = balanced.Account(email_address='buyer@example.org').save()

# add a new card
card = balanced.Marketplace.my_marketplace.create_card(
    name='First card',
    card_number='5105105105105100',
    expiration_month='12',
    expiration_year='2020',
    security_code='123',
)

buyer.add_card(card.uri)

# add another valid card
card = balanced.Marketplace.my_marketplace.create_card(
    name='Second card',
    card_number='5105105105105100',
    expiration_month='12',
    expiration_year='2020',
    security_code='123',
)

buyer.add_card(card.uri)

amount_in_cents = 100  # $1.00

first_card = buyer.cards[0]
second_card = buyer.cards[1]

# charge a specific card
debit = buyer.debit(amount_in_cents, source_uri=first_card.uri)
debit2 = buyer.debit(amount_in_cents, source_uri=second_card.uri)

assert debit.source.uri == first_card.uri
assert debit2.source.uri == second_card.uri

# show all attributes on an object
print first_card.__dict__.keys()

rawDescription: Updating credit card or allowing user to use different card in balanced payments


code:
class MetaHandler(type):
    def __new__(cls, name, bases, attrs):
        instance = type.__new__(cls, name, bases, attrs)
        super_instance = super(instance, instance)
        if hasattr(super_instance, 'prepare') and hasattr(instance, 'prepare'):
            super_prepare = getattr(super_instance, 'prepare')
            sub_prepare = getattr(instance, 'prepare')
            def new_prepare(self):
                super_prepare(self)
                sub_prepare(self)
            setattr(instance, 'prepare', new_prepare)
        return instance

class BaseHandler(object):
    __metaclass__ = MetaHandler
    def prepare(self):
        print 'BaseHandler.prepare'

class SubHandler(BaseHandler):
    def prepare(self):
        print 'SubHandler.prepare'

rawDescription: In python, how can I ensure that one of my class's methods is always called even if a subclass overrides it?


code:
s = ['2-3-1-*-*','2-3-*-*-*','2-1-*-*-*','1-4-3-*-*','2-3-2-*-*','2-1-3-*-*','1-1-*-*-*','2-3-1-1-*']

def isSubElement(subelement, element):
    return (subelement != element) and all([(e1 == e2) or (e1 == "*" and e2 != "*") for e1, e2 in zip(element.split('-'), subelement.split('-'))])

def parseTree(elementList):
    if len(elementList) == 0:
        return {}
    elements = elementList[:]
    d = {}
    for element1 in elements:
        parent = True
        for element2 in elements:
            if isSubElement(element1, element2):
                parent = False
                break
        if parent:
            d[element1] = {}
    for element1 in d.keys():
        d[element1] = parseTree([element for element in elements if isSubElement(element, element1)])
    return d

print parseTree(s)

rawDescription: How to group a list in python?

description: Group a list in python


code:
import csv
sample = '''\
Name,Surname,Address1,Address2,email,etc
Adam1,Smith1,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
Adam2,Smith2,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
Adam3,Smith3,12 Connaugh Rd.,,adamsmith@gmail.com,etc...
'''
reader = csv.DictReader(sample.splitlines())
print next(reader)
print next(reader)
print next(reader)

rawDescription: reading of csv into dictionary, first line becomes the name

description: Reading of csv into dictionary, first line becomes the name


code:
import fileinput
import sys

def update_hosts(hostname, ip):

    if 'linux' in sys.platform:
        hosts_path = '/etc/hosts'
    else:
        hosts_path = 'c:\windows\system32\drivers\etc\hosts'

    for line in fileinput.input(hosts_path, inplace=True):
        if hostname in line:
            print "{0}\t{1}".format(hostname, ip)
        else:
            print line.strip()

if __name__ == '__main__':
    hostname = sys.argv[1]
    ip = sys.argv[2]
    update_hosts(hostname, ip)
    print "Done!"

rawDescription: Scrapyd deploy project on a server with dynamic ip


code:
for review in tree.xpath('//p[@itemprop="description"]'):
    print review.text_content()  # alternatively: ' '.join(review.xpath('text()'))
    print "----"

rawDescription: Parsing Yelp using lxml - ignore html tag


code:
def status(options=[]):
    options.append('new_option')
    return options

print status()
print status()
print status()

rawDescription: Mutable Default Method Arguments In Python


code:
import re
from bs4 import BeautifulSoup

html = """

My HomePage
Sections
So nice to be home

"""

soup = BeautifulSoup(html)
for a in soup.find_all("a", text=re.compile('Home', re.IGNORECASE)):
    print a['href']

rawDescription: Extract links if anchor text contains keyword


code:
from selenium import webdriver
from selenium.webdriver.support.select import Select

# initialize webdriver instance and visit url
url = "http://clerk.house.gov/public_disc/financial-search.aspx"
browser = webdriver.Firefox()
browser.get(url)

# find select tag and select 2008
select = Select(browser.find_element_by_id('ctl00_cphMain_txbFiling_year'))
select.select_by_value('2008')

# find "search" button and click it
button = browser.find_element_by_id('ctl00_cphMain_btnSearch')
button.click()

# display results
table = browser.find_element_by_id('search_results')
for row in table.find_elements_by_tag_name('tr')[1:-1]:
    print [cell.text for cell in row.find_elements_by_tag_name('td')]

# close the browser
browser.close()

rawDescription: Scraping aspx with Python mechanize - getting search results


code:
data = """\
. 55 MORILLO ZONE VIII,
BARANGAY ZONE VIII
(POB.), LUISIANA, LAGROS
F
01/16/1952
ALOMO, TERESITA CABALLES
3412-00000-A1652TCA2
12
. 22 FABRICANTE ST. ZONE
VIII LUISIANA LAGROS,
BARANGAY ZONE VIII
(POB.), LUISIANA, LAGROS
M
10/14/1967
AMURAO, CALIXTO MANALO13
"""

from pyparsing import LineEnd, oneOf, Word, nums, Combine, restOfLine, \
    alphanums, Suppress, empty, originalTextFor, OneOrMore, alphas, \
    Group, ZeroOrMore

NL = LineEnd().suppress()
gender = oneOf("M F")
integer = Word(nums)
date = Combine(integer + '/' + integer + '/' + integer)

# define the simple line definitions
gender_line = gender("sex") + NL
dob_line = date("DOB") + NL
name_line = restOfLine("name") + NL
id_line = Word(alphanums+"-")("ID") + NL
recnum_line = integer("recnum") + NL

# define forms of address lines
first_addr_line = Suppress('.') + empty + restOfLine + NL
# a subsequent address line is any line that is not a gender definition
subsq_addr_line = ~(gender_line) + restOfLine + NL

# a line with a name and a recnum combined, if there is no ID
name_recnum_line = originalTextFor(OneOrMore(Word(alphas+',')))("name") + \
    integer("recnum") + NL

# defining the form of an overall record, either with or without an ID
record = Group((first_addr_line + ZeroOrMore(subsq_addr_line))("address") +
    gender_line +
    dob_line +
    ((name_line +
        id_line +
        recnum_line) |
      name_recnum_line))

# parse data
records = OneOrMore(record).parseString(data)

# output the desired results (note that address is actually a list of lines)
for rec in records:
    if rec.ID:
        print "%(name)s, %(ID)s, %(address)s, %(sex)s, %(DOB)s" % rec
    else:
        print "%(name)s, , %(address)s, %(sex)s, %(DOB)s" % rec
print

# how to access the individual fields of the parsed record
for rec in records:
    print rec.dump()
    print rec.name, 'is', rec.sex
    print

rawDescription: Parsing unstructured text in Python

description: Parsing unstructured text in Python


code:
a,b,c = map(list,map(None,*l))
print a
print b
print c
a,b,c = map(list, zip(*l))
print a
print b
print c

rawDescription: Pass each nested list as an arg to map

description: Pass each nested list as an arg to map


rawDescription: Print Soap Body data using lxml

description: Print Soap Body data using lxml


code:
sql = 'SELECT * FROM table WHERE col = %(p)s OR (col IS NULL AND %(p)s IS NULL)'
possible_params = [{"p":1}, {"p":None}]

for x in possible_params:
    print cur.mogrify(sql,(x,))

rawDescription: pyscopg2 select NULL values

description: Pyscopg2 select NULL values


code:
def pants():
    print "Put on some pants!"

def shorts():
    print "And don't forget your underwear!"

zap = pants
thing = shorts

rawDescription: Why does foo = function() run the function in Python?


code:
class BeginningPrinter(Protocol):
    def __init__(self, finished):
        self.finished = finished
        self.remaining = 1024 * 10
        self.total_response = ""  # This will store the response.

    def dataReceived(self, bytes):
        if self.remaining:
            display = bytes[:self.remaining]
            self.total_response += display  # Append to our response.
            self.remaining -= len(display)

    def connectionLost(self, reason):
        print 'Finished receiving body:', reason.getErrorMessage()
        print 'response is ',self.total_response
        self.finished.callback(self.total_response)

rawDescription: twisted Get response data at client

description: Twisted Get response data at client


code:
class M1(type):
    def __new__(meta, name, bases, atts):
        print "M1 called for " + name
        return super(M1, meta).__new__(meta, name, bases, atts)
class C1(object):
    __metaclass__ = M1
type(C1)
class Sub1(C1):
    pass
type(Sub1)

rawDescription: Customary To Inherit Metaclasses From type?


rawDescription: Weighted output by comparing strings in python


code:
for riga in array:
    if line.strip() != '':
        print riga

rawDescription: Print line of a txt file in python create break line when print


code:
#location_regexes.py
import re
paren_pattern = re.compile(r"([^(]+, )?([^(]+?),? \(([^)]+)\)")

def parse_city_state(locations_list):
    city_list = []
    state_list = []
    coordinate_pairs = []
    for location in locations_list:
        if '(' in location:
            r = re.match(paren_pattern, location)
            city_list.append(r.group(2))
            state_list.append(r.group(3))
        elif location[0].isdigit() or location[0] == '-':
            coordinate_pairs.append(location.split(', '))
        else:
            city_list.append(location.split(', ', 1)[0])
            state_list.append(location.split(', ', 1)[1])
    return city_list, state_list, coordinate_pairs

#to demonstrate output
if __name__ == "__main__":
    locations = ['Washington, DC', 'Miami, FL', 'New York, NY',
                'Kaslo/Nelson area (Canada), BC', 'Plymouth (UK/England)',
                'Mexico, DF - outskirts-, (Mexico),', '38.206471, -111.165271']

    for parse_group in parse_city_state(locations):
        print parse_group

rawDescription: Split string elements of a list with multiple separators/conditions. Any good Python library?


code:
import dis
def foo():
    def bar():
        pass
    print 'boo!'

dis.dis(foo)

rawDescription: Does creating functions inside functions have a recurring cost?


code:
def worker(self):
    while self.progress_bar.value() < 10:
        self.update_signal.emit(self.progress_bar.value()+1)
        v = 0
        print "Starting Add"
        for i in xrange(5000000):
            v = v+random.uniform(0, 100)
        print "End of Add"

rawDescription: How can I print to QTextEdit to mimic printing to console? (Python 3.3)

description: Print to QTextEdit to mimic printing to console? (Python 3.3)


code:
import functools

def decorator(f):
    """
    Method decorator specific to the instance.

    Uses a special descriptor to delay the definition of the method wrapper.
    """
    class SpecialDescriptor(object):
        def __init__(self, f):
            self.f = f

        def __get__(self, instance, cls):
            if instance is None:  # unbound method request?
                return self.make_unbound(cls)
            return self.make_bound(instance)

        def make_unbound(self, cls):
            @functools.wraps(self.f)
            def wrapper(*args, **kwargs):
                raise TypeError('unbound method {}() must be called with {} '
                                'instance as first argument'.format(
                                                                self.f.__name__,
                                                                cls.__name__))
            return wrapper

        def make_bound(self, instance):
            @functools.wraps(self.f)
            def wrapper(*args, **kwargs):
                save = getattr(instance, '_FUNC_', None)
                instance._FUNC_ = getattr(instance, self.f.__name__)
                retval = self.f(instance, *args, **kwargs)
                if save:  instance._FUNC_ = save  # restore any previous value
                else: delattr(instance, '_FUNC_')
                return retval

            # instance no longer needs special descriptor, since method is now
            # wrapped, so make it call the wrapper directly from now on
            setattr(instance, self.f.__name__, wrapper)
            return wrapper

    return SpecialDescriptor(f)

class C(object):
    @decorator
    def M(self, i):
        if i > 0:
            print i,
            self._FUNC_(i-1)  # No explicit 'self' argument required
        else:
            print '- Blast Off!'

C().M(3)  # -> 3 2 1 - Blast Off!

rawDescription: Python:method call itself without method name


code:
import itertools

probs = ([0.1, 0.1, 0.2],
         [0.7, 0.9],
         [0.5, 0.4, 0.1])

for prob in itertools.product(*probs):
    print prob
    # prob is a tuple containing one combination of the variables
    # from each of the input lists, do with it what you will

rawDescription: Nested for loops to recursive function in Python


code:
import numpy
import scipy.integrate
import math

def w(r, theta, phi, alpha, beta, gamma):
    return(-math.log(theta * beta))

def integrand(phi, alpha, gamma, r, theta, beta):
    ww = w(r, theta, phi, alpha, beta, gamma)
    k = 1.
    T = 1.
    return (math.exp(-ww/(k*T)) - 1.)*r*r*math.sin(beta)*math.sin(theta)

# limits of integration

def zero(x, y=0):
    return 0.

def one(x, y=0):
    return 1.

def pi(x, y=0):
    return math.pi

def twopi(x, y=0):
    return 2.*math.pi

# integrate over phi [0, Pi), alpha [0, 2 Pi), gamma [0, 2 Pi)
def secondIntegrals(r, theta, beta):
    res, err = scipy.integrate.tplquad(integrand, 0., 2.*math.pi, zero, twopi, zero, pi, args=(r, theta, beta))
    return res

# integrate over r [0, 1), beta [0, 2 Pi), theta [0, 2 Pi)
def integral():
    return scipy.integrate.tplquad(secondIntegrals, 0., 2.*math.pi, zero, twopi, zero, one)

expected = 16*math.pow(math.pi,5)/3.
result, err = integral()
diff = abs(result - expected)

print "Result = ", result, " estimated error = ", err
print "Known result = ", expected, " error = ", diff, " = ", 100.*diff/expected, "%"

rawDescription: Integrating a multidimensional integral in scipy


code:
import numpy as np
from scipy import stats
import statsmodels.api as sm

# generate some data to check
nobs = 1000
n, p = 50, 0.25
dist0 = stats.nbinom(n, p)
y = dist0.rvs(size=nobs)
x = np.ones(nobs)

loglike_method = 'nb1'  # or use 'nb2'
res = sm.NegativeBinomial(y, x, loglike_method=loglike_method).fit(start_params=[0.1, 0.1])

print dist0.mean()
print res.params

mu = res.predict()   # use this for mean if not constant
mu = np.exp(res.params[0])   # shortcut, we just regress on a constant
alpha = res.params[1]

if loglike_method == 'nb1':
    Q = 1
elif loglike_method == 'nb2':
    Q = 0

size = 1. / alpha * mu**Q
prob = size / (size + mu)

print 'data generating parameters', n, p
print 'estimated params          ', size, prob

#estimated distribution
dist_est = stats.nbinom(size, prob)

rawDescription: statsmodels - plotting the fitted distribution

description: Statsmodels - plotting the fitted distribution


code:
import hashlib
print hashlib.md5(str(sorted({1 : 2, 3 : 4}.items()))).hexdigest()

rawDescription: Python: Compare dict with a static reference?


code:
import json
import re
import urllib2

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

print menu

rawDescription: Parse the JavaScript returned from BeautifulSoup


code:
import csv

myDict = {}
row_number = 0

with open('some.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=' ')
    for row in reader:
        row_number +=1
        if row[0] in myDict:
            myDict[row[0]] += 1
        else:
            myDict[row[0]] = 1

for word in myDict:
    print word, float(myDict[word])/row_number

rawDescription: Python How to calculate percentage of a text found in every row


code: print soup.find('input', attrs={'name': 'T1'})['value']

rawDescription: Can't get a regex pattern to work in Python


code: print ", ".join(data[:-2] + [" and ".join(data[-2:])])

rawDescription: Grammatical List Join in Python

description: Grammatical List Join in Python


rawDescription: Weird Extra Looping

description: Weird Extra Looping


code:
kain = ColoredRectangle(eve, color="blue")
abel = ColoredRectangle(eve, color="red")
print kain.color, abel.color
#blue red

rawDescription: Subclassing in python of instantiated superclass


code:
class AssignableSlice(list):
    def __setitem__(self, i, v):
        if isinstance(i, slice):
            for ii in xrange(*i.indices(len(self))):
                self[ii] = v
        else:
            super(AssignableSlice, self).__setitem__(i, v)

a = AssignableSlice(range(10))
print a
a[::2] = 3
print a
a[1::3] = 99
print a

rawDescription: Access certain elements of a list

description: Access certain elements of a list


code:
d = dict()

for record in result:
    objectname = 'Customer' + str(record[0])
    customername = str(record[1])
    d[objectname] = Customer(customername)

print d

rawDescription: How do I assign a variable to an object name?

description: Assign a variable to an object name


rawDescription: python JSON only get keys in first level

description: Python JSON only get keys in first level


rawDescription: adding text to a line in a file

description: Adding text to a line in a file


code:
# mod1.py
print 'starting mod1'
import mod3
print 'finished mod1'

# mod2.py
print 'starting mod2'
import mod3
print 'finished mod2'

# mod3.py
print 'in mod3'

rawDescription: how to avoid shared module dependency in python

description: Avoid shared module dependency in python


code:
for k,x in parse_qs(urlparse('blahdeblahdeblah?query=This is the query&time=8:30')[4]).items():
    print '%s=%s' % (k, x)

rawDescription: Need to parse something like a GET request in python


code:
def perform_task(callback=None):
    print 'doing stuff'
    if callback is not None:
        callback('x', 'y', z='z')

rawDescription: lambda *args, **kwargs: None

description: Lambda *args, **kwargs: None


code:
import socket, pickle

def socket_to_file(sock):
    try:
        sock.settimeout(15)
        return sock.makefile()
    finally:
        sock.close()

if __name__ == '__main__':
    sock = socket.create_connection(('localhost', 3005))
    sock = socket_to_file(sock)
    try:
        request = {
            'param0': True,
            'param2': 'sztringecske',
        }
        print 'Sending request: %s' % (request,)
        pickle.dump(request, sock)
        sock.flush()    # important!!!
        response = pickle.load(sock)
        print 'Received response: %s' % (response,)
    finally:
        sock.close()

rawDescription: Python working with sockets the method recv


code:
mystuff = {'apple': "I AM APPLES!"}
print mystuff['apple']

rawDescription: Python module object without a file

description: Python module object without a file


code:
with open("inputfile") as f:
    rows = zip(*[line.split() for line in f])

print rows[0]

rawDescription: Grabbing values from within a list of string arrays?


code:
for tuple2 in tuples2:
    for tuple1 in tuples1:
        if tuple1[0] == tuple2[0]:
            print tuple2

rawDescription: Comparing two tuples in two different file


code:
import sys

import numpy as np
import cv2

im = cv2.imread('pitrain.png')
im3 = im.copy()

gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
thresh = cv2.adaptiveThreshold(blur,255,1,1,11,2)

#################      Now finding Contours         ###################

contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)

samples =  np.empty((0,100))
responses = []
keys = [i for i in range(48,58)]

for cnt in contours:
    if cv2.contourArea(cnt)>50:
        [x,y,w,h] = cv2.boundingRect(cnt)

        if  h>28:
            cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
            roi = thresh[y:y+h,x:x+w]
            roismall = cv2.resize(roi,(10,10))
            cv2.imshow('norm',im)
            key = cv2.waitKey(0)

            if key == 27:  # (escape to quit)
                sys.exit()
            elif key in keys:
                responses.append(int(chr(key)))
                sample = roismall.reshape((1,100))
                samples = np.append(samples,sample,0)

responses = np.array(responses,np.float32)
responses = responses.reshape((responses.size,1))
print "training complete"

np.savetxt('generalsamples.data',samples)
np.savetxt('generalresponses.data',responses)

rawDescription: Simple Digit Recognition OCR in OpenCV-Python


code:
a = [1]
b = a
b[0] = 'foo'
print a  #What?? 'a' changed?

rawDescription: Python: copy of a variable

description: Python: copy of a variable


code:
users = User.objects.filter(name__iregex=r'^[A-Z]')
for user in users:
    print user.name

rawDescription: How many time can django objects.filter Q() be used in one query?


code:
import StringIO
import test

buf=StringIO.StringIO("666")
print test.foo(buf)

rawDescription: Python & SWIG: Using StringIO in place of FILE*


code:
text = """  your text as above """
p1=0
output=""
while True:
    p2 = text.find("Data Record",p1)+11
    p3 = text.find("Data Stop", p1)
    if p2<0 or p3<0: break
    s = text[p2:p3]
    output +=s
    p1=p3+10

print output

rawDescription: Is there a command choose where you want to start and stop parsing data in a text file?